Skip to content

Commit 91859f8

Browse files
committed
Merge branch 'mattn-master'
2 parents e627c50 + eb62077 commit 91859f8

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

_example/fuzz/fuzz_openexec.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package sqlite3_fuzz
2+
3+
import (
4+
"bytes"
5+
"database/sql"
6+
"io/ioutil"
7+
8+
_ "github.com/mattn/go-sqlite3"
9+
)
10+
11+
func FuzzOpenExec(data []byte) int {
12+
sep := bytes.IndexByte(data, 0)
13+
if sep <= 0 {
14+
return 0
15+
}
16+
err := ioutil.WriteFile("/tmp/fuzz.db", data[sep+1:], 0644)
17+
if err != nil {
18+
return 0
19+
}
20+
db, err := sql.Open("sqlite3", "/tmp/fuzz.db")
21+
if err != nil {
22+
return 0
23+
}
24+
defer db.Close()
25+
_, err = db.Exec(string(data[:sep-1]))
26+
if err != nil {
27+
return 0
28+
}
29+
return 1
30+
}

sqlite3.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
17251725
//
17261726
// Because default is NORMAL this statement is always executed
17271727
if err := exec(fmt.Sprintf("PRAGMA synchronous = %s;", synchronousMode)); err != nil {
1728-
C.sqlite3_close_v2(db)
1728+
conn.Close()
17291729
return nil, err
17301730
}
17311731

sqlite3_opt_column_metadata.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// +build sqlite_column_metadata
2+
3+
package sqlite3
4+
5+
/*
6+
#ifndef USE_LIBSQLITE3
7+
#cgo CFLAGS: -DSQLITE_ENABLE_COLUMN_METADATA
8+
#include <sqlite3-binding.h>
9+
#else
10+
#include <sqlite3.h>
11+
#endif
12+
*/
13+
import "C"
14+
15+
// ColumnTableName returns the table that is the origin of a particular result
16+
// column in a SELECT statement.
17+
//
18+
// See https://www.sqlite.org/c3ref/column_database_name.html
19+
func (s *SQLiteStmt) ColumnTableName(n int) string {
20+
return C.GoString(C.sqlite3_column_table_name(s.s, C.int(n)))
21+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// +build sqlite_column_metadata
2+
3+
package sqlite3
4+
5+
import "testing"
6+
7+
func TestColumnTableName(t *testing.T) {
8+
d := SQLiteDriver{}
9+
conn, err := d.Open(":memory:")
10+
if err != nil {
11+
t.Fatal("failed to get database connection:", err)
12+
}
13+
defer conn.Close()
14+
sqlite3conn := conn.(*SQLiteConn)
15+
16+
_, err = sqlite3conn.Exec(`CREATE TABLE foo (name string)`, nil)
17+
if err != nil {
18+
t.Fatal("Failed to create table:", err)
19+
}
20+
_, err = sqlite3conn.Exec(`CREATE TABLE bar (name string)`, nil)
21+
if err != nil {
22+
t.Fatal("Failed to create table:", err)
23+
}
24+
25+
stmt, err := sqlite3conn.Prepare(`SELECT * FROM foo JOIN bar ON foo.name = bar.name`)
26+
if err != nil {
27+
t.Fatal(err)
28+
}
29+
30+
if exp, got := "foo", stmt.(*SQLiteStmt).ColumnTableName(0); exp != got {
31+
t.Fatalf("Incorrect table name returned expected: %s, got: %s", exp, got)
32+
}
33+
if exp, got := "bar", stmt.(*SQLiteStmt).ColumnTableName(1); exp != got {
34+
t.Fatalf("Incorrect table name returned expected: %s, got: %s", exp, got)
35+
}
36+
if exp, got := "", stmt.(*SQLiteStmt).ColumnTableName(2); exp != got {
37+
t.Fatalf("Incorrect table name returned expected: %s, got: %s", exp, got)
38+
}
39+
}

0 commit comments

Comments
 (0)