diff --git a/chain.go b/chain.go index 5f96812..3ddeb8a 100644 --- a/chain.go +++ b/chain.go @@ -288,21 +288,6 @@ func (this *Query) BuildSelectSql() (map[string]interface{}, error) { return nil, errors.New("参数错误,没有数据表") } var table = "" - if strings.Contains(this.table, "select ") || strings.HasPrefix(this.table, "(") { - table = this.table - } else { - table = getTableName(this.dbname, this.table, this.dbtype) - } - - // var err error - - var sql, title string - - if this.title != "" { - title = this.title - } else { - title = "*" - } withSql := "" if len(this.with) > 0 { var builder strings.Builder @@ -326,6 +311,22 @@ func (this *Query) BuildSelectSql() (map[string]interface{}, error) { withSql = builder.String() } } + if withSql != "" || strings.Contains(this.table, "select ") || strings.HasPrefix(this.table, "(") { + table = this.table + } else { + table = getTableName(this.dbname, this.table, this.dbtype) + } + + // var err error + + var sql, title string + + if this.title != "" { + title = this.title + } else { + title = "*" + } + if this.dbtype == "mssql" { if this.page_size > 0 { sql = helper.StringJoin(withSql, "select top ", helper.ToStr(this.page_size), " ") @@ -362,7 +363,7 @@ func (this *Query) BuildSelectSql() (map[string]interface{}, error) { builder.WriteString("left") } builder.WriteString(" join ") - if strings.Contains(joinitem[0], "select ") || strings.HasPrefix(joinitem[0], "(") { + if withSql != "" || strings.Contains(joinitem[0], "select ") || strings.HasPrefix(joinitem[0], "(") { builder.WriteString(joinitem[0]) } else { builder.WriteString(getTableName(this.dbname, joinitem[0])) diff --git a/db.go b/db.go index e2acdd2..43c7340 100644 --- a/db.go +++ b/db.go @@ -304,18 +304,6 @@ func GetRow(dbName, table_name, alias string, titles string, with, join [][]stri } table := "" - if strings.Contains(table_name, "select ") || strings.HasPrefix(table, "(") { - table = table_name - } else { - table = getTableName(dbName, table_name) - } - var sql_str, title string - - if titles != "" { - title = titles - } else { - title = "*" - } withSql := "" if len(with) > 0 { var builder strings.Builder @@ -339,6 +327,19 @@ func GetRow(dbName, table_name, alias string, titles string, with, join [][]stri withSql = builder.String() } } + if withSql != "" || strings.Contains(table_name, "select ") || strings.HasPrefix(table, "(") { + table = table_name + } else { + table = getTableName(dbName, table_name) + } + var sql_str, title string + + if titles != "" { + title = titles + } else { + title = "*" + } + if DB_PROVIDER == "TencentDB" { sql_str = helper.StringJoin("/*slave*/ ", withSql, " select ", title) } else { @@ -364,7 +365,7 @@ func GetRow(dbName, table_name, alias string, titles string, with, join [][]stri builder.WriteString("left") } builder.WriteString(" join ") - if strings.Contains(joinitem[0], "select ") || strings.HasPrefix(joinitem[0], "(") || len(joinitem) > 4 { + if withSql != "" || strings.Contains(joinitem[0], "select ") || strings.HasPrefix(joinitem[0], "(") || len(joinitem) > 4 { builder.WriteString(joinitem[0]) } else { builder.WriteString(getTableName(dbName, joinitem[0])) @@ -492,19 +493,6 @@ func FetchRows(dbName, table_name, alias string, titles string, with, join [][]s return count, list, errors.New("没有数据表") } table := "" - if strings.Contains(table_name, "select ") || strings.HasPrefix(table, "(") { - table = table_name - } else { - table = getTableName(dbName, table_name) - } - - var sql_str, title string - - if titles != "" { - title = titles - } else { - title = "*" - } withSql := "" if len(with) > 0 { var builder strings.Builder @@ -528,6 +516,20 @@ func FetchRows(dbName, table_name, alias string, titles string, with, join [][]s withSql = builder.String() } } + if withSql != "" || strings.Contains(table_name, "select ") || strings.HasPrefix(table, "(") { + table = table_name + } else { + table = getTableName(dbName, table_name) + } + + var sql_str, title string + + if titles != "" { + title = titles + } else { + title = "*" + } + if DB_PROVIDER == "TencentDB" { sql_str = helper.StringJoin("/*slave*/ ", withSql, " select ", title) } else { @@ -553,7 +555,7 @@ func FetchRows(dbName, table_name, alias string, titles string, with, join [][]s builder.WriteString("left") } builder.WriteString(" join ") - if strings.Contains(joinitem[0], "select ") || strings.HasPrefix(joinitem[0], "(") || len(joinitem) > 4 { + if withSql != "" || strings.Contains(joinitem[0], "select ") || strings.HasPrefix(joinitem[0], "(") || len(joinitem) >= 4 { builder.WriteString(joinitem[0]) } else { builder.WriteString(getTableName(dbName, joinitem[0])) diff --git a/transaction_chain.go b/transaction_chain.go index 2cc6934..d73b639 100644 --- a/transaction_chain.go +++ b/transaction_chain.go @@ -222,19 +222,6 @@ func (this *TxQuery) BuildSelectSql() (map[string]interface{}, error) { return nil, errors.New("参数错误,没有数据表") } var table = "" - if strings.Contains(this.table, "select ") || strings.HasPrefix(this.table, "(") { - table = this.table - } else { - table = getTableName(this.dbname, this.table) - } - - var sql, title string - - if this.title != "" { - title = this.title - } else { - title = "*" - } withSql := "" if len(this.with) > 0 { var builder strings.Builder @@ -258,6 +245,20 @@ func (this *TxQuery) BuildSelectSql() (map[string]interface{}, error) { withSql = builder.String() } } + if withSql != "" || strings.Contains(this.table, "select ") || strings.HasPrefix(this.table, "(") { + table = this.table + } else { + table = getTableName(this.dbname, this.table) + } + + var sql, title string + + if this.title != "" { + title = this.title + } else { + title = "*" + } + sql = helper.StringJoin(withSql, "select ", title) if this.alias != "" { @@ -267,15 +268,31 @@ func (this *TxQuery) BuildSelectSql() (map[string]interface{}, error) { sql = helper.StringJoin(sql, " from ", table) if len(this.join) > 0 { + var builder strings.Builder for _, joinitem := range this.join { if len(joinitem) < 2 { continue } - if len(joinitem) == 3 { - sql = helper.StringJoin(sql, " ", joinitem[2], " join ", getTableName(this.dbname, joinitem[0]), " on ", joinitem[1]) - } else { //默认左连接 - sql = helper.StringJoin(sql, " left join ", getTableName(this.dbname, joinitem[0]), " on ", joinitem[1]) + builder.WriteString(sql) + builder.WriteString(" ") + builder.WriteString(sql) + builder.WriteString(" ") + if len(joinitem) >= 3 { + builder.WriteString(joinitem[2]) + } else { + builder.WriteString("left") + } + builder.WriteString(" join ") + if withSql != "" || strings.Contains(joinitem[0], "select ") || strings.HasPrefix(joinitem[0], "(") { + builder.WriteString(joinitem[0]) + } else { + builder.WriteString(getTableName(this.dbname, joinitem[0])) } + builder.WriteString(" on ") + builder.WriteString(joinitem[1]) + } + if builder.Len() > 0 { + sql = builder.String() } } if len(this.where) > 0 || len(this.where_or) > 0 {