Reuse statement on post insert
This commit is contained in:
parent
8a3974c27d
commit
efbba9e1ba
10
database.go
10
database.go
|
@ -557,15 +557,19 @@ func (db *datastore) CreatePost(userID, collID int64, post *SubmittedPost) (*Pos
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = db.Exec("INSERT INTO posts (id, slug, title, content, text_appearance, language, rtl, owner_id, collection_id, updated, view_count) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?)", friendlyID, slug, post.Title, post.Content, appearance, post.Language, post.IsRTL, ownerID, ownerCollID, 0)
|
stmt, err := db.Prepare("INSERT INTO posts (id, slug, title, content, text_appearance, language, rtl, owner_id, collection_id, updated, view_count) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?)")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer stmt.Close()
|
||||||
|
_, err = stmt.Exec(friendlyID, slug, post.Title, post.Content, appearance, post.Language, post.IsRTL, ownerID, ownerCollID, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if mysqlErr, ok := err.(*mysql.MySQLError); ok {
|
if mysqlErr, ok := err.(*mysql.MySQLError); ok {
|
||||||
if mysqlErr.Number == mySQLErrDuplicateKey {
|
if mysqlErr.Number == mySQLErrDuplicateKey {
|
||||||
// Duplicate entry error; try a new slug
|
// Duplicate entry error; try a new slug
|
||||||
// TODO: make this a little more robust
|
// TODO: make this a little more robust
|
||||||
// TODO: reuse exact same db.Exec statement as above
|
|
||||||
slug = sql.NullString{id.GenSafeUniqueSlug(slug.String), true}
|
slug = sql.NullString{id.GenSafeUniqueSlug(slug.String), true}
|
||||||
_, err = db.Exec("INSERT INTO posts (id, slug, title, content, text_appearance, language, rtl, owner_id, collection_id, updated, view_count) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?)", friendlyID, slug, post.Title, post.Content, appearance, post.Language, post.IsRTL, ownerID, ownerCollID, 0)
|
_, err = stmt.Exec(friendlyID, slug, post.Title, post.Content, appearance, post.Language, post.IsRTL, ownerID, ownerCollID, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, handleFailedPostInsert(fmt.Errorf("Retried slug generation, still failed: %v", err))
|
return nil, handleFailedPostInsert(fmt.Errorf("Retried slug generation, still failed: %v", err))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue