add missing pragmas

This commit is contained in:
Frank Braun 2017-05-08 18:54:00 +00:00
parent 2fc2f58d75
commit 1ea89987f6
2 changed files with 31 additions and 2 deletions

View File

@ -15,7 +15,7 @@ Track files:
error.go error.go
error_test.go error_test.go
sqlite3.go (dead code and extension loading coding removed) sqlite3.go (dead code and extension loading removed, sqlcipher pragmas added)
sqlite3_other.go sqlite3_other.go
sqlite3_test.go (adjust path) sqlite3_test.go (adjust path)
sqlite3_windows.go sqlite3_windows.go

View File

@ -330,6 +330,11 @@ func errorString(err Error) string {
// "deferred", "exclusive". // "deferred", "exclusive".
// _foreign_keys=X // _foreign_keys=X
// Enable or disable enforcement of foreign keys. X can be 1 or 0. // Enable or disable enforcement of foreign keys. X can be 1 or 0.
// go-sqlcipher adds the following query parameters to those used by SQLite:
// _pragma_key=XXX
// Specify PRAGMA key.
// _pragma_cipher_page_size=XXX
// Set the PRAGMA cipher_page_size to adjust the page size.
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")
@ -340,8 +345,10 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
busyTimeout := 5000 busyTimeout := 5000
foreignKeys := -1 foreignKeys := -1
pos := strings.IndexRune(dsn, '?') pos := strings.IndexRune(dsn, '?')
var params url.Values
if pos >= 1 { if pos >= 1 {
params, err := url.ParseQuery(dsn[pos+1:]) var err error
params, err = url.ParseQuery(dsn[pos+1:])
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -440,6 +447,28 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
} }
} }
// process SQLCipher pragmas encoded in dsn, if necessary
if params != nil {
// _pragma_key
if val := params.Get("_pragma_key"); val != "" {
query := fmt.Sprintf("PRAGMA key = \"%s\";", val)
if err := exec(query); err != nil {
return nil, err
}
}
// _pragma_cipher_page_size
if val := params.Get("_pragma_cipher_page_size"); val != "" {
pageSize, err := strconv.Atoi(val)
if err != nil {
return nil, fmt.Errorf("sqlite3: _pragma_cipher_page_size cannot be parsed: %s", err)
}
query := fmt.Sprintf("PRAGMA cipher_page_size = %d;", pageSize)
if err := exec(query); err != nil {
return nil, err
}
}
}
conn := &SQLiteConn{db: db, loc: loc, txlock: txlock} conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
if d.ConnectHook != nil { if d.ConnectHook != nil {