Return a map for table types

This commit is contained in:
Philip O'Toole
2025-09-11 10:49:10 -04:00
committed by GitHub
parent db87f96409
commit ae2a4a967d
3 changed files with 15 additions and 9 deletions

View File

@@ -24,7 +24,7 @@
- [PR #2310](https://github.com/rqlite/rqlite/pull/2310): Correct key deletion in BoltDB.
- [PR #2311](https://github.com/rqlite/rqlite/pull/2311): Test all CDC event types.
- [PR #2312](https://github.com/rqlite/rqlite/pull/2312): Upgrade Go dependencies.
- [PR #2313](https://github.com/rqlite/rqlite/pull/2313): Database layer supports querying just for a table's column types.
- [PR #2313](https://github.com/rqlite/rqlite/pull/2313), [PR #2316](https://github.com/rqlite/rqlite/pull/2316): Database layer supports querying just for a table's column types.
- [PR #2314](https://github.com/rqlite/rqlite/pull/2314): Don't generate CDC events for COMMIT-only operations.
- [PR #2315](https://github.com/rqlite/rqlite/pull/2315): Include "before" and "after" in CDC events.

View File

@@ -835,7 +835,7 @@ func (db *DB) ConnectionPoolStats(sqlDB *sql.DB) *PoolStats {
}
// TableColumnTypes returns the declared types of all columns in the given table.
func (db *DB) TableColumnTypes(table string) ([]string, error) {
func (db *DB) TableColumnTypes(table string) (map[string]string, error) {
rows, err := db.queryStmtWithConn(context.Background(), &command.Statement{
Sql: fmt.Sprintf("PRAGMA table_info(\"%s\")", strings.ReplaceAll(table, `"`, `\"\"`)),
ForceQuery: true,
@@ -846,12 +846,12 @@ func (db *DB) TableColumnTypes(table string) ([]string, error) {
if len(rows.Values) < 1 {
return nil, fmt.Errorf("no such table: %s", table)
}
colTypes := make([]string, len(rows.Values))
for i, v := range rows.Values {
colTypes := make(map[string]string)
for _, v := range rows.Values {
if len(v.Parameters) < 3 {
return nil, fmt.Errorf("unexpected result from PRAGMA table_info")
}
colTypes[i] = v.Parameters[2].GetS()
colTypes[v.Parameters[1].GetS()] = v.Parameters[2].GetS()
}
return colTypes, nil
}

View File

@@ -159,13 +159,19 @@ func Test_DB_TableColumnTypes(t *testing.T) {
t.Fatalf("failed to get column types: %s", err.Error())
}
expTypes := []string{"INTEGER", "TEXT", "NUMERIC", "REAL", "BLOB"}
expTypes := map[string]string{
"id": "INTEGER",
"name": "TEXT",
"age": "NUMERIC",
"height": "REAL",
"data": "BLOB",
}
if len(types) != len(expTypes) {
t.Fatalf("unexpected number of column types, expected %d, got %d", len(expTypes), len(types))
}
for i, et := range expTypes {
if types[i] != et {
t.Fatalf("unexpected column type at index %d, expected %s, got %s", i, et, types[i])
for k, v := range expTypes {
if types[k] != v {
t.Fatalf("unexpected type for column %s, expected %s, got %s", k, v, types[k])
}
}
}