1 // Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
3 // Use of this source code is governed by an MIT-style
4 // license that can be found in the LICENSE file.
9 #cgo CFLAGS: -std=gnu99
10 #cgo CFLAGS: -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE
11 #cgo CFLAGS: -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61
12 #include <sqlite3-binding.h>
20 #ifndef SQLITE_OPEN_READWRITE
21 # define SQLITE_OPEN_READWRITE 0
24 #ifndef SQLITE_OPEN_FULLMUTEX
25 # define SQLITE_OPEN_FULLMUTEX 0
29 _sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs) {
30 #ifdef SQLITE_OPEN_URI
31 return sqlite3_open_v2(filename, ppDb, flags | SQLITE_OPEN_URI, zVfs);
33 return sqlite3_open_v2(filename, ppDb, flags, zVfs);
38 _sqlite3_bind_text(sqlite3_stmt *stmt, int n, char *p, int np) {
39 return sqlite3_bind_text(stmt, n, p, np, SQLITE_TRANSIENT);
43 _sqlite3_bind_blob(sqlite3_stmt *stmt, int n, void *p, int np) {
44 return sqlite3_bind_blob(stmt, n, p, np, SQLITE_TRANSIENT);
51 _sqlite3_exec(sqlite3* db, const char* pcmd, long long* rowid, long long* changes)
53 int rv = sqlite3_exec(db, pcmd, 0, 0, 0);
54 *rowid = (long long) sqlite3_last_insert_rowid(db);
55 *changes = (long long) sqlite3_changes(db);
60 _sqlite3_step(sqlite3_stmt* stmt, long long* rowid, long long* changes)
62 int rv = sqlite3_step(stmt);
63 sqlite3* db = sqlite3_db_handle(stmt);
64 *rowid = (long long) sqlite3_last_insert_rowid(db);
65 *changes = (long long) sqlite3_changes(db);
69 void _sqlite3_result_text(sqlite3_context* ctx, const char* s) {
70 sqlite3_result_text(ctx, s, -1, &free);
73 void _sqlite3_result_blob(sqlite3_context* ctx, const void* b, int l) {
74 sqlite3_result_blob(ctx, b, l, SQLITE_TRANSIENT);
77 void callbackTrampoline(sqlite3_context*, int, sqlite3_value**);
78 void stepTrampoline(sqlite3_context*, int, sqlite3_value**);
79 void doneTrampoline(sqlite3_context*);
97 // Timestamp formats understood by both this module and SQLite.
98 // The first format in the slice will be used when saving time values
99 // into the database. When parsing a string from a timestamp or
100 // datetime column, the formats are tried in order.
101 var SQLiteTimestampFormats = []string{
102 // By default, store timestamps with whatever timezone they come with.
103 // When parsed, they will be returned with the same timezone.
104 "2006-01-02 15:04:05.999999999-07:00",
105 "2006-01-02T15:04:05.999999999-07:00",
106 "2006-01-02 15:04:05.999999999",
107 "2006-01-02T15:04:05.999999999",
108 "2006-01-02 15:04:05",
109 "2006-01-02T15:04:05",
116 sql.Register("sqlite3", &SQLiteDriver{})
119 // Return SQLite library Version information.
120 func Version() (libVersion string, libVersionNumber int, sourceId string) {
121 libVersion = C.GoString(C.sqlite3_libversion())
122 libVersionNumber = int(C.sqlite3_libversion_number())
123 sourceId = C.GoString(C.sqlite3_sourceid())
124 return libVersion, libVersionNumber, sourceId
128 type SQLiteDriver struct {
130 ConnectHook func(*SQLiteConn) error
134 type SQLiteConn struct {
138 funcs []*functionInfo
139 aggregators []*aggInfo
143 type SQLiteTx struct {
148 type SQLiteStmt struct {
159 type SQLiteResult struct {
165 type SQLiteRows struct {
173 type functionInfo struct {
175 argConverters []callbackArgConverter
176 variadicConverter callbackArgConverter
177 retConverter callbackRetConverter
180 func (fi *functionInfo) Call(ctx *C.sqlite3_context, argv []*C.sqlite3_value) {
181 args, err := callbackConvertArgs(argv, fi.argConverters, fi.variadicConverter)
183 callbackError(ctx, err)
187 ret := fi.f.Call(args)
189 if len(ret) == 2 && ret[1].Interface() != nil {
190 callbackError(ctx, ret[1].Interface().(error))
194 err = fi.retConverter(ctx, ret[0])
196 callbackError(ctx, err)
201 type aggInfo struct {
202 constructor reflect.Value
204 // Active aggregator objects for aggregations in flight. The
205 // aggregators are indexed by a counter stored in the aggregation
206 // user data space provided by sqlite.
207 active map[int64]reflect.Value
210 stepArgConverters []callbackArgConverter
211 stepVariadicConverter callbackArgConverter
213 doneRetConverter callbackRetConverter
216 func (ai *aggInfo) agg(ctx *C.sqlite3_context) (int64, reflect.Value, error) {
217 aggIdx := (*int64)(C.sqlite3_aggregate_context(ctx, C.int(8)))
220 ret := ai.constructor.Call(nil)
221 if len(ret) == 2 && ret[1].Interface() != nil {
222 return 0, reflect.Value{}, ret[1].Interface().(error)
225 return 0, reflect.Value{}, errors.New("aggregator constructor returned nil state")
228 ai.active[*aggIdx] = ret[0]
230 return *aggIdx, ai.active[*aggIdx], nil
233 func (ai *aggInfo) Step(ctx *C.sqlite3_context, argv []*C.sqlite3_value) {
234 _, agg, err := ai.agg(ctx)
236 callbackError(ctx, err)
240 args, err := callbackConvertArgs(argv, ai.stepArgConverters, ai.stepVariadicConverter)
242 callbackError(ctx, err)
246 ret := agg.MethodByName("Step").Call(args)
247 if len(ret) == 1 && ret[0].Interface() != nil {
248 callbackError(ctx, ret[0].Interface().(error))
253 func (ai *aggInfo) Done(ctx *C.sqlite3_context) {
254 idx, agg, err := ai.agg(ctx)
256 callbackError(ctx, err)
259 defer func() { delete(ai.active, idx) }()
261 ret := agg.MethodByName("Done").Call(nil)
262 if len(ret) == 2 && ret[1].Interface() != nil {
263 callbackError(ctx, ret[1].Interface().(error))
267 err = ai.doneRetConverter(ctx, ret[0])
269 callbackError(ctx, err)
274 // Commit transaction.
275 func (tx *SQLiteTx) Commit() error {
276 _, err := tx.c.exec("COMMIT")
280 // Rollback transaction.
281 func (tx *SQLiteTx) Rollback() error {
282 _, err := tx.c.exec("ROLLBACK")
286 // RegisterFunc makes a Go function available as a SQLite function.
288 // The Go function can have arguments of the following types: any
289 // numeric type except complex, bool, []byte, string and
290 // interface{}. interface{} arguments are given the direct translation
291 // of the SQLite data type: int64 for INTEGER, float64 for FLOAT,
292 // []byte for BLOB, string for TEXT.
294 // The function can additionally be variadic, as long as the type of
295 // the variadic argument is one of the above.
297 // If pure is true. SQLite will assume that the function's return
298 // value depends only on its inputs, and make more aggressive
299 // optimizations in its queries.
301 // See _example/go_custom_funcs for a detailed example.
302 func (c *SQLiteConn) RegisterFunc(name string, impl interface{}, pure bool) error {
304 fi.f = reflect.ValueOf(impl)
306 if t.Kind() != reflect.Func {
307 return errors.New("Non-function passed to RegisterFunc")
309 if t.NumOut() != 1 && t.NumOut() != 2 {
310 return errors.New("SQLite functions must return 1 or 2 values")
312 if t.NumOut() == 2 && !t.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
313 return errors.New("Second return value of SQLite function must be error")
321 for i := 0; i < numArgs; i++ {
322 conv, err := callbackArg(t.In(i))
326 fi.argConverters = append(fi.argConverters, conv)
330 conv, err := callbackArg(t.In(numArgs).Elem())
334 fi.variadicConverter = conv
335 // Pass -1 to sqlite so that it allows any number of
336 // arguments. The call helper verifies that the minimum number
337 // of arguments is present for variadic functions.
341 conv, err := callbackRet(t.Out(0))
345 fi.retConverter = conv
347 // fi must outlast the database connection, or we'll have dangling pointers.
348 c.funcs = append(c.funcs, &fi)
350 cname := C.CString(name)
351 defer C.free(unsafe.Pointer(cname))
352 opts := C.SQLITE_UTF8
354 opts |= C.SQLITE_DETERMINISTIC
356 rv := C.sqlite3_create_function(c.db, cname, C.int(numArgs), C.int(opts), unsafe.Pointer(&fi), (*[0]byte)(unsafe.Pointer(C.callbackTrampoline)), nil, nil)
357 if rv != C.SQLITE_OK {
363 // RegisterAggregator makes a Go type available as a SQLite aggregation function.
365 // Because aggregation is incremental, it's implemented in Go with a
366 // type that has 2 methods: func Step(values) accumulates one row of
367 // data into the accumulator, and func Done() ret finalizes and
368 // returns the aggregate value. "values" and "ret" may be any type
369 // supported by RegisterFunc.
371 // RegisterAggregator takes as implementation a constructor function
372 // that constructs an instance of the aggregator type each time an
373 // aggregation begins. The constructor must return a pointer to a
374 // type, or an interface that implements Step() and Done().
376 // The constructor function and the Step/Done methods may optionally
377 // return an error in addition to their other return values.
379 // See _example/go_custom_funcs for a detailed example.
380 func (c *SQLiteConn) RegisterAggregator(name string, impl interface{}, pure bool) error {
382 ai.constructor = reflect.ValueOf(impl)
383 t := ai.constructor.Type()
384 if t.Kind() != reflect.Func {
385 return errors.New("non-function passed to RegisterAggregator")
387 if t.NumOut() != 1 && t.NumOut() != 2 {
388 return errors.New("SQLite aggregator constructors must return 1 or 2 values")
390 if t.NumOut() == 2 && !t.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
391 return errors.New("Second return value of SQLite function must be error")
394 return errors.New("SQLite aggregator constructors must not have arguments")
399 case reflect.Ptr, reflect.Interface:
401 return errors.New("SQlite aggregator constructor must return a pointer object")
403 stepFn, found := agg.MethodByName("Step")
405 return errors.New("SQlite aggregator doesn't have a Step() function")
408 if step.NumOut() != 0 && step.NumOut() != 1 {
409 return errors.New("SQlite aggregator Step() function must return 0 or 1 values")
411 if step.NumOut() == 1 && !step.Out(0).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
412 return errors.New("type of SQlite aggregator Step() return value must be error")
415 stepNArgs := step.NumIn()
417 if agg.Kind() == reflect.Ptr {
418 // Skip over the method receiver
422 if step.IsVariadic() {
425 for i := start; i < start+stepNArgs; i++ {
426 conv, err := callbackArg(step.In(i))
430 ai.stepArgConverters = append(ai.stepArgConverters, conv)
432 if step.IsVariadic() {
433 conv, err := callbackArg(t.In(start + stepNArgs).Elem())
437 ai.stepVariadicConverter = conv
438 // Pass -1 to sqlite so that it allows any number of
439 // arguments. The call helper verifies that the minimum number
440 // of arguments is present for variadic functions.
444 doneFn, found := agg.MethodByName("Done")
446 return errors.New("SQlite aggregator doesn't have a Done() function")
449 doneNArgs := done.NumIn()
450 if agg.Kind() == reflect.Ptr {
451 // Skip over the method receiver
455 return errors.New("SQlite aggregator Done() function must have no arguments")
457 if done.NumOut() != 1 && done.NumOut() != 2 {
458 return errors.New("SQLite aggregator Done() function must return 1 or 2 values")
460 if done.NumOut() == 2 && !done.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
461 return errors.New("second return value of SQLite aggregator Done() function must be error")
464 conv, err := callbackRet(done.Out(0))
468 ai.doneRetConverter = conv
469 ai.active = make(map[int64]reflect.Value)
472 // ai must outlast the database connection, or we'll have dangling pointers.
473 c.aggregators = append(c.aggregators, &ai)
475 cname := C.CString(name)
476 defer C.free(unsafe.Pointer(cname))
477 opts := C.SQLITE_UTF8
479 opts |= C.SQLITE_DETERMINISTIC
481 rv := C.sqlite3_create_function(c.db, cname, C.int(stepNArgs), C.int(opts), unsafe.Pointer(&ai), nil, (*[0]byte)(unsafe.Pointer(C.stepTrampoline)), (*[0]byte)(unsafe.Pointer(C.doneTrampoline)))
482 if rv != C.SQLITE_OK {
488 // AutoCommit return which currently auto commit or not.
489 func (c *SQLiteConn) AutoCommit() bool {
490 return int(C.sqlite3_get_autocommit(c.db)) != 0
493 func (c *SQLiteConn) lastError() Error {
495 Code: ErrNo(C.sqlite3_errcode(c.db)),
496 ExtendedCode: ErrNoExtended(C.sqlite3_extended_errcode(c.db)),
497 err: C.GoString(C.sqlite3_errmsg(c.db)),
502 func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
508 s, err := c.Prepare(query)
512 var res driver.Result
513 if s.(*SQLiteStmt).s != nil {
516 return nil, fmt.Errorf("Not enough args to execute query. Expected %d, got %d.", na, len(args))
518 res, err = s.Exec(args[:na])
519 if err != nil && err != driver.ErrSkip {
525 tail := s.(*SQLiteStmt).t
534 // Implements Queryer
535 func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
537 s, err := c.Prepare(query)
541 s.(*SQLiteStmt).cls = true
544 return nil, fmt.Errorf("Not enough args to execute query. Expected %d, got %d.", na, len(args))
546 rows, err := s.Query(args[:na])
547 if err != nil && err != driver.ErrSkip {
552 tail := s.(*SQLiteStmt).t
562 func (c *SQLiteConn) exec(cmd string) (driver.Result, error) {
563 pcmd := C.CString(cmd)
564 defer C.free(unsafe.Pointer(pcmd))
566 var rowid, changes C.longlong
567 rv := C._sqlite3_exec(c.db, pcmd, &rowid, &changes)
568 if rv != C.SQLITE_OK {
569 return nil, c.lastError()
571 return &SQLiteResult{int64(rowid), int64(changes)}, nil
574 // Begin transaction.
575 func (c *SQLiteConn) Begin() (driver.Tx, error) {
576 if _, err := c.exec(c.txlock); err != nil {
579 return &SQLiteTx{c}, nil
582 func errorString(err Error) string {
583 return C.GoString(C.sqlite3_errstr(C.int(err.Code)))
586 // Open database and return a new connection.
587 // You can specify DSN string with URI filename.
589 // file:test.db?cache=shared&mode=memory
592 // go-sqlite3 adds the following query parameters to those used by SQLite:
594 // Specify location of time format. It's possible to specify "auto".
596 // Specify value for sqlite3_busy_timeout.
598 // Specify locking behavior for transactions. XXX can be "immediate",
599 // "deferred", "exclusive".
600 func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
601 if C.sqlite3_threadsafe() == 0 {
602 return nil, errors.New("sqlite library was not compiled for thread-safe operation")
605 var loc *time.Location
608 pos := strings.IndexRune(dsn, '?')
610 params, err := url.ParseQuery(dsn[pos+1:])
616 if val := params.Get("_loc"); val != "" {
620 loc, err = time.LoadLocation(val)
622 return nil, fmt.Errorf("Invalid _loc: %v: %v", val, err)
628 if val := params.Get("_busy_timeout"); val != "" {
629 iv, err := strconv.ParseInt(val, 10, 64)
631 return nil, fmt.Errorf("Invalid _busy_timeout: %v: %v", val, err)
633 busy_timeout = int(iv)
637 if val := params.Get("_txlock"); val != "" {
640 txlock = "BEGIN IMMEDIATE"
642 txlock = "BEGIN EXCLUSIVE"
646 return nil, fmt.Errorf("Invalid _txlock: %v", val)
650 if !strings.HasPrefix(dsn, "file:") {
656 name := C.CString(dsn)
657 defer C.free(unsafe.Pointer(name))
658 rv := C._sqlite3_open_v2(name, &db,
659 C.SQLITE_OPEN_FULLMUTEX|
660 C.SQLITE_OPEN_READWRITE|
661 C.SQLITE_OPEN_CREATE,
664 return nil, Error{Code: ErrNo(rv)}
667 return nil, errors.New("sqlite succeeded without returning a database")
670 rv = C.sqlite3_busy_timeout(db, C.int(busy_timeout))
671 if rv != C.SQLITE_OK {
672 return nil, Error{Code: ErrNo(rv)}
675 conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
677 if len(d.Extensions) > 0 {
678 if err := conn.loadExtensions(d.Extensions); err != nil {
683 if d.ConnectHook != nil {
684 if err := d.ConnectHook(conn); err != nil {
688 runtime.SetFinalizer(conn, (*SQLiteConn).Close)
692 // Close the connection.
693 func (c *SQLiteConn) Close() error {
694 rv := C.sqlite3_close_v2(c.db)
695 if rv != C.SQLITE_OK {
699 runtime.SetFinalizer(c, nil)
703 // Prepare query string. Return a new statement.
704 func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
705 pquery := C.CString(query)
706 defer C.free(unsafe.Pointer(pquery))
707 var s *C.sqlite3_stmt
709 rv := C.sqlite3_prepare_v2(c.db, pquery, -1, &s, &tail)
710 if rv != C.SQLITE_OK {
711 return nil, c.lastError()
714 if tail != nil && *tail != '\000' {
715 t = strings.TrimSpace(C.GoString(tail))
717 nv := int(C.sqlite3_bind_parameter_count(s))
719 for i := 0; i < nv; i++ {
720 pn := C.GoString(C.sqlite3_bind_parameter_name(s, C.int(i+1)))
721 if len(pn) > 1 && pn[0] == '$' && 48 <= pn[1] && pn[1] <= 57 {
722 nn = append(nn, C.GoString(C.sqlite3_bind_parameter_name(s, C.int(i+1))))
725 ss := &SQLiteStmt{c: c, s: s, nv: nv, nn: nn, t: t}
726 runtime.SetFinalizer(ss, (*SQLiteStmt).Close)
730 // Close the statement.
731 func (s *SQLiteStmt) Close() error {
736 if s.c == nil || s.c.db == nil {
737 return errors.New("sqlite statement with already closed database connection")
739 rv := C.sqlite3_finalize(s.s)
740 if rv != C.SQLITE_OK {
741 return s.c.lastError()
743 runtime.SetFinalizer(s, nil)
747 // Return a number of parameters.
748 func (s *SQLiteStmt) NumInput() int {
752 type bindArg struct {
757 func (s *SQLiteStmt) bind(args []driver.Value) error {
758 rv := C.sqlite3_reset(s.s)
759 if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
760 return s.c.lastError()
765 vargs = make([]bindArg, narg)
767 for i, v := range s.nn {
768 if pi, err := strconv.Atoi(v[1:]); err == nil {
769 vargs[i] = bindArg{pi, args[i]}
773 for i, v := range args {
774 vargs[i] = bindArg{i + 1, v}
778 for _, varg := range vargs {
781 switch v := v.(type) {
783 rv = C.sqlite3_bind_null(s.s, n)
787 rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(0))
790 rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
793 rv = C.sqlite3_bind_int64(s.s, n, C.sqlite3_int64(v))
796 rv = C.sqlite3_bind_int(s.s, n, 1)
798 rv = C.sqlite3_bind_int(s.s, n, 0)
801 rv = C.sqlite3_bind_double(s.s, n, C.double(v))
807 rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(p), C.int(len(v)))
809 b := []byte(v.Format(SQLiteTimestampFormats[0]))
810 rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
812 if rv != C.SQLITE_OK {
813 return s.c.lastError()
819 // Query the statement with arguments. Return records.
820 func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
821 if err := s.bind(args); err != nil {
824 return &SQLiteRows{s, int(C.sqlite3_column_count(s.s)), nil, nil, s.cls}, nil
827 // Return last inserted ID.
828 func (r *SQLiteResult) LastInsertId() (int64, error) {
832 // Return how many rows affected.
833 func (r *SQLiteResult) RowsAffected() (int64, error) {
834 return r.changes, nil
837 // Execute the statement with arguments. Return result object.
838 func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
839 if err := s.bind(args); err != nil {
841 C.sqlite3_clear_bindings(s.s)
844 var rowid, changes C.longlong
845 rv := C._sqlite3_step(s.s, &rowid, &changes)
846 if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
847 err := s.c.lastError()
849 C.sqlite3_clear_bindings(s.s)
852 return &SQLiteResult{int64(rowid), int64(changes)}, nil
856 func (rc *SQLiteRows) Close() error {
863 rv := C.sqlite3_reset(rc.s.s)
864 if rv != C.SQLITE_OK {
865 return rc.s.c.lastError()
870 // Return column names.
871 func (rc *SQLiteRows) Columns() []string {
872 if rc.nc != len(rc.cols) {
873 rc.cols = make([]string, rc.nc)
874 for i := 0; i < rc.nc; i++ {
875 rc.cols[i] = C.GoString(C.sqlite3_column_name(rc.s.s, C.int(i)))
881 // Move cursor to next.
882 func (rc *SQLiteRows) Next(dest []driver.Value) error {
883 rv := C.sqlite3_step(rc.s.s)
884 if rv == C.SQLITE_DONE {
887 if rv != C.SQLITE_ROW {
888 rv = C.sqlite3_reset(rc.s.s)
889 if rv != C.SQLITE_OK {
890 return rc.s.c.lastError()
895 if rc.decltype == nil {
896 rc.decltype = make([]string, rc.nc)
897 for i := 0; i < rc.nc; i++ {
898 rc.decltype[i] = strings.ToLower(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))))
902 for i := range dest {
903 switch C.sqlite3_column_type(rc.s.s, C.int(i)) {
904 case C.SQLITE_INTEGER:
905 val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
906 switch rc.decltype[i] {
907 case "timestamp", "datetime", "date":
909 // Assume a millisecond unix timestamp if it's 13 digits -- too
910 // large to be a reasonable timestamp in seconds.
911 if val > 1e12 || val < -1e12 {
912 val *= int64(time.Millisecond) // convert ms to nsec
914 val *= int64(time.Second) // convert sec to nsec
916 t = time.Unix(0, val).UTC()
917 if rc.s.c.loc != nil {
927 dest[i] = float64(C.sqlite3_column_double(rc.s.s, C.int(i)))
929 p := C.sqlite3_column_blob(rc.s.s, C.int(i))
934 n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i)))
935 switch dest[i].(type) {
937 dest[i] = (*[1 << 30]byte)(unsafe.Pointer(p))[0:n]
939 slice := make([]byte, n)
940 copy(slice[:], (*[1 << 30]byte)(unsafe.Pointer(p))[0:n])
947 var timeVal time.Time
949 n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i)))
950 s := C.GoStringN((*C.char)(unsafe.Pointer(C.sqlite3_column_text(rc.s.s, C.int(i)))), C.int(n))
952 switch rc.decltype[i] {
953 case "timestamp", "datetime", "date":
955 s = strings.TrimSuffix(s, "Z")
956 for _, format := range SQLiteTimestampFormats {
957 if timeVal, err = time.ParseInLocation(format, s, time.UTC); err == nil {
963 // The column is a time value, so return the zero time on parse failure.
966 if rc.s.c.loc != nil {