root / trunk / web / dojo / dojox / sql / _base.js @ 12
History | View | Annotate | Download (6.51 KB)
1 |
/*
|
---|---|
2 |
Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
3 |
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
4 |
see: http://dojotoolkit.org/license for details
|
5 |
*/
|
6 |
|
7 |
|
8 |
if(!dojo._hasResource["dojox.sql._base"]){ |
9 |
dojo._hasResource["dojox.sql._base"]=true; |
10 |
dojo.provide("dojox.sql._base");
|
11 |
dojo.require("dojox.sql._crypto");
|
12 |
dojo.mixin(dojox.sql,{dbName:null,debug:(dojo.exists("dojox.sql.debug")?dojox.sql.debug:false),open:function(_1){ |
13 |
if(this._dbOpen&&(!_1||_1==this.dbName)){ |
14 |
return;
|
15 |
} |
16 |
if(!this.dbName){ |
17 |
this.dbName="dot_store_"+window.location.href.replace(/[^0-9A-Za-z_]/g,"_"); |
18 |
if(this.dbName.length>63){ |
19 |
this.dbName=this.dbName.substring(0,63); |
20 |
} |
21 |
} |
22 |
if(!_1){
|
23 |
_1=this.dbName;
|
24 |
} |
25 |
try{
|
26 |
this._initDb();
|
27 |
this.db.open(_1);
|
28 |
this._dbOpen=true; |
29 |
} |
30 |
catch(exp){
|
31 |
throw exp.message||exp;
|
32 |
} |
33 |
},close:function(_2){ |
34 |
if(dojo.isIE){
|
35 |
return;
|
36 |
} |
37 |
if(!this._dbOpen&&(!_2||_2==this.dbName)){ |
38 |
return;
|
39 |
} |
40 |
if(!_2){
|
41 |
_2=this.dbName;
|
42 |
} |
43 |
try{
|
44 |
this.db.close(_2);
|
45 |
this._dbOpen=false; |
46 |
} |
47 |
catch(exp){
|
48 |
throw exp.message||exp;
|
49 |
} |
50 |
},_exec:function(_3){ |
51 |
try{
|
52 |
this._initDb();
|
53 |
if(!this._dbOpen){ |
54 |
this.open();
|
55 |
this._autoClose=true; |
56 |
} |
57 |
var _4=null; |
58 |
var _5=null; |
59 |
var _6=null; |
60 |
var _7=dojo._toArray(_3);
|
61 |
_4=_7.splice(0,1)[0]; |
62 |
if(this._needsEncrypt(_4)||this._needsDecrypt(_4)){ |
63 |
_5=_7.splice(_7.length-1,1)[0]; |
64 |
_6=_7.splice(_7.length-1,1)[0]; |
65 |
} |
66 |
if(this.debug){ |
67 |
this._printDebugSQL(_4,_7);
|
68 |
} |
69 |
var _8;
|
70 |
if(this._needsEncrypt(_4)){ |
71 |
_8=new dojox.sql._SQLCrypto("encrypt",_4,_6,_7,_5); |
72 |
return null; |
73 |
}else{
|
74 |
if(this._needsDecrypt(_4)){ |
75 |
_8=new dojox.sql._SQLCrypto("decrypt",_4,_6,_7,_5); |
76 |
return null; |
77 |
} |
78 |
} |
79 |
var rs=this.db.execute(_4,_7); |
80 |
rs=this._normalizeResults(rs);
|
81 |
if(this._autoClose){ |
82 |
this.close();
|
83 |
} |
84 |
return rs;
|
85 |
} |
86 |
catch(exp){
|
87 |
exp=exp.message||exp; |
88 |
if(this._autoClose){ |
89 |
try{
|
90 |
this.close();
|
91 |
} |
92 |
catch(e){
|
93 |
} |
94 |
} |
95 |
throw exp;
|
96 |
} |
97 |
return null; |
98 |
},_initDb:function(){ |
99 |
if(!this.db){ |
100 |
try{
|
101 |
this.db=google.gears.factory.create("beta.database","1.0"); |
102 |
} |
103 |
catch(exp){
|
104 |
dojo.setObject("google.gears.denied",true); |
105 |
if(dojox.off){
|
106 |
dojox.off.onFrameworkEvent("coreOperationFailed");
|
107 |
} |
108 |
throw "Google Gears must be allowed to run"; |
109 |
} |
110 |
} |
111 |
},_printDebugSQL:function(_9,_a){ |
112 |
var _b="dojox.sql(\""+_9+"\""; |
113 |
for(var i=0;i<_a.length;i++){ |
114 |
if(typeof _a[i]=="string"){ |
115 |
_b+=", \""+_a[i]+"\""; |
116 |
}else{
|
117 |
_b+=", "+_a[i];
|
118 |
} |
119 |
} |
120 |
_b+=")";
|
121 |
},_normalizeResults:function(rs){ |
122 |
var _c=[];
|
123 |
if(!rs){
|
124 |
return [];
|
125 |
} |
126 |
while(rs.isValidRow()){
|
127 |
var _d={};
|
128 |
for(var i=0;i<rs.fieldCount();i++){ |
129 |
var _e=rs.fieldName(i);
|
130 |
var _f=rs.field(i);
|
131 |
_d[_e]=_f; |
132 |
} |
133 |
_c.push(_d); |
134 |
rs.next(); |
135 |
} |
136 |
rs.close(); |
137 |
return _c;
|
138 |
},_needsEncrypt:function(sql){ |
139 |
return /encrypt\([^\)]*\)/i.test(sql); |
140 |
},_needsDecrypt:function(sql){ |
141 |
return /decrypt\([^\)]*\)/i.test(sql); |
142 |
}}); |
143 |
dojo.declare("dojox.sql._SQLCrypto",null,{constructor:function(_10,sql,_11,_12,_13){ |
144 |
if(_10=="encrypt"){ |
145 |
this._execEncryptSQL(sql,_11,_12,_13);
|
146 |
}else{
|
147 |
this._execDecryptSQL(sql,_11,_12,_13);
|
148 |
} |
149 |
},_execEncryptSQL:function(sql,_14,_15,_16){ |
150 |
var _17=this._stripCryptoSQL(sql); |
151 |
var _18=this._flagEncryptedArgs(sql,_15); |
152 |
var _19=this; |
153 |
this._encrypt(_17,_14,_15,_18,function(_1a){ |
154 |
var _1b=false; |
155 |
var _1c=[];
|
156 |
var exp=null; |
157 |
try{
|
158 |
_1c=dojox.sql.db.execute(_17,_1a); |
159 |
} |
160 |
catch(execError){
|
161 |
_1b=true;
|
162 |
exp=execError.message||execError; |
163 |
} |
164 |
if(exp!=null){ |
165 |
if(dojox.sql._autoClose){
|
166 |
try{
|
167 |
dojox.sql.close(); |
168 |
} |
169 |
catch(e){
|
170 |
} |
171 |
} |
172 |
_16(null,true,exp.toString()); |
173 |
return;
|
174 |
} |
175 |
_1c=dojox.sql._normalizeResults(_1c); |
176 |
if(dojox.sql._autoClose){
|
177 |
dojox.sql.close(); |
178 |
} |
179 |
if(dojox.sql._needsDecrypt(sql)){
|
180 |
var _1d=_19._determineDecryptedColumns(sql);
|
181 |
_19._decrypt(_1c,_1d,_14,function(_1e){
|
182 |
_16(_1e,false,null); |
183 |
}); |
184 |
}else{
|
185 |
_16(_1c,false,null); |
186 |
} |
187 |
}); |
188 |
},_execDecryptSQL:function(sql,_1f,_20,_21){ |
189 |
var _22=this._stripCryptoSQL(sql); |
190 |
var _23=this._determineDecryptedColumns(sql); |
191 |
var _24=false; |
192 |
var _25=[];
|
193 |
var exp=null; |
194 |
try{
|
195 |
_25=dojox.sql.db.execute(_22,_20); |
196 |
} |
197 |
catch(execError){
|
198 |
_24=true;
|
199 |
exp=execError.message||execError; |
200 |
} |
201 |
if(exp!=null){ |
202 |
if(dojox.sql._autoClose){
|
203 |
try{
|
204 |
dojox.sql.close(); |
205 |
} |
206 |
catch(e){
|
207 |
} |
208 |
} |
209 |
_21(_25,true,exp.toString());
|
210 |
return;
|
211 |
} |
212 |
_25=dojox.sql._normalizeResults(_25); |
213 |
if(dojox.sql._autoClose){
|
214 |
dojox.sql.close(); |
215 |
} |
216 |
this._decrypt(_25,_23,_1f,function(_26){ |
217 |
_21(_26,false,null); |
218 |
}); |
219 |
},_encrypt:function(sql,_27,_28,_29,_2a){ |
220 |
this._totalCrypto=0; |
221 |
this._finishedCrypto=0; |
222 |
this._finishedSpawningCrypto=false; |
223 |
this._finalArgs=_28;
|
224 |
for(var i=0;i<_28.length;i++){ |
225 |
if(_29[i]){
|
226 |
var _2b=_28[i];
|
227 |
var _2c=i;
|
228 |
this._totalCrypto++;
|
229 |
dojox.sql._crypto.encrypt(_2b,_27,dojo.hitch(this,function(_2d){ |
230 |
this._finalArgs[_2c]=_2d;
|
231 |
this._finishedCrypto++;
|
232 |
if(this._finishedCrypto>=this._totalCrypto&&this._finishedSpawningCrypto){ |
233 |
_2a(this._finalArgs);
|
234 |
} |
235 |
})); |
236 |
} |
237 |
} |
238 |
this._finishedSpawningCrypto=true; |
239 |
},_decrypt:function(_2e,_2f,_30,_31){ |
240 |
this._totalCrypto=0; |
241 |
this._finishedCrypto=0; |
242 |
this._finishedSpawningCrypto=false; |
243 |
this._finalResultSet=_2e;
|
244 |
for(var i=0;i<_2e.length;i++){ |
245 |
var row=_2e[i];
|
246 |
for(var _32 in row){ |
247 |
if(_2f=="*"||_2f[_32]){ |
248 |
this._totalCrypto++;
|
249 |
var _33=row[_32];
|
250 |
this._decryptSingleColumn(_32,_33,_30,i,function(_34){ |
251 |
_31(_34); |
252 |
}); |
253 |
} |
254 |
} |
255 |
} |
256 |
this._finishedSpawningCrypto=true; |
257 |
},_stripCryptoSQL:function(sql){ |
258 |
sql=sql.replace(/DECRYPT\(\*\)/ig,"*"); |
259 |
var _35=sql.match(/ENCRYPT\([^\)]*\)/ig); |
260 |
if(_35!=null){ |
261 |
for(var i=0;i<_35.length;i++){ |
262 |
var _36=_35[i];
|
263 |
var _37=_36.match(/ENCRYPT\(([^\)]*)\)/i)[1]; |
264 |
sql=sql.replace(_36,_37); |
265 |
} |
266 |
} |
267 |
_35=sql.match(/DECRYPT\([^\)]*\)/ig);
|
268 |
if(_35!=null){ |
269 |
for(i=0;i<_35.length;i++){ |
270 |
var _38=_35[i];
|
271 |
var _39=_38.match(/DECRYPT\(([^\)]*)\)/i)[1]; |
272 |
sql=sql.replace(_38,_39); |
273 |
} |
274 |
} |
275 |
return sql;
|
276 |
},_flagEncryptedArgs:function(sql,_3a){ |
277 |
var _3b=new RegExp(/([\"][^\"]*\?[^\"]*[\"])|([\'][^\']*\?[^\']*[\'])|(\?)/ig); |
278 |
var _3c;
|
279 |
var _3d=0; |
280 |
var _3e=[];
|
281 |
while((_3c=_3b.exec(sql))!=null){ |
282 |
var _3f=RegExp.lastMatch+""; |
283 |
if(/^[\"\']/.test(_3f)){ |
284 |
continue;
|
285 |
} |
286 |
var _40=false; |
287 |
if(/ENCRYPT\([^\)]*$/i.test(RegExp.leftContext)){ |
288 |
_40=true;
|
289 |
} |
290 |
_3e[_3d]=_40; |
291 |
_3d++; |
292 |
} |
293 |
return _3e;
|
294 |
},_determineDecryptedColumns:function(sql){ |
295 |
var _41={};
|
296 |
if(/DECRYPT\(\*\)/i.test(sql)){ |
297 |
_41="*";
|
298 |
}else{
|
299 |
var _42=/DECRYPT\((?:\s*\w*\s*\,?)*\)/ig; |
300 |
var _43=_42.exec(sql);
|
301 |
while(_43){
|
302 |
var _44=new String(RegExp.lastMatch); |
303 |
var _45=_44.replace(/DECRYPT\(/i,""); |
304 |
_45=_45.replace(/\)/,""); |
305 |
_45=_45.split(/\s*,\s*/);
|
306 |
dojo.forEach(_45,function(_46){
|
307 |
if(/\s*\w* AS (\w*)/i.test(_46)){ |
308 |
_46=_46.match(/\s*\w* AS (\w*)/i)[1]; |
309 |
} |
310 |
_41[_46]=true;
|
311 |
}); |
312 |
_43=_42.exec(sql); |
313 |
} |
314 |
} |
315 |
return _41;
|
316 |
},_decryptSingleColumn:function(_47,_48,_49,_4a,_4b){ |
317 |
dojox.sql._crypto.decrypt(_48,_49,dojo.hitch(this,function(_4c){ |
318 |
this._finalResultSet[_4a][_47]=_4c;
|
319 |
this._finishedCrypto++;
|
320 |
if(this._finishedCrypto>=this._totalCrypto&&this._finishedSpawningCrypto){ |
321 |
_4b(this._finalResultSet);
|
322 |
} |
323 |
})); |
324 |
}}); |
325 |
(function(){
|
326 |
var _4d=dojox.sql;
|
327 |
dojox.sql=new Function("return dojox.sql._exec(arguments);"); |
328 |
dojo.mixin(dojox.sql,_4d); |
329 |
})(); |
330 |
} |