forked from mattn/go-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make column metadata functionality opt-in
- Loading branch information
Showing
5 changed files
with
61 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// +build sqlite_column_metadata | ||
|
||
package sqlite3 | ||
|
||
/* | ||
#ifndef USE_LIBSQLITE3 | ||
#cgo CFLAGS: -DSQLITE_ENABLE_COLUMN_METADATA | ||
#include <sqlite3-binding.h> | ||
#else | ||
#include <sqlite3.h> | ||
#endif | ||
*/ | ||
import "C" | ||
|
||
// ColumnTableName returns the table that is the origin of a particular result | ||
// column in a SELECT statement. | ||
// | ||
// See https://www.sqlite.org/c3ref/column_database_name.html | ||
func (s *SQLiteStmt) ColumnTableName(n int) string { | ||
return C.GoString(C.sqlite3_column_table_name(s.s, C.int(n))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// +build sqlite_column_metadata | ||
|
||
package sqlite3 | ||
|
||
import "testing" | ||
|
||
func TestColumnTableName(t *testing.T) { | ||
d := SQLiteDriver{} | ||
conn, err := d.Open(":memory:") | ||
if err != nil { | ||
t.Fatal("failed to get database connection:", err) | ||
} | ||
defer conn.Close() | ||
sqlite3conn := conn.(*SQLiteConn) | ||
|
||
_, err = sqlite3conn.Exec(`CREATE TABLE foo (name string)`, nil) | ||
if err != nil { | ||
t.Fatal("Failed to create table:", err) | ||
} | ||
_, err = sqlite3conn.Exec(`CREATE TABLE bar (name string)`, nil) | ||
if err != nil { | ||
t.Fatal("Failed to create table:", err) | ||
} | ||
|
||
stmt, err := sqlite3conn.Prepare(`SELECT * FROM foo JOIN bar ON foo.name = bar.name`) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if exp, got := "foo", stmt.(*SQLiteStmt).ColumnTableName(0); exp != got { | ||
t.Fatalf("Incorrect table name returned expected: %s, got: %s", exp, got) | ||
} | ||
if exp, got := "bar", stmt.(*SQLiteStmt).ColumnTableName(1); exp != got { | ||
t.Fatalf("Incorrect table name returned expected: %s, got: %s", exp, got) | ||
} | ||
if exp, got := "", stmt.(*SQLiteStmt).ColumnTableName(2); exp != got { | ||
t.Fatalf("Incorrect table name returned expected: %s, got: %s", exp, got) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters