Skip to content

Commit

Permalink
Update xsql DB.Delete
Browse files Browse the repository at this point in the history
  • Loading branch information
onanying committed Oct 7, 2023
1 parent a8a7a1e commit 6f5e421
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/xsql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ res, err := DB.Update(&test, "id = ?", 10)

## Delete

> Oracle placeholder needs to be modified to :id
```go
test := Test{
Id: 10,
Foo: "test",
Bar: time.Now(),
}
res, err := DB.Delete(&test, "DELETE FROM ${TABLE} WHERE id = ?", test.Id)
```

## Exec

Use `Exec()` to manually execute the delete, you can also manually execute the update operation.

> Oracle placeholder needs to be modified to :id
Expand Down Expand Up @@ -196,6 +209,9 @@ type sqlOptions struct {

// Default: INSERT INTO
InsertKey string

// Default: ${TABLE}
TableKey string

// Default: ?
// oracle can be configured as :%d
Expand Down
5 changes: 5 additions & 0 deletions src/xsql/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func (t *DB) Update(data interface{}, expr string, args ...interface{}) (sql.Res
return t.executor.Update(data, expr, args, t.Options)
}

func (t *DB) Delete(i interface{}, query string, args ...interface{}) (sql.Result, error) {
query = t.tableComplete(i, query)
return t.executor.Exec(query, args, t.Options)
}

func (t *DB) Exec(query string, args ...interface{}) (sql.Result, error) {
return t.executor.Exec(query, args, t.Options)
}
Expand Down
19 changes: 17 additions & 2 deletions src/xsql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestDebugFunc(t *testing.T) {

test := Test{
Id: 0,
Foo: "test update",
Foo: "test",
Bar: time.Now(),
}
_, err := DB.Update(&test, "id = ?", 0)
Expand Down Expand Up @@ -219,14 +219,29 @@ func TestUpdate(t *testing.T) {

test := Test{
Id: 999,
Foo: "test update",
Foo: "test",
Bar: time.Now(),
}
_, err := DB.Update(&test, "id = ?", 10)

a.Empty(err)
}

func TestDelete(t *testing.T) {
a := assert.New(t)

DB := newDB()

test := Test{
Id: 10,
Foo: "test",
Bar: time.Now(),
}
_, err := DB.Delete(&test, "DELETE FROM ${TABLE} WHERE id = ?", test.Id)

a.Empty(err)
}

func TestExec(t *testing.T) {
a := assert.New(t)

Expand Down

0 comments on commit 6f5e421

Please sign in to comment.