Golang DB.Begin Examples

Golang DB.Begin - 30 examples found. These are the top rated real world Golang examples of database/sql.DB.Begin extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func RegisterData(params martini.Params, l *log.Logger, db *sql.DB) (int, string) {
	key := params["key"]
	value := params["value"]
	l.Println(key, value)

	tx, err := db.Begin()
	if err != nil {
		log.Fatal(err)
	}
	stmt, err := tx.Prepare("insert into foo(id, name) values(?, ?)")
	if err != nil {
		log.Fatal(err)
	}
	defer stmt.Close()

	for i := 0; i < 10; i++ {
		_, err = stmt.Exec(i, fmt.Sprintf("[%s = %s] ねぷねぷのぷりん%03d", key, value, i))
		if err != nil {
			log.Fatal(err)
		}
	}
	tx.Commit()

	return 200, "Hello!" + value
}
Example #2
0
// LoadFixtures loads test fixtures in a transaction
// if the transaction fails, no fatal error is triggered
func LoadFixtures(db *sql.DB, driver string) *sql.DB {

	migrationFile, err := os.Open("./testdata/fixtures/" + driver + "/fixtures.sql")
	if err != nil {
		log.Fatal(err)
	}
	buffer := new(bytes.Buffer)
	_, err = buffer.ReadFrom(migrationFile)
	if err != nil {
		log.Fatal(err)
	}
	tx, err := db.Begin()
	if err != nil {
		log.Fatal(err)
	}
	_, err = tx.Exec(buffer.String())
	if err != nil {
		log.Printf("Error loading fixtures, %s", err)
		tx.Rollback()
	} else {
		err = tx.Commit()
		if err != nil {
			log.Fatal(err)
		}
	}
	return db
}
Example #3
0
// Run a migration specified in raw SQL.
//
// Sections of the script can be annotated with a special comment,
// starting with "-- +goose" to specify whether the section should
// be applied during an Up or Down migration
//
// All statements following an Up or Down directive are grouped together
// until another direction directive is found.
func runSQLMigration(conf *DBConf, db *sql.DB, scriptFile string, v int64, direction bool) error {

	txn, err := db.Begin()
	if err != nil {
		log.Fatal("db.Begin:", err)
	}

	f, err := os.Open(scriptFile)
	if err != nil {
		log.Fatal(err)
	}

	// find each statement, checking annotations for up/down direction
	// and execute each of them in the current transaction.
	// Commits the transaction if successfully applied each statement and
	// records the version into the version table or returns an error and
	// rolls back the transaction.
	for _, query := range splitSQLStatements(f, direction) {
		if _, err = txn.Exec(query); err != nil {
			txn.Rollback()
			log.Fatalf("FAIL %s (%v), quitting migration.", filepath.Base(scriptFile), err)
			return err
		}
	}

	if err = FinalizeMigration(conf, txn, direction, v); err != nil {
		log.Fatalf("error finalizing migration %s, quitting. (%v)", filepath.Base(scriptFile), err)
	}

	return nil
}
Example #4
0
// Run a migration specified in raw SQL.
//
// Sections of the script can be annotated with a special comment,
// starting with "-- +goose" to specify whether the section should
// be applied during an Up or Down migration
//
// All statements following an Up or Down directive are grouped together
// until another direction directive is found.
func runSQLMigration(conf *DBConf, db *sql.DB, script string, v int64, direction bool) error {

	txn, err := db.Begin()
	if err != nil {
		log.Fatal("db.Begin:", err)
	}

	f, err := os.Open(script)
	if err != nil {
		log.Fatal(err)
	}

	// find each statement, checking annotations for up/down direction
	// and execute each of them in the current transaction
	for _, query := range splitSQLStatements(f, direction) {
		if _, err = txn.Exec(query); err != nil {
			txn.Rollback()
			log.Fatalf("FAIL %s (%v), quitting migration.", filepath.Base(script), err)
			return err
		}
	}

	if err = finalizeMigration(conf, txn, direction, v); err != nil {
		log.Fatalf("error finalizing migration %s, quitting. (%v)", filepath.Base(script), err)
	}

	return nil
}
Example #5
0
//In a transaction block of executive function
func Transaction(db *sql.DB, f func() error) error {

	tx, err := db.Begin()
	if err != nil {
		//panic(err)
		return err
	}

	//如果f()函数是通过panic抛出错误,那也将此错误使用panic抛出
	defer func() {
		if r := recover(); r != nil {
			err := tx.Rollback()
			if err != nil {
				panic(err)
				//return err
			}
			panic(r)
			//return errors.New(fmt.Sprint(r))
		} else {
			err = tx.Commit()
			if err != nil {
				//panic(err)
				//return err
				log.Fatal(err)
			}
		}
	}()

	err = f()
	if err != nil {
		return tx.Rollback()
	}
	return err
}
// DeleteContact deletes the contact from the DB.
func (c *Contact) DeleteContact(db *sql.DB) error {
	// Do in a transaction because we have to first delete the contact on the sites.
	tx, err := db.Begin()
	if err != nil {
		return err
	}
	_, err = db.Exec(
		`DELETE FROM SiteContacts WHERE ContactID = $1`,
		c.ContactID,
	)
	if err != nil {
		tx.Rollback()
		return err
	}

	_, err = db.Exec(
		`DELETE FROM Contacts WHERE ContactID = $1;`,
		c.ContactID,
	)
	if err != nil {
		tx.Rollback()
		return err
	}

	tx.Commit()
	return nil
}