From f9701636b0534d12f116211e8ec1028d89a0e32a Mon Sep 17 00:00:00 2001 From: Vojtech Bocek Date: Mon, 9 Nov 2015 12:27:09 +0100 Subject: [PATCH] Implement read-only mode via _readonly=true flag in Open() Signed-off-by: Vojtech Bocek --- sqlite3.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index 3af2752..9b855b8 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -597,6 +597,8 @@ func errorString(err Error) string { // _txlock=XXX // Specify locking behavior for transactions. XXX can be "immediate", // "deferred", "exclusive". +// _readonly=true +// Open database in read-only mode (SQLITE_OPEN_READONLY) func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { if C.sqlite3_threadsafe() == 0 { return nil, errors.New("sqlite library was not compiled for thread-safe operation") @@ -605,6 +607,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { var loc *time.Location txlock := "BEGIN" busy_timeout := 5000 + openflags := C.int(C.SQLITE_OPEN_FULLMUTEX | C.SQLITE_OPEN_READWRITE | C.SQLITE_OPEN_CREATE) pos := strings.IndexRune(dsn, '?') if pos >= 1 { params, err := url.ParseQuery(dsn[pos+1:]) @@ -647,6 +650,11 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { } } + // _readonly + if params.Get("_readonly") == "true" { + openflags = C.SQLITE_OPEN_READONLY | C.SQLITE_OPEN_FULLMUTEX + } + if !strings.HasPrefix(dsn, "file:") { dsn = dsn[:pos] } @@ -655,11 +663,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { var db *C.sqlite3 name := C.CString(dsn) defer C.free(unsafe.Pointer(name)) - rv := C._sqlite3_open_v2(name, &db, - C.SQLITE_OPEN_FULLMUTEX| - C.SQLITE_OPEN_READWRITE| - C.SQLITE_OPEN_CREATE, - nil) + rv := C._sqlite3_open_v2(name, &db, openflags, nil) if rv != 0 { return nil, Error{Code: ErrNo(rv)} }