executeSql: function(query, parameters){ // should roughly follow executeSql in http://www.w3.org/TR/webdatabase/ var rawResults = adapter.executeSql(query, parameters); var results = {rows:LazyArray(rawResults)}; if(rawResults.insertId){ results.insertId = rawResults.insertId; } return results; },
store.fulltext = function(query, field, options){ var idResults = LazyArray(searcher.query(query, field, options.start || 0, options.end || 100000000, null)); return { query: "?id.in(" + idResults.join(",") + ")", totalCount: idResults.totalCount }; /*return LazyArray({ some: function(callback){ idResults.some(function(id){ try{ callback(store.get(id)); } catch(e){ print(e.message); } }); }, totalCount: idResults.totalCount });*/ };
query: function(query, options){ options = options || {}; var all = []; for(var i in this.index){ all.push(this.index[i]); } var result = executeQuery(query, options, all); // make a copy return LazyArray({ some: function(callback){ result.some(function(item){ var object = {}; for(var i in item){ if(item.hasOwnProperty(i)){ object[i] = item[i]; } } return callback(object); }); }, length: result.length, totalCount: result.totalCount }); }
return when(source, function(source){ if(!source){ throw new NotFoundError("not found"); } if(source instanceof Array){ // this handles query results, but probably should create a branch for real arrays var results = LazyArray({ some: function(callback){ source.some(function(item){ callback((item && typeof item == "object" && wrap(item, transaction, item, true)) || item); }); }, length: source.length, }); results.totalCount = source.totalCount; return results; } var instancePrototype = Object.create(facetPrototype); defineProperties(instancePrototype, { load: { value: function(){ if(facetSchema.allowed && !facetSchema.allowed(transaction.request, source)){ throw new AccessError("Access denied to " + source); } if(source.load && this != source){ var loadingSource = source.load(); } else{ var loadingSource = sourceClass.get(source.getId ? source.getId() : source.id); } return when(loadingSource, function(loadingSource){ source = loadingSource; copyFromSource(); loaded(); return wrapped; }); }, enumerable: false, writable: true } }); if(partial !== true){ loaded(); } function loaded(){ defineProperties(instancePrototype,{ get: { value: function(name){ if(links[name]){ var self = this; return wrap(facetClass.get(links[name].replace(/\{([^\}]*)\}/g, function(t, property){ var value = self[decodeURIComponent(property)]; if(value instanceof Array){ // the value is an array, it should produce a URI like /Table/(4,5,8) and store.get() should handle that as an array of values return '(' + value.join(',') + ')'; } return value; })), transaction); } if(facetPrototype.get){ if(facetPrototype.get === DELEGATE){ return sourceClass.prototype.get.call(source, name); } return facetPrototype.get.call(this, name) } var value = this[name]; if(value && value.$ref){ return wrap(facetClass.get(value.$ref), transaction); } return value; }, enumerable: false }, set: { value: function(name, value){ var propDef = properties && properties[name]; if(propDef){ mustBeValid(checkPropertyChange(value, propDef, name)); if(propDef.set){ value = propDef.set.call(this, name, value); } } sourceClass.get(source.getId ? source.getId() : source.id).set(name, value); this[name] = value; }, enumerable: false }, save: { value: function(directives){ directives = directives || {}; if(facetPrototype.save){ facetPrototype.save.call(this, source); } var validation = validate(this, facetSchema); var instance = this; for(var i in this){ if(this.hasOwnProperty(i)){ transfer(this[i]); } } for (var i in source){ if(source.hasOwnProperty(i) && !this.hasOwnProperty(i)){ transfer(undefined); } } mustBeValid(validation); try{ if(typeof facetSchema.put === "function"){ var id = facetSchema.put(source, directives); } else{ if(facetSchema.__noSuchMethod__){ var id = facetSchema.__noSuchMethod__("put", [source, directives]); } else{ throw new MethodNotAllowedError("put is not allowed"); } } var self = this; /*if(typeof id == "string" || typeof id == "number"){ source.id = id; }*/ return when(id, function(){ copyFromSource(); return self; }); } finally{ if(typeof id == "string" || typeof id == "number"){ transaction.generatedId = id; } } function transfer(value){ var propDef = properties && properties[i]; propDef = propDef || facetSchema.additionalProperties; var cancelled; if(propDef){ if(propDef.blocked){ addError("can't save a blocked property"); } if(propDef["transient"]){ cancelled = true; } if(source[i] !== value){ if(propDef.set){ try{ var newValue = propDef.set.call(instance, value, source, source[i]); if(newValue !== undefined){ value = newValue; } }catch(e){ addError(e.message); } } else if(propDef.get){ cancelled = true; } else if(propDef.readonly && source.hasOwnProperty(i)){ addError("property is read only"); } } } if(!cancelled){ if(value === undefined){ delete source[i]; } else{ source[i] = value; } } function addError(message){ validation.valid = false; validation.errors.push({property: i, message: message}); cancelled = true; } } }, enumerable: false }, load: { value: function(){ if(typeof source.load === "function"){ source.load(); } copyFromSource(); return wrapped; }, enumerable: false } }); } function copyFromSource(){ for(var i in source){ if(source.hasOwnProperty(i)){ var propDef = properties && properties[i]; if(!(propDef && propDef.blocked)){ wrapped[i] = source[i]; } } } for(var i in properties){ var propDef = properties[i]; if(propDef.get){ wrapped[i] = propDef.get.call(source, i); } } } for(var i in needSourceParameter){ // splice in the source argument for each method that needs it (function(param, protoFunc, i){ instancePrototype[i] = function(){ splice.call(arguments, param, 0, source); return protoFunc.apply(this, arguments); }; })(needSourceParameter[i], facetPrototype[i], i); } if(writableProto && partial === true){ source.__proto__ = instancePrototype; wrapped = source; } else{ if(wrapped){ wrapped.__proto__ = instancePrototype; } else{ wrapped = Object.create(instancePrototype); } if(partial !== NEW){ copyFromSource(); } } if(facetSchema.onWrap){ wrapped = facetSchema.onWrap(wrapped) || wrapped; } return wrapped; });