Skip to content

Added query override and ability for filter OR's #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
45 changes: 0 additions & 45 deletions sqlite/Connection.js

This file was deleted.

168 changes: 106 additions & 62 deletions sqlite/data/proxy/SqliteStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Ext.define('Sqlite.data.proxy.SqliteStorage', {
me.setTable();
},


/* INTERFACE FUNCTIONS */

//inherit docs
Expand Down Expand Up @@ -116,17 +115,17 @@ Ext.define('Sqlite.data.proxy.SqliteStorage', {
if (typeof callback == 'function') {
callback.call(scope, operation);
}
},
onError = function (tx, err) {
operation.setCompleted();
operation.setException(err ? err : '');
if (typeof callback == 'function') {
callback.call(scope, operation);
}
};
};

operation.setStarted();
onError = function (tx, err) {
operation.setCompleted();
operation.setException(err ? err : '');
if (typeof callback == 'function') {
callback.call(scope, operation);
}
};

operation.setStarted();
for (i = 0; i < length; i++) {
queries.push(this.getDeleteRecordFunc(records[i], me.getDbConfig().tablename));
}
Expand Down Expand Up @@ -154,20 +153,42 @@ Ext.define('Sqlite.data.proxy.SqliteStorage', {
filters = operation.getFilters(),
fieldTypes = {};

grouper = this.applyGrouper(grouper);
filters = this.applyFilters(filters);
sorters = this.applySorters(sorters);

// generate sql
var sql = "SELECT _ROWID_,*\nFROM " + me.getDbConfig().tablename;
if (filters != null) sql += me.whereClause(filters);
if (grouper != null) sql += me.groupClause(grouper);
if (sorters != null) sql += me.orderClause(sorters);
if (limit != null && start != null) sql += me.limitClause(limit, start);

if(!operation.config.hasOwnProperty("query")) {
grouper = this.applyGrouper(grouper);
filters = this.applyFilters(filters);
sorters = this.applySorters(sorters);

// generate sql
if (filters != null) sql += me.whereClause(filters);
if (grouper != null) sql += me.groupClause(grouper);
if (sorters != null) sql += me.orderClause(sorters);
}else{
sql = operation.config.query;
}

var regExp = /select(.*\n*)from/gi;
var sql_count = sql.replace(regExp, "SELECT count(*) as total FROM");
if (!isNaN(limit) && limit != null) sql += me.limitClause(limit, start);
var onSuccess, onError;

var countGroups = operation.config.hasOwnProperty("countTotals") ? operation.config.countTotals : true;
onSuccess = function (tx, results) {
me.applyDataToModel(tx, results, operation, callback, scope);
me.transactionDB(me.getDb(), [function (tx) {
tx.executeSql(sql_count, [], function(count_tx, count_results){
var total_count = count_results.rows.length;
if(countGroups){
total_count = 0;
for (var i=0;i<count_results.rows.length;i++) {
var item = count_results.rows.item(i);
total_count += parseInt(item.total);
}
}
me.applyDataToModel(tx, results, total_count, operation, callback, scope);
}, onError);
}], Ext.emptyFn, Ext.emptyFn);

};

onError = function (tx, err) {
Expand All @@ -181,9 +202,14 @@ Ext.define('Sqlite.data.proxy.SqliteStorage', {


/* GENERAL DB FUNCTIONS */

getDb: function () {
return this.getDbConfig().dbConn.dbConn;
if(!this.cachedDb){
this.cachedDb = this.getDbConfig().dbConn;
if(Ext.isFunction(this.cachedDb)) this.cachedDb = this.cachedDb();
if(this.cachedDb.hasOwnProperty("dbConn")) this.cachedDb = this.cachedDb.dbConn;
}

return this.cachedDb;
},

throwDbError: function (tx, err) {
Expand Down Expand Up @@ -366,6 +392,7 @@ Ext.define('Sqlite.data.proxy.SqliteStorage', {

whereClause: function (filters) {
var me = this,
firstProperty = true,
firstFilter = true,
sql = '',
fieldTypes = {};
Expand All @@ -375,43 +402,60 @@ Ext.define('Sqlite.data.proxy.SqliteStorage', {
});

Ext.each(filters, function (filter) {
// need to make sure this property is in the database
if (!Ext.isDefined(fieldTypes[filter.getProperty()]))
return;
//force filter property to be an array
var filterProperties = Ext.isArray(filter.getProperty()) ?filter.getProperty() : [filter.getProperty()];
var filterValues = Ext.isArray(filter.getValue()) ?filter.getValue() : [filter.getValue()];

if (!firstFilter) sql += "\n AND";
else sql += "\nWHERE\n ";
firstFilter = false;
//Confirm all filter properties are fields
if(!Ext.each(filterProperties, function(filterProperty){
// need to make sure this property is in the database
if (!Ext.isDefined(fieldTypes[filterProperty])) return false;
})) return;

sql += ' `' + filter.getProperty() + '`';
var propertyConcat = filter.config.hasOwnProperty("propertyIsAny") ? filter.config.propertyIsAny ? "AND" : "OR" : "AND";
var valueConcat = filter.config.hasOwnProperty("valueIsAny") ? filter.config.valueIsAny ? "AND" : "OR" : "OR";

// now: do we use like or =?
var fieldType = fieldTypes[filter.getProperty()].toUpperCase();

if(typeof filter.getFilterFn() == 'string'){
sql += " " + filter.getFilterFn();
}else{
if (fieldType == 'TEXT' &&
!filter.getCaseSensitive()) sql += ' LIKE';
else sql += ' =';
}
if (!firstFilter) sql += "\n "+propertyConcat ;
else sql += "\nWHERE\n ";
firstFilter = false;
firstProperty = true;

Ext.each(filterProperties, function(filterProperty){
Ext.each(filterValues, function(filterValue){
//Separate all properties with OR
if (!firstProperty) sql += " "+valueConcat+" ";
firstProperty = false;

sql += ' `' + filterProperty + '`';
var fieldType = fieldTypes[filterProperty].toUpperCase();

// now: do we use like or =?
if(typeof filter.getFilterFn() == 'string'){
sql += " " + filter.getFilterFn();
}else{
if (fieldType == 'TEXT' &&
!filter.getCaseSensitive()) sql += ' LIKE';
else sql += ' =';
}

// need to surround with %?
if (!filter.getExactMatch() &&
fieldType == 'TEXT') {
sql += " '%" + filter.getValue() + "%'";
} else if (fieldType == 'TEXT') {
sql += " '" + filter.getValue() + "'";
} else if (fieldType == 'boolean') {
if (filter.getValue()) {
sql += " 'true'";
}
else {
sql += " 'false'";
}
} else {
sql += ' ' + filter.getValue();
}
// need to surround with %?
if (!filter.getExactMatch() &&
fieldType == 'TEXT') {
sql += " '%" + filterValue + "%'";
} else if (fieldType == 'TEXT') {
sql += " '" + filterValue + "'";
} else if (fieldType == 'boolean') {
if (filter.getValue()) {
sql += " 'true'";
}
else {
sql += " 'false'";
}
} else {
sql += ' ' + filterValue;
}
});
});
});

return sql;
Expand All @@ -420,7 +464,6 @@ Ext.define('Sqlite.data.proxy.SqliteStorage', {
orderClause: function (sorters) {
var me = this,
sql = '',
orders = [],
fields = {},
firstOrder = true;

Expand Down Expand Up @@ -480,8 +523,9 @@ Ext.define('Sqlite.data.proxy.SqliteStorage', {
},
limitClause: function (limit, start) {
var sql = "\nLIMIT";
if (start != null) sql += ' ' + start + ',';
if (limit != null) sql += ' ' + limit;
if(isNaN(start) || start == null) start = 0;
sql += ' ' + start + ',';
sql += ' ' + limit;
return sql;
},

Expand All @@ -497,15 +541,15 @@ Ext.define('Sqlite.data.proxy.SqliteStorage', {
return data;
},

applyData: function (data, operation, callback, scope) {
applyData: function (data, total_count, operation, callback, scope) {
var me = this;

operation.setSuccessful();
operation.setCompleted();

operation.setResultSet(Ext.create('Ext.data.ResultSet', {
records: data,
total: data.length,
total: total_count,
loaded: true
}));

Expand All @@ -517,7 +561,7 @@ Ext.define('Sqlite.data.proxy.SqliteStorage', {
}
},

applyDataToModel: function (tx, results, operation, callback, scope) {
applyDataToModel: function (tx, results, total_count, operation, callback, scope) {

var me = this,
Model = me.getModel(),
Expand Down Expand Up @@ -545,7 +589,7 @@ Ext.define('Sqlite.data.proxy.SqliteStorage', {
}
}

me.applyData(storedatas, operation, callback, scope);
me.applyData(storedatas, total_count, operation, callback, scope);
},


Expand Down