当前位置: 首页>>代码示例>>Golang>>正文


Golang Tx.Commit方法代码示例

本文整理汇总了Golang中github.com/jmoiron/sqlx.Tx.Commit方法的典型用法代码示例。如果您正苦于以下问题:Golang Tx.Commit方法的具体用法?Golang Tx.Commit怎么用?Golang Tx.Commit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/jmoiron/sqlx.Tx的用法示例。


在下文中一共展示了Tx.Commit方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: DeleteById

func (b *Base) DeleteById(tx *sqlx.Tx, id string) (sql.Result, error) {
	var result sql.Result

	if b.table == "" {
		return nil, errors.New("Table must not be empty.")
	}

	tx, wrapInSingleTransaction, err := b.newTransactionIfNeeded(tx)
	if tx == nil {
		return nil, errors.New("Transaction struct must not be empty.")
	}
	if err != nil {
		return nil, err
	}

	query := fmt.Sprintf("DELETE FROM %v WHERE id=$1", b.table)

	result, err = tx.Exec(query, id)

	if wrapInSingleTransaction == true {
		err = tx.Commit()
	}

	if err != nil {
		return nil, err
	}

	return result, err
}
开发者ID:marpio,项目名称:webapp2,代码行数:29,代码来源:dal.go

示例2: DeleteFromTable

func (b *BaseStore) DeleteFromTable(tx *sqlx.Tx, where string) (sql.Result, error) {
	var result sql.Result

	if b.Table == "" {
		return nil, errors.New("Table must not be empty.")
	}

	tx, wrapInSingleTransaction, err := b.newTransactionIfNeeded(tx)
	if tx == nil {
		return nil, errors.New("Transaction struct must not be empty.")
	}
	if err != nil {
		return nil, err
	}

	query := fmt.Sprintf("DELETE FROM %v", b.Table)

	if where != "" {
		query = query + " WHERE " + where
	}

	result, err = tx.Exec(query)

	if wrapInSingleTransaction == true {
		err = tx.Commit()
	}

	if err != nil {
		return nil, err
	}

	return result, err
}
开发者ID:truongminh,项目名称:glowing,代码行数:33,代码来源:base.go

示例3: DeletePerson

func (s *PeopleStore) DeletePerson(id int64) (err error) {
	var tx *sqlx.Tx

	tx, err = s.db.Beginx()
	if err != nil {
		return
	}

	// Automatically rollback/commit if there's an error.
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()

	// Remove the given Person
	if _, err = tx.Exec(s.db.Rebind(personDeleteQuery), id); err != nil {
		return
	}

	// Done!
	return nil
}
开发者ID:jmptrader,项目名称:go-webapp-skeleton,代码行数:25,代码来源:person.go

示例4: DeleteList

func (s *ListStore) DeleteList(id int64) (err error) {
	var tx *sqlx.Tx

	tx, err = s.db.Beginx()
	if err != nil {
		return
	}

	// Automatically rollback/commit if there's an error.
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()

	// Remove todos in this list.
	if _, err = tx.Exec(s.db.Rebind(listDeleteTodosQuery), id); err != nil {
		return
	}

	// Remove the given List
	if _, err = tx.Exec(s.db.Rebind(listDeleteQuery), id); err != nil {
		return
	}

	// Done!
	return nil
}
开发者ID:andrew-d,项目名称:todo,代码行数:30,代码来源:list.go

示例5: CommitTransaction

func (db Database) CommitTransaction(tx *sqlx.Tx) error {
	err := tx.Commit()
	if err != nil {
		fmt.Println("Could not commit database transaction:", err)
	}
	return err
}
开发者ID:yeerkkiller1,项目名称:buildblast,代码行数:7,代码来源:database.go

示例6: InsertIntoTable

func (b *Base) InsertIntoTable(tx *sqlx.Tx, data map[string]interface{}) (sql.Result, error) {
	if b.table == "" {
		return nil, errors.New("Table must not be empty.")
	}

	tx, wrapInSingleTransaction, err := b.newTransactionIfNeeded(tx)
	if tx == nil {
		return nil, errors.New("Transaction struct must not be empty.")
	}
	if err != nil {
		return nil, err
	}

	keys := make([]string, 0)
	dollarMarks := make([]string, 0)
	values := make([]interface{}, 0)

	loopCounter := 1
	for key, value := range data {
		keys = append(keys, key)
		dollarMarks = append(dollarMarks, fmt.Sprintf("$%v", loopCounter))
		values = append(values, value)

		loopCounter++
	}

	query := fmt.Sprintf(
		"INSERT INTO %v (%v) VALUES (%v)",
		b.table,
		strings.Join(keys, ","),
		strings.Join(dollarMarks, ","))

	result := &InsertResult{}
	result.rowsAffected = 1

	if b.hasID {
		query = query + " RETURNING id"

		var lastInsertId int64
		err = tx.QueryRow(query, values...).Scan(&lastInsertId)
		if err != nil {
			return nil, err
		}

		result.lastInsertId = lastInsertId

	} else {
		_, err := tx.Exec(query, values...)
		if err != nil {
			return nil, err
		}
	}

	if wrapInSingleTransaction == true {
		err = tx.Commit()
	}

	return result, err
}
开发者ID:simudream,项目名称:resourced-master,代码行数:59,代码来源:base.go

示例7: UpdateByKeyValueString

func (b *Base) UpdateByKeyValueString(tx *sqlx.Tx, data map[string]interface{}, key, value string) (sql.Result, error) {
	var result sql.Result

	if b.table == "" {
		return nil, errors.New("Table must not be empty.")
	}

	tx, wrapInSingleTransaction, err := b.newTransactionIfNeeded(tx)
	if tx == nil {
		return nil, errors.New("Transaction struct must not be empty.")
	}
	if err != nil {
		return nil, err
	}

	keysWithDollarMarks := make([]string, 0)
	values := make([]interface{}, 0)

	loopCounter := 1
	for key, value := range data {
		keysWithDollarMark := fmt.Sprintf("%v=$%v", key, loopCounter)
		keysWithDollarMarks = append(keysWithDollarMarks, keysWithDollarMark)
		values = append(values, value)

		loopCounter++
	}

	// Add value as part of values
	values = append(values, value)

	query := fmt.Sprintf(
		"UPDATE %v SET %v WHERE %v=$%v",
		b.table,
		strings.Join(keysWithDollarMarks, ","),
		key,
		loopCounter)

	result, err = tx.Exec(query, values...)

	if err != nil {
		return nil, err
	}

	if wrapInSingleTransaction == true {
		err = tx.Commit()
	}

	return result, err
}
开发者ID:marpio,项目名称:webapp2,代码行数:49,代码来源:dal.go

示例8: transaction

// Transaction manages a db transaction, automatically calling commit
// on success or rollback on failure.
func transaction(fn func(*sqlx.Tx) error) (err error) {
	var tx *sqlx.Tx
	tx, err = db.Beginx()
	if err != nil {
		return err
	}
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			err = tx.Commit()
		}
	}()
	return fn(tx)
}
开发者ID:stepheninabox,项目名称:fowtcg,代码行数:17,代码来源:tidb.go

示例9: UpdateByID

func (b *BaseStore) UpdateByID(tx *sqlx.Tx, data map[string]interface{}, id int64) (sql.Result, error) {
	var result sql.Result

	if b.Table == "" {
		return nil, errors.New("Table must not be empty.")
	}

	tx, wrapInSingleTransaction, err := b.newTransactionIfNeeded(tx)
	if tx == nil {
		return nil, errors.New("Transaction struct must not be empty.")
	}
	if err != nil {
		return nil, err
	}

	keysWithQuestionMarks := make([]string, 0)
	values := make([]interface{}, 0)

	for key, value := range data {
		keysWithQuestionMark := fmt.Sprintf("%v=?", key)
		keysWithQuestionMarks = append(keysWithQuestionMarks, keysWithQuestionMark)
		values = append(values, value)
	}

	// Add id as part of values
	values = append(values, id)

	query := fmt.Sprintf(
		"UPDATE %v SET %v WHERE id=?",
		b.Table,
		strings.Join(keysWithQuestionMarks, ","))

	result, err = tx.Exec(query, values...)

	if err != nil {
		return nil, err
	}

	if wrapInSingleTransaction == true {
		err = tx.Commit()
	}

	return result, err
}
开发者ID:truongminh,项目名称:glowing,代码行数:44,代码来源:base.go

示例10: DeleteTodo

func (s *TodoStore) DeleteTodo(id int64) (err error) {
	var tx *sqlx.Tx

	tx, err = s.db.Beginx()
	if err != nil {
		return
	}

	// Automatically rollback/commit if there's an error.
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()

	// Get the given Todo
	todo := &model.Todo{}
	if err = tx.Get(todo, s.db.Rebind(todoGetQuery), id); err != nil {
		return
	}

	// Remove the given Todo
	if _, err = tx.Exec(s.db.Rebind(todoDeleteQuery), id); err != nil {
		return
	}

	// Fix any previous link - change any `previous_id` that points at our
	// current id to point at what we point at.
	if _, err = tx.Exec(s.db.Rebind(todoRelinkQuery),
		todo.PreviousID, // New previous_id
		id,              // Old previous_id
		todo.ListID,     // List ID
	); err != nil {
		return
	}

	// Done!
	return nil
}
开发者ID:andrew-d,项目名称:todo,代码行数:41,代码来源:todo.go

示例11: InsertIntoTable

func (b *BaseStore) InsertIntoTable(tx *sqlx.Tx, data map[string]interface{}) (sql.Result, error) {
	if b.Table == "" {
		return nil, errors.New("Table must not be empty.")
	}

	tx, wrapInSingleTransaction, err := b.newTransactionIfNeeded(tx)
	if tx == nil {
		return nil, errors.New("Transaction struct must not be empty.")
	}
	if err != nil {
		return nil, err
	}

	keys := make([]string, 0)
	qMarks := make([]string, 0)
	values := make([]interface{}, 0)

	for key, value := range data {
		keys = append(keys, key)
		qMarks = append(qMarks, "?")
		values = append(values, value)
	}

	query := fmt.Sprintf(
		"INSERT INTO %v (%v) VALUES (%v)",
		b.Table,
		strings.Join(keys, ","),
		strings.Join(qMarks, ","))

	result, err := tx.Exec(query, values...)
	if err != nil {
		return nil, err
	}

	if wrapInSingleTransaction == true {
		err = tx.Commit()
	}

	return result, err
}
开发者ID:truongminh,项目名称:glowing,代码行数:40,代码来源:base.go

示例12: Commit

func (l *Loader) Commit(tx *sqlx.Tx) (err error) {
	if err = tx.Commit(); err != nil {
		return
	}
	return
}
开发者ID:kisom,项目名称:hockeypuck,代码行数:6,代码来源:loader.go

示例13: ApplyAll

func (this *Migrator) ApplyAll() error {
	if len(this.scripts) == 0 {
		return errors.New("Nothing to execute")
	}
	storageExists, err := this.storage.StorageExists()
	if err != nil {
		return err
	}
	if !storageExists {
		err = this.storage.CreateStorage()
		if err != nil {
			return err
		}
	}

	executedMigrationsCount := 0
	for _, script := range this.scripts {
		migrationExists, err := this.storage.MigrationExists(script.GetName())
		if err != nil {
			return err
		}
		if migrationExists {
			this.response.AddMessage("Migration '" + script.GetName() + "' exists. Skipping.")
			continue
		}
		this.response.AddMessage("Executing migration " + script.GetName())
		executionContext := script.GetExecutionContext()
		if executionContext.IsEmpty() {
			this.response.AddMessage("Migration " + script.GetName() + " is empty. Skipping")
			continue
		}
		var tx *sqlx.Tx
		tx, err = this.connection.TransactionStart()
		if err != nil {
			return err
		}
		for _, migration := range executionContext.QueriesToApply {
			res, err := tx.NamedExec(migration.sql, migration.args)
			if err != nil {
				this.response.AddError(errors.New("Query failed. Rollbacking."))
				tx.Rollback()
				return err
			} else {
				rowsAffected, _ := res.RowsAffected()
				lastInserted, _ := res.LastInsertId()
				this.response.AddMessage(fmt.Sprintf("Rows affected %d, last inserted id %d", rowsAffected, lastInserted))
			}
		}
		if executionContext.SuccessCallback != nil {
			err := executionContext.SuccessCallback()
			if err != nil {
				this.response.AddError(errors.New("Callback failed. Rollbacking."))
				tx.Rollback()
				return err
			} else {
				this.response.AddMessage("Callback executed successfully")
			}
		}
		err = this.storage.RegisterNewMigration(script.GetName())
		if err != nil {
			tx.Rollback()
			return err
		} else {
			this.response.AddMessage("New migration with name " + script.GetName() + " is registered")
		}
		tx.Commit()
		executedMigrationsCount++
	}

	this.response.AddMessage(fmt.Sprintf("Successfully executed migrations %d", executedMigrationsCount))
	return nil
}
开发者ID:breathbath,项目名称:golang-webapp,代码行数:72,代码来源:migrator.go


注:本文中的github.com/jmoiron/sqlx.Tx.Commit方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。