Browse Source

增加左/右连接方法

master v0.12.0
guzeng 1 year ago
parent
commit
f8d6d88e9f
2 changed files with 47 additions and 4 deletions
  1. +25
    -2
      chain.go
  2. +22
    -2
      transaction_chain.go

+ 25
- 2
chain.go View File

@ -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
@ -290,15 +311,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 {


+ 22
- 2
transaction_chain.go View File

@ -147,6 +147,26 @@ func (this *TxQuery) Join(join []string) *TxQuery {
this.join = append(this.join, join)
return this
}
/**
* 左连接
* 2023/08/10
* gz
*/
func (this *TxQuery) LeftJoin(table_name string, condition string) *TxQuery {
this.join = append(this.join, []string{table_name, condition, "left"})
return this
}
/**
* 右连接
* 2023/08/10
* gz
*/
func (this *TxQuery) RightJoin(table_name string, condition string) *TxQuery {
this.join = append(this.join, []string{table_name, condition, "right"})
return this
}
func (this *TxQuery) Data(data string) *TxQuery {
this.data = append(this.data, data)
return this
@ -180,7 +200,7 @@ func (this *TxQuery) Clean() *TxQuery {
return this
}
//构造子查询
// 构造子查询
func (this *TxQuery) BuildSelectSql() (map[string]interface{}, error) {
if this.dbname == "" && this.table == "" {
return nil, errors.New("参数错误,没有数据表")
@ -278,7 +298,7 @@ func (this *TxQuery) BuildSelectSql() (map[string]interface{}, error) {
}, nil
}
//获取表格信息
// 获取表格信息
func (this *TxQuery) GetTableInfo(table string) (map[string]interface{}, error) {
field := []string{
"COLUMN_NAME", //字段名


Loading…
Cancel
Save