Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions packages/cubejs-sqlite-driver/driver/SqliteDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SqliteDriver extends BaseDriver {
const dataSource =
config.dataSource ||
assertDataSource('default');

this.config = {
database: getEnv('dbName', { dataSource }),
...config
Expand Down Expand Up @@ -64,39 +64,33 @@ class SqliteDriver extends BaseDriver {

informationSchemaQuery() {
return `
SELECT name, sql
SELECT name
FROM sqlite_master
WHERE type='table'
AND name!='sqlite_sequence'
ORDER BY name
`;
}

tableColumnsQuery(tableName) {
return `
SELECT name, type
FROM pragma_table_info('${tableName}')
`;
}

async tablesSchema() {
const query = this.informationSchemaQuery();

const tables = await this.query(query);

const tableColumns = await Promise.all(tables.map(async table => {
const columns = await this.query(this.tableColumnsQuery(table.name));
return [table.name, columns];
}));

return {
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()
// replace \t with whitespace
.replace(/\t/g, ' ')
// obtain "([|`|")?name(]|`|")? type"
.match(/([|`|"])?([^[\]"`]+)(]|`|")?\s+(\w+)/);
return { name: match[2], type: match[4] };
})
}), {}),
main: Object.fromEntries(tableColumns)
};
}

Expand Down
6 changes: 4 additions & 2 deletions packages/cubejs-sqlite-driver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"main": "driver/SqliteDriver.js",
"typings": "driver/index.d.ts",
"scripts": {
"lint": "eslint **/*.js"
"lint": "eslint **/*.js",
"unit": "jest"
},
"dependencies": {
"@cubejs-backend/base-driver": "1.3.78",
Expand All @@ -23,7 +24,8 @@
},
"license": "Apache-2.0",
"devDependencies": {
"@cubejs-backend/linter": "1.3.78"
"@cubejs-backend/linter": "1.3.78",
"jest": "^29"
},
"publishConfig": {
"access": "public"
Expand Down
53 changes: 53 additions & 0 deletions packages/cubejs-sqlite-driver/test/SqliteDriver.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* globals describe, test, expect, beforeEach */
const sqlite3 = require('sqlite3');
const SqliteDriver = require('../driver/SqliteDriver.js');

describe('SqliteDriver', () => {
let driver;

beforeEach(() => {
const db = new sqlite3.Database(':memory:');
driver = new SqliteDriver({ db });
});

test('testConnection', async () => {
await driver.testConnection();
});

test('tableSchema', async () => {
await driver.query(`
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
age INTEGER,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
`);

await driver.query(`
CREATE TABLE groups (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
`);

const tableSchema = await driver.tablesSchema();

expect(tableSchema).toEqual({
main: {
users: [
{ name: 'id', type: 'INTEGER' },
{ name: 'name', type: 'TEXT' },
{ name: 'email', type: 'TEXT' },
{ name: 'age', type: 'INTEGER' },
{ name: 'created_at', type: 'DATETIME' },
],
groups: [
{ name: 'id', type: 'INTEGER' },
{ name: 'name', type: 'TEXT' },
]
}
});
});
});
Loading