Skip to content

Commit b57df9f

Browse files
committed
Use pragma_table_info to fetch table columns
1 parent f749e88 commit b57df9f

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

packages/cubejs-sqlite-driver/driver/SqliteDriver.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,39 +64,33 @@ class SqliteDriver extends BaseDriver {
6464

6565
informationSchemaQuery() {
6666
return `
67-
SELECT name, sql
67+
SELECT name
6868
FROM sqlite_master
6969
WHERE type='table'
7070
AND name!='sqlite_sequence'
7171
ORDER BY name
7272
`;
7373
}
7474

75+
tableColumnsQuery(tableName) {
76+
return `
77+
SELECT name, type
78+
FROM pragma_table_info('${tableName}')
79+
`;
80+
}
81+
7582
async tablesSchema() {
7683
const query = this.informationSchemaQuery();
7784

7885
const tables = await this.query(query);
7986

87+
const tableColumns = await Promise.all(tables.map(async table => {
88+
const columns = await this.query(this.tableColumnsQuery(table.name));
89+
return [table.name, columns];
90+
}));
91+
8092
return {
81-
main: tables.reduce((acc, table) => ({
82-
...acc,
83-
[table.name]: table.sql
84-
// remove EOL for next .match to read full string
85-
.replace(/\n/g, '')
86-
// extract fields
87-
.match(/\((.*)\)/)[1]
88-
// split fields
89-
.split(',')
90-
.map((nameAndType) => {
91-
const match = nameAndType
92-
.trim()
93-
// replace \t with whitespace
94-
.replace(/\t/g, ' ')
95-
// obtain "([|`|")?name(]|`|")? type"
96-
.match(/([|`|"])?([^[\]"`]+)(]|`|")?\s+(\w+)/);
97-
return { name: match[2], type: match[4] };
98-
})
99-
}), {}),
93+
main: Object.fromEntries(tableColumns)
10094
};
10195
}
10296

0 commit comments

Comments
 (0)