本文整理汇总了Golang中github.com/jmoiron/sqlx.DB.MustBegin方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.MustBegin方法的具体用法?Golang DB.MustBegin怎么用?Golang DB.MustBegin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jmoiron/sqlx.DB
的用法示例。
在下文中一共展示了DB.MustBegin方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GET_profile
func GET_profile(rw http.ResponseWriter, r *http.Request, identity *data.Identity,
render render.Render, db *sqlx.DB) {
var (
tx = db.MustBegin()
success bool
page struct {
Accounts []*data.Account
OwnedApplications []*data.Application
}
)
defer func() {
if success {
tx.Commit()
} else {
tx.Rollback()
}
}()
accounts, err := data.GetAccountsWithIdentityId(tx, identity.Id)
if err != nil {
panic(err)
}
owned_applications, err := data.GetApplicationsOwnedByIdentity(tx, identity.Id)
if err != nil {
panic(err)
}
page.Accounts = accounts
page.OwnedApplications = owned_applications
render.HTML(200, "profile", &page)
}
示例2: POST_application
func POST_application(rw http.ResponseWriter, r *http.Request, identity *data.Identity,
render render.Render, db *sqlx.DB) {
var (
tx = db.MustBegin()
success bool
)
defer func() {
if success {
tx.Commit()
} else {
tx.Rollback()
}
}()
err := r.ParseForm()
if err != nil {
panic(err)
}
err = data.CreateApplication(tx, &data.Application{
OwnerId: identity.Id,
Name: r.PostForm.Get("application[name]"),
RedirectURI: r.PostForm.Get("application[redirect_uri]"),
})
if err != nil {
panic(err)
}
success = true
http.Redirect(rw, r, "/", http.StatusFound)
}
示例3: initDB
func initDB(db *sqlx.DB) {
db.MustExec(schema)
tx := db.MustBegin()
tx.MustExec("INSERT into event (date, title, description, font) values ($1, $2, $3, $4)", timeToString(time.Now()), "Do something fun!", "description goes here", "Helvetica")
tx.MustExec("INSERT into event (date, title, description, font) values ($1, $2, $3, $4)", timeToString(time.Now().Add(time.Hour*24)), "Do something Else!", "discription goes here", "Helvetica")
tx.Commit()
}
示例4: queryWhereToStructTX
func queryWhereToStructTX(db *sqlx.DB) {
log.Println("Query with Where Map to Struct TX...")
log.Println("====================================")
tx := db.MustBegin()
now := time.Now()
t := Todo{
Subject: sql.NullString{String: "Mow Lawn"},
Description: sql.NullString{String: "Yuck!"},
CreatedAt: now,
UpdatedAt: now,
}
tx.Exec("Insert into todos(subject, description, created_at, updated_at) values ($1, $2, $3, $4)", t.Subject, t.Description, t.CreatedAt, t.UpdatedAt)
tx.Commit()
todos := []Todo{}
err := db.Select(&todos, "select * from todos")
if err != nil {
log.Fatal(err)
}
for _, todo := range todos {
log.Printf("Subject is %s\n", todo.CreatedAt)
}
}
示例5: DeleteUserAuth
func DeleteUserAuth(db *sqlx.DB, email string) error {
var err error
tx := db.MustBegin()
tx.MustExec(`DELETE FROM tinyplannr_auth.user WHERE email = $1`, email)
err = tx.Commit()
if err != nil {
log.Printf("Error removing user from auth table: %v", err)
return err
}
return err
}
示例6: GET_me
func GET_me(identity *data.Identity, render render.Render, db *sqlx.DB) {
type account struct {
Id string `json:"id"`
Name string `json:"name"`
Email string `json:"email,omitempty"`
Picture string `json:"picture,omitempty"`
}
var (
tx = db.MustBegin()
success bool
err error
resp struct {
Id string `json:"id"`
Accounts []*account `json:"accounts"`
}
)
defer func() {
if success {
tx.Commit()
} else {
tx.Rollback()
}
}()
resp.Id = strconv.FormatInt(identity.Id, 10)
accounts, err := data.GetAccountsWithIdentityId(tx, identity.Id)
if err != nil {
panic(err)
}
for _, acc := range accounts {
resp.Accounts = append(resp.Accounts, &account{
Id: acc.RemoteId,
Name: acc.Name,
Email: acc.Email,
Picture: acc.Picture,
})
}
success = true
render.JSON(200, &resp)
}
示例7: DeleteUser
func DeleteUser(db *sqlx.DB, email string) error {
var err error
// We have to drop the UserAuth entry first to avoid breaking foreign key constraints
err = DeleteUserAuth(db, email)
if err != nil {
log.Printf("Encountered an error removing the UserAuth entry: %v", err)
}
tx := db.MustBegin()
tx.MustExec(`DELETE FROM tinyplannr_api.user WHERE email = $1`, email)
err = tx.Commit()
if err != nil {
log.Println(err)
return err
}
return err
}
示例8: CreateUserAuth
func CreateUserAuth(db *sqlx.DB, au ApiUserCreate) error {
var err error
// Let's use Bcrypt to generate a password hash
pw := []byte(au.Password)
hashedPassword, err := bcrypt.GenerateFromPassword(pw, 13)
if err != nil {
return err
}
// Rolling a transaction here. We don't need to return anything.
tx := db.MustBegin()
tx.MustExec(`INSERT INTO tinyplannr_auth.user (user_id, email, hash_pw, update_dt, last_login_dt)
VALUES ($1, $2, $3, $4, $5);`, au.UserId, au.Email, string(hashedPassword), time.Now(), time.Now())
err = tx.Commit()
if err != nil {
log.Println(err)
return err
}
return err
}
示例9: NewDevice
func NewDevice(db *sqlx.DB, name string, host string, port string, token string, description string) {
tx := db.MustBegin()
tx.MustExec("INSERT INTO device_list (device_name, host, port, token, description) VALUES (?, ?, ?, ?, ?)", name, host, port, token, description)
tx.Commit()
}
示例10: GET_callback
func GET_callback(c martini.Context, sess sessions.Session, r *http.Request, db *sqlx.DB) {
flow, ok := sess.Get("flow").(FlowState)
if !ok {
c.Invoke(redirect_to("/login"))
return
}
if flow.StartAt.Before(time.Now().Add(-10 * time.Minute)) {
c.Invoke(redirect_to("/login"))
return
}
if flow.State == "" {
c.Invoke(redirect_to("/login"))
return
}
if r.URL.Query().Get("code") == "" {
c.Invoke(redirect_to("/login"))
return
}
if flow.State != r.URL.Query().Get("state") {
c.Invoke(redirect_to("/login"))
return
}
var (
provider = tmp_new_provider(flow.Provider)
transport = provider.Transport(nil)
)
token, err := transport.Exchange(r.URL.Query().Get("code"))
if err != nil {
panic(err)
}
profile, err := provider.GetProfile(transport)
if err != nil {
panic(err)
}
var (
tx = db.MustBegin()
success bool
)
defer func() {
if success {
tx.Commit()
} else {
tx.Rollback()
}
}()
account, err := data.GetAccountWithRemoteId(tx, profile.RemoteId())
if err != nil {
panic(err)
}
c.MapTo(profile, (*providers.Profile)(nil))
c.Map(token)
c.Map(tx)
c.Map(account)
if account != nil {
c.Invoke(GET_callback_A)
} else {
c.Invoke(GET_callback_B)
}
success = true
}