Skip to content

Commit

Permalink
feat: Support DELETE
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash committed Oct 25, 2021
1 parent fc6198d commit a2d1583
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 72 deletions.
3 changes: 2 additions & 1 deletion .CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
- [Support Aggregate Functions](https://github.com/gotomicro/eql/pull/37)
- [Updater implementation, excluding WHERE clause](https://github.com/gotomicro/eql/pull/36)
- [Force test and lint in pre-push](https://github.com/gotomicro/eql/pull/35)
- [Insert implementation ](https://github.com/gotomicro/eql/pull/38)
- [Insert implementation ](https://github.com/gotomicro/eql/pull/38)
- [Delete implementation ](https://github.com/gotomicro/eql/pull/43)
3 changes: 2 additions & 1 deletion .deepsource.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ enabled = true

[[analyzers]]
name = "sql"
enabled = true
enabled = true

27 changes: 0 additions & 27 deletions .github/workflows/deepsource.yml

This file was deleted.

10 changes: 7 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ on:
branches: [ main ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
Expand All @@ -36,4 +34,10 @@ jobs:
run: go build -v ./...

- name: Test
run: go test -v ./...
run: go test -coverprofile=cover.out -v ./...
# - name: Deepsource Cli
# run: curl https://deepsource.io/cli | sh
# - name: Report Code Coverage
# run: ./bin/deepsource report --analyzer test-coverage --key go --value-file ./cover.out
# env:
# DEEPSOURCE_DSN: ${{ secrets.DEEPSOURCE_DSN }}
6 changes: 4 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ func (db *DB) Select(columns ...Selectable) *Selector {
}

// Delete starts a "delete" query.
func (*DB) Delete() *Deleter {
panic("implement me")
func (db *DB) Delete() *Deleter {
return &Deleter{
builder: db.builder(),
}
}

func (db *DB) Update(table interface{}) *Updater {
Expand Down
43 changes: 29 additions & 14 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,45 @@ package eql

// Deleter builds DELETE query
type Deleter struct {

builder
table interface{}
where []Predicate
}

// Build returns DELETE query
func (d *Deleter) Build() (*Query, error) {
panic("implement me")
_, err := d.buffer.WriteString("DELETE FROM ")
if err != nil {
return nil, err
}
d.meta, err = d.registry.Get(d.table)
if err != nil {
return nil, err
}

d.quote(d.meta.tableName)
if len(d.where) > 0 {
_, err = d.buffer.WriteString(" WHERE ")
if err != nil {
return nil, err
}
err = d.buildPredicates(d.where)
if err != nil {
return nil, err
}
}
d.end()
return &Query{SQL: d.buffer.String(), Args: d.args}, nil
}

// From accepts model definition
func (d *Deleter) From(table interface{}) *Deleter {
panic("implement me")
d.table = table
return d
}

// Where accepts predicates
func (d *Deleter) Where(predicates...Predicate) *Deleter {
panic("implement me")
}

// OrderBy means "ORDER BY"
func (d *Deleter) OrderBy(orderBy... OrderBy) *Deleter {
panic("implement me")
}

// Limit limits the number of deleted rows
func (d *Deleter) Limit(limit int) *Deleter {
panic("implement me")
d.where = predicates
return d
}
21 changes: 0 additions & 21 deletions delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,6 @@ func TestDeleter_Build(t *testing.T) {
wantSql: "DELETE FROM `test_model` WHERE `id`=?;",
wantArgs: []interface{}{16},
},
{
name: "order",
builder: New().Delete().From(&TestModel{}).Where(C("Id").EQ(14)).
OrderBy(ASC("Id"), DESC("Name")),
wantSql: "DELETE FROM `test_model` WHERE `id`=? ORDER BY `id` ASC, `name` DESC;",
wantArgs: []interface{}{14},
},
{
name: "order and limit",
builder: New().Delete().From(&TestModel{}).Where(C("Id").EQ(14)).
OrderBy(ASC("Id"), DESC("Name")).Limit(3),
wantSql: "DELETE FROM `test_model` WHERE `id`=? ORDER BY `id` ASC, `name` DESC LIMIT ?;",
wantArgs: []interface{}{14, 3},
},

{
name: "limit",
builder: New().Delete().From(&TestModel{}).Where(C("Id").EQ(14)).Limit(3),
wantSql: "DELETE FROM `test_model` WHERE `id`=? LIMIT ?;",
wantArgs: []interface{}{14, 3},
},
}

for _, tc := range testCases {
Expand Down
6 changes: 3 additions & 3 deletions model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ func TestTagMetaRegistry(t *testing.T) {
assert.Equal(t, "id", idMeta.columnName)
assert.Equal(t, "Id", idMeta.fieldName)
assert.Equal(t, reflect.TypeOf(int64(0)), idMeta.typ)
assert.False(t, idMeta.isAutoIncrement)
assert.False(t, idMeta.isPrimaryKey)
assert.True(t, idMeta.isAutoIncrement)
assert.True(t, idMeta.isPrimaryKey)

idMetaFistName := meta.fieldMap["FirstName"]
assert.Equal(t, "fist_name", idMetaFistName.columnName)
assert.Equal(t, "first_name", idMetaFistName.columnName)
assert.Equal(t, "FirstName", idMetaFistName.fieldName)
assert.Equal(t, reflect.TypeOf(string("")), idMetaFistName.typ)

Expand Down

0 comments on commit a2d1583

Please sign in to comment.