本站源代码
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.

77 satır
1.4KB

  1. package testfixtures
  2. import (
  3. "database/sql"
  4. "path/filepath"
  5. )
  6. // SQLite is the SQLite Helper for this package
  7. type SQLite struct {
  8. baseHelper
  9. }
  10. func (*SQLite) paramType() int {
  11. return paramTypeQuestion
  12. }
  13. func (*SQLite) databaseName(q queryable) (string, error) {
  14. var seq int
  15. var main, dbName string
  16. err := q.QueryRow("PRAGMA database_list").Scan(&seq, &main, &dbName)
  17. if err != nil {
  18. return "", err
  19. }
  20. dbName = filepath.Base(dbName)
  21. return dbName, nil
  22. }
  23. func (*SQLite) tableNames(q queryable) ([]string, error) {
  24. query := `
  25. SELECT name
  26. FROM sqlite_master
  27. WHERE type = 'table';
  28. `
  29. rows, err := q.Query(query)
  30. if err != nil {
  31. return nil, err
  32. }
  33. defer rows.Close()
  34. var tables []string
  35. for rows.Next() {
  36. var table string
  37. if err = rows.Scan(&table); err != nil {
  38. return nil, err
  39. }
  40. tables = append(tables, table)
  41. }
  42. if err = rows.Err(); err != nil {
  43. return nil, err
  44. }
  45. return tables, nil
  46. }
  47. func (*SQLite) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
  48. defer func() {
  49. if _, err2 := db.Exec("PRAGMA defer_foreign_keys = OFF"); err2 != nil && err == nil {
  50. err = err2
  51. }
  52. }()
  53. if _, err = db.Exec("PRAGMA defer_foreign_keys = ON"); err != nil {
  54. return err
  55. }
  56. tx, err := db.Begin()
  57. if err != nil {
  58. return err
  59. }
  60. defer tx.Rollback()
  61. if err = loadFn(tx); err != nil {
  62. return err
  63. }
  64. return tx.Commit()
  65. }
上海开阖软件有限公司 沪ICP备12045867号-1