@@ -18,18 +18,18 @@ test "Through the sql package" {
1818 return
1919 }
2020
21- // `?` placeholders are written for Postgres on the way out
22- let changed = db.exec("INSERT INTO sql_users (name, age, active) VALUES (?, ?, ? )", .{
23- sql.Value.of(" Ada")
24- sql.Value.of_int(36)
25- sql.Value.of_bool( true)
21+ // Values go in by name, and Postgres gets `$1`, `$2`, ... for them
22+ let changed = db.exec("INSERT INTO sql_users (name, age, active) VALUES (:name, :age, :active )", .{
23+ "name" => " Ada"
24+ "age" => 36
25+ "active" => true
2626 }) ! { println(E.message) assert(false) return }
2727 assert(changed == 1)
2828
29- db.exec("INSERT INTO sql_users (name, age, active) VALUES (?, ?, ? )", .{
30- sql.Value.of(" Bob")
31- sql.Value.of_int(20)
32- sql.Value.of_bool( false)
29+ db.exec("INSERT INTO sql_users (name, age, active) VALUES (:name, :age, :active )", .{
30+ "name" => " Bob"
31+ "age" => 20
32+ "active" => false
3333 }) ! { assert(false) return }
3434
3535 let rows = db.all("SELECT * FROM sql_users ORDER BY id") ! { println(E.message) assert(false) return }
@@ -44,20 +44,24 @@ test "Through the sql package" {
4444 assert((first.get("avatar") !? sql.Value.of("x")).is_null())
4545
4646 assert((db.value("SELECT count(*) FROM sql_users") ! { assert(false) return }).to_int() == 2)
47- let one = db.one("SELECT * FROM sql_users WHERE name = ? ", .{ sql.Value.of(" Bob") }) ! { assert(false) return }
47+ let one = db.one("SELECT * FROM sql_users WHERE name = :name ", .{ "name" => " Bob" }) ! { assert(false) return }
4848 assert(isset(one))
4949 if isset(one) : assert((one.get("age") !? sql.Value.null()).to_int() == 20)
50- assert(!isset(db.one("SELECT * FROM sql_users WHERE name = ? ", .{ sql.Value.of(" nobody") }) ! { assert(false) return }))
50+ assert(!isset(db.one("SELECT * FROM sql_users WHERE name = :name ", .{ "name" => " nobody" }) ! { assert(false) return }))
5151
5252 // RETURNING is how an insert gives its id back here
53- let returned = db.all("INSERT INTO sql_users (name, age, active) VALUES (?, ?, ? ) RETURNING id", .{
54- sql.Value.of(" Cid")
55- sql.Value.of_int(44)
56- sql.Value.of_bool( true)
53+ let returned = db.all("INSERT INTO sql_users (name, age, active) VALUES (:name, :age, :active ) RETURNING id", .{
54+ "name" => " Cid"
55+ "age" => 44
56+ "active" => true
5757 }) ! { println(E.message) assert(false) return }
5858 assert(returned.length == 1)
5959 assert(((returned.get(0) !? .{}).get("id") !? sql.Value.null()).to_int() > 0)
6060
61+ // A `?` of the statement's own, the JSON operator here, and a `::` cast stay as written
62+ let has_key = db.value("SELECT '{\"a\": 1}'::jsonb ? :key", .{ "key" => "a" }) ! { println(E.message) assert(false) return }
63+ assert(has_key.to_bool())
64+
6165 // Errors arrive as sql errors, with the SQLSTATE kept
6266 db.exec("INSERT INTO sql_users (id, name) VALUES (1, 'clash')") ! {
6367 assert(error_is(E.code, constraint))
@@ -79,18 +83,18 @@ test "The builder against Postgres" {
7983 db.exec("DROP TABLE IF EXISTS sql_posts") ! { assert(false) return }
8084 db.exec("CREATE TABLE sql_posts (id serial PRIMARY KEY, title text, views int, published boolean)") ! { assert(false) return }
8185
82- db.insert_into("sql_posts").values(.{ "title" => sql.Value.of( "first") , "views" => sql.Value.of_int(10) , "published" => sql.Value.of_bool( true) }).run() ! {
86+ db.insert_into("sql_posts").values(.{ "title" => "first", "views" => 10 , "published" => true }).run() ! {
8387 println(E.message)
8488 assert(false)
8589 return
8690 }
87- db.insert_into("sql_posts").values(.{ "title" => sql.Value.of( "second") , "views" => sql.Value.of_int(3) , "published" => sql.Value.of_bool( false) }).run() ! { assert(false) return }
88- db.insert_into("sql_posts").values(.{ "title" => sql.Value.of( "third") , "views" => sql.Value.of_int(30) , "published" => sql.Value.of_bool( true) }).run() ! { assert(false) return }
91+ db.insert_into("sql_posts").values(.{ "title" => "second", "views" => 3 , "published" => false }).run() ! { assert(false) return }
92+ db.insert_into("sql_posts").values(.{ "title" => "third", "views" => 30 , "published" => true }).run() ! { assert(false) return }
8993
9094 let query = db.select("title, views")
9195 query.from("sql_posts")
92- query.where("published = ? ", .{ sql.Value.of_bool( true) } )
93- query.where("views > ? ", .{ sql.Value.of_int(5) } )
96+ query.where("published", true)
97+ query.where("views", "> ", 5 )
9498 query.order_by("views DESC")
9599 let rows = query.all() ! { println(E.message) assert(false) return }
96100 assert(rows.length == 2)
@@ -99,13 +103,13 @@ test "The builder against Postgres" {
99103 let in_rows = db.select().from("sql_posts").where_in("title", .{ sql.Value.of("first"), sql.Value.of("second") }).all() ! { assert(false) return }
100104 assert(in_rows.length == 2)
101105
102- let updated = db.update("sql_posts").set_expression("views", "views + ? ", .{ sql.Value.of_int(1) }).where("published = ? ", .{ sql.Value.of_bool( true) } ).run() ! {
106+ let updated = db.update("sql_posts").set_expression("views", "views + :step ", .{ "step" => 1 }).where("published", true).run() ! {
103107 println(E.message)
104108 assert(false)
105109 return
106110 }
107111 assert(updated == 2)
108- let deleted = db.delete_from("sql_posts").where("views < ? ", .{ sql.Value.of_int(5) } ).run() ! { assert(false) return }
112+ let deleted = db.delete_from("sql_posts").where("views", "< ", 5 ).run() ! { assert(false) return }
109113 assert(deleted == 1)
110114
111115 db.exec("DROP TABLE sql_posts") ! { assert(false) return }
@@ -141,7 +145,7 @@ test "Transactions and migrations on Postgres" {
141145 let ran = migrator.migrate() ! { println(E.message) assert(false) return }
142146 assert(ran.length == 2)
143147 assert((migrator.migrate() ! { assert(false) return }).length == 0)
144- db.exec("INSERT INTO sql_migrations_test (name) VALUES (? )", .{ sql.Value.of("x") }) ! { println(E.message) assert(false) return }
148+ db.exec("INSERT INTO sql_migrations_test (name) VALUES (:name )", .{ "name" => "x" }) ! { println(E.message) assert(false) return }
145149
146150 db.exec("DROP TABLE sql_tx") ! { assert(false) return }
147151 db.exec("DROP TABLE sql_migrations_test") ! { assert(false) return }
@@ -158,14 +162,14 @@ test "Conditions that mix AND and OR on Postgres" {
158162 return
159163 }
160164
161- let rows = Array[Array [sql.Value]]{
162- .{ sql.Value.of(" ada") , sql.Value.of( "admin") , sql.Value.of_int(10) , sql.Value.of_bool( true) }
163- .{ sql.Value.of(" bob") , sql.Value.of( "member"), sql.Value.of_int( 150), sql.Value.of_bool( true) }
164- .{ sql.Value.of(" dora"), sql.Value.of( "admin") , sql.Value.of_int(30) , sql.Value.of_bool( false) }
165- .{ sql.Value.of(" eve") , sql.Value.of( "owner") , sql.Value.null(), sql.Value.of_bool( true) }
165+ let rows = Array[Map [sql.Value]]{
166+ .{ "name" => " ada", "role" => "admin", "score" => 10 , "active" => true }
167+ .{ "name" => " bob", "role" => "member", "score" => 150, "active" => true }
168+ .{ "name" => " dora", "role" => "admin", "score" => 30 , "active" => false }
169+ .{ "name" => " eve", "role" => "owner", "score" => sql.Value.null(), "active" => true }
166170 }
167171 each rows as row {
168- db.exec("INSERT INTO sql_mixed (name, role, score, active) VALUES (?, ?, ?, ? )", row) ! {
172+ db.exec("INSERT INTO sql_mixed (name, role, score, active) VALUES (:name, :role, :score, :active )", row) ! {
169173 println(E.message)
170174 assert(false)
171175 return
@@ -175,10 +179,10 @@ test "Conditions that mix AND and OR on Postgres" {
175179 // The placeholders are numbered $1, $2, ... in the order the conditions nest
176180 let found = db.select("name")
177181 .from("sql_mixed")
178- .where("active = ? ", .{ sql.Value.of_bool( true) } )
182+ .where("active", true)
179183 .where_group(fn(w: sql.Conditions) {
180- w.where("role = ? ", .{ sql.Value.of( "admin") } )
181- w.or_where("score > ? ", .{ sql.Value.of_int( 100) } )
184+ w.where("role", "admin")
185+ w.or_where("score", "> ", 100)
182186 })
183187 .order_by("name")
184188 .all() ! { println(E.message) assert(false) return }
@@ -189,9 +193,9 @@ test "Conditions that mix AND and OR on Postgres" {
189193 // A NULL score is found by the group that allows it
190194 let owners = db.select("name")
191195 .from("sql_mixed")
192- .where("role = ? ", .{ sql.Value.of( "owner") } )
196+ .where("role", "owner")
193197 .where_group(fn(w: sql.Conditions) {
194- w.where("score > ? ", .{ sql.Value.of_int(4) } )
198+ w.where("score", "> ", 4 )
195199 w.or_where_null("score")
196200 })
197201 .all() ! { println(E.message) assert(false) return }
@@ -210,7 +214,7 @@ test "Chunking and rows taken over, on Postgres" {
210214 return
211215 }
212216 let i: uint = 0
213- while i++ < 120 : db.exec("INSERT INTO sql_chunks (name) VALUES (? )", .{ sql.Value.of(" row " + i) }) ! { assert(false) return }
217+ while i++ < 120 : db.exec("INSERT INTO sql_chunks (name) VALUES (:name )", .{ "name" => " row " + i }) ! { assert(false) return }
214218
215219 // Pages, with the last one short
216220 let pages: Array[uint] = .{}
@@ -224,7 +228,7 @@ test "Chunking and rows taken over, on Postgres" {
224228 // Walking by id while writing to every row
225229 let updated = db.select().from("sql_chunks").chunk_by_id("id", 40, fn(rows: Array[Map[sql.Value]]) bool !sql.Error {
226230 each rows as row {
227- db.exec("UPDATE sql_chunks SET checked = true WHERE id = ? ", .{ row.get("id") !? sql.Value.null() }) !>
231+ db.exec("UPDATE sql_chunks SET checked = true WHERE id = :id ", .{ "id" => row.get("id") !? sql.Value.null() }) !>
228232 }
229233 return true
230234 }) ! { println(E.message) assert(false) return }
0 commit comments