本文整理匯總了Golang中database/sql.Tx.Stmt方法的典型用法代碼示例。如果您正苦於以下問題:Golang Tx.Stmt方法的具體用法?Golang Tx.Stmt怎麽用?Golang Tx.Stmt使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類database/sql.Tx
的用法示例。
在下文中一共展示了Tx.Stmt方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Execute
func (p *TnCommandContextHandler) Execute(args []interface{}, tx *sql.Tx, behavior *Behavior) interface{} {
p.sql = (*p.CommandContext).getSql()
// fmt.Printf("getBindVariables %v\n",(*p.CommandContext).getBindVariables())
// p.logSql((*p.CommandContext).getBindVariables(),
// (*p.CommandContext).getBindVariableTypes())
// log.Flush()
// fmt.Printf("statementFactory %v \n",p.statementFactory)
bindVariables := (*p.CommandContext).getBindVariables()
bindVariableTypes := (*p.CommandContext).getBindVariableTypes()
dbc := (*(*behavior).GetBaseBehavior().GetBehaviorCommandInvoker().InvokerAssistant).GetDBCurrent()
ps := (*p.statementFactory).PrepareStatement(p.sql, tx, dbc)
defer ps.Close()
bindVar := (*p.statementFactory).ModifyBindVariables(bindVariables, bindVariableTypes)
p.logSql(bindVariables, bindVariableTypes)
log.Flush()
res, err := tx.Stmt(ps).Exec(bindVar.data...)
if err != nil {
panic(err.Error())
}
updateno, _ := res.RowsAffected()
log.InternalDebug(fmt.Sprintln("result no:", updateno))
return updateno
}
示例2: TxStmt
// TxStmt convert a prepared statement to a transaction specific prepared
// statement.
func TxStmt(sql string, tx *sql.Tx) *sql.Stmt {
dbStmt, ok := StmtMap[sql]
if !ok {
log.Fatalf("No Prepared SQL statement for: %s", sql)
}
return tx.Stmt(dbStmt)
}
示例3: cascadeDelete
func (t *Table) cascadeDelete(txn *sql.Tx, pk interface{}) error {
for _, child := range t.Children {
child.cascadeDelete(txn, pk)
}
stmt := txn.Stmt(t.stmts.stmtDelete)
_, e := stmt.Exec(pk)
return e
}
示例4: execInsert
func execInsert(db *sql.DB, query string, parameters ...interface{}) {
var err error
var tx *sql.Tx
var stmt *sql.Stmt
stmt, err = db.Prepare(query)
tx, err = db.Begin()
_, err = tx.Stmt(stmt).Exec(parameters...)
err = tx.Commit()
if err != nil {
log.Fatalf("Insert error %q\n", err)
}
}
示例5: updateDevice
func (s *querysrv) updateDevice(tx *sql.Tx, device protocol.DeviceID) error {
res, err := tx.Stmt(s.prep["updateDevice"]).Exec(device.String())
if err != nil {
return err
}
if rows, _ := res.RowsAffected(); rows == 0 {
_, err := tx.Stmt(s.prep["insertDevice"]).Exec(device.String())
if err != nil {
return err
}
}
return nil
}
示例6: updateAddress
func (s *querysrv) updateAddress(ctx context.Context, tx *sql.Tx, device protocol.DeviceID, uri string) error {
res, err := tx.Stmt(s.prep["updateAddress"]).Exec(device.String(), uri)
if err != nil {
return err
}
if rows, _ := res.RowsAffected(); rows == 0 {
_, err := tx.Stmt(s.prep["insertAddress"]).Exec(device.String(), uri)
if err != nil {
return err
}
}
return nil
}
示例7: execute
func (t *TnBasicSelectHandler) execute(bindVariables *List, bindVariableTypes *StringList, tx *sql.Tx, behavior *Behavior) interface{} {
dbc := (*(*behavior).GetBaseBehavior().GetBehaviorCommandInvoker().InvokerAssistant).GetDBCurrent()
ps := (*t.statementFactory).PrepareStatement(t.sql, tx, dbc)
defer ps.Close()
bindVar := (*t.statementFactory).ModifyBindVariables(bindVariables, bindVariableTypes)
//ps := t.prepareStatement(tx, dbc)
// ns:=new(sql.NullString)
// ns.Valid=true
// ns.String="2"
// var itest interface{}=ns
t.logSql(bindVariables, bindVariableTypes)
var rows *sql.Rows
var err error
if bindVariables == nil {
rows, err = tx.Stmt(ps).Query()
if err != nil {
panic(err.Error())
}
} else {
rows, err = tx.Stmt(ps).Query(bindVar.data...)
if err != nil {
panic(err.Error())
}
}
log.InternalDebug(fmt.Sprintln("ResultType:", t.ResultType))
rh := new(ResultSetHandler)
//l := BhvUtil_I.GetListResultBean(rows, t.ResultType,t.SqlClause)
l := rh.GetListResultBean(rows, t.ResultType, t.SqlClause)
log.InternalDebug(fmt.Sprintln("result no:", l.List.Size()))
log.InternalDebug(fmt.Sprintf("data %v\n", l.List.Get(0)))
// logSql(args, argTypes);
// PreparedStatement ps = null;
// try {
// ps = prepareStatement(conn);
// bindArgs(conn, ps, args, argTypes);
// return queryResult(ps);
// } catch (SQLException e) {
// final SQLExceptionResource resource = createSQLExceptionResource();
// resource.setNotice("Failed to execute the SQL for select.");
// handleSQLException(e, resource);
// return null; // unreachable
// } finally {
// close(ps);
// }
return l
}
示例8: addSCTFeedbackIfNotExists
func (s *Storage) addSCTFeedbackIfNotExists(tx *sql.Tx, chainID, sctID int64) error {
stmt := tx.Stmt(s.insertSCTFeedback)
_, err := stmt.Exec(chainID, sctID)
if err != nil {
switch err.(type) {
case sqlite3.Error:
// If this is a dupe that's fine, no need to return an error
if err.(sqlite3.Error).Code != sqlite3.ErrConstraint {
return err
}
default:
return err
}
}
return nil
}
示例9: insertThingOrSelectID
// insertThingOrSelectID will attempt to execute the insert Statement (under transaction tx), if that fails due to
// a unique primary key constraint, it will look up that primary key by executing the getID Statement.
// Returns the ID associated with persistent thing, or an error describing the failure.
func insertThingOrSelectID(tx *sql.Tx, insert *sql.Stmt, getID *sql.Stmt, thing interface{}) (int64, error) {
txInsert := tx.Stmt(insert)
txGetID := tx.Stmt(getID)
r, err := txInsert.Exec(thing)
if err != nil {
switch e := err.(type) {
case sqlite3.Error:
if e.Code == sqlite3.ErrConstraint {
return selectThingID(txGetID, thing)
}
}
return -1, err
}
id, err := r.LastInsertId()
if err != nil {
return -1, err
}
return id, nil
}
示例10: addSTHIfNotExists
func (s *Storage) addSTHIfNotExists(tx *sql.Tx, sth ct.SignedTreeHead) error {
stmt := tx.Stmt(s.insertSTHPollination)
sigB64, err := sth.TreeHeadSignature.Base64String()
if err != nil {
return fmt.Errorf("Failed to base64 sth signature: %v", err)
}
_, err = stmt.Exec(sth.Version, sth.TreeSize, sth.Timestamp, sth.SHA256RootHash.Base64String(), sigB64, sth.LogID.Base64String())
if err != nil {
switch err.(type) {
case sqlite3.Error:
// If this is a dupe that's fine, no need to return an error
if err.(sqlite3.Error).Code != sqlite3.ErrConstraint {
return err
}
default:
return err
}
}
return nil
}
示例11: Tx
func (p *prepareds) Tx(tx *sql.Tx) (px *prepareds, err error) {
px = &prepareds{}
t := reflect.TypeOf(p).Elem()
v := reflect.Indirect(reflect.ValueOf(p))
v2 := reflect.Indirect(reflect.ValueOf(px))
nf := t.NumField()
for i := 0; i < nf; i++ {
f := t.Field(i)
if f.Tag == "" {
continue
}
fv := v.Field(i)
fv2 := v2.Field(i)
fvi := fv.Interface()
if fvi != nil {
if stmt, ok := fvi.(*sql.Stmt); ok {
fv2.Set(reflect.ValueOf(tx.Stmt(stmt)))
}
}
}
return
}
示例12: CreateUserInTx
func (store *DBStore) CreateUserInTx(tx *sql.Tx, email, password string) (user *User, err error) {
createdAt := time.Now()
salt, hash := crypto.Base64SaltAndHash(password, 64)
var userId int64
if err = tx.Stmt(store.insertUserStmt).QueryRow(email, salt, hash, createdAt, createdAt).Scan(&userId); err != nil {
// and error "sql: no rows in result set" is returned when the insert fails
// for example, because there is a unique index collision (e.g) email
return
}
user = &User{
Id: userId,
Email: strings.ToLower(email),
PasswordSalt: salt,
PasswordHash: hash,
CreatedAt: createdAt,
UpdatedAt: createdAt,
}
return
}
示例13: StoreEvent
func StoreEvent(event *data.Event) (err error) {
// insert the tags and bail if we get an error
// TODO, do this within a transaction
var tagIds []int64
if tagIds, err = findOrCreateTags(event.Tags); err != nil {
return
}
var tx *sql.Tx
if tx, err = pg.Begin(); err != nil {
return
}
defer /* panic-recover */ func() {
if err != nil {
tx.Rollback()
} else {
tx.Commit()
}
}()
if err = tx.Stmt(insertEventStmt).QueryRow(event.Desc).Scan(&event.Id); err != nil {
return
}
if err = tagEvents(tx, event.Id, tagIds); err != nil {
return
}
newEvents.Events <- event
return
}
示例14: updateDevice
func (s *querysrv) updateDevice(ctx context.Context, tx *sql.Tx, device protocol.DeviceID) error {
reqID := ctx.Value("id").(requestID)
t0 := time.Now()
res, err := tx.Stmt(s.prep["updateDevice"]).Exec(device.String())
if err != nil {
return err
}
if debug {
log.Println(reqID, "updateDevice in", time.Since(t0))
}
if rows, _ := res.RowsAffected(); rows == 0 {
t0 = time.Now()
_, err := tx.Stmt(s.prep["insertDevice"]).Exec(device.String())
if err != nil {
return err
}
if debug {
log.Println(reqID, "insertDevice in", time.Since(t0))
}
}
return nil
}
示例15: cleanOldEntries
func (s *cleansrv) cleanOldEntries() (err error) {
var tx *sql.Tx
tx, err = s.db.Begin()
if err != nil {
return err
}
defer func() {
if err == nil {
err = tx.Commit()
} else {
tx.Rollback()
}
}()
res, err := tx.Stmt(s.prep["cleanAddress"]).Exec()
if err != nil {
return err
}
if rows, _ := res.RowsAffected(); rows > 0 {
log.Printf("Clean: %d old addresses", rows)
}
res, err = tx.Stmt(s.prep["cleanDevice"]).Exec()
if err != nil {
return err
}
if rows, _ := res.RowsAffected(); rows > 0 {
log.Printf("Clean: %d old devices", rows)
}
var devs, addrs int
row := tx.Stmt(s.prep["countDevice"]).QueryRow()
if err = row.Scan(&devs); err != nil {
return err
}
row = tx.Stmt(s.prep["countAddress"]).QueryRow()
if err = row.Scan(&addrs); err != nil {
return err
}
log.Printf("Database: %d devices, %d addresses", devs, addrs)
return nil
}