mirror of
https://gitee.com/johng/gf
synced 2026-07-07 06:15:15 +08:00
feat(contrib/drivers/dm): add WherePri support (#4157)
The Dameng database supports the wherepri method. eg: `dao.User.Ctx(ctx).WherePri(id)` --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: John Guo <claymore1986@gmail.com> Co-authored-by: hailaz <739476267@qq.com>
This commit is contained in:
@ -11,12 +11,21 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gmap"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/util/gutil"
|
||||
)
|
||||
|
||||
// escapeSingleQuote escapes single quotes in the string to prevent SQL injection.
|
||||
// In SQL, single quotes are escaped by doubling them (two single quotes).
|
||||
func escapeSingleQuote(s string) string {
|
||||
return strings.ReplaceAll(s, "'", "''")
|
||||
}
|
||||
|
||||
const (
|
||||
tableFieldsSqlTmp = `SELECT * FROM ALL_TAB_COLUMNS WHERE Table_Name= '%s' AND OWNER = '%s'`
|
||||
tableFieldsSqlTmp = `SELECT c.COLUMN_NAME, c.DATA_TYPE, c.DATA_DEFAULT, c.NULLABLE, cc.COMMENTS FROM ALL_TAB_COLUMNS c LEFT JOIN ALL_COL_COMMENTS cc ON c.COLUMN_NAME = cc.COLUMN_NAME AND c.TABLE_NAME = cc.TABLE_NAME AND c.OWNER = cc.OWNER WHERE c.TABLE_NAME = '%s' AND c.OWNER = '%s'`
|
||||
tableFieldsPkSqlSchemaTmp = `SELECT COLS.COLUMN_NAME AS PRIMARY_KEY_COLUMN FROM USER_CONSTRAINTS CONS JOIN USER_CONS_COLUMNS COLS ON CONS.CONSTRAINT_NAME = COLS.CONSTRAINT_NAME WHERE CONS.TABLE_NAME = '%s' AND CONS.CONSTRAINT_TYPE = 'P'`
|
||||
tableFieldsPkSqlDBATmp = `SELECT COLS.COLUMN_NAME AS PRIMARY_KEY_COLUMN FROM DBA_CONSTRAINTS CONS JOIN DBA_CONS_COLUMNS COLS ON CONS.CONSTRAINT_NAME = COLS.CONSTRAINT_NAME WHERE CONS.TABLE_NAME = '%s' AND CONS.OWNER = '%s' AND CONS.CONSTRAINT_TYPE = 'P'`
|
||||
)
|
||||
|
||||
// TableFields retrieves and returns the fields' information of specified table of current schema.
|
||||
@ -24,8 +33,9 @@ func (d *Driver) TableFields(
|
||||
ctx context.Context, table string, schema ...string,
|
||||
) (fields map[string]*gdb.TableField, err error) {
|
||||
var (
|
||||
result gdb.Result
|
||||
link gdb.Link
|
||||
result gdb.Result
|
||||
pkResult gdb.Result
|
||||
link gdb.Link
|
||||
// When no schema is specified, the configuration item is returned by default
|
||||
usedSchema = gutil.GetOrDefaultStr(d.GetSchema(), schema...)
|
||||
)
|
||||
@ -38,14 +48,35 @@ func (d *Driver) TableFields(
|
||||
ctx, link,
|
||||
fmt.Sprintf(
|
||||
tableFieldsSqlTmp,
|
||||
strings.ToUpper(table),
|
||||
strings.ToUpper(d.GetSchema()),
|
||||
escapeSingleQuote(strings.ToUpper(table)),
|
||||
escapeSingleQuote(strings.ToUpper(d.GetSchema())),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Query the primary key field
|
||||
pkResult, err = d.DoSelect(
|
||||
ctx, link,
|
||||
fmt.Sprintf(tableFieldsPkSqlSchemaTmp, escapeSingleQuote(strings.ToUpper(table))),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if pkResult.IsEmpty() {
|
||||
pkResult, err = d.DoSelect(
|
||||
ctx, link,
|
||||
fmt.Sprintf(tableFieldsPkSqlDBATmp, escapeSingleQuote(strings.ToUpper(table)), escapeSingleQuote(strings.ToUpper(d.GetSchema()))),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
fields = make(map[string]*gdb.TableField)
|
||||
pkFields := gmap.NewStrStrMap()
|
||||
for _, pk := range pkResult {
|
||||
pkFields.Set(pk["PRIMARY_KEY_COLUMN"].String(), "PRI")
|
||||
}
|
||||
for i, m := range result {
|
||||
// m[NULLABLE] returns "N" "Y"
|
||||
// "N" means not null
|
||||
@ -60,9 +91,9 @@ func (d *Driver) TableFields(
|
||||
Type: m["DATA_TYPE"].String(),
|
||||
Null: nullable,
|
||||
Default: m["DATA_DEFAULT"].Val(),
|
||||
// Key: m["Key"].String(),
|
||||
Key: pkFields.Get(m["COLUMN_NAME"].String()),
|
||||
// Extra: m["Extra"].String(),
|
||||
// Comment: m["Comment"].String(),
|
||||
Comment: m["COMMENTS"].String(),
|
||||
}
|
||||
}
|
||||
return fields, nil
|
||||
|
||||
Reference in New Issue
Block a user