Skip to content

Commit c4cba1d

Browse files
committed
Make Database initialization more Swift-like
Carrying over ":memory:" and "" seems less-than-ideal. Signed-off-by: Stephen Celis <stephen@stephencelis.com>
1 parent ef9c07c commit c4cba1d

File tree

2 files changed

+88
-9
lines changed

2 files changed

+88
-9
lines changed

SQLite Tests/DatabaseTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,32 @@ class DatabaseTests: SQLiteTestCase {
99
createUsersTable()
1010
}
1111

12+
func test_init_withInMemory_returnsInMemoryConnection() {
13+
let db = Database(.InMemory)
14+
XCTAssertEqual("", db.description)
15+
XCTAssertEqual(":memory:", Database.Location.InMemory.description)
16+
}
17+
18+
func test_init_returnsInMemoryByDefault() {
19+
let db = Database()
20+
XCTAssertEqual("", db.description)
21+
}
22+
23+
func test_init_withTemporary_returnsTemporaryConnection() {
24+
let db = Database(.Temporary)
25+
XCTAssertEqual("", db.description)
26+
}
27+
28+
func test_init_withURI_returnsURIConnection() {
29+
let db = Database(.URI("\(NSTemporaryDirectory())/SQLite.swift Tests.sqlite3"))
30+
XCTAssertEqual("\(NSTemporaryDirectory())/SQLite.swift Tests.sqlite3", db.description)
31+
}
32+
33+
func test_init_withString_returnsURIConnection() {
34+
let db = Database("\(NSTemporaryDirectory())/SQLite.swift Tests.sqlite3")
35+
XCTAssertEqual("\(NSTemporaryDirectory())/SQLite.swift Tests.sqlite3", db.description)
36+
}
37+
1238
func test_readonly_returnsFalseOnReadWriteConnections() {
1339
XCTAssert(!db.readonly)
1440
}

SQLite/Database.swift

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,64 @@ import sqlite3
2727
/// A connection (handle) to SQLite.
2828
public final class Database {
2929

30+
/// The location of a SQLite database.
31+
public enum Location {
32+
33+
/// An in-memory database (equivalent to `.URI(":memory:")`).
34+
///
35+
/// See: <https://www.sqlite.org/inmemorydb.html#sharedmemdb>
36+
case InMemory
37+
38+
/// A temporary, file-backed database (equivalent to `.URI("")`).
39+
///
40+
/// See: <https://www.sqlite.org/inmemorydb.html#temp_db>
41+
case Temporary
42+
43+
/// A database located at the given URI filename (or path).
44+
///
45+
/// See: <https://www.sqlite.org/uri.html>
46+
///
47+
/// :param: filename A URI filename
48+
case URI(String)
49+
50+
}
51+
3052
internal var handle: COpaquePointer = nil
3153

3254
/// Whether or not the database was opened in a read-only state.
3355
public var readonly: Bool { return sqlite3_db_readonly(handle, nil) == 1 }
3456

35-
/// Instantiates a new connection to a database.
57+
/// Initializes a new connection to a database.
3658
///
37-
/// :param: path The path to the database. Creates a new database if it
38-
/// doesn’t already exist (unless in read-only mode). Pass
39-
/// ":memory:" (or nothing) to open a new, in-memory
40-
/// database. Pass "" (or nil) to open a temporary,
41-
/// file-backed database. Default: ":memory:".
59+
/// :param: location The location of the database. Creates a new database if
60+
/// it doesn’t already exist (unless in read-only mode).
61+
///
62+
/// Default: `.InMemory`.
4263
///
4364
/// :param: readonly Whether or not to open the database in a read-only
44-
/// state. Default: false.
65+
/// state.
66+
///
67+
/// Default: `false`.
4568
///
4669
/// :returns: A new database connection.
47-
public init(_ path: String? = ":memory:", readonly: Bool = false) {
70+
public init(_ location: Location = .InMemory, readonly: Bool = false) {
4871
let flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE
49-
try { sqlite3_open_v2(path ?? "", &self.handle, flags | SQLITE_OPEN_FULLMUTEX, nil) }
72+
try { sqlite3_open_v2(location.description, &self.handle, flags | SQLITE_OPEN_FULLMUTEX, nil) }
73+
}
74+
75+
/// Initializes a new connection to a database.
76+
///
77+
/// :param: filename The location of the database. Creates a new database if
78+
/// it doesn’t already exist (unless in read-only mode).
79+
///
80+
/// :param: readonly Whether or not to open the database in a read-only
81+
/// state.
82+
///
83+
/// Default: `false`.
84+
///
85+
/// :returns: A new database connection.
86+
public convenience init(_ filename: String, readonly: Bool = false) {
87+
self.init(.URI(filename), readonly: readonly)
5088
}
5189

5290
deinit { try { sqlite3_close(self.handle) } } // sqlite3_close_v2 in Yosemite/iOS 8?
@@ -583,6 +621,21 @@ extension Database: Printable {
583621

584622
}
585623

624+
extension Database.Location: Printable {
625+
626+
public var description: String {
627+
switch self {
628+
case .InMemory:
629+
return ":memory:"
630+
case .Temporary:
631+
return ""
632+
case .URI(let URI):
633+
return URI
634+
}
635+
}
636+
637+
}
638+
586639
internal func quote(#literal: String) -> String {
587640
return quote(literal, "'")
588641
}

0 commit comments

Comments
 (0)