本站源代码
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

74 lignes
1.7KB

  1. package testfixtures
  2. import (
  3. "database/sql"
  4. "fmt"
  5. )
  6. const (
  7. paramTypeDollar = iota + 1
  8. paramTypeQuestion
  9. paramTypeColon
  10. )
  11. type loadFunction func(tx *sql.Tx) error
  12. // Helper is the generic interface for the database helper
  13. type Helper interface {
  14. init(*sql.DB) error
  15. disableReferentialIntegrity(*sql.DB, loadFunction) error
  16. paramType() int
  17. databaseName(queryable) (string, error)
  18. tableNames(queryable) ([]string, error)
  19. isTableModified(queryable, string) (bool, error)
  20. afterLoad(queryable) error
  21. quoteKeyword(string) string
  22. whileInsertOnTable(*sql.Tx, string, func() error) error
  23. }
  24. type queryable interface {
  25. Exec(string, ...interface{}) (sql.Result, error)
  26. Query(string, ...interface{}) (*sql.Rows, error)
  27. QueryRow(string, ...interface{}) *sql.Row
  28. }
  29. // batchSplitter is an interface with method which returns byte slice for
  30. // splitting SQL batches. This need to split sql statements and run its
  31. // separately.
  32. //
  33. // For Microsoft SQL Server batch splitter is "GO". For details see
  34. // https://docs.microsoft.com/en-us/sql/t-sql/language-elements/sql-server-utilities-statements-go
  35. type batchSplitter interface {
  36. splitter() []byte
  37. }
  38. var (
  39. _ Helper = &MySQL{}
  40. _ Helper = &PostgreSQL{}
  41. _ Helper = &SQLite{}
  42. _ Helper = &Oracle{}
  43. _ Helper = &SQLServer{}
  44. )
  45. type baseHelper struct{}
  46. func (baseHelper) init(_ *sql.DB) error {
  47. return nil
  48. }
  49. func (baseHelper) quoteKeyword(str string) string {
  50. return fmt.Sprintf(`"%s"`, str)
  51. }
  52. func (baseHelper) whileInsertOnTable(_ *sql.Tx, _ string, fn func() error) error {
  53. return fn()
  54. }
  55. func (baseHelper) isTableModified(_ queryable, _ string) (bool, error) {
  56. return true, nil
  57. }
  58. func (baseHelper) afterLoad(_ queryable) error {
  59. return nil
  60. }
上海开阖软件有限公司 沪ICP备12045867号-1