Commit 99c43b96 authored by gwenn's avatar gwenn

Add binding to sqlite3_table_column_metadata.

parent 928f2264
...@@ -12,6 +12,23 @@ package sqlite ...@@ -12,6 +12,23 @@ package sqlite
static char *my_mprintf(char *zFormat, char *arg) { static char *my_mprintf(char *zFormat, char *arg) {
return sqlite3_mprintf(zFormat, arg); return sqlite3_mprintf(zFormat, arg);
} }
// just to get ride of warning
static int my_table_column_metadata(
sqlite3 *db,
const char *zDbName,
const char *zTableName,
const char *zColumnName,
char **pzDataType,
char **pzCollSeq,
int *pNotNull,
int *pPrimaryKey,
int *pAutoinc
) {
return sqlite3_table_column_metadata(db, zDbName, zTableName, zColumnName,
(char const **)pzDataType, (char const **)pzCollSeq, pNotNull, pPrimaryKey, pAutoinc);
}
*/ */
import "C" import "C"
...@@ -67,6 +84,8 @@ type Column struct { ...@@ -67,6 +84,8 @@ type Column struct {
NotNull bool NotNull bool
DfltValue string // FIXME type ? DfltValue string // FIXME type ?
Pk bool Pk bool
Autoinc bool
CollSeq string
} }
// Executes pragma 'table_info' // Executes pragma 'table_info'
...@@ -93,6 +112,27 @@ func (c *Conn) Columns(table string) ([]Column, error) { ...@@ -93,6 +112,27 @@ func (c *Conn) Columns(table string) ([]Column, error) {
return columns, nil return columns, nil
} }
func (c *Conn) Column(dbName, tableName, columnName string) (*Column, error) {
var zDbName *C.char
if len(dbName) > 0 {
zDbName = C.CString(dbName)
defer C.free(unsafe.Pointer(zDbName))
}
zTableName := C.CString(tableName)
defer C.free(unsafe.Pointer(zTableName))
zColumnName := C.CString(columnName)
defer C.free(unsafe.Pointer(zColumnName))
var zDataType, zCollSeq *C.char
var notNull, primaryKey, autoinc C.int
rv := C.my_table_column_metadata(c.db, zDbName, zTableName, zColumnName, &zDataType, &zCollSeq,
&notNull, &primaryKey, &autoinc)
if rv != C.SQLITE_OK {
return nil, c.error(rv)
}
return &Column{-1, columnName, C.GoString(zDataType), notNull == 1, "", primaryKey == 1,
autoinc == 1, C.GoString(zCollSeq)}, nil
}
// See Conn.ForeignKeys // See Conn.ForeignKeys
type ForeignKey struct { type ForeignKey struct {
Table string Table string
......
...@@ -61,6 +61,24 @@ func TestColumns(t *testing.T) { ...@@ -61,6 +61,24 @@ func TestColumns(t *testing.T) {
} }
} }
func TestColumn(t *testing.T) {
db := open(t)
defer db.Close()
createTable(db, t)
column, err := db.Column("", "test", "id")
checkNoError(t, err, "error getting column metadata: %s")
if column.Name != "id" {
t.Errorf("Wrong column name: 'id' <> %s", column.Name)
}
if !column.Pk {
t.Errorf("Expecting primary key flag to be true")
}
if !column.Autoinc {
t.Errorf("Expecting autoinc flag to be true")
}
}
func TestForeignKeys(t *testing.T) { func TestForeignKeys(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() 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