5 Commits

4 changed files with 64 additions and 7 deletions
Split View
  1. +31
    -2
      chain.go
  2. +2
    -0
      db.go
  3. +3
    -3
      transaction.go
  4. +28
    -2
      transaction_chain.go

+ 31
- 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
@ -176,6 +197,7 @@ 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
}
@ -245,6 +267,11 @@ 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 == "" {
@ -290,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 {


+ 2
- 0
db.go View File

@ -406,6 +406,7 @@ func GetRow(dbName, table_name, alias string, titles string, join [][]string, wh
}
if err != nil {
log.Println("DB error:", err)
rows.Close()
return count, info, err
}
@ -436,6 +437,7 @@ func GetRow(dbName, table_name, alias string, titles string, join [][]string, wh
}
rows.Close()
if rowerr != nil {
log.Println("DB row error:", rowerr)
return count, info, rowerr
}
return count, info, nil


+ 3
- 3
transaction.go View File

@ -25,7 +25,7 @@ func TxInsert(tx *sql.Tx, dbname, table string, data map[string]string) (int64,
if strings.Contains(table, "select ") {
dbName = table
} else {
dbName = getTableName(dbName, table)
dbName = getTableName(dbname, table)
}
if len(data) < 1 {
return 0, errors.New("参数错误,没有要写入的数据")
@ -186,7 +186,7 @@ func TxPreUpdate(tx *sql.Tx, dbname, table string, data []string, where []string
if strings.Contains(table, "select ") {
dbName = table
} else {
dbName = getTableName(dbName, table)
dbName = getTableName(dbname, table)
}
if len(where) < 1 {
@ -228,7 +228,7 @@ func TxDelete(tx *sql.Tx, dbname, table string, where map[string]string, del_cou
if strings.Contains(table, "select ") {
dbName = table
} else {
dbName = getTableName(dbName, table)
dbName = getTableName(dbname, table)
}
if len(where) < 1 {
return count, errors.New("参数错误,没有删除条件")


+ 28
- 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
@ -177,10 +197,16 @@ func (this *TxQuery) Clean() *TxQuery {
this.save_data = this.save_data[0:0]
this.upd_field = this.upd_field[0:0]
this.having = ""
this.alias = ""
return this
}
//构造子查询
// 返回表名
func (this *TxQuery) GetTableName(table string) string {
return getTableName(this.dbname, table)
}
// 构造子查询
func (this *TxQuery) BuildSelectSql() (map[string]interface{}, error) {
if this.dbname == "" && this.table == "" {
return nil, errors.New("参数错误,没有数据表")
@ -278,7 +304,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