@@ -27,26 +27,64 @@ import sqlite3
2727/// A connection (handle) to SQLite.
2828public  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+ 
586639internal  func  quote( #literal:  String)  ->  String  { 
587640    return  quote ( literal,  " ' " ) 
588641} 
0 commit comments