Fix tests
This commit is contained in:
parent
ae0d5506c1
commit
bfaffeebb1
10
engine.go
10
engine.go
|
@ -444,7 +444,7 @@ func (engine *Engine) DumpTables(tables []*schemas.Table, w io.Writer, tp ...sch
|
||||||
return engine.dumpTables(tables, w, tp...)
|
return engine.dumpTables(tables, w, tp...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatColumnValue(dstDialect dialects.Dialect, d interface{}, col *schemas.Column) string {
|
func formatColumnValue(dbLocation *time.Location, dstDialect dialects.Dialect, d interface{}, col *schemas.Column) string {
|
||||||
if d == nil {
|
if d == nil {
|
||||||
return "NULL"
|
return "NULL"
|
||||||
}
|
}
|
||||||
|
@ -473,10 +473,8 @@ func formatColumnValue(dstDialect dialects.Dialect, d interface{}, col *schemas.
|
||||||
|
|
||||||
return "'" + strings.Replace(v, "'", "''", -1) + "'"
|
return "'" + strings.Replace(v, "'", "''", -1) + "'"
|
||||||
} else if col.SQLType.IsTime() {
|
} else if col.SQLType.IsTime() {
|
||||||
if dstDialect.URI().DBType == schemas.MSSQL && col.SQLType.Name == schemas.DateTime {
|
|
||||||
if t, ok := d.(time.Time); ok {
|
if t, ok := d.(time.Time); ok {
|
||||||
return "'" + t.UTC().Format("2006-01-02 15:04:05") + "'"
|
return "'" + t.In(dbLocation).Format("2006-01-02 15:04:05") + "'"
|
||||||
}
|
|
||||||
}
|
}
|
||||||
var v = fmt.Sprintf("%s", d)
|
var v = fmt.Sprintf("%s", d)
|
||||||
if strings.HasSuffix(v, " +0000 UTC") {
|
if strings.HasSuffix(v, " +0000 UTC") {
|
||||||
|
@ -653,7 +651,7 @@ func (engine *Engine) dumpTables(tables []*schemas.Table, w io.Writer, tp ...sch
|
||||||
}
|
}
|
||||||
|
|
||||||
field := dataStruct.FieldByIndex(col.FieldIndex)
|
field := dataStruct.FieldByIndex(col.FieldIndex)
|
||||||
temp += "," + formatColumnValue(dstDialect, field.Interface(), col)
|
temp += "," + formatColumnValue(engine.DatabaseTZ, dstDialect, field.Interface(), col)
|
||||||
}
|
}
|
||||||
_, err = io.WriteString(w, temp[1:]+");\n")
|
_, err = io.WriteString(w, temp[1:]+");\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -680,7 +678,7 @@ func (engine *Engine) dumpTables(tables []*schemas.Table, w io.Writer, tp ...sch
|
||||||
return errors.New("unknow column error")
|
return errors.New("unknow column error")
|
||||||
}
|
}
|
||||||
|
|
||||||
temp += "," + formatColumnValue(dstDialect, d, col)
|
temp += "," + formatColumnValue(engine.DatabaseTZ, dstDialect, d, col)
|
||||||
}
|
}
|
||||||
_, err = io.WriteString(w, temp[1:]+");\n")
|
_, err = io.WriteString(w, temp[1:]+");\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -168,16 +168,16 @@ func TestInsertAutoIncr(t *testing.T) {
|
||||||
assert.Greater(t, user.Uid, int64(0))
|
assert.Greater(t, user.Uid, int64(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
type DefaultInsert struct {
|
func TestInsertDefault(t *testing.T) {
|
||||||
|
assert.NoError(t, PrepareEngine())
|
||||||
|
|
||||||
|
type DefaultInsert struct {
|
||||||
Id int64
|
Id int64
|
||||||
Status int `xorm:"default -1"`
|
Status int `xorm:"default -1"`
|
||||||
Name string
|
Name string
|
||||||
Created time.Time `xorm:"created"`
|
Created time.Time `xorm:"created"`
|
||||||
Updated time.Time `xorm:"updated"`
|
Updated time.Time `xorm:"updated"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInsertDefault(t *testing.T) {
|
|
||||||
assert.NoError(t, PrepareEngine())
|
|
||||||
|
|
||||||
di := new(DefaultInsert)
|
di := new(DefaultInsert)
|
||||||
err := testEngine.Sync2(di)
|
err := testEngine.Sync2(di)
|
||||||
|
@ -195,15 +195,15 @@ func TestInsertDefault(t *testing.T) {
|
||||||
assert.EqualValues(t, di2.Created.Unix(), di.Created.Unix())
|
assert.EqualValues(t, di2.Created.Unix(), di.Created.Unix())
|
||||||
}
|
}
|
||||||
|
|
||||||
type DefaultInsert2 struct {
|
func TestInsertDefault2(t *testing.T) {
|
||||||
|
assert.NoError(t, PrepareEngine())
|
||||||
|
|
||||||
|
type DefaultInsert2 struct {
|
||||||
Id int64
|
Id int64
|
||||||
Name string
|
Name string
|
||||||
Url string `xorm:"text"`
|
Url string `xorm:"text"`
|
||||||
CheckTime time.Time `xorm:"not null default '2000-01-01 00:00:00' TIMESTAMP"`
|
CheckTime time.Time `xorm:"not null default '2000-01-01 00:00:00' TIMESTAMP"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInsertDefault2(t *testing.T) {
|
|
||||||
assert.NoError(t, PrepareEngine())
|
|
||||||
|
|
||||||
di := new(DefaultInsert2)
|
di := new(DefaultInsert2)
|
||||||
err := testEngine.Sync2(di)
|
err := testEngine.Sync2(di)
|
||||||
|
|
18
scan.go
18
scan.go
|
@ -28,3 +28,21 @@ func (engine *Engine) row2mapStr(rows *core.Rows, types []*sql.ColumnType, field
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) row2sliceStr(rows *core.Rows, types []*sql.ColumnType, fields []string) ([]string, error) {
|
||||||
|
results := make([]string, 0, len(fields))
|
||||||
|
var scanResults = make([]interface{}, len(fields))
|
||||||
|
for i := 0; i < len(fields); i++ {
|
||||||
|
var s sql.NullString
|
||||||
|
scanResults[i] = &s
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := rows.Scan(scanResults...); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(fields); i++ {
|
||||||
|
results = append(results, scanResults[i].(*sql.NullString).String)
|
||||||
|
}
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
|
@ -75,34 +75,6 @@ func value2String(rawValue *reflect.Value) (str string, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func row2sliceStr(rows *core.Rows, fields []string) (results []string, err error) {
|
|
||||||
result := make([]string, 0, len(fields))
|
|
||||||
scanResultContainers := make([]interface{}, len(fields))
|
|
||||||
for i := 0; i < len(fields); i++ {
|
|
||||||
var scanResultContainer interface{}
|
|
||||||
scanResultContainers[i] = &scanResultContainer
|
|
||||||
}
|
|
||||||
if err := rows.Scan(scanResultContainers...); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < len(fields); i++ {
|
|
||||||
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[i]))
|
|
||||||
// if row is null then as empty string
|
|
||||||
if rawValue.Interface() == nil {
|
|
||||||
result = append(result, "")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if data, err := value2String(&rawValue); err == nil {
|
|
||||||
result = append(result, data)
|
|
||||||
} else {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (session *Session) rows2Strings(rows *core.Rows) (resultsSlice []map[string]string, err error) {
|
func (session *Session) rows2Strings(rows *core.Rows) (resultsSlice []map[string]string, err error) {
|
||||||
fields, err := rows.Columns()
|
fields, err := rows.Columns()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -124,13 +96,18 @@ func (session *Session) rows2Strings(rows *core.Rows) (resultsSlice []map[string
|
||||||
return resultsSlice, nil
|
return resultsSlice, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func rows2SliceString(rows *core.Rows) (resultsSlice [][]string, err error) {
|
func (session *Session) rows2SliceString(rows *core.Rows) (resultsSlice [][]string, err error) {
|
||||||
fields, err := rows.Columns()
|
fields, err := rows.Columns()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
types, err := rows.ColumnTypes()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
record, err := row2sliceStr(rows, fields)
|
record, err := session.engine.row2sliceStr(rows, types, fields)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -177,7 +154,7 @@ func (session *Session) QuerySliceString(sqlOrArgs ...interface{}) ([][]string,
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
return rows2SliceString(rows)
|
return session.rows2SliceString(rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
func row2mapInterface(rows *core.Rows, fields []string) (resultsMap map[string]interface{}, err error) {
|
func row2mapInterface(rows *core.Rows, fields []string) (resultsMap map[string]interface{}, err error) {
|
||||||
|
|
Loading…
Reference in New Issue