@@ -1921,26 +1921,90 @@ func (s *SQLiteStmt) NumInput() int {
1921
1921
1922
1922
var placeHolder = []byte {0 }
1923
1923
1924
+ func hasNamedArgs (args []driver.NamedValue ) bool {
1925
+ for _ , v := range args {
1926
+ if v .Name != "" {
1927
+ return true
1928
+ }
1929
+ }
1930
+ return false
1931
+ }
1932
+
1924
1933
func (s * SQLiteStmt ) bind (args []driver.NamedValue ) error {
1925
1934
rv := C .sqlite3_reset (s .s )
1926
1935
if rv != C .SQLITE_ROW && rv != C .SQLITE_OK && rv != C .SQLITE_DONE {
1927
1936
return s .c .lastError ()
1928
1937
}
1929
1938
1939
+ if hasNamedArgs (args ) {
1940
+ return s .bindIndices (args )
1941
+ }
1942
+
1943
+ for _ , arg := range args {
1944
+ n := C .int (arg .Ordinal )
1945
+ switch v := arg .Value .(type ) {
1946
+ case nil :
1947
+ rv = C .sqlite3_bind_null (s .s , n )
1948
+ case string :
1949
+ p := stringData (v )
1950
+ rv = C ._sqlite3_bind_text (s .s , n , (* C .char )(unsafe .Pointer (p )), C .int (len (v )))
1951
+ case int64 :
1952
+ rv = C .sqlite3_bind_int64 (s .s , n , C .sqlite3_int64 (v ))
1953
+ case bool :
1954
+ val := 0
1955
+ if v {
1956
+ val = 1
1957
+ }
1958
+ rv = C .sqlite3_bind_int (s .s , n , C .int (val ))
1959
+ case float64 :
1960
+ rv = C .sqlite3_bind_double (s .s , n , C .double (v ))
1961
+ case []byte :
1962
+ if v == nil {
1963
+ rv = C .sqlite3_bind_null (s .s , n )
1964
+ } else {
1965
+ ln := len (v )
1966
+ if ln == 0 {
1967
+ v = placeHolder
1968
+ }
1969
+ rv = C ._sqlite3_bind_blob (s .s , n , unsafe .Pointer (& v [0 ]), C .int (ln ))
1970
+ }
1971
+ case time.Time :
1972
+ ts := v .Format (SQLiteTimestampFormats [0 ])
1973
+ p := stringData (ts )
1974
+ rv = C ._sqlite3_bind_text (s .s , n , (* C .char )(unsafe .Pointer (p )), C .int (len (ts )))
1975
+ }
1976
+ if rv != C .SQLITE_OK {
1977
+ return s .c .lastError ()
1978
+ }
1979
+ }
1980
+ return nil
1981
+ }
1982
+
1983
+ func (s * SQLiteStmt ) bindIndices (args []driver.NamedValue ) error {
1984
+ // Find the longest named parameter name.
1985
+ n := 0
1986
+ for _ , v := range args {
1987
+ if m := len (v .Name ); m > n {
1988
+ n = m
1989
+ }
1990
+ }
1991
+ buf := make ([]byte , 0 , n + 2 ) // +2 for placeholder and null terminator
1992
+
1930
1993
bindIndices := make ([][3 ]int , len (args ))
1931
- prefixes := []string {":" , "@" , "$" }
1932
1994
for i , v := range args {
1933
1995
bindIndices [i ][0 ] = args [i ].Ordinal
1934
1996
if v .Name != "" {
1935
- for j := range prefixes {
1936
- cname := C .CString (prefixes [j ] + v .Name )
1937
- bindIndices [i ][j ] = int (C .sqlite3_bind_parameter_index (s .s , cname ))
1938
- C .free (unsafe .Pointer (cname ))
1997
+ for j , c := range []byte {':' , '@' , '$' } {
1998
+ buf = append (buf [:0 ], c )
1999
+ buf = append (buf , v .Name ... )
2000
+ buf = append (buf , 0 )
2001
+ bindIndices [i ][j ] = int (C .sqlite3_bind_parameter_index (s .s , (* C .char )(unsafe .Pointer (& buf [0 ]))))
1939
2002
}
1940
2003
args [i ].Ordinal = bindIndices [i ][0 ]
1941
2004
}
1942
2005
}
1943
2006
2007
+ var rv C.int
1944
2008
for i , arg := range args {
1945
2009
for j := range bindIndices [i ] {
1946
2010
if bindIndices [i ][j ] == 0 {
@@ -1951,20 +2015,16 @@ func (s *SQLiteStmt) bind(args []driver.NamedValue) error {
1951
2015
case nil :
1952
2016
rv = C .sqlite3_bind_null (s .s , n )
1953
2017
case string :
1954
- if len (v ) == 0 {
1955
- rv = C ._sqlite3_bind_text (s .s , n , (* C .char )(unsafe .Pointer (& placeHolder [0 ])), C .int (0 ))
1956
- } else {
1957
- b := []byte (v )
1958
- rv = C ._sqlite3_bind_text (s .s , n , (* C .char )(unsafe .Pointer (& b [0 ])), C .int (len (b )))
1959
- }
2018
+ p := stringData (v )
2019
+ rv = C ._sqlite3_bind_text (s .s , n , (* C .char )(unsafe .Pointer (p )), C .int (len (v )))
1960
2020
case int64 :
1961
2021
rv = C .sqlite3_bind_int64 (s .s , n , C .sqlite3_int64 (v ))
1962
2022
case bool :
2023
+ val := 0
1963
2024
if v {
1964
- rv = C .sqlite3_bind_int (s .s , n , 1 )
1965
- } else {
1966
- rv = C .sqlite3_bind_int (s .s , n , 0 )
2025
+ val = 1
1967
2026
}
2027
+ rv = C .sqlite3_bind_int (s .s , n , C .int (val ))
1968
2028
case float64 :
1969
2029
rv = C .sqlite3_bind_double (s .s , n , C .double (v ))
1970
2030
case []byte :
@@ -1978,8 +2038,9 @@ func (s *SQLiteStmt) bind(args []driver.NamedValue) error {
1978
2038
rv = C ._sqlite3_bind_blob (s .s , n , unsafe .Pointer (& v [0 ]), C .int (ln ))
1979
2039
}
1980
2040
case time.Time :
1981
- b := []byte (v .Format (SQLiteTimestampFormats [0 ]))
1982
- rv = C ._sqlite3_bind_text (s .s , n , (* C .char )(unsafe .Pointer (& b [0 ])), C .int (len (b )))
2041
+ ts := v .Format (SQLiteTimestampFormats [0 ])
2042
+ p := stringData (ts )
2043
+ rv = C ._sqlite3_bind_text (s .s , n , (* C .char )(unsafe .Pointer (p )), C .int (len (ts )))
1983
2044
}
1984
2045
if rv != C .SQLITE_OK {
1985
2046
return s .c .lastError ()
0 commit comments