Skip to content

Instantly share code, notes, and snippets.

@leapingbytes
Created August 28, 2013 06:59
Show Gist options
  • Save leapingbytes/6362916 to your computer and use it in GitHub Desktop.
Save leapingbytes/6362916 to your computer and use it in GitHub Desktop.
Extension to Ext.data.proxy.Sql to allow specifying JOINs You are free to use this code in any of your projects (open source or otherwise). I would appreciate if you mentioned my name (Andrei Tchijov @ Leaping Bytes, LLC) if you do.
Ext.define('LB.store.SqlJoinProxy', {
alias: 'proxy.sqljoin',
extend: 'Ext.data.proxy.Sql',
config: {
/**
* config below will join table image ON image_id = i.id and will select field image_thumbnail as thumbnail and
* field image_full_size as full_size
*
* joins: [{
* 'table' : 'image',
* 'alias' : 'i',
* 'foreignKey' : 'image_id',
* 'fields' : {
* 'thumbnail' : 'image_thumbnail',
* 'full_size' : 'image_full_size'
* }
* }]
*/
joins: []
},
buildJoinedSQL: function(joins, mainAlias) {
var result = {
'fields' : '',
'joins' : ''
};
for (var i = 0; i < joins.length; i++) {
var join = joins[i];
result.joins += ' LEFT JOIN ' + join.table + ' ' + join.alias + ' ON ( ' + join.alias + '.id = ' + mainAlias + '.' + join.foreignKey + ') ';
for(var f in join.fields) {
result.fields += ', ' + join.alias + '.' + f + ' as ' + join.fields[f];
}
}
return result;
},
selectRecords: function(transaction, params, callback, scope) {
var me = this,
table = me.getTable(),
idProperty = me.getModel().getIdProperty(),
join_sqls = me.buildJoinedSQL(me.getJoins(), 't'),
sql = 'SELECT t.* ' + join_sqls.fields + ' FROM ' + table + ' t ' + join_sqls.joins,
records = [],
filterStatement = ' WHERE ',
sortStatement = ' ORDER BY ',
i, ln, data, result, count, rows, filter, sorter, property, value;
result = new Ext.data.ResultSet({
records: records,
success: true
});
if (!Ext.isObject(params)) {
sql += filterStatement + 't.' + idProperty + ' = ' + params;
} else {
ln = params.filters && params.filters.length;
if (ln) {
for (i = 0; i < ln; i++) {
filter = params.filters[i];
property = filter.getProperty();
value = filter.getValue();
if (property !== null) {
property = property.indexOf('.') > 0 ? property : 't.' + property;
sql += filterStatement + property + ' ' + (filter.getAnyMatch() ? ('LIKE \'%' + value + '%\'') : ('= \'' + value + '\''));
filterStatement = ' AND ';
}
}
}
ln = params.sorters && params.sorters.length;
if (ln) {
for (i = 0; i < ln; i++) {
sorter = params.sorters[i];
property = sorter.getProperty();
if (property !== null) {
property = property.indexOf('.') > 0 ? property : 't.' + property;
sql += sortStatement + property + ' ' + sorter.getDirection();
sortStatement = ', ';
}
}
}
// handle start, limit, sort, filter and group params
if (params.page !== undefined) {
sql += ' LIMIT ' + parseInt(params.start, 10) + ', ' + parseInt(params.limit, 10);
}
}
transaction.executeSql(sql, null,
function(transaction, resultSet) {
rows = resultSet.rows;
count = rows.length;
for (i = 0, ln = count; i < ln; i++) {
data = rows.item(i);
records.push({
clientId: null,
id: data[idProperty],
data: data,
node: data
});
}
result.setSuccess(true);
result.setTotal(count);
result.setCount(count);
if (typeof callback == 'function') {
callback.call(scope || me, result)
}
},
function(transaction, errors) {
result.setSuccess(false);
result.setTotal(0);
result.setCount(0);
if (typeof callback == 'function') {
callback.call(scope || me, result)
}
}
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment