use {
crate::types::SignedMsgOrderId,
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U32},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct SignedMsgUserOrdersRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub authority_pubkey: Pubkey,
pub padding: U32,
pub signed_msg_order_data: sqlx::types::Json<Vec<SignedMsgOrderId>>,
}
impl SignedMsgUserOrdersRow {
pub fn from_parts(
source: crate::accounts::signed_msg_user_orders::SignedMsgUserOrders,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
authority_pubkey: source.authority_pubkey.into(),
padding: source.padding.into(),
signed_msg_order_data: sqlx::types::Json(source.signed_msg_order_data.to_vec()),
}
}
}
impl TryFrom<SignedMsgUserOrdersRow>
for crate::accounts::signed_msg_user_orders::SignedMsgUserOrders
{
type Error = carbon_core::error::Error;
fn try_from(source: SignedMsgUserOrdersRow) -> Result<Self, Self::Error> {
Ok(Self {
authority_pubkey: *source.authority_pubkey,
padding: source.padding.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
signed_msg_order_data: source.signed_msg_order_data.0,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::signed_msg_user_orders::SignedMsgUserOrders
{
fn table() -> &'static str {
"signed_msg_user_orders_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"authority_pubkey",
"padding",
"signed_msg_order_data",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for SignedMsgUserOrdersRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO signed_msg_user_orders_account (
"authority_pubkey",
"padding",
"signed_msg_order_data",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5
)"#,
)
.bind(self.authority_pubkey)
.bind(self.padding)
.bind(&self.signed_msg_order_data)
.bind(self.account_metadata.pubkey)
.bind(&self.account_metadata.slot)
.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 SignedMsgUserOrdersRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO signed_msg_user_orders_account (
"authority_pubkey",
"padding",
"signed_msg_order_data",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"authority_pubkey" = EXCLUDED."authority_pubkey",
"padding" = EXCLUDED."padding",
"signed_msg_order_data" = EXCLUDED."signed_msg_order_data",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.authority_pubkey)
.bind(self.padding)
.bind(&self.signed_msg_order_data)
.bind(self.account_metadata.pubkey)
.bind(&self.account_metadata.slot)
.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 SignedMsgUserOrdersRow {
type Key = carbon_core::postgres::primitives::Pubkey;
async fn delete(key: Self::Key, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"DELETE FROM signed_msg_user_orders_account WHERE
__pubkey = $1
"#,
)
.bind(key)
.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 SignedMsgUserOrdersRow {
type Key = carbon_core::postgres::primitives::Pubkey;
async fn lookup(
key: Self::Key,
pool: &sqlx::PgPool,
) -> carbon_core::error::CarbonResult<Option<Self>> {
let row = sqlx::query_as(
r#"SELECT * FROM signed_msg_user_orders_account WHERE
__pubkey = $1
"#,
)
.bind(key)
.fetch_optional(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(row)
}
}
pub struct SignedMsgUserOrdersMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for SignedMsgUserOrdersMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS signed_msg_user_orders_account (
-- Account data
"authority_pubkey" BYTEA NOT NULL,
"padding" INT8 NOT NULL,
"signed_msg_order_data" JSONB NOT NULL,
-- Account metadata
__pubkey BYTEA NOT NULL,
__slot NUMERIC(20),
PRIMARY KEY (__pubkey)
)"#,
)
.execute(connection)
.await?;
Ok(())
}
async fn down(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(r#"DROP TABLE IF EXISTS signed_msg_user_orders_account"#)
.execute(connection)
.await?;
Ok(())
}
}