Skip to content

Commit

Permalink
InsertInto now takes Params{}, added "OrIgnore"
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacwhyte committed Dec 13, 2021
1 parent 1efc048 commit c48ea36
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,12 @@ frank := WrestlerBio{
```
And simply pass it to sqlEZ using `InsertInto`:
```
res, err := db.InsertInto("wrestlers", frank, false)
res, err := db.InsertInto("wrestlers", frank, Params{})
```
You may be wondering about the `false` being passed into the `InsertInto` method. More on that below ([Ignoring unset values](#ignoring-unset-values)).
You can also pass a `Params` struct with some bool flags:

SkipEmpty - See below ([Ignoring unset values](#ignoring-unset-values))
OrIgnore - Will "INSERT OR IGNORE INTO" if you want to silently ignore duplicate keys

### UPDAT[E]ing data
Updating data is very similar to inserting, but it usually helps to add a `Where` statement to make sure you only update what you want to:
Expand All @@ -129,7 +132,7 @@ res, err := db.Update("wrestlers", WrestlerBio{Nickname: "Shinagawa Slender"}, s
SkipEmpty: true})
```

`InsertInto` also has this feature, which will allow your SQL database to populate columns with default values if you don't want to set them. Enable it by passing `true` to `InsertInto`'s third parameter (`skipEmpty`).
`InsertInto` also has this feature, which will allow your SQL database to populate columns with default values if you don't want to set them.

### Storing Go types that don't have a database counterpart
Sometimes you may want to store a type of data that exists in Go but doesn't have a related database type--for example, a map, slice, or populated struct. SqlEZ makes this possible by converting those datatypes to JSON and storing them as a string in your database. Simply give an item in your struct a field label of "dbjson" (followed by the column name) and sqlEZ will automatically convert your data type to and from JSON when moving data into/out of the database. Maps will automatically be converted to JSON strings, even if you don't specify the "dbjson" tag.
Expand Down
19 changes: 15 additions & 4 deletions sqlez.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Params struct {
OrderBy string
Limit int
SkipEmpty bool
OrIgnore bool
}

// DB represents the sqlez database wrapper
Expand Down Expand Up @@ -255,16 +256,21 @@ func (s *DB) SelectFrom(table string, structure interface{}, params ...interface
}

// InsertInto performs a "INSERT INTO x (y) VALUES (z)" query on the database and returns the results.
// Pass a struct representing the data you want to insert. Set skipEmpty to true if you want to ignore
// Pass a struct representing the data you want to insert. Set params.SkipEmpty to true if you want to ignore
// fields in the struct that are unset/zero. Otherwise the zeroed values will be inserted.
func (s *DB) InsertInto(table string, data interface{}, skipEmpty bool) (res sql.Result, err error) {
func (s *DB) InsertInto(table string, data interface{}, params ...Params) (res sql.Result, err error) {

p := Params{}
if params != nil {
p = params[0]
}

v := reflect.ValueOf(data)
if v.Kind() != reflect.Struct {
return nil, errors.New(`sqlez.InsertInto: 'structure' must be struct, got ` + v.Kind().String())
}

labels, interfaces, err := s.scanStruct(v, false, skipEmpty, true)
labels, interfaces, err := s.scanStruct(v, false, p.SkipEmpty, true)
if err != nil {
return nil, err
}
Expand All @@ -280,7 +286,12 @@ func (s *DB) InsertInto(table string, data interface{}, skipEmpty bool) (res sql
}
}

query := "INSERT INTO " + table + " (" + columns + ") VALUES (" + values + ")"
query := "INSERT INTO "
if p.OrIgnore {
query = "INSERT OR IGNORE INTO "
}

query = query + table + " (" + columns + ") VALUES (" + values + ")"
s.LastQuery = query
return s.DB.Exec(query, interfaces...)
}
Expand Down

0 comments on commit c48ea36

Please sign in to comment.