Skip to content

Commit 9ad002d

Browse files
committed
Enable SEE for external library
* Add DSN key * Add PRAGMA execution * Add SEE for external libsqlite3 library Closes mattn#162 Closes mattn#487
1 parent 49dc629 commit 9ad002d

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

driver/config.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,9 @@ type Config struct {
466466
// Database
467467
Database string
468468

469+
// Encryption Key
470+
Key string
471+
469472
// Mode of the SQLite database
470473
Mode Mode
471474

@@ -603,6 +606,10 @@ func (cfg *Config) FormatDSN() string {
603606
var buf bytes.Buffer
604607

605608
params := url.Values{}
609+
if len(cfg.Key) > 0 {
610+
params.Set("key", cfg.Key)
611+
}
612+
606613
if len(cfg.Cache.String()) > 0 {
607614
params.Set("cache", cfg.Cache.String())
608615
}
@@ -759,6 +766,22 @@ func (cfg *Config) createConnection() (driver.Conn, error) {
759766
return nil, errors.New("sqlite succeeded without returning a database")
760767
}
761768

769+
// Create basic connection
770+
conn := &SQLiteConn{
771+
cfg: cfg,
772+
db: db,
773+
tz: cfg.TimeZone,
774+
txlock: cfg.TransactionLock.Value(),
775+
}
776+
777+
// Handle Encryption key before anything else
778+
if len(cfg.Key) > 0 {
779+
if err := conn.PRAGMA(PRAGMA_SEE_KEY, cfg.Key); err != nil {
780+
C.sqlite3_close_v2(db)
781+
return nil, err
782+
}
783+
}
784+
762785
// Set SQLITE Busy Timeout Handler
763786
if cfg.BusyTimeout > 0 {
764787
rv = C.sqlite3_busy_timeout(db, C.int(cfg.BusyTimeout))
@@ -771,14 +794,6 @@ func (cfg *Config) createConnection() (driver.Conn, error) {
771794
}
772795
}
773796

774-
// Create basic connection
775-
conn := &SQLiteConn{
776-
cfg: cfg,
777-
db: db,
778-
tz: cfg.TimeZone,
779-
txlock: cfg.TransactionLock.Value(),
780-
}
781-
782797
// At this point we have the following
783798
// - database pointer
784799
// - basic connection
@@ -1021,6 +1036,11 @@ func ParseDSN(dsn string) (cfg *Config, err error) {
10211036
dsn = dsn[:pos]
10221037
}
10231038

1039+
// Parse encryption key
1040+
if val := params.Get("key"); val != "" {
1041+
cfg.Key = val
1042+
}
1043+
10241044
// Parse Autentication
10251045
if val := params.Get("user"); val != "" {
10261046
cfg.Authentication.Username = val

driver/connection.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ type SQLiteConn struct {
5050
func (c *SQLiteConn) PRAGMA(name, value string) error {
5151
stmt := fmt.Sprintf("PRAGMA %s = %s;", name, value)
5252

53+
// Use switch for future modifications to PRAGMA's
54+
switch name {
55+
case PRAGMA_SEE_KEY, PRAGMA_SEE_REKEY:
56+
stmt = fmt.Sprintf("PRAGMA %s = '%s'", name, strings.Replace(value, "'", "''", -1))
57+
}
58+
5359
cs := C.CString(stmt)
5460
rv := C.sqlite3_exec(c.db, cs, nil, nil, nil)
5561
C.free(unsafe.Pointer(cs))

driver/libsqlite_see.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (C) 2018 The Go-SQLite3 Authors.
2+
//
3+
// Use of this source code is governed by an MIT-style
4+
// license that can be found in the LICENSE file.
5+
6+
// +build libsqlite3
7+
// +build sqlite_see
8+
9+
package sqlite3
10+
11+
/*
12+
#cgo CFLAGS: -DSQLITE_HAS_CODEC=1
13+
*/
14+
import "C"

driver/pragma.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package sqlite3
99

1010
const (
11-
PRAGMA_SSE_KEY = "key"
1211
PRAGMA_AUTO_VACUUM = "auto_vacuum"
1312
PRAGMA_CASE_SENSITIVE_LIKE = "case_sensitive_like"
1413
PRAGMA_DEFER_FOREIGN_KEYS = "defer_foreign_keys"
@@ -18,6 +17,8 @@ const (
1817
PRAGMA_LOCKING_MODE = "locking_mode"
1918
PRAGMA_QUERY_ONLY = "query_only"
2019
PRAGMA_RECURSIVE_TRIGGERS = "recursive_triggers"
20+
PRAGMA_SEE_KEY = "key"
21+
PRAGMA_SEE_REKEY = "rekey"
2122
PRAGMA_SECURE_DELETE = "secure_delete"
2223
PRAGMA_SYNCHRONOUS = "synchronous"
2324
PRAGMA_WRITABLE_SCHEMA = "writable_schema"

0 commit comments

Comments
 (0)