mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
fix duplicated fields in function TableFields of driver pgsql (#2620)
This commit is contained in:
@ -17,6 +17,8 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
@ -25,7 +27,6 @@ import (
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/gogf/gf/v2/util/gutil"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
// Driver is the driver for postgresql database.
|
||||
@ -273,9 +274,10 @@ ORDER BY
|
||||
// Also see DriverMysql.TableFields.
|
||||
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
|
||||
usedSchema = gutil.GetOrDefaultStr(d.GetSchema(), schema...)
|
||||
result gdb.Result
|
||||
link gdb.Link
|
||||
usedSchema = gutil.GetOrDefaultStr(d.GetSchema(), schema...)
|
||||
// TODO duplicated `id` result?
|
||||
structureSql = fmt.Sprintf(`
|
||||
SELECT a.attname AS field, t.typname AS type,a.attnotnull as null,
|
||||
(case when d.contype is not null then 'pri' else '' end) as key
|
||||
@ -302,16 +304,27 @@ ORDER BY a.attnum`,
|
||||
return nil, err
|
||||
}
|
||||
fields = make(map[string]*gdb.TableField)
|
||||
for i, m := range result {
|
||||
fields[m["field"].String()] = &gdb.TableField{
|
||||
Index: i,
|
||||
Name: m["field"].String(),
|
||||
var (
|
||||
index = 0
|
||||
name string
|
||||
ok bool
|
||||
)
|
||||
for _, m := range result {
|
||||
name = m["field"].String()
|
||||
// Filter duplicated fields.
|
||||
if _, ok = fields[name]; ok {
|
||||
continue
|
||||
}
|
||||
fields[name] = &gdb.TableField{
|
||||
Index: index,
|
||||
Name: name,
|
||||
Type: m["type"].String(),
|
||||
Null: !m["null"].Bool(),
|
||||
Key: m["key"].String(),
|
||||
Default: m["default_value"].Val(),
|
||||
Comment: m["comment"].String(),
|
||||
}
|
||||
index++
|
||||
}
|
||||
return fields, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user