Skip to content

Commit 42026fb

Browse files
philippefutureboypaveltiunov
authored andcommitted
fix(sqlite-driver): Fixed table schema parsing: support for escape characters (#289). Thanks to @philippefutureboy!
* fix(SqliteDriver.js): Fixed table schema parsing #1 .match on line 52 returned null => fixed by removing the EOL character #2 The describe table returns the name of the field between square brackets => fixed by removing the square brackets * Added support for all escape symbols ((\[|`|")?) Fix now supports all escape symbols and absence of escape symbol. + Refactored the `tables.map.reduce` to `tables.reduce` instead.
1 parent 16222d1 commit 42026fb

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,23 @@ class SqliteDriver extends BaseDriver {
4646
const tables = await this.query(query);
4747

4848
return {
49-
default: {
50-
...(tables.map(
51-
table => ({
52-
[table.name]: table.sql.match(/\((.*)\)/)[1].split(',')
53-
.map(nameAndType => nameAndType.trim().split(' ')).map(([name, type]) => ({ name, type }))
54-
})
55-
)).reduce((a, b) => ({ ...a, ...b }), {})
56-
}
49+
default: tables.reduce((acc, table) => ({
50+
...acc,
51+
[table.name]: table.sql
52+
// remove EOL for next .match to read full string
53+
.replace(/\n/g, '')
54+
// extract fields
55+
.match(/\((.*)\)/)[1]
56+
// split fields
57+
.split(',')
58+
.map((nameAndType) => {
59+
const match = nameAndType
60+
.trim()
61+
// obtain "([|`|")?name(]|`|")? type"
62+
.match(/(\[|`|")?([^\[\]"`]+)(\]|`|")?\s+(\w+)/)
63+
return { name: match[2], type: match[4] };
64+
})
65+
}), {}),
5766
};
5867
}
5968
}

0 commit comments

Comments
 (0)