Commit ec392678 authored by gwenn's avatar gwenn

Adds helpers for pragma 'locking_mode' & 'synchronous'.

parent 3f04bdc0
......@@ -86,6 +86,49 @@ func (c *Conn) SetJournalMode(dbName, mode string) (string, error) {
return newMode, nil
}
// LockingMode queries the database connection locking-mode.
// Database name is optional (default is 'main').
// (See http://sqlite.org/pragma.html#pragma_locking_mode)
func (c *Conn) LockingMode(dbName string) (string, error) {
var mode string
err := c.OneValue(pragma(dbName, "locking_mode"), &mode)
if err != nil {
return "", err
}
return mode, nil
}
// SetLockingMode changes the database connection locking-mode.
// Database name is optional (default is 'main').
// (See http://sqlite.org/pragma.html#pragma_locking_mode)
func (c *Conn) SetLockingMode(dbName, mode string) (string, error) {
var newMode string
err := c.OneValue(pragma(dbName, Mprintf("locking_mode=%Q", mode)), &newMode)
if err != nil {
return "", err
}
return newMode, nil
}
// Synchronous queries the synchronous flag.
// Database name is optional (default is 'main').
// (See http://sqlite.org/pragma.html#pragma_synchronous)
func (c *Conn) Synchronous(dbName string) (int, error) {
var mode int
err := c.OneValue(pragma(dbName, "synchronous"), &mode)
if err != nil {
return -1, err
}
return mode, nil
}
// SetSynchronous changes the synchronous flag.
// Database name is optional (default is 'main').
// (See http://sqlite.org/pragma.html#pragma_synchronous)
func (c *Conn) SetSynchronous(dbName string, mode int) error {
return c.exec(pragma(dbName, fmt.Sprintf("synchronous=%d", mode)))
}
func pragma(dbName, pragmaName string) string {
if len(dbName) == 0 {
return "PRAGMA " + pragmaName
......
......@@ -45,3 +45,37 @@ func TestSetJournalMode(t *testing.T) {
checkNoError(t, err, "Error setting journaling mode of database: %s")
assertEquals(t, "expecting %s but got %s", "off", mode)
}
func TestLockingMode(t *testing.T) {
db := open(t)
defer db.Close()
mode, err := db.LockingMode("")
checkNoError(t, err, "Error reading locking-mode of database: %s")
assertEquals(t, "expecting %s but got %s", "normal", mode)
}
func TestSetLockingMode(t *testing.T) {
db := open(t)
defer db.Close()
mode, err := db.SetLockingMode("", "exclusive")
checkNoError(t, err, "Error setting locking-mode of database: %s")
assertEquals(t, "expecting %s but got %s", "exclusive", mode)
}
func TestSynchronous(t *testing.T) {
db := open(t)
defer db.Close()
mode, err := db.Synchronous("")
checkNoError(t, err, "Error reading synchronous flag of database: %s")
assertEquals(t, "expecting %d but got %d", 2, mode)
}
func TestSetSynchronous(t *testing.T) {
db := open(t)
defer db.Close()
err := db.SetSynchronous("", 0)
checkNoError(t, err, "Error setting synchronous flag of database: %s")
mode, err := db.Synchronous("")
checkNoError(t, err, "Error reading synchronous flag of database: %s")
assertEquals(t, "expecting %d but got %d", 0, mode)
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment