use carbon_core::instruction::InstructionMetadata;
use carbon_core::postgres::metadata::InstructionRowMetadata;
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct SignV1Row {
#[sqlx(flatten)]
pub instruction_metadata: InstructionRowMetadata,
}
impl SignV1Row {
pub fn from_parts(
_source: crate::instructions::sign_v1::SignV1,
metadata: InstructionMetadata,
) -> Self {
Self {
instruction_metadata: metadata.into(),
}
}
}
impl TryFrom<SignV1Row> for crate::instructions::sign_v1::SignV1 {
type Error = carbon_core::error::Error;
fn try_from(_source: SignV1Row) -> Result<Self, Self::Error> {
Ok(Self {})
}
}
impl carbon_core::postgres::operations::Table for crate::instructions::sign_v1::SignV1 {
fn table() -> &'static str {
"sign_v1_instruction"
}
fn columns() -> Vec<&'static str> {
vec![
"__signature",
"__instruction_index",
"__stack_height",
"__slot",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for SignV1Row {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO sign_v1_instruction (
__signature, __instruction_index, __stack_height, __slot
) VALUES (
$1, $2, $3, $4
)"#,
)
.bind(self.instruction_metadata.signature.clone())
.bind(self.instruction_metadata.instruction_index)
.bind(self.instruction_metadata.stack_height)
.bind(self.instruction_metadata.slot.clone())
.execute(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(())
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Upsert for SignV1Row {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO sign_v1_instruction (
__signature, __instruction_index, __stack_height, __slot
) VALUES (
$1, $2, $3, $4
) ON CONFLICT (
__signature, __instruction_index, __stack_height
) DO UPDATE SET
__instruction_index = EXCLUDED.__instruction_index,
__stack_height = EXCLUDED.__stack_height,
__slot = EXCLUDED.__slot
"#,
)
.bind(self.instruction_metadata.signature.clone())
.bind(self.instruction_metadata.instruction_index)
.bind(self.instruction_metadata.stack_height)
.bind(self.instruction_metadata.slot.clone())
.execute(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(())
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Delete for SignV1Row {
type Key = (
String,
carbon_core::postgres::primitives::U32,
carbon_core::postgres::primitives::U32,
);
async fn delete(key: Self::Key, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"DELETE FROM sign_v1_instruction WHERE
__signature = $1 AND __instruction_index = $2 AND __stack_height = $3
"#,
)
.bind(key.0)
.bind(key.1)
.bind(key.2)
.execute(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(())
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::LookUp for SignV1Row {
type Key = (
String,
carbon_core::postgres::primitives::U32,
carbon_core::postgres::primitives::U32,
);
async fn lookup(
key: Self::Key,
pool: &sqlx::PgPool,
) -> carbon_core::error::CarbonResult<Option<Self>> {
let row = sqlx::query_as(
r#"SELECT * FROM sign_v1_instruction WHERE
__signature = $1 AND __instruction_index = $2 AND __stack_height = $3
"#,
)
.bind(key.0)
.bind(key.1)
.bind(key.2)
.fetch_optional(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(row)
}
}
pub struct SignV1MigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for SignV1MigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS sign_v1_instruction (
-- Instruction data
-- Instruction metadata
__signature TEXT NOT NULL,
__instruction_index BIGINT NOT NULL,
__stack_height BIGINT NOT NULL,
__slot NUMERIC(20),
PRIMARY KEY (__signature, __instruction_index, __stack_height)
)"#,
)
.execute(connection)
.await?;
Ok(())
}
async fn down(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(r#"DROP TABLE IF EXISTS sign_v1_instruction"#)
.execute(connection)
.await?;
Ok(())
}
}