Skip to content

Commit

Permalink
Update xsql Update,Delete
Browse files Browse the repository at this point in the history
  • Loading branch information
onanying committed Nov 20, 2023
1 parent 2e8d20c commit 93aaca0
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 28 deletions.
23 changes: 19 additions & 4 deletions src/xsql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,24 @@ if err != nil {

> Oracle placeholder needs to be modified to :id
Update full

```go
test := Test{
Id: 10,
Id: 8,
Foo: "test",
Bar: time.Now(),
}
res, err := DB.Update(&test, "id = ?", 10)
res, err := DB.Update(&test, "id = ?", 8)
```

Update columns

```go
test := map[string]interface{}{
"foo": "test",
}
res, err := DB.Model(&Test{}).Update(test, "id = ?", 8)
```

## Delete
Expand All @@ -156,11 +167,15 @@ res, err := DB.Update(&test, "id = ?", 10)
```go
test := Test{
Id: 10,
Id: 8,
Foo: "test",
Bar: time.Now(),
}
res, err := DB.Delete(&test, "id = ?", test.Id)
res, err := DB.Model(&test).Delete("id = ?", test.Id)
```

```go
res, err := DB.Model(&Test{}).Delete("id = ?", 8)
```

## Exec
Expand Down
4 changes: 2 additions & 2 deletions src/xsql/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ 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(data interface{}, expr string, args ...interface{}) (sql.Result, error) {
return t.executor.Delete(data, expr, args, t.Options)
func (t *DB) Model(s interface{}) *ModelExecutor {
return t.executor.model(s, t.Options)
}

func (t *DB) Exec(query string, args ...interface{}) (sql.Result, error) {
Expand Down
29 changes: 22 additions & 7 deletions src/xsql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func TestEmbeddingUpdate(t *testing.T) {
Bar: time.Now(),
},
}
_, err := DB.Update(&test, "id = ?", 10)
_, err := DB.Update(&test, "id = ?", 8)

a.Empty(err)
}
Expand All @@ -222,11 +222,24 @@ func TestUpdate(t *testing.T) {
DB := newDB()

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

a.Empty(err)
}

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

DB := newDB()

test := map[string]interface{}{
"foo": "test update 2",
}
_, err := DB.Model(&Test{}).Update(test, "id = ?", 8)

a.Empty(err)
}
Expand All @@ -237,12 +250,14 @@ func TestDelete(t *testing.T) {
DB := newDB()

test := Test{
Id: 10,
Id: 8,
Foo: "test",
Bar: time.Now(),
}
_, err := DB.Delete(&test, "id = ?", test.Id)
_, err := DB.Model(&test).Delete("id = ?", test.Id)
a.Empty(err)

_, err = DB.Model(&Test{}).Delete("id = ?", 8)
a.Empty(err)
}

Expand All @@ -251,7 +266,7 @@ func TestExec(t *testing.T) {

DB := newDB()

_, err := DB.Exec("DELETE FROM xsql WHERE id = ?", 10)
_, err := DB.Exec("DELETE FROM xsql WHERE id = ?", 7)

a.Empty(err)
}
Expand Down
84 changes: 69 additions & 15 deletions src/xsql/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ type executor struct {
Executor
}

type ModelExecutor struct {
Executor
Options *sqlOptions
TableName string
}

func (t *executor) Insert(data interface{}, opts *sqlOptions) (sql.Result, error) {
fields := make([]string, 0)
vars := make([]string, 0)
Expand Down Expand Up @@ -143,6 +149,27 @@ func (t *executor) BatchInsert(array interface{}, opts *sqlOptions) (sql.Result,
return res, nil
}

func (t *executor) model(s interface{}, opts *sqlOptions) *ModelExecutor {
var table string
value := reflect.ValueOf(s)
switch value.Kind() {
case reflect.Ptr:
return t.model(value.Elem().Interface(), opts)
case reflect.Struct:
if tab, ok := s.(Table); ok {
table = tab.TableName()
} else {
table = value.Type().Name()
}
break
}
return &ModelExecutor{
Executor: t.Executor,
Options: opts,
TableName: table,
}
}

func (t *executor) Update(data interface{}, expr string, args []interface{}, opts *sqlOptions) (sql.Result, error) {
set := make([]string, 0)
bindArgs := make([]interface{}, 0)
Expand Down Expand Up @@ -196,24 +223,16 @@ func (t *executor) Update(data interface{}, expr string, args []interface{}, opt
return res, nil
}

func (t *executor) Delete(data interface{}, expr string, args []interface{}, opts *sqlOptions) (sql.Result, error) {
func (t *ModelExecutor) Update(data map[string]interface{}, expr string, args ...interface{}) (sql.Result, error) {
set := make([]string, 0)
bindArgs := make([]interface{}, 0)

table := ""
table := t.TableName
opts := t.Options

value := reflect.ValueOf(data)
switch value.Kind() {
case reflect.Ptr:
return t.Delete(value.Elem().Interface(), expr, args, opts)
case reflect.Struct:
if tab, ok := data.(Table); ok {
table = tab.TableName()
} else {
table = value.Type().Name()
}
break
default:
return nil, errors.New("xsql: only for struct type")
for k, v := range data {
set = append(set, fmt.Sprintf("`%s` = ?", k))
bindArgs = append(bindArgs, v)
}

where := ""
Expand All @@ -222,6 +241,41 @@ func (t *executor) Delete(data interface{}, expr string, args []interface{}, opt
bindArgs = append(bindArgs, args...)
}

SQL := fmt.Sprintf(`UPDATE %s SET %s%s`, table, strings.Join(set, ", "), where)

startTime := time.Now()
res, err := t.Executor.Exec(SQL, bindArgs...)
var rowsAffected int64
if res != nil {
rowsAffected, _ = res.RowsAffected()
}
l := &Log{
Duration: time.Now().Sub(startTime),
SQL: SQL,
Bindings: bindArgs,
RowsAffected: rowsAffected,
Error: err,
}
opts.doDebug(l)
if err != nil {
return nil, err
}

return res, nil
}

func (t *ModelExecutor) Delete(expr string, args ...interface{}) (sql.Result, error) {
bindArgs := make([]interface{}, 0)

table := t.TableName
opts := t.Options

where := ""
if expr != "" {
where = fmt.Sprintf(` WHERE %s`, expr)
bindArgs = append(bindArgs, args...)
}

SQL := fmt.Sprintf(`DELETE FROM %s%s`, table, where)

startTime := time.Now()
Expand Down

0 comments on commit 93aaca0

Please sign in to comment.