Commit efc5245d authored by gwenn's avatar gwenn

Add partial support to metadata.

 - list tables,
 - list table columns.
parent 17ef1633
......@@ -8,6 +8,7 @@ TARG=github.com/gwenn/sqlite
CGOFILES=\
sqlite.go\
backup.go
backup.go\
meta.go
include $(GOROOT)/src/Make.pkg
// 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 provides access to the SQLite library, version 3.
package sqlite
/*
#include <sqlite3.h>
#include <stdlib.h>
// cgo doesn't support varargs
static char *my_mprintf(char *zFormat, char *arg) {
return sqlite3_mprintf(zFormat, arg);
}
*/
import "C"
import (
"os"
"unsafe"
)
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_%'")
if err != nil {
return nil, err
}
var tables []string = make([]string, 0, 20)
var ok bool
var name string
for ok, err = s.Next(); ok; ok, err = s.Next() {
s.Scan(&name)
tables = append(tables, name)
}
if err != nil {
return nil, err
}
return tables, nil
}
type Column struct {
cid int
name string
dataType string
notNull bool
dfltValue string // FIXME type ?
pk bool
}
func (c *Conn) Columns(table string) ([]Column, os.Error) {
s, err := c.Prepare(Mprintf("pragma table_info(%Q)", table))
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
var columns []Column = make([]Column, 0, 20)
var ok bool
for ok, err = s.Next(); ok; ok, err = s.Next() {
c := Column{}
s.Scan(&c.cid, &c.name, &c.dataType, &c.notNull, &c.dfltValue, &c.pk)
columns = append(columns, c)
}
return columns, nil
}
func Mprintf(format string, arg string) string {
cf := C.CString(format)
defer C.free(unsafe.Pointer(cf))
ca := C.CString(arg)
defer C.free(unsafe.Pointer(ca))
zSQL := C.my_mprintf(cf, ca)
defer C.sqlite3_free(unsafe.Pointer(zSQL))
return C.GoString(zSQL)
}
......@@ -26,7 +26,6 @@ static int my_bind_text(sqlite3_stmt *stmt, int n, char *p, int np) {
static int my_bind_blob(sqlite3_stmt *stmt, int n, void *p, int np) {
return sqlite3_bind_blob(stmt, n, p, np, SQLITE_TRANSIENT);
}
*/
import "C"
......
......@@ -177,6 +177,40 @@ func TestInsertWithStatement(t *testing.T) {
}
}
func TestTables(t *testing.T) {
db := open(t)
defer db.Close()
tables, err := db.Tables()
if err != nil {
t.Fatalf("error looking for tables: %s", err)
}
if len(tables) != 0 {
t.Errorf("Expected no table but got %d\n", len(tables))
}
createTable(db, t)
tables, err = db.Tables()
if err != nil {
t.Fatalf("error looking for tables: %s", err)
}
if len(tables) != 1 {
t.Errorf("Expected one table but got %d\n", len(tables))
}
if tables[0] != "test" {
t.Errorf("Wrong table name: 'test' <> %s\n", tables[0])
}
columns, err := db.Columns("test")
if err != nil {
t.Fatalf("error looking for columns: %s", err)
}
if len(columns) != 4 {
t.Errorf("Expected 4 columns <> %d", len(columns))
}
column := columns[2]
if column.name != "int_num" {
t.Errorf("Wrong column name: 'int_num' <> %s", column.name)
}
}
func BenchmarkScan(b *testing.B) {
b.StopTimer()
db, _ := Open("")
......
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