refactor: sql执行列返回字段类型

This commit is contained in:
meilin.huang
2023-12-14 21:27:11 +08:00
parent 18cf2e54c4
commit a5a813f95f
15 changed files with 127 additions and 44 deletions

View File

@@ -136,7 +136,7 @@ func (d *Db) ExecSql(rc *req.Ctx) {
}
colAndRes := make(map[string]any)
colAndRes["colNames"] = execResAll.ColNames
colAndRes["columns"] = execResAll.Columns
colAndRes["res"] = execResAll.Res
rc.ResData = colAndRes
}
@@ -349,11 +349,11 @@ func (d *Db) dumpDb(writer *gzipWriter, dbId uint64, dbName string, tables []str
writer.WriteString(fmt.Sprintf("\n-- ----------------------------\n-- 表记录: %s \n-- ----------------------------\n", table))
writer.WriteString("BEGIN;\n")
insertSql := "INSERT INTO %s VALUES (%s);\n"
dbMeta.WalkTableRecord(table, func(record map[string]any, columns []string) {
dbMeta.WalkTableRecord(table, func(record map[string]any, columns []*dbm.QueryColumn) {
var values []string
writer.TryFlush()
for _, column := range columns {
value := record[column]
value := record[column.Name]
if value == nil {
values = append(values, "NULL")
continue

View File

@@ -26,19 +26,19 @@ type DbSqlExecReq struct {
}
type DbSqlExecRes struct {
ColNames []string
Res []map[string]any
Columns []*dbm.QueryColumn
Res []map[string]any
}
// 合并执行结果主要用于执行多条sql使用
func (d *DbSqlExecRes) Merge(execRes *DbSqlExecRes) {
canMerge := len(d.ColNames) == len(execRes.ColNames)
canMerge := len(d.Columns) == len(execRes.Columns)
if !canMerge {
return
}
// 列名不一致,则不合并
for i, colName := range d.ColNames {
if execRes.ColNames[i] != colName {
for i, col := range d.Columns {
if execRes.Columns[i].Name != col.Name {
return
}
}
@@ -188,13 +188,13 @@ func doSelect(ctx context.Context, selectStmt *sqlparser.Select, execSqlReq *DbS
func doRead(ctx context.Context, execSqlReq *DbSqlExecReq) (*DbSqlExecRes, error) {
dbConn := execSqlReq.DbConn
sql := execSqlReq.Sql
colNames, res, err := dbConn.QueryContext(ctx, sql)
cols, res, err := dbConn.QueryContext(ctx, sql)
if err != nil {
return nil, err
}
return &DbSqlExecRes{
ColNames: colNames,
Res: res,
Columns: cols,
Res: res,
}, nil
}
@@ -285,7 +285,11 @@ func doExec(ctx context.Context, sql string, dbConn *dbm.DbConn) (*DbSqlExecRes,
res = append(res, resData)
return &DbSqlExecRes{
ColNames: []string{"sql", "rowsAffected", "result"},
Res: res,
Columns: []*dbm.QueryColumn{
{Name: "sql", Type: "string"},
{Name: "rowsAffected", Type: "number"},
{Name: "result", Type: "string"},
},
Res: res,
}, err
}

View File

@@ -19,17 +19,23 @@ type DbConn struct {
db *sql.DB
}
// 执行数据库查询返回的列信息
type QueryColumn struct {
Name string `json:"name"` // 列名
Type string `json:"type"` // 类型
}
// 执行查询语句
// 依次返回 列数组(顺序)结果map错误
func (d *DbConn) Query(querySql string) ([]string, []map[string]any, error) {
// 依次返回 列信息数组(顺序)结果map错误
func (d *DbConn) Query(querySql string) ([]*QueryColumn, []map[string]any, error) {
return d.QueryContext(context.Background(), querySql)
}
// 执行查询语句
// 依次返回 列数组(顺序)结果map错误
func (d *DbConn) QueryContext(ctx context.Context, querySql string) ([]string, []map[string]any, error) {
// 依次返回 列信息数组(顺序)结果map错误
func (d *DbConn) QueryContext(ctx context.Context, querySql string) ([]*QueryColumn, []map[string]any, error) {
result := make([]map[string]any, 0, 16)
columns, err := walkTableRecord(ctx, d.db, querySql, func(record map[string]any, columns []string) {
columns, err := walkTableRecord(ctx, d.db, querySql, func(record map[string]any, columns []*QueryColumn) {
result = append(result, record)
})
if err != nil {
@@ -55,7 +61,7 @@ func (d *DbConn) Query2Struct(execSql string, dest any) error {
}
// WalkTableRecord 遍历表记录
func (d *DbConn) WalkTableRecord(ctx context.Context, selectSql string, walk func(record map[string]any, columns []string)) error {
func (d *DbConn) WalkTableRecord(ctx context.Context, selectSql string, walk func(record map[string]any, columns []*QueryColumn)) error {
_, err := walkTableRecord(ctx, d.db, selectSql, walk)
return err
}
@@ -100,7 +106,7 @@ func (d *DbConn) Close() {
}
}
func walkTableRecord(ctx context.Context, db *sql.DB, selectSql string, walk func(record map[string]any, columns []string)) ([]string, error) {
func walkTableRecord(ctx context.Context, db *sql.DB, selectSql string, walk func(record map[string]any, columns []*QueryColumn)) ([]*QueryColumn, error) {
rows, err := db.QueryContext(ctx, selectSql)
if err != nil {
@@ -120,13 +126,13 @@ func walkTableRecord(ctx context.Context, db *sql.DB, selectSql string, walk fun
}
lenCols := len(colTypes)
// 列名用于前端表头名称按照数据库与查询字段顺序显示
colNames := make([]string, lenCols)
cols := make([]*QueryColumn, lenCols)
// 这里表示一行填充数据
scans := make([]any, lenCols)
// 这里表示一行所有列的值,用[]byte表示
values := make([][]byte, lenCols)
for k, colType := range colTypes {
colNames[k] = colType.Name()
cols[k] = &QueryColumn{Name: colType.Name(), Type: colType.DatabaseTypeName()}
// 这里scans引用values把数据填充到[]byte里
scans[k] = &values[k]
}
@@ -142,10 +148,10 @@ func walkTableRecord(ctx context.Context, db *sql.DB, selectSql string, walk fun
for i, v := range values {
rowData[colTypes[i].Name()] = valueConvert(v, colTypes[i])
}
walk(rowData, colNames)
walk(rowData, cols)
}
return colNames, nil
return cols, nil
}
// 将查询的值转为对应列类型的实际值,不全部转为字符串

View File

@@ -63,10 +63,10 @@ type DbDialect interface {
// 获取指定表的数据-分页查询
// @return columns: 列字段名result: 结果集error: 错误
GetTableRecord(tableName string, pageNum, pageSize int) ([]string, []map[string]any, error)
GetTableRecord(tableName string, pageNum, pageSize int) ([]*QueryColumn, []map[string]any, error)
// WalkTableRecord 遍历指定表的数据
WalkTableRecord(tableName string, walk func(record map[string]any, columns []string)) error
WalkTableRecord(tableName string, walk func(record map[string]any, columns []*QueryColumn)) error
GetSchemas() ([]string, error)
}

View File

@@ -232,11 +232,11 @@ func (pd *DMDialect) GetCreateTableDdl(tableName string) (string, error) {
return builder.String(), nil
}
func (pd *DMDialect) GetTableRecord(tableName string, pageNum, pageSize int) ([]string, []map[string]any, error) {
func (pd *DMDialect) GetTableRecord(tableName string, pageNum, pageSize int) ([]*QueryColumn, []map[string]any, error) {
return pd.dc.Query(fmt.Sprintf("SELECT * FROM %s OFFSET %d LIMIT %d", tableName, (pageNum-1)*pageSize, pageSize))
}
func (pd *DMDialect) WalkTableRecord(tableName string, walk func(record map[string]any, columns []string)) error {
func (pd *DMDialect) WalkTableRecord(tableName string, walk func(record map[string]any, columns []*QueryColumn)) error {
return pd.dc.WalkTableRecord(context.Background(), fmt.Sprintf("SELECT * FROM %s", tableName), walk)
}

View File

@@ -174,11 +174,11 @@ func (md *MysqlDialect) GetCreateTableDdl(tableName string) (string, error) {
return res[0]["Create Table"].(string) + ";", nil
}
func (md *MysqlDialect) GetTableRecord(tableName string, pageNum, pageSize int) ([]string, []map[string]any, error) {
func (md *MysqlDialect) GetTableRecord(tableName string, pageNum, pageSize int) ([]*QueryColumn, []map[string]any, error) {
return md.dc.Query(fmt.Sprintf("SELECT * FROM %s LIMIT %d, %d", tableName, (pageNum-1)*pageSize, pageSize))
}
func (md *MysqlDialect) WalkTableRecord(tableName string, walk func(record map[string]any, columns []string)) error {
func (md *MysqlDialect) WalkTableRecord(tableName string, walk func(record map[string]any, columns []*QueryColumn)) error {
return md.dc.WalkTableRecord(context.Background(), fmt.Sprintf("SELECT * FROM %s", tableName), walk)
}

View File

@@ -245,11 +245,11 @@ func (pd *PgsqlDialect) GetCreateTableDdl(tableName string) (string, error) {
return res[0]["sql"].(string), nil
}
func (pd *PgsqlDialect) GetTableRecord(tableName string, pageNum, pageSize int) ([]string, []map[string]any, error) {
func (pd *PgsqlDialect) GetTableRecord(tableName string, pageNum, pageSize int) ([]*QueryColumn, []map[string]any, error) {
return pd.dc.Query(fmt.Sprintf("SELECT * FROM %s OFFSET %d LIMIT %d", tableName, (pageNum-1)*pageSize, pageSize))
}
func (pd *PgsqlDialect) WalkTableRecord(tableName string, walk func(record map[string]any, columns []string)) error {
func (pd *PgsqlDialect) WalkTableRecord(tableName string, walk func(record map[string]any, columns []*QueryColumn)) error {
return pd.dc.WalkTableRecord(context.Background(), fmt.Sprintf("SELECT * FROM %s", tableName), walk)
}