Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: query result column type name (#802)

STEVEN82e9f4a3aa26
diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.go
index 730df6ecba..3de1348e19 100644
--- a/plugin/db/util/driverutil.go
+++ b/plugin/db/util/driverutil.go
@@ -306,18 +306,29 @@ func Query(ctx context.Context, l *zap.Logger, db *sql.DB, statement string, lim
}
colCount := len(columnTypes)
+
+ columnTypeNames := make([]string, colCount)
+ for i, v := range columnTypes {
+ // DatabaseTypeName returns the database system name of the column type.
+ // refer: https://pkg.go.dev/database/sql#ColumnType.DatabaseTypeName
+ columnTypeNames[i] = strings.ToUpper(v.DatabaseTypeName())
+ }
+
rowCount := 0
data := []interface{}{}
for rows.Next() {
scanArgs := make([]interface{}, colCount)
- for i, v := range columnTypes {
- switch v.DatabaseTypeName() {
+ for i, v := range columnTypeNames {
+ // TODO(steven need help): Consult a common list of data types from database driver documentation. e.g. MySQL,PostgreSQL.
+ switch v {
case "VARCHAR", "TEXT", "UUID", "TIMESTAMP":
scanArgs[i] = new(sql.NullString)
case "BOOL":
scanArgs[i] = new(sql.NullBool)
- case "INT4":
+ case "INT", "INTEGER":
scanArgs[i] = new(sql.NullInt64)
+ case "FLOAT":
+ scanArgs[i] = new(sql.NullFloat64)
default:
scanArgs[i] = new(sql.NullString)
}
@@ -341,14 +352,14 @@ func Query(ctx context.Context, l *zap.Logger, db *sql.DB, statement string, lim
rowData = append(rowData, v.Int64)
continue
}
- if v, ok := (scanArgs[i]).(*sql.NullFloat64); ok && v.Valid {
- rowData = append(rowData, v.Float64)
- continue
- }
if v, ok := (scanArgs[i]).(*sql.NullInt32); ok && v.Valid {
rowData = append(rowData, v.Int32)
continue
}
+ if v, ok := (scanArgs[i]).(*sql.NullFloat64); ok && v.Valid {
+ rowData = append(rowData, v.Float64)
+ continue
+ }
// If none of them match, set nil to its value.
rowData = append(rowData, nil)
}
diff --git a/tests/tests.go b/tests/tests.go
index b7e5b4fde4..ceba34eb10 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -34,9 +34,9 @@ var (
name TEXT NULL
);`
bookTableQuery = "SELECT * FROM sqlite_schema WHERE type = 'table' AND tbl_name = 'book';"
- bookSchemaSQLResult = `[["type","name","tbl_name","rootpage","sql"],[["table","book","book","2","CREATE TABLE book (\n\t\tid INTEGER PRIMARY KEY AUTOINCREMENT,\n\t\tname TEXT NULL\n\t)"]]]`
+ bookSchemaSQLResult = `[["type","name","tbl_name","rootpage","sql"],[["table","book","book",2,"CREATE TABLE book (\n\t\tid INTEGER PRIMARY KEY AUTOINCREMENT,\n\t\tname TEXT NULL\n\t)"]]]`
bookDataQuery = `SELECT * FROM book;`
- bookDataSQLResult = `[["id","name"],[["1","byte"],["2",null]]]`
+ bookDataSQLResult = `[["id","name"],[[1,"byte"],[2,null]]]`
dataUpdateStatement = `
INSERT INTO book(name) VALUES