2018-01-16 06:09:17 +00:00
|
|
|
const frappe = require('frappejs');
|
2018-01-12 12:25:07 +00:00
|
|
|
const sqlite3 = require('sqlite3').verbose();
|
2018-02-08 06:46:38 +00:00
|
|
|
const Database = require('./database');
|
2018-02-07 13:23:52 +00:00
|
|
|
const debug = false;
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-02-08 06:46:38 +00:00
|
|
|
module.exports = class sqliteDatabase extends Database {
|
2018-08-18 15:54:17 +00:00
|
|
|
constructor({ dbPath }) {
|
|
|
|
super();
|
|
|
|
this.dbPath = dbPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(dbPath) {
|
|
|
|
if (dbPath) {
|
|
|
|
this.dbPath = dbPath;
|
2018-01-31 13:04:46 +00:00
|
|
|
}
|
2019-01-12 12:10:52 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.conn = new sqlite3.Database(this.dbPath, (err) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
2018-08-18 15:54:17 +00:00
|
|
|
if (debug) {
|
|
|
|
this.conn.on('trace', (trace) => console.log(trace));
|
2018-01-31 13:04:46 +00:00
|
|
|
}
|
2018-08-18 15:54:17 +00:00
|
|
|
this.run('PRAGMA foreign_keys=ON').then(resolve);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-01-31 13:04:46 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
async tableExists(table) {
|
|
|
|
const name = await this.sql(`SELECT name FROM sqlite_master WHERE type='table' AND name='${table}'`);
|
|
|
|
return (name && name.length) ? true : false;
|
|
|
|
}
|
2018-01-31 13:04:46 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
async addForeignKeys(doctype, newForeignKeys) {
|
|
|
|
await this.run('PRAGMA foreign_keys=OFF');
|
|
|
|
await this.run('BEGIN TRANSACTION');
|
2018-02-20 09:53:38 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
const tempName = 'TEMP' + doctype
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
// create temp table
|
|
|
|
await this.createTable(doctype, tempName);
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
const columns = (await this.getTableColumns(tempName)).join(', ');
|
2018-02-20 09:53:38 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
// copy from old to new table
|
|
|
|
await this.run(`INSERT INTO ${tempName} (${columns}) SELECT ${columns} from ${doctype}`);
|
2018-02-20 09:53:38 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
// drop old table
|
|
|
|
await this.run(`DROP TABLE ${doctype}`);
|
2018-02-20 09:53:38 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
// rename new table
|
|
|
|
await this.run(`ALTER TABLE ${tempName} RENAME TO ${doctype}`);
|
2018-02-20 09:53:38 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
await this.run('COMMIT');
|
|
|
|
await this.run('PRAGMA foreign_keys=ON');
|
|
|
|
}
|
2018-02-20 09:53:38 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
removeColumns() {
|
|
|
|
// pass
|
|
|
|
}
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
async runCreateTableQuery(doctype, columns, indexes) {
|
|
|
|
const query = `CREATE TABLE IF NOT EXISTS ${doctype} (
|
2018-02-20 09:53:38 +00:00
|
|
|
${columns.join(", ")} ${indexes.length ? (", " + indexes.join(", ")) : ''})`;
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
return await this.run(query);
|
|
|
|
}
|
2018-02-23 16:17:55 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
updateColumnDefinition(field, columns, indexes) {
|
|
|
|
let def = this.getColumnDefinition(field);
|
2018-02-23 16:17:55 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
columns.push(def);
|
2018-03-29 14:28:31 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
if (field.fieldtype === 'Link' && field.target) {
|
2019-10-19 14:33:08 +00:00
|
|
|
let meta = frappe.getMeta(field.target);
|
|
|
|
indexes.push(`FOREIGN KEY (${field.fieldname}) REFERENCES ${meta.getBaseDocType()} ON UPDATE CASCADE ON DELETE RESTRICT`);
|
2018-01-31 13:04:46 +00:00
|
|
|
}
|
2018-08-18 15:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getColumnDefinition(field) {
|
2019-10-19 14:30:31 +00:00
|
|
|
let defaultValue = field.default;
|
|
|
|
if (typeof defaultValue === 'string') {
|
|
|
|
defaultValue = `'${defaultValue}'`
|
|
|
|
}
|
2018-08-18 15:54:17 +00:00
|
|
|
let def = [
|
|
|
|
field.fieldname,
|
|
|
|
this.typeMap[field.fieldtype],
|
|
|
|
field.fieldname === 'name' ? 'PRIMARY KEY NOT NULL' : '',
|
|
|
|
field.required ? 'NOT NULL' : '',
|
2019-10-19 14:30:31 +00:00
|
|
|
field.default ? `DEFAULT ${defaultValue}` : ''
|
2018-08-18 15:54:17 +00:00
|
|
|
].join(' ');
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getTableColumns(doctype) {
|
|
|
|
return (await this.sql(`PRAGMA table_info(${doctype})`)).map(d => d.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getForeignKeys(doctype) {
|
|
|
|
return (await this.sql(`PRAGMA foreign_key_list(${doctype})`)).map(d => d.from);
|
|
|
|
}
|
|
|
|
|
|
|
|
async runAddColumnQuery(doctype, field, values) {
|
|
|
|
await this.run(`ALTER TABLE ${doctype} ADD COLUMN ${this.getColumnDefinition(field)}`, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
getOne(doctype, name, fields = '*') {
|
2019-10-05 21:47:09 +00:00
|
|
|
let meta = frappe.getMeta(doctype);
|
|
|
|
let baseDoctype = meta.getBaseDocType();
|
2018-08-18 15:54:17 +00:00
|
|
|
fields = this.prepareFields(fields);
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-10-05 21:47:09 +00:00
|
|
|
this.conn.get(`select ${fields} from ${baseDoctype}
|
2018-01-12 12:25:07 +00:00
|
|
|
where name = ?`, name,
|
2018-08-18 15:54:17 +00:00
|
|
|
(err, row) => {
|
|
|
|
resolve(row || {});
|
2018-01-31 13:04:46 +00:00
|
|
|
});
|
2018-08-18 15:54:17 +00:00
|
|
|
});
|
|
|
|
}
|
2018-01-31 13:04:46 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
async insertOne(doctype, doc) {
|
|
|
|
let fields = this.getKeys(doctype);
|
|
|
|
let placeholders = fields.map(d => '?').join(', ');
|
2018-01-31 13:04:46 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
if (!doc.name) {
|
|
|
|
doc.name = frappe.getRandomString();
|
|
|
|
}
|
2018-02-01 11:07:36 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
return await this.run(`insert into ${doctype}
|
2018-02-01 11:07:36 +00:00
|
|
|
(${fields.map(field => field.fieldname).join(", ")})
|
2018-02-08 06:46:38 +00:00
|
|
|
values (${placeholders})`, this.getFormattedValues(fields, doc));
|
2018-08-18 15:54:17 +00:00
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
async updateOne(doctype, doc) {
|
|
|
|
let fields = this.getKeys(doctype);
|
|
|
|
let assigns = fields.map(field => `${field.fieldname} = ?`);
|
|
|
|
let values = this.getFormattedValues(fields, doc);
|
2018-01-31 12:56:21 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
// additional name for where clause
|
|
|
|
values.push(doc.name);
|
2018-01-31 12:56:21 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
return await this.run(`update ${doctype}
|
2018-01-12 12:25:07 +00:00
|
|
|
set ${assigns.join(", ")} where name=?`, values);
|
2018-08-18 15:54:17 +00:00
|
|
|
}
|
2018-01-31 13:04:46 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
async runDeleteOtherChildren(field, added) {
|
|
|
|
// delete other children
|
|
|
|
// `delete from doctype where parent = ? and name not in (?, ?, ?)}`
|
|
|
|
await this.run(`delete from ${field.childtype}
|
2018-02-08 06:46:38 +00:00
|
|
|
where
|
|
|
|
parent = ? and
|
|
|
|
name not in (${added.slice(1).map(d => '?').join(', ')})`, added);
|
2018-08-18 15:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async deleteOne(doctype, name) {
|
|
|
|
return await this.run(`delete from ${doctype} where name=?`, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteChildren(parenttype, parent) {
|
|
|
|
await this.run(`delete from ${parenttype} where parent=?`, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteSingleValues(name) {
|
|
|
|
await frappe.db.run('delete from SingleValue where parent=?', name)
|
|
|
|
}
|
|
|
|
|
2019-10-05 21:46:14 +00:00
|
|
|
async rename(doctype, oldName, newName) {
|
2019-10-05 21:47:09 +00:00
|
|
|
let meta = frappe.getMeta(doctype);
|
|
|
|
let baseDoctype = meta.getBaseDocType();
|
2019-10-05 21:46:14 +00:00
|
|
|
await frappe.db.run(`update ${baseDoctype} set name = ? where name = ?`, [newName, oldName]);
|
|
|
|
await frappe.db.commit();
|
|
|
|
}
|
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
async setValues(doctype, name, fieldValuePair) {
|
|
|
|
const meta = frappe.getMeta(doctype);
|
2019-10-05 21:47:09 +00:00
|
|
|
const baseDoctype = meta.getBaseDocType();
|
2018-08-18 15:54:17 +00:00
|
|
|
const validFields = this.getKeys(doctype);
|
|
|
|
const validFieldnames = validFields.map(df => df.fieldname);
|
|
|
|
const fieldsToUpdate = Object.keys(fieldValuePair)
|
|
|
|
.filter(fieldname => validFieldnames.includes(fieldname))
|
|
|
|
|
|
|
|
// assignment part of query
|
|
|
|
const assigns = fieldsToUpdate.map(fieldname => `${fieldname} = ?`);
|
|
|
|
|
|
|
|
// values
|
|
|
|
const values = fieldsToUpdate.map(fieldname => {
|
|
|
|
const field = meta.getField(fieldname);
|
|
|
|
const value = fieldValuePair[fieldname];
|
|
|
|
return this.getFormattedValue(field, value);
|
|
|
|
});
|
|
|
|
|
|
|
|
// additional name for where clause
|
|
|
|
values.push(name);
|
|
|
|
|
2019-10-05 21:47:09 +00:00
|
|
|
return await this.run(`update ${baseDoctype}
|
2018-08-18 15:54:17 +00:00
|
|
|
set ${assigns.join(', ')} where name=?`, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
getAll({ doctype, fields, filters, start, limit, orderBy = 'modified', groupBy, order = 'desc' } = {}) {
|
2019-10-05 21:47:09 +00:00
|
|
|
let meta = frappe.getMeta(doctype);
|
|
|
|
let baseDoctype = meta.getBaseDocType();
|
2018-08-18 15:54:17 +00:00
|
|
|
if (!fields) {
|
2019-10-05 21:47:09 +00:00
|
|
|
fields = meta.getKeywordFields();
|
2018-01-31 13:04:46 +00:00
|
|
|
}
|
2018-08-18 15:54:17 +00:00
|
|
|
if (typeof fields === 'string') {
|
|
|
|
fields = [fields];
|
2018-02-01 11:07:36 +00:00
|
|
|
}
|
2019-10-05 21:47:09 +00:00
|
|
|
if (meta.filters) {
|
2019-10-19 14:33:23 +00:00
|
|
|
filters = Object.assign({}, filters, meta.filters);
|
2019-10-05 21:47:09 +00:00
|
|
|
}
|
2018-02-01 11:07:36 +00:00
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let conditions = this.getFilterConditions(filters);
|
|
|
|
let query = `select ${fields.join(", ")}
|
2019-10-05 21:47:09 +00:00
|
|
|
from ${baseDoctype}
|
2018-01-12 12:25:07 +00:00
|
|
|
${conditions.conditions ? "where" : ""} ${conditions.conditions}
|
2018-04-29 09:07:04 +00:00
|
|
|
${groupBy ? ("group by " + groupBy.join(', ')) : ""}
|
2018-04-26 10:19:53 +00:00
|
|
|
${orderBy ? ("order by " + orderBy) : ""} ${orderBy ? (order || "asc") : ""}
|
2018-02-12 12:01:31 +00:00
|
|
|
${limit ? ("limit " + limit) : ""} ${start ? ("offset " + start) : ""}`;
|
|
|
|
|
2018-08-18 15:54:17 +00:00
|
|
|
this.conn.all(query, conditions.values,
|
|
|
|
(err, rows) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(rows);
|
|
|
|
}
|
2018-01-31 13:04:46 +00:00
|
|
|
});
|
2018-08-18 15:54:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
run(query, params) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.conn.run(query, params, (err) => {
|
|
|
|
if (err) {
|
2019-01-12 12:10:52 +00:00
|
|
|
console.error('Error in sql:', query);
|
2018-08-18 15:54:17 +00:00
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve();
|
2018-01-31 13:04:46 +00:00
|
|
|
}
|
2018-08-18 15:54:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
sql(query, params) {
|
2019-01-12 12:10:52 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-08-18 15:54:17 +00:00
|
|
|
this.conn.all(query, params, (err, rows) => {
|
2019-01-12 12:10:52 +00:00
|
|
|
if (err) {
|
|
|
|
console.error('Error in sql:', query);
|
|
|
|
reject(err)
|
|
|
|
}
|
2018-08-18 15:54:17 +00:00
|
|
|
resolve(rows);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async commit() {
|
|
|
|
try {
|
|
|
|
await this.run('commit');
|
|
|
|
} catch (e) {
|
|
|
|
if (e.errno !== 1) {
|
|
|
|
throw e;
|
|
|
|
}
|
2018-01-31 13:04:46 +00:00
|
|
|
}
|
2018-08-18 15:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
initTypeMap() {
|
|
|
|
this.typeMap = {
|
2019-10-26 14:46:34 +00:00
|
|
|
'AutoComplete': 'text'
|
2018-08-18 15:54:17 +00:00
|
|
|
, 'Currency': 'real'
|
|
|
|
, 'Int': 'integer'
|
|
|
|
, 'Float': 'real'
|
|
|
|
, 'Percent': 'real'
|
|
|
|
, 'Check': 'integer'
|
|
|
|
, 'Small Text': 'text'
|
|
|
|
, 'Long Text': 'text'
|
|
|
|
, 'Code': 'text'
|
|
|
|
, 'Text Editor': 'text'
|
|
|
|
, 'Date': 'text'
|
|
|
|
, 'Datetime': 'text'
|
|
|
|
, 'Time': 'text'
|
|
|
|
, 'Text': 'text'
|
|
|
|
, 'Data': 'text'
|
|
|
|
, 'Link': 'text'
|
|
|
|
, 'DynamicLink': 'text'
|
|
|
|
, 'Password': 'text'
|
|
|
|
, 'Select': 'text'
|
|
|
|
, 'Read Only': 'text'
|
|
|
|
, 'File': 'text'
|
|
|
|
, 'Attach': 'text'
|
|
|
|
, 'Attach Image': 'text'
|
|
|
|
, 'Signature': 'text'
|
|
|
|
, 'Color': 'text'
|
|
|
|
, 'Barcode': 'text'
|
|
|
|
, 'Geolocation': 'text'
|
2018-01-31 13:04:46 +00:00
|
|
|
}
|
2018-08-18 15:54:17 +00:00
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|