Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

A simple database dump function. Restore will come soon.

  • Loading branch information...
commit ef3606a29fa20f20c57babd8f14da6fc309779de 1 parent c76f098
Zef Hemel authored
Showing with 86 additions and 34 deletions.
  1. +20 −0 README.md
  2. +33 −1 persistence.js
  3. +33 −33 persistence.min.js
View
20 README.md
@@ -165,6 +165,26 @@ explicitly call `persistence.flush()`. The exception to this rule is
when using the `list(...)` method on a `QueryCollection`, which also
flushes first.
+Dumping (and restoring) a database
+--------------------------------
+
+`persistence.dump` can be used to create an object containing a full
+dump of a database. Naturally, it is adviced to only do this with
+smaller databases. Example:
+
+ persistence.dump(tx, [Task, Category], function(dump) {
+ console.log(dump);
+ });
+
+When `null` is provided as a first argument a new transaction will be started for the operation. If `null` is provided as second argument, `dump` defaults to dumping _all_ defined entities.
+
+The dump format is:
+
+ {"entity-name": [list of instances],
+ ...}
+
+There is no restore API yet, but this will be implemented soon.
+
Query collections
-----------------
View
34 persistence.js
@@ -117,7 +117,7 @@ var persistence = window.persistence || {};
rowDef = '';
for (var prop in meta.fields) {
if (meta.fields.hasOwnProperty(prop)) {
- rowDef += prop + " " + meta.fields[prop] + ", ";
+ rowDef += "`" + prop + "` " + meta.fields[prop] + ", ";
}
}
for (var rel in meta.hasOne) {
@@ -589,6 +589,38 @@ var persistence = window.persistence || {};
return Entity;
}
+
+ /**
+ * Dumps the entire database into an object (that can be serialized to JSON for instance)
+ * @param entities a list of entity constructor functions to serialize, defaults to all
+ * @param callback (object) the callback function called with the results.
+ */
+ persistence.dump = function(tx, entities, callback) {
+ if(!entities) { // Default: all entity types
+ entities = [];
+ for(e in entityClassCache) {
+ if(entityClassCache.hasOwnProperty(e)) {
+ entities.push(entityClassCache[e]);
+ }
+ }
+ }
+
+ var finishedCount = 0;
+ var result = {};
+ for(var i = 0; i < entities.length; i++) {
+ (function() {
+ var Entity = entities[i];
+ Entity.all().list(tx, function(all) {
+ result[Entity.meta.name] = all.map(function(e) { return e._data; });
+ finishedCount++;
+ if(finishedCount === entities.length) {
+ callback(result);
+ }
+ });
+ }());
+ }
+ };
+
/**
* Internal function to persist an object to the database
* this function is invoked by persistence.flush()
View
66 persistence.min.js
@@ -24,36 +24,36 @@
OTHER DEALINGS IN THE SOFTWARE.
*/
var persistence=window.persistence||{};
-(function(){function k(a){function c(d){var e=this;this.id=M();this._new=true;this._type=a;this._dirtyProperties={};this._data={};this._data_obj={};this.subscribers={};for(var g in b.fields)(function(){if(b.fields.hasOwnProperty(g)){var f=g;e.__defineSetter__(f,function(j){e._data[f]=j;e._dirtyProperties[f]=true;e.triggerEvent("set",e,f,j)});e.__defineGetter__(f,function(){return e._data[f]});e._data[g]=N(b.fields[g])}})();for(var h in b.hasOne)b.hasOne.hasOwnProperty(h)&&function(){var f=h;e.__defineSetter__(f,
-function(j){if(j==null){e._data[f]=null;e._data_obj[f]=undefined}else if(j.id){e._data[f]=j.id;e._data_obj[f]=j;persistence.add(j)}else e._data[f]=j;e._dirtyProperties[f]=true;e.triggerEvent("set",e,f,j)});e.__defineGetter__(f,function(){if(e._data[f]===null||e._data_obj[f]!==undefined)return e._data_obj[f];else throw"Property '"+f+"' with id: "+e._data[f]+" not fetched, either prefetch it or fetch it manually.";})}();for(h in b.hasMany)b.hasMany.hasOwnProperty(h)&&function(){var f=h;if(b.hasMany[f].manyToMany){e.__defineSetter__(f,
-function(){throw"Not yet supported.";});e.__defineGetter__(f,function(){if(this._data[f])return e._data[f];else{var j=b.hasMany[f].type.meta,l=new v(j.name);l.initManyToMany(e,f);l._additionalJoinSqls.push("LEFT JOIN `"+b.hasMany[f].tableName+"` AS mtm ON mtm.`"+j.name+"_"+b.hasMany[f].inverseProperty+"` = `"+j.name+"`.`id` ");l._additionalWhereSqls.push("mtm.`"+b.name+"_"+f+"` = '"+e.id+"'");return e._data[f]=l}})}else{e.__defineSetter__(f,function(){throw"Not yet supported.";});e.__defineGetter__(f,
-function(){if(this._data[f])return e._data[f];else{var j=(new t(b.hasMany[f].type.meta.name)).filter(b.hasMany[f].inverseProperty,"=",e);return e._data[f]=j}})}}();for(var i in d)if(d.hasOwnProperty(i))e[i]=d[i]}if(G[a])return G[a];var b=u[a];c.prototype=new z;c.meta=b;c.prototype.equals=function(d){return this.id==d.id};c.prototype.fetch=function(d,e,g){var h=this;if(d)if(this._data[e])if(this._data_obj[e])g&&g(this._data_obj[e]);else b.hasOne[e].type.load(d,this._data[e],function(i){h._data_obj[e]=
-i;g&&g(i)});else g&&g(null);else persistence.transaction(function(i){h.fetch(i,e,g)})};c.all=function(){if(!this.allCollection)this.allCollection=new D(a);return this.allCollection};c.load=function(d,e,g){if(d){e||g(null);d.executeSql("SELECT * FROM `"+a+"` WHERE id = ?",[e],function(h){h.length==0&&g(null);g(persistence.rowToEntity(a,h[0]))})}else persistence.transaction(function(h){c.load(h,e,g)})};c.hasMany=function(d,e,g){var h=e.meta;if(h.hasMany[g]){var i=b.name+"_"+d+"_"+h.name,f=h.name+"_"+
-g+"_"+b.name;if(i>f)i=f;b.hasMany[d]={type:e,inverseProperty:g,manyToMany:true,tableName:i};h.hasMany[g]={type:c,inverseProperty:d,manyToMany:true,tableName:i};delete b.hasOne[d]}else{b.hasMany[d]={type:e,inverseProperty:g};h.hasOne[g]={type:c,inverseProperty:d}}};c.hasOne=function(d,e){b.hasOne[d]={type:e}};return G[a]=c}function o(a,c,b){var d=u[a._type],e=[],g=[],h=[],i=[];for(var f in a._dirtyProperties)if(a._dirtyProperties.hasOwnProperty(f)){e.push("`"+f+"`");g.push(persistence.entityValToDbVal(a[f],
-d.fields[f]));h.push("?");i.push("`"+f+"` = ?")}var j=[];for(f in d.hasMany)if(d.hasMany.hasOwnProperty(f))j=j.concat(a[f].persistQueries());H(c,j,function(){if(e.length===0)b&&b();else{a._dirtyProperties={};if(a._new){e.push("id");g.push(a.id);h.push("?");var l="INSERT INTO `"+a._type+"` ("+e.join(", ")+") VALUES ("+h.join(", ")+")";a._new=false}else l="UPDATE `"+a._type+"` SET "+i.join(",")+" WHERE id = '"+a.id+"'";c.executeSql(l,g,b)}})}function q(a,c,b){var d=[["DELETE FROM `"+a._type+"` WHERE id = '"+
-a.id+"'",null]],e=persistence.getMeta(a._type);for(var g in e.hasMany)e.hasMany.hasOwnProperty(g)&&e.hasMany[g].manyToMany&&d.push(["DELETE FROM `"+e.hasMany[g].tableName+"` WHERE `"+e.name+"_"+g+"` = '"+a.id+"'",null]);H(c,d,b)}function H(a,c,b){function d(){var h=c.pop();a.executeSql(h[0],h[1],function(){if(c.length>0)d();else b&&b.apply(this,e)},function(i,f){console.log(f)})}for(var e=[],g=3;g<arguments.length;g++)e.push(arguments[g]);if(c.length>0)d();else b&&b.apply(this,e)}function M(){for(var a=
-[],c=0;c<32;c++)a[c]="0123456789ABCDEF".substr(Math.floor(Math.random()*16),1);a[12]="4";a[16]="0123456789ABCDEF".substr(a[16]&3|8,1);return a.join("")}function N(a){switch(a){case "TEXT":return"";case "INT":return 0;case "BOOL":return false;default:return null}}function z(){this.subscribers={}}function O(){this.sql=function(){return"1=1"};this.match=function(){return true};this.makeFit=function(){};this.makeNotFit=function(){}}function P(a,c){this.sql=function(b,d){return"("+a.sql(b,d)+" AND "+c.sql(b,
-d)+")"};this.match=function(b){return a.match(b)&&c.match(b)};this.makeFit=function(b){a.makeFit(b);c.makeFit(b)};this.makeNotFit=function(b){a.makeNotFit(b);c.makeNotFit(b)}}function Q(a,c,b){this.sql=function(d,e){if(c==="="&&b===null)return"`"+d+a+"` IS NULL";else if(c==="!="&&b===null)return"`"+d+a+"` IS NOT NULL";else{if(b===true||b===false)b=b?1:0;e.push(persistence.entityValToDbVal(b));return"`"+d+a+"` "+c+" ?"}};this.match=function(d){switch(c){case "=":return d[a]===b;case "!=":return d[a]!==
-b;case "<":return d[a]<b;case "<=":return d[a]<=b;case ">":return d[a]>b;case ">=":return d[a]>=b}};this.makeFit=function(d){if(c==="=")d[a]=b;else throw"Sorry, can't perform makeFit for other filters than =";};this.makeNotFit=function(d){if(c==="=")d[a]=null;else throw"Sorry, can't perform makeNotFit for other filters than =";}}function m(){}function t(a){this.init(a,t)}function D(a){this.init(a,D)}function v(a){this.init(a,v);this._localAdded=[];this._localRemoved=[]}function w(a,c){this.init(a,
-w);this._data={};if(c)for(a=0;a<c.length;a++)this._data[c[a].id]=c[a]}var u={},s={},A={};persistence.trackedObjects=s;persistence.getMeta=function(a){return u[a]};persistence.connect=function(a,c,b){persistence._conn=persistence.db.connect(a,c,b);if(!persistence._conn)throw{type:"NoSupportedDatabaseFound",message:"No supported database found in this browser."};};persistence.transaction=function(a){if(persistence._conn)persistence._conn.transaction(a);else throw"No ongoing database connection, please connect first.";
-};persistence.define=function(a,c){if(u[a])return k(a);u[a]={name:a,fields:c,hasMany:{},hasOne:{}};return k(a)};var B={};persistence.schemaSync=function(a){var c=[],b,d,e,g;for(var h in u)if(u.hasOwnProperty(h)){b=u[h];d="";for(var i in b.fields)if(b.fields.hasOwnProperty(i))d+=i+" "+b.fields[i]+", ";for(var f in b.hasOne)if(b.hasOne.hasOwnProperty(f)){e=b.hasOne[f].type.meta;d+=f+" VARCHAR(255), ";c.push(["CREATE INDEX IF NOT EXISTS `"+b.name+"_"+f+"_"+e.name+"` ON `"+b.name+"` (`"+f+"`)",null])}for(f in b.hasMany)if(b.hasMany.hasOwnProperty(f)&&
-b.hasMany[f].manyToMany){g=b.hasMany[f].tableName;if(!B[g]){e=b.hasMany[f].type.meta;c.push(["CREATE INDEX IF NOT EXISTS `"+g+"_"+b.name+"_"+f+"` ON `"+g+"` (`"+b.name+"_"+f+"`)",null]);c.push(["CREATE INDEX IF NOT EXISTS `"+g+"_"+e.name+"_"+b.hasMany[f].inverseProperty+"` ON `"+g+"` (`"+e.name+"_"+b.hasMany[f].inverseProperty+"`)",null]);c.push(["CREATE TABLE IF NOT EXISTS `"+g+"` (`"+b.name+"_"+f+"` VARCHAR(32), `"+e.name+"_"+b.hasMany[f].inverseProperty+"` VARCHAR(32))",null]);B[g]=true}}d=d.substring(0,
-d.length-2);B[b.name]=true;c.push(["CREATE TABLE IF NOT EXISTS `"+b.name+"` ( id VARCHAR(32) PRIMARY KEY, "+d+")",null])}persistence.transaction(function(j){H(j,c,a,j)})};persistence.add=function(a){s[a.id]||(s[a.id]=a)};persistence.remove=function(a){A[a.id]||(A[a.id]=a)};persistence.flush=function(a,c){if(a){var b=[];for(var d in s)s.hasOwnProperty(d)&&b.push(s[d]);var e=[];for(d in A)if(A.hasOwnProperty(d)){e.push(A[d]);delete s[d]}A={};if(c){function g(){var i=e.pop();q(i,a,function(){if(e.length>
-0)g();else c&&c()})}function h(){var i=b.pop();o(i,a,function(){if(b.length>0)h();else if(e.length>0)g();else c&&c()})}if(b.length>0)h();else if(e.length>0)g();else c&&c()}else{for(d=0;d<b.length;d++)o(b[d],a);for(d=0;d<e.length;d++)q(e[d],a)}}else persistence.transaction(function(i){persistence.flush(i,c)})};persistence.clean=function(){s={}};persistence.reset=function(a){function c(){var d=b.pop();a.executeSql("DROP TABLE "+d,null,function(){b.length>0&&c()})}var b=[];for(p in B)B.hasOwnProperty(p)&&
-b.push(p);c();persistence.clean();B={}};persistence.rowToEntity=function(a,c,b){b=b||"";if(s[c[b+"id"]])return s[c[b+"id"]];var d=u[a];a=new (k(a));a.id=c[b+"id"];a._new=false;for(var e in c)if(c.hasOwnProperty(e))if(e.substring(0,b.length)===b){var g=e.substring(b.length);if(g!="id")a._data[g]=persistence.dbValToEntityVal(c[e],d.fields[g])}return a};persistence.dbValToEntityVal=function(a,c){switch(c){case "BOOL":return a==1;default:return a}};persistence.entityValToDbVal=function(a,c){return a===
-undefined||a===null?null:a.id?a.id:c==="BOOL"?a?1:0:a};var G={};z.prototype.addEventListener=function(a,c){if(typeof a=="object")for(var b=a,d=0;d<b.length;d++){a=b[d];this.subscribers[a]||(this.subscribers[a]=[]);this.subscribers[a].push(c)}else{this.subscribers[a]||(this.subscribers[a]=[]);this.subscribers[a].push(c)}};z.prototype.removeEventListener=function(a,c){for(var b=this.subscribers[a],d=0;d<b.length;d++)if(b[d]==c){this.subscribers[a].splice(d,1);return true}return false};z.prototype.triggerEvent=
-function(a){if(this.subscribers[a])for(var c in this.subscribers[a])this.subscribers[a].hasOwnProperty(c)&&this.subscribers[a][c].apply(null,arguments)};m.prototype=new z;m.prototype.persistQueries=function(){return[]};m.prototype.init=function(a,c){this._filter=new O;this._orderColumns=[];this._prefetchFields=[];this._additionalJoinSqls=[];this._additionalWhereSqls=[];this._entityName=a;this._constructor=c;this._limit=-1;this._skip=0;this.subscribers={}};m.prototype.clone=function(){var a=new this._constructor(this._entityName);
-a._filter=this._filter;a._prefetchFields=this._prefetchFields.slice(0);a._orderColumns=this._orderColumns.slice(0);a._limit=this._limit;a._skip=this._skip;a.subscribers=this.subscribers;return a};m.prototype.filter=function(a,c,b){var d=this.clone();d._filter=new P(this._filter,new Q(a,c,b));return d};m.prototype.order=function(a,c){c=c||true;var b=this.clone();b._orderColumns.push([a,c]);return b};m.prototype.limit=function(a){var c=this.clone();c._limit=a;return c};m.prototype.skip=function(a){var c=
-this.clone();c._skip=a;return c};m.prototype.prefetch=function(a){var c=this.clone();c._prefetchFields.push(a);return c};m.prototype.add=function(a){if(!a.id||!a._type)throw"Cannot add object of non-entity type onto collection.";persistence.add(a);this._filter.makeFit(a);this.triggerEvent("add",this,a)};m.prototype.remove=function(a){if(!a.id||!a._type)throw"Cannot remove object of non-entity type from collection.";this._filter.makeNotFit(a);this.triggerEvent("remove",this,a)};t.prototype=new m;t.prototype.each=
-function(a,c){this.list(a,function(b){for(var d=0;d<b.length;d++)c(b[d])})};t.prototype.list=function(a,c){function b(n,x,y){var C=["`"+x+"`.id AS "+y+"id"];for(var r in n.fields)n.fields.hasOwnProperty(r)&&C.push("`"+x+"`.`"+r+"` AS `"+y+r+"`");for(r in n.hasOne)n.hasOne.hasOwnProperty(r)&&C.push("`"+x+"`.`"+r+"` AS `"+y+r+"`");return C}var d=this;if(a){for(var e=this._entityName,g=persistence.getMeta(e),h=[],i=e+"_",f=b(g,g.name,i),j=this._additionalJoinSqls.join(" "),l=0;l<this._prefetchFields.length;l++){var E=
-this._prefetchFields[l],I=g.hasOne[E].type.meta,J=I.name+"_"+E+"_tbl";f=f.concat(b(I,J,E+"_"));j+="LEFT JOIN `"+I.name+"` AS `"+J+"` ON `"+J+"`.`id` = `"+i+E+"` "}l="WHERE "+[this._filter.sql(i,h)].concat(this._additionalWhereSqls).join(" AND ");var F="SELECT "+f.join(", ")+" FROM `"+e+"` "+j+" "+l;if(this._orderColumns.length>0)F+=" ORDER BY "+this._orderColumns.map(function(n){return"`"+i+n[0]+"` "+(n[1]?"ASC":"DESC")}).join(", ");if(this._limit>=0)F+=" LIMIT "+this._limit;if(this._skip>0)F+=" OFFSET "+
-this._skip;persistence.flush(a,function(){a.executeSql(F,h,function(n){for(var x=[],y=0;y<n.length;y++){for(var C=n[y],r=persistence.rowToEntity(e,C,i),K=0;K<d._prefetchFields.length;K++){var L=d._prefetchFields[K];r[L]=persistence.rowToEntity(g.hasOne[L].type.meta.name,C,L+"_")}x.push(r);persistence.add(r)}c(x);d.triggerEvent("list",d,x)})})}else persistence.transaction(function(n){d.list(n,c)})};D.prototype=new t;D.prototype.add=function(a){persistence.add(a);this.triggerEvent("add",this,a)};D.prototype.remove=
-function(a){persistence.remove(a);this.triggerEvent("remove",this,a)};v.prototype=new t;v.prototype.initManyToMany=function(a,c){this._obj=a;this._coll=c};v.prototype.add=function(a){if(!this._localAdded.contains(a)){persistence.add(a);this._localAdded.push(a)}};v.prototype.clone=function(){var a=t.prototype.clone.call(this);a._localAdded=this._localAdded;a._localRemoved=this._localRemoved;a._obj=this._obj;a._coll=this._coll;return a};v.prototype.remove=function(a){if(this._localAdded.contains(a))this._localAdded.remove(a);
-else this._localRemoved.contains(a)||this._localRemoved.push(a)};v.prototype.persistQueries=function(){for(var a=[],c=persistence.getMeta(this._obj._type),b=c.hasMany[this._coll].type.meta,d=0;d<this._localAdded.length;d++)a.push(["INSERT INTO "+c.hasMany[this._coll].tableName+" (`"+c.name+"_"+this._coll+"`, `"+b.name+"_"+c.hasMany[this._coll].inverseProperty+"`) VALUES (?, ?)",[this._obj.id,this._localAdded[d].id]]);this._localAdded=[];for(d=0;d<this._localRemoved.length;d++)a.push(["DELETE FROM "+
-c.hasMany[this._coll].tableName+" WHERE `"+c.name+"_"+this._coll+"` = ? AND `"+b.name+"_"+c.hasMany[this._coll].inverseProperty+"` = ?",[this._obj.id,this._localRemoved[d].id]]);this._localRemoved=[];return a};w.prototype=new m;w.prototype.clone=function(){var a=t.prototype.clone.call(this);a._data=this._data;return a};w.prototype.add=function(a){this._data[a.id]=a;this.triggerEvent("add",this,a)};w.prototype.remove=function(a){if(this._data[a.id]){delete this._data[a.id];this.triggerEvent("remove",
-this,a)}};w.prototype.list=function(a,c){if(!a||a.executeSql)a=c;c=[];var b=this;for(var d in this._data)this._data.hasOwnProperty(d)&&c.push(this._data[d]);d=[];for(var e=0;e<c.length;e++)this._filter.match(c[e])&&d.push(c[e]);d.sort(function(g,h){for(var i=0;i<b._orderColumns.length;i++){var f=b._orderColumns[i][0],j=b._orderColumns[i][1];console.log(b._orderColumns[i]);console.log(g);console.log(h);if(g[f]<h[f]){console.log(g.name+" < "+h.name);return j?1:-1}else if(g[f]>h[f]){console.log(g.name+
-" > "+h.name);return j?-1:1}}return 0});if(a)a(d);else return d};persistence.LocalQueryCollection=w;persistence.Observable=z;persistence.db=persistence.db||{};persistence.db.implementation="unsupported";persistence.db.conn=null;persistence.db.log=true;if(window.google&&google.gears)persistence.db.implementation="gears";else if(window.openDatabase)persistence.db.implementation="html5";persistence.db.html5={};persistence.db.html5.connect=function(a,c,b){var d={},e=openDatabase(a,"1.0",c,b);d.transaction=
-function(g){return e.transaction(function(h){return g(persistence.db.html5.transaction(h))})};return d};persistence.db.html5.transaction=function(a){var c={};c.executeSql=function(b,d,e,g){persistence.db.log&&console.log(b);a.executeSql(b,d,function(h,i){if(e){h=[];for(var f=0;f<i.rows.length;f++)h.push(i.rows.item(f));e(h)}},g)};return c};persistence.db.gears={};persistence.db.gears.connect=function(a){var c={},b=google.gears.factory.create("beta.database");b.open(a);c.transaction=function(d){d(persistence.db.gears.transaction(b))};
-return c};persistence.db.gears.transaction=function(a){var c={};c.executeSql=function(b,d,e){persistence.db.log&&console.log(b);b=a.execute(b,d);if(e){for(d=[];b.isValidRow();){for(var g={},h=0;h<b.fieldCount();h++)g[b.fieldName(h)]=b.field(h);d.push(g);b.next()}e(d)}};return c};persistence.db.connect=function(a,c,b){if(persistence.db.implementation=="html5")return persistence.db.html5.connect(a,c,b);else if(persistence.db.implementation=="gears")return persistence.db.gears.connect(a)}})();
-Number.prototype.equals=function(k){return this==k};Boolean.prototype.equals=function(k){return this==k};String.prototype.equals=function(k){return this==k};Array.prototype.equals=function(k){if(this.length!==k.length)return false;for(var o=0;o<this.length;o++)if(!this[o].equals(k[o]))return false;return true};Array.prototype.contains=function(k){for(var o=this.length,q=0;q<o;q++)if(this[q].equals(k))return true;return false};
-Array.prototype.remove=function(k){for(var o=this.length,q=0;q<o;q++)this[q].equals(k)&&this.splice(q,1)};
+(function(){function l(a){function c(d){var f=this;this.id=N();this._new=true;this._type=a;this._dirtyProperties={};this._data={};this._data_obj={};this.subscribers={};for(var h in b.fields)(function(){if(b.fields.hasOwnProperty(h)){var g=h;f.__defineSetter__(g,function(k){f._data[g]=k;f._dirtyProperties[g]=true;f.triggerEvent("set",f,g,k)});f.__defineGetter__(g,function(){return f._data[g]});f._data[h]=O(b.fields[h])}})();for(var i in b.hasOne)b.hasOne.hasOwnProperty(i)&&function(){var g=i;f.__defineSetter__(g,
+function(k){if(k==null){f._data[g]=null;f._data_obj[g]=undefined}else if(k.id){f._data[g]=k.id;f._data_obj[g]=k;persistence.add(k)}else f._data[g]=k;f._dirtyProperties[g]=true;f.triggerEvent("set",f,g,k)});f.__defineGetter__(g,function(){if(f._data[g]===null||f._data_obj[g]!==undefined)return f._data_obj[g];else throw"Property '"+g+"' with id: "+f._data[g]+" not fetched, either prefetch it or fetch it manually.";})}();for(i in b.hasMany)b.hasMany.hasOwnProperty(i)&&function(){var g=i;if(b.hasMany[g].manyToMany){f.__defineSetter__(g,
+function(){throw"Not yet supported.";});f.__defineGetter__(g,function(){if(this._data[g])return f._data[g];else{var k=b.hasMany[g].type.meta,m=new w(k.name);m.initManyToMany(f,g);m._additionalJoinSqls.push("LEFT JOIN `"+b.hasMany[g].tableName+"` AS mtm ON mtm.`"+k.name+"_"+b.hasMany[g].inverseProperty+"` = `"+k.name+"`.`id` ");m._additionalWhereSqls.push("mtm.`"+b.name+"_"+g+"` = '"+f.id+"'");return f._data[g]=m}})}else{f.__defineSetter__(g,function(){throw"Not yet supported.";});f.__defineGetter__(g,
+function(){if(this._data[g])return f._data[g];else{var k=(new u(b.hasMany[g].type.meta.name)).filter(b.hasMany[g].inverseProperty,"=",f);return f._data[g]=k}})}}();for(var j in d)if(d.hasOwnProperty(j))f[j]=d[j]}if(A[a])return A[a];var b=v[a];c.prototype=new B;c.meta=b;c.prototype.equals=function(d){return this.id==d.id};c.prototype.fetch=function(d,f,h){var i=this;if(d)if(this._data[f])if(this._data_obj[f])h&&h(this._data_obj[f]);else b.hasOne[f].type.load(d,this._data[f],function(j){i._data_obj[f]=
+j;h&&h(j)});else h&&h(null);else persistence.transaction(function(j){i.fetch(j,f,h)})};c.all=function(){if(!this.allCollection)this.allCollection=new F(a);return this.allCollection};c.load=function(d,f,h){if(d){f||h(null);d.executeSql("SELECT * FROM `"+a+"` WHERE id = ?",[f],function(i){i.length==0&&h(null);h(persistence.rowToEntity(a,i[0]))})}else persistence.transaction(function(i){c.load(i,f,h)})};c.hasMany=function(d,f,h){var i=f.meta;if(i.hasMany[h]){var j=b.name+"_"+d+"_"+i.name,g=i.name+"_"+
+h+"_"+b.name;if(j>g)j=g;b.hasMany[d]={type:f,inverseProperty:h,manyToMany:true,tableName:j};i.hasMany[h]={type:c,inverseProperty:d,manyToMany:true,tableName:j};delete b.hasOne[d]}else{b.hasMany[d]={type:f,inverseProperty:h};i.hasOne[h]={type:c,inverseProperty:d}}};c.hasOne=function(d,f){b.hasOne[d]={type:f}};return A[a]=c}function q(a,c,b){var d=v[a._type],f=[],h=[],i=[],j=[];for(var g in a._dirtyProperties)if(a._dirtyProperties.hasOwnProperty(g)){f.push("`"+g+"`");h.push(persistence.entityValToDbVal(a[g],
+d.fields[g]));i.push("?");j.push("`"+g+"` = ?")}var k=[];for(g in d.hasMany)if(d.hasMany.hasOwnProperty(g))k=k.concat(a[g].persistQueries());I(c,k,function(){if(f.length===0)b&&b();else{a._dirtyProperties={};if(a._new){f.push("id");h.push(a.id);i.push("?");var m="INSERT INTO `"+a._type+"` ("+f.join(", ")+") VALUES ("+i.join(", ")+")";a._new=false}else m="UPDATE `"+a._type+"` SET "+j.join(",")+" WHERE id = '"+a.id+"'";c.executeSql(m,h,b)}})}function r(a,c,b){var d=[["DELETE FROM `"+a._type+"` WHERE id = '"+
+a.id+"'",null]],f=persistence.getMeta(a._type);for(var h in f.hasMany)f.hasMany.hasOwnProperty(h)&&f.hasMany[h].manyToMany&&d.push(["DELETE FROM `"+f.hasMany[h].tableName+"` WHERE `"+f.name+"_"+h+"` = '"+a.id+"'",null]);I(c,d,b)}function I(a,c,b){function d(){var i=c.pop();a.executeSql(i[0],i[1],function(){if(c.length>0)d();else b&&b.apply(this,f)},function(j,g){console.log(g)})}for(var f=[],h=3;h<arguments.length;h++)f.push(arguments[h]);if(c.length>0)d();else b&&b.apply(this,f)}function N(){for(var a=
+[],c=0;c<32;c++)a[c]="0123456789ABCDEF".substr(Math.floor(Math.random()*16),1);a[12]="4";a[16]="0123456789ABCDEF".substr(a[16]&3|8,1);return a.join("")}function O(a){switch(a){case "TEXT":return"";case "INT":return 0;case "BOOL":return false;default:return null}}function B(){this.subscribers={}}function P(){this.sql=function(){return"1=1"};this.match=function(){return true};this.makeFit=function(){};this.makeNotFit=function(){}}function Q(a,c){this.sql=function(b,d){return"("+a.sql(b,d)+" AND "+c.sql(b,
+d)+")"};this.match=function(b){return a.match(b)&&c.match(b)};this.makeFit=function(b){a.makeFit(b);c.makeFit(b)};this.makeNotFit=function(b){a.makeNotFit(b);c.makeNotFit(b)}}function R(a,c,b){this.sql=function(d,f){if(c==="="&&b===null)return"`"+d+a+"` IS NULL";else if(c==="!="&&b===null)return"`"+d+a+"` IS NOT NULL";else{if(b===true||b===false)b=b?1:0;f.push(persistence.entityValToDbVal(b));return"`"+d+a+"` "+c+" ?"}};this.match=function(d){switch(c){case "=":return d[a]===b;case "!=":return d[a]!==
+b;case "<":return d[a]<b;case "<=":return d[a]<=b;case ">":return d[a]>b;case ">=":return d[a]>=b}};this.makeFit=function(d){if(c==="=")d[a]=b;else throw"Sorry, can't perform makeFit for other filters than =";};this.makeNotFit=function(d){if(c==="=")d[a]=null;else throw"Sorry, can't perform makeNotFit for other filters than =";}}function n(){}function u(a){this.init(a,u)}function F(a){this.init(a,F)}function w(a){this.init(a,w);this._localAdded=[];this._localRemoved=[]}function x(a,c){this.init(a,
+x);this._data={};if(c)for(a=0;a<c.length;a++)this._data[c[a].id]=c[a]}var v={},t={},C={};persistence.trackedObjects=t;persistence.getMeta=function(a){return v[a]};persistence.connect=function(a,c,b){persistence._conn=persistence.db.connect(a,c,b);if(!persistence._conn)throw{type:"NoSupportedDatabaseFound",message:"No supported database found in this browser."};};persistence.transaction=function(a){if(persistence._conn)persistence._conn.transaction(a);else throw"No ongoing database connection, please connect first.";
+};persistence.define=function(a,c){if(v[a])return l(a);v[a]={name:a,fields:c,hasMany:{},hasOne:{}};return l(a)};var D={};persistence.schemaSync=function(a){var c=[],b,d,f,h;for(var i in v)if(v.hasOwnProperty(i)){b=v[i];d="";for(var j in b.fields)if(b.fields.hasOwnProperty(j))d+="`"+j+"` "+b.fields[j]+", ";for(var g in b.hasOne)if(b.hasOne.hasOwnProperty(g)){f=b.hasOne[g].type.meta;d+=g+" VARCHAR(255), ";c.push(["CREATE INDEX IF NOT EXISTS `"+b.name+"_"+g+"_"+f.name+"` ON `"+b.name+"` (`"+g+"`)",null])}for(g in b.hasMany)if(b.hasMany.hasOwnProperty(g)&&
+b.hasMany[g].manyToMany){h=b.hasMany[g].tableName;if(!D[h]){f=b.hasMany[g].type.meta;c.push(["CREATE INDEX IF NOT EXISTS `"+h+"_"+b.name+"_"+g+"` ON `"+h+"` (`"+b.name+"_"+g+"`)",null]);c.push(["CREATE INDEX IF NOT EXISTS `"+h+"_"+f.name+"_"+b.hasMany[g].inverseProperty+"` ON `"+h+"` (`"+f.name+"_"+b.hasMany[g].inverseProperty+"`)",null]);c.push(["CREATE TABLE IF NOT EXISTS `"+h+"` (`"+b.name+"_"+g+"` VARCHAR(32), `"+f.name+"_"+b.hasMany[g].inverseProperty+"` VARCHAR(32))",null]);D[h]=true}}d=d.substring(0,
+d.length-2);D[b.name]=true;c.push(["CREATE TABLE IF NOT EXISTS `"+b.name+"` ( id VARCHAR(32) PRIMARY KEY, "+d+")",null])}persistence.transaction(function(k){I(k,c,a,k)})};persistence.add=function(a){t[a.id]||(t[a.id]=a)};persistence.remove=function(a){C[a.id]||(C[a.id]=a)};persistence.flush=function(a,c){if(a){var b=[];for(var d in t)t.hasOwnProperty(d)&&b.push(t[d]);var f=[];for(d in C)if(C.hasOwnProperty(d)){f.push(C[d]);delete t[d]}C={};if(c){function h(){var j=f.pop();r(j,a,function(){if(f.length>
+0)h();else c&&c()})}function i(){var j=b.pop();q(j,a,function(){if(b.length>0)i();else if(f.length>0)h();else c&&c()})}if(b.length>0)i();else if(f.length>0)h();else c&&c()}else{for(d=0;d<b.length;d++)q(b[d],a);for(d=0;d<f.length;d++)r(f[d],a)}}else persistence.transaction(function(j){persistence.flush(j,c)})};persistence.clean=function(){t={}};persistence.reset=function(a){function c(){var d=b.pop();a.executeSql("DROP TABLE "+d,null,function(){b.length>0&&c()})}var b=[];for(p in D)D.hasOwnProperty(p)&&
+b.push(p);c();persistence.clean();D={}};persistence.rowToEntity=function(a,c,b){b=b||"";if(t[c[b+"id"]])return t[c[b+"id"]];var d=v[a];a=new (l(a));a.id=c[b+"id"];a._new=false;for(var f in c)if(c.hasOwnProperty(f))if(f.substring(0,b.length)===b){var h=f.substring(b.length);if(h!="id")a._data[h]=persistence.dbValToEntityVal(c[f],d.fields[h])}return a};persistence.dbValToEntityVal=function(a,c){switch(c){case "BOOL":return a==1;default:return a}};persistence.entityValToDbVal=function(a,c){return a===
+undefined||a===null?null:a.id?a.id:c==="BOOL"?a?1:0:a};var A={};persistence.dump=function(a,c,b){if(!c){c=[];for(e in A)A.hasOwnProperty(e)&&c.push(A[e])}for(var d=0,f={},h=0;h<c.length;h++)(function(){var i=c[h];i.all().list(a,function(j){f[i.meta.name]=j.map(function(g){return g._data});d++;d===c.length&&b(f)})})()};B.prototype.addEventListener=function(a,c){if(typeof a=="object")for(var b=a,d=0;d<b.length;d++){a=b[d];this.subscribers[a]||(this.subscribers[a]=[]);this.subscribers[a].push(c)}else{this.subscribers[a]||
+(this.subscribers[a]=[]);this.subscribers[a].push(c)}};B.prototype.removeEventListener=function(a,c){for(var b=this.subscribers[a],d=0;d<b.length;d++)if(b[d]==c){this.subscribers[a].splice(d,1);return true}return false};B.prototype.triggerEvent=function(a){if(this.subscribers[a])for(var c in this.subscribers[a])this.subscribers[a].hasOwnProperty(c)&&this.subscribers[a][c].apply(null,arguments)};n.prototype=new B;n.prototype.persistQueries=function(){return[]};n.prototype.init=function(a,c){this._filter=
+new P;this._orderColumns=[];this._prefetchFields=[];this._additionalJoinSqls=[];this._additionalWhereSqls=[];this._entityName=a;this._constructor=c;this._limit=-1;this._skip=0;this.subscribers={}};n.prototype.clone=function(){var a=new this._constructor(this._entityName);a._filter=this._filter;a._prefetchFields=this._prefetchFields.slice(0);a._orderColumns=this._orderColumns.slice(0);a._limit=this._limit;a._skip=this._skip;a.subscribers=this.subscribers;return a};n.prototype.filter=function(a,c,b){var d=
+this.clone();d._filter=new Q(this._filter,new R(a,c,b));return d};n.prototype.order=function(a,c){c=c||true;var b=this.clone();b._orderColumns.push([a,c]);return b};n.prototype.limit=function(a){var c=this.clone();c._limit=a;return c};n.prototype.skip=function(a){var c=this.clone();c._skip=a;return c};n.prototype.prefetch=function(a){var c=this.clone();c._prefetchFields.push(a);return c};n.prototype.add=function(a){if(!a.id||!a._type)throw"Cannot add object of non-entity type onto collection.";persistence.add(a);
+this._filter.makeFit(a);this.triggerEvent("add",this,a)};n.prototype.remove=function(a){if(!a.id||!a._type)throw"Cannot remove object of non-entity type from collection.";this._filter.makeNotFit(a);this.triggerEvent("remove",this,a)};u.prototype=new n;u.prototype.each=function(a,c){this.list(a,function(b){for(var d=0;d<b.length;d++)c(b[d])})};u.prototype.list=function(a,c){function b(o,y,z){var E=["`"+y+"`.id AS "+z+"id"];for(var s in o.fields)o.fields.hasOwnProperty(s)&&E.push("`"+y+"`.`"+s+"` AS `"+
+z+s+"`");for(s in o.hasOne)o.hasOne.hasOwnProperty(s)&&E.push("`"+y+"`.`"+s+"` AS `"+z+s+"`");return E}var d=this;if(a){for(var f=this._entityName,h=persistence.getMeta(f),i=[],j=f+"_",g=b(h,h.name,j),k=this._additionalJoinSqls.join(" "),m=0;m<this._prefetchFields.length;m++){var G=this._prefetchFields[m],J=h.hasOne[G].type.meta,K=J.name+"_"+G+"_tbl";g=g.concat(b(J,K,G+"_"));k+="LEFT JOIN `"+J.name+"` AS `"+K+"` ON `"+K+"`.`id` = `"+j+G+"` "}m="WHERE "+[this._filter.sql(j,i)].concat(this._additionalWhereSqls).join(" AND ");
+var H="SELECT "+g.join(", ")+" FROM `"+f+"` "+k+" "+m;if(this._orderColumns.length>0)H+=" ORDER BY "+this._orderColumns.map(function(o){return"`"+j+o[0]+"` "+(o[1]?"ASC":"DESC")}).join(", ");if(this._limit>=0)H+=" LIMIT "+this._limit;if(this._skip>0)H+=" OFFSET "+this._skip;persistence.flush(a,function(){a.executeSql(H,i,function(o){for(var y=[],z=0;z<o.length;z++){for(var E=o[z],s=persistence.rowToEntity(f,E,j),L=0;L<d._prefetchFields.length;L++){var M=d._prefetchFields[L];s[M]=persistence.rowToEntity(h.hasOne[M].type.meta.name,
+E,M+"_")}y.push(s);persistence.add(s)}c(y);d.triggerEvent("list",d,y)})})}else persistence.transaction(function(o){d.list(o,c)})};F.prototype=new u;F.prototype.add=function(a){persistence.add(a);this.triggerEvent("add",this,a)};F.prototype.remove=function(a){persistence.remove(a);this.triggerEvent("remove",this,a)};w.prototype=new u;w.prototype.initManyToMany=function(a,c){this._obj=a;this._coll=c};w.prototype.add=function(a){if(!this._localAdded.contains(a)){persistence.add(a);this._localAdded.push(a)}};
+w.prototype.clone=function(){var a=u.prototype.clone.call(this);a._localAdded=this._localAdded;a._localRemoved=this._localRemoved;a._obj=this._obj;a._coll=this._coll;return a};w.prototype.remove=function(a){if(this._localAdded.contains(a))this._localAdded.remove(a);else this._localRemoved.contains(a)||this._localRemoved.push(a)};w.prototype.persistQueries=function(){for(var a=[],c=persistence.getMeta(this._obj._type),b=c.hasMany[this._coll].type.meta,d=0;d<this._localAdded.length;d++)a.push(["INSERT INTO "+
+c.hasMany[this._coll].tableName+" (`"+c.name+"_"+this._coll+"`, `"+b.name+"_"+c.hasMany[this._coll].inverseProperty+"`) VALUES (?, ?)",[this._obj.id,this._localAdded[d].id]]);this._localAdded=[];for(d=0;d<this._localRemoved.length;d++)a.push(["DELETE FROM "+c.hasMany[this._coll].tableName+" WHERE `"+c.name+"_"+this._coll+"` = ? AND `"+b.name+"_"+c.hasMany[this._coll].inverseProperty+"` = ?",[this._obj.id,this._localRemoved[d].id]]);this._localRemoved=[];return a};x.prototype=new n;x.prototype.clone=
+function(){var a=u.prototype.clone.call(this);a._data=this._data;return a};x.prototype.add=function(a){this._data[a.id]=a;this.triggerEvent("add",this,a)};x.prototype.remove=function(a){if(this._data[a.id]){delete this._data[a.id];this.triggerEvent("remove",this,a)}};x.prototype.list=function(a,c){if(!a||a.executeSql)a=c;c=[];var b=this;for(var d in this._data)this._data.hasOwnProperty(d)&&c.push(this._data[d]);d=[];for(var f=0;f<c.length;f++)this._filter.match(c[f])&&d.push(c[f]);d.sort(function(h,
+i){for(var j=0;j<b._orderColumns.length;j++){var g=b._orderColumns[j][0],k=b._orderColumns[j][1];console.log(b._orderColumns[j]);console.log(h);console.log(i);if(h[g]<i[g]){console.log(h.name+" < "+i.name);return k?1:-1}else if(h[g]>i[g]){console.log(h.name+" > "+i.name);return k?-1:1}}return 0});if(a)a(d);else return d};persistence.LocalQueryCollection=x;persistence.Observable=B;persistence.db=persistence.db||{};persistence.db.implementation="unsupported";persistence.db.conn=null;persistence.db.log=
+true;if(window.google&&google.gears)persistence.db.implementation="gears";else if(window.openDatabase)persistence.db.implementation="html5";persistence.db.html5={};persistence.db.html5.connect=function(a,c,b){var d={},f=openDatabase(a,"1.0",c,b);d.transaction=function(h){return f.transaction(function(i){return h(persistence.db.html5.transaction(i))})};return d};persistence.db.html5.transaction=function(a){var c={};c.executeSql=function(b,d,f,h){persistence.db.log&&console.log(b);a.executeSql(b,d,
+function(i,j){if(f){i=[];for(var g=0;g<j.rows.length;g++)i.push(j.rows.item(g));f(i)}},h)};return c};persistence.db.gears={};persistence.db.gears.connect=function(a){var c={},b=google.gears.factory.create("beta.database");b.open(a);c.transaction=function(d){d(persistence.db.gears.transaction(b))};return c};persistence.db.gears.transaction=function(a){var c={};c.executeSql=function(b,d,f){persistence.db.log&&console.log(b);b=a.execute(b,d);if(f){for(d=[];b.isValidRow();){for(var h={},i=0;i<b.fieldCount();i++)h[b.fieldName(i)]=
+b.field(i);d.push(h);b.next()}f(d)}};return c};persistence.db.connect=function(a,c,b){if(persistence.db.implementation=="html5")return persistence.db.html5.connect(a,c,b);else if(persistence.db.implementation=="gears")return persistence.db.gears.connect(a)}})();Number.prototype.equals=function(l){return this==l};Boolean.prototype.equals=function(l){return this==l};String.prototype.equals=function(l){return this==l};
+Array.prototype.equals=function(l){if(this.length!==l.length)return false;for(var q=0;q<this.length;q++)if(!this[q].equals(l[q]))return false;return true};Array.prototype.contains=function(l){for(var q=this.length,r=0;r<q;r++)if(this[r].equals(l))return true;return false};Array.prototype.remove=function(l){for(var q=this.length,r=0;r<q;r++)this[r].equals(l)&&this.splice(r,1)};
Please sign in to comment.
Something went wrong with that request. Please try again.