gitlab.com/vegasq/sqlite@v1.0.0/sqlite_go18.go (about)

     1  // Copyright 2017 The Sqlite Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //+build go1.8
     6  
     7  package sqlite // import "modernc.org/sqlite"
     8  
     9  import (
    10  	"context"
    11  	"database/sql/driver"
    12  	"errors"
    13  	"unsafe"
    14  )
    15  
    16  // Ping implements driver.Pinger
    17  func (c *conn) Ping(ctx context.Context) error {
    18  	c.Lock()
    19  	defer c.Unlock()
    20  
    21  	if uintptr(unsafe.Pointer(c.ppdb)) == 0 {
    22  		return errors.New("db is closed")
    23  	}
    24  
    25  	_, err := c.ExecContext(ctx, "select 1", nil)
    26  	return err
    27  }
    28  
    29  // BeginTx implements driver.ConnBeginTx
    30  func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
    31  	return c.begin(ctx, txOptions{
    32  		Isolation: int(opts.Isolation),
    33  		ReadOnly:  opts.ReadOnly,
    34  	})
    35  }
    36  
    37  // PrepareContext implements driver.ConnPrepareContext
    38  func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
    39  	return c.prepare(ctx, query)
    40  }
    41  
    42  // ExecContext implements driver.ExecerContext
    43  func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
    44  	return c.exec(ctx, query, toNamedValues2(args))
    45  }
    46  
    47  // QueryContext implements driver.QueryerContext
    48  func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
    49  	return c.query(ctx, query, toNamedValues2(args))
    50  }
    51  
    52  // ExecContext implements driver.StmtExecContext
    53  func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
    54  	return s.exec(ctx, toNamedValues2(args))
    55  }
    56  
    57  // QueryContext implements driver.StmtQueryContext
    58  func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
    59  	return s.query(ctx, toNamedValues2(args))
    60  }
    61  
    62  // converts []driver.NamedValue to []namedValue
    63  func toNamedValues2(vals []driver.NamedValue) []namedValue {
    64  	args := make([]namedValue, 0, len(vals))
    65  	for _, val := range vals {
    66  		args = append(args, namedValue(val))
    67  	}
    68  	return args
    69  }