Skip to content

Commit

Permalink
fix(sqlite-driver): Fixed table schema parsing: support for escape ch…
Browse files Browse the repository at this point in the history
…aracters (#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.
  • Loading branch information
philippefutureboy authored and paveltiunov committed Dec 14, 2019
1 parent 16222d1 commit 42026fb
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions packages/cubejs-sqlite-driver/driver/SqliteDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,23 @@ class SqliteDriver extends BaseDriver {
const tables = await this.query(query);

return {
default: {
...(tables.map(
table => ({
[table.name]: table.sql.match(/\((.*)\)/)[1].split(',')
.map(nameAndType => nameAndType.trim().split(' ')).map(([name, type]) => ({ name, type }))
})
)).reduce((a, b) => ({ ...a, ...b }), {})
}
default: tables.reduce((acc, table) => ({
...acc,
[table.name]: table.sql
// remove EOL for next .match to read full string
.replace(/\n/g, '')
// extract fields
.match(/\((.*)\)/)[1]
// split fields
.split(',')
.map((nameAndType) => {
const match = nameAndType
.trim()
// obtain "([|`|")?name(]|`|")? type"
.match(/(\[|`|")?([^\[\]"`]+)(\]|`|")?\s+(\w+)/)
return { name: match[2], type: match[4] };
})
}), {}),
};
}
}
Expand Down

0 comments on commit 42026fb

Please sign in to comment.