Commit 30d005b4 authored by gwenn's avatar gwenn

Add method to list FKs.

parent 7dc71ca7
......@@ -40,12 +40,12 @@ func (c *Conn) Tables() ([]string, os.Error) {
}
type Column struct {
cid int
name string
dataType string
notNull bool
dfltValue string // FIXME type ?
pk bool
Cid int
Name string
DataType string
NotNull bool
DfltValue string // FIXME type ?
Pk bool
}
func (c *Conn) Columns(table string) ([]Column, os.Error) {
......@@ -53,19 +53,51 @@ func (c *Conn) Columns(table string) ([]Column, os.Error) {
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)
err = s.Scan(&c.Cid, &c.Name, &c.DataType, &c.NotNull, &c.DfltValue, &c.Pk)
if err != nil {
return nil, err
}
columns = append(columns, c)
}
if err != nil {
return nil, err
}
return columns, nil
}
type ForeignKey struct {
Id int
Seq int
Table string
From string
To string
}
func (c *Conn) ForeignKeys(table string) ([]ForeignKey, os.Error) {
s, err := c.Prepare(Mprintf("pragma foreign_key_list(%Q)", table))
if err != nil {
return nil, err
}
var fks []ForeignKey = make([]ForeignKey, 0, 20)
var ok bool
for ok, err = s.Next(); ok; ok, err = s.Next() {
fk := ForeignKey{}
err = s.NamedScan("id", &fk.Id, "seq", &fk.Seq, "table", &fk.Table, "from", &fk.From, "to", &fk.To)
if err != nil {
return nil, err
}
fks = append(fks, fk)
}
if err != nil {
return nil, err
}
return fks, nil
}
func Mprintf(format string, arg string) string {
cf := C.CString(format)
defer C.free(unsafe.Pointer(cf))
......
......@@ -198,6 +198,7 @@ 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)
......@@ -216,16 +217,46 @@ func TestTables(t *testing.T) {
if tables[0] != "test" {
t.Errorf("Wrong table name: 'test' <> %s\n", tables[0])
}
}
func TestColumns(t *testing.T) {
db := open(t)
defer db.Close()
createTable(db, t)
columns, err := db.Columns("test")
if err != nil {
t.Fatalf("error looking for columns: %s", err)
t.Fatalf("error listing columns: %s", err)
}
if len(columns) != 4 {
t.Errorf("Expected 4 columns <> %d", len(columns))
t.Fatalf("Expected 4 columns <> %d", len(columns))
}
column := columns[2]
if column.name != "int_num" {
t.Errorf("Wrong column name: 'int_num' <> %s", column.name)
if column.Name != "int_num" {
t.Errorf("Wrong column name: 'int_num' <> %s", column.Name)
}
}
func TestForeignKeys(t *testing.T) {
db := open(t)
defer db.Close()
err := db.Exec("CREATE TABLE parent (id INTEGER PRIMARY KEY);" +
"CREATE TABLE child (id INTEGER PRIMARY KEY, parentId INTEGER, " +
"FOREIGN KEY (parentId) REFERENCES parent(id));")
if err != nil {
t.Fatalf("error creating tables: %s", err)
}
fks, err := db.ForeignKeys("child")
if err != nil {
t.Fatalf("error listing FKs: %s", err)
}
if len(fks) != 1 {
t.Fatalf("Expected 1 FK <> %d", len(fks))
}
fk := fks[0]
if fk.From != "parentId" || fk.Table != "parent" || fk.To != "id" {
t.Errorf("Unexpected FK data: %#v", fk)
}
}
......
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