};
/** @private */
- var _fetch = function (recordType, query) {
+ var _fetch = function (recordType, backingType, query) {
query = query || {};
var data = Object.create(XT.Data),
namespace = recordType.beforeDot(),
type = recordType.afterDot(),
+ tableNamespace = backingType.beforeDot(),
+ table = backingType.afterDot(),
customerId = null,
accountId = -1,
shiptoId,
clause,
keySearch = false,
extra = "",
- sql = 'select * ' +
- 'from %1$I.%2$I ' +
+ sql = 'select t1.* ' +
+ 'from %1$I.%2$I t1 {joins} ' +
'where {conditions} {extra}';
/* Handle special parameters */
/* Over-ride usual search behavior */
if (param.keySearch) {
keySearch = param.value;
- sql += ' and (number ~^ ${p1} or barcode ~^ ${p1}) ' +
+ sql += ' and (jt0.item_number ~^ ${p1} or jt0.item_upccode ~^ ${p1}) ' +
'union ' +
- 'select %2$I.* ' +
- 'from %1$I.%2$I ' +
- ' join itemsite on itemsite_id=id ' +
+ 'select t1.* ' +
+ 'from %1$I.%2$I t1 {joins} ' +
' join itemalias on itemsite_item_id=itemalias_item_id ' +
' and itemalias_crmacct_id is null ' +
'where {conditions} {extra} ' +
' and (itemalias_number ~^ ${p1}) ' +
'union ' +
- 'select %2$I.* ' +
- 'from %1$I.%2$I ' +
- ' join itemsite on itemsite_id=id ' +
+ 'select t1.* ' +
+ 'from %1$I.%2$I t1 {joins} ' +
' join itemalias on itemsite_item_id=itemalias_item_id ' +
' and itemalias_crmacct_id={accountId} ' +
'where {conditions} {extra} ' +
})
}
- clause = data.buildClause(namespace, type, query.parameters, query.orderBy);
+ clause = data.buildClauseOptimized(namespace, type, query.parameters, query.orderByColumns);
/* If customer passed, restrict results to item sites allowed to be sold to that customer */
if (customerId) {
- extra += ' and (item).id in (' +
+ extra += ' and jt0.item_id in (' + /* XXX jt0 is a dangerous assumption */
'select item_id from item where item_sold and not item_exclusive ' +
'union ' +
'select item_id from xt.custitem where cust_id=${p2} ' +
/* If vendor passed, and vendor can only supply against defined item sources, then restrict results */
if (vendorId) {
- extra += ' and (item).id in (' +
+ extra += ' and jt0.item_id in (' + /* XXX jt0 is a dangerous assumption */
' select itemsrc_item_id ' +
' from itemsrc ' +
' where itemsrc_active ' +
sql = XT.format(
sql += '{orderBy} %3$s %4$s;',
- [namespace.decamelize(), type.decamelize(), limit, offset]
+ [tableNamespace, table, limit, offset]
);
/* Query the model */
sql = sql.replace(/{conditions}/g, clause.conditions)
.replace(/{extra}/g, extra)
+ .replace('{joins}', clause.joins)
+ .replace('{joins}', clause.joins)
+ .replace('{joins}', clause.joins)
.replace('{orderBy}', clause.orderBy)
.replace('{limit}', limit)
.replace('{offset}', offset)
}
if (DEBUG) {
plv8.elog(NOTICE, 'sql = ', sql.slice(0,500));
- plv8.elog(NOTICE, 'sql = ', sql.slice(500, 1000));
- plv8.elog(NOTICE, 'sql = ', sql.slice(1000, 1500));
- plv8.elog(NOTICE, 'sql = ', sql.slice(1500, 2000));
+ plv8.elog(NOTICE, sql.slice(500, 1000));
+ plv8.elog(NOTICE, sql.slice(1000, 1500));
+ plv8.elog(NOTICE, sql.slice(1500, 2000));
+ plv8.elog(NOTICE, sql.slice(2000, 2500));
plv8.elog(NOTICE, 'parameters = ', clause.parameters);
}
return plv8.execute(sql, clause.parameters);
@returns {Array}
*/
XM.ItemSiteListItem.fetch = function (query) {
- return _fetch("XM.ItemSiteListItem", query);
+ return _fetch("XM.ItemSiteListItem", "public.itemsite", query);
};
if (!XM.ItemSiteRelation) { XM.ItemSiteRelation = {}; }
@returns {Array}
*/
XM.ItemSiteRelation.fetch = function (query) {
- return _fetch("XM.ItemSiteRelation", query);
+ return _fetch("XM.ItemSiteRelation", "xt.itemsiteinfo", query);
};
}());