Skip to content

Commit

Permalink
add InsertAutoIncrAny to the interface. This is a compile time breaka…
Browse files Browse the repository at this point in the history
…ge for custom dialects, but doesn't break or impact any other code. This is in preparation for #20
  • Loading branch information
jmoiron committed Sep 6, 2014
1 parent 729532c commit c028a25
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type Dialect interface {

// InsertAutoIncr
InsertAutoIncr(e SqlExecutor, insertSql string, params ...interface{}) (int64, error)
// InsertAutIncrAny takes a destination for non-integer auto-incr, like
// uuids which scan to strings, hashes, etc.
InsertAutoIncrAny(e SqlExecutor, insertSql string, dest interface{}, params ...interface{}) error

// BindVar returns the variable string to use when forming SQL statements
// in many dbs it is "?", but Postgres requires '$#'
Expand Down Expand Up @@ -65,6 +68,18 @@ func standardInsertAutoIncr(e SqlExecutor, insertSql string, params ...interface
return res.LastInsertId()
}

func standardAutoIncrAny(e SqlExecutor, insertSql string, dest interface{}, params ...interface{}) error {
rows, err := e.handle().Queryx(insertSql, params...)
if err != nil {
return err
}
defer rows.Close()
if rows.Next() {
return rows.Scan(dest)
}
return fmt.Errorf("No auto-incr value returned for insert: `%s` error: %s", insertSql, rows.Err())
}

// -- sqlite3

// SqliteDialect implements the Dialect interface for Sqlite3.
Expand Down Expand Up @@ -139,6 +154,10 @@ func (d SqliteDialect) InsertAutoIncr(e SqlExecutor, insertSql string, params ..
return standardInsertAutoIncr(e, insertSql, params...)
}

func (d SqliteDialect) InsertAutoIncrAny(e SqlExecutor, insertSql string, dest interface{}, params ...interface{}) error {
return standardAutoIncrAny(e, insertSql, dest, params...)
}

// QuoteField quotes f with "" for sqlite
func (d SqliteDialect) QuoteField(f string) string {
return `"` + f + `"`
Expand Down Expand Up @@ -257,6 +276,10 @@ func (d PostgresDialect) InsertAutoIncr(e SqlExecutor, insertSql string, params
return 0, errors.New("No serial value returned for insert: " + insertSql + ", error: " + rows.Err().Error())
}

func (d PostgresDialect) InsertAutoIncrAny(e SqlExecutor, insertSql string, dest interface{}, params ...interface{}) error {
return standardAutoIncrAny(e, insertSql, dest, params...)
}

// QuoteField quotes f with ""
func (d PostgresDialect) QuoteField(f string) string {
return `"` + sqlx.NameMapper(f) + `"`
Expand Down Expand Up @@ -359,6 +382,10 @@ func (d MySQLDialect) InsertAutoIncr(e SqlExecutor, insertSql string, params ...
return standardInsertAutoIncr(e, insertSql, params...)
}

func (d MySQLDialect) InsertAutoIncrAny(e SqlExecutor, insertSql string, dest interface{}, params ...interface{}) error {
return standardAutoIncrAny(e, insertSql, dest, params...)
}

// QuoteField quotes f using ``.
func (d MySQLDialect) QuoteField(f string) string {
return "`" + f + "`"
Expand Down

0 comments on commit c028a25

Please sign in to comment.