Skip to content

Commit

Permalink
some rewrite + documentation of the new functionnalities picked from …
Browse files Browse the repository at this point in the history
…aroldan's branch
  • Loading branch information
xavierlacot committed Jul 26, 2011
1 parent 1bf1bb0 commit e6934a2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
15 changes: 14 additions & 1 deletion README.markdown
Expand Up @@ -200,7 +200,20 @@ This is particularly powerful when you want to add restrictions to the query in

var humans = q.execute();

The Query API supports lots of things. Just have a check at the `joli.query` class!
The Query API supports lots of things. Just have a check at the `joli.query` class, or look at the samples provided in [the joli.query test suite](https://github.com/xavierlacot/joli.js-demo/blob/master/Resources/test/query.js)!

For instance, the following query syntaxes are supported by the API:

var q = new joli.query()
.select()
.from('human')
.whereIn('last_name', [ 'Doe', 'Smith' ]);


var q = new joli.query()
.select()
.from('view_count')
.where('nb_views between ? and ?', [1000, 2000]);

In some cases however, you will find this way of querying your models just too long, and you will prefer an other alternative syntax (Criteria-style querying API):

Expand Down
39 changes: 21 additions & 18 deletions joli.js
Expand Up @@ -408,13 +408,13 @@ joli.Models.prototype = {
var query = 'DROP TABLE IF EXISTS ' + modelName;
joli.connection.execute(query);
});

// optional migration callback
if(migrationCallback && typeof(migrationCallback) == "function") {
migrationCallback({
table:this.migration.table,
newVersion:version
});
if (migrationCallback && 'function' == joli.getType(migrationCallback)) {
migrationCallback({
table: this.migration.table,
newVersion: version
});
}

// insert migration
Expand Down Expand Up @@ -708,18 +708,21 @@ joli.query.prototype = {
this.data.where = '';
}

// handle replacing multiple values, as is needed in a BETWEEN query or similar
if(typeof(value) == "object") {
var i = 0;
while(expression.indexOf("?") != -1) { // replace question marks one at a time from the array
expression = expression.replace(/\?/i, value[i]);
i++;
}
this.data.where += expression;
} else {
this.data.where += expression.replace(/\?/gi, '"' + value + '"');
}

// handle replacing multiple values
if ('array' == joli.getType(value)) {
var i = 0;

// replace question marks one at a time from the array
while (expression.indexOf('?') != -1 && value[i]) {
expression = expression.replace(/\?/i, '"' + value[i] + '"');
i++;
}

this.data.where += expression;
} else {
this.data.where += expression.replace(/\?/gi, '"' + value + '"');
}

return this;
},

Expand Down

0 comments on commit e6934a2

Please sign in to comment.