nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: mattn/go-sqlite3
Pull request #165 (2015-01-02 06:33:25)

Handle 13 digit datetime values
Adds support for `datetime` fields that contain 13 digit integers (Unix timestamp with millisecond precision).


coveralls: 
[![Coverage Status](https://coveralls.io/builds/1682040/badge)](https://coveralls.io/builds/1682040)

Coverage decreased (-0.68%) when pulling **b59c1e23edc94999cd9d124a19e339a390c0e211 on porjo:porjo** into **e068bcd7684d6746d750842775265d301892ce13 on mattn:master**.


Patch:
diff --git a/sqlite3.go b/sqlite3.go
index bc532cd..04f0edb 100644
--- a/sqlite3.go
+++ b/sqlite3.go
@@ -64,6 +64,7 @@ import (
 	"fmt"
 	"io"
 	"runtime"
+	"strconv"
 	"strings"
 	"time"
 	"unsafe"
@@ -504,7 +505,18 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
 			val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
 			switch rc.decltype[i] {
 			case "timestamp", "datetime", "date":
-				dest[i] = time.Unix(val, 0).Local()
+				unixTimestamp := strconv.FormatInt(val, 10)
+				if len(unixTimestamp) == 13 {
+					duration, err := time.ParseDuration(unixTimestamp + "ms")
+					if err != nil {
+						return fmt.Errorf("error parsing %s value %d, %s", rc.decltype[i], val, err)
+					}
+					epoch := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
+					dest[i] = epoch.Add(duration)
+				} else {
+					dest[i] = time.Unix(val, 0).Local()
+				}
+
 			case "boolean":
 				dest[i] = val > 0
 			default:


Test patch:
diff --git a/sqlite3_test.go b/sqlite3_test.go
index 9cc5a0e..3e50258 100644
--- a/sqlite3_test.go
+++ b/sqlite3_test.go
@@ -309,6 +309,7 @@ func TestTimestamp(t *testing.T) {
 		{"0000-00-00 00:00:00", time.Time{}},
 		{timestamp1, timestamp1},
 		{timestamp1.Unix(), timestamp1},
+		{timestamp1.UnixNano() / int64(time.Millisecond), timestamp1},
 		{timestamp1.In(time.FixedZone("TEST", -7*3600)), timestamp1},
 		{timestamp1.Format("2006-01-02 15:04:05.000"), timestamp1},
 		{timestamp1.Format("2006-01-02T15:04:05.000"), timestamp1},
Repository: mattn/go-sqlite3
Pull request #282 (2016-03-06 20:30:56)

Add SQLiteRows.DeclTypes() method
I would like to use this method to solve https://github.com/otoolep/rqlite/issues/52.


Patch:
diff --git a/sqlite3.go b/sqlite3.go
index db34b23..afeb99e 100644
--- a/sqlite3.go
+++ b/sqlite3.go
@@ -893,6 +893,17 @@ func (rc *SQLiteRows) Columns() []string {
 	return rc.cols
 }
 
+// Return column types.
+func (rc *SQLiteRows) DeclTypes() []string {
+	if rc.decltype == nil {
+		rc.decltype = make([]string, rc.nc)
+		for i := 0; i < rc.nc; i++ {
+			rc.decltype[i] = strings.ToLower(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))))
+		}
+	}
+	return rc.decltype
+}
+
 // Move cursor to next.
 func (rc *SQLiteRows) Next(dest []driver.Value) error {
 	rv := C.sqlite3_step(rc.s.s)
@@ -907,12 +918,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
 		return nil
 	}
 
-	if rc.decltype == nil {
-		rc.decltype = make([]string, rc.nc)
-		for i := 0; i < rc.nc; i++ {
-			rc.decltype[i] = strings.ToLower(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))))
-		}
-	}
+	rc.DeclTypes()
 
 	for i := range dest {
 		switch C.sqlite3_column_type(rc.s.s, C.int(i)) {


Test patch:
diff --git a/sqlite3_test.go b/sqlite3_test.go
index 9efd313..c7996c6 100644
--- a/sqlite3_test.go
+++ b/sqlite3_test.go
@@ -1279,6 +1279,41 @@ func TestAggregatorRegistration(t *testing.T) {
 	}
 }
 
+func TestDeclTypes(t *testing.T) {
+
+	d := SQLiteDriver{}
+
+	conn, err := d.Open(":memory:")
+	if err != nil {
+		t.Fatal("Failed to begin transaction:", err)
+	}
+	defer conn.Close()
+
+	sqlite3conn := conn.(*SQLiteConn)
+
+	_, err = sqlite3conn.Exec("create table foo (id integer not null primary key, name text)", nil)
+	if err != nil {
+		t.Fatal("Failed to create table:", err)
+	}
+
+	_, err = sqlite3conn.Exec("insert into foo(name) values(\"bar\")", nil)
+	if err != nil {
+		t.Fatal("Failed to insert:", err)
+	}
+
+	rs, err := sqlite3conn.Query("select * from foo", nil)
+	if err != nil {
+		t.Fatal("Failed to select:", err)
+	}
+	defer rs.Close()
+
+	declTypes := rs.(*SQLiteRows).DeclTypes()
+
+	if !reflect.DeepEqual(declTypes, []string{"integer", "text"}) {
+		t.Fatal("Unexpected declTypes:", declTypes)
+	}
+}
+
 var customFunctionOnce sync.Once
 
 func BenchmarkCustomFunctions(b *testing.B) {
Repository: mattn/go-sqlite3
Pull request #302 (2016-04-22 15:59:15)

Build with libsqlite3 on Ubuntu 14.04 LTS
* Sets default value of SQLITE_DETERMINISTIC to 0 if undefined.
* Skip FTS4 test if unicode61 tokenizer is unavailable
* Test go-sqlite3 against libsqlite3
* Test go-sqlite3 against golang versions 1.5 / 1.6


Patch:
diff --git a/.travis.yml b/.travis.yml
index 03d626f..e85e473 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,8 +1,13 @@
 language: go
+sudo: required
+dist: trusty
 go:
+  - 1.5
+  - 1.6
   - tip
 before_install:
   - go get github.com/mattn/goveralls
   - go get golang.org/x/tools/cmd/cover
 script:
-    - $HOME/gopath/bin/goveralls -repotoken 3qJVUE0iQwqnCbmNcDsjYu1nh4J4KIFXx
+  - $HOME/gopath/bin/goveralls -repotoken 3qJVUE0iQwqnCbmNcDsjYu1nh4J4KIFXx
+  - go test -v . -tags "libsqlite3"
diff --git a/README.md b/README.md
index 1092c9b..c2e0d5a 100644
--- a/README.md
+++ b/README.md
@@ -38,6 +38,7 @@ FAQ
 * Want to build go-sqlite3 with libsqlite3 on OS X.
 
     Install sqlite3 from homebrew: `brew install sqlite3`
+
     Use `go build --tags "libsqlite3 darwin"`
 
 * Want to build go-sqlite3 with icu extension.
diff --git a/sqlite3.go b/sqlite3.go
index 4d81917..852c299 100644
--- a/sqlite3.go
+++ b/sqlite3.go
@@ -29,6 +29,10 @@ package sqlite3
 # define SQLITE_OPEN_FULLMUTEX 0
 #endif
 
+#ifndef SQLITE_DETERMINISTIC
+# define SQLITE_DETERMINISTIC 0
+#endif
+
 static int
 _sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs) {
 #ifdef SQLITE_OPEN_URI


Test patch:
diff --git a/sqlite3_fts3_test.go b/sqlite3_fts3_test.go
index 803afbd..e06fc5d 100644
--- a/sqlite3_fts3_test.go
+++ b/sqlite3_fts3_test.go
@@ -93,7 +93,10 @@ func TestFTS4(t *testing.T) {
 
 	_, err = db.Exec("DROP TABLE foo")
 	_, err = db.Exec("CREATE VIRTUAL TABLE foo USING fts4(tokenize=unicode61, id INTEGER PRIMARY KEY, value TEXT)")
-	if err != nil {
+	switch {
+	case err != nil && err.Error() == "unknown tokenizer: unicode61":
+		t.Skip("FTS4 not supported")
+	case err != nil:
 		t.Fatal("Failed to create table:", err)
 	}
Repository: mattn/go-sqlite3
Pull request #436 (2017-07-09 14:39:58)

Add connection option for recursive triggers
Similar to foreign keys, the recursive triggers PRAGMA affects the
interpretation of all statements on a connection.


Patch:
diff --git a/sqlite3.go b/sqlite3.go
index 2b7b8df..2ebf7e7 100644
--- a/sqlite3.go
+++ b/sqlite3.go
@@ -599,6 +599,8 @@ func errorString(err Error) string {
 //     "deferred", "exclusive".
 //   _foreign_keys=X
 //     Enable or disable enforcement of foreign keys.  X can be 1 or 0.
+//   _recursive_triggers=X
+//     Enable or disable recursive triggers.  X can be 1 or 0.
 func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
 	if C.sqlite3_threadsafe() == 0 {
 		return nil, errors.New("sqlite library was not compiled for thread-safe operation")
@@ -608,6 +610,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
 	txlock := "BEGIN"
 	busyTimeout := 5000
 	foreignKeys := -1
+	recursiveTriggers := -1
 	pos := strings.IndexRune(dsn, '?')
 	if pos >= 1 {
 		params, err := url.ParseQuery(dsn[pos+1:])
@@ -662,6 +665,18 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
 			}
 		}
 
+		// _recursive_triggers
+		if val := params.Get("_recursive_triggers"); val != "" {
+			switch val {
+			case "1":
+				recursiveTriggers = 1
+			case "0":
+				recursiveTriggers = 0
+			default:
+				return nil, fmt.Errorf("Invalid _recursive_triggers: %v", val)
+			}
+		}
+
 		if !strings.HasPrefix(dsn, "file:") {
 			dsn = dsn[:pos]
 		}
@@ -708,6 +723,17 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
 			return nil, err
 		}
 	}
+	if recursiveTriggers == 0 {
+		if err := exec("PRAGMA recursive_triggers = OFF;"); err != nil {
+			C.sqlite3_close_v2(db)
+			return nil, err
+		}
+	} else if recursiveTriggers == 1 {
+		if err := exec("PRAGMA recursive_triggers = ON;"); err != nil {
+			C.sqlite3_close_v2(db)
+			return nil, err
+		}
+	}
 
 	conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
 


Test patch:
diff --git a/sqlite3_test.go b/sqlite3_test.go
index f11c349..a00e622 100644
--- a/sqlite3_test.go
+++ b/sqlite3_test.go
@@ -136,6 +136,35 @@ func TestForeignKeys(t *testing.T) {
 	}
 }
 
+func TestRecursiveTriggers(t *testing.T) {
+	cases := map[string]bool{
+		"?_recursive_triggers=1": true,
+		"?_recursive_triggers=0": false,
+	}
+	for option, want := range cases {
+		fname := TempFilename(t)
+		uri := "file:" + fname + option
+		db, err := sql.Open("sqlite3", uri)
+		if err != nil {
+			os.Remove(fname)
+			t.Errorf("sql.Open(\"sqlite3\", %q): %v", uri, err)
+			continue
+		}
+		var enabled bool
+		err = db.QueryRow("PRAGMA recursive_triggers;").Scan(&enabled)
+		db.Close()
+		os.Remove(fname)
+		if err != nil {
+			t.Errorf("query recursive_triggers for %s: %v", uri, err)
+			continue
+		}
+		if enabled != want {
+			t.Errorf("\"PRAGMA recursive_triggers;\" for %q = %t; want %t", uri, enabled, want)
+			continue
+		}
+	}
+}
+
 func TestClose(t *testing.T) {
 	tempFilename := TempFilename(t)
 	defer os.Remove(tempFilename)
Repository: mattn/go-sqlite3
Pull request #489 (2017-11-21 12:52:29)

Fix race in ExecContext
This demonstrates and fixes issue #488.


Patch:
diff --git a/sqlite3.go b/sqlite3.go
index a931735..c9edd40 100644
--- a/sqlite3.go
+++ b/sqlite3.go
@@ -1171,9 +1171,13 @@ func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result
 	defer close(done)
 	go func(db *C.sqlite3) {
 		select {
-		case <-ctx.Done():
-			C.sqlite3_interrupt(db)
 		case <-done:
+		case <-ctx.Done():
+			select {
+			case <-done:
+			default:
+				C.sqlite3_interrupt(db)
+			}
 		}
 	}(s.c.db)
 


Test patch:
diff --git a/sqlite3_go18_test.go b/sqlite3_go18_test.go
index 2662fcf..44fc4df 100644
--- a/sqlite3_go18_test.go
+++ b/sqlite3_go18_test.go
@@ -134,3 +134,24 @@ func TestShortTimeout(t *testing.T) {
 		t.Fatal(ctx.Err())
 	}
 }
+
+func TestExecCancel(t *testing.T) {
+	db, err := sql.Open("sqlite3", ":memory:")
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer db.Close()
+
+	if _, err = db.Exec("create table foo (id integer primary key)"); err != nil {
+		t.Fatal(err)
+	}
+
+	for n := 0; n < 100; n++ {
+		ctx, cancel := context.WithCancel(context.Background())
+		_, err = db.ExecContext(ctx, "insert into foo (id) values (?)", n)
+		cancel()
+		if err != nil {
+			t.Fatal(err)
+		}
+	}
+}
Repository: mattn/go-sqlite3
Pull request #583 (2018-05-31 00:49:08)

Add nil check in bind and a test
This is the fix for #542. 

There are extensive tests in the existing `TestNilAndEmptyBytes`, but we cannot test the change in there because of how `sqlite3_column_blob()` works: _"The return value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer."_ -> this means that both `NULL` and `zero-length blob` map to a `nil` byte slice. So... I ended up adding a separate test `TestInsertNilByteSlice` to check that storing a nil byte slice is now storing a `NULL`.


Patch:
diff --git a/sqlite3.go b/sqlite3.go
index 75a5ee8..f9bbf12 100644
--- a/sqlite3.go
+++ b/sqlite3.go
@@ -1511,11 +1511,15 @@ func (s *SQLiteStmt) bind(args []namedValue) error {
 		case float64:
 			rv = C.sqlite3_bind_double(s.s, n, C.double(v))
 		case []byte:
-			ln := len(v)
-			if ln == 0 {
-				v = placeHolder
+			if v == nil {
+				rv = C.sqlite3_bind_null(s.s, n)
+			} else {
+				ln := len(v)
+				if ln == 0 {
+					v = placeHolder
+				}
+				rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(&v[0]), C.int(ln))
 			}
-			rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(&v[0]), C.int(ln))
 		case time.Time:
 			b := []byte(v.Format(SQLiteTimestampFormats[0]))
 			rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))


Test patch:
diff --git a/sqlite3_test.go b/sqlite3_test.go
index 782f57c..eadcf4a 100644
--- a/sqlite3_test.go
+++ b/sqlite3_test.go
@@ -1580,6 +1580,25 @@ func TestNilAndEmptyBytes(t *testing.T) {
 	}
 }
 
+func TestInsertNilByteSlice(t *testing.T) {
+	db, err := sql.Open("sqlite3", ":memory:")
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer db.Close()
+	if _, err := db.Exec("create table blob_not_null (b blob not null)"); err != nil {
+		t.Fatal(err)
+	}
+	var nilSlice []byte
+	if _, err := db.Exec("insert into blob_not_null (b) values (?)", nilSlice); err == nil {
+		t.Fatal("didn't expect INSERT to 'not null' column with a nil []byte slice to work")
+	}
+	zeroLenSlice := []byte{}
+	if _, err := db.Exec("insert into blob_not_null (b) values (?)", zeroLenSlice); err != nil {
+		t.Fatal("failed to insert zero-length slice")
+	}
+}
+
 var customFunctionOnce sync.Once
 
 func BenchmarkCustomFunctions(b *testing.B) {
Repository: mattn/go-sqlite3
Pull request #692 (2019-02-10 15:49:22)

Fix 688


Patch:
diff --git a/sqlite3.go b/sqlite3.go
index f731d20..1cf9451 100644
--- a/sqlite3.go
+++ b/sqlite3.go
@@ -2062,9 +2062,8 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
 				}
 				dest[i] = t
 			default:
-				dest[i] = []byte(s)
+				dest[i] = s
 			}
-
 		}
 	}
 	return nil


Test patch:
diff --git a/sqlite3_test.go b/sqlite3_test.go
index 16bbb10..3ef8533 100644
--- a/sqlite3_test.go
+++ b/sqlite3_test.go
@@ -1670,6 +1670,26 @@ func TestAuthorizer(t *testing.T) {
 	}
 }
 
+func TestNonColumnString(t *testing.T) {
+	db, err := sql.Open("sqlite3", ":memory:")
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer db.Close()
+
+	var x interface{}
+	if err := db.QueryRow("SELECT 'hello'").Scan(&x); err != nil {
+		t.Fatal(err)
+	}
+	s, ok := x.(string)
+	if !ok {
+		t.Fatalf("non-column string must return string but got %T", x)
+	}
+	if s != "hello" {
+		t.Fatalf("non-column string must return %q but got %q", "hello", s)
+	}
+}
+
 func TestNilAndEmptyBytes(t *testing.T) {
 	db, err := sql.Open("sqlite3", ":memory:")
 	if err != nil {
Repository: mattn/go-sqlite3
Pull request #737 (2019-08-22 10:16:46)

Fix _auth_* parameter check
Fixes: #724

Updated Tests

GJRTimmer: LGTM
coveralls: 
[![Coverage Status](https://coveralls.io/builds/25283586/badge)](https://coveralls.io/builds/25283586)

Coverage remained the same at 50.381% when pulling **db47e596f08fa8738c91f01c374db621ad353dc0 on fix-724** into **2ea5857c0eb3179b078ce5fb881ace11eb28f285 on master**.


Patch:
diff --git a/sqlite3.go b/sqlite3.go
index 5ef56ad..4000173 100644
--- a/sqlite3.go
+++ b/sqlite3.go
@@ -1522,10 +1522,10 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
 		// Before going any further, we need to check that the user
 		// has provided an username and password within the DSN.
 		// We are not allowed to continue.
-		if len(authUser) < 0 {
+		if len(authUser) == 0 {
 			return nil, fmt.Errorf("Missing '_auth_user' while user authentication was requested with '_auth'")
 		}
-		if len(authPass) < 0 {
+		if len(authPass) == 0 {
 			return nil, fmt.Errorf("Missing '_auth_pass' while user authentication was requested with '_auth'")
 		}
 


Test patch:
diff --git a/sqlite3_opt_userauth_test.go b/sqlite3_opt_userauth_test.go
index df65215..543f48e 100644
--- a/sqlite3_opt_userauth_test.go
+++ b/sqlite3_opt_userauth_test.go
@@ -60,7 +60,14 @@ func init() {
 			file = TempFilename(t)
 		}
 
-		db, err = sql.Open("sqlite3_with_conn", "file:"+file+fmt.Sprintf("?_auth&_auth_user=%s&_auth_pass=%s", username, password))
+		params := "?_auth"
+		if len(username) > 0 {
+			params = fmt.Sprintf("%s&_auth_user=%s", params, username)
+		}
+		if len(password) > 0 {
+			params = fmt.Sprintf("%s&_auth_pass=%s", params, password)
+		}
+		db, err = sql.Open("sqlite3_with_conn", "file:"+file+params)
 		if err != nil {
 			defer os.Remove(file)
 			return file, nil, nil, err
@@ -164,6 +171,23 @@ func TestUserAuthCreateDatabase(t *testing.T) {
 	}
 }
 
+func TestUserAuthCreateDatabaseWithoutArgs(t *testing.T) {
+	_, db, c, err := connect(t, "", "", "")
+	if err == nil && c == nil && db == nil {
+		t.Fatal("Should have failed due to missing _auth_* parameters")
+	}
+
+	_, db, c, err = connect(t, "", "", "admin")
+	if err == nil && c == nil && db == nil {
+		t.Fatal("Should have failed due to missing _auth_user parameter")
+	}
+
+	_, db, c, err = connect(t, "", "admin", "")
+	if err == nil && c == nil && db == nil {
+		t.Fatal("Should have failed due to missing _auth_pass parameter")
+	}
+}
+
 func TestUserAuthLogin(t *testing.T) {
 	f1, err := create(t, "admin", "admin")
 	if err != nil {