-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathdescribe.rs
382 lines (293 loc) · 12.3 KB
/
describe.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
use sqlx::error::DatabaseError;
use sqlx::sqlite::{SqliteConnectOptions, SqliteError};
use sqlx::ConnectOptions;
use sqlx::TypeInfo;
use sqlx::{sqlite::Sqlite, Column, Executor};
use sqlx_test::new;
use std::env;
#[sqlx_macros::test]
async fn it_describes_simple() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let info = conn.describe("SELECT * FROM tweet").await?;
let columns = info.columns();
assert_eq!(columns[0].name(), "id");
assert_eq!(columns[1].name(), "text");
assert_eq!(columns[2].name(), "is_sent");
assert_eq!(columns[3].name(), "owner_id");
assert_eq!(columns[0].ordinal(), 0);
assert_eq!(columns[1].ordinal(), 1);
assert_eq!(columns[2].ordinal(), 2);
assert_eq!(columns[3].ordinal(), 3);
assert_eq!(info.nullable(0), Some(false));
assert_eq!(info.nullable(1), Some(false));
assert_eq!(info.nullable(2), Some(false));
assert_eq!(info.nullable(3), Some(true)); // owner_id
assert_eq!(columns[0].type_info().name(), "INTEGER");
assert_eq!(columns[1].type_info().name(), "TEXT");
assert_eq!(columns[2].type_info().name(), "BOOLEAN");
assert_eq!(columns[3].type_info().name(), "INTEGER");
Ok(())
}
#[sqlx_macros::test]
async fn it_describes_variables() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
// without any context, we resolve to NULL
let info = conn.describe("SELECT ?1").await?;
assert_eq!(info.columns()[0].type_info().name(), "NULL");
assert_eq!(info.nullable(0), Some(true)); // nothing prevents the value from being bound to null
// context can be provided by using CAST(_ as _)
let info = conn.describe("SELECT CAST(?1 AS REAL)").await?;
assert_eq!(info.columns()[0].type_info().name(), "REAL");
assert_eq!(info.nullable(0), Some(true)); // nothing prevents the value from being bound to null
Ok(())
}
#[sqlx_macros::test]
async fn it_describes_expression() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let d = conn
.describe("SELECT 1 + 10, 5.12 * 2, 'Hello', x'deadbeef', null")
.await?;
let columns = d.columns();
assert_eq!(columns[0].type_info().name(), "INTEGER");
assert_eq!(columns[0].name(), "1 + 10");
assert_eq!(d.nullable(0), Some(false)); // literal constant
assert_eq!(columns[1].type_info().name(), "REAL");
assert_eq!(columns[1].name(), "5.12 * 2");
assert_eq!(d.nullable(1), Some(false)); // literal constant
assert_eq!(columns[2].type_info().name(), "TEXT");
assert_eq!(columns[2].name(), "'Hello'");
assert_eq!(d.nullable(2), Some(false)); // literal constant
assert_eq!(columns[3].type_info().name(), "BLOB");
assert_eq!(columns[3].name(), "x'deadbeef'");
assert_eq!(d.nullable(3), Some(false)); // literal constant
assert_eq!(columns[4].type_info().name(), "NULL");
assert_eq!(columns[4].name(), "null");
assert_eq!(d.nullable(4), Some(true)); // literal null
Ok(())
}
#[sqlx_macros::test]
async fn it_describes_expression_from_empty_table() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
conn.execute("CREATE TEMP TABLE _temp_empty ( name TEXT NOT NULL, a INT )")
.await?;
let d = conn
.describe("SELECT COUNT(*), a + 1, name, 5.12, 'Hello' FROM _temp_empty")
.await?;
assert_eq!(d.columns()[0].type_info().name(), "INTEGER");
assert_eq!(d.nullable(0), Some(false)); // COUNT(*)
assert_eq!(d.columns()[1].type_info().name(), "INTEGER");
assert_eq!(d.nullable(1), Some(true)); // `a+1` is nullable, because a is nullable
assert_eq!(d.columns()[2].type_info().name(), "TEXT");
assert_eq!(d.nullable(2), Some(true)); // `name` is not nullable, but the query can be null due to zero rows
assert_eq!(d.columns()[3].type_info().name(), "REAL");
assert_eq!(d.nullable(3), Some(false)); // literal constant
assert_eq!(d.columns()[4].type_info().name(), "TEXT");
assert_eq!(d.nullable(4), Some(false)); // literal constant
Ok(())
}
#[sqlx_macros::test]
async fn it_describes_expression_from_empty_table_with_star() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
conn.execute("CREATE TEMP TABLE _temp_empty ( name TEXT, a INT )")
.await?;
let d = conn
.describe("SELECT *, 5, 'Hello' FROM _temp_empty")
.await?;
assert_eq!(d.columns()[0].type_info().name(), "TEXT");
assert_eq!(d.columns()[1].type_info().name(), "INTEGER");
assert_eq!(d.columns()[2].type_info().name(), "INTEGER");
assert_eq!(d.columns()[3].type_info().name(), "TEXT");
Ok(())
}
#[sqlx_macros::test]
async fn it_describes_insert() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let d = conn
.describe("INSERT INTO tweet (id, text) VALUES (2, 'Hello')")
.await?;
assert_eq!(d.columns().len(), 0);
let d = conn
.describe("INSERT INTO tweet (id, text) VALUES (2, 'Hello'); SELECT last_insert_rowid();")
.await?;
assert_eq!(d.columns().len(), 1);
assert_eq!(d.columns()[0].type_info().name(), "INTEGER");
assert_eq!(d.nullable(0), Some(false));
Ok(())
}
#[sqlx_macros::test]
async fn it_describes_insert_with_read_only() -> anyhow::Result<()> {
sqlx_test::setup_if_needed();
let mut options: SqliteConnectOptions = env::var("DATABASE_URL")?.parse().unwrap();
options = options.read_only(true);
let mut conn = options.connect().await?;
let d = conn
.describe("INSERT INTO tweet (id, text) VALUES (2, 'Hello')")
.await?;
assert_eq!(d.columns().len(), 0);
Ok(())
}
#[sqlx_macros::test]
async fn it_describes_insert_with_returning() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let d = conn
.describe("INSERT INTO tweet (id, text) VALUES (2, 'Hello') RETURNING *")
.await?;
assert_eq!(d.columns().len(), 4);
assert_eq!(d.column(0).type_info().name(), "INTEGER");
assert_eq!(d.column(1).type_info().name(), "TEXT");
Ok(())
}
#[sqlx_macros::test]
async fn it_describes_bad_statement() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let err = conn.describe("SELECT 1 FROM not_found").await.unwrap_err();
let err = err
.as_database_error()
.unwrap()
.downcast_ref::<SqliteError>();
assert_eq!(err.message(), "no such table: not_found");
assert_eq!(err.code().as_deref(), Some("1"));
Ok(())
}
#[sqlx_macros::test]
async fn it_describes_left_join() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let d = conn.describe("select accounts.id from accounts").await?;
assert_eq!(d.column(0).type_info().name(), "INTEGER");
assert_eq!(d.nullable(0), Some(false));
let d = conn
.describe("select tweet.id from accounts left join tweet on owner_id = accounts.id")
.await?;
assert_eq!(d.column(0).type_info().name(), "INTEGER");
assert_eq!(d.nullable(0), Some(true));
let d = conn
.describe(
"select tweet.id, accounts.id from accounts left join tweet on owner_id = accounts.id",
)
.await?;
assert_eq!(d.column(0).type_info().name(), "INTEGER");
assert_eq!(d.nullable(0), Some(true));
assert_eq!(d.column(1).type_info().name(), "INTEGER");
assert_eq!(d.nullable(1), Some(false));
let d = conn
.describe(
"select tweet.id, accounts.id from accounts inner join tweet on owner_id = accounts.id",
)
.await?;
assert_eq!(d.column(0).type_info().name(), "INTEGER");
assert_eq!(d.nullable(0), Some(false));
assert_eq!(d.column(1).type_info().name(), "INTEGER");
assert_eq!(d.nullable(1), Some(false));
let d = conn
.describe(
"select tweet.id, accounts.id from accounts left join tweet on tweet.id = accounts.id",
)
.await?;
assert_eq!(d.column(0).type_info().name(), "INTEGER");
assert_eq!(d.nullable(0), Some(true));
assert_eq!(d.column(1).type_info().name(), "INTEGER");
assert_eq!(d.nullable(1), Some(false));
Ok(())
}
#[sqlx_macros::test]
async fn it_describes_literal_subquery() -> anyhow::Result<()> {
async fn assert_literal_described(
conn: &mut sqlx::SqliteConnection,
query: &str,
) -> anyhow::Result<()> {
let info = conn.describe(query).await?;
assert_eq!(info.column(0).type_info().name(), "TEXT", "{}", query);
assert_eq!(info.nullable(0), Some(false), "{}", query);
assert_eq!(info.column(1).type_info().name(), "NULL", "{}", query);
assert_eq!(info.nullable(1), Some(true), "{}", query);
Ok(())
}
let mut conn = new::<Sqlite>().await?;
assert_literal_described(&mut conn, "SELECT 'a', NULL").await?;
assert_literal_described(&mut conn, "SELECT * FROM (SELECT 'a', NULL)").await?;
assert_literal_described(
&mut conn,
"WITH cte AS (SELECT 'a', NULL) SELECT * FROM cte",
)
.await?;
assert_literal_described(
&mut conn,
"WITH cte AS MATERIALIZED (SELECT 'a', NULL) SELECT * FROM cte",
)
.await?;
assert_literal_described(
&mut conn,
"WITH RECURSIVE cte(a,b) AS (SELECT 'a', NULL UNION ALL SELECT a||a, NULL FROM cte WHERE length(a)<3) SELECT * FROM cte",
)
.await?;
Ok(())
}
#[sqlx_macros::test]
async fn it_describes_table_subquery() -> anyhow::Result<()> {
async fn assert_tweet_described(
conn: &mut sqlx::SqliteConnection,
query: &str,
) -> anyhow::Result<()> {
let info = conn.describe(query).await?;
let columns = info.columns();
assert_eq!(columns[0].name(), "id", "{}", query);
assert_eq!(columns[1].name(), "text", "{}", query);
assert_eq!(columns[2].name(), "is_sent", "{}", query);
assert_eq!(columns[3].name(), "owner_id", "{}", query);
assert_eq!(columns[0].ordinal(), 0, "{}", query);
assert_eq!(columns[1].ordinal(), 1, "{}", query);
assert_eq!(columns[2].ordinal(), 2, "{}", query);
assert_eq!(columns[3].ordinal(), 3, "{}", query);
assert_eq!(info.nullable(0), Some(false), "{}", query);
assert_eq!(info.nullable(1), Some(false), "{}", query);
assert_eq!(info.nullable(2), Some(false), "{}", query);
assert_eq!(info.nullable(3), Some(true), "{}", query);
assert_eq!(columns[0].type_info().name(), "INTEGER", "{}", query);
assert_eq!(columns[1].type_info().name(), "TEXT", "{}", query);
assert_eq!(columns[2].type_info().name(), "BOOLEAN", "{}", query);
assert_eq!(columns[3].type_info().name(), "INTEGER", "{}", query);
Ok(())
}
let mut conn = new::<Sqlite>().await?;
assert_tweet_described(&mut conn, "SELECT * FROM tweet").await?;
assert_tweet_described(&mut conn, "SELECT * FROM (SELECT * FROM tweet)").await?;
assert_tweet_described(
&mut conn,
"WITH cte AS (SELECT * FROM tweet) SELECT * FROM cte",
)
.await?;
assert_tweet_described(
&mut conn,
"WITH cte AS MATERIALIZED (SELECT * FROM tweet) SELECT * FROM cte",
)
.await?;
Ok(())
}
#[sqlx_macros::test]
async fn it_describes_union() -> anyhow::Result<()> {
async fn assert_union_described(
conn: &mut sqlx::SqliteConnection,
query: &str,
) -> anyhow::Result<()> {
let info = conn.describe(query).await?;
assert_eq!(info.column(0).type_info().name(), "TEXT", "{}", query);
assert_eq!(info.nullable(0), Some(false), "{}", query);
assert_eq!(info.column(1).type_info().name(), "TEXT", "{}", query);
assert_eq!(info.nullable(1), Some(true), "{}", query);
assert_eq!(info.column(2).type_info().name(), "INTEGER", "{}", query);
assert_eq!(info.nullable(2), Some(true), "{}", query);
//TODO: mixed type columns not handled correctly
//assert_eq!(info.column(3).type_info().name(), "NULL", "{}", query);
//assert_eq!(info.nullable(3), Some(false), "{}", query);
Ok(())
}
let mut conn = new::<Sqlite>().await?;
assert_union_described(
&mut conn,
"SELECT 'txt','a',null,'b' UNION ALL SELECT 'int',NULL,1,2 ",
)
.await?;
//TODO: insert into temp-table not merging datatype/nullable of all operations - currently keeping last-writer
//assert_union_described(&mut conn, "SELECT 'txt','a',null,'b' UNION SELECT 'int',NULL,1,2 ").await?;
Ok(())
}