Example #1
0
// Prepare query string. Return a new statement.
func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
	pquery := C.CString(query)
	defer C.free(unsafe.Pointer(pquery))
	var s *C.sqlite3_stmt
	var tail *C.char
	rv := C.sqlite3_prepare_v2(c.db, pquery, -1, &s, &tail)
	if rv != C.SQLITE_OK {
		return nil, c.lastError()
	}
	var t string
	if tail != nil && *tail != '\000' {
		t = strings.TrimSpace(C.GoString(tail))
	}
	nv := int(C.sqlite3_bind_parameter_count(s))
	var nn []string
	for i := 0; i < nv; i++ {
		pn := C.GoString(C.sqlite3_bind_parameter_name(s, C.int(i+1)))
		if len(pn) > 1 && pn[0] == '$' && 48 <= pn[1] && pn[1] <= 57 {
			nn = append(nn, C.GoString(C.sqlite3_bind_parameter_name(s, C.int(i+1))))
		}
	}
	ss := &SQLiteStmt{c: c, s: s, nv: nv, nn: nn, t: t}
	runtime.SetFinalizer(ss, (*SQLiteStmt).Close)
	return ss, nil
}
Example #2
0
func (c *Conn) prepare(sql string, args ...interface{}) (*Stmt, error) {
	if c == nil {
		return nil, errors.New("nil sqlite database")
	}
	sqlstr := C.CString(sql)
	defer C.free(unsafe.Pointer(sqlstr))
	var stmt *C.sqlite3_stmt
	var tail *C.char
	rv := C.sqlite3_prepare_v2(c.db, sqlstr, -1, &stmt, &tail)
	if rv != C.SQLITE_OK {
		// C.sqlite3_finalize(stmt) // If there is an error, *stmt is set to NULL
		return nil, c.error(rv, sql)
	}
	var t string
	if tail != nil && *tail != '\000' {
		t = C.GoString(tail)
	}
	s := &Stmt{c: c, stmt: stmt, tail: strings.TrimSpace(t), columnCount: -1, bindParameterCount: -1}
	if len(args) > 0 {
		err := s.Bind(args...)
		if err != nil {
			s.finalize()
			return nil, err
		}
	}
	return s, nil
}
Example #3
0
// newStmt creates a new prepared statement.
func newStmt(c *Conn, sql string) (*Stmt, error) {
	zSql := sql + "\x00"

	var stmt *C.sqlite3_stmt
	var tail *C.char
	rc := C.sqlite3_prepare_v2(c.db, cStr(zSql), -1, &stmt, &tail)
	if rc != OK {
		return nil, libErr(rc, c.db)
	}

	// stmt will be nil if sql contained only comments or whitespace. s.Tail may
	// be useful to the caller, so s is still returned without an error.
	s := &Stmt{conn: c, stmt: stmt}
	if stmt != nil {
		if s.nVars = int(C.sqlite3_bind_parameter_count(stmt)); s.nVars == 0 {
			s.varNames = unnamedVars
		}
		s.nCols = int(C.sqlite3_column_count(stmt))
		runtime.SetFinalizer(s, (*Stmt).Close)
	}
	if tail != nil {
		if n := cStrOffset(zSql, tail); n < len(sql) {
			sql, s.Tail = sql[:n], sql[n:]
		}
	}
	s.text = sql
	return s, nil
}
Example #4
0
func (db *Database) Prepare(sql string, values ...interface{}) (s *Statement, e error) {
	s = &Statement{db: db, timestamp: time.Now().Unix()}
	if rv := Errno(C.sqlite3_prepare_v2(db.handle, C.CString(sql), -1, &s.cptr, nil)); rv != OK {
		s, e = nil, rv
	} else {
		if len(values) > 0 {
			e, _ = s.BindAll(values...)
		}
	}
	return
}
Example #5
0
func (c *Conn) Prepare(cmd string) (*Stmt, os.Error) {
	cmdstr := C.CString(cmd)
	defer C.free(unsafe.Pointer(cmdstr))
	var stmt *C.sqlite3_stmt
	var tail *C.char
	rv := C.sqlite3_prepare_v2(c.db, cmdstr, C.int(len(cmd)+1), &stmt, &tail)
	if rv != 0 {
		return nil, c.error(rv)
	}
	return &Stmt{c: c, stmt: stmt}, nil
}
Example #6
0
func (c *Conn) Prepare(cmd string) (*Stmt, error) {
	if c == nil || c.db == nil {
		return nil, errors.New("nil sqlite database")
	}
	cmdstr := C.CString(cmd)
	defer C.free(unsafe.Pointer(cmdstr))
	var stmt *C.sqlite3_stmt
	var tail *C.char
	rv := C.sqlite3_prepare_v2(c.db, cmdstr, C.int(len(cmd)+1), &stmt, &tail)
	if rv != 0 {
		return nil, c.error(rv)
	}
	return &Stmt{c: c, stmt: stmt, sql: cmd, t0: time.Now()}, nil
}
Example #7
0
File: driver.go Project: rsc/sqlite
func (c *conn) Prepare(cmd string) (driver.Stmt, error) {
	if c.closed {
		panic("database/sql/driver: misuse of sqlite driver: Prepare after Close")
	}
	cmdstr := C.CString(cmd)
	defer C.free(unsafe.Pointer(cmdstr))
	var s *C.sqlite3_stmt
	var tail *C.char
	rv := C.sqlite3_prepare_v2(c.db, cmdstr, C.int(len(cmd)+1), &s, &tail)
	if rv != 0 {
		return nil, c.error(rv)
	}
	return &stmt{c: c, stmt: s, sql: cmd, t0: time.Now()}, nil
}
Example #8
0
func (conn Conn) Prepare(query string) (driver.Stmt, error) {
	s := C.CString(query)
	defer C.free(unsafe.Pointer(s))
	l := C.int(C.strlen(s))

	var stmt Stmt
	var tail *C.char
	r := C.sqlite3_prepare_v2(conn.db, s, l, &stmt.stmt, &tail)

	if r != C.SQLITE_OK {
		return &stmt, dbError(conn.db)
	}
	return &stmt, nil
}
Example #9
0
// Prepare query string. Return a new statement.
func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
	pquery := C.CString(query)
	defer C.free(unsafe.Pointer(pquery))
	var s *C.sqlite3_stmt
	var perror *C.char
	rv := C.sqlite3_prepare_v2(c.db, pquery, -1, &s, &perror)
	if rv != C.SQLITE_OK {
		return nil, errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
	}
	var t string
	if perror != nil && C.strlen(perror) > 0 {
		t = C.GoString(perror)
	}
	return &SQLiteStmt{c: c, s: s, t: t}, nil
}
Example #10
0
// Prepare query string. Return a new statement.
func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
	pquery := C.CString(query)
	defer C.free(unsafe.Pointer(pquery))
	var s *C.sqlite3_stmt
	var tail *C.char
	rv := C.sqlite3_prepare_v2(c.db, pquery, -1, &s, &tail)
	if rv != C.SQLITE_OK {
		return nil, c.lastError()
	}
	var t string
	if tail != nil && C.strlen(tail) > 0 {
		t = strings.TrimSpace(C.GoString(tail))
	}
	return &SQLiteStmt{c: c, s: s, t: t}, nil
}
Example #11
0
func (c *SQLiteConn) prepare(ctx context.Context, query string) (driver.Stmt, error) {
	pquery := C.CString(query)
	defer C.free(unsafe.Pointer(pquery))
	var s *C.sqlite3_stmt
	var tail *C.char
	rv := C.sqlite3_prepare_v2(c.db, pquery, -1, &s, &tail)
	if rv != C.SQLITE_OK {
		return nil, c.lastError()
	}
	var t string
	if tail != nil && *tail != '\000' {
		t = strings.TrimSpace(C.GoString(tail))
	}
	ss := &SQLiteStmt{c: c, s: s, t: t}
	runtime.SetFinalizer(ss, (*SQLiteStmt).Close)
	return ss, nil
}
Example #12
0
func (c *Conn) Prepare(cmd string) (*Stmt, error) {
	if c == nil || c.db == nil {
		return nil, errors.New("nil sqlite database")
	}
	cmdstr := C.CString(cmd)
	defer C.free(unsafe.Pointer(cmdstr))
	var stmt *C.sqlite3_stmt
	var tail *C.char
	// the next line causes a compiler warning because the last
	// argument should be "const char **" but there is no way
	// to express const-ness to CGO
	rv := C.sqlite3_prepare_v2(c.db, cmdstr, C.int(len(cmd)+1), &stmt, &tail)
	if rv != 0 {
		return nil, c.error(rv)
	}
	return &Stmt{c: c, stmt: stmt, sql: cmd, t0: time.Now()}, nil
}
Example #13
0
// Passing multiple sql commands in a single command string requires some extra
// work to make it go. Sqlite normally executes only one expression and returns
// the remainder in the @tail parameter of sqlite3_prepare(). This sqlite binding
// does not take that into account. We need to loop and process @tail until it
// is empty. This also means taking care of any parameters we pass to Stmt.Exec.
// We need to ensure that we offset the supplied argument list after every
// execution.
func (c *Conn) ExecRange(cmd string, argv ...interface{}) (err os.Error) {
	var tail *C.char
	var rv C.int
	var errno Errno

	cmdlen := len(cmd) + 1
	s := new(Stmt)
	s.c = c

	cmdstr := C.CString(strings.TrimSpace(cmd))
	defer C.free(unsafe.Pointer(cmdstr))

	for {
		if rv = C.sqlite3_prepare_v2(c.db, cmdstr, C.int(cmdlen), &s.stmt, &tail); rv != 0 {
			return c.error(rv)
		}

		if rv = C.sqlite3_bind_parameter_count(s.stmt); int(rv) > len(argv) {
			return os.NewError(fmt.Sprintf("incorrect argument count: have %d, want %d", len(argv), rv))
		}

		if err = s.Exec(argv[0:rv]...); err != nil {
			s.Finalize()
			return
		}

		argv = argv[rv:]

		if errno = Errno(C.sqlite3_step(s.stmt)); errno != Done {
			s.Finalize()
			return errno
		}

		s.Finalize()

		if C.GoString(tail) == "" {
			break
		}

		cmdstr = tail
		cmdlen = len(C.GoString(cmdstr)) + 1
	}

	return
}
Example #14
0
func (self *sqlConnection) sqlPrepare(query string) (stat *sqlStatement, rc int) {
	stat = new(sqlStatement)

	p := C.CString(query)
	// TODO: may need tail to process statement sequence? or at
	// least to generate an error that we missed some SQL?
	//
	// -1: process query until 0 byte
	// nil: don't return tail pointer
	rc = int(C.sqlite3_prepare_v2(self.handle, p, -1, &stat.handle, nil))
	C.free(unsafe.Pointer(p))

	// We are not supposed to get a handle on error. Since
	// sqlite3_open() follows a different rule, however, we
	// indulge in paranoia and check to make sure. We really
	// don't want to return a statement on error.
	if rc != StatusOk && stat.handle != nil {
		_ = stat.sqlFinalize()
		stat = nil
	}

	return
}