Esempio n. 1
0
func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result, error) {
	if err := s.bind(args); err != nil {
		C.sqlite3_reset(s.s)
		C.sqlite3_clear_bindings(s.s)
		return nil, err
	}

	done := make(chan struct{})
	defer close(done)
	go func() {
		select {
		case <-ctx.Done():
			C.sqlite3_interrupt(s.c.db)
		case <-done:
		}
	}()

	var rowid, changes C.longlong
	rv := C._sqlite3_step(s.s, &rowid, &changes)
	if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
		err := s.c.lastError()
		C.sqlite3_reset(s.s)
		C.sqlite3_clear_bindings(s.s)
		return nil, err
	}

	return &SQLiteResult{id: int64(rowid), changes: int64(changes)}, nil
}
Esempio n. 2
0
// Execute the statement with arguments. Return result object.
func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
	if err := s.bind(args); err != nil {
		C.sqlite3_reset(s.s)
		C.sqlite3_clear_bindings(s.s)
		return nil, err
	}
	var rowid, changes C.longlong
	rv := C._sqlite3_step(s.s, &rowid, &changes)
	if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
		err := s.c.lastError()
		C.sqlite3_reset(s.s)
		C.sqlite3_clear_bindings(s.s)
		return nil, err
	}
	return &SQLiteResult{int64(rowid), int64(changes)}, nil
}
Esempio n. 3
0
func (stmt Stmt) bind(args []driver.Value) error {
	if C.sqlite3_clear_bindings(stmt.stmt) != C.SQLITE_OK {
		return stmtError(stmt.stmt)
	}
	for i, arg := range args {
		r := C.int(-1)
		switch val := arg.(type) {
		case int64:
			r = C.sqlite3_bind_int64(stmt.stmt, C.int(i+1), C.sqlite3_int64(val))
		case float64:
			r = C.sqlite3_bind_double(stmt.stmt, C.int(i+1), C.double(val))
		case bool:
			if val {
				r = C.sqlite3_bind_int64(stmt.stmt, C.int(i+1), C.sqlite3_int64(1))
			} else {
				r = C.sqlite3_bind_int64(stmt.stmt, C.int(i+1), C.sqlite3_int64(0))
			}
		case nil:
			r = C.sqlite3_bind_null(stmt.stmt, C.int(i+1))
		case string:
			str := C.CString(val)
			defer C.free(unsafe.Pointer(str))
			l := C.int(C.strlen(str))
			r = C.bind_text(stmt.stmt, C.int(i+1), str, l)
		default:
			panic("unsupported type")
		}

		if r != C.SQLITE_OK {
			return stmtError(stmt.stmt)
		}
	}
	return nil
}
Esempio n. 4
0
// Reset returns the prepared statement to its initial state, ready to be
// re-executed. This should be done when the remaining rows returned by a query
// are not needed, which releases some resources that would otherwise persist
// until the next call to Exec or Query.
// [http://www.sqlite.org/c3ref/reset.html]
func (s *Stmt) Reset() {
	if s.haveRow {
		s.haveRow = false
		C.sqlite3_reset(s.stmt)
		if s.nVars > 0 {
			C.sqlite3_clear_bindings(s.stmt)
		}
	}
}
Esempio n. 5
0
// step evaluates the next step in the statement's program, automatically
// resetting the statement if the result is anything other than SQLITE_ROW.
func (s *Stmt) step() error {
	s.colTypes = s.colTypes[:0]
	s.haveRow = C.sqlite3_step(s.stmt) == ROW
	if !s.haveRow {
		// If step returned DONE, reset returns OK. Otherwise, reset returns the
		// same error code as step (v2 interface).
		rc := C.sqlite3_reset(s.stmt)
		if s.nVars > 0 {
			C.sqlite3_clear_bindings(s.stmt)
		}
		if rc != OK {
			return libErr(rc, s.conn.db)
		}
	}
	return nil
}
Esempio n. 6
0
// exec resets the prepared statement, binds new parameter values, and executes
// the first step.
func (s *Stmt) exec(args []interface{}) (err error) {
	if s.haveRow {
		s.Reset()
	}
	if named := namedArgs(args); named != nil {
		err = s.bindNamed(named)
	} else {
		err = s.bindUnnamed(args)
	}
	if err == nil {
		err = s.step()
		if s.nCols > 0 {
			// If the statement was recompiled (v2 interface, no indication),
			// then column counts, names, and declarations may have changed and
			// need to be reloaded.
			s.nCols = int(C.sqlite3_column_count(s.stmt))
			s.colNames = s.colNames[:0]
			s.colDecls = s.colDecls[:0]
		}
	} else if s.nVars > 0 {
		C.sqlite3_clear_bindings(s.stmt)
	}
	return
}
Esempio n. 7
0
func (self *sqlStatement) sqlClearBindings() int {
	return int(C.sqlite3_clear_bindings(self.handle))
}
Esempio n. 8
0
// ClearBindings resets all bindings on a prepared statement.
// (See http://sqlite.org/c3ref/clear_bindings.html)
func (s *Stmt) ClearBindings() error {
	return s.error(C.sqlite3_clear_bindings(s.stmt), "Stmt.ClearBindings")
}
Esempio n. 9
0
func (s *Statement) ClearBindings() os.Error {
	if e := Errno(C.sqlite3_clear_bindings(s.cptr)); e != OK {
		return e
	}
	return nil
}
Esempio n. 10
0
// ClearBindings is used to reset all parameters to NULL.
func (s *Statement) ClearBindings() error {
	return SQLiteError(C.sqlite3_clear_bindings(s.cptr))
}