Merge branch 'lunny/bug_fixed_mssql'

This commit is contained in:
Lunny Xiao 2016-12-01 14:49:49 +08:00
commit 2597ab2401
2 changed files with 35 additions and 7 deletions

View File

@ -684,6 +684,7 @@ func (session *Session) canCache() bool {
session.Statement.JoinStr != "" ||
session.Statement.RawSQL != "" ||
!session.Statement.UseCache ||
session.Statement.IsForUpdate ||
session.Tx != nil ||
len(session.Statement.selectStr) > 0 {
return false
@ -1332,9 +1333,13 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
}
colName = session.Engine.Quote(nm) + "." + colName
}
if session.Engine.dialect.DBType() == core.MSSQL {
autoCond = builder.IsNull{colName}
} else {
autoCond = builder.IsNull{colName}.Or(builder.Eq{colName: "0001-01-01 00:00:00"})
}
}
}
var sqlStr string
var args []interface{}
@ -1839,16 +1844,24 @@ func (session *Session) _row2Bean(rows *core.Rows, fields []string, fieldsCount
hasAssigned = true
t := vv.Convert(core.TimeType).Interface().(time.Time)
z, _ := t.Zone()
if len(z) == 0 || t.Year() == 0 { // !nashtsai! HACK tmp work around for lib/pq doesn't properly time with location
dbTZ := session.Engine.DatabaseTZ
if dbTZ == nil {
if session.Engine.dialect.DBType() == core.SQLITE {
dbTZ = time.UTC
} else {
dbTZ = time.Local
}
}
// set new location if database don't save timezone or give an incorrect timezone
if len(z) == 0 || t.Year() == 0 || t.Location().String() != dbTZ.String() { // !nashtsai! HACK tmp work around for lib/pq doesn't properly time with location
session.Engine.logger.Debugf("empty zone key[%v] : %v | zone: %v | location: %+v\n", key, t, z, *t.Location())
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(),
t.Minute(), t.Second(), t.Nanosecond(), dbTZ)
}
// !nashtsai! convert to engine location
if col.TimeZone == nil {
t = t.In(session.Engine.TZLocation)
@ -3011,6 +3024,9 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val
if err != nil {
return 0, err
}
if col.SQLType.IsBlob() {
return data, nil
}
return string(data), nil
}
}
@ -3020,6 +3036,9 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val
if err != nil {
return 0, err
}
if col.SQLType.IsBlob() {
return data, nil
}
return string(data), nil
}

View File

@ -497,7 +497,7 @@ func buildConds(engine *Engine, table *core.Table, bean interface{},
continue
}
if engine.dialect.DBType() == core.MSSQL && col.SQLType.Name == core.Text {
if engine.dialect.DBType() == core.MSSQL && (col.SQLType.Name == core.Text || col.SQLType.IsBlob() || col.SQLType.Name == core.TimeStampz) {
continue
}
if col.SQLType.IsJson() {
@ -522,8 +522,12 @@ func buildConds(engine *Engine, table *core.Table, bean interface{},
}
if col.IsDeleted && !unscoped { // tag "deleted" is enabled
if engine.dialect.DBType() == core.MSSQL {
conds = append(conds, builder.IsNull{colName})
} else {
conds = append(conds, builder.IsNull{colName}.Or(builder.Eq{colName: "0001-01-01 00:00:00"}))
}
}
fieldValue := *fieldValuePtr
if fieldValue.Interface() == nil {
@ -1316,7 +1320,12 @@ func (statement *Statement) convertIDSQL(sqlStr string) string {
return ""
}
return fmt.Sprintf("SELECT %s FROM %v", colstrs, sqls[1])
var top string
if statement.LimitN > 0 && statement.Engine.dialect.DBType() == core.MSSQL {
top = fmt.Sprintf("TOP %d ", statement.LimitN)
}
return fmt.Sprintf("SELECT %s%s FROM %v", top, colstrs, sqls[1])
}
return ""
}