예제 #1
0
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
}
예제 #2
0
파일: list.go 프로젝트: andrew-d/todo
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
}
예제 #3
0
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
}
예제 #4
0
// 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)
}
예제 #5
0
파일: todo.go 프로젝트: andrew-d/todo
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
}
예제 #6
0
파일: loader.go 프로젝트: kisom/hockeypuck
func (l *Loader) Rollback(tx *sqlx.Tx) (err error) {
	err = tx.Rollback()
	return
}
예제 #7
0
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
}
예제 #8
0
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
}
예제 #9
0
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
}