From 06a1b9da56b549097e4beb135abc4f53f523700a Mon Sep 17 00:00:00 2001 From: David Lobmaier Date: Tue, 29 Nov 2016 16:47:05 +0100 Subject: [PATCH 1/2] Using varchar(max) instead of text in mssql. --- mssql_dialect.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mssql_dialect.go b/mssql_dialect.go index 023f4de8..0cfa93bf 100644 --- a/mssql_dialect.go +++ b/mssql_dialect.go @@ -242,8 +242,8 @@ func (db *mssql) SqlType(c *core.Column) string { c.Length = 7 case core.MediumInt: res = core.Int - case core.MediumText, core.TinyText, core.LongText, core.Json: - res = core.Text + case core.Text, core.MediumText, core.TinyText, core.LongText, core.Json: + res = core.Varchar + "(MAX)" case core.Double: res = core.Real case core.Uuid: From 7222d6d7b340c0c1f02117ee0c33bb1293f59f0f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 1 Dec 2016 14:48:40 +0800 Subject: [PATCH 2/2] some fixed for mssql support --- session.go | 27 +++++++++++++++++++++++---- statement.go | 15 ++++++++++++--- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/session.go b/session.go index b37fb5ee..f6f7c9c0 100644 --- a/session.go +++ b/session.go @@ -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,7 +1333,11 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{}) } colName = session.Engine.Quote(nm) + "." + colName } - autoCond = builder.IsNull{colName}.Or(builder.Eq{colName: "0001-01-01 00:00:00"}) + 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"}) + } } } @@ -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 { + 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 } diff --git a/statement.go b/statement.go index f0d3fb50..88488e93 100644 --- a/statement.go +++ b/statement.go @@ -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,7 +522,11 @@ func buildConds(engine *Engine, table *core.Table, bean interface{}, } if col.IsDeleted && !unscoped { // tag "deleted" is enabled - conds = append(conds, builder.IsNull{colName}.Or(builder.Eq{colName: "0001-01-01 00:00:00"})) + 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 @@ -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 "" }