Skip to content

Commit

Permalink
Merge pull request #801 from gobuffalo/pass-connection-instead-of-store
Browse files Browse the repository at this point in the history
made to pass connection instead of copying store
  • Loading branch information
sio4 authored Dec 16, 2022
2 parents 17f09c0 + fe5ea37 commit 5dfc37d
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 120 deletions.
2 changes: 2 additions & 0 deletions database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ cockroach_ssl:
sqlite:
dialect: "sqlite3"
database: "tmp/test.sqlite"
options:
lock: true

14 changes: 7 additions & 7 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
)

type crudable interface {
SelectOne(store, *Model, Query) error
SelectMany(store, *Model, Query) error
Create(store, *Model, columns.Columns) error
Update(store, *Model, columns.Columns) error
UpdateQuery(store, *Model, columns.Columns, Query) (int64, error)
Destroy(store, *Model) error
Delete(store, *Model, Query) error
SelectOne(*Connection, *Model, Query) error
SelectMany(*Connection, *Model, Query) error
Create(*Connection, *Model, columns.Columns) error
Update(*Connection, *Model, columns.Columns) error
UpdateQuery(*Connection, *Model, columns.Columns, Query) (int64, error)
Destroy(*Connection, *Model) error
Delete(*Connection, *Model, Query) error
}

type fizzable interface {
Expand Down
32 changes: 16 additions & 16 deletions dialect_cockroach.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (p *cockroach) Details() *ConnectionDetails {
return p.ConnectionDetails
}

func (p *cockroach) Create(s store, model *Model, cols columns.Columns) error {
func (p *cockroach) Create(c *Connection, model *Model, cols columns.Columns) error {
keyType, err := model.PrimaryKeyType()
if err != nil {
return err
Expand All @@ -81,8 +81,8 @@ func (p *cockroach) Create(s store, model *Model, cols columns.Columns) error {
} else {
query = fmt.Sprintf("INSERT INTO %s DEFAULT VALUES returning %s", p.Quote(model.TableName()), model.IDField())
}
txlog(logging.SQL, s, query, model.Value)
stmt, err := s.PrepareNamed(query)
txlog(logging.SQL, c, query, model.Value)
stmt, err := c.Store.PrepareNamed(query)
if err != nil {
return err
}
Expand All @@ -100,33 +100,33 @@ func (p *cockroach) Create(s store, model *Model, cols columns.Columns) error {
}
return nil
}
return genericCreate(s, model, cols, p)
return genericCreate(c, model, cols, p)
}

func (p *cockroach) Update(s store, model *Model, cols columns.Columns) error {
return genericUpdate(s, model, cols, p)
func (p *cockroach) Update(c *Connection, model *Model, cols columns.Columns) error {
return genericUpdate(c, model, cols, p)
}

func (p *cockroach) UpdateQuery(s store, model *Model, cols columns.Columns, query Query) (int64, error) {
return genericUpdateQuery(s, model, cols, p, query, sqlx.DOLLAR)
func (p *cockroach) UpdateQuery(c *Connection, model *Model, cols columns.Columns, query Query) (int64, error) {
return genericUpdateQuery(c, model, cols, p, query, sqlx.DOLLAR)
}

func (p *cockroach) Destroy(s store, model *Model) error {
func (p *cockroach) Destroy(c *Connection, model *Model) error {
stmt := p.TranslateSQL(fmt.Sprintf("DELETE FROM %s AS %s WHERE %s", p.Quote(model.TableName()), model.Alias(), model.WhereID()))
_, err := genericExec(s, stmt, model.ID())
_, err := genericExec(c, stmt, model.ID())
return err
}

func (p *cockroach) Delete(s store, model *Model, query Query) error {
return genericDelete(s, model, query)
func (p *cockroach) Delete(c *Connection, model *Model, query Query) error {
return genericDelete(c, model, query)
}

func (p *cockroach) SelectOne(s store, model *Model, query Query) error {
return genericSelectOne(s, model, query)
func (p *cockroach) SelectOne(c *Connection, model *Model, query Query) error {
return genericSelectOne(c, model, query)
}

func (p *cockroach) SelectMany(s store, models *Model, query Query) error {
return genericSelectMany(s, models, query)
func (p *cockroach) SelectMany(c *Connection, models *Model, query Query) error {
return genericSelectMany(c, models, query)
}

func (p *cockroach) CreateDB() error {
Expand Down
42 changes: 21 additions & 21 deletions dialect_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (commonDialect) Quote(key string) string {
return strings.Join(parts, ".")
}

func genericCreate(s store, model *Model, cols columns.Columns, quoter quotable) error {
func genericCreate(c *Connection, model *Model, cols columns.Columns, quoter quotable) error {
keyType, err := model.PrimaryKeyType()
if err != nil {
return err
Expand All @@ -53,8 +53,8 @@ func genericCreate(s store, model *Model, cols columns.Columns, quoter quotable)
cols.Remove(model.IDField())
w := cols.Writeable()
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", quoter.Quote(model.TableName()), w.QuotedString(quoter), w.SymbolizedString())
txlog(logging.SQL, s, query, model.Value)
res, err := s.NamedExec(query, model.Value)
txlog(logging.SQL, c, query, model.Value)
res, err := c.Store.NamedExec(query, model.Value)
if err != nil {
return err
}
Expand All @@ -81,8 +81,8 @@ func genericCreate(s store, model *Model, cols columns.Columns, quoter quotable)
w := cols.Writeable()
w.Add(model.IDField())
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", quoter.Quote(model.TableName()), w.QuotedString(quoter), w.SymbolizedString())
txlog(logging.SQL, s, query, model.Value)
stmt, err := s.PrepareNamed(query)
txlog(logging.SQL, c, query, model.Value)
stmt, err := c.Store.PrepareNamed(query)
if err != nil {
return err
}
Expand All @@ -101,17 +101,17 @@ func genericCreate(s store, model *Model, cols columns.Columns, quoter quotable)
return fmt.Errorf("can not use %s as a primary key type!", keyType)
}

func genericUpdate(s store, model *Model, cols columns.Columns, quoter quotable) error {
func genericUpdate(c *Connection, model *Model, cols columns.Columns, quoter quotable) error {
stmt := fmt.Sprintf("UPDATE %s AS %s SET %s WHERE %s", quoter.Quote(model.TableName()), model.Alias(), cols.Writeable().QuotedUpdateString(quoter), model.WhereNamedID())
txlog(logging.SQL, s, stmt, model.ID())
_, err := s.NamedExec(stmt, model.Value)
txlog(logging.SQL, c, stmt, model.ID())
_, err := c.Store.NamedExec(stmt, model.Value)
if err != nil {
return err
}
return nil
}

func genericUpdateQuery(s store, model *Model, cols columns.Columns, quoter quotable, query Query, bindType int) (int64, error) {
func genericUpdateQuery(c *Connection, model *Model, cols columns.Columns, quoter quotable, query Query, bindType int) (int64, error) {
q := fmt.Sprintf("UPDATE %s AS %s SET %s", quoter.Quote(model.TableName()), model.Alias(), cols.Writeable().QuotedUpdateString(quoter))

q, updateArgs, err := sqlx.Named(q, model.Value)
Expand All @@ -124,7 +124,7 @@ func genericUpdateQuery(s store, model *Model, cols columns.Columns, quoter quot

q = sqlx.Rebind(bindType, q)

result, err := genericExec(s, q, append(updateArgs, sb.args...)...)
result, err := genericExec(c, q, append(updateArgs, sb.args...)...)
if err != nil {
return 0, err
}
Expand All @@ -137,41 +137,41 @@ func genericUpdateQuery(s store, model *Model, cols columns.Columns, quoter quot
return n, err
}

func genericDestroy(s store, model *Model, quoter quotable) error {
func genericDestroy(c *Connection, model *Model, quoter quotable) error {
stmt := fmt.Sprintf("DELETE FROM %s AS %s WHERE %s", quoter.Quote(model.TableName()), model.Alias(), model.WhereID())
_, err := genericExec(s, stmt, model.ID())
_, err := genericExec(c, stmt, model.ID())
if err != nil {
return err
}
return nil
}

func genericDelete(s store, model *Model, query Query) error {
func genericDelete(c *Connection, model *Model, query Query) error {
sqlQuery, args := query.ToSQL(model)
_, err := genericExec(s, sqlQuery, args...)
_, err := genericExec(c, sqlQuery, args...)
return err
}

func genericExec(s store, stmt string, args ...interface{}) (sql.Result, error) {
txlog(logging.SQL, s, stmt, args...)
res, err := s.Exec(stmt, args...)
func genericExec(c *Connection, stmt string, args ...interface{}) (sql.Result, error) {
txlog(logging.SQL, c, stmt, args...)
res, err := c.Store.Exec(stmt, args...)
return res, err
}

func genericSelectOne(s store, model *Model, query Query) error {
func genericSelectOne(c *Connection, model *Model, query Query) error {
sqlQuery, args := query.ToSQL(model)
txlog(logging.SQL, query.Connection, sqlQuery, args...)
err := s.Get(model.Value, sqlQuery, args...)
err := c.Store.Get(model.Value, sqlQuery, args...)
if err != nil {
return err
}
return nil
}

func genericSelectMany(s store, models *Model, query Query) error {
func genericSelectMany(c *Connection, models *Model, query Query) error {
sqlQuery, args := query.ToSQL(models)
txlog(logging.SQL, query.Connection, sqlQuery, args...)
err := s.Select(models.Value, sqlQuery, args...)
err := c.Store.Select(models.Value, sqlQuery, args...)
if err != nil {
return err
}
Expand Down
28 changes: 14 additions & 14 deletions dialect_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,31 @@ func (m *mysql) MigrationURL() string {
return m.URL()
}

func (m *mysql) Create(s store, model *Model, cols columns.Columns) error {
if err := genericCreate(s, model, cols, m); err != nil {
func (m *mysql) Create(c *Connection, model *Model, cols columns.Columns) error {
if err := genericCreate(c, model, cols, m); err != nil {
return fmt.Errorf("mysql create: %w", err)
}
return nil
}

func (m *mysql) Update(s store, model *Model, cols columns.Columns) error {
if err := genericUpdate(s, model, cols, m); err != nil {
func (m *mysql) Update(c *Connection, model *Model, cols columns.Columns) error {
if err := genericUpdate(c, model, cols, m); err != nil {
return fmt.Errorf("mysql update: %w", err)
}
return nil
}

func (m *mysql) UpdateQuery(s store, model *Model, cols columns.Columns, query Query) (int64, error) {
if n, err := genericUpdateQuery(s, model, cols, m, query, sqlx.QUESTION); err != nil {
func (m *mysql) UpdateQuery(c *Connection, model *Model, cols columns.Columns, query Query) (int64, error) {
if n, err := genericUpdateQuery(c, model, cols, m, query, sqlx.QUESTION); err != nil {
return n, fmt.Errorf("mysql update query: %w", err)
} else {
return n, nil
}
}

func (m *mysql) Destroy(s store, model *Model) error {
func (m *mysql) Destroy(c *Connection, model *Model) error {
stmt := fmt.Sprintf("DELETE FROM %s WHERE %s = ?", m.Quote(model.TableName()), model.IDField())
_, err := genericExec(s, stmt, model.ID())
_, err := genericExec(c, stmt, model.ID())
if err != nil {
return fmt.Errorf("mysql destroy: %w", err)
}
Expand All @@ -115,26 +115,26 @@ func (m *mysql) Destroy(s store, model *Model) error {

var asRegex = regexp.MustCompile(`\sAS\s\S+`) // exactly " AS non-spaces"

func (m *mysql) Delete(s store, model *Model, query Query) error {
func (m *mysql) Delete(c *Connection, model *Model, query Query) error {
sqlQuery, args := query.ToSQL(model)
// * MySQL does not support table alias for DELETE syntax until 8.0.
// * Do not generate SQL manually if they may have `WHERE IN`.
// * Spaces are intentionally added to make it easy to see on the log.
sqlQuery = asRegex.ReplaceAllString(sqlQuery, " ")

_, err := genericExec(s, sqlQuery, args...)
_, err := genericExec(c, sqlQuery, args...)
return err
}

func (m *mysql) SelectOne(s store, model *Model, query Query) error {
if err := genericSelectOne(s, model, query); err != nil {
func (m *mysql) SelectOne(c *Connection, model *Model, query Query) error {
if err := genericSelectOne(c, model, query); err != nil {
return fmt.Errorf("mysql select one: %w", err)
}
return nil
}

func (m *mysql) SelectMany(s store, models *Model, query Query) error {
if err := genericSelectMany(s, models, query); err != nil {
func (m *mysql) SelectMany(c *Connection, models *Model, query Query) error {
if err := genericSelectMany(c, models, query); err != nil {
return fmt.Errorf("mysql select many: %w", err)
}
return nil
Expand Down
32 changes: 16 additions & 16 deletions dialect_postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (p *postgresql) Details() *ConnectionDetails {
return p.ConnectionDetails
}

func (p *postgresql) Create(s store, model *Model, cols columns.Columns) error {
func (p *postgresql) Create(c *Connection, model *Model, cols columns.Columns) error {
keyType, err := model.PrimaryKeyType()
if err != nil {
return err
Expand All @@ -65,8 +65,8 @@ func (p *postgresql) Create(s store, model *Model, cols columns.Columns) error {
} else {
query = fmt.Sprintf("INSERT INTO %s DEFAULT VALUES returning %s", p.Quote(model.TableName()), model.IDField())
}
txlog(logging.SQL, s, query, model.Value)
stmt, err := s.PrepareNamed(query)
txlog(logging.SQL, c, query, model.Value)
stmt, err := c.Store.PrepareNamed(query)
if err != nil {
return err
}
Expand All @@ -84,36 +84,36 @@ func (p *postgresql) Create(s store, model *Model, cols columns.Columns) error {
}
return nil
}
return genericCreate(s, model, cols, p)
return genericCreate(c, model, cols, p)
}

func (p *postgresql) Update(s store, model *Model, cols columns.Columns) error {
return genericUpdate(s, model, cols, p)
func (p *postgresql) Update(c *Connection, model *Model, cols columns.Columns) error {
return genericUpdate(c, model, cols, p)
}

func (p *postgresql) UpdateQuery(s store, model *Model, cols columns.Columns, query Query) (int64, error) {
return genericUpdateQuery(s, model, cols, p, query, sqlx.DOLLAR)
func (p *postgresql) UpdateQuery(c *Connection, model *Model, cols columns.Columns, query Query) (int64, error) {
return genericUpdateQuery(c, model, cols, p, query, sqlx.DOLLAR)
}

func (p *postgresql) Destroy(s store, model *Model) error {
func (p *postgresql) Destroy(c *Connection, model *Model) error {
stmt := p.TranslateSQL(fmt.Sprintf("DELETE FROM %s AS %s WHERE %s", p.Quote(model.TableName()), model.Alias(), model.WhereID()))
_, err := genericExec(s, stmt, model.ID())
_, err := genericExec(c, stmt, model.ID())
if err != nil {
return err
}
return nil
}

func (p *postgresql) Delete(s store, model *Model, query Query) error {
return genericDelete(s, model, query)
func (p *postgresql) Delete(c *Connection, model *Model, query Query) error {
return genericDelete(c, model, query)
}

func (p *postgresql) SelectOne(s store, model *Model, query Query) error {
return genericSelectOne(s, model, query)
func (p *postgresql) SelectOne(c *Connection, model *Model, query Query) error {
return genericSelectOne(c, model, query)
}

func (p *postgresql) SelectMany(s store, models *Model, query Query) error {
return genericSelectMany(s, models, query)
func (p *postgresql) SelectMany(c *Connection, models *Model, query Query) error {
return genericSelectMany(c, models, query)
}

func (p *postgresql) CreateDB() error {
Expand Down
Loading

0 comments on commit 5dfc37d

Please sign in to comment.