Add support for NULL values

This commit is contained in:
Yaacov Akiba Slama
2020-10-12 20:23:17 +03:00
parent 736c530ac7
commit b69b933c94
3 changed files with 40 additions and 0 deletions

View File

@@ -10,3 +10,4 @@ Alexander Menzhinsky <amenzhinsky@gmail.com>
David Skinner <skinner.david@gmail.com> David Skinner <skinner.david@gmail.com>
Jan Mercl <0xjnml@gmail.com> Jan Mercl <0xjnml@gmail.com>
Steffen Butzer <steffen(dot)butzer@outlook.com> Steffen Butzer <steffen(dot)butzer@outlook.com>
Yaacov Akiba Slama <ya@slamail.org>

26
null_test.go Normal file
View File

@@ -0,0 +1,26 @@
package sqlite
import (
"database/sql"
"testing"
)
func TestNullBinding(t *testing.T) {
db, err := sql.Open("sqlite", "file::memory:")
if err != nil {
t.Errorf("cannot open: %v", err)
return
}
_, err = db.Exec(`
CREATE TABLE table1 (field1 varchar NULL);
INSERT INTO table1 (field1) VALUES (?);
`, sql.NullString{})
if err != nil {
t.Errorf("Error binding null: %v", err)
}
err = db.Close()
if err != nil {
t.Errorf("cannot close: %v", err)
return
}
}

View File

@@ -916,6 +916,10 @@ func (c *conn) bind(pstmt uintptr, n int, args []driver.NamedValue) (allocs []ui
if p, err = c.bindText(pstmt, i, x.String()); err != nil { if p, err = c.bindText(pstmt, i, x.String()); err != nil {
return allocs, err return allocs, err
} }
case nil:
if p, err = c.bindNull(pstmt, i); err != nil {
return allocs, err
}
default: default:
return allocs, fmt.Errorf("sqlite: invalid driver.Value type %T", x) return allocs, fmt.Errorf("sqlite: invalid driver.Value type %T", x)
} }
@@ -926,6 +930,15 @@ func (c *conn) bind(pstmt uintptr, n int, args []driver.NamedValue) (allocs []ui
return allocs, nil return allocs, nil
} }
// int sqlite3_bind_null(sqlite3_stmt*, int);
func (c *conn) bindNull(pstmt uintptr, idx1 int) (uintptr, error) {
if rc := sqlite3.Xsqlite3_bind_null(c.tls, pstmt, int32(idx1)); rc != sqlite3.SQLITE_OK {
return 0, c.errstr(rc)
}
return 0, nil
}
// int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*)); // int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
func (c *conn) bindText(pstmt uintptr, idx1 int, value string) (uintptr, error) { func (c *conn) bindText(pstmt uintptr, idx1 int, value string) (uintptr, error) {
p, err := libc.CString(value) p, err := libc.CString(value)