示例#1
0
文件: trans.go 项目: pakohan/go-libs
// Allocates a new transaction
func NewTransaction(connection mysql.Conn, query TransactionQuery) (*Transaction, error) {
	if len(query.Columns) < 1 {
		return nil, transactionError{"Too few columns"}
	}

	sss := fmt.Sprintf("INSERT IGNORE INTO `%s`.`%s` (`%s`) VALUES (?%s)", query.Database, query.Table, strings.Join(query.Columns, "`, `"), strings.Repeat(", ?", len(query.Columns)-1))
	ins, err := connection.Prepare(sss)
	if err != nil {
		return nil, err
	}

	trans, err := connection.Begin()
	if err != nil {
		return nil, err
	}

	return &Transaction{trans.Do(ins), trans, len(query.Columns)}, nil
}