Skip to content

Commit

Permalink
It is sometimes convenient, when using a .join() in a .query(), to pr…
Browse files Browse the repository at this point in the history
…oject the output as something different than the .from() table. For example, if you have


new joli.query().select('table2.*').from('table1').join('table2', 'id', 'table2.id')

This will be translated to:

select table2.* from table1 left outer join on table1.id = table2.id

You may want to prepare the SQL statement in this backwards order (table1 left outer joined to table2) for performance reasons.  However, you may need the results projected as 'table2' items.

To aid in this, I've added a simple .as() function.  You can use .as() to change a query to project as a different table than the .from() function:

new joli.query().select('table2.*').from('table1').join('table2', 'id', 'table2.id').as('table2')

These will produce results as 'table2' objects.
  • Loading branch information
nicjansma committed Nov 2, 2011
1 parent 5211064 commit cdbcf58
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions joli.js
Expand Up @@ -433,7 +433,8 @@ joli.query = function() {
select_columns: '*',
set: [],
values: [],
where: null
where: null,
as: null
};
};

Expand Down Expand Up @@ -479,6 +480,11 @@ joli.query.prototype = {
return this;
},

as: function(table) {
this.data.as = table;
return this;
},

getCount: function(rows) {
var result;

Expand Down Expand Up @@ -616,7 +622,9 @@ joli.query.prototype = {
var i;
var record;
var rowData;
var model = joli.models.get(this.data.from);

// use the model specified by as() first, then from()
var model = joli.models.get(this.data.as || this.data.from);

while (rows.isValidRow()) {
i = 0;
Expand Down

0 comments on commit cdbcf58

Please sign in to comment.