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


Golang Tx.Rollback方法代码示例

本文整理汇总了Golang中github.com/jmoiron/sqlx.Tx.Rollback方法的典型用法代码示例。如果您正苦于以下问题:Golang Tx.Rollback方法的具体用法?Golang Tx.Rollback怎么用?Golang Tx.Rollback使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/jmoiron/sqlx.Tx的用法示例。


在下文中一共展示了Tx.Rollback方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: DeletePerson

func (s *PeopleStore) DeletePerson(id int64) (err error) {
	var tx *sqlx.Tx

	tx, err = s.db.Beginx()
	if err != nil {
		return
	}

	// Automatically rollback/commit if there's an error.
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()

	// Remove the given Person
	if _, err = tx.Exec(s.db.Rebind(personDeleteQuery), id); err != nil {
		return
	}

	// Done!
	return nil
}
开发者ID:jmptrader,项目名称:go-webapp-skeleton,代码行数:25,代码来源:person.go

示例2: DeleteList

func (s *ListStore) DeleteList(id int64) (err error) {
	var tx *sqlx.Tx

	tx, err = s.db.Beginx()
	if err != nil {
		return
	}

	// Automatically rollback/commit if there's an error.
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()

	// Remove todos in this list.
	if _, err = tx.Exec(s.db.Rebind(listDeleteTodosQuery), id); err != nil {
		return
	}

	// Remove the given List
	if _, err = tx.Exec(s.db.Rebind(listDeleteQuery), id); err != nil {
		return
	}

	// Done!
	return nil
}
开发者ID:andrew-d,项目名称:todo,代码行数:30,代码来源:list.go

示例3: getWmLayers

func getWmLayers(params *LayersParams, viewUnpublished bool, tx *sqlx.Tx) (layers []*model.LayerFullInfos, err error) {

	layers = []*model.LayerFullInfos{}

	q := "SELECT m.id, m.type, m.start_date, m.end_date, m.min_scale, m.max_scale, ST_AsGeoJSON(m.geographical_extent_geom) as geographical_extent_geom, m.published, m.created_at, m.creator_user_id, u.firstname || ' ' || u.lastname as author FROM map_layer m LEFT JOIN \"user\" u ON m.creator_user_id = u.id WHERE m.id > 0"

	if params.Author > 0 {
		q += " AND u.id = :author"
	}

	if params.Published || !viewUnpublished {
		q += " AND m.published = 't'"
	}

	if params.Type != "" {
		q += " AND m.type= :type"
	}

	if params.Bounding_box != "" {
		q += " AND (ST_Contains(ST_GeomFromGeoJSON(:bounding_box), m.geographical_extent_geom::::geometry) OR ST_Contains(m.geographical_extent_geom::::geometry, ST_GeomFromGeoJSON(:bounding_box)) OR ST_Overlaps(ST_GeomFromGeoJSON(:bounding_box), m.geographical_extent_geom::::geometry))"
	}

	if params.Check_dates {
		q += " AND m.start_date >= :start_date AND m.end_date <= :end_date"
	}

	in := model.IntJoin(params.Ids, false)

	if in != "" {
		q += " AND m.id IN (" + in + ")"
	}

	nstmt, err := tx.PrepareNamed(q)
	if err != nil {
		log.Println(err)
		_ = tx.Rollback()
		return
	}
	err = nstmt.Select(&layers, params)

	for _, layer := range layers {

		tr := []model.Map_layer_tr{}
		err = tx.Select(&tr, "SELECT * FROM map_layer_tr WHERE map_layer_id = "+strconv.Itoa(layer.Id))
		if err != nil {
			log.Println(err)
			_ = tx.Rollback()
			return
		}
		layer.Uniq_code = layer.Type + strconv.Itoa(layer.Id)
		layer.Name = model.MapSqlTranslations(tr, "Lang_isocode", "Name")
		layer.Attribution = model.MapSqlTranslations(tr, "Lang_isocode", "Attribution")
		layer.Copyright = model.MapSqlTranslations(tr, "Lang_isocode", "Copyright")
		layer.Description = model.MapSqlTranslations(tr, "Lang_isocode", "Description")
	}

	return
}
开发者ID:croll,项目名称:arkeogis-server,代码行数:58,代码来源:layers.go

示例4: transaction

// Transaction manages a db transaction, automatically calling commit
// on success or rollback on failure.
func transaction(fn func(*sqlx.Tx) error) (err error) {
	var tx *sqlx.Tx
	tx, err = db.Beginx()
	if err != nil {
		return err
	}
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			err = tx.Commit()
		}
	}()
	return fn(tx)
}
开发者ID:stepheninabox,项目名称:fowtcg,代码行数:17,代码来源:tidb.go

示例5: DeleteTodo

func (s *TodoStore) DeleteTodo(id int64) (err error) {
	var tx *sqlx.Tx

	tx, err = s.db.Beginx()
	if err != nil {
		return
	}

	// Automatically rollback/commit if there's an error.
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()

	// Get the given Todo
	todo := &model.Todo{}
	if err = tx.Get(todo, s.db.Rebind(todoGetQuery), id); err != nil {
		return
	}

	// Remove the given Todo
	if _, err = tx.Exec(s.db.Rebind(todoDeleteQuery), id); err != nil {
		return
	}

	// Fix any previous link - change any `previous_id` that points at our
	// current id to point at what we point at.
	if _, err = tx.Exec(s.db.Rebind(todoRelinkQuery),
		todo.PreviousID, // New previous_id
		id,              // Old previous_id
		todo.ListID,     // List ID
	); err != nil {
		return
	}

	// Done!
	return nil
}
开发者ID:andrew-d,项目名称:todo,代码行数:41,代码来源:todo.go

示例6: Rollback

func (l *Loader) Rollback(tx *sqlx.Tx) (err error) {
	err = tx.Rollback()
	return
}
开发者ID:kisom,项目名称:hockeypuck,代码行数:4,代码来源:loader.go

示例7: characsGetTree

func characsGetTree(w http.ResponseWriter, tx *sqlx.Tx, id int, project_id int, user model.User) (answer *CharacsUpdateStruct, err error) {

	// answer structure that will be printed when everything is done
	answer = &CharacsUpdateStruct{}

	// get the charac_root row
	answer.Charac_root.Root_charac_id = id
	err = answer.Charac_root.Get(tx)
	if err != nil {
		userSqlError(w, err)
		_ = tx.Rollback()
		return nil, err
	}

	// get the charac (root)
	answer.Charac.Id = id
	err = answer.Charac.Get(tx)
	if err != nil {
		userSqlError(w, err)
		_ = tx.Rollback()
		return nil, err
	}

	// now get the charac translations and all childrens
	err = getCharacRecursive(tx, &answer.CharacTreeStruct, project_id)
	if err != nil {
		userSqlError(w, err)
		_ = tx.Rollback()
		return nil, err
	}

	// get users of the charac group
	group := model.Group{
		Id: answer.Charac_root.Admin_group_id,
	}
	err = group.Get(tx)
	if err != nil {
		userSqlError(w, err)
		_ = tx.Rollback()
		return nil, err
	}
	answer.UsersInGroup, err = group.GetUsers(tx)
	if err != nil {
		userSqlError(w, err)
		_ = tx.Rollback()
		return nil, err
	}

	for i := range answer.UsersInGroup {
		answer.UsersInGroup[i].Password = ""
	}

	// get the author user
	/*	answer.Author.Id = answer.Charac_root.Author_user_id
		err = answer.Author.Get(tx)
		answer.Author.Password = ""
		if err != nil {
			userSqlError(w, err)
			_ = tx.Rollback()
			return nil, err
		}*/ // no author in characs vs chrono

	return answer, nil
}
开发者ID:croll,项目名称:arkeogis-server,代码行数:64,代码来源:characs.go

示例8: loginAnswer

func loginAnswer(w http.ResponseWriter, tx *sqlx.Tx, user model.User, token string) (LoginAnswer, error) {
	// get langs
	lang1 := model.Lang{
		Isocode: user.First_lang_isocode,
	}
	lang2 := model.Lang{
		Isocode: user.Second_lang_isocode,
	}

	err := lang1.Get(tx)
	if err != nil {
		lang1.Isocode = "en"
		err = lang1.Get(tx)
		if err != nil {
			userSqlError(w, err)
			return LoginAnswer{}, err
		}
	}

	err = lang2.Get(tx)
	if err != nil {
		lang2.Isocode = "fr"
		err = lang2.Get(tx)
		if err != nil {
			tx.Rollback()
			log.Fatal("can't load lang2 !")
			return LoginAnswer{}, err
		}
	}

	log.Println("langs: ", lang1, lang2)

	permissions, err := user.GetPermissions(tx)
	if err != nil {
		log.Fatal("can't get permissions!")
		return LoginAnswer{}, err
	}
	log.Println("permissions : ", permissions)

	projectID, err := user.GetProjectId(tx)

	if err != nil {
		log.Fatal("can't get project!")
		return LoginAnswer{}, err
	}

	project := model.ProjectFullInfos{}
	if projectID > 0 {
		project.Id = projectID
		err = project.Get(tx)
		if err != nil {
			log.Fatal("can't get project informations!")
			return LoginAnswer{}, err
		}
	} else {
		project.Id = 0
	}

	log.Println("project: ", project)

	a := LoginAnswer{
		User:        user,
		Token:       token,
		Lang1:       lang1,
		Lang2:       lang2,
		Project:     project,
		Permissions: permissions,
	}

	return a, nil
}
开发者ID:croll,项目名称:arkeogis-server,代码行数:71,代码来源:users.go

示例9: ApplyAll

func (this *Migrator) ApplyAll() error {
	if len(this.scripts) == 0 {
		return errors.New("Nothing to execute")
	}
	storageExists, err := this.storage.StorageExists()
	if err != nil {
		return err
	}
	if !storageExists {
		err = this.storage.CreateStorage()
		if err != nil {
			return err
		}
	}

	executedMigrationsCount := 0
	for _, script := range this.scripts {
		migrationExists, err := this.storage.MigrationExists(script.GetName())
		if err != nil {
			return err
		}
		if migrationExists {
			this.response.AddMessage("Migration '" + script.GetName() + "' exists. Skipping.")
			continue
		}
		this.response.AddMessage("Executing migration " + script.GetName())
		executionContext := script.GetExecutionContext()
		if executionContext.IsEmpty() {
			this.response.AddMessage("Migration " + script.GetName() + " is empty. Skipping")
			continue
		}
		var tx *sqlx.Tx
		tx, err = this.connection.TransactionStart()
		if err != nil {
			return err
		}
		for _, migration := range executionContext.QueriesToApply {
			res, err := tx.NamedExec(migration.sql, migration.args)
			if err != nil {
				this.response.AddError(errors.New("Query failed. Rollbacking."))
				tx.Rollback()
				return err
			} else {
				rowsAffected, _ := res.RowsAffected()
				lastInserted, _ := res.LastInsertId()
				this.response.AddMessage(fmt.Sprintf("Rows affected %d, last inserted id %d", rowsAffected, lastInserted))
			}
		}
		if executionContext.SuccessCallback != nil {
			err := executionContext.SuccessCallback()
			if err != nil {
				this.response.AddError(errors.New("Callback failed. Rollbacking."))
				tx.Rollback()
				return err
			} else {
				this.response.AddMessage("Callback executed successfully")
			}
		}
		err = this.storage.RegisterNewMigration(script.GetName())
		if err != nil {
			tx.Rollback()
			return err
		} else {
			this.response.AddMessage("New migration with name " + script.GetName() + " is registered")
		}
		tx.Commit()
		executedMigrationsCount++
	}

	this.response.AddMessage(fmt.Sprintf("Successfully executed migrations %d", executedMigrationsCount))
	return nil
}
开发者ID:breathbath,项目名称:golang-webapp,代码行数:72,代码来源:migrator.go


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