Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
if(!dojo._hasResource["dojox.storage.GearsStorageProvider"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2
dojo._hasResource["dojox.storage.GearsStorageProvider"] = true;
3
dojo.provide("dojox.storage.GearsStorageProvider");
4
dojo.require("dojox.storage.Provider");
5
dojo.require("dojox.storage.manager");
6
dojo.require("dojox.sql");
7
 
8
if(dojo.isGears){
9
 
10
	(function(){
11
		// make sure we don't define the gears provider if we're not gears
12
		// enabled
13
 
14
		dojo.declare("dojox.storage.GearsStorageProvider", dojox.storage.Provider, {
15
			// summary:
16
			//		Storage provider that uses the features of Google Gears
17
			//		to store data (it is saved into the local SQL database
18
			//		provided by Gears, using dojox.sql)
19
			// description:
20
			//
21
			//
22
			//		You can disable this storage provider with the following djConfig
23
			//		variable:
24
			//		var djConfig = { disableGearsStorage: true };
25
			//
26
			//		Authors of this storage provider-
27
			//			Brad Neuberg, bkn3@columbia.edu
28
			constructor: function(){
29
			},
30
			// instance methods and properties
31
			TABLE_NAME: "__DOJO_STORAGE",
32
			initialized: false,
33
 
34
			_available: null,
35
 
36
			initialize: function(){
37
				//console.debug("dojox.storage.GearsStorageProvider.initialize");
38
				if(djConfig["disableGearsStorage"] == true){
39
					return;
40
				}
41
 
42
				// partition our storage data so that multiple apps
43
				// on the same host won't collide
44
				this.TABLE_NAME = "__DOJO_STORAGE";
45
 
46
				// create the table that holds our data
47
				try{
48
					dojox.sql("CREATE TABLE IF NOT EXISTS " + this.TABLE_NAME + "( "
49
								+ " namespace TEXT, "
50
								+ " key TEXT, "
51
								+ " value TEXT "
52
								+ ")"
53
							);
54
					dojox.sql("CREATE UNIQUE INDEX IF NOT EXISTS namespace_key_index"
55
								+ " ON " + this.TABLE_NAME
56
								+ " (namespace, key)");
57
				}catch(e){
58
					console.debug("dojox.storage.GearsStorageProvider.initialize:", e);
59
 
60
					this.initialized = false; // we were unable to initialize
61
					dojox.storage.manager.loaded();
62
					return;
63
				}
64
 
65
				// indicate that this storage provider is now loaded
66
				this.initialized = true;
67
				dojox.storage.manager.loaded();
68
			},
69
 
70
			isAvailable: function(){
71
				// is Google Gears available and defined?
72
				return this._available = dojo.isGears;
73
			},
74
 
75
			put: function(key, value, resultsHandler, namespace){
76
				if(this.isValidKey(key) == false){
77
					throw new Error("Invalid key given: " + key);
78
				}
79
				namespace = namespace||this.DEFAULT_NAMESPACE;
80
 
81
				// serialize the value;
82
				// handle strings differently so they have better performance
83
				if(dojo.isString(value)){
84
					value = "string:" + value;
85
				}else{
86
					value = dojo.toJson(value);
87
				}
88
 
89
				// try to store the value
90
				try{
91
					dojox.sql("DELETE FROM " + this.TABLE_NAME
92
								+ " WHERE namespace = ? AND key = ?",
93
								namespace, key);
94
					dojox.sql("INSERT INTO " + this.TABLE_NAME
95
								+ " VALUES (?, ?, ?)",
96
								namespace, key, value);
97
				}catch(e){
98
					// indicate we failed
99
					console.debug("dojox.storage.GearsStorageProvider.put:", e);
100
					resultsHandler(this.FAILED, key, e.toString());
101
					return;
102
				}
103
 
104
				if(resultsHandler){
105
					resultsHandler(dojox.storage.SUCCESS, key, null);
106
				}
107
			},
108
 
109
			get: function(key, namespace){
110
				if(this.isValidKey(key) == false){
111
					throw new Error("Invalid key given: " + key);
112
				}
113
				namespace = namespace||this.DEFAULT_NAMESPACE;
114
 
115
				// try to find this key in the database
116
				var results = dojox.sql("SELECT * FROM " + this.TABLE_NAME
117
											+ " WHERE namespace = ? AND "
118
											+ " key = ?",
119
											namespace, key);
120
				if(!results.length){
121
					return null;
122
				}else{
123
					results = results[0].value;
124
				}
125
 
126
				// destringify the content back into a
127
				// real JavaScript object;
128
				// handle strings differently so they have better performance
129
				if(dojo.isString(results) && (/^string:/.test(results))){
130
					results = results.substring("string:".length);
131
				}else{
132
					results = dojo.fromJson(results);
133
				}
134
 
135
				return results;
136
			},
137
 
138
			getNamespaces: function(){
139
				var results = [ dojox.storage.DEFAULT_NAMESPACE ];
140
 
141
				var rs = dojox.sql("SELECT namespace FROM " + this.TABLE_NAME
142
									+ " DESC GROUP BY namespace");
143
				for(var i = 0; i < rs.length; i++){
144
					if(rs[i].namespace != dojox.storage.DEFAULT_NAMESPACE){
145
						results.push(rs[i].namespace);
146
					}
147
				}
148
 
149
				return results;
150
			},
151
 
152
			getKeys: function(namespace){
153
				namespace = namespace||this.DEFAULT_NAMESPACE;
154
				if(this.isValidKey(namespace) == false){
155
					throw new Error("Invalid namespace given: " + namespace);
156
				}
157
 
158
				var rs = dojox.sql("SELECT key FROM " + this.TABLE_NAME
159
									+ " WHERE namespace = ?",
160
									namespace);
161
 
162
				var results = [];
163
				for(var i = 0; i < rs.length; i++){
164
					results.push(rs[i].key);
165
				}
166
 
167
				return results;
168
			},
169
 
170
			clear: function(namespace){
171
				if(this.isValidKey(namespace) == false){
172
					throw new Error("Invalid namespace given: " + namespace);
173
				}
174
				namespace = namespace||this.DEFAULT_NAMESPACE;
175
 
176
				dojox.sql("DELETE FROM " + this.TABLE_NAME
177
							+ " WHERE namespace = ?",
178
							namespace);
179
			},
180
 
181
			remove: function(key, namespace){
182
				namespace = namespace||this.DEFAULT_NAMESPACE;
183
 
184
				dojox.sql("DELETE FROM " + this.TABLE_NAME
185
							+ " WHERE namespace = ? AND"
186
							+ " key = ?",
187
							namespace,
188
							key);
189
			},
190
 
191
			putMultiple: function(keys, values, resultsHandler, namespace) {
192
 				if(this.isValidKeyArray(keys) === false
193
						|| ! values instanceof Array
194
						|| keys.length != values.length){
195
					throw new Error("Invalid arguments: keys = ["
196
									+ keys + "], values = [" + values + "]");
197
				}
198
 
199
				if(namespace == null || typeof namespace == "undefined"){
200
					namespace = dojox.storage.DEFAULT_NAMESPACE;
201
				}
202
 
203
				if(this.isValidKey(namespace) == false){
204
					throw new Error("Invalid namespace given: " + namespace);
205
				}
206
 
207
				this._statusHandler = resultsHandler;
208
 
209
				// try to store the value
210
				try{
211
					dojox.sql.open();
212
					dojox.sql.db.execute("BEGIN TRANSACTION");
213
					var _stmt = "REPLACE INTO " + this.TABLE_NAME + " VALUES (?, ?, ?)";
214
					for(var i=0;i<keys.length;i++) {
215
						// serialize the value;
216
						// handle strings differently so they have better performance
217
						var value = values[i];
218
						if(dojo.isString(value)){
219
							value = "string:" + value;
220
						}else{
221
							value = dojo.toJson(value);
222
						}
223
 
224
						dojox.sql.db.execute( _stmt,
225
							[namespace, keys[i], value]);
226
					}
227
					dojox.sql.db.execute("COMMIT TRANSACTION");
228
					dojox.sql.close();
229
				}catch(e){
230
					// indicate we failed
231
					console.debug("dojox.storage.GearsStorageProvider.putMultiple:", e);
232
					if(resultsHandler){
233
						resultsHandler(this.FAILED, keys, e.toString());
234
					}
235
					return;
236
				}
237
 
238
				if(resultsHandler){
239
					resultsHandler(dojox.storage.SUCCESS, key, null);
240
				}
241
			},
242
 
243
			getMultiple: function(keys, namespace){
244
				//	TODO: Maybe use SELECT IN instead
245
 
246
				if(this.isValidKeyArray(keys) === false){
247
					throw new ("Invalid key array given: " + keys);
248
				}
249
 
250
				if(namespace == null || typeof namespace == "undefined"){
251
					namespace = dojox.storage.DEFAULT_NAMESPACE;
252
				}
253
 
254
				if(this.isValidKey(namespace) == false){
255
					throw new Error("Invalid namespace given: " + namespace);
256
				}
257
 
258
				var _stmt = "SELECT * FROM " + this.TABLE_NAME	+
259
					" WHERE namespace = ? AND "	+ " key = ?";
260
 
261
				var results = [];
262
				for(var i=0;i<keys.length;i++){
263
					var result = dojox.sql( _stmt, namespace, keys[i]);
264
 
265
					if( ! result.length){
266
						results[i] = null;
267
					}else{
268
						result = result[0].value;
269
 
270
						// destringify the content back into a
271
						// real JavaScript object;
272
						// handle strings differently so they have better performance
273
						if(dojo.isString(result) && (/^string:/.test(result))){
274
							results[i] = result.substring("string:".length);
275
						}else{
276
							results[i] = dojo.fromJson(result);
277
						}
278
					}
279
				}
280
 
281
				return results;
282
			},
283
 
284
			removeMultiple: function(keys, namespace){
285
				namespace = namespace||this.DEFAULT_NAMESPACE;
286
 
287
				dojox.sql.open();
288
				dojox.sql.db.execute("BEGIN TRANSACTION");
289
				var _stmt = "DELETE FROM " + this.TABLE_NAME + " WHERE namespace = ? AND key = ?";
290
 
291
				for(var i=0;i<keys.length;i++){
292
					dojox.sql.db.execute( _stmt,
293
						[namespace, keys[i]]);
294
				}
295
				dojox.sql.db.execute("COMMIT TRANSACTION");
296
				dojox.sql.close();
297
			},
298
 
299
			isPermanent: function(){ return true; },
300
 
301
			getMaximumSize: function(){ return this.SIZE_NO_LIMIT; },
302
 
303
			hasSettingsUI: function(){ return false; },
304
 
305
			showSettingsUI: function(){
306
				throw new Error(this.declaredClass
307
									+ " does not support a storage settings user-interface");
308
			},
309
 
310
			hideSettingsUI: function(){
311
				throw new Error(this.declaredClass
312
									+ " does not support a storage settings user-interface");
313
			}
314
		});
315
 
316
		// register the existence of our storage providers
317
		dojox.storage.manager.register("dojox.storage.GearsStorageProvider",
318
										new dojox.storage.GearsStorageProvider());
319
 
320
		dojox.storage.manager.initialize();
321
	})();
322
}
323
 
324
}