use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64, U8},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct PlatformGlobalAccessRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub bump: U8,
pub global_config: Pubkey,
pub platform_config: Pubkey,
pub padding: Vec<U64>,
}
impl PlatformGlobalAccessRow {
pub fn from_parts(
source: crate::accounts::platform_global_access::PlatformGlobalAccess,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
bump: source.bump.into(),
global_config: source.global_config.into(),
platform_config: source.platform_config.into(),
padding: source
.padding
.into_iter()
.map(|element| element.into())
.collect(),
}
}
}
impl TryFrom<PlatformGlobalAccessRow>
for crate::accounts::platform_global_access::PlatformGlobalAccess
{
type Error = carbon_core::error::Error;
fn try_from(source: PlatformGlobalAccessRow) -> Result<Self, Self::Error> {
Ok(Self {
bump: source.bump.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
global_config: *source.global_config,
platform_config: *source.platform_config,
padding: source
.padding
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::platform_global_access::PlatformGlobalAccess
{
fn table() -> &'static str {
"platform_global_access_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"bump",
"global_config",
"platform_config",
"padding",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for PlatformGlobalAccessRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO platform_global_access_account (
"bump",
"global_config",
"platform_config",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6
)"#,
)
.bind(self.bump)
.bind(self.global_config)
.bind(self.platform_config)
.bind(&self.padding)
.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 PlatformGlobalAccessRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO platform_global_access_account (
"bump",
"global_config",
"platform_config",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"bump" = EXCLUDED."bump",
"global_config" = EXCLUDED."global_config",
"platform_config" = EXCLUDED."platform_config",
"padding" = EXCLUDED."padding",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.bump)
.bind(self.global_config)
.bind(self.platform_config)
.bind(&self.padding)
.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 PlatformGlobalAccessRow {
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 platform_global_access_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 PlatformGlobalAccessRow {
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 platform_global_access_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 PlatformGlobalAccessMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for PlatformGlobalAccessMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS platform_global_access_account (
-- Account data
"bump" INT2 NOT NULL,
"global_config" BYTEA NOT NULL,
"platform_config" BYTEA NOT NULL,
"padding" NUMERIC(20)[] 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 platform_global_access_account"#)
.execute(connection)
.await?;
Ok(())
}
}