Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package example
- import(
- "database/sql"
- "errors"
- "github.com/freerware/work"
- u "github.com/gofrs/uuid"
- )
- // AccountDataMapper represents the mapper between our internal
- // user account representations and the database representation.
- // This type satisfies the work.SQLDataMapper interface.
- type AccountDataMapper struct {}
- // toAccount converts []interface{} to []Account.
- func (a AccountDataMapper) toAccount(accounts ...interface{}) ([]Account, error) {
- accs := []Account{}
- for _, account := range accounts {
- var acc Account
- var ok bool
- if acc, ok = account.(Account); !ok {
- return []Account{}, errors.New("invalid type")
- }
- accs = append(accs, acc)
- }
- return accs, nil
- }
- // Insert inserts the provided user accounts using the provided SQL transaction.
- func (a AccountDataMapper) Insert(tx *sql.Tx, accounts ...interface{}) error {
- accs, err := a.toAccount(accounts...)
- if err != nil {
- return err
- }
- return a.insert(tx, accs...)
- }
- // insert creates and executes the prepared statement to insert the provided accounts.
- func (a AccountDataMapper) insert(tx *sql.Tx, accounts ...Account) error {
- sql, args := a.insertSQL(accounts...)
- statement, err := tx.Prepare(sql)
- if err != nil {
- return err
- }
- return statement.Exec(args...)
- }
- // insertSQL constructs the SQL and arguments needed to insert the provided accounts.
- func (a AccountDataMapper) insertSQL(accounts ...Account) (sql string, args []interface{}) {
- sql = "INSERT INTO ACCOUNT (UUID, NAME, EMAIL) VALUES "
- var vals []string
- for _, account := range accounts {
- vals = append(vals, "(?, ?, ?)")
- args = append(account.UUID().String(), account.Name(), account.Email())
- }
- sql = sql + strings.Join(vals, ", ") + ";"
- return
- }
- // Update updates the provided user accounts using the provided SQL transaction.
- func (a AccountDataMapper) Update(tx *sql.Tx, accounts ...interface{}) error {
- accs, err := a.toAccount(accounts...)
- if err != nil {
- return err
- }
- return a.update(tx, accs...)
- }
- // update creates and executes the prepared statements to update the provided accounts.
- func (a AccountDataMapper) update(tx *sql.Tx, accounts ...Account) error {
- sql, args := a.updateSQL(accounts...)
- for idx, s := range sql {
- statement, err := tx.Prepare(s)
- if err != nil {
- return err
- }
- if err = statement.Exec(args[idx]...); err != nil {
- return err
- }
- }
- return nil
- }
- // updateSQL constructs the SQL and arguments needed to update the provided accounts.
- func (a AccountDataMapper) updateSQL(accounts ...Account) (sql []string, args [][]interface{}) {
- for _, account := range accounts {
- sql = append(sql, "UPDATE ACCOUNT SET NAME = ?, EMAIL = ? WHERE UUID = ?;")
- sArgs := []interface{}{account.Name(), account.Email(), account.UUID().String()}
- args = append(args, sArgs)
- }
- return
- }
- // Delete removes the provided user accounts using the provided SQL transaction.
- func (a AccountDataMapper) Delete(tx *sql.Tx, accounts ...interface{}) error {
- accs, err := a.toAccount(accounts...)
- if err != nil {
- return err
- }
- return a.delete(tx, accs...)
- }
- // delete creates and executes the prepared statement to delete the provided accounts.
- func (a AccountDataMapper) delete(tx *sql.Tx, accounts ...Account) error {
- sql, args := a.deleteSQL(accounts...)
- statement, err := tx.Prepare(sql)
- if err != nil {
- return err
- }
- return statement.Exec(args...)
- }
- // deleteSQL constructs the SQL and arguments needed to delete the provided accounts.
- func (a AccountDataMapper) deleteSQL(accounts ...Account) (sql string, args []interface{}) {
- sql = "DELETE FROM ACCOUNT WHERE UUID IN ("
- var vals []string
- for _, account := range accounts {
- vals = append(vals, "?")
- args = append(account.UUID())
- }
- sql = sql + strings.Join(vals, ", ") + ");"
- return
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement