You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Most things should work as they have been, with a few notes:
- The `db.transaction` helpers that took variadic auto-closures have
been removed (Swift no longer supports variadic auto-closures).
Update path: use `&&` and `||` for control flow:
db.transaction() &&
stmt1 &&
stmt2 &&
db.commit() || db.rollback()
Or use the block-based helper:
db.transaction { _ in
stmt1.run()
if stmt1.failed { return .Rollback }
stmt2.run()
if stmt2.failed { return .Rollback }
return .Commit
}
Note: You'll need to explicitly call/return COMMIT and ROLLBACK now.
- There appears to be a bug in Swift causing 2 memory-related,
over-releasing crashes in the test suite. Filed: rdar://19782170
Many bugs marked FIXME with links to rdars are now fixable and have been
fixed.
The tests have also been heavily refactored (they were abusing the power
of `@autoclosure`, which has been curtailed with `@noescape`), but
should be generally more readable, if slightly less flexible.
Signed-off-by: Stephen Celis <stephen@stephencelis.com>
Copy file name to clipboardExpand all lines: Documentation/Index.md
+21-35Lines changed: 21 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -237,7 +237,7 @@ let name = Expression<String?>("name")
237
237
238
238
### Compound Expressions
239
239
240
-
Expressions can be combined with other expressions and types using [filters](#filter-operators-and-functions), and [other operators](#other-operators) and [functions](#core-sqlite-functions). These building blocks can create complex SQLite statements.
240
+
Expressions can be combined with other expressions and types using [filter operators and functions](#filter-operators-and-functions) (as well asother [non-filter operators](#other-operators) and [functions](#core-sqlite-functions)). These building blocks can create complex SQLite statements.
241
241
242
242
243
243
### Queries
@@ -407,23 +407,19 @@ The `insert` function can return several different types that are useful in diff
407
407
}
408
408
```
409
409
410
-
We can use the optional nature of the value to disambiguate with a simple `?` or `!`.
410
+
If a value is always expected, we can disambiguate with a `!`.
411
411
412
412
``` swift
413
-
// ignore failure
414
-
users.insert(email <-"alice@mac.com")?
415
-
416
-
// assertion on failure
417
413
users.insert(email <-"alice@mac.com")!
418
414
```
419
415
420
416
- A `Statement`, for [the transaction and savepoint helpers](#transactions-and-savepoints) that take a list of statements.
421
417
422
418
``` swift
423
-
db.transaction(
424
-
users.insert(email <-"alice@mac.com"),
425
-
users.insert(email <-"betty@mac.com")
426
-
)
419
+
db.transaction()
420
+
&&users.insert(email <-"alice@mac.com")
421
+
&&users.insert(email <-"betty@mac.com")
422
+
&& db.commit() || db.rollback()
427
423
// BEGIN DEFERRED TRANSACTION;
428
424
// INSERT INTO "users" ("email") VALUES ('alice@mac.com');
429
425
// INSERT INTO "users" ("email") VALUES ('betty@mac.com');
@@ -473,10 +469,10 @@ To take an amount and “move” it via transaction, we can use `-=` and `+=`:
473
469
474
470
``` swift
475
471
let amount =100.0
476
-
db.transaction(
477
-
alice.update(balance -= amount),
478
-
betty.update(balance += amount)
479
-
)
472
+
db.transaction()
473
+
&&alice.update(balance -= amount)
474
+
&&betty.update(balance += amount)
475
+
&& db.commit() || db.rollback()
480
476
// BEGIN DEFERRED TRANSACTION;
481
477
// UPDATE "users" SET "balance" = "balance" - 100.0 WHERE ("id" = 1);
482
478
// UPDATE "users" SET "balance" = "balance" + 100.0 WHERE ("id" = 2);
@@ -841,13 +837,9 @@ Like [`insert`](#inserting-rows) (and [`delete`](#updating-rows)), `update` can
841
837
}
842
838
```
843
839
844
-
We can use the optional nature of the value to disambiguate with a simple `?` or `!`.
840
+
If a value is always expected, we can disambiguate with a `!`.
845
841
846
842
``` swift
847
-
// ignore failure
848
-
alice.update(email <-"alice@me.com")?
849
-
850
-
// assertion on failure
851
843
alice.update(email <-"alice@me.com")!
852
844
```
853
845
@@ -885,13 +877,9 @@ Like [`insert`](#inserting-rows) and [`update`](#updating-rows), `delete` can re
885
877
}
886
878
```
887
879
888
-
We can use the optional nature of the value to disambiguate with a simple `?` or `!`.
880
+
If a value is always expected, we can disambiguate with a `!`.
889
881
890
882
``` swift
891
-
// ignore failure
892
-
alice.delete()?
893
-
894
-
// assertion on failure
895
883
alice.delete()!
896
884
```
897
885
@@ -902,13 +890,13 @@ Like [`insert`](#inserting-rows) and [`update`](#updating-rows), `delete` can re
902
890
903
891
## Transactions and Savepoints
904
892
905
-
Using the `transaction` and `savepoint` functions, we can run a series of statements, commiting the changes to the database if they all succeed. If a single statement fails, we bail out early and roll back.
893
+
Using the `transaction` and `savepoint` functions, we can run a series of statements, committing the changes to the database if they all succeed. If a single statement fails, we can bail out early and roll back.
> _Note:_ Each statement is captured in an auto-closure and won’t execute till the preceding statement succeeds. This means we can use the `lastId` property on `Database` to reference the previous statement’s insert [`ROWID`][ROWID].
@@ -1319,12 +1307,10 @@ We can create loosely-typed functions by handling an array of raw arguments, ins
1319
1307
1320
1308
``` swift
1321
1309
db.create(function: "typeConformsTo", deterministic: true) { args in
1322
-
switch (args[0], args[1]) {
1323
-
caselet (UTI asString, conformsToUTI asString):
1310
+
iflet UTI = args[0] as?String, conformsToUTI = args[1] as?String {
1324
1311
returnInt(UTTypeConformsTo(UTI, conformsToUTI))
1325
-
default:
1326
-
returnnil
1327
1312
}
1313
+
returnnil
1328
1314
}
1329
1315
```
1330
1316
@@ -1447,14 +1433,14 @@ Though we recommend you stick with SQLite.swift’s [type-safe system](#building
1447
1433
- `scalar` prepares a single `Statement` object from a SQL string, optionally binds values to it (using the statement’s `bind` function), executes, and returns the first value of the first row.
1448
1434
1449
1435
``` swift
1450
-
db.scalar("SELECT count(*) FROM users") asInt64
1436
+
db.scalar("SELECT count(*) FROM users") as!Int64
1451
1437
```
1452
1438
1453
1439
Statements also have a `scalar` function, which can optionally re-bind values at execution.
1454
1440
1455
1441
``` swift
1456
1442
let stmt = db.prepare("SELECT count (*) FROM users")
0 commit comments