Commit c4bfbe45 authored by gwenn's avatar gwenn

Add shortcut methods to invoke some pragmas.

parent b5cfeaf5
......@@ -142,7 +142,7 @@ type ForeignKey struct {
}
// Executes pragma 'foreign_key_list'
// TODO How to specify a database-name?
// TODO Make possible to specify the database-name (PRAGMA %Q.foreign_key_list(%Q))
func (c *Conn) ForeignKeys(table string) (map[int]*ForeignKey, error) {
s, err := c.Prepare(Mprintf("PRAGMA foreign_key_list(%Q)", table))
if err != nil {
......@@ -179,7 +179,7 @@ type Index struct {
}
// Executes pragma 'index_list'
// TODO How to specify a database-name?
// TODO Make possible to specify the database-name (PRAGMA %Q.index_list(%Q))
func (c *Conn) Indexes(table string) ([]Index, error) {
s, err := c.Prepare(Mprintf("PRAGMA index_list(%Q)", table))
if err != nil {
......
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqlite
import (
"fmt"
)
// Check database integrity
// (See http://www.sqlite.org/pragma.html#pragma_integrity_check
// and http://www.sqlite.org/pragma.html#pragma_quick_check)
// TODO Make possible to specify the database-name (PRAGMA %Q.integrity_check(.))
func (c *Conn) IntegrityCheck(max int, quick bool) error {
var pragma string
if quick {
pragma = "quick"
} else {
pragma = "integrity"
}
msg, err := c.OneValue(fmt.Sprintf("PRAGMA %s_check(%d)", pragma, max))
if err != nil {
return err
}
if msg != "ok" {
return c.specificError("Integrity check failed (%s)", msg)
}
return nil
}
// Returns the text encoding used by the main database
// (See http://sqlite.org/pragma.html#pragma_encoding)
// TODO Make possible to specify the database-name (PRAGMA %Q.encoding)
func (c *Conn) Encoding() (string, error) {
value, err := c.OneValue("PRAGMA encoding")
if err != nil {
return "", err
}
if encoding, ok := value.(string); ok {
return encoding, nil
}
return "", c.specificError("Unexpected encoding (%v)", value)
}
// (See http://sqlite.org/pragma.html#pragma_schema_version)
// TODO Make possible to specify the database-name (PRAGMA %Q.schema_version)
func (c *Conn) SchemaVersion() (int64, error) {
value, err := c.OneValue("PRAGMA schema_version")
if err != nil {
return -1, err
}
if version, ok := value.(int64); ok {
return version, nil
}
return -1, c.specificError("Unexpected version (%v)", value)
}
\ No newline at end of file
package sqlite_test
import (
// . "github.com/gwenn/gosqlite"
"testing"
)
func TestIntegrityCheck(t *testing.T) {
db := open(t)
defer db.Close()
checkNoError(t, db.IntegrityCheck(1, true), "Error checking integrity of database: %s")
}
func TestEncoding(t *testing.T) {
db := open(t)
defer db.Close()
encoding, err := db.Encoding()
checkNoError(t, err, "Error reading encoding of database: %s")
if encoding != "UTF-8" {
t.Errorf("Expecting %s but got %s", "UTF-8", encoding)
}
}
func TestSchemaVersion(t *testing.T) {
db := open(t)
defer db.Close()
version, err := db.SchemaVersion()
checkNoError(t, err, "Error reading schema version of database: %s")
if version != 0 {
t.Errorf("Expecting %d but got %d", 0, version)
}
}
......@@ -1286,26 +1286,6 @@ func EnableSharedCache(b bool) {
C.sqlite3_enable_shared_cache(btocint(b))
}
// Check database integrity
// (See http://www.sqlite.org/pragma.html#pragma_integrity_check
// and http://www.sqlite.org/pragma.html#pragma_quick_check)
func (c *Conn) IntegrityCheck(max int, quick bool) error {
var pragma string
if quick {
pragma = "quick"
} else {
pragma = "integrity"
}
msg, err := c.OneValue(fmt.Sprintf("PRAGMA %s_check(%d)", pragma, max))
if err != nil {
return err
}
if msg != "ok" {
return c.specificError("Integrity check failed (%s)", msg)
}
return nil
}
// Must is a helper that wraps a call to a function returning (bool, os.Error)
// and panics if the error is non-nil.
func Must(b bool, err error) bool {
......
......@@ -60,12 +60,6 @@ func TestEnableExtendedResultCodes(t *testing.T) {
checkNoError(t, db.EnableExtendedResultCodes(true), "cannot enabled extended result codes: %s")
}
func TestIntegrityCheck(t *testing.T) {
db := open(t)
defer db.Close()
checkNoError(t, db.IntegrityCheck(1, true), "Error checking integrity of database: %s")
}
func TestCreateTable(t *testing.T) {
db := open(t)
defer db.Close()
......
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