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


Golang DB.MustBegin方法代码示例

本文整理汇总了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)
}
开发者ID:rogeriomarques,项目名称:oapx,代码行数:33,代码来源:get_profile.go

示例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)
}
开发者ID:rogeriomarques,项目名称:oapx,代码行数:32,代码来源:post_application.go

示例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()
}
开发者ID:danmondy,项目名称:everydaybham,代码行数:7,代码来源:repo.go

示例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)
	}

}
开发者ID:robertojrojas,项目名称:go-database-frameworks-and-tools,代码行数:30,代码来源:main.go

示例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
}
开发者ID:cyarie,项目名称:tinyplannr-api-v2,代码行数:14,代码来源:authUser.go

示例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)
}
开发者ID:rogeriomarques,项目名称:oapx,代码行数:45,代码来源:get_me.go

示例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
}
开发者ID:cyarie,项目名称:tinyplannr-api-v2,代码行数:21,代码来源:apiUser.go

示例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
}
开发者ID:cyarie,项目名称:tinyplannr-api-v2,代码行数:24,代码来源:authUser.go

示例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()
}
开发者ID:freycool,项目名称:tesnunuardi,代码行数:6,代码来源:test.go

示例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
}
开发者ID:rogeriomarques,项目名称:oapx,代码行数:69,代码来源:flow.go


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