//>>built
define("dojox/sql/_base",["dijit","dojo","dojox","dojo/require!dojox/sql/_crypto"],function(q,l,f){l.provide("dojox.sql._base");l.require("dojox.sql._crypto");l.mixin(f.sql,{dbName:null,debug:l.exists("dojox.sql.debug")?f.sql.debug:!1,open:function(a){if(!this._dbOpen||a&&a!=this.dbName){this.dbName||(this.dbName="dot_store_"+window.location.href.replace(/[^0-9A-Za-z_]/g,"_"),63<this.dbName.length&&(this.dbName=this.dbName.substring(0,63)));a||(a=this.dbName);try{this._initDb(),this.db.open(a),this._dbOpen=
!0}catch(b){throw b.message||b;}}},close:function(a){if(!l.isIE&&(this._dbOpen||a&&a!=this.dbName)){a||(a=this.dbName);try{this.db.close(a),this._dbOpen=!1}catch(b){throw b.message||b;}}},_exec:function(a){try{this._initDb();this._dbOpen||(this.open(),this._autoClose=!0);var b=null,e=null,c=null,d=l._toArray(a);b=d.splice(0,1)[0];if(this._needsEncrypt(b)||this._needsDecrypt(b))e=d.splice(d.length-1,1)[0],c=d.splice(d.length-1,1)[0];this.debug&&this._printDebugSQL(b,d);if(this._needsEncrypt(b))return new f.sql._SQLCrypto("encrypt",
b,c,d,e),null;if(this._needsDecrypt(b))return new f.sql._SQLCrypto("decrypt",b,c,d,e),null;var g=this.db.execute(b,d);g=this._normalizeResults(g);this._autoClose&&this.close();return g}catch(h){h=h.message||h;if(this._autoClose)try{this.close()}catch(k){}throw h;}},_initDb:function(){if(!this.db)try{this.db=google.gears.factory.create("beta.database","1.0")}catch(a){l.setObject("google.gears.denied",!0);if(f.off)f.off.onFrameworkEvent("coreOperationFailed");throw"Google Gears must be allowed to run";
}},_printDebugSQL:function(a,b){for(var e=0;e<b.length;e++);},_normalizeResults:function(a){var b=[];if(!a)return[];for(;a.isValidRow();){for(var e={},c=0;c<a.fieldCount();c++){var d=a.fieldName(c),g=a.field(c);e[d]=g}b.push(e);a.next()}a.close();return b},_needsEncrypt:function(a){return/encrypt\([^\)]*\)/i.test(a)},_needsDecrypt:function(a){return/decrypt\([^\)]*\)/i.test(a)}});l.declare("dojox.sql._SQLCrypto",null,{constructor:function(a,b,e,c,d){"encrypt"==a?this._execEncryptSQL(b,e,c,d):this._execDecryptSQL(b,
e,c,d)},_execEncryptSQL:function(a,b,e,c){var d=this._stripCryptoSQL(a),g=this._flagEncryptedArgs(a,e),h=this;this._encrypt(d,b,e,g,function(k){var m=[],p=null;try{m=f.sql.db.execute(d,k)}catch(n){p=n.message||n}if(null!=p){if(f.sql._autoClose)try{f.sql.close()}catch(n){}c(null,!0,p.toString())}else m=f.sql._normalizeResults(m),f.sql._autoClose&&f.sql.close(),f.sql._needsDecrypt(a)?(k=h._determineDecryptedColumns(a),h._decrypt(m,k,b,function(n){c(n,!1,null)})):c(m,!1,null)})},_execDecryptSQL:function(a,
b,e,c){var d=this._stripCryptoSQL(a);a=this._determineDecryptedColumns(a);var g=[],h=null;try{g=f.sql.db.execute(d,e)}catch(k){h=k.message||k}if(null!=h){if(f.sql._autoClose)try{f.sql.close()}catch(k){}c(g,!0,h.toString())}else g=f.sql._normalizeResults(g),f.sql._autoClose&&f.sql.close(),this._decrypt(g,a,b,function(k){c(k,!1,null)})},_encrypt:function(a,b,e,c,d){this._finishedCrypto=this._totalCrypto=0;this._finishedSpawningCrypto=!1;this._finalArgs=e;for(a=0;a<e.length;a++)if(c[a]){var g=e[a],h=
a;this._totalCrypto++;f.sql._crypto.encrypt(g,b,l.hitch(this,function(k){this._finalArgs[h]=k;this._finishedCrypto++;this._finishedCrypto>=this._totalCrypto&&this._finishedSpawningCrypto&&d(this._finalArgs)}))}this._finishedSpawningCrypto=!0},_decrypt:function(a,b,e,c){this._finishedCrypto=this._totalCrypto=0;this._finishedSpawningCrypto=!1;this._finalResultSet=a;for(var d=0;d<a.length;d++){var g=a[d],h;for(h in g)if("*"==b||b[h])this._totalCrypto++,this._decryptSingleColumn(h,g[h],e,d,function(k){c(k)})}this._finishedSpawningCrypto=
!0},_stripCryptoSQL:function(a){a=a.replace(/DECRYPT\(\*\)/ig,"*");var b=a.match(/ENCRYPT\([^\)]*\)/ig);if(null!=b)for(var e=0;e<b.length;e++){var c=b[e],d=c.match(/ENCRYPT\(([^\)]*)\)/i)[1];a=a.replace(c,d)}b=a.match(/DECRYPT\([^\)]*\)/ig);if(null!=b)for(e=0;e<b.length;e++)c=b[e],d=c.match(/DECRYPT\(([^\)]*)\)/i)[1],a=a.replace(c,d);return a},_flagEncryptedArgs:function(a,b){for(var e=new RegExp(/(["][^"]*\?[^"]*["])|(['][^']*\?[^']*['])|(\?)/ig),c=0,d=[];null!=e.exec(a);)if(!/^["']/.test(RegExp.lastMatch+
"")){var g=!1;/ENCRYPT\([^\)]*$/i.test(RegExp.leftContext)&&(g=!0);d[c]=g;c++}return d},_determineDecryptedColumns:function(a){var b={};if(/DECRYPT\(\*\)/i.test(a))b="*";else for(var e=/DECRYPT\((?:\s*\w*\s*,?)*\)/ig,c=e.exec(a);c;)c=(new String(RegExp.lastMatch)).replace(/DECRYPT\(/i,""),c=c.replace(/\)/,""),c=c.split(/\s*,\s*/),l.forEach(c,function(d){/\s*\w* AS (\w*)/i.test(d)&&(d=d.match(/\s*\w* AS (\w*)/i)[1]);b[d]=!0}),c=e.exec(a);return b},_decryptSingleColumn:function(a,b,e,c,d){f.sql._crypto.decrypt(b,
e,l.hitch(this,function(g){this._finalResultSet[c][a]=g;this._finishedCrypto++;this._finishedCrypto>=this._totalCrypto&&this._finishedSpawningCrypto&&d(this._finalResultSet)}))}});(function(){var a=f.sql;f.sql=new Function("return dojox.sql._exec(arguments);");l.mixin(f.sql,a)})()});