Skip to content

Commit 95e88ca

Browse files
authored
Export sqlite3_column_table_name (#900)
1 parent 323de98 commit 95e88ca

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

sqlite3.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package sqlite3
1919
#cgo CFLAGS: -DSQLITE_OMIT_DEPRECATED
2020
#cgo CFLAGS: -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1
2121
#cgo CFLAGS: -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT
22+
#cgo CFLAGS: -DSQLITE_ENABLE_COLUMN_METADATA
2223
#cgo CFLAGS: -Wno-deprecated-declarations
2324
#cgo linux,!android CFLAGS: -DHAVE_PREAD64=1 -DHAVE_PWRITE64=1
2425
#ifndef USE_LIBSQLITE3
@@ -2014,6 +2015,14 @@ func (s *SQLiteStmt) Readonly() bool {
20142015
return C.sqlite3_stmt_readonly(s.s) == 1
20152016
}
20162017

2018+
// ColumnTableName returns the table that is the origin of a particular result
2019+
// column in a SELECT statement.
2020+
//
2021+
// See https://www.sqlite.org/c3ref/column_database_name.html
2022+
func (s *SQLiteStmt) ColumnTableName(n int) string {
2023+
return C.GoString(C.sqlite3_column_table_name(s.s, C.int(n)))
2024+
}
2025+
20172026
// Close the rows.
20182027
func (rc *SQLiteRows) Close() error {
20192028
rc.s.mu.Lock()

sqlite3_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,40 @@ func TestDeclTypes(t *testing.T) {
16041604
}
16051605
}
16061606

1607+
func TestColumnTableName(t *testing.T) {
1608+
d := SQLiteDriver{}
1609+
conn, err := d.Open(":memory:")
1610+
if err != nil {
1611+
t.Fatal("failed to get database connection:", err)
1612+
}
1613+
defer conn.Close()
1614+
sqlite3conn := conn.(*SQLiteConn)
1615+
1616+
_, err = sqlite3conn.Exec(`CREATE TABLE foo (name string)`, nil)
1617+
if err != nil {
1618+
t.Fatal("Failed to create table:", err)
1619+
}
1620+
_, err = sqlite3conn.Exec(`CREATE TABLE bar (name string)`, nil)
1621+
if err != nil {
1622+
t.Fatal("Failed to create table:", err)
1623+
}
1624+
1625+
stmt, err := sqlite3conn.Prepare(`SELECT * FROM foo JOIN bar ON foo.name = bar.name`)
1626+
if err != nil {
1627+
t.Fatal(err)
1628+
}
1629+
1630+
if exp, got := "foo", stmt.(*SQLiteStmt).ColumnTableName(0); exp != got {
1631+
t.Fatalf("Incorrect table name returned expected: %s, got: %s", exp, got)
1632+
}
1633+
if exp, got := "bar", stmt.(*SQLiteStmt).ColumnTableName(1); exp != got {
1634+
t.Fatalf("Incorrect table name returned expected: %s, got: %s", exp, got)
1635+
}
1636+
if exp, got := "", stmt.(*SQLiteStmt).ColumnTableName(2); exp != got {
1637+
t.Fatalf("Incorrect table name returned expected: %s, got: %s", exp, got)
1638+
}
1639+
}
1640+
16071641
func TestPinger(t *testing.T) {
16081642
db, err := sql.Open("sqlite3", ":memory:")
16091643
if err != nil {

0 commit comments

Comments
 (0)