Commit 3f04bdc0 authored by gwenn's avatar gwenn

Adds helpers for pragma 'journal_mode'.

parent 0f8a7605
...@@ -62,6 +62,30 @@ func (c *Conn) SetRecursiveTriggers(dbName string, on bool) error { ...@@ -62,6 +62,30 @@ func (c *Conn) SetRecursiveTriggers(dbName string, on bool) error {
return c.exec(pragma(dbName, fmt.Sprintf("recursive_triggers=%t", on))) return c.exec(pragma(dbName, fmt.Sprintf("recursive_triggers=%t", on)))
} }
// JournalMode queries the current journaling mode for database.
// Database name is optional (default is 'main').
// (See http://sqlite.org/pragma.html#pragma_journal_mode)
func (c *Conn) JournalMode(dbName string) (string, error) {
var mode string
err := c.OneValue(pragma(dbName, "journal_mode"), &mode)
if err != nil {
return "", err
}
return mode, nil
}
// SetJournalMode changes the journaling mode for database.
// Database name is optional (default is 'main').
// (See http://sqlite.org/pragma.html#pragma_journal_mode)
func (c *Conn) SetJournalMode(dbName, mode string) (string, error) {
var newMode string
err := c.OneValue(pragma(dbName, Mprintf("journal_mode=%Q", mode)), &newMode)
if err != nil {
return "", err
}
return newMode, nil
}
func pragma(dbName, pragmaName string) string { func pragma(dbName, pragmaName string) string {
if len(dbName) == 0 { if len(dbName) == 0 {
return "PRAGMA " + pragmaName return "PRAGMA " + pragmaName
......
...@@ -29,3 +29,19 @@ func TestSchemaVersion(t *testing.T) { ...@@ -29,3 +29,19 @@ func TestSchemaVersion(t *testing.T) {
checkNoError(t, err, "Error reading schema version of database: %s") checkNoError(t, err, "Error reading schema version of database: %s")
assertEquals(t, "expecting %d but got %d", 0, version) assertEquals(t, "expecting %d but got %d", 0, version)
} }
func TestJournalMode(t *testing.T) {
db := open(t)
defer db.Close()
mode, err := db.JournalMode("")
checkNoError(t, err, "Error reading journaling mode of database: %s")
assertEquals(t, "expecting %s but got %s", "memory", mode)
}
func TestSetJournalMode(t *testing.T) {
db := open(t)
defer db.Close()
mode, err := db.SetJournalMode("", "OFF")
checkNoError(t, err, "Error setting journaling mode of database: %s")
assertEquals(t, "expecting %s but got %s", "off", 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