use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U8},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct ReferrerAliasRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub nonce: U8,
pub alias: Vec<u8>,
pub referrer: Pubkey,
}
impl ReferrerAliasRow {
pub fn from_parts(
source: crate::accounts::referrer_alias::ReferrerAlias,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
nonce: source.nonce.into(),
alias: source.alias.to_vec(),
referrer: source.referrer.into(),
}
}
}
impl TryFrom<ReferrerAliasRow> for crate::accounts::referrer_alias::ReferrerAlias {
type Error = carbon_core::error::Error;
fn try_from(source: ReferrerAliasRow) -> Result<Self, Self::Error> {
Ok(Self {
nonce: source.nonce.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
alias: source.alias.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 15 bytes"
.to_string(),
)
})?,
referrer: *source.referrer,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::referrer_alias::ReferrerAlias {
fn table() -> &'static str {
"referrer_alias_account"
}
fn columns() -> Vec<&'static str> {
vec!["__pubkey", "__slot", "nonce", "alias", "referrer"]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for ReferrerAliasRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO referrer_alias_account (
"nonce",
"alias",
"referrer",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5
)"#,
)
.bind(self.nonce)
.bind(&self.alias)
.bind(self.referrer)
.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 ReferrerAliasRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO referrer_alias_account (
"nonce",
"alias",
"referrer",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"nonce" = EXCLUDED."nonce",
"alias" = EXCLUDED."alias",
"referrer" = EXCLUDED."referrer",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.nonce)
.bind(&self.alias)
.bind(self.referrer)
.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 ReferrerAliasRow {
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 referrer_alias_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 ReferrerAliasRow {
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 referrer_alias_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 ReferrerAliasMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for ReferrerAliasMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS referrer_alias_account (
-- Account data
"nonce" INT2 NOT NULL,
"alias" BYTEA NOT NULL,
"referrer" BYTEA 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 referrer_alias_account"#)
.execute(connection)
.await?;
Ok(())
}
}