Commit b8b93a7a authored by gwenn's avatar gwenn

Test metadata on view and expression.

parent 3de617b3
......@@ -96,6 +96,7 @@ func (c *Conn) Views(dbName string, temp bool) ([]string, error) {
}
// Indexes returns indexes from 'sqlite_master'/'sqlite_temp_master'.
// As the index name is unique by database, (index name, table name) couples are returned.
func (c *Conn) Indexes(dbName string, temp bool) (map[string]string, error) {
var sql string
if len(dbName) == 0 {
......@@ -137,7 +138,7 @@ type Column struct {
CollSeq string
}
// Columns returns a description for each column in the named table.
// Columns returns a description for each column in the named table/view.
// Column.Autoinc and Column.CollSeq are left unspecified.
// (See http://www.sqlite.org/pragma.html#pragma_table_info)
func (c *Conn) Columns(dbName, table string) ([]Column, error) {
......@@ -167,7 +168,7 @@ func (c *Conn) Columns(dbName, table string) ([]Column, error) {
return columns, nil
}
// Column extracts metadata about a column of a table.
// Column extracts metadata about a column of a table (doesn't work with view).
// Column.Cid and Column.DfltValue are left unspecified.
// (See http://sqlite.org/c3ref/table_column_metadata.html)
func (c *Conn) Column(dbName, tableName, columnName string) (*Column, error) {
......@@ -187,7 +188,6 @@ func (c *Conn) Column(dbName, tableName, columnName string) (*Column, error) {
if rv != C.SQLITE_OK {
return nil, c.error(rv, fmt.Sprintf("Conn.Column(db: %q, tbl: %q, col: %q)", dbName, tableName, columnName))
}
// TODO How to avoid copy?
return &Column{-1, columnName, C.GoString(zDataType), notNull == 1, "", int(primaryKey),
autoinc == 1, C.GoString(zCollSeq)}, nil
}
......
......@@ -57,7 +57,7 @@ func TestViews(t *testing.T) {
checkNoError(t, err, "error looking for views: %s")
assert.Equal(t, 0, len(views), "table count")
err = db.FastExec("CREATE VIEW myview AS SELECT 1")
checkNoError(t, err, "error creating for views: %s")
checkNoError(t, err, "error creating view: %s")
views, err = db.Views("", false)
checkNoError(t, err, "error looking for views: %s")
assert.Equal(t, 1, len(views), "table count")
......@@ -171,3 +171,48 @@ func TestColumnMetadata(t *testing.T) {
affinity := s.ColumnTypeAffinity(0)
assert.Equal(t, Textual, affinity, "affinity")
}
func TestColumnMetadataOnView(t *testing.T) {
db := open(t)
defer checkClose(db, t)
createTable(db, t)
err := db.FastExec("CREATE VIEW vtest AS SELECT * FROM test")
checkNoError(t, err, "error creating view: %s")
s, err := db.Prepare("SELECT a_string AS str FROM vtest")
check(err)
defer checkFinalize(s, t)
databaseName := s.ColumnDatabaseName(0)
assert.Equal(t, "main", databaseName, "database name")
tableName := s.ColumnTableName(0)
assert.Equal(t, "test", tableName, "table name")
originName := s.ColumnOriginName(0)
assert.Equal(t, "a_string", originName, "origin name")
declType := s.ColumnDeclaredType(0)
assert.Equal(t, "TEXT", declType, "declared type")
affinity := s.ColumnTypeAffinity(0)
assert.Equal(t, Textual, affinity, "affinity")
}
func TestColumnMetadataOnExpr(t *testing.T) {
db := open(t)
defer checkClose(db, t)
err := db.FastExec("CREATE VIEW vtest AS SELECT date('now') as tic")
checkNoError(t, err, "error creating view: %s")
s, err := db.Prepare("SELECT tic FROM vtest")
check(err)
defer checkFinalize(s, t)
databaseName := s.ColumnDatabaseName(0)
assert.Equal(t, "", databaseName, "database name")
tableName := s.ColumnTableName(0)
assert.Equal(t, "", tableName, "table name")
originName := s.ColumnOriginName(0)
assert.Equal(t, "", originName, "origin name")
declType := s.ColumnDeclaredType(0)
assert.Equal(t, "", declType, "declared type")
affinity := s.ColumnTypeAffinity(0)
assert.Equal(t, None, affinity, "affinity")
}
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