Browse Source

bug fixed for mssql

xormplus 9 years ago
parent
commit
3b8d374a86
3 changed files with 22 additions and 5 deletions
  1. 1 1
      rows.go
  2. 19 2
      statement.go
  3. 2 2
      xorm.go

+ 1 - 1
rows.go

@@ -104,7 +104,7 @@ func (rows *Rows) Err() error {
 	return rows.lastError
 }
 
-// Scan row record to bean properties.
+// Scan row record to bean properties
 func (rows *Rows) Scan(bean interface{}) error {
 	if rows.lastError != nil {
 		return rows.lastError

+ 19 - 2
statement.go

@@ -1140,7 +1140,14 @@ func (statement *Statement) genCountSQL(bean interface{}) (string, []interface{}
 
 	condSQL, condArgs, _ := statement.genConds(bean)
 
-	return statement.genSelectSQL("count(*)", condSQL), append(statement.joinArgs, condArgs...)
+	var selectSql = statement.selectStr
+	if len(selectSql) <= 0 {
+		if statement.IsDistinct {
+			selectSql = fmt.Sprintf("count(DISTINCT %s)", statement.ColumnStr)
+		}
+		selectSql = "count(*)"
+	}
+	return statement.genSelectSQL(selectSql, condSQL), append(statement.joinArgs, condArgs...)
 }
 
 func (statement *Statement) genSumSQL(bean interface{}, columns ...string) (string, []interface{}) {
@@ -1192,7 +1199,7 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string) (a string) {
 			top = fmt.Sprintf(" TOP %d ", statement.LimitN)
 		}
 		if statement.Start > 0 {
-			var column = "(id)"
+			var column string
 			if len(statement.RefTable.PKColumns()) == 0 {
 				for _, index := range statement.RefTable.Indexes {
 					if len(index.Cols) == 1 {
@@ -1203,7 +1210,17 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string) (a string) {
 				if len(column) == 0 {
 					column = statement.RefTable.ColumnsSeq()[0]
 				}
+			} else {
+				column = statement.RefTable.PKColumns()[0].Name
 			}
+			if statement.needTableName() {
+				if len(statement.TableAlias) > 0 {
+					column = statement.TableAlias + "." + column
+				} else {
+					column = statement.TableName() + "." + column
+				}
+			}
+
 			var orderStr string
 			if len(statement.OrderStr) > 0 {
 				orderStr = " ORDER BY " + statement.OrderStr

+ 2 - 2
xorm.go

@@ -16,8 +16,8 @@ import (
 )
 
 const (
-	// Version show the xorm's version.
-	Version string = "0.6.0.0929"
+	// Version show the xorm's version
+	Version string = "0.6.0.1010"
 )
 
 func regDrvsNDialects() bool {