Skip to content

Commit d13a184

Browse files
committed
Update unit tests + QueryBuilder changes
1 parent af52820 commit d13a184

File tree

9 files changed

+151
-101
lines changed

9 files changed

+151
-101
lines changed

sqlx-core/src/query_builder.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::query_as::QueryAs;
1414
use crate::query_scalar::QueryScalar;
1515
use crate::sql_str::AssertSqlSafe;
1616
use crate::sql_str::SqlSafeStr;
17+
use crate::sql_str::SqlStr;
1718
use crate::types::Type;
1819
use crate::Either;
1920

@@ -37,7 +38,7 @@ impl<DB: Database> Default for QueryBuilder<'_, DB> {
3738
fn default() -> Self {
3839
QueryBuilder {
3940
init_len: 0,
40-
query: String::default().into(),
41+
query: Default::default(),
4142
arguments: Some(Default::default()),
4243
}
4344
}
@@ -460,7 +461,7 @@ where
460461
self.sanity_check();
461462

462463
Query {
463-
statement: Either::Left(AssertSqlSafe(self.query.clone()).into_sql_str()),
464+
statement: Either::Left(self.sql()),
464465
arguments: self.arguments.take().map(Ok),
465466
database: PhantomData,
466467
persistent: true,
@@ -527,14 +528,19 @@ where
527528
}
528529

529530
/// Get the current build SQL; **note**: may not be syntactically correct.
530-
pub fn sql(&self) -> &str {
531-
&self.query
531+
pub fn sql(&self) -> SqlStr {
532+
AssertSqlSafe(self.query.clone()).into_sql_str()
532533
}
533534

534535
/// Deconstruct this `QueryBuilder`, returning the built SQL. May not be syntactically correct.
535536
pub fn into_sql(self) -> String {
536537
Arc::unwrap_or_clone(self.query)
537538
}
539+
540+
/// Deconstruct this `QueryBuilder`, returning the built SQL. May not be syntactically correct.
541+
pub fn into_sql(self) -> SqlStr {
542+
AssertSqlSafe(self.query).into_sql_str()
543+
}
538544
}
539545

540546
/// A wrapper around `QueryBuilder` for creating comma(or other token)-separated lists.

sqlx-core/src/testing/fixtures.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ where
150150
}
151151
}
152152

153-
query.into_sql()
153+
query.into_string()
154154
}
155155
}
156156

tests/mysql/describe.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use sqlx::mysql::MySql;
2-
use sqlx::{Column, Executor, Type, TypeInfo};
2+
use sqlx::{Column, Executor, SqlSafeStr, Type, TypeInfo};
33
use sqlx_test::new;
44

55
#[sqlx_macros::test]
66
async fn it_describes_simple() -> anyhow::Result<()> {
77
let mut conn = new::<MySql>().await?;
88

9-
let d = conn.describe("SELECT * FROM tweet").await?;
9+
let d = conn.describe("SELECT * FROM tweet".into_sql_str()).await?;
1010

1111
assert_eq!(d.columns()[0].name(), "id");
1212
assert_eq!(d.columns()[1].name(), "created_at");
@@ -43,7 +43,9 @@ CREATE TEMPORARY TABLE with_bit_and_tinyint (
4343
)
4444
.await?;
4545

46-
let d = conn.describe("SELECT * FROM with_bit_and_tinyint").await?;
46+
let d = conn
47+
.describe("SELECT * FROM with_bit_and_tinyint".into_sql_str())
48+
.await?;
4749

4850
assert_eq!(d.column(2).name(), "value_bool");
4951
assert_eq!(d.column(2).type_info().name(), "BOOLEAN");
@@ -62,7 +64,7 @@ async fn uses_alias_name() -> anyhow::Result<()> {
6264
let mut conn = new::<MySql>().await?;
6365

6466
let d = conn
65-
.describe("SELECT text AS tweet_text FROM tweet")
67+
.describe("SELECT text AS tweet_text FROM tweet".into_sql_str())
6668
.await?;
6769

6870
assert_eq!(d.columns()[0].name(), "tweet_text");

tests/mysql/mysql.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::Context;
22
use futures_util::TryStreamExt;
33
use sqlx::mysql::{MySql, MySqlConnection, MySqlPool, MySqlPoolOptions, MySqlRow};
4-
use sqlx::{Column, Connection, Executor, Row, Statement, TypeInfo};
4+
use sqlx::{Column, Connection, Executor, Row, SqlSafeStr, Statement, TypeInfo};
55
use sqlx_core::connection::ConnectOptions;
66
use sqlx_mysql::MySqlConnectOptions;
77
use sqlx_test::{new, setup_if_needed};
@@ -391,7 +391,9 @@ async fn it_can_prepare_then_execute() -> anyhow::Result<()> {
391391
.await?
392392
.last_insert_id();
393393

394-
let statement = tx.prepare("SELECT * FROM tweet WHERE id = ?").await?;
394+
let statement = tx
395+
.prepare("SELECT * FROM tweet WHERE id = ?".into_sql_str())
396+
.await?;
395397

396398
assert_eq!(statement.column(0).name(), "id");
397399
assert_eq!(statement.column(1).name(), "created_at");

tests/postgres/describe.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use sqlx::{postgres::Postgres, Column, Executor, TypeInfo};
1+
use sqlx::{postgres::Postgres, Column, Executor, SqlSafeStr, TypeInfo};
22
use sqlx_test::new;
33

44
#[sqlx_macros::test]
55
async fn it_describes_simple() -> anyhow::Result<()> {
66
let mut conn = new::<Postgres>().await?;
77

8-
let d = conn.describe("SELECT * FROM tweet").await?;
8+
let d = conn.describe("SELECT * FROM tweet".into_sql_str()).await?;
99

1010
assert_eq!(d.columns()[0].name(), "id");
1111
assert_eq!(d.columns()[1].name(), "created_at");
@@ -29,7 +29,7 @@ async fn it_describes_simple() -> anyhow::Result<()> {
2929
async fn it_describes_expression() -> anyhow::Result<()> {
3030
let mut conn = new::<Postgres>().await?;
3131

32-
let d = conn.describe("SELECT 1::int8 + 10").await?;
32+
let d = conn.describe("SELECT 1::int8 + 10".into_sql_str()).await?;
3333

3434
// ?column? will cause the macro to emit an error ad ask the user to explicitly name the type
3535
assert_eq!(d.columns()[0].name(), "?column?");
@@ -46,7 +46,9 @@ async fn it_describes_expression() -> anyhow::Result<()> {
4646
async fn it_describes_enum() -> anyhow::Result<()> {
4747
let mut conn = new::<Postgres>().await?;
4848

49-
let d = conn.describe("SELECT 'open'::status as _1").await?;
49+
let d = conn
50+
.describe("SELECT 'open'::status as _1".into_sql_str())
51+
.await?;
5052

5153
assert_eq!(d.columns()[0].name(), "_1");
5254

@@ -66,7 +68,9 @@ async fn it_describes_enum() -> anyhow::Result<()> {
6668
async fn it_describes_record() -> anyhow::Result<()> {
6769
let mut conn = new::<Postgres>().await?;
6870

69-
let d = conn.describe("SELECT (true, 10::int2)").await?;
71+
let d = conn
72+
.describe("SELECT (true, 10::int2)".into_sql_str())
73+
.await?;
7074

7175
let ty = d.columns()[0].type_info();
7276
assert_eq!(ty.name(), "RECORD");
@@ -79,7 +83,7 @@ async fn it_describes_composite() -> anyhow::Result<()> {
7983
let mut conn = new::<Postgres>().await?;
8084

8185
let d = conn
82-
.describe("SELECT ROW('name',10,500)::inventory_item")
86+
.describe("SELECT ROW('name',10,500)::inventory_item".into_sql_str())
8387
.await?;
8488

8589
let ty = d.columns()[0].type_info();

tests/postgres/postgres.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use sqlx::postgres::{
55
PgAdvisoryLock, PgConnectOptions, PgConnection, PgDatabaseError, PgErrorPosition, PgListener,
66
PgPoolOptions, PgRow, PgSeverity, Postgres, PG_COPY_MAX_DATA_LEN,
77
};
8-
use sqlx::{Column, Connection, Executor, Row, Statement, TypeInfo};
8+
use sqlx::{Column, Connection, Executor, Row, SqlSafeStr, Statement, TypeInfo};
99
use sqlx_core::sql_str::AssertSqlSafe;
1010
use sqlx_core::{bytes::Bytes, error::BoxDynError};
1111
use sqlx_test::{new, pool, setup_if_needed};
@@ -220,7 +220,7 @@ CREATE TEMPORARY TABLE json_stuff (obj json, obj2 jsonb);
220220
.await?;
221221

222222
let query = "INSERT INTO json_stuff (obj, obj2) VALUES ($1, $2)";
223-
let _ = conn.describe(query).await?;
223+
let _ = conn.describe(query.into_sql_str()).await?;
224224

225225
let done = sqlx::query(query)
226226
.bind(serde_json::json!({ "a": "a" }))
@@ -881,7 +881,9 @@ async fn it_can_prepare_then_execute() -> anyhow::Result<()> {
881881
.fetch_one(&mut *tx)
882882
.await?;
883883

884-
let statement = tx.prepare("SELECT * FROM tweet WHERE id = $1").await?;
884+
let statement = tx
885+
.prepare("SELECT * FROM tweet WHERE id = $1".into_sql_str())
886+
.await?;
885887

886888
assert_eq!(statement.column(0).name(), "id");
887889
assert_eq!(statement.column(1).name(), "created_at");
@@ -967,7 +969,8 @@ async fn test_describe_outer_join_nullable() -> anyhow::Result<()> {
967969
.describe(
968970
"select tweet.id
969971
from tweet
970-
inner join products on products.name = tweet.text",
972+
inner join products on products.name = tweet.text"
973+
.into_sql_str(),
971974
)
972975
.await?;
973976

@@ -978,7 +981,8 @@ async fn test_describe_outer_join_nullable() -> anyhow::Result<()> {
978981
.describe(
979982
"select tweet.id
980983
from (values (null)) vals(val)
981-
left join tweet on false",
984+
left join tweet on false"
985+
.into_sql_str(),
982986
)
983987
.await?;
984988

@@ -992,7 +996,8 @@ from (values (null)) vals(val)
992996
.describe(
993997
"select tweet1.id, tweet2.id
994998
from tweet tweet1
995-
left join tweet tweet2 on false",
999+
left join tweet tweet2 on false"
1000+
.into_sql_str(),
9961001
)
9971002
.await?;
9981003

@@ -1005,7 +1010,8 @@ from (values (null)) vals(val)
10051010
.describe(
10061011
"select tweet1.id, tweet2.id
10071012
from tweet tweet1
1008-
right join tweet tweet2 on false",
1013+
right join tweet tweet2 on false"
1014+
.into_sql_str(),
10091015
)
10101016
.await?;
10111017

@@ -1018,7 +1024,8 @@ from (values (null)) vals(val)
10181024
.describe(
10191025
"select tweet1.id, tweet2.id
10201026
from tweet tweet1
1021-
full join tweet tweet2 on false",
1027+
full join tweet tweet2 on false"
1028+
.into_sql_str(),
10221029
)
10231030
.await?;
10241031

tests/postgres/query_builder.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use sqlx::query_builder::QueryBuilder;
33
use sqlx::Executor;
44
use sqlx::Type;
55
use sqlx::{Either, Execute};
6-
use sqlx_core::sql_str::AssertSqlSafe;
76
use sqlx_test::new;
87

98
#[test]
@@ -134,7 +133,7 @@ async fn test_max_number_of_binds() -> anyhow::Result<()> {
134133
let mut conn = new::<Postgres>().await?;
135134

136135
// Indirectly ensures the macros support this many binds since this is what they use.
137-
let describe = conn.describe(AssertSqlSafe(qb.sql().to_string())).await?;
136+
let describe = conn.describe(qb.sql()).await?;
138137

139138
match describe
140139
.parameters

0 commit comments

Comments
 (0)