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

#[derive(Serialize)] for Record #1073

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ members = [
"examples/postgres/listen",
"examples/postgres/todos",
"examples/postgres/mockable-todos",
"examples/postgres/serialize",
"examples/sqlite/todos",
]

Expand Down Expand Up @@ -92,6 +93,7 @@ time = [ "sqlx-core/time", "sqlx-macros/time" ]
bit-vec = [ "sqlx-core/bit-vec", "sqlx-macros/bit-vec"]
bstr = [ "sqlx-core/bstr" ]
git2 = [ "sqlx-core/git2" ]
serialize = [ "sqlx-core/serialize", "sqlx-macros/serialize" ]

[dependencies]
sqlx-core = { version = "0.5.1", path = "sqlx-core", default-features = false }
Expand Down
16 changes: 16 additions & 0 deletions examples/postgres/serialize/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "serialize"
version = "0.1.0"
edition = "2018"
workspace = "../../../"

[dependencies]
anyhow = "1.0"
async-std = { version = "1.6.0", features = [ "attributes" ] }
dotenv = "0.15.0"
futures = "0.3"
paw = "1.0"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sqlx = { path = "../../../", features = ["runtime-async-std-rustls", "postgres", "json", "serialize"] }
structopt = { version = "0.3", features = ["paw"] }
47 changes: 47 additions & 0 deletions examples/postgres/serialize/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# JSON Example using serialize feature

When the serialize feature is enabled, the query!() macro returns a
struct that implements serde::Serialize. This means that each 'Row'
value can be converted to json text using serde_json::to_string(&row).
This includes nested 'jsonb', such as the person column in this
example.

## Setup

1. Declare the database URL

```
export DATABASE_URL="postgres://postgres:password@localhost/serialize"
```

2. Create the database.

```
$ sqlx db create
```

3. Run sql migrations

```
$ sqlx migrate run
```

## Usage

Add a person

```
echo '{ "name": "John Doe", "age": 30 }' | cargo run -- add
```

or with extra keys

```
echo '{ "name": "Jane Doe", "age": 25, "array": ["string", true, 0] }' | cargo run -- add
```

List all people

```
cargo run
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE IF NOT EXISTS people
(
id BIGSERIAL PRIMARY KEY,
person JSONB NOT NULL
);
91 changes: 91 additions & 0 deletions examples/postgres/serialize/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use sqlx::postgres::PgPool;
use sqlx::types::Json;
use std::io::{self, Read};
use std::num::NonZeroU8;
use structopt::StructOpt;

#[derive(StructOpt)]
struct Args {
#[structopt(subcommand)]
cmd: Option<Command>,
}

#[derive(StructOpt)]
enum Command {
Add,
}

#[derive(Deserialize, Serialize)]
struct Person {
name: String,
age: NonZeroU8,
#[serde(flatten)]
extra: Map<String, Value>,
}

#[async_std::main]
#[paw::main]
async fn main(args: Args) -> anyhow::Result<()> {
let pool = PgPool::connect(&dotenv::var("DATABASE_URL")?).await?;

match args.cmd {
Some(Command::Add) => {
let mut json = String::new();
io::stdin().read_to_string(&mut json)?;

let person: Person = serde_json::from_str(&json)?;
println!(
"Adding new person: {}",
&serde_json::to_string_pretty(&person)?
);

let person_id = add_person(&pool, person).await?;
println!("Added new person with ID {}", person_id);
}
None => {
println!("{}", list_people(&pool).await?);
}
}

Ok(())
}

async fn add_person(pool: &PgPool, person: Person) -> anyhow::Result<i64> {
let rec = sqlx::query!(
r#"
INSERT INTO people ( person )
VALUES ( $1 )
RETURNING id
"#,
Json(person) as _
)
.fetch_one(pool)
.await?;

Ok(rec.id)
}

async fn list_people(pool: &PgPool) -> anyhow::Result<String> {
let mut buf = String::from("[");
for (i, row) in sqlx::query!(
r#"
SELECT id, person
FROM people
ORDER BY id
"#
)
.fetch_all(pool)
.await?
.iter()
.enumerate()
{
if i > 0 {
buf.push_str(",\n");
}
buf.push_str(&serde_json::to_string_pretty(&row)?);
}
buf.push_str("]\n");
Ok(buf)
}
1 change: 1 addition & 0 deletions sqlx-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ all-types = [ "chrono", "time", "bigdecimal", "decimal", "ipnetwork", "json", "u
bigdecimal = [ "bigdecimal_", "num-bigint" ]
decimal = [ "rust_decimal", "num-bigint" ]
json = [ "serde", "serde_json" ]
serialize = [ "serde", "serde_json" ]

# runtimes
runtime-actix-native-tls = [ "sqlx-rt/runtime-actix-native-tls", "_tls-native-tls", "_rt-actix" ]
Expand Down
1 change: 1 addition & 0 deletions sqlx-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ ipnetwork = [ "sqlx-core/ipnetwork" ]
uuid = [ "sqlx-core/uuid" ]
bit-vec = [ "sqlx-core/bit-vec" ]
json = [ "sqlx-core/json", "serde_json" ]
serialize = [ "serde", "serde_json" ]

[dependencies]
dotenv = { version = "0.15.0", default-features = false }
Expand Down
11 changes: 9 additions & 2 deletions sqlx-macros/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,19 @@ where
}| quote!(#ident: #type_,),
);

let mut record_tokens = quote! {
let mut record_tokens = TokenStream::new();

#[cfg(feature = "serialize")]
record_tokens.extend(quote! {
#[derive(serde::Serialize)]
});

record_tokens.extend(quote! {
#[derive(Debug)]
struct #record_name {
#(#record_fields)*
}
};
});

record_tokens.extend(output::quote_query_as::<DB>(
&input,
Expand Down
11 changes: 11 additions & 0 deletions tests/postgres/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,3 +887,14 @@ from (values (null)) vals(val)

Ok(())
}

#[cfg(feature = "serialize")]
#[sqlx_macros::test]
async fn it_row_serializes_to_json() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;

let json = serde_json::to_string(&sqlx::query!(" select (1) as id ").fetch_all(&pool).await?)?;
assert_eq!(&json, r#"[{"id":1}]"#);

Ok(())
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a sufficient test to ensure that the codegen works for external users. Maybe add a small project in examples/postgres/ to demonstrate (which will be covered whenever we get around to turning on CI for examples).

Copy link
Contributor Author

@rich-murphey rich-murphey Mar 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll do the equivalent of examples/postgres/json unless you have more preferences.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @abonander, thanks for clarifying the concern that the feature flag would cause compile time errors for users' existing code. And also thanks for the pointer to #916.

We might consider using a visually similar syntax for specifying the derives, such as:

sqlx::query!("SELECT * FROM foo", derive(Serialize))

Given that the feature flag appears to be unworkable, I'm ready to close this PR in favor of a configurable query!() macro.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That old conflict with a call to a function named derive, though not sure whether that's a problem 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Even though it could be disambiguated by matching derive(($id:ident),*), yea, that would still break any code that might pass derive(a,b,c...) as a bound SQL parameter.