Skip to content

Commit 5ffb3d2

Browse files
committed
feat(sqlite-driver): Pre-aggregations support
1 parent 22f77a6 commit 5ffb3d2

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

packages/cubejs-schema-compiler/adapter/SqliteQuery.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ class SqliteQuery extends BaseQuery {
5858
).join(' UNION ALL ');
5959
return `SELECT dates.f date_from, dates.t date_to FROM (${values}) AS dates`;
6060
}
61+
62+
nowTimestampSql() {
63+
return `strftime('%Y-%m-%dT%H:%M:%fZ', 'now')`;
64+
}
6165
}
6266

6367
module.exports = SqliteQuery;

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

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

4848
return {
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-
}), {}),
49+
main: 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+
}), {}),
6666
};
6767
}
68+
69+
createSchemaIfNotExists(schemaName) {
70+
return this.query(
71+
`PRAGMA database_list`
72+
).then((schemas) => {
73+
if (!schemas.find(s => s.name === schemaName)) {
74+
return this.query(`ATTACH DATABASE ${schemaName} AS ${schemaName}`);
75+
}
76+
return null;
77+
});
78+
}
79+
80+
async getTablesQuery(schemaName) {
81+
const attachedDatabases = await this.query(
82+
`PRAGMA database_list`
83+
);
84+
if (!attachedDatabases.find(s => s.name === schemaName)) {
85+
return [];
86+
}
87+
return this.query(`SELECT name as table_name FROM ${schemaName}.sqlite_master WHERE type='table' ORDER BY name`);
88+
}
6889
}
6990

7091
module.exports = SqliteDriver;

packages/cubejs-sqlite-driver/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,23 @@
1212
"node": ">=8.11.1"
1313
},
1414
"main": "driver/SqliteDriver.js",
15+
"scripts": {
16+
"lint": "eslint **/*.js"
17+
},
1518
"dependencies": {
1619
"@cubejs-backend/query-orchestrator": "^0.13.2",
1720
"sqlite3": "^4.1.0"
1821
},
1922
"license": "Apache-2.0",
23+
"devDependencies": {
24+
"eslint": "^5.16.0",
25+
"eslint-config-airbnb-base": "^13.1.0",
26+
"eslint-plugin-import": "^2.16.0",
27+
"eslint-plugin-node": "^5.2.1",
28+
"mocha": "^5.2.0",
29+
"should": "^13.2.3",
30+
"testcontainers": "^1.1.10"
31+
},
2032
"publishConfig": {
2133
"access": "public"
2234
}

0 commit comments

Comments
 (0)