support constraints, order, and limit

--HG--
extra : rebase_source : abaefa842690d34653824641b86f846b6c738119
This commit is contained in:
Myk Melez 2009-06-02 16:00:52 -07:00
Родитель 7659a75ef2
Коммит 60b5d2c106
1 изменённых файлов: 44 добавлений и 2 удалений

Просмотреть файл

@ -72,11 +72,35 @@ Sync(Function);
* for each (let message in collection)
* dump("retrieved message " + message.id + "\n");
*/
function Collection2() {
function Collection2(args) {
// Extract values from arguments and assign them to member properties.
this.constraints = "constraints" in args ? args.constraints : [];
if ("order" in args) this.order = args.order;
if ("limit" in args) this.limit = args.limit;
// Execute the query so its results are available once the constructor returns.
this.execute.syncBind(this)();
}
Collection2.prototype = {
//**************************************************************************//
// Properties
/**
* An array of Constraint objects with expression and parameters properties
* that let you constrain the messages returned by the datastore, f.e.:
*
* [ { expression: "messages.timestamp > :timestamp",
* parameters: { name: "timestamp", value: 2454985 } } ]
*
* Constraint.parameters is optional.
*/
constraints: null,
order: null,
limit: null,
//**************************************************************************//
// Shortcuts
@ -143,10 +167,28 @@ Collection2.prototype = {
// all messages whether or not they have a content part.
"AND parts.partType = " + PART_TYPE_CONTENT;
this._log.info(query);
let conditions = [];
for each (let constraint in this.constraints)
conditions.push(constraint.expression);
if (conditions.length > 0)
query += " WHERE " + conditions.join(" AND ");
if (this.order)
query += " ORDER BY " + this.order;
if (this.limit)
query += " LIMIT " + this.limit;
let statement = SnowlDatastore.createStatement(query);
for each (let constraint in this.constraints)
if ("parameters" in constraint)
for (let [name, value] in Iterator(constraint.parameters))
statement.params[name] = value;
this._log.info(query);
return statement;
},