root / trunk / web / dojo / dojox / storage / GearsStorageProvider.js
History | View | Annotate | Download (6.34 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.storage.GearsStorageProvider"]){ |
| 9 |
dojo._hasResource["dojox.storage.GearsStorageProvider"]=true; |
| 10 |
dojo.provide("dojox.storage.GearsStorageProvider");
|
| 11 |
dojo.require("dojo.gears");
|
| 12 |
dojo.require("dojox.storage.Provider");
|
| 13 |
dojo.require("dojox.storage.manager");
|
| 14 |
dojo.require("dojox.sql");
|
| 15 |
if(dojo.gears.available){
|
| 16 |
(function(){
|
| 17 |
dojo.declare("dojox.storage.GearsStorageProvider",dojox.storage.Provider,{constructor:function(){ |
| 18 |
},TABLE_NAME:"__DOJO_STORAGE",initialized:false,_available:null,_storageReady:false,initialize:function(){ |
| 19 |
if(dojo.config["disableGearsStorage"]==true){ |
| 20 |
return;
|
| 21 |
} |
| 22 |
this.TABLE_NAME="__DOJO_STORAGE"; |
| 23 |
this.initialized=true; |
| 24 |
dojox.storage.manager.loaded(); |
| 25 |
},isAvailable:function(){ |
| 26 |
return this._available=dojo.gears.available; |
| 27 |
},put:function(_1,_2,_3,_4){ |
| 28 |
this._initStorage();
|
| 29 |
if(!this.isValidKey(_1)){ |
| 30 |
throw new Error("Invalid key given: "+_1); |
| 31 |
} |
| 32 |
_4=_4||this.DEFAULT_NAMESPACE;
|
| 33 |
if(!this.isValidKey(_4)){ |
| 34 |
throw new Error("Invalid namespace given: "+_1); |
| 35 |
} |
| 36 |
if(dojo.isString(_2)){
|
| 37 |
_2="string:"+_2;
|
| 38 |
}else{
|
| 39 |
_2=dojo.toJson(_2); |
| 40 |
} |
| 41 |
try{
|
| 42 |
dojox.sql("DELETE FROM "+this.TABLE_NAME+" WHERE namespace = ? AND key = ?",_4,_1); |
| 43 |
dojox.sql("INSERT INTO "+this.TABLE_NAME+" VALUES (?, ?, ?)",_4,_1,_2); |
| 44 |
} |
| 45 |
catch(e){
|
| 46 |
_3(this.FAILED,_1,e.toString(),_4);
|
| 47 |
return;
|
| 48 |
} |
| 49 |
if(_3){
|
| 50 |
_3(dojox.storage.SUCCESS,_1,null,_4);
|
| 51 |
} |
| 52 |
},get:function(_5,_6){ |
| 53 |
this._initStorage();
|
| 54 |
if(!this.isValidKey(_5)){ |
| 55 |
throw new Error("Invalid key given: "+_5); |
| 56 |
} |
| 57 |
_6=_6||this.DEFAULT_NAMESPACE;
|
| 58 |
if(!this.isValidKey(_6)){ |
| 59 |
throw new Error("Invalid namespace given: "+_5); |
| 60 |
} |
| 61 |
var _7=dojox.sql("SELECT * FROM "+this.TABLE_NAME+" WHERE namespace = ? AND "+" key = ?",_6,_5); |
| 62 |
if(!_7.length){
|
| 63 |
return null; |
| 64 |
}else{
|
| 65 |
_7=_7[0].value;
|
| 66 |
} |
| 67 |
if(dojo.isString(_7)&&(/^string:/.test(_7))){ |
| 68 |
_7=_7.substring("string:".length);
|
| 69 |
}else{
|
| 70 |
_7=dojo.fromJson(_7); |
| 71 |
} |
| 72 |
return _7;
|
| 73 |
},getNamespaces:function(){ |
| 74 |
this._initStorage();
|
| 75 |
var _8=[dojox.storage.DEFAULT_NAMESPACE];
|
| 76 |
var rs=dojox.sql("SELECT namespace FROM "+this.TABLE_NAME+" DESC GROUP BY namespace"); |
| 77 |
for(var i=0;i<rs.length;i++){ |
| 78 |
if(rs[i].namespace!=dojox.storage.DEFAULT_NAMESPACE){
|
| 79 |
_8.push(rs[i].namespace); |
| 80 |
} |
| 81 |
} |
| 82 |
return _8;
|
| 83 |
},getKeys:function(_9){ |
| 84 |
this._initStorage();
|
| 85 |
_9=_9||this.DEFAULT_NAMESPACE;
|
| 86 |
if(!this.isValidKey(_9)){ |
| 87 |
throw new Error("Invalid namespace given: "+_9); |
| 88 |
} |
| 89 |
var rs=dojox.sql("SELECT key FROM "+this.TABLE_NAME+" WHERE namespace = ?",_9); |
| 90 |
var _a=[];
|
| 91 |
for(var i=0;i<rs.length;i++){ |
| 92 |
_a.push(rs[i].key); |
| 93 |
} |
| 94 |
return _a;
|
| 95 |
},clear:function(_b){ |
| 96 |
this._initStorage();
|
| 97 |
_b=_b||this.DEFAULT_NAMESPACE;
|
| 98 |
if(!this.isValidKey(_b)){ |
| 99 |
throw new Error("Invalid namespace given: "+_b); |
| 100 |
} |
| 101 |
dojox.sql("DELETE FROM "+this.TABLE_NAME+" WHERE namespace = ?",_b); |
| 102 |
},remove:function(_c,_d){ |
| 103 |
this._initStorage();
|
| 104 |
if(!this.isValidKey(_c)){ |
| 105 |
throw new Error("Invalid key given: "+_c); |
| 106 |
} |
| 107 |
_d=_d||this.DEFAULT_NAMESPACE;
|
| 108 |
if(!this.isValidKey(_d)){ |
| 109 |
throw new Error("Invalid namespace given: "+_c); |
| 110 |
} |
| 111 |
dojox.sql("DELETE FROM "+this.TABLE_NAME+" WHERE namespace = ? AND"+" key = ?",_d,_c); |
| 112 |
},putMultiple:function(_e,_f,_10,_11){ |
| 113 |
this._initStorage();
|
| 114 |
if(!this.isValidKeyArray(_e)||!_f instanceof Array||_e.length!=_f.length){ |
| 115 |
throw new Error("Invalid arguments: keys = ["+_e+"], values = ["+_f+"]"); |
| 116 |
} |
| 117 |
if(_11==null||typeof _11=="undefined"){ |
| 118 |
_11=dojox.storage.DEFAULT_NAMESPACE; |
| 119 |
} |
| 120 |
if(!this.isValidKey(_11)){ |
| 121 |
throw new Error("Invalid namespace given: "+_11); |
| 122 |
} |
| 123 |
this._statusHandler=_10;
|
| 124 |
try{
|
| 125 |
dojox.sql.open(); |
| 126 |
dojox.sql.db.execute("BEGIN TRANSACTION");
|
| 127 |
var _12="REPLACE INTO "+this.TABLE_NAME+" VALUES (?, ?, ?)"; |
| 128 |
for(var i=0;i<_e.length;i++){ |
| 129 |
var _13=_f[i];
|
| 130 |
if(dojo.isString(_13)){
|
| 131 |
_13="string:"+_13;
|
| 132 |
}else{
|
| 133 |
_13=dojo.toJson(_13); |
| 134 |
} |
| 135 |
dojox.sql.db.execute(_12,[_11,_e[i],_13]); |
| 136 |
} |
| 137 |
dojox.sql.db.execute("COMMIT TRANSACTION");
|
| 138 |
dojox.sql.close(); |
| 139 |
} |
| 140 |
catch(e){
|
| 141 |
if(_10){
|
| 142 |
_10(this.FAILED,_e,e.toString(),_11);
|
| 143 |
} |
| 144 |
return;
|
| 145 |
} |
| 146 |
if(_10){
|
| 147 |
_10(dojox.storage.SUCCESS,_e,null,_11);
|
| 148 |
} |
| 149 |
},getMultiple:function(_14,_15){ |
| 150 |
this._initStorage();
|
| 151 |
if(!this.isValidKeyArray(_14)){ |
| 152 |
throw new ("Invalid key array given: "+_14); |
| 153 |
} |
| 154 |
if(_15==null||typeof _15=="undefined"){ |
| 155 |
_15=dojox.storage.DEFAULT_NAMESPACE; |
| 156 |
} |
| 157 |
if(!this.isValidKey(_15)){ |
| 158 |
throw new Error("Invalid namespace given: "+_15); |
| 159 |
} |
| 160 |
var _16="SELECT * FROM "+this.TABLE_NAME+" WHERE namespace = ? AND "+" key = ?"; |
| 161 |
var _17=[];
|
| 162 |
for(var i=0;i<_14.length;i++){ |
| 163 |
var _18=dojox.sql(_16,_15,_14[i]);
|
| 164 |
if(!_18.length){
|
| 165 |
_17[i]=null;
|
| 166 |
}else{
|
| 167 |
_18=_18[0].value;
|
| 168 |
if(dojo.isString(_18)&&(/^string:/.test(_18))){ |
| 169 |
_17[i]=_18.substring("string:".length);
|
| 170 |
}else{
|
| 171 |
_17[i]=dojo.fromJson(_18); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
return _17;
|
| 176 |
},removeMultiple:function(_19,_1a){ |
| 177 |
this._initStorage();
|
| 178 |
if(!this.isValidKeyArray(_19)){ |
| 179 |
throw new Error("Invalid arguments: keys = ["+_19+"]"); |
| 180 |
} |
| 181 |
if(_1a==null||typeof _1a=="undefined"){ |
| 182 |
_1a=dojox.storage.DEFAULT_NAMESPACE; |
| 183 |
} |
| 184 |
if(!this.isValidKey(_1a)){ |
| 185 |
throw new Error("Invalid namespace given: "+_1a); |
| 186 |
} |
| 187 |
dojox.sql.open(); |
| 188 |
dojox.sql.db.execute("BEGIN TRANSACTION");
|
| 189 |
var _1b="DELETE FROM "+this.TABLE_NAME+" WHERE namespace = ? AND key = ?"; |
| 190 |
for(var i=0;i<_19.length;i++){ |
| 191 |
dojox.sql.db.execute(_1b,[_1a,_19[i]]); |
| 192 |
} |
| 193 |
dojox.sql.db.execute("COMMIT TRANSACTION");
|
| 194 |
dojox.sql.close(); |
| 195 |
},isPermanent:function(){ |
| 196 |
return true; |
| 197 |
},getMaximumSize:function(){ |
| 198 |
return this.SIZE_NO_LIMIT; |
| 199 |
},hasSettingsUI:function(){ |
| 200 |
return false; |
| 201 |
},showSettingsUI:function(){ |
| 202 |
throw new Error(this.declaredClass+" does not support a storage settings user-interface"); |
| 203 |
},hideSettingsUI:function(){ |
| 204 |
throw new Error(this.declaredClass+" does not support a storage settings user-interface"); |
| 205 |
},_initStorage:function(){ |
| 206 |
if(this._storageReady){ |
| 207 |
return;
|
| 208 |
} |
| 209 |
if(!google.gears.factory.hasPermission){
|
| 210 |
var _1c=null; |
| 211 |
var _1d=null; |
| 212 |
var msg="This site would like to use Google Gears to enable "+"enhanced functionality."; |
| 213 |
var _1e=google.gears.factory.getPermission(_1c,_1d,msg);
|
| 214 |
if(!_1e){
|
| 215 |
throw new Error("You must give permission to use Gears in order to "+"store data"); |
| 216 |
} |
| 217 |
} |
| 218 |
try{
|
| 219 |
dojox.sql("CREATE TABLE IF NOT EXISTS "+this.TABLE_NAME+"( "+" namespace TEXT, "+" key TEXT, "+" value TEXT "+")"); |
| 220 |
dojox.sql("CREATE UNIQUE INDEX IF NOT EXISTS namespace_key_index"+" ON "+this.TABLE_NAME+" (namespace, key)"); |
| 221 |
} |
| 222 |
catch(e){
|
| 223 |
throw new Error("Unable to create storage tables for Gears in "+"Dojo Storage"); |
| 224 |
} |
| 225 |
this._storageReady=true; |
| 226 |
}}); |
| 227 |
dojox.storage.manager.register("dojox.storage.GearsStorageProvider",new dojox.storage.GearsStorageProvider()); |
| 228 |
})(); |
| 229 |
} |
| 230 |
} |