You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
784 B

  1. package testfixtures
  2. import (
  3. "database/sql"
  4. "fmt"
  5. )
  6. // MySQL is the MySQL helper for this package
  7. type MySQL struct {
  8. baseHelper
  9. }
  10. func (*MySQL) paramType() int {
  11. return paramTypeQuestion
  12. }
  13. func (*MySQL) quoteKeyword(str string) string {
  14. return fmt.Sprintf("`%s`", str)
  15. }
  16. func (*MySQL) databaseName(db *sql.DB) (dbName string) {
  17. db.QueryRow("SELECT DATABASE()").Scan(&dbName)
  18. return
  19. }
  20. func (h *MySQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) error {
  21. // re-enable after load
  22. defer db.Exec("SET FOREIGN_KEY_CHECKS = 1")
  23. tx, err := db.Begin()
  24. if err != nil {
  25. return err
  26. }
  27. if _, err = tx.Exec("SET FOREIGN_KEY_CHECKS = 0"); err != nil {
  28. return err
  29. }
  30. if err = loadFn(tx); err != nil {
  31. tx.Rollback()
  32. return err
  33. }
  34. return tx.Commit()
  35. }