diff --git a/README.md b/README.md index 2136d30..064d83c 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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. diff --git a/sqlez.go b/sqlez.go index 6e4466f..2e6a1a8 100644 --- a/sqlez.go +++ b/sqlez.go @@ -15,6 +15,7 @@ type Params struct { OrderBy string Limit int SkipEmpty bool + OrIgnore bool } // DB represents the sqlez database wrapper @@ -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 } @@ -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...) }