Skip to content

Commit 24cbd40

Browse files
committed
ADD: PRAGMA writable_schema
1 parent cb04140 commit 24cbd40

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Boolean values can be one of:
9292
| Synchronous | `_synchronous` \| `_sync` | <ul><li>0 \| OFF</li><li>1 \| NORMAL</li><li>2 \| FULL</li><li>3 \| EXTRA</li></ul> | For more information see [PRAGMA synchronous](https://www.sqlite.org/pragma.html#pragma_synchronous) |
9393
| Time Zone Location | `_loc` | auto | Specify location of time format. |
9494
| Transaction Lock | `_txlock` | <ul><li>immediate</li><li>deferred</li><li>exclusive</li></ul> | Specify locking behavior for transactions. |
95+
| Writable Schema | `_writable_schema` | `Boolean` | When this pragma is on, the SQLITE_MASTER tables in which database can be changed using ordinary UPDATE, INSERT, and DELETE statements. Warning: misuse of this pragma can easily result in a corrupt database file. |
9596

9697
## DSN Examples
9798

sqlite3.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,11 @@ func errorString(err Error) string {
830830
// Specify locking behavior for transactions. XXX can be "immediate",
831831
// "deferred", "exclusive".
832832
//
833+
// _auto_vacuum=X | _vacuum=X
834+
// 0 | none - Auto Vacuum disabled
835+
// 1 | full - Auto Vacuum FULL
836+
// 2 | incremental - Auto Vacuum Incremental
837+
//
833838
// _busy_timeout=XXX"| _timeout=XXX
834839
// Specify value for sqlite3_busy_timeout.
835840
//
@@ -870,10 +875,12 @@ func errorString(err Error) string {
870875
// Change the setting of the "synchronous" flag.
871876
// https://www.sqlite.org/pragma.html#pragma_synchronous
872877
//
873-
// _vacuum=X
874-
// 0 | none - Auto Vacuum disabled
875-
// 1 | full - Auto Vacuum FULL
876-
// 2 | incremental - Auto Vacuum Incremental
878+
// _writable_schema=Boolean
879+
// When this pragma is on, the SQLITE_MASTER tables in which database
880+
// can be changed using ordinary UPDATE, INSERT, and DELETE statements.
881+
// Warning: misuse of this pragma can easily result in a corrupt database file.
882+
//
883+
//
877884
func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
878885
if C.sqlite3_threadsafe() == 0 {
879886
return nil, errors.New("sqlite library was not compiled for thread-safe operation")
@@ -899,6 +906,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
899906
recursiveTriggers := -1
900907
secureDelete := "DEFAULT"
901908
synchronousMode := "NORMAL"
909+
writableSchema := -1
902910

903911
pos := strings.IndexRune(dsn, '?')
904912
if pos >= 1 {
@@ -1162,6 +1170,21 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
11621170
}
11631171
}
11641172

1173+
// Writable Schema (_writeable_schema)
1174+
//
1175+
// https://www.sqlite.org/pragma.html#pragma_writeable_schema
1176+
//
1177+
if val := params.Get("_writable_schema"); val != "" {
1178+
switch strings.ToLower(val) {
1179+
case "0", "no", "false", "off":
1180+
writableSchema = 0
1181+
case "1", "yes", "true", "on":
1182+
writableSchema = 1
1183+
default:
1184+
return nil, fmt.Errorf("Invalid _writable_schema: %v, expecting boolean value of '0 1 false true no yes off on'", val)
1185+
}
1186+
}
1187+
11651188
if !strings.HasPrefix(dsn, "file:") {
11661189
dsn = dsn[:pos]
11671190
}
@@ -1287,6 +1310,14 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
12871310
return nil, err
12881311
}
12891312

1313+
// Writable Schema
1314+
if writableSchema > -1 {
1315+
if err := exec(fmt.Sprintf("PRAGMA writable_schema = %d;", writableSchema)); err != nil {
1316+
C.sqlite3_close_v2(db)
1317+
return nil, err
1318+
}
1319+
}
1320+
12901321
conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
12911322

12921323
if len(d.Extensions) > 0 {

0 commit comments

Comments
 (0)