Commit a4a86761 authored by gwenn's avatar gwenn

List databases.

parent b45166a1
......@@ -22,6 +22,30 @@ import (
"unsafe"
)
// Executes pragma 'database_list'
func (c *Conn) Databases() (map[string]string, os.Error) {
s, err := c.Prepare("PRAGMA database_list")
if err != nil {
return nil, err
}
defer s.Finalize()
var databases map[string]string = make(map[string]string)
var ok bool
var name, file string
var seq int
for ok, err = s.Next(); ok; ok, err = s.Next() {
err = s.Scan(&seq, &name, &file)
if err != nil {
return nil, err
}
databases[name] = file
}
if err != nil {
return nil, err
}
return databases, nil
}
// Selects tables (no view) from 'sqlite_master' and filters system tables out.
func (c *Conn) Tables() ([]string, os.Error) {
s, err := c.Prepare("SELECT name FROM sqlite_master WHERE type IN ('table') AND name NOT LIKE 'sqlite_%'")
......@@ -53,6 +77,7 @@ type Column struct {
// Executes pragma 'table_info'
// TODO How to specify a database-name?
// TODO sqlite3_table_column_metadata?
func (c *Conn) Columns(table string) ([]Column, os.Error) {
s, err := c.Prepare(Mprintf("PRAGMA table_info(%Q)", table))
if err != nil {
......
......@@ -413,7 +413,6 @@ func (s *Stmt) BindParameterName(i int) string {
return C.GoString(C.sqlite3_bind_parameter_name(s.stmt, C.int(i)))
}
// Calls sqlite3_bind_parameter_count and sqlite3_bind_(blob|double|int|int64|null|text) depending on args type.
// http://sqlite.org/c3ref/bind_blob.html
func (s *Stmt) Bind(args ...interface{}) os.Error {
......@@ -710,7 +709,7 @@ func (c *Conn) Close() os.Error {
C.sqlite3_finalize(stmt)
stmt = C.sqlite3_next_stmt(c.db, stmt)
}
rv := C.sqlite3_close(c.db)
if rv != C.SQLITE_OK {
return c.error(rv)
......@@ -721,7 +720,7 @@ func (c *Conn) Close() os.Error {
// Calls http://sqlite.org/c3ref/stmt_readonly.html
func (s *Stmt) ReadOnly() bool {
return C.sqlite3_stmt_readonly(s.stmt) == 1;
return C.sqlite3_stmt_readonly(s.stmt) == 1
}
// Calls http://sqlite.org/c3ref/enable_load_extension.html
......
......@@ -278,6 +278,22 @@ func TestInsertWithStatement(t *testing.T) {
}
}
func TestDatabases(t *testing.T) {
db := open(t)
defer db.Close()
databases, err := db.Databases()
if err != nil {
t.Fatalf("error looking for databases: %s", err)
}
if len(databases) != 1 {
t.Errorf("Expected one database but got %d\n", len(databases))
}
if _, ok := databases["main"]; !ok {
t.Errorf("Expected 'main' database\n")
}
}
func TestTables(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