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.

helper.go 1.6KB

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