Skip to content

Commit

Permalink
feat(sqlite-driver): Pre-aggregations support
Browse files Browse the repository at this point in the history
  • Loading branch information
paveltiunov committed Dec 16, 2019
1 parent 22f77a6 commit 5ffb3d2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 17 deletions.
4 changes: 4 additions & 0 deletions packages/cubejs-schema-compiler/adapter/SqliteQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class SqliteQuery extends BaseQuery {
).join(' UNION ALL ');
return `SELECT dates.f date_from, dates.t date_to FROM (${values}) AS dates`;
}

nowTimestampSql() {
return `strftime('%Y-%m-%dT%H:%M:%fZ', 'now')`;
}
}

module.exports = SqliteQuery;
55 changes: 38 additions & 17 deletions packages/cubejs-sqlite-driver/driver/SqliteDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,46 @@ class SqliteDriver extends BaseDriver {
const tables = await this.query(query);

return {
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] };
})
}), {}),
main: 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] };
})
}), {}),
};
}

createSchemaIfNotExists(schemaName) {
return this.query(
`PRAGMA database_list`
).then((schemas) => {
if (!schemas.find(s => s.name === schemaName)) {
return this.query(`ATTACH DATABASE ${schemaName} AS ${schemaName}`);
}
return null;
});
}

async getTablesQuery(schemaName) {
const attachedDatabases = await this.query(
`PRAGMA database_list`
);
if (!attachedDatabases.find(s => s.name === schemaName)) {
return [];
}
return this.query(`SELECT name as table_name FROM ${schemaName}.sqlite_master WHERE type='table' ORDER BY name`);
}
}

module.exports = SqliteDriver;
12 changes: 12 additions & 0 deletions packages/cubejs-sqlite-driver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@
"node": ">=8.11.1"
},
"main": "driver/SqliteDriver.js",
"scripts": {
"lint": "eslint **/*.js"
},
"dependencies": {
"@cubejs-backend/query-orchestrator": "^0.13.2",
"sqlite3": "^4.1.0"
},
"license": "Apache-2.0",
"devDependencies": {
"eslint": "^5.16.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-node": "^5.2.1",
"mocha": "^5.2.0",
"should": "^13.2.3",
"testcontainers": "^1.1.10"
},
"publishConfig": {
"access": "public"
}
Expand Down

0 comments on commit 5ffb3d2

Please sign in to comment.