Merge branch 'master' into feature/mysql-reserved
This commit is contained in:
commit
af65b069ae
|
@ -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{}
|
||||||
}
|
}
|
||||||
|
|
|
@ -399,6 +399,9 @@ func (db *mysql) AddColumnSQL(tableName string, col *schemas.Column) string {
|
||||||
// ModifyColumnSQL returns a SQL to modify SQL
|
// ModifyColumnSQL returns a SQL to modify SQL
|
||||||
func (db *mysql) ModifyColumnSQL(tableName string, col *schemas.Column) string {
|
func (db *mysql) ModifyColumnSQL(tableName string, col *schemas.Column) string {
|
||||||
s, _ := ColumnString(db.dialect, col, false, true)
|
s, _ := ColumnString(db.dialect, col, false, true)
|
||||||
|
if col.Comment != "" {
|
||||||
|
s += fmt.Sprintf(" COMMENT '%s'", col.Comment)
|
||||||
|
}
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -289,6 +289,48 @@ func TestGetColumnsComment(t *testing.T) {
|
||||||
assert.Zero(t, noComment)
|
assert.Zero(t, noComment)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TestCommentUpdate struct {
|
||||||
|
HasComment int `xorm:"bigint comment('this is a comment before update')"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *TestCommentUpdate) TableName() string {
|
||||||
|
return "test_comment_struct"
|
||||||
|
}
|
||||||
|
|
||||||
|
type TestCommentUpdate2 struct {
|
||||||
|
HasComment int `xorm:"bigint comment('this is a comment after update')"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *TestCommentUpdate2) TableName() string {
|
||||||
|
return "test_comment_struct"
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestColumnCommentUpdate(t *testing.T) {
|
||||||
|
comment := "this is a comment after update"
|
||||||
|
assertSync(t, new(TestCommentUpdate))
|
||||||
|
assert.NoError(t, testEngine.Sync2(new(TestCommentUpdate2))) // modify table column comment
|
||||||
|
|
||||||
|
switch testEngine.Dialect().URI().DBType {
|
||||||
|
case schemas.POSTGRES, schemas.MYSQL: // only postgres / mysql dialect implement the feature of modify comment in postgres.ModifyColumnSQL
|
||||||
|
default:
|
||||||
|
t.Skip()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tables, err := testEngine.DBMetas()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
tableName := "test_comment_struct"
|
||||||
|
var hasComment string
|
||||||
|
for _, table := range tables {
|
||||||
|
if table.Name == tableName {
|
||||||
|
col := table.GetColumn(testEngine.GetColumnMapper().Obj2Table("HasComment"))
|
||||||
|
assert.NotNil(t, col)
|
||||||
|
hasComment = col.Comment
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.Equal(t, comment, hasComment)
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetColumnsLength(t *testing.T) {
|
func TestGetColumnsLength(t *testing.T) {
|
||||||
var max_length int64
|
var max_length int64
|
||||||
switch testEngine.Dialect().URI().DBType {
|
switch testEngine.Dialect().URI().DBType {
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"xorm.io/xorm"
|
"xorm.io/xorm"
|
||||||
"xorm.io/xorm/internal/utils"
|
"xorm.io/xorm/internal/utils"
|
||||||
"xorm.io/xorm/names"
|
"xorm.io/xorm/names"
|
||||||
|
"xorm.io/xorm/schemas"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
@ -1196,3 +1197,43 @@ func TestUpdateFindDate(t *testing.T) {
|
||||||
assert.EqualValues(t, 1, len(tufs))
|
assert.EqualValues(t, 1, len(tufs))
|
||||||
assert.EqualValues(t, tuf.Tm.Format("2006-01-02"), tufs[0].Tm.Format("2006-01-02"))
|
assert.EqualValues(t, tuf.Tm.Format("2006-01-02"), tufs[0].Tm.Format("2006-01-02"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuilderDialect(t *testing.T) {
|
||||||
|
assert.NoError(t, PrepareEngine())
|
||||||
|
|
||||||
|
type TestBuilderDialect struct {
|
||||||
|
Id int64
|
||||||
|
Name string `xorm:"index"`
|
||||||
|
Age2 int
|
||||||
|
}
|
||||||
|
|
||||||
|
type TestBuilderDialectFoo struct {
|
||||||
|
Id int64
|
||||||
|
DialectId int64 `xorm:"index"`
|
||||||
|
Age int
|
||||||
|
}
|
||||||
|
|
||||||
|
assertSync(t, new(TestBuilderDialect), new(TestBuilderDialectFoo))
|
||||||
|
|
||||||
|
session := testEngine.NewSession()
|
||||||
|
defer session.Close()
|
||||||
|
|
||||||
|
var dialect string
|
||||||
|
switch testEngine.Dialect().URI().DBType {
|
||||||
|
case schemas.MYSQL:
|
||||||
|
dialect = builder.MYSQL
|
||||||
|
case schemas.MSSQL:
|
||||||
|
dialect = builder.MSSQL
|
||||||
|
case schemas.POSTGRES:
|
||||||
|
dialect = builder.POSTGRES
|
||||||
|
case schemas.SQLITE:
|
||||||
|
dialect = builder.SQLITE
|
||||||
|
}
|
||||||
|
|
||||||
|
tbName := testEngine.TableName(new(TestBuilderDialectFoo), dialect == builder.POSTGRES)
|
||||||
|
|
||||||
|
inner := builder.Dialect(dialect).Select("*").From(tbName).Where(builder.Eq{"age": 20})
|
||||||
|
result := make([]*TestBuilderDialect, 0, 10)
|
||||||
|
err := testEngine.Table("test_builder_dialect").Where(builder.Eq{"age2": 2}).Join("INNER", inner, "test_builder_dialect_foo.dialect_id = test_builder_dialect.id").Find(&result)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
|
@ -15,82 +15,97 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Join The joinOP should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
// Join The joinOP should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
||||||
func (statement *Statement) Join(joinOP string, tablename interface{}, condition interface{}, args ...interface{}) *Statement {
|
func (statement *Statement) Join(joinOP string, joinTable interface{}, condition interface{}, args ...interface{}) *Statement {
|
||||||
var buf strings.Builder
|
statement.joins = append(statement.joins, join{
|
||||||
if len(statement.JoinStr) > 0 {
|
op: joinOP,
|
||||||
fmt.Fprintf(&buf, "%v %v JOIN ", statement.JoinStr, joinOP)
|
table: joinTable,
|
||||||
} else {
|
condition: condition,
|
||||||
fmt.Fprintf(&buf, "%v JOIN ", joinOP)
|
args: args,
|
||||||
}
|
})
|
||||||
|
|
||||||
condStr := ""
|
|
||||||
condArgs := []interface{}{}
|
|
||||||
switch condTp := condition.(type) {
|
|
||||||
case string:
|
|
||||||
condStr = condTp
|
|
||||||
case builder.Cond:
|
|
||||||
var err error
|
|
||||||
condStr, condArgs, err = builder.ToSQL(condTp)
|
|
||||||
if err != nil {
|
|
||||||
statement.LastError = err
|
|
||||||
return statement
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
statement.LastError = fmt.Errorf("unsupported join condition type: %v", condTp)
|
|
||||||
return statement
|
|
||||||
}
|
|
||||||
|
|
||||||
switch tp := tablename.(type) {
|
|
||||||
case builder.Builder:
|
|
||||||
subSQL, subQueryArgs, err := tp.ToSQL()
|
|
||||||
if err != nil {
|
|
||||||
statement.LastError = err
|
|
||||||
return statement
|
|
||||||
}
|
|
||||||
|
|
||||||
fields := strings.Split(tp.TableName(), ".")
|
|
||||||
aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1])
|
|
||||||
aliasName = schemas.CommonQuoter.Trim(aliasName)
|
|
||||||
|
|
||||||
fmt.Fprintf(&buf, "(%s) %s ON %v", statement.ReplaceQuote(subSQL), statement.quote(aliasName), statement.ReplaceQuote(condStr))
|
|
||||||
statement.joinArgs = append(append(statement.joinArgs, subQueryArgs...), condArgs...)
|
|
||||||
case *builder.Builder:
|
|
||||||
subSQL, subQueryArgs, err := tp.ToSQL()
|
|
||||||
if err != nil {
|
|
||||||
statement.LastError = err
|
|
||||||
return statement
|
|
||||||
}
|
|
||||||
|
|
||||||
fields := strings.Split(tp.TableName(), ".")
|
|
||||||
aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1])
|
|
||||||
aliasName = schemas.CommonQuoter.Trim(aliasName)
|
|
||||||
|
|
||||||
fmt.Fprintf(&buf, "(%s) %s ON %v", statement.ReplaceQuote(subSQL), statement.quote(aliasName), statement.ReplaceQuote(condStr))
|
|
||||||
statement.joinArgs = append(append(statement.joinArgs, subQueryArgs...), condArgs...)
|
|
||||||
default:
|
|
||||||
tbName := dialects.FullTableName(statement.dialect, statement.tagParser.GetTableMapper(), tablename, true)
|
|
||||||
if !utils.IsSubQuery(tbName) {
|
|
||||||
var buf strings.Builder
|
|
||||||
_ = statement.dialect.Quoter().QuoteTo(&buf, tbName)
|
|
||||||
tbName = buf.String()
|
|
||||||
} else {
|
|
||||||
tbName = statement.ReplaceQuote(tbName)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(&buf, "%s ON %v", tbName, statement.ReplaceQuote(condStr))
|
|
||||||
statement.joinArgs = append(statement.joinArgs, condArgs...)
|
|
||||||
}
|
|
||||||
|
|
||||||
statement.JoinStr = buf.String()
|
|
||||||
statement.joinArgs = append(statement.joinArgs, args...)
|
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) writeJoin(w builder.Writer) error {
|
func (statement *Statement) writeJoins(w *builder.BytesWriter) error {
|
||||||
if statement.JoinStr != "" {
|
for _, join := range statement.joins {
|
||||||
if _, err := fmt.Fprint(w, " ", statement.JoinStr); err != nil {
|
if err := statement.writeJoin(w, join); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
w.Append(statement.joinArgs...)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (statement *Statement) writeJoin(buf *builder.BytesWriter, join join) error {
|
||||||
|
// write join operator
|
||||||
|
if _, err := fmt.Fprintf(buf, " %v JOIN", join.op); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// write join table or subquery
|
||||||
|
switch tp := join.table.(type) {
|
||||||
|
case builder.Builder:
|
||||||
|
if _, err := fmt.Fprintf(buf, " ("); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := tp.WriteTo(statement.QuoteReplacer(buf)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fields := strings.Split(tp.TableName(), ".")
|
||||||
|
aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1])
|
||||||
|
aliasName = schemas.CommonQuoter.Trim(aliasName)
|
||||||
|
|
||||||
|
if _, err := fmt.Fprintf(buf, ") %s", statement.quote(aliasName)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case *builder.Builder:
|
||||||
|
if _, err := fmt.Fprintf(buf, " ("); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := tp.WriteTo(statement.QuoteReplacer(buf)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fields := strings.Split(tp.TableName(), ".")
|
||||||
|
aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1])
|
||||||
|
aliasName = schemas.CommonQuoter.Trim(aliasName)
|
||||||
|
|
||||||
|
if _, err := fmt.Fprintf(buf, ") %s", statement.quote(aliasName)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
tbName := dialects.FullTableName(statement.dialect, statement.tagParser.GetTableMapper(), join.table, true)
|
||||||
|
if !utils.IsSubQuery(tbName) {
|
||||||
|
var sb strings.Builder
|
||||||
|
if err := statement.dialect.Quoter().QuoteTo(&sb, tbName); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tbName = sb.String()
|
||||||
|
} else {
|
||||||
|
tbName = statement.ReplaceQuote(tbName)
|
||||||
|
}
|
||||||
|
if _, err := fmt.Fprint(buf, " ", tbName); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// write on condition
|
||||||
|
if _, err := fmt.Fprint(buf, " ON "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch condTp := join.condition.(type) {
|
||||||
|
case string:
|
||||||
|
if _, err := fmt.Fprint(buf, statement.ReplaceQuote(condTp)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case builder.Cond:
|
||||||
|
if err := condTp.WriteTo(statement.QuoteReplacer(buf)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported join condition type: %v", condTp)
|
||||||
|
}
|
||||||
|
buf.Append(join.args...)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -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 statement.JoinStr == "" {
|
|
||||||
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
|
||||||
|
@ -108,7 +90,7 @@ func (statement *Statement) GenGetSQL(bean interface{}) (string, []interface{},
|
||||||
columnStr = statement.SelectStr
|
columnStr = statement.SelectStr
|
||||||
} else {
|
} else {
|
||||||
// TODO: always generate column names, not use * even if join
|
// TODO: always generate column names, not use * even if join
|
||||||
if len(statement.JoinStr) == 0 {
|
if len(statement.joins) == 0 {
|
||||||
if len(columnStr) == 0 {
|
if len(columnStr) == 0 {
|
||||||
if len(statement.GroupByStr) > 0 {
|
if len(statement.GroupByStr) > 0 {
|
||||||
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
||||||
|
@ -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,19 +160,27 @@ 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.Writer) error {
|
func (statement *Statement) writeFrom(w *builder.BytesWriter) error {
|
||||||
if _, err := fmt.Fprint(w, " FROM "); err != nil {
|
if _, err := fmt.Fprint(w, " FROM "); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -198,7 +190,7 @@ func (statement *Statement) writeFrom(w builder.Writer) error {
|
||||||
if err := statement.writeAlias(w); err != nil {
|
if err := statement.writeAlias(w); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return statement.writeJoin(w)
|
return statement.writeJoins(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) writeLimitOffset(w builder.Writer) error {
|
func (statement *Statement) writeLimitOffset(w builder.Writer) error {
|
||||||
|
@ -218,153 +210,183 @@ 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 {
|
if statement.RefTable == nil {
|
||||||
whereStr = " WHERE "
|
return errors.New("unsupported query limit without reference table")
|
||||||
}
|
}
|
||||||
|
|
||||||
pLimitN := statement.LimitN
|
var column string
|
||||||
if dialect.URI().DBType == schemas.MSSQL {
|
if len(statement.RefTable.PKColumns()) == 0 {
|
||||||
if pLimitN != nil {
|
for _, index := range statement.RefTable.Indexes {
|
||||||
LimitNValue := *pLimitN
|
if len(index.Cols) == 1 {
|
||||||
top = fmt.Sprintf("TOP %d ", LimitNValue)
|
column = index.Cols[0]
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if statement.Start > 0 {
|
if len(column) == 0 {
|
||||||
if statement.RefTable == nil {
|
column = statement.RefTable.ColumnsSeq()[0]
|
||||||
return "", nil, errors.New("Unsupported query limit without reference table")
|
}
|
||||||
}
|
} else {
|
||||||
var column string
|
column = statement.RefTable.PKColumns()[0].Name
|
||||||
if len(statement.RefTable.PKColumns()) == 0 {
|
}
|
||||||
for _, index := range statement.RefTable.Indexes {
|
if statement.NeedTableName() {
|
||||||
if len(index.Cols) == 1 {
|
if len(statement.TableAlias) > 0 {
|
||||||
column = index.Cols[0]
|
column = fmt.Sprintf("%s.%s", statement.TableAlias, column)
|
||||||
break
|
} else {
|
||||||
}
|
column = fmt.Sprintf("%s.%s", statement.TableName(), column)
|
||||||
}
|
|
||||||
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 = fmt.Sprintf("%s.%s", statement.TableAlias, column)
|
|
||||||
} else {
|
|
||||||
column = fmt.Sprintf("%s.%s", statement.TableName(), column)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(mssqlCondi, "(%s NOT IN (SELECT TOP %d %s",
|
|
||||||
column, statement.Start, column); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
if err := statement.writeFrom(mssqlCondi); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
if whereStr != "" {
|
|
||||||
if _, err := fmt.Fprint(mssqlCondi, whereStr); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
if err := utils.WriteBuilder(mssqlCondi, statement.QuoteReplacer(condWriter)); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if needOrderBy {
|
|
||||||
if err := statement.WriteOrderBy(mssqlCondi); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := statement.WriteGroupBy(mssqlCondi); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
if _, err := fmt.Fprint(mssqlCondi, "))"); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := builder.NewWriter()
|
subWriter := builder.NewWriter()
|
||||||
if _, err := fmt.Fprintf(buf, "SELECT %v%v%v", distinct, top, columnStr); err != nil {
|
if _, err := fmt.Fprintf(subWriter, "(%s NOT IN (SELECT TOP %d %s",
|
||||||
return "", nil, err
|
column, statement.Start, column); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := statement.writeFrom(subWriter); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if statement.cond.IsValid() {
|
||||||
|
if _, err := fmt.Fprint(subWriter, " WHERE "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := statement.cond.WriteTo(statement.QuoteReplacer(subWriter)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := statement.WriteOrderBy(subWriter); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := statement.writeGroupBy(subWriter); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := fmt.Fprint(subWriter, "))"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if statement.cond.IsValid() {
|
||||||
|
if _, err := fmt.Fprint(w, " AND "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if _, err := fmt.Fprint(w, " WHERE "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return utils.WriteBuilder(w, subWriter)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (statement *Statement) writeOracleLimit(w *builder.BytesWriter, columnStr string) error {
|
||||||
|
if statement.LimitN == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
oldString := w.String()
|
||||||
|
w.Reset()
|
||||||
|
rawColStr := columnStr
|
||||||
|
if rawColStr == "*" {
|
||||||
|
rawColStr = "at.*"
|
||||||
|
}
|
||||||
|
_, 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+*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 {
|
if err := statement.writeFrom(buf); err != nil {
|
||||||
return "", nil, err
|
return err
|
||||||
}
|
}
|
||||||
if whereStr != "" {
|
if err := statement.writeWhere(buf); err != nil {
|
||||||
if _, err := fmt.Fprint(buf, whereStr); err != nil {
|
return err
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
if err := utils.WriteBuilder(buf, statement.QuoteReplacer(condWriter)); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if mssqlCondi.Len() > 0 {
|
if err := statement.writeGroupBy(buf); err != nil {
|
||||||
if len(whereStr) > 0 {
|
return err
|
||||||
if _, err := fmt.Fprint(buf, " AND "); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if _, err := fmt.Fprint(buf, " WHERE "); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := utils.WriteBuilder(buf, mssqlCondi); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := statement.WriteGroupBy(buf); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
}
|
||||||
if err := statement.writeHaving(buf); err != nil {
|
if err := statement.writeHaving(buf); err != nil {
|
||||||
return "", nil, err
|
return err
|
||||||
}
|
}
|
||||||
if needOrderBy {
|
if err := statement.WriteOrderBy(buf); err != nil {
|
||||||
if err := statement.WriteOrderBy(buf); err != nil {
|
return err
|
||||||
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
|
|
||||||
if rawColStr == "*" {
|
|
||||||
rawColStr = "at.*"
|
|
||||||
}
|
|
||||||
fmt.Fprintf(buf, "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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if statement.IsForUpdate {
|
|
||||||
return dialect.ForUpdateSQL(buf.String()), buf.Args(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
@ -402,7 +424,7 @@ func (statement *Statement) GenExistSQL(bean ...interface{}) (string, []interfac
|
||||||
if _, err := fmt.Fprintf(buf, "SELECT TOP 1 * FROM %s", tableName); err != nil {
|
if _, err := fmt.Fprintf(buf, "SELECT TOP 1 * FROM %s", tableName); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if err := statement.writeJoin(buf); err != nil {
|
if err := statement.writeJoins(buf); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if statement.Conds().IsValid() {
|
if statement.Conds().IsValid() {
|
||||||
|
@ -417,7 +439,7 @@ func (statement *Statement) GenExistSQL(bean ...interface{}) (string, []interfac
|
||||||
if _, err := fmt.Fprintf(buf, "SELECT * FROM %s", tableName); err != nil {
|
if _, err := fmt.Fprintf(buf, "SELECT * FROM %s", tableName); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if err := statement.writeJoin(buf); err != nil {
|
if err := statement.writeJoins(buf); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if _, err := fmt.Fprintf(buf, " WHERE "); err != nil {
|
if _, err := fmt.Fprintf(buf, " WHERE "); err != nil {
|
||||||
|
@ -438,7 +460,7 @@ func (statement *Statement) GenExistSQL(bean ...interface{}) (string, []interfac
|
||||||
if _, err := fmt.Fprintf(buf, "SELECT 1 FROM %s", tableName); err != nil {
|
if _, err := fmt.Fprintf(buf, "SELECT 1 FROM %s", tableName); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if err := statement.writeJoin(buf); err != nil {
|
if err := statement.writeJoins(buf); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if statement.Conds().IsValid() {
|
if statement.Conds().IsValid() {
|
||||||
|
@ -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 statement.JoinStr == "" {
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -102,7 +102,7 @@ func (statement *Statement) genColumnStr() string {
|
||||||
buf.WriteString(", ")
|
buf.WriteString(", ")
|
||||||
}
|
}
|
||||||
|
|
||||||
if statement.JoinStr != "" {
|
if len(statement.joins) > 0 {
|
||||||
if statement.TableAlias != "" {
|
if statement.TableAlias != "" {
|
||||||
buf.WriteString(statement.TableAlias)
|
buf.WriteString(statement.TableAlias)
|
||||||
} else {
|
} else {
|
||||||
|
@ -119,7 +119,7 @@ func (statement *Statement) genColumnStr() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) colName(col *schemas.Column, tableName string) string {
|
func (statement *Statement) colName(col *schemas.Column, tableName string) string {
|
||||||
if statement.needTableName() {
|
if statement.NeedTableName() {
|
||||||
nm := tableName
|
nm := tableName
|
||||||
if len(statement.TableAlias) > 0 {
|
if len(statement.TableAlias) > 0 {
|
||||||
nm = statement.TableAlias
|
nm = statement.TableAlias
|
||||||
|
|
|
@ -34,6 +34,13 @@ var (
|
||||||
ErrTableNotFound = errors.New("Table not found")
|
ErrTableNotFound = errors.New("Table not found")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type join struct {
|
||||||
|
op string
|
||||||
|
table interface{}
|
||||||
|
condition interface{}
|
||||||
|
args []interface{}
|
||||||
|
}
|
||||||
|
|
||||||
// Statement save all the sql info for executing SQL
|
// Statement save all the sql info for executing SQL
|
||||||
type Statement struct {
|
type Statement struct {
|
||||||
RefTable *schemas.Table
|
RefTable *schemas.Table
|
||||||
|
@ -45,8 +52,7 @@ type Statement struct {
|
||||||
idParam schemas.PK
|
idParam schemas.PK
|
||||||
orderStr string
|
orderStr string
|
||||||
orderArgs []interface{}
|
orderArgs []interface{}
|
||||||
JoinStr string
|
joins []join
|
||||||
joinArgs []interface{}
|
|
||||||
GroupByStr string
|
GroupByStr string
|
||||||
HavingStr string
|
HavingStr string
|
||||||
SelectStr string
|
SelectStr string
|
||||||
|
@ -123,8 +129,7 @@ func (statement *Statement) Reset() {
|
||||||
statement.LimitN = nil
|
statement.LimitN = nil
|
||||||
statement.ResetOrderBy()
|
statement.ResetOrderBy()
|
||||||
statement.UseCascade = true
|
statement.UseCascade = true
|
||||||
statement.JoinStr = ""
|
statement.joins = nil
|
||||||
statement.joinArgs = make([]interface{}, 0)
|
|
||||||
statement.GroupByStr = ""
|
statement.GroupByStr = ""
|
||||||
statement.HavingStr = ""
|
statement.HavingStr = ""
|
||||||
statement.ColumnMap = columnMap{}
|
statement.ColumnMap = columnMap{}
|
||||||
|
@ -205,8 +210,8 @@ func (statement *Statement) SetRefBean(bean interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) needTableName() bool {
|
func (statement *Statement) NeedTableName() bool {
|
||||||
return len(statement.JoinStr) > 0
|
return len(statement.joins) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Incr Generate "Update ... Set column = column + arg" statement
|
// Incr Generate "Update ... Set column = column + arg" statement
|
||||||
|
@ -290,7 +295,7 @@ func (statement *Statement) GroupBy(keys string) *Statement {
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) WriteGroupBy(w builder.Writer) error {
|
func (statement *Statement) writeGroupBy(w builder.Writer) error {
|
||||||
if statement.GroupByStr == "" {
|
if statement.GroupByStr == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -605,7 +610,7 @@ func (statement *Statement) BuildConds(table *schemas.Table, bean interface{}, i
|
||||||
// MergeConds merge conditions from bean and id
|
// MergeConds merge conditions from bean and id
|
||||||
func (statement *Statement) MergeConds(bean interface{}) error {
|
func (statement *Statement) MergeConds(bean interface{}) error {
|
||||||
if !statement.NoAutoCondition && statement.RefTable != nil {
|
if !statement.NoAutoCondition && statement.RefTable != nil {
|
||||||
addedTableName := (len(statement.JoinStr) > 0)
|
addedTableName := (len(statement.joins) > 0)
|
||||||
autoCond, err := statement.BuildConds(statement.RefTable, bean, true, true, false, true, addedTableName)
|
autoCond, err := statement.BuildConds(statement.RefTable, bean, true, true, false, true, addedTableName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -673,7 +678,7 @@ func (statement *Statement) joinColumns(cols []*schemas.Column, includeTableName
|
||||||
// CondDeleted returns the conditions whether a record is soft deleted.
|
// CondDeleted returns the conditions whether a record is soft deleted.
|
||||||
func (statement *Statement) CondDeleted(col *schemas.Column) builder.Cond {
|
func (statement *Statement) CondDeleted(col *schemas.Column) builder.Cond {
|
||||||
colName := statement.quote(col.Name)
|
colName := statement.quote(col.Name)
|
||||||
if statement.JoinStr != "" {
|
if len(statement.joins) > 0 {
|
||||||
var prefix string
|
var prefix string
|
||||||
if statement.TableAlias != "" {
|
if statement.TableAlias != "" {
|
||||||
prefix = statement.TableAlias
|
prefix = statement.TableAlias
|
||||||
|
|
|
@ -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,
|
||||||
|
|
10
rows.go
10
rows.go
|
@ -46,8 +46,8 @@ func newRows(session *Session, bean interface{}) (*Rows, error) {
|
||||||
|
|
||||||
if rows.session.statement.RawSQL == "" {
|
if rows.session.statement.RawSQL == "" {
|
||||||
var autoCond builder.Cond
|
var autoCond builder.Cond
|
||||||
var addedTableName = (len(session.statement.JoinStr) > 0)
|
addedTableName := session.statement.NeedTableName()
|
||||||
var table = rows.session.statement.RefTable
|
table := rows.session.statement.RefTable
|
||||||
|
|
||||||
if !session.statement.NoAutoCondition {
|
if !session.statement.NoAutoCondition {
|
||||||
var err error
|
var err error
|
||||||
|
@ -103,12 +103,12 @@ func (rows *Rows) Scan(beans ...interface{}) error {
|
||||||
return rows.Err()
|
return rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
var bean = beans[0]
|
bean := beans[0]
|
||||||
var tp = reflect.TypeOf(bean)
|
tp := reflect.TypeOf(bean)
|
||||||
if tp.Kind() == reflect.Ptr {
|
if tp.Kind() == reflect.Ptr {
|
||||||
tp = tp.Elem()
|
tp = tp.Elem()
|
||||||
}
|
}
|
||||||
var beanKind = tp.Kind()
|
beanKind := tp.Kind()
|
||||||
|
|
||||||
if len(beans) == 1 {
|
if len(beans) == 1 {
|
||||||
if reflect.Indirect(reflect.ValueOf(bean)).Type() != rows.beanType {
|
if reflect.Indirect(reflect.ValueOf(bean)).Type() != rows.beanType {
|
||||||
|
|
|
@ -354,7 +354,7 @@ func (session *Session) DB() *core.DB {
|
||||||
|
|
||||||
func (session *Session) canCache() bool {
|
func (session *Session) canCache() bool {
|
||||||
if session.statement.RefTable == nil ||
|
if session.statement.RefTable == nil ||
|
||||||
session.statement.JoinStr != "" ||
|
session.statement.NeedTableName() ||
|
||||||
session.statement.RawSQL != "" ||
|
session.statement.RawSQL != "" ||
|
||||||
!session.statement.UseCache ||
|
!session.statement.UseCache ||
|
||||||
session.statement.IsForUpdate ||
|
session.statement.IsForUpdate ||
|
||||||
|
|
|
@ -114,7 +114,7 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
|
|
||||||
var (
|
var (
|
||||||
table = session.statement.RefTable
|
table = session.statement.RefTable
|
||||||
addedTableName = (len(session.statement.JoinStr) > 0)
|
addedTableName = session.statement.NeedTableName()
|
||||||
autoCond builder.Cond
|
autoCond builder.Cond
|
||||||
)
|
)
|
||||||
if tp == tpStruct {
|
if tp == tpStruct {
|
||||||
|
|
5
sync.go
5
sync.go
|
@ -181,7 +181,10 @@ func (session *Session) SyncWithOptions(opts SyncOptions, beans ...interface{})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if col.Comment != oriCol.Comment {
|
} else if col.Comment != oriCol.Comment {
|
||||||
_, err = session.exec(engine.dialect.ModifyColumnSQL(tbNameWithSchema, col))
|
if engine.dialect.URI().DBType == schemas.POSTGRES ||
|
||||||
|
engine.dialect.URI().DBType == schemas.MYSQL {
|
||||||
|
_, err = session.exec(engine.dialect.ModifyColumnSQL(tbNameWithSchema, col))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if col.Default != oriCol.Default {
|
if col.Default != oriCol.Default {
|
||||||
|
|
Loading…
Reference in New Issue