some fixed for mssql support

This commit is contained in:
Lunny Xiao 2016-12-01 14:48:40 +08:00
parent 06a1b9da56
commit 7222d6d7b3
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.JoinStr != "" ||
session.Statement.RawSQL != "" || session.Statement.RawSQL != "" ||
!session.Statement.UseCache || !session.Statement.UseCache ||
session.Statement.IsForUpdate ||
session.Tx != nil || session.Tx != nil ||
len(session.Statement.selectStr) > 0 { len(session.Statement.selectStr) > 0 {
return false return false
@ -1332,9 +1333,13 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
} }
colName = session.Engine.Quote(nm) + "." + colName 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"}) autoCond = builder.IsNull{colName}.Or(builder.Eq{colName: "0001-01-01 00:00:00"})
} }
} }
}
var sqlStr string var sqlStr string
var args []interface{} var args []interface{}
@ -1839,16 +1844,24 @@ func (session *Session) _row2Bean(rows *core.Rows, fields []string, fieldsCount
hasAssigned = true hasAssigned = true
t := vv.Convert(core.TimeType).Interface().(time.Time) t := vv.Convert(core.TimeType).Interface().(time.Time)
z, _ := t.Zone() 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 dbTZ := session.Engine.DatabaseTZ
if dbTZ == nil { if dbTZ == nil {
if session.Engine.dialect.DBType() == core.SQLITE {
dbTZ = time.UTC
} else {
dbTZ = time.Local 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()) 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 = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(),
t.Minute(), t.Second(), t.Nanosecond(), dbTZ) t.Minute(), t.Second(), t.Nanosecond(), dbTZ)
} }
// !nashtsai! convert to engine location // !nashtsai! convert to engine location
if col.TimeZone == nil { if col.TimeZone == nil {
t = t.In(session.Engine.TZLocation) t = t.In(session.Engine.TZLocation)
@ -3011,6 +3024,9 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val
if err != nil { if err != nil {
return 0, err return 0, err
} }
if col.SQLType.IsBlob() {
return data, nil
}
return string(data), nil return string(data), nil
} }
} }
@ -3020,6 +3036,9 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val
if err != nil { if err != nil {
return 0, err return 0, err
} }
if col.SQLType.IsBlob() {
return data, nil
}
return string(data), nil return string(data), nil
} }

View File

@ -497,7 +497,7 @@ func buildConds(engine *Engine, table *core.Table, bean interface{},
continue 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 continue
} }
if col.SQLType.IsJson() { 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 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"})) conds = append(conds, builder.IsNull{colName}.Or(builder.Eq{colName: "0001-01-01 00:00:00"}))
} }
}
fieldValue := *fieldValuePtr fieldValue := *fieldValuePtr
if fieldValue.Interface() == nil { if fieldValue.Interface() == nil {
@ -1316,7 +1320,12 @@ func (statement *Statement) convertIDSQL(sqlStr string) string {
return "" 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 "" return ""
} }