本文整理匯總了Golang中github.com/jmoiron/sqlx.Tx.Preparex方法的典型用法代碼示例。如果您正苦於以下問題:Golang Tx.Preparex方法的具體用法?Golang Tx.Preparex怎麽用?Golang Tx.Preparex使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/jmoiron/sqlx.Tx
的用法示例。
在下文中一共展示了Tx.Preparex方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: CreateWithId
func (h Helper) CreateWithId(tx *sqlx.Tx, name string, args ...interface{}) (int64, error) {
var id int64
sql := h.SQL(name)
if sql == "" {
panic("No statement registered under " + name)
}
stmt, err := tx.Preparex(sql)
if err != nil {
return 0, err
}
defer stmt.Close()
res, err := stmt.Exec(args...)
if err != nil {
return 0, err
}
id, err = res.LastInsertId()
if err != nil {
return 0, err
}
return id, nil
}
示例2: UpdateAccountEmailConfirmed
func (db Database) UpdateAccountEmailConfirmed(tx *sqlx.Tx, account Account) error {
stmt, err := tx.Preparex("UPDATE account SET email_confirmed = TRUE where id = $1")
if err != nil {
return err
}
_, err = stmt.Exec(account.Id)
return err
}
示例3: DeleteEmailConfirmation
func (db Database) DeleteEmailConfirmation(tx *sqlx.Tx, account Account) error {
stmt, err := tx.Preparex("DELETE FROM email_confirmation WHERE account_id=$1")
if err != nil {
return err
}
_, err = stmt.Exec(account.Id)
return err
}
示例4: DeleteAccountSession
func (db Database) DeleteAccountSession(tx *sqlx.Tx, key string) error {
stmt, err := tx.Preparex("DELETE FROM account_session WHERE key=$1")
if err != nil {
return err
}
_, err = stmt.Exec(key)
return err
}
示例5: CreateWithId
func (h Helper) CreateWithId(tx *sqlx.Tx, sql string, args ...interface{}) (int64, error) {
var id int64
sql += " RETURNING id"
stmt, err := tx.Preparex(sql)
if err != nil {
return 0, err
}
defer stmt.Close()
err = stmt.QueryRow(args...).Scan(&id)
if err != nil {
return 0, err
}
return id, nil
}
示例6: updateArticle
func updateArticle(a content.Article, tx *sqlx.Tx, db *db.DB, logger webfw.Logger) {
if a.HasErr() {
return
}
if err := a.Validate(); err != nil {
a.Err(err)
return
}
logger.Infof("Updating article %s\n", a)
d := a.Data()
s := db.SQL()
stmt, err := tx.Preparex(s.Article.Update)
if err != nil {
a.Err(err)
return
}
defer stmt.Close()
res, err := stmt.Exec(d.Title, d.Description, d.Date, d.Guid, d.Link, d.FeedId)
if err != nil {
a.Err(err)
return
}
if num, err := res.RowsAffected(); err != nil && err == sql.ErrNoRows || num == 0 {
logger.Infof("Creating article %s\n", a)
aId, err := db.CreateWithId(tx, s.Article.Create, d.FeedId, d.Link, d.Guid,
d.Title, d.Description, d.Date)
if err != nil {
a.Err(fmt.Errorf("Error updating article %s (guid - %v, link - %s): %v", a, d.Guid, d.Link, err))
return
}
d.Id = data.ArticleId(aId)
d.IsNew = true
a.Data(d)
}
}
示例7: updateFeedArticles
func (db DB) updateFeedArticles(tx *sqlx.Tx, f Feed, articles []Article) (bool, error) {
if len(articles) == 0 {
return false, nil
}
Debug.Println("Updating feed articles for " + f.Link)
newArticles := []Article{}
for _, a := range articles {
if err := a.Validate(); err != nil {
return false, err
}
stmt, err := tx.Preparex(db.NamedSQL("update_feed_article"))
if err != nil {
return false, err
}
defer stmt.Close()
res, err := stmt.Exec(a.Title, a.Description, a.Link, a.Date, a.Id, f.Id)
if err != nil {
return false, err
}
if num, err := res.RowsAffected(); err != nil || num == 0 {
newArticles = append(newArticles, a)
}
}
if len(newArticles) == 0 {
return false, nil
}
sql := `INSERT INTO articles(id, feed_id, title, description, link, date) `
args := []interface{}{}
index := 1
for i, a := range newArticles {
if err := a.Validate(); err != nil {
return false, err
}
if i != 0 {
sql += ` UNION `
}
sql += fmt.Sprintf(`SELECT $%d, $%d, $%d, $%d ,$%d, $%d EXCEPT SELECT id, feed_id, title, description, link, date FROM articles WHERE id = $%d AND feed_id = $%d`, index, index+1, index+2, index+3, index+4, index+5, index, index+1)
args = append(args, a.Id, f.Id, a.Title, a.Description, a.Link, a.Date)
index = len(args) + 1
}
stmt, err := tx.Preparex(sql)
if err != nil {
return false, err
}
defer stmt.Close()
_, err = stmt.Exec(args...)
if err != nil {
return false, err
}
return true, nil
}