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

Use Query() instead of QueryRow() for PostgreSQL insert action. #11

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion base.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (d *base) QuerySql(criteria *Criteria) (string, []interface{}) {
if order.desc {
query = append(query, "DESC")
}
if i < orderByLen -1 {
if i < orderByLen-1 {
query = append(query, ",")
}
}
Expand Down
12 changes: 6 additions & 6 deletions criteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func NewCondition(expr string, args ...interface{}) *Condition {
}

//Snakecase column name
func NewEqualCondition(column string, value interface {}) *Condition{
func NewEqualCondition(column string, value interface{}) *Condition {
expr := column + " = ?"
return NewCondition(expr,value)
return NewCondition(expr, value)
}

func NewInCondition(column string, values []interface{}) *Condition {
Expand All @@ -72,9 +72,9 @@ func (c *Condition) And(expr string, args ...interface{}) *Condition {
}

//Snakecase column name
func (c *Condition) AndEqual(column string, value interface {}) *Condition{
func (c *Condition) AndEqual(column string, value interface{}) *Condition {
expr := column + " = ?"
c.And(expr,value)
c.And(expr, value)
return c
}

Expand All @@ -97,9 +97,9 @@ func (c *Condition) Or(expr string, args ...interface{}) *Condition {
}

//Snakecase column name
func (c *Condition) OrEqual(column string, value interface {}) *Condition{
func (c *Condition) OrEqual(column string, value interface{}) *Condition {
expr := column + " = ?"
c.Or(expr,value)
c.Or(expr, value)
return c
}

Expand Down
2 changes: 1 addition & 1 deletion dialect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func DoTestQuerySQL(assert *assrt.Assert, info dialectSyntax) {
subCondition := NewCondition("score <= ?", 60).Or("score >= ?", 80)
condition.AndCondition(subCondition)
criteria.condition = condition
criteria.orderBys = []order{order{info.dialect.Quote("name"),false},order{info.dialect.Quote("grade"),true}}
criteria.orderBys = []order{order{info.dialect.Quote("name"), false}, order{info.dialect.Quote("grade"), true}}
criteria.offset = 3
criteria.limit = 10
sql, _ := info.dialect.QuerySql(criteria)
Expand Down
4 changes: 2 additions & 2 deletions migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (mg *Migration) dropTableIfExists(structPtr interface{}) {

//Can only drop table on database which name has "test" suffix.
//Used for testing
func (mg *Migration) DropTable(strutPtr interface {}) {
func (mg *Migration) DropTable(strutPtr interface{}) {
if !strings.HasSuffix(mg.DbName, "test") {
panic("Drop table can only be executed on database which name has 'test' suffix")
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func (mg *Migration) CreateIndexIfNotExists(table interface{}, name string, uniq
func (mg *Migration) Close() {
if mg.Db != nil {
err := mg.Db.Close()
if err != nil{
if err != nil {
panic(err)
}
}
Expand Down
8 changes: 4 additions & 4 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ func (model *Model) columnsAndValues(forUpdate bool) ([]string, []interface{}) {
include = column.Value != nil && !column.PK
} else {
include = true
if column.Value == nil{
if column.Value == nil {
include = false
}else if column.PK {
if intValue,ok := column.Value.(int64); ok{
} else if column.PK {
if intValue, ok := column.Value.(int64); ok {
include = intValue != 0
}else if strValue, ok := column.Value.(string); ok{
} else if strValue, ok := column.Value.(string); ok {
include = strValue != ""
}
}
Expand Down
17 changes: 10 additions & 7 deletions postgres.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package qbs

import (
"database/sql"
"fmt"
"strings"
"time"
"database/sql"
)

type postgres struct {
Expand Down Expand Up @@ -51,16 +51,19 @@ func (d *postgres) SqlType(f interface{}, size int) string {
}

func (d *postgres) Insert(q *Qbs) (int64, error) {
var id int64
sql, args := d.Dialect.InsertSql(q.criteria)
row := q.QueryRow(sql, args...)
rows, err := q.Query(sql, args...)
if err != nil {
return id, err
}
value := q.criteria.model.Pk.Value
var err error
var id int64
rows.Next() // Must call Next() before Scan() on *Rows
if _, ok := value.(int64); ok {
err = row.Scan(&id)
}else if _, ok := value.(string); ok {
err = rows.Scan(&id)
} else if _, ok := value.(string); ok {
var str string
err = row.Scan(&str)
err = rows.Scan(&str)
}
return id, err
}
Expand Down
10 changes: 5 additions & 5 deletions qbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (q *Qbs) Where(expr string, args ...interface{}) *Qbs {
}

//Snakecase column name
func (q *Qbs) WhereEqual(column string, value interface {}) *Qbs{
func (q *Qbs) WhereEqual(column string, value interface{}) *Qbs {
q.criteria.condition = NewEqualCondition(column, value)
return q
}
Expand All @@ -109,12 +109,12 @@ func (q *Qbs) Offset(offset int) *Qbs {
}

func (q *Qbs) OrderBy(path string) *Qbs {
q.criteria.orderBys = append(q.criteria.orderBys,order{q.Dialect.Quote(path),false})
q.criteria.orderBys = append(q.criteria.orderBys, order{q.Dialect.Quote(path), false})
return q
}

func (q *Qbs) OrderByDesc(path string) *Qbs {
q.criteria.orderBys = append(q.criteria.orderBys,order{q.Dialect.Quote(path),true})
q.criteria.orderBys = append(q.criteria.orderBys, order{q.Dialect.Quote(path), true})
return q
}

Expand Down Expand Up @@ -422,8 +422,8 @@ func (q *Qbs) ContainsValue(table interface{}, column string, value interface{})
return err == nil
}

func (q *Qbs) Close() error{
if q.Db != nil{
func (q *Qbs) Close() error {
if q.Db != nil {
return q.Db.Close()
}
return nil
Expand Down
11 changes: 5 additions & 6 deletions qbs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
)

import (
"fmt"
// _ "github.com/bmizerany/pq"
// _ "github.com/mattn/go-sqlite3"
"errors"
"fmt"
"github.com/coocood/assrt"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
_ "github.com/ziutek/mymysql/godrv"
"os"
)

var toRun = []dialectInfo{
// allDialectInfos[0],
allDialectInfos[0],
allDialectInfos[1],
// allDialectInfos[2],
allDialectInfos[2],
}

const (
Expand Down Expand Up @@ -494,4 +494,3 @@ func DoTestStringPk(assert *assrt.Assert, info dialectInfo) {
q.Find(spk)
assert.Equal(10, spk.Count)
}