Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return errors in QueryRow properly #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ func (d oracle) sqlType(field modelField) string {

func (d oracle) insert(q *Qbs) (int64, error) {
sql, args := d.dialect.insertSql(q.criteria)
row := q.QueryRow(sql, args...)
row, err := q.QueryRow(sql, args...)
if err != nil {
return 0, err
}
value := q.criteria.model.pk.value
var err error
var id int64
if _, ok := value.(int64); ok {
err = row.Scan(&id)
Expand Down
6 changes: 4 additions & 2 deletions postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ func (d postgres) sqlType(field modelField) string {

func (d postgres) insert(q *Qbs) (int64, error) {
sql, args := d.dialect.insertSql(q.criteria)
row := q.QueryRow(sql, args...)
row, err := q.QueryRow(sql, args...)
if err != nil {
return 0, err
}
value := q.criteria.model.pk.value
var err error
var id int64
if _, ok := value.(int64); ok {
err = row.Scan(&id)
Expand Down
27 changes: 18 additions & 9 deletions qbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,15 +380,14 @@ func (q *Qbs) Exec(query string, args ...interface{}) (sql.Result, error) {
}

// Same as sql.Db.QueryRow or sql.Tx.QueryRow depends on if transaction has began
func (q *Qbs) QueryRow(query string, args ...interface{}) *sql.Row {
func (q *Qbs) QueryRow(query string, args ...interface{}) (*sql.Row, error) {
q.log(query, args...)
query = q.Dialect.substituteMarkers(query)
stmt, err := q.prepare(query)
if err != nil {
q.updateTxError(err)
return nil
return nil, q.updateTxError(err)
}
return stmt.QueryRow(args...)
return stmt.QueryRow(args...), nil
}

// Same as sql.Db.Query or sql.Tx.Query depends on if transaction has began
Expand Down Expand Up @@ -575,9 +574,13 @@ func (q *Qbs) ContainsValue(table interface{}, column string, value interface{})
quotedColumn := q.Dialect.quote(column)
quotedTable := q.Dialect.quote(tableName(table))
query := fmt.Sprintf("SELECT %v FROM %v WHERE %v = ?", quotedColumn, quotedTable, quotedColumn)
row := q.QueryRow(query, value)
row, err := q.QueryRow(query, value)
if err != nil {
q.updateTxError(err)
return false
}
var result interface{}
err := row.Scan(&result)
err = row.Scan(&result)
q.updateTxError(err)
return err == nil
}
Expand All @@ -599,15 +602,21 @@ func (q *Qbs) Count(table interface{}) int64 {
quotedTable := q.Dialect.quote(tableName(table))
query := "SELECT COUNT(*) FROM " + quotedTable
var row *sql.Row
var err error
if q.criteria.condition != nil {
conditionSql, args := q.criteria.condition.Merge()
query += " WHERE " + conditionSql
row = q.QueryRow(query, args...)
row, err = q.QueryRow(query, args...)
} else {
row = q.QueryRow(query)
row, err = q.QueryRow(query)
}
if err != nil {
q.updateTxError(err)
return 0
}

var count int64
err := row.Scan(&count)
err = row.Scan(&count)
if err == sql.ErrNoRows {
return 0
} else if err != nil {
Expand Down