Commit 96365052 authored by gwenn's avatar gwenn

Fix Tables/Views/Indexes methods because there is only TEMP database.

parent e3326449
......@@ -59,7 +59,7 @@ func ExampleConn_Exec() {
err = db.Exec("CREATE TABLE test1 (content TEXT); CREATE TABLE test2 (content TEXT); INSERT INTO test1 VALUES ('DATA')")
check(err)
tables, err := db.Tables("", false)
tables, err := db.Tables("")
check(err)
fmt.Printf("%d tables\n", len(tables))
// Output: 2 tables
......
......@@ -40,16 +40,16 @@ func (c *Conn) Databases() (map[string]string, error) {
}
// Tables returns tables (no view) from 'sqlite_master'/'sqlite_temp_master' and filters system tables out.
func (c *Conn) Tables(dbName string, temp bool) ([]string, error) {
// The database name can be empty, "main", "temp" or the name of an attached database.
func (c *Conn) Tables(dbName string) ([]string, error) {
var sql string
if len(dbName) == 0 {
sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY 1"
} else if strings.EqualFold("temp", dbName) {
sql = "SELECT name FROM sqlite_temp_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY 1"
} else {
sql = fmt.Sprintf("SELECT name FROM %s.sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%%' ORDER BY 1", doubleQuote(dbName))
}
if temp {
sql = strings.Replace(sql, "sqlite_master", "sqlite_temp_master", 1)
}
s, err := c.prepare(sql)
if err != nil {
return nil, err
......@@ -68,16 +68,16 @@ func (c *Conn) Tables(dbName string, temp bool) ([]string, error) {
}
// Views returns views from 'sqlite_master'/'sqlite_temp_master'.
func (c *Conn) Views(dbName string, temp bool) ([]string, error) {
// The database name can be empty, "main", "temp" or the name of an attached database.
func (c *Conn) Views(dbName string) ([]string, error) {
var sql string
if len(dbName) == 0 {
sql = "SELECT name FROM sqlite_master WHERE type = 'view' ORDER BY 1"
} else if strings.EqualFold("temp", dbName) {
sql = "SELECT name FROM sqlite_temp_master WHERE type = 'view' ORDER BY 1"
} else {
sql = fmt.Sprintf("SELECT name FROM %s.sqlite_master WHERE type = 'view' ORDER BY 1", doubleQuote(dbName))
}
if temp {
sql = strings.Replace(sql, "sqlite_master", "sqlite_temp_master", 1)
}
s, err := c.prepare(sql)
if err != nil {
return nil, err
......@@ -97,16 +97,16 @@ 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) {
// The database name can be empty, "main", "temp" or the name of an attached database.
func (c *Conn) Indexes(dbName string) (map[string]string, error) {
var sql string
if len(dbName) == 0 {
sql = "SELECT name, tbl_name FROM sqlite_master WHERE type = 'index'"
} else if strings.EqualFold("temp", dbName) {
sql = "SELECT name, tbl_name FROM sqlite_temp_master WHERE type = 'index'"
} else {
sql = fmt.Sprintf("SELECT name, tbl_name FROM %s.sqlite_master WHERE type = 'index'", doubleQuote(dbName))
}
if temp {
sql = strings.Replace(sql, "sqlite_master", "sqlite_temp_master", 1)
}
s, err := c.prepare(sql)
if err != nil {
return nil, err
......
......@@ -35,20 +35,20 @@ func TestTables(t *testing.T) {
db := open(t)
defer checkClose(db, t)
tables, err := db.Tables("", false)
tables, err := db.Tables("")
checkNoError(t, err, "error looking for tables: %s")
assert.Equal(t, 0, len(tables), "table count")
createTable(db, t)
tables, err = db.Tables("main", false)
tables, err = db.Tables("main")
checkNoError(t, err, "error looking for tables: %s")
assert.Equal(t, 1, len(tables), "table count")
assert.Equal(t, "test", tables[0], "table name")
tables, err = db.Tables("", true)
tables, err = db.Tables("temp")
checkNoError(t, err, "error looking for tables: %s")
assert.Equal(t, 0, len(tables), "table count")
tables, err = db.Tables("bim", false)
tables, err = db.Tables("bim")
assert.T(t, err != nil, "error expected")
//println(err.Error())
}
......@@ -57,21 +57,21 @@ func TestViews(t *testing.T) {
db := open(t)
defer checkClose(db, t)
views, err := db.Views("", false)
views, err := db.Views("")
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 view: %s")
views, err = db.Views("main", false)
views, err = db.Views("main")
checkNoError(t, err, "error looking for views: %s")
assert.Equal(t, 1, len(views), "table count")
assert.Equal(t, "myview", views[0], "table name")
views, err = db.Views("", true)
views, err = db.Views("temp")
checkNoError(t, err, "error looking for views: %s")
assert.Equal(t, 0, len(views), "table count")
_, err = db.Views("bim", false)
_, err = db.Views("bim")
assert.T(t, err != nil)
}
......@@ -81,20 +81,20 @@ func TestIndexes(t *testing.T) {
createTable(db, t)
checkNoError(t, db.Exec("CREATE INDEX idx ON test(a_string)"), "%s")
indexes, err := db.Indexes("", false)
indexes, err := db.Indexes("")
checkNoError(t, err, "error looking for indexes: %s")
assert.Equal(t, 1, len(indexes), "index count")
tbl, ok := indexes["idx"]
assert.T(t, ok, "no index")
assert.Equalf(t, "test", tbl, "got: %s; want: %s", tbl, "test")
indexes, err = db.Indexes("main", false)
indexes, err = db.Indexes("main")
checkNoError(t, err, "error looking for indexes: %s")
_, err = db.Indexes("", true)
_, err = db.Indexes("temp")
checkNoError(t, err, "error looking for indexes: %s")
_, err = db.Indexes("bim", false)
_, err = db.Indexes("bim")
assert.T(t, err != nil)
}
......
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