-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add a top-level derive for a new SQL Type ( general ideas, needs refinement ) #76
Comments
This will be an excellent addition to the library, for sure! |
No because they cannot emit more |
tuple structs of one and enums seem pretty straight forward. how does encode & decode work for structs though? and how would repr(json) do? add derives for serde and use serde_json? |
Rust#[derive(sqlx::Type)]
#[repr(transparent)]
struct Health(i32); SQL
Rust#[derive(sqlx::Type)]
#[sqlx(postgres(oid = 101010))]
enum Direction { Up, Down, Left, Right }; PostgresCREATE TYPE direction AS ENUM ('up', 'down', 'left', 'right'); MySQLENUM('up', 'down', 'left', 'right')
Rust#[derive(sqlx::Type)]
#[repr(i32)]
enum Direction { Up, Down, Left, Right }; SQLINT
Rust#[derive(sqlx::Type)]
#[sqlx(postgres(oid = 101010))]
struct Point { x: i16, y: i16 }; PostgresCREATE TYPE point AS (
x INT2,
y INT2
); Unresolved Questions
Future Extensions
@Freax13 Let me know if you have any other questions. Encode and Decode for structs in Postgres is a touch complicated. The only documentation is in the source code. It's a specific binary encoding. If you don't feel comfortable tackling this, that's totally fine. Here is the best "docs" I can find on it. https://github.com/scrive/hpqtypes/blob/master/libpqtypes/src/record.c |
turns out deriving HasSqlType for transparent types (and weak enums?) isnt as straight forward as I thought it would be. ideally we would want something like struct Foo(i32);
impl<DB: sqlx::Database> sqlx::types::HasSqlType<Foo> for DB where DB: sqlx::types::HasSqlType<i32> {
fn type_info() -> Self::TypeInfo {
<Self as HasSqlType<i32>>::type_info()
}
} however this doesnt work due to the orphan rules (the two foreign traits being HasSqlType and Database) so we could either
#[derive(HasSqlType(MySql,Postgres,OtherDB))]
struct Foo(i32);
|
I like option |
This is actually changing in 1.41 to be allowed. Orphan rules are relaxed if the type parameter of a foreign trait is your crate-local type. See: https://github.com/rust-lang/rust/blob/master/RELEASES.md#language Until 1.41 is out and we depend on it, I would recommend exposing a boolean or something from |
some rules will be relaxed, but it still wont work |
Right.. It's the Regardless, thinking about this again, the derive should probably just follow the feature flags that are being applied to the If the |
The following are ideas and not final.
Tuple Struct (of 1)
Enum
Struct
Basic idea of implementation is these derives would implement
HasSqlType
and add theEncode
andDecode
derives. Can proc derives be recursive (e.g., can this derive generate more derive macro invocations)?The text was updated successfully, but these errors were encountered: