Some refactor (#2292)
replace #2285 Reviewed-on: https://gitea.com/xorm/xorm/pulls/2292
This commit is contained in:
parent
79a8bc804b
commit
f1f5e7cd1a
|
@ -85,8 +85,6 @@ type Dialect interface {
|
||||||
AddColumnSQL(tableName string, col *schemas.Column) string
|
AddColumnSQL(tableName string, col *schemas.Column) string
|
||||||
ModifyColumnSQL(tableName string, col *schemas.Column) string
|
ModifyColumnSQL(tableName string, col *schemas.Column) string
|
||||||
|
|
||||||
ForUpdateSQL(query string) string
|
|
||||||
|
|
||||||
Filters() []Filter
|
Filters() []Filter
|
||||||
SetParams(params map[string]string)
|
SetParams(params map[string]string)
|
||||||
}
|
}
|
||||||
|
@ -245,11 +243,6 @@ func (db *Base) ModifyColumnSQL(tableName string, col *schemas.Column) string {
|
||||||
return fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s", db.quoter.Quote(tableName), s)
|
return fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s", db.quoter.Quote(tableName), s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ForUpdateSQL returns for updateSQL
|
|
||||||
func (db *Base) ForUpdateSQL(query string) string {
|
|
||||||
return query + " FOR UPDATE"
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetParams set params
|
// SetParams set params
|
||||||
func (db *Base) SetParams(params map[string]string) {
|
func (db *Base) SetParams(params map[string]string) {
|
||||||
}
|
}
|
||||||
|
|
|
@ -669,10 +669,6 @@ func (db *mssql) CreateTableSQL(ctx context.Context, queryer core.Queryer, table
|
||||||
return b.String(), true, nil
|
return b.String(), true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *mssql) ForUpdateSQL(query string) string {
|
|
||||||
return query
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *mssql) Filters() []Filter {
|
func (db *mssql) Filters() []Filter {
|
||||||
return []Filter{}
|
return []Filter{}
|
||||||
}
|
}
|
||||||
|
|
|
@ -193,11 +193,11 @@ func (db *sqlite3) Features() *DialectFeatures {
|
||||||
func (db *sqlite3) SetQuotePolicy(quotePolicy QuotePolicy) {
|
func (db *sqlite3) SetQuotePolicy(quotePolicy QuotePolicy) {
|
||||||
switch quotePolicy {
|
switch quotePolicy {
|
||||||
case QuotePolicyNone:
|
case QuotePolicyNone:
|
||||||
var q = sqlite3Quoter
|
q := sqlite3Quoter
|
||||||
q.IsReserved = schemas.AlwaysNoReserve
|
q.IsReserved = schemas.AlwaysNoReserve
|
||||||
db.quoter = q
|
db.quoter = q
|
||||||
case QuotePolicyReserved:
|
case QuotePolicyReserved:
|
||||||
var q = sqlite3Quoter
|
q := sqlite3Quoter
|
||||||
q.IsReserved = db.IsReserved
|
q.IsReserved = db.IsReserved
|
||||||
db.quoter = q
|
db.quoter = q
|
||||||
case QuotePolicyAlways:
|
case QuotePolicyAlways:
|
||||||
|
@ -291,10 +291,6 @@ func (db *sqlite3) DropIndexSQL(tableName string, index *schemas.Index) string {
|
||||||
return fmt.Sprintf("DROP INDEX %v", db.Quoter().Quote(idxName))
|
return fmt.Sprintf("DROP INDEX %v", db.Quoter().Quote(idxName))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *sqlite3) ForUpdateSQL(query string) string {
|
|
||||||
return query
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *sqlite3) IsColumnExist(queryer core.Queryer, ctx context.Context, tableName, colName string) (bool, error) {
|
func (db *sqlite3) IsColumnExist(queryer core.Queryer, ctx context.Context, tableName, colName string) (bool, error) {
|
||||||
query := "SELECT * FROM " + tableName + " LIMIT 0"
|
query := "SELECT * FROM " + tableName + " LIMIT 0"
|
||||||
rows, err := queryer.QueryContext(ctx, query)
|
rows, err := queryer.QueryContext(ctx, query)
|
||||||
|
@ -320,7 +316,7 @@ func (db *sqlite3) IsColumnExist(queryer core.Queryer, ctx context.Context, tabl
|
||||||
// splitColStr splits a sqlite col strings as fields
|
// splitColStr splits a sqlite col strings as fields
|
||||||
func splitColStr(colStr string) []string {
|
func splitColStr(colStr string) []string {
|
||||||
colStr = strings.TrimSpace(colStr)
|
colStr = strings.TrimSpace(colStr)
|
||||||
var results = make([]string, 0, 10)
|
results := make([]string, 0, 10)
|
||||||
var lastIdx int
|
var lastIdx int
|
||||||
var hasC, hasQuote bool
|
var hasC, hasQuote bool
|
||||||
for i, c := range colStr {
|
for i, c := range colStr {
|
||||||
|
|
|
@ -89,7 +89,7 @@ func TestCountWithOthers(t *testing.T) {
|
||||||
})
|
})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
total, err := testEngine.OrderBy("`id` desc").Limit(1).Count(new(CountWithOthers))
|
total, err := testEngine.OrderBy("count(`id`) desc").Limit(1).Count(new(CountWithOthers))
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, 2, total)
|
assert.EqualValues(t, 2, total)
|
||||||
}
|
}
|
||||||
|
@ -118,11 +118,11 @@ func TestWithTableName(t *testing.T) {
|
||||||
})
|
})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
total, err := testEngine.OrderBy("`id` desc").Count(new(CountWithTableName))
|
total, err := testEngine.OrderBy("count(`id`) desc").Count(new(CountWithTableName))
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, 2, total)
|
assert.EqualValues(t, 2, total)
|
||||||
|
|
||||||
total, err = testEngine.OrderBy("`id` desc").Count(CountWithTableName{})
|
total, err = testEngine.OrderBy("count(`id`) desc").Count(CountWithTableName{})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, 2, total)
|
assert.EqualValues(t, 2, total)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ package statements
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -29,37 +30,15 @@ func (statement *Statement) GenQuerySQL(sqlOrArgs ...interface{}) (string, []int
|
||||||
return "", nil, ErrTableNotFound
|
return "", nil, ErrTableNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
columnStr := statement.ColumnStr()
|
|
||||||
if len(statement.SelectStr) > 0 {
|
|
||||||
columnStr = statement.SelectStr
|
|
||||||
} else {
|
|
||||||
if len(statement.joins) == 0 {
|
|
||||||
if columnStr == "" {
|
|
||||||
if statement.GroupByStr != "" {
|
|
||||||
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
|
||||||
} else {
|
|
||||||
columnStr = statement.genColumnStr()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if columnStr == "" {
|
|
||||||
if statement.GroupByStr != "" {
|
|
||||||
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
|
||||||
} else {
|
|
||||||
columnStr = "*"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if columnStr == "" {
|
|
||||||
columnStr = "*"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := statement.ProcessIDParam(); err != nil {
|
if err := statement.ProcessIDParam(); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return statement.genSelectSQL(columnStr, true, true)
|
buf := builder.NewWriter()
|
||||||
|
if err := statement.writeSelect(buf, statement.genSelectColumnStr(), true); err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
return buf.String(), buf.Args(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenSumSQL generates sum SQL
|
// GenSumSQL generates sum SQL
|
||||||
|
@ -81,13 +60,16 @@ func (statement *Statement) GenSumSQL(bean interface{}, columns ...string) (stri
|
||||||
}
|
}
|
||||||
sumStrs = append(sumStrs, fmt.Sprintf("COALESCE(sum(%s),0)", colName))
|
sumStrs = append(sumStrs, fmt.Sprintf("COALESCE(sum(%s),0)", colName))
|
||||||
}
|
}
|
||||||
sumSelect := strings.Join(sumStrs, ", ")
|
|
||||||
|
|
||||||
if err := statement.MergeConds(bean); err != nil {
|
if err := statement.MergeConds(bean); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return statement.genSelectSQL(sumSelect, true, true)
|
buf := builder.NewWriter()
|
||||||
|
if err := statement.writeSelect(buf, strings.Join(sumStrs, ", "), true); err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
return buf.String(), buf.Args(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenGetSQL generates Get SQL
|
// GenGetSQL generates Get SQL
|
||||||
|
@ -139,7 +121,11 @@ func (statement *Statement) GenGetSQL(bean interface{}) (string, []interface{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return statement.genSelectSQL(columnStr, true, true)
|
buf := builder.NewWriter()
|
||||||
|
if err := statement.writeSelect(buf, columnStr, true); err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
return buf.String(), buf.Args(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenCountSQL generates the SQL for counting
|
// GenCountSQL generates the SQL for counting
|
||||||
|
@ -148,8 +134,6 @@ func (statement *Statement) GenCountSQL(beans ...interface{}) (string, []interfa
|
||||||
return statement.GenRawSQL(), statement.RawParams, nil
|
return statement.GenRawSQL(), statement.RawParams, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var condArgs []interface{}
|
|
||||||
var err error
|
|
||||||
if len(beans) > 0 {
|
if len(beans) > 0 {
|
||||||
if err := statement.SetRefBean(beans[0]); err != nil {
|
if err := statement.SetRefBean(beans[0]); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
|
@ -176,16 +160,24 @@ func (statement *Statement) GenCountSQL(beans ...interface{}) (string, []interfa
|
||||||
subQuerySelect = selectSQL
|
subQuerySelect = selectSQL
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlStr, condArgs, err := statement.genSelectSQL(subQuerySelect, false, false)
|
buf := builder.NewWriter()
|
||||||
if err != nil {
|
if statement.GroupByStr != "" {
|
||||||
|
if _, err := fmt.Fprintf(buf, "SELECT %s FROM (", selectSQL); err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := statement.writeSelect(buf, subQuerySelect, false); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if statement.GroupByStr != "" {
|
if statement.GroupByStr != "" {
|
||||||
sqlStr = fmt.Sprintf("SELECT %s FROM (%s) sub", selectSQL, sqlStr)
|
if _, err := fmt.Fprintf(buf, ") sub"); err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return sqlStr, condArgs, nil
|
return buf.String(), buf.Args(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) writeFrom(w *builder.BytesWriter) error {
|
func (statement *Statement) writeFrom(w *builder.BytesWriter) error {
|
||||||
|
@ -218,37 +210,73 @@ func (statement *Statement) writeLimitOffset(w builder.Writer) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) genSelectSQL(columnStr string, needLimit, needOrderBy bool) (string, []interface{}, error) {
|
func (statement *Statement) writeTop(w builder.Writer) error {
|
||||||
var (
|
if statement.dialect.URI().DBType != schemas.MSSQL {
|
||||||
distinct string
|
return nil
|
||||||
dialect = statement.dialect
|
}
|
||||||
top, whereStr string
|
if statement.LimitN == nil {
|
||||||
mssqlCondi = builder.NewWriter()
|
return nil
|
||||||
)
|
}
|
||||||
|
_, err := fmt.Fprintf(w, " TOP %d", *statement.LimitN)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if statement.IsDistinct && !strings.HasPrefix(columnStr, "count") {
|
func (statement *Statement) writeDistinct(w builder.Writer) error {
|
||||||
distinct = "DISTINCT "
|
if statement.IsDistinct && !strings.HasPrefix(statement.SelectStr, "count(") {
|
||||||
|
_, err := fmt.Fprint(w, " DISTINCT")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (statement *Statement) writeSelectColumns(w *builder.BytesWriter, columnStr string) error {
|
||||||
|
if _, err := fmt.Fprintf(w, "SELECT "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := statement.writeDistinct(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := statement.writeTop(w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err := fmt.Fprint(w, " ", columnStr)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (statement *Statement) writeWhere(w *builder.BytesWriter) error {
|
||||||
|
if !statement.cond.IsValid() {
|
||||||
|
return statement.writeMssqlPaginationCond(w)
|
||||||
|
}
|
||||||
|
if _, err := fmt.Fprint(w, " WHERE "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := statement.cond.WriteTo(statement.QuoteReplacer(w)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return statement.writeMssqlPaginationCond(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (statement *Statement) writeForUpdate(w io.Writer) error {
|
||||||
|
if !statement.IsForUpdate {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
condWriter := builder.NewWriter()
|
if statement.dialect.URI().DBType != schemas.MYSQL {
|
||||||
if err := statement.cond.WriteTo(statement.QuoteReplacer(condWriter)); err != nil {
|
return errors.New("only support mysql for update")
|
||||||
return "", nil, err
|
}
|
||||||
|
_, err := fmt.Fprint(w, " FOR UPDATE")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (statement *Statement) writeMssqlPaginationCond(w *builder.BytesWriter) error {
|
||||||
|
if statement.dialect.URI().DBType != schemas.MSSQL || statement.Start <= 0 {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if condWriter.Len() > 0 {
|
|
||||||
whereStr = " WHERE "
|
|
||||||
}
|
|
||||||
|
|
||||||
pLimitN := statement.LimitN
|
|
||||||
if dialect.URI().DBType == schemas.MSSQL {
|
|
||||||
if pLimitN != nil {
|
|
||||||
LimitNValue := *pLimitN
|
|
||||||
top = fmt.Sprintf("TOP %d ", LimitNValue)
|
|
||||||
}
|
|
||||||
if statement.Start > 0 {
|
|
||||||
if statement.RefTable == nil {
|
if statement.RefTable == nil {
|
||||||
return "", nil, errors.New("Unsupported query limit without reference table")
|
return errors.New("unsupported query limit without reference table")
|
||||||
}
|
}
|
||||||
|
|
||||||
var column string
|
var column string
|
||||||
if len(statement.RefTable.PKColumns()) == 0 {
|
if len(statement.RefTable.PKColumns()) == 0 {
|
||||||
for _, index := range statement.RefTable.Indexes {
|
for _, index := range statement.RefTable.Indexes {
|
||||||
|
@ -271,100 +299,94 @@ func (statement *Statement) genSelectSQL(columnStr string, needLimit, needOrderB
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(mssqlCondi, "(%s NOT IN (SELECT TOP %d %s",
|
subWriter := builder.NewWriter()
|
||||||
|
if _, err := fmt.Fprintf(subWriter, "(%s NOT IN (SELECT TOP %d %s",
|
||||||
column, statement.Start, column); err != nil {
|
column, statement.Start, column); err != nil {
|
||||||
return "", nil, err
|
return err
|
||||||
}
|
}
|
||||||
if err := statement.writeFrom(mssqlCondi); err != nil {
|
if err := statement.writeFrom(subWriter); err != nil {
|
||||||
return "", nil, err
|
return err
|
||||||
}
|
}
|
||||||
if whereStr != "" {
|
if statement.cond.IsValid() {
|
||||||
if _, err := fmt.Fprint(mssqlCondi, whereStr); err != nil {
|
if _, err := fmt.Fprint(subWriter, " WHERE "); err != nil {
|
||||||
return "", nil, err
|
return err
|
||||||
}
|
}
|
||||||
if err := utils.WriteBuilder(mssqlCondi, statement.QuoteReplacer(condWriter)); err != nil {
|
if err := statement.cond.WriteTo(statement.QuoteReplacer(subWriter)); err != nil {
|
||||||
return "", nil, err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if needOrderBy {
|
if err := statement.WriteOrderBy(subWriter); err != nil {
|
||||||
if err := statement.WriteOrderBy(mssqlCondi); err != nil {
|
return err
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := statement.writeGroupBy(mssqlCondi); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
if _, err := fmt.Fprint(mssqlCondi, "))"); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
}
|
||||||
|
if err := statement.writeGroupBy(subWriter); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
if _, err := fmt.Fprint(subWriter, "))"); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := builder.NewWriter()
|
if statement.cond.IsValid() {
|
||||||
if _, err := fmt.Fprintf(buf, "SELECT %v%v%v", distinct, top, columnStr); err != nil {
|
if _, err := fmt.Fprint(w, " AND "); err != nil {
|
||||||
return "", nil, err
|
return err
|
||||||
}
|
|
||||||
if err := statement.writeFrom(buf); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
if whereStr != "" {
|
|
||||||
if _, err := fmt.Fprint(buf, whereStr); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
if err := utils.WriteBuilder(buf, statement.QuoteReplacer(condWriter)); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if mssqlCondi.Len() > 0 {
|
|
||||||
if len(whereStr) > 0 {
|
|
||||||
if _, err := fmt.Fprint(buf, " AND "); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if _, err := fmt.Fprint(buf, " WHERE "); err != nil {
|
if _, err := fmt.Fprint(w, " WHERE "); err != nil {
|
||||||
return "", nil, err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := utils.WriteBuilder(buf, mssqlCondi); err != nil {
|
return utils.WriteBuilder(w, subWriter)
|
||||||
return "", nil, err
|
}
|
||||||
}
|
|
||||||
|
func (statement *Statement) writeOracleLimit(w *builder.BytesWriter, columnStr string) error {
|
||||||
|
if statement.LimitN == nil {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := statement.writeGroupBy(buf); err != nil {
|
oldString := w.String()
|
||||||
return "", nil, err
|
w.Reset()
|
||||||
}
|
|
||||||
if err := statement.writeHaving(buf); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
if needOrderBy {
|
|
||||||
if err := statement.WriteOrderBy(buf); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if needLimit {
|
|
||||||
if dialect.URI().DBType != schemas.MSSQL && dialect.URI().DBType != schemas.ORACLE {
|
|
||||||
if err := statement.writeLimitOffset(buf); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
} else if dialect.URI().DBType == schemas.ORACLE {
|
|
||||||
if pLimitN != nil {
|
|
||||||
oldString := buf.String()
|
|
||||||
buf.Reset()
|
|
||||||
rawColStr := columnStr
|
rawColStr := columnStr
|
||||||
if rawColStr == "*" {
|
if rawColStr == "*" {
|
||||||
rawColStr = "at.*"
|
rawColStr = "at.*"
|
||||||
}
|
}
|
||||||
fmt.Fprintf(buf, "SELECT %v FROM (SELECT %v,ROWNUM RN FROM (%v) at WHERE ROWNUM <= %d) aat WHERE RN > %d",
|
_, err := fmt.Fprintf(w, "SELECT %v FROM (SELECT %v,ROWNUM RN FROM (%v) at WHERE ROWNUM <= %d) aat WHERE RN > %d",
|
||||||
columnStr, rawColStr, oldString, statement.Start+*pLimitN, statement.Start)
|
columnStr, rawColStr, oldString, statement.Start+*statement.LimitN, statement.Start)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (statement *Statement) writeSelect(buf *builder.BytesWriter, columnStr string, needLimit bool) error {
|
||||||
|
if err := statement.writeSelectColumns(buf, columnStr); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
if err := statement.writeFrom(buf); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
if err := statement.writeWhere(buf); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
if statement.IsForUpdate {
|
if err := statement.writeGroupBy(buf); err != nil {
|
||||||
return dialect.ForUpdateSQL(buf.String()), buf.Args(), nil
|
return err
|
||||||
|
}
|
||||||
|
if err := statement.writeHaving(buf); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := statement.WriteOrderBy(buf); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf.String(), buf.Args(), nil
|
dialect := statement.dialect
|
||||||
|
if needLimit {
|
||||||
|
if dialect.URI().DBType == schemas.ORACLE {
|
||||||
|
if err := statement.writeOracleLimit(buf, columnStr); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if dialect.URI().DBType != schemas.MSSQL {
|
||||||
|
if err := statement.writeLimitOffset(buf); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return statement.writeForUpdate(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenExistSQL generates Exist SQL
|
// GenExistSQL generates Exist SQL
|
||||||
|
@ -457,6 +479,33 @@ func (statement *Statement) GenExistSQL(bean ...interface{}) (string, []interfac
|
||||||
return buf.String(), buf.Args(), nil
|
return buf.String(), buf.Args(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (statement *Statement) genSelectColumnStr() string {
|
||||||
|
// manually select columns
|
||||||
|
if len(statement.SelectStr) > 0 {
|
||||||
|
return statement.SelectStr
|
||||||
|
}
|
||||||
|
|
||||||
|
columnStr := statement.ColumnStr()
|
||||||
|
if columnStr != "" {
|
||||||
|
return columnStr
|
||||||
|
}
|
||||||
|
|
||||||
|
// autodetect columns
|
||||||
|
if statement.GroupByStr != "" {
|
||||||
|
return statement.quoteColumnStr(statement.GroupByStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(statement.joins) != 0 {
|
||||||
|
return "*"
|
||||||
|
}
|
||||||
|
|
||||||
|
columnStr = statement.genColumnStr()
|
||||||
|
if columnStr == "" {
|
||||||
|
columnStr = "*"
|
||||||
|
}
|
||||||
|
return columnStr
|
||||||
|
}
|
||||||
|
|
||||||
// GenFindSQL generates Find SQL
|
// GenFindSQL generates Find SQL
|
||||||
func (statement *Statement) GenFindSQL(autoCond builder.Cond) (string, []interface{}, error) {
|
func (statement *Statement) GenFindSQL(autoCond builder.Cond) (string, []interface{}, error) {
|
||||||
if statement.RawSQL != "" {
|
if statement.RawSQL != "" {
|
||||||
|
@ -467,33 +516,11 @@ func (statement *Statement) GenFindSQL(autoCond builder.Cond) (string, []interfa
|
||||||
return "", nil, ErrTableNotFound
|
return "", nil, ErrTableNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
columnStr := statement.ColumnStr()
|
|
||||||
if len(statement.SelectStr) > 0 {
|
|
||||||
columnStr = statement.SelectStr
|
|
||||||
} else {
|
|
||||||
if len(statement.joins) == 0 {
|
|
||||||
if columnStr == "" {
|
|
||||||
if statement.GroupByStr != "" {
|
|
||||||
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
|
||||||
} else {
|
|
||||||
columnStr = statement.genColumnStr()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if columnStr == "" {
|
|
||||||
if statement.GroupByStr != "" {
|
|
||||||
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
|
||||||
} else {
|
|
||||||
columnStr = "*"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if columnStr == "" {
|
|
||||||
columnStr = "*"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
statement.cond = statement.cond.And(autoCond)
|
statement.cond = statement.cond.And(autoCond)
|
||||||
|
|
||||||
return statement.genSelectSQL(columnStr, true, true)
|
buf := builder.NewWriter()
|
||||||
|
if err := statement.writeSelect(buf, statement.genSelectColumnStr(), true); err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
return buf.String(), buf.Args(), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (statement *Statement) ifAddColUpdate(col *schemas.Column, includeVersion, includeUpdated, includeNil,
|
func (statement *Statement) ifAddColUpdate(col *schemas.Column, includeVersion, includeUpdated, includeNil,
|
||||||
includeAutoIncr, update bool) (bool, error) {
|
includeAutoIncr, update bool,
|
||||||
|
) (bool, error) {
|
||||||
columnMap := statement.ColumnMap
|
columnMap := statement.ColumnMap
|
||||||
omitColumnMap := statement.OmitColumnMap
|
omitColumnMap := statement.OmitColumnMap
|
||||||
unscoped := statement.unscoped
|
unscoped := statement.unscoped
|
||||||
|
@ -64,15 +65,16 @@ func (statement *Statement) ifAddColUpdate(col *schemas.Column, includeVersion,
|
||||||
// BuildUpdates auto generating update columnes and values according a struct
|
// BuildUpdates auto generating update columnes and values according a struct
|
||||||
func (statement *Statement) BuildUpdates(tableValue reflect.Value,
|
func (statement *Statement) BuildUpdates(tableValue reflect.Value,
|
||||||
includeVersion, includeUpdated, includeNil,
|
includeVersion, includeUpdated, includeNil,
|
||||||
includeAutoIncr, update bool) ([]string, []interface{}, error) {
|
includeAutoIncr, update bool,
|
||||||
|
) ([]string, []interface{}, error) {
|
||||||
table := statement.RefTable
|
table := statement.RefTable
|
||||||
allUseBool := statement.allUseBool
|
allUseBool := statement.allUseBool
|
||||||
useAllCols := statement.useAllCols
|
useAllCols := statement.useAllCols
|
||||||
mustColumnMap := statement.MustColumnMap
|
mustColumnMap := statement.MustColumnMap
|
||||||
nullableMap := statement.NullableMap
|
nullableMap := statement.NullableMap
|
||||||
|
|
||||||
var colNames = make([]string, 0)
|
colNames := make([]string, 0)
|
||||||
var args = make([]interface{}, 0)
|
args := make([]interface{}, 0)
|
||||||
|
|
||||||
for _, col := range table.Columns() {
|
for _, col := range table.Columns() {
|
||||||
ok, err := statement.ifAddColUpdate(col, includeVersion, includeUpdated, includeNil,
|
ok, err := statement.ifAddColUpdate(col, includeVersion, includeUpdated, includeNil,
|
||||||
|
|
Loading…
Reference in New Issue