From f8d6d88e9f02b1076681c5788512c1df119a1de5 Mon Sep 17 00:00:00 2001 From: guzeng Date: Wed, 9 Aug 2023 10:08:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B7=A6/=E5=8F=B3=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chain.go | 27 +++++++++++++++++++++++++-- transaction_chain.go | 24 ++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/chain.go b/chain.go index 4a422e4..27153be 100644 --- a/chain.go +++ b/chain.go @@ -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 { diff --git a/transaction_chain.go b/transaction_chain.go index 4e27911..2cc5f2d 100644 --- a/transaction_chain.go +++ b/transaction_chain.go @@ -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", //字段名