1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-27 08:21:03 +02:00

Added modelSelectAll/One methods

This commit is contained in:
Laurent Cozic 2017-06-17 19:12:09 +01:00
parent cbbadf7d65
commit b0cc9ec18d
4 changed files with 55 additions and 38 deletions

View File

@ -90,8 +90,19 @@ class BaseModel {
return this.loadByField('id', id);
}
static modelSelectOne(sql, params = null) {
if (params === null) params = [];
return this.db().selectOne(sql, params);
}
static modelSelectAll(sql, params = null) {
if (params === null) params = [];
return this.db().selectAll(sql, params);
}
static loadByField(fieldName, fieldValue) {
return this.db().selectOne('SELECT * FROM ' + this.tableName() + ' WHERE `' + fieldName + '` = ?', [fieldValue]);
return this.modelSelectOne('SELECT * FROM ' + this.tableName() + ' WHERE `' + fieldName + '` = ?', [fieldValue]);
//return this.db().selectOne('SELECT * FROM ' + this.tableName() + ' WHERE `' + fieldName + '` = ?', [fieldValue]);
}
static applyPatch(model, patch) {
@ -170,33 +181,33 @@ class BaseModel {
queries.push(saveQuery);
// TODO: DISABLED DISABLED DISABLED DISABLED DISABLED DISABLED DISABLED DISABLED DISABLED DISABLED
if (0&& options.trackChanges && this.trackChanges()) {
// Cannot import this class the normal way due to cyclical dependencies between Change and BaseModel
// which are not handled by React Native.
const { Change } = require('src/models/change.js');
// if (options.trackChanges && this.trackChanges()) {
// // Cannot import this class the normal way due to cyclical dependencies between Change and BaseModel
// // which are not handled by React Native.
// const { Change } = require('src/models/change.js');
if (isNew) {
let change = Change.newChange();
change.type = Change.TYPE_CREATE;
change.item_id = itemId;
change.item_type = this.itemType();
// if (isNew) {
// let change = Change.newChange();
// change.type = Change.TYPE_CREATE;
// change.item_id = itemId;
// change.item_type = this.itemType();
queries.push(Change.saveQuery(change));
} else {
for (let n in o) {
if (!o.hasOwnProperty(n)) continue;
if (n == 'id') continue;
// queries.push(Change.saveQuery(change));
// } else {
// for (let n in o) {
// if (!o.hasOwnProperty(n)) continue;
// if (n == 'id') continue;
let change = Change.newChange();
change.type = Change.TYPE_UPDATE;
change.item_id = itemId;
change.item_type = this.itemType();
change.item_field = n;
// let change = Change.newChange();
// change.type = Change.TYPE_UPDATE;
// change.item_id = itemId;
// change.item_type = this.itemType();
// change.item_field = n;
queries.push(Change.saveQuery(change));
}
}
}
// queries.push(Change.saveQuery(change));
// }
// }
// }
return this.db().transactionExecBatch(queries).then(() => {
o = Object.assign({}, o);
@ -216,16 +227,16 @@ class BaseModel {
}
return this.db().exec('DELETE FROM ' + this.tableName() + ' WHERE id = ?', [id]).then(() => {
if (options.trackChanges && this.trackChanges()) {
const { Change } = require('src/models/change.js');
// if (options.trackChanges && this.trackChanges()) {
// const { Change } = require('src/models/change.js');
let change = Change.newChange();
change.type = Change.TYPE_DELETE;
change.item_id = id;
change.item_type = this.itemType();
// let change = Change.newChange();
// change.type = Change.TYPE_DELETE;
// change.item_id = id;
// change.item_type = this.itemType();
return Change.save(change);
}
// return Change.save(change);
// }
});
}

View File

@ -70,11 +70,13 @@ class Folder extends BaseItem {
}
static loadNoteByField(folderId, field, value) {
return this.db().selectOne('SELECT * FROM notes WHERE `parent_id` = ? AND `' + field + '` = ?', [folderId, value]);
return this.modelSelectAll('SELECT * FROM notes WHERE `parent_id` = ? AND `' + field + '` = ?', [folderId, value]);
//return this.db().selectOne('SELECT * FROM notes WHERE `parent_id` = ? AND `' + field + '` = ?', [folderId, value]);
}
static all() {
return this.db().selectAll('SELECT * FROM folders');
return this.modelSelectAll('SELECT * FROM folders');
// return this.db().selectAll('SELECT * FROM folders');
}
static save(o, options = null) {

View File

@ -41,11 +41,13 @@ class Note extends BaseItem {
}
static previews(parentId) {
return this.db().selectAll('SELECT ' + this.previewFieldsSql() + ' FROM notes WHERE parent_id = ?', [parentId]);
return this.modelSelectAll('SELECT ' + this.previewFieldsSql() + ' FROM notes WHERE parent_id = ?', [parentId]);
//return this.db().selectAll('SELECT ' + this.previewFieldsSql() + ' FROM notes WHERE parent_id = ?', [parentId]);
}
static preview(noteId) {
return this.db().selectOne('SELECT ' + this.previewFieldsSql() + ' FROM notes WHERE id = ?', [noteId]);
return this.modelSelectOne('SELECT ' + this.previewFieldsSql() + ' FROM notes WHERE id = ?', [noteId]);
//return this.db().selectOne('SELECT ' + this.previewFieldsSql() + ' FROM notes WHERE id = ?', [noteId]);
}
static updateGeolocation(noteId) {

View File

@ -79,11 +79,13 @@ class NoteFolderService extends BaseService {
// folders (TODO).
function getFolders(limit) {
return BaseModel.db().selectAll('SELECT * FROM folders WHERE sync_time < updated_time LIMIT ' + limit);
return Folder.modelSelectAll('SELECT * FROM folders WHERE sync_time < updated_time LIMIT ' + limit);
//return BaseModel.db().selectAll('SELECT * FROM folders WHERE sync_time < updated_time LIMIT ' + limit);
}
function getNotes(limit) {
return BaseModel.db().selectAll('SELECT * FROM notes WHERE sync_time < updated_time LIMIT ' + limit);
return Note.modelSelectAll('SELECT * FROM notes WHERE sync_time < updated_time LIMIT ' + limit);
//return BaseModel.db().selectAll('SELECT * FROM notes WHERE sync_time < updated_time LIMIT ' + limit);
}
return getFolders(limit).then((items) => {