mirror of https://github.com/mattn/go-sqlite3.git
Implement read-only mode via _readonly=true flag in Open()
Signed-off-by: Vojtech Bocek <vojtech.bocek@avg.com>
This commit is contained in:
parent
5651a9d9d4
commit
f9701636b0
14
sqlite3.go
14
sqlite3.go
|
@ -597,6 +597,8 @@ func errorString(err Error) string {
|
||||||
// _txlock=XXX
|
// _txlock=XXX
|
||||||
// Specify locking behavior for transactions. XXX can be "immediate",
|
// Specify locking behavior for transactions. XXX can be "immediate",
|
||||||
// "deferred", "exclusive".
|
// "deferred", "exclusive".
|
||||||
|
// _readonly=true
|
||||||
|
// Open database in read-only mode (SQLITE_OPEN_READONLY)
|
||||||
func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
|
func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
|
||||||
if C.sqlite3_threadsafe() == 0 {
|
if C.sqlite3_threadsafe() == 0 {
|
||||||
return nil, errors.New("sqlite library was not compiled for thread-safe operation")
|
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
|
var loc *time.Location
|
||||||
txlock := "BEGIN"
|
txlock := "BEGIN"
|
||||||
busy_timeout := 5000
|
busy_timeout := 5000
|
||||||
|
openflags := C.int(C.SQLITE_OPEN_FULLMUTEX | C.SQLITE_OPEN_READWRITE | C.SQLITE_OPEN_CREATE)
|
||||||
pos := strings.IndexRune(dsn, '?')
|
pos := strings.IndexRune(dsn, '?')
|
||||||
if pos >= 1 {
|
if pos >= 1 {
|
||||||
params, err := url.ParseQuery(dsn[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:") {
|
if !strings.HasPrefix(dsn, "file:") {
|
||||||
dsn = dsn[:pos]
|
dsn = dsn[:pos]
|
||||||
}
|
}
|
||||||
|
@ -655,11 +663,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
|
||||||
var db *C.sqlite3
|
var db *C.sqlite3
|
||||||
name := C.CString(dsn)
|
name := C.CString(dsn)
|
||||||
defer C.free(unsafe.Pointer(name))
|
defer C.free(unsafe.Pointer(name))
|
||||||
rv := C._sqlite3_open_v2(name, &db,
|
rv := C._sqlite3_open_v2(name, &db, openflags, nil)
|
||||||
C.SQLITE_OPEN_FULLMUTEX|
|
|
||||||
C.SQLITE_OPEN_READWRITE|
|
|
||||||
C.SQLITE_OPEN_CREATE,
|
|
||||||
nil)
|
|
||||||
if rv != 0 {
|
if rv != 0 {
|
||||||
return nil, Error{Code: ErrNo(rv)}
|
return nil, Error{Code: ErrNo(rv)}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue