Skip to content

Commit 5532303

Browse files
committed
Swift 1.2 beta 1
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>
1 parent 83f08f7 commit 5532303

25 files changed

+1362
-1457
lines changed

Documentation/Index.md

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ let name = Expression<String?>("name")
237237

238238
### Compound Expressions
239239

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 as other [non-filter operators](#other-operators) and [functions](#core-sqlite-functions)). These building blocks can create complex SQLite statements.
241241

242242

243243
### Queries
@@ -407,23 +407,19 @@ The `insert` function can return several different types that are useful in diff
407407
}
408408
```
409409

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 `!`.
411411

412412
``` swift
413-
// ignore failure
414-
users.insert(email <- "alice@mac.com")?
415-
416-
// assertion on failure
417413
users.insert(email <- "alice@mac.com")!
418414
```
419415

420416
- A `Statement`, for [the transaction and savepoint helpers](#transactions-and-savepoints) that take a list of statements.
421417

422418
``` 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()
427423
// BEGIN DEFERRED TRANSACTION;
428424
// INSERT INTO "users" ("email") VALUES ('alice@mac.com');
429425
// 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 `+=`:
473469

474470
``` swift
475471
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()
480476
// BEGIN DEFERRED TRANSACTION;
481477
// UPDATE "users" SET "balance" = "balance" - 100.0 WHERE ("id" = 1);
482478
// UPDATE "users" SET "balance" = "balance" + 100.0 WHERE ("id" = 2);
@@ -841,13 +837,9 @@ Like [`insert`](#inserting-rows) (and [`delete`](#updating-rows)), `update` can
841837
}
842838
```
843839

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 `!`.
845841

846842
``` swift
847-
// ignore failure
848-
alice.update(email <- "alice@me.com")?
849-
850-
// assertion on failure
851843
alice.update(email <- "alice@me.com")!
852844
```
853845

@@ -885,13 +877,9 @@ Like [`insert`](#inserting-rows) and [`update`](#updating-rows), `delete` can re
885877
}
886878
```
887879

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 `!`.
889881

890882
``` swift
891-
// ignore failure
892-
alice.delete()?
893-
894-
// assertion on failure
895883
alice.delete()!
896884
```
897885

@@ -902,13 +890,13 @@ Like [`insert`](#inserting-rows) and [`update`](#updating-rows), `delete` can re
902890

903891
## Transactions and Savepoints
904892

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.
906894

907895
``` swift
908-
db.transaction(
909-
users.insert(email <- "betty@icloud.com"),
910-
users.insert(email <- "cathy@icloud.com", manager_id <- db.lastId)
911-
)
896+
db.transaction()
897+
&& users.insert(email <- "betty@icloud.com")
898+
&& users.insert(email <- "cathy@icloud.com", manager_id <- db.lastId)
899+
&& db.commit() || db.rollback()
912900
```
913901

914902
> _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
13191307

13201308
``` swift
13211309
db.create(function: "typeConformsTo", deterministic: true) { args in
1322-
switch (args[0], args[1]) {
1323-
case let (UTI as String, conformsToUTI as String):
1310+
if let UTI = args[0] as? String, conformsToUTI = args[1] as? String {
13241311
return Int(UTTypeConformsTo(UTI, conformsToUTI))
1325-
default:
1326-
return nil
13271312
}
1313+
return nil
13281314
}
13291315
```
13301316

@@ -1447,14 +1433,14 @@ Though we recommend you stick with SQLite.swift’s [type-safe system](#building
14471433
- `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.
14481434

14491435
``` swift
1450-
db.scalar("SELECT count(*) FROM users") as Int64
1436+
db.scalar("SELECT count(*) FROM users") as! Int64
14511437
```
14521438

14531439
Statements also have a `scalar` function, which can optionally re-bind values at execution.
14541440

14551441
``` swift
14561442
let stmt = db.prepare("SELECT count (*) FROM users")
1457-
stmt.scalar() as Int64
1443+
stmt.scalar() as! Int64
14581444
```
14591445

14601446

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ syntax _and_ intent.
1717
- A flexible, chainable, lazy-executing query layer
1818
- Automatically-typed data access
1919
- A lightweight, uncomplicated query and parameter binding interface
20-
- Transactions with implicit commit/rollback
2120
- Developer-friendly error handling and debugging
2221
- [Full-text search][] support
2322
- [SQLCipher](#sqlcipher) support
@@ -106,13 +105,9 @@ interactively, from the Xcode project’s playground.
106105

107106
## Installation
108107

109-
> _Note:_ SQLite.swift requires Swift 1.1 (and [Xcode][] 6.1) or
108+
> _Note:_ SQLite.swift requires Swift 1.2 (and [Xcode][] 6.3) or
110109
> greater.
111110
>
112-
> For the Swift 1.2 beta (included in Xcode 6.3), use the
113-
> [`swift-1-2`](https://github.com/stephencelis/SQLite.swift/tree/swift-1-2)
114-
> branch.
115-
>
116111
> The following instructions apply to targets that support embedded
117112
> Swift frameworks. To use SQLite.swift in iOS 7 or an OS X command line
118113
> tool, please read the [Frameworkless Targets][] section of the

0 commit comments

Comments
 (0)