|
|
@ -145,6 +145,27 @@ func (this *Query) Join(join []string) *Query { |
|
|
|
this.join = append(this.join, join) |
|
|
|
return this |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 左连接 |
|
|
|
* 2023/08/10 |
|
|
|
* gz |
|
|
|
*/ |
|
|
|
func (this *Query) LeftJoin(table_name string, condition string) *Query { |
|
|
|
this.join = append(this.join, []string{table_name, condition, "left"}) |
|
|
|
return this |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 右连接 |
|
|
|
* 2023/08/10 |
|
|
|
* gz |
|
|
|
*/ |
|
|
|
func (this *Query) RightJoin(table_name string, condition string) *Query { |
|
|
|
this.join = append(this.join, []string{table_name, condition, "right"}) |
|
|
|
return this |
|
|
|
} |
|
|
|
|
|
|
|
func (this *Query) Data(data string) *Query { |
|
|
|
this.data = append(this.data, data) |
|
|
|
return this |
|
|
@ -176,10 +197,11 @@ func (this *Query) Clean() *Query { |
|
|
|
this.save_data = this.save_data[0:0] |
|
|
|
this.upd_field = this.upd_field[0:0] |
|
|
|
this.having = "" |
|
|
|
this.alias = "" |
|
|
|
return this |
|
|
|
} |
|
|
|
|
|
|
|
//获取表格信息
|
|
|
|
// 获取表格信息
|
|
|
|
func (this *Query) GetTableInfo(table string) (map[string]interface{}, error) { |
|
|
|
field := []string{ |
|
|
|
"COLUMN_NAME", //字段名
|
|
|
@ -245,7 +267,12 @@ func (this *Query) GetTableInfo(table string) (map[string]interface{}, error) { |
|
|
|
}, nil |
|
|
|
} |
|
|
|
|
|
|
|
//构造子查询
|
|
|
|
// 返回表名
|
|
|
|
func (this *Query) GetTableName(table string) string { |
|
|
|
return getTableName(this.dbname, table) |
|
|
|
} |
|
|
|
|
|
|
|
// 构造子查询
|
|
|
|
func (this *Query) BuildSelectSql() (map[string]interface{}, error) { |
|
|
|
if this.dbname == "" && this.table == "" { |
|
|
|
return nil, errors.New("参数错误,没有数据表") |
|
|
@ -266,12 +293,23 @@ func (this *Query) BuildSelectSql() (map[string]interface{}, error) { |
|
|
|
} else { |
|
|
|
title = "*" |
|
|
|
} |
|
|
|
if DB_PROVIDER == "TencentDB" { |
|
|
|
sql = helper.StringJoin("/*slave*/ select ", title) |
|
|
|
|
|
|
|
if this.dbtype == "mssql" { |
|
|
|
if this.page_size > 0 { |
|
|
|
sql = helper.StringJoin("select top ", helper.ToStr(this.page_size), " ") |
|
|
|
} else { |
|
|
|
sql = "select " |
|
|
|
} |
|
|
|
} else { |
|
|
|
sql = helper.StringJoin("select ", title) |
|
|
|
if DB_PROVIDER == "TencentDB" { |
|
|
|
sql = "/*slave*/ select " |
|
|
|
} else { |
|
|
|
sql = "select " |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
sql = helper.StringJoin(sql, title) |
|
|
|
|
|
|
|
if this.alias != "" { |
|
|
|
table = helper.StringJoin(table, " as ", this.alias) |
|
|
|
} |
|
|
@ -279,15 +317,17 @@ func (this *Query) BuildSelectSql() (map[string]interface{}, error) { |
|
|
|
sql = helper.StringJoin(sql, " from ", table) |
|
|
|
|
|
|
|
if len(this.join) > 0 { |
|
|
|
join_type := "left" |
|
|
|
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], this.dbtype), " on ", joinitem[1]) |
|
|
|
join_type = joinitem[2] |
|
|
|
} else { //默认左连接
|
|
|
|
sql = helper.StringJoin(sql, " left join ", getTableName(this.dbname, joinitem[0], this.dbtype), " on ", joinitem[1]) |
|
|
|
join_type = "left" |
|
|
|
} |
|
|
|
sql = helper.StringJoin(sql, " ", join_type, " join ", getTableName(this.dbname, joinitem[0], this.dbtype), " on ", joinitem[1]) |
|
|
|
} |
|
|
|
} |
|
|
|
if len(this.where) > 0 || len(this.where_or) > 0 { |
|
|
@ -315,7 +355,7 @@ func (this *Query) BuildSelectSql() (map[string]interface{}, error) { |
|
|
|
sql = helper.StringJoin(sql, " order by ", this.orderby) |
|
|
|
} |
|
|
|
|
|
|
|
if this.page > 0 || this.page_size > 0 { |
|
|
|
if this.dbtype == "mysql" && (this.page > 0 || this.page_size > 0) { |
|
|
|
|
|
|
|
if this.page < 1 { |
|
|
|
this.page = 1 |
|
|
@ -358,7 +398,11 @@ func (this *Query) QueryStmt() error { |
|
|
|
|
|
|
|
if SLAVER_DB != nil { |
|
|
|
this.conn = SLAVER_DB |
|
|
|
} else { |
|
|
|
} |
|
|
|
// else {
|
|
|
|
// this.conn = DB
|
|
|
|
// }
|
|
|
|
if this.conn == nil { |
|
|
|
this.conn = DB |
|
|
|
} |
|
|
|
|
|
|
@ -798,7 +842,7 @@ func (this *Query) Update() (int64, error) { |
|
|
|
return StmtForUpdateExec(this.stmt, this.value) |
|
|
|
} |
|
|
|
|
|
|
|
//批量更新
|
|
|
|
// 批量更新
|
|
|
|
func (this *Query) UpdateAll() (int64, error) { |
|
|
|
|
|
|
|
err := this.UpdateAllStmt() |
|
|
|