Пример #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
}
Пример #2
0
// BindParameterName returns the name of a wildcard parameter (not cached).
// Returns "" if the index is out of range or if the wildcard is unnamed.
// The first host parameter has an index of 1, not 0.
// (See http://sqlite.org/c3ref/bind_parameter_name.html)
func (s *Stmt) BindParameterName(index int) (string, error) {
	name := C.sqlite3_bind_parameter_name(s.stmt, C.int(index))
	if name == nil {
		return "", s.specificError("invalid parameter index: %d", index)
	}
	return C.GoString(name), nil
}
Пример #3
0
// Params returns the names of bound parameters in the prepared statement. Nil
// is returned if the statement does not use named parameters.
// [http://www.sqlite.org/c3ref/bind_parameter_name.html]
func (s *Stmt) Params() []string {
	if s.varNames == nil {
		var names []string
		for i := 0; i < s.nVars; i++ {
			name := C.sqlite3_bind_parameter_name(s.stmt, C.int(i+1))
			if name == nil {
				names = unnamedVars
				break
			}
			if names == nil {
				names = make([]string, s.nVars)
			}
			names[i] = C.GoString(name)
		}
		s.varNames = names
	}
	if len(s.varNames) == 0 {
		return nil // unnamedVars -> nil
	}
	return s.varNames
}