@@ -27,26 +27,64 @@ import sqlite3
27
27
/// A connection (handle) to SQLite.
28
28
public final class Database {
29
29
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
+
30
52
internal var handle : COpaquePointer = nil
31
53
32
54
/// Whether or not the database was opened in a read-only state.
33
55
public var readonly : Bool { return sqlite3_db_readonly ( handle, nil ) == 1 }
34
56
35
- /// Instantiates a new connection to a database.
57
+ /// Initializes a new connection to a database.
36
58
///
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`.
42
63
///
43
64
/// :param: readonly Whether or not to open the database in a read-only
44
- /// state. Default: false.
65
+ /// state.
66
+ ///
67
+ /// Default: `false`.
45
68
///
46
69
/// :returns: A new database connection.
47
- public init ( _ path : String ? = " :memory: " , readonly: Bool = false ) {
70
+ public init ( _ location : Location = . InMemory , readonly: Bool = false ) {
48
71
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)
50
88
}
51
89
52
90
deinit { try { sqlite3_close ( self . handle) } } // sqlite3_close_v2 in Yosemite/iOS 8?
@@ -583,6 +621,21 @@ extension Database: Printable {
583
621
584
622
}
585
623
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
+
586
639
internal func quote( #literal: String) -> String {
587
640
return quote ( literal, " ' " )
588
641
}
0 commit comments