2
0
mirror of https://github.com/frappe/books.git synced 2024-12-26 20:30:25 +00:00
books/backends/sqlite.js

109 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-01-16 06:09:17 +00:00
const frappe = require('frappejs');
const Database = require('./database');
2018-01-12 12:25:07 +00:00
2019-11-20 09:38:32 +00:00
class SqliteDatabase extends Database {
constructor({ dbPath }) {
super();
this.dbPath = dbPath;
2019-12-09 19:57:26 +00:00
this.connectionParams = {
client: 'sqlite3',
connection: {
filename: this.dbPath
},
pool: {
afterCreate(conn, done) {
conn.run('PRAGMA foreign_keys=ON');
done();
2019-01-12 12:10:52 +00:00
}
2019-12-09 19:57:26 +00:00
},
useNullAsDefault: true,
asyncStackTraces: process.env.NODE_ENV === 'development'
2019-12-09 19:57:26 +00:00
};
}
2018-01-31 13:04:46 +00:00
async addForeignKeys(doctype, newForeignKeys) {
2019-12-09 19:57:26 +00:00
await this.sql('PRAGMA foreign_keys=OFF');
await this.sql('BEGIN TRANSACTION');
2018-02-20 09:53:38 +00:00
2019-12-09 19:57:26 +00:00
const tempName = 'TEMP' + doctype;
2018-03-05 16:45:21 +00:00
// create temp table
await this.createTable(doctype, tempName);
2018-03-05 16:45:21 +00:00
// copy from old to new table
2019-12-09 19:57:26 +00:00
await this.knex(tempName).insert(this.knex.select().from(doctype));
2018-02-20 09:53:38 +00:00
// drop old table
2019-12-09 19:57:26 +00:00
await this.knex.schema.dropTable(doctype);
2018-02-20 09:53:38 +00:00
// rename new table
2019-12-09 19:57:26 +00:00
await this.knex.schema.renameTable(tempName, doctype);
2018-02-20 09:53:38 +00:00
2019-12-09 19:57:26 +00:00
await this.sql('COMMIT');
await this.sql('PRAGMA foreign_keys=ON');
}
2018-02-20 09:53:38 +00:00
removeColumns() {
// pass
}
2018-03-05 16:45:21 +00:00
async getTableColumns(doctype) {
return (await this.sql(`PRAGMA table_info(${doctype})`)).map(d => d.name);
}
async getForeignKeys(doctype) {
2019-12-09 19:57:26 +00:00
return (await this.sql(`PRAGMA foreign_key_list(${doctype})`)).map(
d => d.from
);
}
initTypeMap() {
2019-12-09 19:57:26 +00:00
// prettier-ignore
this.typeMap = {
2019-12-09 19:57:26 +00:00
'AutoComplete': 'text',
'Currency': 'float',
'Int': 'integer',
'Float': 'float',
'Percent': 'float',
'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',
'AttachImage': 'text',
'Signature': 'text',
'Color': 'text',
'Barcode': 'text',
'Geolocation': 'text'
};
}
2019-11-15 07:44:45 +00:00
2019-11-20 09:38:32 +00:00
getError(err) {
let errorType = frappe.errors.DatabaseError;
2019-11-20 09:38:32 +00:00
if (err.message.includes('FOREIGN KEY')) {
errorType = frappe.errors.LinkValidationError;
2019-11-20 09:38:32 +00:00
}
if (err.message.includes('SQLITE_ERROR: cannot commit')) {
errorType = frappe.errors.CannotCommitError;
2019-11-20 09:38:32 +00:00
}
if (err.message.includes('SQLITE_CONSTRAINT: UNIQUE constraint failed:')) {
errorType = frappe.errors.DuplicateEntryError;
}
return errorType;
2019-11-15 07:44:45 +00:00
}
2018-01-12 12:25:07 +00:00
}
2019-11-20 09:38:32 +00:00
module.exports = SqliteDatabase;