Generator clean up.
Ensure all sql types can be processed.
This commit is contained in:
parent
b3a52ceb31
commit
64ba909381
21 changed files with 495 additions and 208 deletions
|
|
@ -3,7 +3,7 @@ package sqlbuilder
|
|||
import (
|
||||
"database/sql"
|
||||
"github.com/dropbox/godropbox/errors"
|
||||
"github.com/sub0zero/go-sqlbuilder/types"
|
||||
"github.com/sub0zero/go-sqlbuilder/sqlbuilder/execution"
|
||||
)
|
||||
|
||||
type deleteStatement interface {
|
||||
|
|
@ -71,10 +71,10 @@ func (d *deleteStatementImpl) DebugSql() (query string, err error) {
|
|||
return DebugSql(d)
|
||||
}
|
||||
|
||||
func (d *deleteStatementImpl) Query(db types.Db, destination interface{}) error {
|
||||
func (d *deleteStatementImpl) Query(db execution.Db, destination interface{}) error {
|
||||
return Query(d, db, destination)
|
||||
}
|
||||
|
||||
func (d *deleteStatementImpl) Execute(db types.Db) (res sql.Result, err error) {
|
||||
func (d *deleteStatementImpl) Execute(db execution.Db) (res sql.Result, err error) {
|
||||
return Execute(d, db)
|
||||
}
|
||||
|
|
|
|||
8
sqlbuilder/execution/db.go
Normal file
8
sqlbuilder/execution/db.go
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
package execution
|
||||
|
||||
import "database/sql"
|
||||
|
||||
type Db interface {
|
||||
Exec(query string, args ...interface{}) (sql.Result, error)
|
||||
Query(query string, args ...interface{}) (*sql.Rows, error)
|
||||
}
|
||||
|
|
@ -6,14 +6,13 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"github.com/serenize/snaker"
|
||||
"github.com/sub0zero/go-sqlbuilder/types"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Query(db types.Db, query string, args []interface{}, destinationPtr interface{}) error {
|
||||
func Query(db Db, query string, args []interface{}, destinationPtr interface{}) error {
|
||||
|
||||
if destinationPtr == nil {
|
||||
return errors.New("Destination is nil. ")
|
||||
|
|
@ -54,7 +53,7 @@ func Query(db types.Db, query string, args []interface{}, destinationPtr interfa
|
|||
}
|
||||
}
|
||||
|
||||
func queryToSlice(db types.Db, query string, args []interface{}, slicePtr interface{}) error {
|
||||
func queryToSlice(db Db, query string, args []interface{}, slicePtr interface{}) error {
|
||||
if db == nil {
|
||||
return errors.New("db is nil")
|
||||
}
|
||||
|
|
@ -527,8 +526,8 @@ func isGoBaseType(objType reflect.Type) bool {
|
|||
typeStr := objType.String()
|
||||
|
||||
switch typeStr {
|
||||
case "string", "int", "int32", "int16", "float32", "float64", "time.Time", "bool", "[]byte", "[]uint8",
|
||||
"*string", "*int", "*int32", "*int16", "*float32", "*float64", "*time.Time", "*bool", "*[]byte", "*[]uint8":
|
||||
case "string", "int", "int16", "int32", "int64", "float32", "float64", "time.Time", "bool", "[]byte", "[]uint8",
|
||||
"*string", "*int", "*int16", "*int32", "*int64", "*float32", "*float64", "*time.Time", "*bool", "*[]byte", "*[]uint8":
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
@ -544,10 +543,10 @@ func setReflectValue(source, destination reflect.Value) error {
|
|||
if source.CanAddr() {
|
||||
sourceElem = source.Addr()
|
||||
} else {
|
||||
newDestination := reflect.New(destination.Type().Elem())
|
||||
newDestination.Elem().Set(source)
|
||||
sourceCopy := reflect.New(source.Type())
|
||||
sourceCopy.Elem().Set(source)
|
||||
|
||||
sourceElem = newDestination
|
||||
sourceElem = sourceCopy
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
@ -599,6 +598,7 @@ var nullInt64Type = reflect.TypeOf(sql.NullInt64{})
|
|||
var nullStringType = reflect.TypeOf(sql.NullString{})
|
||||
var nullBoolType = reflect.TypeOf(sql.NullBool{})
|
||||
var nullTimeType = reflect.TypeOf(NullTime{})
|
||||
var nullByteArrayType = reflect.TypeOf(NullByteArray{})
|
||||
|
||||
func newScanType(columnType *sql.ColumnType) reflect.Type {
|
||||
switch columnType.DatabaseTypeName() {
|
||||
|
|
@ -608,7 +608,7 @@ func newScanType(columnType *sql.ColumnType) reflect.Type {
|
|||
return nullInt32Type
|
||||
case "INT8":
|
||||
return nullInt64Type
|
||||
case "VARCHAR", "TEXT", "", "_TEXT", "TSVECTOR", "BPCHAR", "BYTEA", "UUID", "JSON", "JSONB":
|
||||
case "VARCHAR", "TEXT", "", "_TEXT", "TSVECTOR", "BPCHAR", "UUID", "JSON", "JSONB", "INTERVAL", "POINT", "BIT", "VARBIT", "XML":
|
||||
return nullStringType
|
||||
case "FLOAT4":
|
||||
return nullFloatType
|
||||
|
|
@ -616,10 +616,13 @@ func newScanType(columnType *sql.ColumnType) reflect.Type {
|
|||
return nullFloat64Type
|
||||
case "BOOL":
|
||||
return nullBoolType
|
||||
case "DATE", "TIMESTAMP", "TIMESTAMPTZ":
|
||||
case "BYTEA":
|
||||
return nullByteArrayType
|
||||
case "DATE", "TIMESTAMP", "TIMESTAMPTZ", "TIME", "TIMETZ":
|
||||
return nullTimeType
|
||||
default:
|
||||
panic("Unknown column database type " + columnType.DatabaseTypeName())
|
||||
fmt.Println("Unknown column database type " + columnType.DatabaseTypeName() + " using string as default.")
|
||||
return nullStringType
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,27 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
// NullByteArray
|
||||
type NullByteArray struct {
|
||||
ByteArray []byte
|
||||
Valid bool // Valid is true if Time is not NULL
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (nb *NullByteArray) Scan(value interface{}) error {
|
||||
nb.ByteArray, nb.Valid = value.([]byte)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (nb NullByteArray) Value() (driver.Value, error) {
|
||||
if !nb.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return nb.ByteArray, nil
|
||||
}
|
||||
|
||||
//NullTime
|
||||
type NullTime struct {
|
||||
Time time.Time
|
||||
Valid bool // Valid is true if Time is not NULL
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import (
|
|||
"database/sql"
|
||||
"github.com/dropbox/godropbox/errors"
|
||||
"github.com/serenize/snaker"
|
||||
"github.com/sub0zero/go-sqlbuilder/types"
|
||||
"github.com/sub0zero/go-sqlbuilder/sqlbuilder/execution"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
|
@ -39,11 +39,11 @@ type insertStatementImpl struct {
|
|||
errors []string
|
||||
}
|
||||
|
||||
func (s *insertStatementImpl) Query(db types.Db, destination interface{}) error {
|
||||
func (s *insertStatementImpl) Query(db execution.Db, destination interface{}) error {
|
||||
return Query(s, db, destination)
|
||||
}
|
||||
|
||||
func (u *insertStatementImpl) Execute(db types.Db) (res sql.Result, err error) {
|
||||
func (u *insertStatementImpl) Execute(db execution.Db) (res sql.Result, err error) {
|
||||
return Execute(u, db)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ package sqlbuilder
|
|||
import (
|
||||
"database/sql"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sub0zero/go-sqlbuilder/types"
|
||||
"github.com/sub0zero/go-sqlbuilder/sqlbuilder/execution"
|
||||
)
|
||||
|
||||
type lockMode string
|
||||
|
|
@ -92,10 +92,10 @@ func (l *lockStatementImpl) Sql() (query string, args []interface{}, err error)
|
|||
return
|
||||
}
|
||||
|
||||
func (l *lockStatementImpl) Query(db types.Db, destination interface{}) error {
|
||||
func (l *lockStatementImpl) Query(db execution.Db, destination interface{}) error {
|
||||
return Query(l, db, destination)
|
||||
}
|
||||
|
||||
func (l *lockStatementImpl) Execute(db types.Db) (sql.Result, error) {
|
||||
func (l *lockStatementImpl) Execute(db execution.Db) (sql.Result, error) {
|
||||
return Execute(l, db)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ package sqlbuilder
|
|||
import (
|
||||
"database/sql"
|
||||
"github.com/dropbox/godropbox/errors"
|
||||
"github.com/sub0zero/go-sqlbuilder/types"
|
||||
"github.com/sub0zero/go-sqlbuilder/sqlbuilder/execution"
|
||||
)
|
||||
|
||||
type selectStatement interface {
|
||||
|
|
@ -259,11 +259,11 @@ func (s *selectStatementImpl) FOR_UPDATE() selectStatement {
|
|||
return s
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) Query(db types.Db, destination interface{}) error {
|
||||
func (s *selectStatementImpl) Query(db execution.Db, destination interface{}) error {
|
||||
return Query(s, db, destination)
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) Execute(db types.Db) (res sql.Result, err error) {
|
||||
func (s *selectStatementImpl) Execute(db execution.Db) (res sql.Result, err error) {
|
||||
return Execute(s, db)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ package sqlbuilder
|
|||
import (
|
||||
"database/sql"
|
||||
"github.com/dropbox/godropbox/errors"
|
||||
"github.com/sub0zero/go-sqlbuilder/types"
|
||||
"github.com/sub0zero/go-sqlbuilder/sqlbuilder/execution"
|
||||
)
|
||||
|
||||
type setStatement interface {
|
||||
|
|
@ -197,10 +197,10 @@ func (s *setStatementImpl) DebugSql() (query string, err error) {
|
|||
return DebugSql(s)
|
||||
}
|
||||
|
||||
func (s *setStatementImpl) Query(db types.Db, destination interface{}) error {
|
||||
func (s *setStatementImpl) Query(db execution.Db, destination interface{}) error {
|
||||
return Query(s, db, destination)
|
||||
}
|
||||
|
||||
func (u *setStatementImpl) Execute(db types.Db) (res sql.Result, err error) {
|
||||
func (u *setStatementImpl) Execute(db execution.Db) (res sql.Result, err error) {
|
||||
return Execute(u, db)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ package sqlbuilder
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/sub0zero/go-sqlbuilder/types"
|
||||
"github.com/sub0zero/go-sqlbuilder/sqlbuilder/execution"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
|
@ -13,8 +13,8 @@ type Statement interface {
|
|||
|
||||
DebugSql() (query string, err error)
|
||||
|
||||
Query(db types.Db, destination interface{}) error
|
||||
Execute(db types.Db) (sql.Result, error)
|
||||
Query(db execution.Db, destination interface{}) error
|
||||
Execute(db execution.Db) (sql.Result, error)
|
||||
}
|
||||
|
||||
func DebugSql(statement Statement) (string, error) {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ package sqlbuilder
|
|||
import (
|
||||
"database/sql"
|
||||
"github.com/dropbox/godropbox/errors"
|
||||
"github.com/sub0zero/go-sqlbuilder/types"
|
||||
"github.com/sub0zero/go-sqlbuilder/sqlbuilder/execution"
|
||||
)
|
||||
|
||||
type updateStatement interface {
|
||||
|
|
@ -135,10 +135,10 @@ func (u *updateStatementImpl) DebugSql() (query string, err error) {
|
|||
return DebugSql(u)
|
||||
}
|
||||
|
||||
func (u *updateStatementImpl) Query(db types.Db, destination interface{}) error {
|
||||
func (u *updateStatementImpl) Query(db execution.Db, destination interface{}) error {
|
||||
return Query(u, db, destination)
|
||||
}
|
||||
|
||||
func (u *updateStatementImpl) Execute(db types.Db) (res sql.Result, err error) {
|
||||
func (u *updateStatementImpl) Execute(db execution.Db) (res sql.Result, err error) {
|
||||
return Execute(u, db)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import (
|
|||
"database/sql"
|
||||
"errors"
|
||||
"github.com/sub0zero/go-sqlbuilder/sqlbuilder/execution"
|
||||
"github.com/sub0zero/go-sqlbuilder/types"
|
||||
)
|
||||
|
||||
func serializeOrderByClauseList(statement statementType, orderByClauses []orderByClause, out *queryData) error {
|
||||
|
|
@ -114,7 +113,7 @@ func serializeColumnList(statement statementType, columns []column, out *queryDa
|
|||
return nil
|
||||
}
|
||||
|
||||
func Query(statement Statement, db types.Db, destination interface{}) error {
|
||||
func Query(statement Statement, db execution.Db, destination interface{}) error {
|
||||
query, args, err := statement.Sql()
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -124,7 +123,7 @@ func Query(statement Statement, db types.Db, destination interface{}) error {
|
|||
return execution.Query(db, query, args, destination)
|
||||
}
|
||||
|
||||
func Execute(statement Statement, db types.Db) (res sql.Result, err error) {
|
||||
func Execute(statement Statement, db execution.Db) (res sql.Result, err error) {
|
||||
query, args, err := statement.Sql()
|
||||
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue