Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use BYTEA SQLite type in wrapper crate #368

Open
ceyhunsen opened this issue Nov 29, 2024 · 0 comments
Open

Use BYTEA SQLite type in wrapper crate #368

ceyhunsen opened this issue Nov 29, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@ceyhunsen
Copy link
Member

Proposal Description

Almost every database wrapper converts Rust native type to string and writes it to database that way. This is both slow and uses too much storage. Instead, use BYTEA type and write datas in bytes.

Example impl:

#[derive(Serialize, Deserialize, sqlx::FromRow, Debug, Clone)]
pub struct SignatureDB(pub secp256k1::schnorr::Signature);

impl sqlx::Type<sqlx::Postgres> for SignatureDB {
    fn type_info() -> sqlx::postgres::PgTypeInfo {
        sqlx::postgres::PgTypeInfo::with_name("BYTEA")
    }
}
impl Encode<'_, Postgres> for SignatureDB {
    fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> sqlx::encode::IsNull {
        let serialized = self.0.serialize().to_vec();
        <Vec<u8> as Encode<Postgres>>::encode_by_ref(&serialized, buf)
    }
}
impl<'r> Decode<'r, Postgres> for SignatureDB {
    fn decode(value: PgValueRef<'r>) -> Result<Self, sqlx::error::BoxDynError> {
        let s = <Vec<u8> as Decode<Postgres>>::decode(value)?;
        let x: secp256k1::schnorr::Signature = secp256k1::schnorr::Signature::from_slice(&s)?;
        Ok(SignatureDB(x))
    }
}
@ceyhunsen ceyhunsen added the enhancement New feature or request label Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant