start & limit implementation for mssql
This commit is contained in:
parent
a190f71d40
commit
84b89289a3
53
statement.go
53
statement.go
|
@ -974,20 +974,52 @@ func (statement *Statement) genSelectSql(columnStr string) (a string) {
|
||||||
distinct = "DISTINCT "
|
distinct = "DISTINCT "
|
||||||
}
|
}
|
||||||
|
|
||||||
// !nashtsai! REVIEW Sprintf is considered slowest mean of string concatnation, better to work with builder pattern
|
var top string
|
||||||
a = fmt.Sprintf("SELECT %v%v FROM %v", distinct, columnStr,
|
var mssqlCondi string
|
||||||
statement.Engine.Quote(statement.TableName()))
|
var orderBy string
|
||||||
if statement.JoinStr != "" {
|
if statement.OrderStr != "" {
|
||||||
a = fmt.Sprintf("%v %v", a, statement.JoinStr)
|
orderBy = fmt.Sprintf(" ORDER BY %v", statement.OrderStr)
|
||||||
}
|
}
|
||||||
statement.processIdParam()
|
statement.processIdParam()
|
||||||
|
var whereStr string
|
||||||
if statement.WhereStr != "" {
|
if statement.WhereStr != "" {
|
||||||
a = fmt.Sprintf("%v WHERE %v", a, statement.WhereStr)
|
whereStr = fmt.Sprintf(" WHERE %v", statement.WhereStr)
|
||||||
if statement.ConditionStr != "" {
|
if statement.ConditionStr != "" {
|
||||||
a = fmt.Sprintf("%v AND %v", a, statement.ConditionStr)
|
whereStr = fmt.Sprintf("%v AND %v", whereStr, statement.ConditionStr)
|
||||||
}
|
}
|
||||||
} else if statement.ConditionStr != "" {
|
} else if statement.ConditionStr != "" {
|
||||||
a = fmt.Sprintf("%v WHERE %v", a, statement.ConditionStr)
|
whereStr = fmt.Sprintf(" WHERE %v", statement.ConditionStr)
|
||||||
|
}
|
||||||
|
var fromStr string = " FROM " + statement.Engine.Quote(statement.TableName())
|
||||||
|
if statement.JoinStr != "" {
|
||||||
|
fromStr = fmt.Sprintf("%v %v", fromStr, statement.JoinStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if statement.Engine.dialect.DBType() == core.MSSQL {
|
||||||
|
top = fmt.Sprintf(" TOP %d", statement.LimitN)
|
||||||
|
if statement.Start > 0 {
|
||||||
|
var column string = "(id)"
|
||||||
|
if len(statement.RefTable.PKColumns()) == 0 {
|
||||||
|
for _, index := range statement.RefTable.Indexes {
|
||||||
|
if len(index.Cols) == 1 {
|
||||||
|
column = index.Cols[0]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(column) == 0 {
|
||||||
|
column = statement.RefTable.ColumnsSeq()[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mssqlCondi = fmt.Sprintf("(%s NOT IN (SELECT TOP %d %s%s%s%s))",
|
||||||
|
column, statement.Start, column, fromStr, whereStr, orderBy)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// !nashtsai! REVIEW Sprintf is considered slowest mean of string concatnation, better to work with builder pattern
|
||||||
|
a = fmt.Sprintf("SELECT %v%v%v%v%v", top, distinct, columnStr,
|
||||||
|
fromStr, whereStr)
|
||||||
|
if mssqlCondi != "" {
|
||||||
|
a += " AND " + mssqlCondi
|
||||||
}
|
}
|
||||||
|
|
||||||
if statement.GroupByStr != "" {
|
if statement.GroupByStr != "" {
|
||||||
|
@ -1005,11 +1037,6 @@ func (statement *Statement) genSelectSql(columnStr string) (a string) {
|
||||||
} else if statement.LimitN > 0 {
|
} else if statement.LimitN > 0 {
|
||||||
a = fmt.Sprintf("%v LIMIT %v", a, statement.LimitN)
|
a = fmt.Sprintf("%v LIMIT %v", a, statement.LimitN)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
//TODO: for mssql, should handler limit.
|
|
||||||
/*SELECT * FROM (
|
|
||||||
SELECT *, ROW_NUMBER() OVER (ORDER BY id desc) as row FROM "userinfo"
|
|
||||||
) a WHERE row > [start] and row <= [start+limit] order by id desc*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue