本文整理汇总了Golang中database/sql.Tx类的典型用法代码### 示例。如果您正苦于以下问题:Golang Tx类的具体用法?Golang Tx怎么用?Golang Tx使用的例子?那么恭喜您, 这里精选的类代码### 示例或许可以为您提供帮助。
在下文中一共展示了Tx类的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。
示例1: createLoadStmt
func createLoadStmt(tableName *string, values *[]string, vld_op string, blWithPK bool, db *sql.Tx, verbose *bool) *sql.Stmt {
if len(*values) == 0 {
log.Fatalln("Nothing to build insert with!")
}
var buffer bytes.Buffer
buffer.WriteString("INSERT ")
if len(vld_op) > 0 && blWithPK {
buffer.WriteString(" OR " + vld_op + " ")
}
buffer.WriteString(" INTO " + (*tableName) + " VALUES (")
for i := range *values {
buffer.WriteString("?")
if i != len(*values)-1 {
buffer.WriteString(", ")
}
}
buffer.WriteString(");")
if *verbose {
fmt.Println(buffer.String())
}
stmt, err := db.Prepare(buffer.String())
if err != nil {
log.Fatalln(err)
}
return stmt
}
开发者ID:robinmin,项目名称:textql,代码行数:29,代码来源:main.go
示例2: setTablesDataTransaction
func setTablesDataTransaction(data map[string][]map[string]string, tx *sql.Tx) error {
for tableName, tableData := range data {
sql := fmt.Sprintf("truncate `%s`", tableName)
_, err := tx.Exec(sql)
if err != nil {
return err
}
for _, row := range tableData {
colNameList := []string{}
placeHolderNum := len(row)
valueList := []interface{}{}
for name, value := range row {
colNameList = append(colNameList, name)
valueList = append(valueList, value)
}
sqlColNamePart := "`" + strings.Join(colNameList, "`, `") + "`"
sqlValuePart := strings.Repeat("?, ", placeHolderNum-1) + "?"
sql = fmt.Sprintf("INSERT INTO `%s` (%s) VALUES (%s)", tableName, sqlColNamePart, sqlValuePart)
_, err := tx.Exec(sql, valueList...)
if err != nil {
return err
}
}
}
return nil
}
开发者ID:iizotop,项目名称:kmg,代码行数:26,代码来源:SetTableData.go
示例3: Down_20130708104836
// Down is executed when this migration is rolled back
func Down_20130708104836(txn *sql.Tx) {
_, err := txn.Exec(sqlDown)
if err != nil {
fmt.Printf("error adding col announce key: %s", err.Error())
}
}
开发者ID:frazy,项目名称:babou,代码行数:8,代码来源:20130708104836_AddAnnounceKeyToUsers.go
示例4: chooseWorkUnits
// chooseWorkUnits chooses up to a specified number of work units from
// some work spec.
func (w *worker) chooseWorkUnits(tx *sql.Tx, spec *workSpec, numUnits int) ([]*workUnit, error) {
query := buildSelect([]string{
workUnitID,
workUnitName,
}, []string{
workUnitTable,
}, []string{
inThisWorkSpec,
workUnitAttempt + " IS NULL",
})
query += fmt.Sprintf(" ORDER BY priority DESC, name ASC")
query += fmt.Sprintf(" LIMIT %v", numUnits)
query += " FOR UPDATE OF work_unit"
rows, err := tx.Query(query, spec.id)
if err != nil {
return nil, err
}
var result []*workUnit
err = scanRows(rows, func() error {
unit := workUnit{spec: spec}
if err := rows.Scan(&unit.id, &unit.name); err == nil {
result = append(result, &unit)
}
return err
})
if err != nil {
return nil, err
}
return result, nil
}
开发者ID:dmaze,项目名称:goordinate,代码行数:32,代码来源:attempt.go
示例5: insert
// insert saves a profile to a role
func (m *RoleProfileType) insert(
tx *sql.Tx,
siteID int64,
roleID int64,
) (
int,
error,
) {
status, err := m.Validate(siteID)
if err != nil {
return status, err
}
// upsert
_, err = tx.Exec(`
INSERT INTO role_profiles
SELECT $1, $2
WHERE NOT EXISTS (
SELECT role_id
,profile_id
FROM role_profiles
WHERE role_id = $1
AND profile_id = $2
)`,
roleID,
m.ID,
)
if err != nil {
return http.StatusInternalServerError,
fmt.Errorf("Error executing upsert: %v", err.Error())
}
return http.StatusOK, nil
}
开发者ID:riseofthetigers,项目名称:microcosm,代码行数:36,代码来源:role_profiles.go
最后编辑: kuteng 文档更新时间: 2021-08-23 19:14 作者:kuteng