@@ -25,6 +25,7 @@ type Conn struct {
2525 ConnCache
2626
2727 c chan struct {}
28+ f SQFlag
2829 ctx context.Context
2930}
3031
@@ -38,11 +39,24 @@ type TxnFunc func(SQTransaction) error
3839////////////////////////////////////////////////////////////////////////////////
3940// LIFECYCLE
4041
41- func OpenPath (path string , flags sqlite3.OpenFlags ) (* Conn , error ) {
42+ // New creates an in-memory database. Pass any flags to set open options. If
43+ // no flags are provided, the default is to create a read/write database.
44+ func New (flags ... SQFlag ) (* Conn , error ) {
45+ f := SQFlag (0 )
46+ if len (flags ) == 0 {
47+ f |= SQFlag (sqlite3 .DefaultFlags )
48+ }
49+ for _ , flag := range flags {
50+ f |= flag
51+ }
52+ return OpenPath (defaultMemory , f )
53+ }
54+
55+ func OpenPath (path string , flags SQFlag ) (* Conn , error ) {
4256 conn := new (Conn )
4357
4458 // If no create flag then check to make sure database exists
45- if path != defaultMemory && flags & sqlite3 .SQLITE_OPEN_CREATE == 0 {
59+ if path != defaultMemory && flags & SQFlag ( sqlite3 .SQLITE_OPEN_MEMORY ) == 0 && SQFlag ( sqlite3 . SQLITE_OPEN_CREATE ) == 0 {
4660 if _ , err := os .Stat (path ); os .IsNotExist (err ) {
4761 return nil , ErrNotFound .Withf ("%q" , path )
4862 } else if err != nil {
@@ -51,14 +65,15 @@ func OpenPath(path string, flags sqlite3.OpenFlags) (*Conn, error) {
5165 }
5266
5367 // Open database with flags
54- if c , err := sqlite3 .OpenPathEx (path , flags , "" ); err != nil {
68+ if c , err := sqlite3 .OpenPathEx (path , sqlite3 . OpenFlags ( flags ) , "" ); err != nil {
5569 return nil , err
5670 } else {
5771 conn .ConnEx = c
72+ conn .f = flags
5873 }
5974
6075 // Set cache to default size
61- if flags & sqlite3 . SQLITE_OPEN_CONNCACHE != 0 {
76+ if flags & SQLITE_OPEN_CACHE != 0 {
6277 conn .SetCap (defaultCapacity )
6378 } else {
6479 conn .SetCap (0 )
@@ -97,7 +112,7 @@ func (conn *Conn) Exec(st SQStatement, fn ExecFunc) error {
97112}
98113
99114// Perform a transaction, rollback if error is returned
100- func (conn * Conn ) Do (ctx context.Context , flag SQTxnFlag , fn func (SQTransaction ) error ) error {
115+ func (conn * Conn ) Do (ctx context.Context , flag SQFlag , fn func (SQTransaction ) error ) error {
101116 conn .Mutex .Lock ()
102117 defer conn .Mutex .Unlock ()
103118
@@ -117,7 +132,7 @@ func (conn *Conn) Do(ctx context.Context, flag SQTxnFlag, fn func(SQTransaction)
117132 }
118133 }
119134
120- // Flags
135+ // Transaction flags
121136 v := sqlite3 .SQLITE_TXN_DEFAULT
122137 if flag .Is (SQLITE_TXN_EXCLUSIVE ) {
123138 v = sqlite3 .SQLITE_TXN_EXCLUSIVE
@@ -185,3 +200,8 @@ func (txn *Txn) Query(st SQStatement, v ...interface{}) (SQResults, error) {
185200 return r , nil
186201 }
187202}
203+
204+ // Flags returns the Open Flags
205+ func (c * Conn ) Flags () SQFlag {
206+ return c .f
207+ }
0 commit comments