//>>built
define("dojox/store/db/SQL","dojo/_base/declare dojo/Deferred dojo/when dojo/store/util/QueryResults dojo/_base/lang dojo/promise/all".split(" "),function(A,B,r,C,u,t){function v(a){return a&&u.mixin(a,JSON.parse(a.__extra))}var D=/(.*)\*$/;return A([],{constructor:function(a){var d=a.dbConfig;this.database=openDatabase(a.dbName||"dojo-db","1.0","dojo-db",4194304);var m=this.indexPrefix=a.indexPrefix||"idx_",e=a.table||a.storeName;this.table=(a.table||a.storeName).replace(/[^\w]/g,"_");a=[];this.indices=
d.stores[e];this.repeatingIndices={};for(var c in this.indices)this.indices[c].multiEntry&&(this.repeatingIndices[c]=!0);if(!d.available){for(e in d.stores){var k=d.stores[e],h=e.replace(/[^\w]/g,"_"),g=k[this.idProperty],b=["__extra",this.idProperty+" "+(g&&g.autoIncrement?"INTEGER PRIMARY KEY AUTOINCREMENT":"PRIMARY KEY")];g=[this.idProperty];for(c in k)c!=this.idProperty&&b.push(c);a.push(this.executeSql("CREATE TABLE IF NOT EXISTS "+h+" ("+b.join(",")+")"));for(c in k)c!=this.idProperty&&(k[c].multiEntry?
(g.push(c),b=h+"_repeating_"+c,a.push(this.executeSql("CREATE TABLE IF NOT EXISTS "+b+" (id,value)")),a.push(this.executeSql("CREATE INDEX IF NOT EXISTS idx_"+b+"_id ON "+b+"(id)")),a.push(this.executeSql("CREATE INDEX IF NOT EXISTS idx_"+b+"_value ON "+b+"(value)"))):(a.push(this.executeSql("ALTER TABLE "+h+" ADD "+c).then(null,function(){})),!1!==k[c].indexed&&a.push(this.executeSql("CREATE INDEX IF NOT EXISTS "+m+h+"_"+c+" ON "+h+"("+c+")"))))}d.available=t(a)}this.available=d.available},idProperty:"id",
selectColumns:["*"],get:function(a){return r(this.executeSql("SELECT "+this.selectColumns.join(",")+" FROM "+this.table+" WHERE "+this.idProperty+"\x3d?",[a]),function(d){return 0<d.rows.length?v(d.rows.item(0)):void 0})},getIdentity:function(a){return a[this.idProperty]},remove:function(a){return this.executeSql("DELETE FROM "+this.table+" WHERE "+this.idProperty+"\x3d?",[a])},identifyGeneratedKey:!0,add:function(a,d){var m=[],e=[],c=[],k={},h=[],g=this,b;for(b in a)a.hasOwnProperty(b)&&(b in this.indices||
b==this.idProperty?this.repeatingIndices[b]?h.push(function(l){return t(a[b].map(function(f){return g.executeSql("INSERT INTO "+g.table+"_repeating_"+b+" (value, id) VALUES (?, ?)",[f,l])}))}):(c.push(b),e.push("?"),m.push(a[b])):k[b]=a[b]);c.push("__extra");e.push("?");m.push(JSON.stringify(k));var n=this.idProperty;this.identifyGeneratedKey&&(m.idColumn=n);e="INSERT INTO "+this.table+" ("+c.join(",")+") VALUES ("+e.join(",")+")";return r(this.executeSql(e,m),function(l){var f=l.insertId;a[n]=f;
return t(h.map(function(p){return p(f)})).then(function(){return f})})},put:function(a,d){d=d||{};var m=d.id||a[this.idProperty],e=d.overwrite;if(void 0===e){var c=this;return this.get(m).then(function(f){return(d.overwrite=!!f)?(d.overwrite=!0,c.put(a,d)):c.add(a,d)})}if(!e)return c.add(a,d);e="UPDATE "+this.table+" SET ";var k=[],h=[],g={},b;for(b in a)if(a.hasOwnProperty(b))if(b in this.indices||b==this.idProperty)if(this.repeatingIndices[b]){this.executeSql("DELETE FROM "+this.table+"_repeating_"+
b+" WHERE id\x3d?",[m]);for(var n=a[b],l=0;l<n.length;l++)this.executeSql("INSERT INTO "+this.table+"_repeating_"+b+" (value, id) VALUES (?, ?)",[n[l],m])}else h.push(b+"\x3d?"),k.push(a[b]);else g[b]=a[b];h.push("__extra\x3d?");k.push(JSON.stringify(g));e+=h.join(",")+" WHERE "+this.idProperty+"\x3d?";k.push(a[this.idProperty]);return r(this.executeSql(e,k),function(f){return m})},query:function(a,d){function m(n){var l=[],f;for(f in n){var p=n[f],x=function(q){var w=q&&q.match&&q.match(D);if(w)return g.push(w[1]+
"%")," LIKE ?";g.push(q);return"\x3d?"};if(p)if(p.contains){var E=k.table+"_repeating_"+f;l.push(p.contains.map(function(q){return k.idProperty+" IN (SELECT id FROM "+E+" WHERE value"+x(q)+")"}).join(" AND "));continue}else if("object"==typeof p&&("from"in p||"to"in p)){var y=p.excludeFrom?"\x3e":"\x3e\x3d",z=p.excludeTo?"\x3c":"\x3c\x3d";"from"in p?(g.push(p.from),"to"in p?(g.push(p.to),l.push("("+h+"."+f+y+"? AND "+h+"."+f+z+"?)")):l.push(h+"."+f+y+"?")):(g.push(p.to),l.push(h+"."+f+z+"?"));continue}l.push(h+
"."+f+x(p))}return l.join(" AND ")}d=d||{};var e="FROM "+this.table,c,k=this,h=this.table,g=[];a.forEach?(c=a.map(m).join(") OR ("))&&(c="("+c+")"):c=m(a);c&&(c=" WHERE "+c);d.sort&&(c+=" ORDER BY "+d.sort.map(function(n){return h+"."+n.attribute+" "+(n.descending?"desc":"asc")}));var b=c;d.count&&(b+=" LIMIT "+d.count);d.start&&(b+=" OFFSET "+d.start);b=u.delegate(this.executeSql("SELECT * "+e+b,g).then(function(n){for(var l=[],f=0;f<n.rows.length;f++)l.push(v(n.rows.item(f)));return l}));k=this;
b.total={then:function(n,l){return k.executeSql("SELECT COUNT(*) "+e+c,g).then(function(f){return f.rows.item(0)["COUNT(*)"]}).then(n,l)}};return new C(b)},executeSql:function(a,d){var m=new B,e,c;this.database.transaction(function(k){k.executeSql(a,d,function(h,g){m.resolve(e=g)},function(h,g){m.reject(c=g)})});if(e)return e;if(c)throw c;return m.promise}})});