-
-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add query.Delete #658
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,13 @@ import ( | |
"github.com/gobuffalo/pop/v5/logging" | ||
) | ||
|
||
type operation string | ||
|
||
const ( | ||
Select operation = "SELECT" | ||
Delete operation = "DELETE" | ||
) | ||
Comment on lines
+12
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For bulk inserts/updates this list will likely grow. |
||
|
||
// Query is the main value that is used to build up a query | ||
// to be executed against the `Connection`. | ||
type Query struct { | ||
|
@@ -25,6 +32,7 @@ type Query struct { | |
havingClauses havingClauses | ||
Paginator *Paginator | ||
Connection *Connection | ||
Operation operation | ||
} | ||
|
||
// Clone will fill targetQ query with the connection used in q, if | ||
|
@@ -42,6 +50,7 @@ func (q *Query) Clone(targetQ *Query) { | |
targetQ.groupClauses = q.groupClauses | ||
targetQ.havingClauses = q.havingClauses | ||
targetQ.addColumns = q.addColumns | ||
targetQ.Operation = q.Operation | ||
|
||
if q.Paginator != nil { | ||
paginator := *q.Paginator | ||
|
@@ -196,6 +205,7 @@ func Q(c *Connection) *Query { | |
eager: c.eager, | ||
eagerFields: c.eagerFields, | ||
eagerMode: eagerModeNil, | ||
Operation: Select, | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,7 +84,14 @@ func (sq *sqlBuilder) compile() { | |
sq.sql = sq.Query.RawSQL.Fragment | ||
} | ||
} else { | ||
sq.sql = sq.buildSelectSQL() | ||
switch sq.Query.Operation { | ||
case Select: | ||
sq.sql = sq.buildSelectSQL() | ||
case Delete: | ||
sq.sql = sq.buildDeleteSQL() | ||
default: | ||
panic("unexpected query operation " + sq.Query.Operation) | ||
} | ||
} | ||
|
||
if inRegex.MatchString(sq.sql) { | ||
|
@@ -114,6 +121,27 @@ func (sq *sqlBuilder) buildSelectSQL() string { | |
return sql | ||
} | ||
|
||
func (sq *sqlBuilder) buildDeleteSQL() string { | ||
fc := sq.buildfromClauses() | ||
|
||
sql := fmt.Sprintf("DELETE FROM %s", fc) | ||
|
||
sql = sq.buildWhereClauses(sql) | ||
|
||
// paginated delete supported by sqlite and mysql | ||
// > If SQLite is compiled with the SQLITE_ENABLE_UPDATE_DELETE_LIMIT compile-time option [...] - from https://www.sqlite.org/lang_delete.html | ||
// | ||
// not generic enough IMO, therefore excluded | ||
// | ||
//switch sq.Query.Connection.Dialect.Name() { | ||
//case nameMySQL, nameSQLite3: | ||
// sql = sq.buildOrderClauses(sql) | ||
// sql = sq.buildPaginationClauses(sql) | ||
//} | ||
Comment on lines
+131
to
+140
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure about this part, but I think it is better to leave it out. Should I delete the comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah leave the feature out, but the comment in :) |
||
|
||
return sql | ||
} | ||
|
||
func (sq *sqlBuilder) buildfromClauses() fromClauses { | ||
models := []*Model{ | ||
sq.Model, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comment in
test.sh