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