Skip to content

Commit 0708a27

Browse files
committed
Tighten naming conventions with SQLite
We should prefer, in the raw interface, at least, the same naming conventions as SQLite. This should make it easier to transition with less of a need to reference the documentation. Signed-off-by: Stephen Celis <[email protected]>
1 parent ad8ec58 commit 0708a27

File tree

6 files changed

+50
-50
lines changed

6 files changed

+50
-50
lines changed

Documentation/Index.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,12 @@ The `insert` function can return several different types that are useful in diff
429429
// COMMIT TRANSACTION;
430430
```
431431

432-
- A tuple of the above [`ROWID`][ROWID] and statement: `(id: Int64?, statement: Statement)`, for flexibility.
432+
- A tuple of the above [`ROWID`][ROWID] and statement: `(rowid: Int64?, statement: Statement)`, for flexibility.
433433

434434
``` swift
435-
let (id, statement) = users.insert(email <- "[email protected]")
436-
if let id = id {
437-
println("inserted id: \(id)")
435+
let (rowid, statement) = users.insert(email <- "[email protected]")
436+
if let rowid = rowid {
437+
println("inserted id: \(rowid)")
438438
} else if statement.failed {
439439
println("insertion failed: \(statement.reason)")
440440
}
@@ -898,11 +898,11 @@ Using the `transaction` and `savepoint` functions, we can run a series of statem
898898
``` swift
899899
db.transaction()
900900
&& users.insert(email <- "[email protected]")
901-
&& users.insert(email <- "[email protected]", manager_id <- db.lastId)
901+
&& users.insert(email <- "[email protected]", manager_id <- db.lastInsertRowid)
902902
&& db.commit() || db.rollback()
903903
```
904904

905-
> _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].
905+
> _Note:_ Each statement is captured in an auto-closure and won’t execute till the preceding statement succeeds. This means we can use the `lastInsertRowid` property on `Database` to reference the previous statement’s insert [`ROWID`][ROWID].
906906

907907

908908
## Altering the Schema
@@ -1414,7 +1414,7 @@ Though we recommend you stick with SQLite.swift’s [type-safe system](#building
14141414

14151415
``` swift
14161416
stmt.run("[email protected]")
1417-
db.lastChanges // -> {Some 1}
1417+
db.changes // -> {Some 1}
14181418
```
14191419

14201420
Statements with results may be iterated over.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ for email in ["[email protected]", "[email protected]"] {
8484
stmt.run(email)
8585
}
8686

87-
db.totalChanges // 3
88-
db.lastChanges // 1
89-
db.lastId // 3
87+
db.totalChanges // 3
88+
db.changes // 1
89+
db.lastInsertRowid // 3
9090

9191
for row in db.prepare("SELECT id, email FROM users") {
9292
println("id: \(row[0]), email: \(row[1])")

SQLite Tests/DatabaseTests.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@ class DatabaseTests: SQLiteTestCase {
1818
XCTAssert(db.readonly)
1919
}
2020

21-
func test_lastId_returnsNilOnNewConnections() {
22-
XCTAssert(db.lastId == nil)
21+
func test_lastInsertRowid_returnsNilOnNewConnections() {
22+
XCTAssert(db.lastInsertRowid == nil)
2323
}
2424

25-
func test_lastId_returnsLastIdAfterInserts() {
25+
func test_lastInsertRowid_returnsLastIdAfterInserts() {
2626
insertUser("alice")
27-
XCTAssert(db.lastId! == 1)
27+
XCTAssert(db.lastInsertRowid! == 1)
2828
}
2929

30-
func test_lastChanges_returnsZeroOnNewConnections() {
31-
XCTAssertEqual(0, db.lastChanges)
30+
func test_changes_returnsZeroOnNewConnections() {
31+
XCTAssertEqual(0, db.changes)
3232
}
3333

34-
func test_lastChanges_returnsNumberOfChanges() {
34+
func test_changes_returnsNumberOfChanges() {
3535
insertUser("alice")
36-
XCTAssertEqual(1, db.lastChanges)
36+
XCTAssertEqual(1, db.changes)
3737
insertUser("betsy")
38-
XCTAssertEqual(1, db.lastChanges)
38+
XCTAssertEqual(1, db.changes)
3939
}
4040

4141
func test_totalChanges_returnsTotalNumberOfChanges() {

SQLite Tests/QueryTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,11 @@ class QueryTests: SQLiteTestCase {
296296
}
297297

298298
func test_insert_insertsRows() {
299-
XCTAssertEqual(1, users.insert(email <- "[email protected]", age <- 30).id!)
299+
XCTAssertEqual(1, users.insert(email <- "[email protected]", age <- 30).rowid!)
300300

301301
AssertSQL("INSERT INTO \"users\" (\"email\", \"age\") VALUES ('[email protected]', 30)")
302302

303-
XCTAssert(users.insert(email <- "[email protected]", age <- 30).id == nil)
303+
XCTAssert(users.insert(email <- "[email protected]", age <- 30).rowid == nil)
304304
}
305305

306306
func test_insert_withQuery_insertsRows() {
@@ -316,15 +316,15 @@ class QueryTests: SQLiteTestCase {
316316
db.execute("CREATE TABLE \"timestamps\" (\"id\" INTEGER PRIMARY KEY, \"timestamp\" TEXT DEFAULT CURRENT_DATETIME)")
317317
let table = db["timestamps"]
318318

319-
XCTAssertEqual(1, table.insert().id!)
319+
XCTAssertEqual(1, table.insert().rowid!)
320320
AssertSQL("INSERT INTO \"timestamps\" DEFAULT VALUES")
321321
}
322322

323323
func test_replace_replaceRows() {
324-
XCTAssertEqual(1, users.replace(email <- "[email protected]", age <- 30).id!)
324+
XCTAssertEqual(1, users.replace(email <- "[email protected]", age <- 30).rowid!)
325325
AssertSQL("INSERT OR REPLACE INTO \"users\" (\"email\", \"age\") VALUES ('[email protected]', 30)")
326326

327-
XCTAssertEqual(1, users.replace(id <- 1, email <- "[email protected]", age <- 30).id!)
327+
XCTAssertEqual(1, users.replace(id <- 1, email <- "[email protected]", age <- 30).rowid!)
328328
AssertSQL("INSERT OR REPLACE INTO \"users\" (\"id\", \"email\", \"age\") VALUES (1, '[email protected]', 30)")
329329
}
330330

SQLite/Database.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ public final class Database {
5454
// MARK: -
5555

5656
/// The last rowid inserted into the database via this connection.
57-
public var lastId: Int64? {
58-
let lastId = sqlite3_last_insert_rowid(handle)
59-
return lastId == 0 ? nil : lastId
57+
public var lastInsertRowid: Int64? {
58+
let rowid = sqlite3_last_insert_rowid(handle)
59+
return rowid == 0 ? nil : rowid
6060
}
6161

6262
/// The last number of changes (inserts, updates, or deletes) made to the
6363
/// database via this connection.
64-
public var lastChanges: Int {
64+
public var changes: Int {
6565
return Int(sqlite3_changes(handle))
6666
}
6767

@@ -363,7 +363,7 @@ public final class Database {
363363
/// Sets a busy timeout to retry after encountering a busy signal (lock).
364364
///
365365
/// :param: ms Milliseconds to wait before retrying.
366-
public func timeout(ms: Int) {
366+
public func busyTimeout(ms: Int) {
367367
sqlite3_busy_timeout(handle, Int32(ms))
368368
}
369369

@@ -374,17 +374,17 @@ public final class Database {
374374
/// number of times it’s been called for this lock. If it
375375
/// returns true, it will try again. If it returns false,
376376
/// no further attempts will be made.
377-
public func busy(callback: ((tries: Int) -> Bool)?) {
377+
public func busyHandler(callback: ((tries: Int) -> Bool)?) {
378378
try {
379379
if let callback = callback {
380-
self.busy = { callback(tries: Int($0)) ? 1 : 0 }
380+
self.busyHandler = { callback(tries: Int($0)) ? 1 : 0 }
381381
} else {
382-
self.busy = nil
382+
self.busyHandler = nil
383383
}
384-
return _SQLiteBusyHandler(self.handle, self.busy)
384+
return _SQLiteBusyHandler(self.handle, self.busyHandler)
385385
}
386386
}
387-
private var busy: _SQLiteBusyHandlerCallback?
387+
private var busyHandler: _SQLiteBusyHandlerCallback?
388388

389389
/// Sets a handler to call when a statement is executed with the compiled
390390
/// SQL.

SQLite/Query.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,14 @@ public struct Query {
444444
/// :param: values A list of values to set.
445445
///
446446
/// :returns: The rowid.
447-
public func insert(value: Setter, _ more: Setter...) -> Int64? { return insert([value] + more).id }
447+
public func insert(value: Setter, _ more: Setter...) -> Int64? { return insert([value] + more).rowid }
448448

449449
/// Runs an INSERT statement against the query.
450450
///
451451
/// :param: values A list of values to set.
452452
///
453453
/// :returns: The rowid and statement.
454-
public func insert(value: Setter, _ more: Setter...) -> (id: Int64?, statement: Statement) {
454+
public func insert(value: Setter, _ more: Setter...) -> (rowid: Int64?, statement: Statement) {
455455
return insert([value] + more)
456456
}
457457

@@ -460,16 +460,16 @@ public struct Query {
460460
/// :param: values An array of values to set.
461461
///
462462
/// :returns: The rowid.
463-
public func insert(values: [Setter]) -> Int64? { return insert(values).id }
463+
public func insert(values: [Setter]) -> Int64? { return insert(values).rowid }
464464

465465
/// Runs an INSERT statement against the query.
466466
///
467467
/// :param: values An array of values to set.
468468
///
469469
/// :returns: The rowid and statement.
470-
public func insert(values: [Setter]) -> (id: Int64?, statement: Statement) {
470+
public func insert(values: [Setter]) -> (rowid: Int64?, statement: Statement) {
471471
let statement = insertStatement(values).run()
472-
return (statement.failed ? nil : database.lastId, statement)
472+
return (statement.failed ? nil : database.lastInsertRowid, statement)
473473
}
474474

475475
public func insert(query: Query) -> Int? { return insert(query).changes }
@@ -479,16 +479,16 @@ public struct Query {
479479
public func insert(query: Query) -> (changes: Int?, statement: Statement) {
480480
let expression = query.selectExpression
481481
let statement = database.run("INSERT INTO \(tableName.unaliased.SQL) \(expression.SQL)", expression.bindings)
482-
return (statement.failed ? nil : database.lastChanges, statement)
482+
return (statement.failed ? nil : database.changes, statement)
483483
}
484484

485-
public func insert() -> Int64? { return insert().id }
485+
public func insert() -> Int64? { return insert().rowid }
486486

487487
public func insert() -> Statement { return insert().statement }
488488

489-
public func insert() -> (id: Int64?, statement: Statement) {
489+
public func insert() -> (rowid: Int64?, statement: Statement) {
490490
let statement = database.run("INSERT INTO \(tableName.unaliased.SQL) DEFAULT VALUES")
491-
return (statement.failed ? nil : database.lastId, statement)
491+
return (statement.failed ? nil : database.lastInsertRowid, statement)
492492
}
493493

494494
/// Runs a REPLACE statement against the query.
@@ -503,14 +503,14 @@ public struct Query {
503503
/// :param: values A list of values to set.
504504
///
505505
/// :returns: The rowid.
506-
public func replace(values: Setter...) -> Int64? { return replace(values).id }
506+
public func replace(values: Setter...) -> Int64? { return replace(values).rowid }
507507

508508
/// Runs a REPLACE statement against the query.
509509
///
510510
/// :param: values A list of values to set.
511511
///
512512
/// :returns: The rowid and statement.
513-
public func replace(values: Setter...) -> (id: Int64?, statement: Statement) {
513+
public func replace(values: Setter...) -> (rowid: Int64?, statement: Statement) {
514514
return replace(values)
515515
}
516516

@@ -519,16 +519,16 @@ public struct Query {
519519
/// :param: values An array of values to set.
520520
///
521521
/// :returns: The rowid.
522-
public func replace(values: [Setter]) -> Int64? { return replace(values).id }
522+
public func replace(values: [Setter]) -> Int64? { return replace(values).rowid }
523523

524524
/// Runs a REPLACE statement against the query.
525525
///
526526
/// :param: values An array of values to set.
527527
///
528528
/// :returns: The rowid and statement.
529-
public func replace(values: [Setter]) -> (id: Int64?, statement: Statement) {
529+
public func replace(values: [Setter]) -> (rowid: Int64?, statement: Statement) {
530530
let statement = insertStatement(values, or: .Replace).run()
531-
return (statement.failed ? nil : database.lastId, statement)
531+
return (statement.failed ? nil : database.lastInsertRowid, statement)
532532
}
533533

534534
/// Runs an UPDATE statement against the query.
@@ -568,7 +568,7 @@ public struct Query {
568568
/// :returns: The number of updated rows and statement.
569569
public func update(values: [Setter]) -> (changes: Int?, statement: Statement) {
570570
let statement = updateStatement(values).run()
571-
return (statement.failed ? nil : database.lastChanges, statement)
571+
return (statement.failed ? nil : database.changes, statement)
572572
}
573573

574574
/// Runs a DELETE statement against the query.
@@ -586,7 +586,7 @@ public struct Query {
586586
/// :returns: The number of deleted rows and statement.
587587
public func delete() -> (changes: Int?, statement: Statement) {
588588
let statement = deleteStatement.run()
589-
return (statement.failed ? nil : database.lastChanges, statement)
589+
return (statement.failed ? nil : database.changes, statement)
590590
}
591591

592592
// MARK: - Aggregate Functions

0 commit comments

Comments
 (0)