Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. package example
  2.  
  3. import(
  4. "database/sql"
  5. "errors"
  6.  
  7. "github.com/freerware/work"
  8. u "github.com/gofrs/uuid"
  9. )
  10.  
  11. // AccountDataMapper represents the mapper between our internal
  12. // user account representations and the database representation.
  13. // This type satisfies the work.SQLDataMapper interface.
  14. type AccountDataMapper struct {}
  15.  
  16. // toAccount converts []interface{} to []Account.
  17. func (a AccountDataMapper) toAccount(accounts ...interface{}) ([]Account, error) {
  18. accs := []Account{}
  19. for _, account := range accounts {
  20. var acc Account
  21. var ok bool
  22. if acc, ok = account.(Account); !ok {
  23. return []Account{}, errors.New("invalid type")
  24. }
  25. accs = append(accs, acc)
  26. }
  27. return accs, nil
  28. }
  29.  
  30. // Insert inserts the provided user accounts using the provided SQL transaction.
  31. func (a AccountDataMapper) Insert(tx *sql.Tx, accounts ...interface{}) error {
  32. accs, err := a.toAccount(accounts...)
  33. if err != nil {
  34. return err
  35. }
  36. return a.insert(tx, accs...)
  37. }
  38.  
  39. // insert creates and executes the prepared statement to insert the provided accounts.
  40. func (a AccountDataMapper) insert(tx *sql.Tx, accounts ...Account) error {
  41. sql, args := a.insertSQL(accounts...)
  42. statement, err := tx.Prepare(sql)
  43. if err != nil {
  44. return err
  45. }
  46. return statement.Exec(args...)
  47. }
  48.  
  49. // insertSQL constructs the SQL and arguments needed to insert the provided accounts.
  50. func (a AccountDataMapper) insertSQL(accounts ...Account) (sql string, args []interface{}) {
  51. sql = "INSERT INTO ACCOUNT (UUID, NAME, EMAIL) VALUES "
  52. var vals []string
  53. for _, account := range accounts {
  54. vals = append(vals, "(?, ?, ?)")
  55. args = append(account.UUID().String(), account.Name(), account.Email())
  56. }
  57. sql = sql + strings.Join(vals, ", ") + ";"
  58. return
  59. }
  60.  
  61. // Update updates the provided user accounts using the provided SQL transaction.
  62. func (a AccountDataMapper) Update(tx *sql.Tx, accounts ...interface{}) error {
  63. accs, err := a.toAccount(accounts...)
  64. if err != nil {
  65. return err
  66. }
  67. return a.update(tx, accs...)
  68. }
  69.  
  70. // update creates and executes the prepared statements to update the provided accounts.
  71. func (a AccountDataMapper) update(tx *sql.Tx, accounts ...Account) error {
  72. sql, args := a.updateSQL(accounts...)
  73. for idx, s := range sql {
  74. statement, err := tx.Prepare(s)
  75. if err != nil {
  76. return err
  77. }
  78. if err = statement.Exec(args[idx]...); err != nil {
  79. return err
  80. }
  81. }
  82. return nil
  83. }
  84.  
  85. // updateSQL constructs the SQL and arguments needed to update the provided accounts.
  86. func (a AccountDataMapper) updateSQL(accounts ...Account) (sql []string, args [][]interface{}) {
  87. for _, account := range accounts {
  88. sql = append(sql, "UPDATE ACCOUNT SET NAME = ?, EMAIL = ? WHERE UUID = ?;")
  89. sArgs := []interface{}{account.Name(), account.Email(), account.UUID().String()}
  90. args = append(args, sArgs)
  91. }
  92. return
  93. }
  94.  
  95. // Delete removes the provided user accounts using the provided SQL transaction.
  96. func (a AccountDataMapper) Delete(tx *sql.Tx, accounts ...interface{}) error {
  97. accs, err := a.toAccount(accounts...)
  98. if err != nil {
  99. return err
  100. }
  101. return a.delete(tx, accs...)
  102. }
  103.  
  104. // delete creates and executes the prepared statement to delete the provided accounts.
  105. func (a AccountDataMapper) delete(tx *sql.Tx, accounts ...Account) error {
  106. sql, args := a.deleteSQL(accounts...)
  107. statement, err := tx.Prepare(sql)
  108. if err != nil {
  109. return err
  110. }
  111. return statement.Exec(args...)
  112. }
  113.  
  114. // deleteSQL constructs the SQL and arguments needed to delete the provided accounts.
  115. func (a AccountDataMapper) deleteSQL(accounts ...Account) (sql string, args []interface{}) {
  116. sql = "DELETE FROM ACCOUNT WHERE UUID IN ("
  117. var vals []string
  118. for _, account := range accounts {
  119. vals = append(vals, "?")
  120. args = append(account.UUID())
  121. }
  122. sql = sql + strings.Join(vals, ", ") + ");"
  123. return
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement