Skip to content
Closed
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
52 changes: 52 additions & 0 deletions src/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,66 @@ export class SQLiteDatabaseClient {
element("tbody", rows.map(r => element("tr", columns.map(c => element("td", [text(r[c])])))))
]);
}
async describeSchema() {
return await this.query(`SELECT name FROM sqlite_master WHERE type = 'table'`);
}
async describeTable(specifier) {
const rows = await this.query(`SELECT * FROM pragma_table_info(?)`, [specifier]);
if (!rows.length) throw new Error(`table not found: ${specifier}`);
return rows.map(({name, type, notnull}) => {
const t = sqliteType(type);
return {
name,
type: notnull || t === "null" ? t : [t, "null"],
databaseType: type
};
});
}
async sql(strings, ...args) {
return this.query(strings.join("?"), args);
}
}

Object.defineProperty(SQLiteDatabaseClient.prototype, "dialect", {
value: "sqlite"
});

// https://www.sqlite.org/datatype3.html
function sqliteType(type) {
switch (type) {
case "NULL":
return "null";
case "INT":
case "INTEGER":
case "TINYINT":
case "SMALLINT":
case "MEDIUMINT":
case "BIGINT":
case "UNSIGNED BIG INT":
case "INT2":
case "INT8":
return "integer";
case "TEXT":
case "CLOB":
return "string";
case "REAL":
case "DOUBLE":
case "DOUBLE PRECISION":
case "FLOAT":
case "NUMERIC":
return "number";
case "BLOB":
return "buffer";
case "DATE":
case "DATETIME":
return "string"; // TODO convert strings to Date instances
default:
return /^(?:(?:(?:VARYING|NATIVE) )?CHARACTER|(?:N|VAR|NVAR)CHAR)\(/.test(type) ? "string"
: /^(?:DECIMAL|NUMERIC)\(/.test(type) ? "number"
: "other";
}
}

function load(source) {
return typeof source === "string" ? fetch(source).then(load)
: source instanceof Response || source instanceof Blob ? source.arrayBuffer().then(load)
Expand Down