1 | dojo.provide("dojox.storage.AirDBStorageProvider"); |
---|
2 | dojo.require("dojox.storage.manager"); |
---|
3 | dojo.require("dojox.storage.Provider"); |
---|
4 | |
---|
5 | if (dojo.isAIR) { |
---|
6 | (function(){ |
---|
7 | |
---|
8 | if (!air) { |
---|
9 | var air = {}; |
---|
10 | } |
---|
11 | air.File = window.runtime.flash.filesystem.File; |
---|
12 | air.SQLConnection = window.runtime.flash.data.SQLConnection; |
---|
13 | air.SQLStatement = window.runtime.flash.data.SQLStatement; |
---|
14 | |
---|
15 | // summary: |
---|
16 | // Storage provider that uses features in the Adobe AIR runtime to achieve |
---|
17 | // permanent storage |
---|
18 | dojo.declare("dojox.storage.AirDBStorageProvider", [ dojox.storage.Provider ], { |
---|
19 | DATABASE_FILE: "dojo.db", |
---|
20 | TABLE_NAME: "__DOJO_STORAGE", |
---|
21 | initialized: false, |
---|
22 | |
---|
23 | _db: null, |
---|
24 | |
---|
25 | initialize: function(){ |
---|
26 | this.initialized = false; |
---|
27 | |
---|
28 | // need to initialize our storage database |
---|
29 | try{ |
---|
30 | this._db = new air.SQLConnection(); |
---|
31 | this._db.open(air.File.applicationStorageDirectory.resolvePath(this.DATABASE_FILE)); |
---|
32 | |
---|
33 | this._sql("CREATE TABLE IF NOT EXISTS " + this.TABLE_NAME + "(namespace TEXT, key TEXT, value TEXT)"); |
---|
34 | this._sql("CREATE UNIQUE INDEX IF NOT EXISTS namespace_key_index ON " + this.TABLE_NAME + " (namespace, key)"); |
---|
35 | |
---|
36 | this.initialized = true; |
---|
37 | }catch(e){ |
---|
38 | console.debug("dojox.storage.AirDBStorageProvider.initialize:", e); |
---|
39 | } |
---|
40 | |
---|
41 | // indicate that this storage provider is now loaded |
---|
42 | dojox.storage.manager.loaded(); |
---|
43 | }, |
---|
44 | |
---|
45 | _sql: function(query, params){ |
---|
46 | var stmt = new air.SQLStatement(); |
---|
47 | stmt.sqlConnection = this._db; |
---|
48 | stmt.text = query; |
---|
49 | if (params){ |
---|
50 | for (var param in params){ |
---|
51 | stmt.parameters[param] = params[param]; |
---|
52 | } |
---|
53 | } |
---|
54 | stmt.execute(); |
---|
55 | return stmt.getResult(); |
---|
56 | }, |
---|
57 | |
---|
58 | _beginTransaction: function(){ |
---|
59 | this._db.begin(); |
---|
60 | }, |
---|
61 | |
---|
62 | _commitTransaction: function(){ |
---|
63 | this._db.commit(); |
---|
64 | }, |
---|
65 | |
---|
66 | isAvailable: function(){ |
---|
67 | return true; |
---|
68 | }, |
---|
69 | |
---|
70 | put: function(key, value, resultsHandler, namespace){ |
---|
71 | if(this.isValidKey(key) == false){ |
---|
72 | throw new Error("Invalid key given: " + key); |
---|
73 | } |
---|
74 | namespace = namespace||this.DEFAULT_NAMESPACE; |
---|
75 | if(this.isValidKey(namespace) == false){ |
---|
76 | throw new Error("Invalid namespace given: " + namespace); |
---|
77 | } |
---|
78 | |
---|
79 | // try to store the value |
---|
80 | try{ |
---|
81 | this._sql("DELETE FROM " + this.TABLE_NAME + " WHERE namespace = :namespace AND key = :key", |
---|
82 | { ":namespace":namespace, ":key":key }); |
---|
83 | this._sql("INSERT INTO " + this.TABLE_NAME + " VALUES (:namespace, :key, :value)", |
---|
84 | { ":namespace":namespace, ":key":key, ":value":value }); |
---|
85 | }catch(e){ |
---|
86 | // indicate we failed |
---|
87 | console.debug("dojox.storage.AirDBStorageProvider.put:", e); |
---|
88 | resultsHandler(this.FAILED, key, e.toString()); |
---|
89 | return; |
---|
90 | } |
---|
91 | |
---|
92 | if(resultsHandler){ |
---|
93 | resultsHandler(this.SUCCESS, key, null, namespace); |
---|
94 | } |
---|
95 | }, |
---|
96 | |
---|
97 | get: function(key, namespace){ |
---|
98 | if(this.isValidKey(key) == false){ |
---|
99 | throw new Error("Invalid key given: " + key); |
---|
100 | } |
---|
101 | namespace = namespace||this.DEFAULT_NAMESPACE; |
---|
102 | |
---|
103 | var results = this._sql("SELECT * FROM " + this.TABLE_NAME + " WHERE namespace = :namespace AND key = :key", |
---|
104 | { ":namespace":namespace, ":key":key }); |
---|
105 | |
---|
106 | if(results.data && results.data.length){ |
---|
107 | return results.data[0].value; |
---|
108 | } |
---|
109 | |
---|
110 | return null; |
---|
111 | }, |
---|
112 | |
---|
113 | getNamespaces: function(){ |
---|
114 | var results = [ this.DEFAULT_NAMESPACE ]; |
---|
115 | var rs = this._sql("SELECT namespace FROM " + this.TABLE_NAME + " DESC GROUP BY namespace"); |
---|
116 | if (rs.data){ |
---|
117 | for(var i = 0; i < rs.data.length; i++){ |
---|
118 | if(rs.data[i].namespace != this.DEFAULT_NAMESPACE){ |
---|
119 | results.push(rs.data[i].namespace); |
---|
120 | } |
---|
121 | } |
---|
122 | } |
---|
123 | return results; |
---|
124 | }, |
---|
125 | |
---|
126 | getKeys: function(namespace){ |
---|
127 | namespace = namespace||this.DEFAULT_NAMESPACE; |
---|
128 | if(this.isValidKey(namespace) == false){ |
---|
129 | throw new Error("Invalid namespace given: " + namespace); |
---|
130 | } |
---|
131 | |
---|
132 | var results = []; |
---|
133 | var rs = this._sql("SELECT key FROM " + this.TABLE_NAME + " WHERE namespace = :namespace", { ":namespace":namespace }); |
---|
134 | if (rs.data){ |
---|
135 | for(var i = 0; i < rs.data.length; i++){ |
---|
136 | results.push(rs.data[i].key); |
---|
137 | } |
---|
138 | } |
---|
139 | return results; |
---|
140 | }, |
---|
141 | |
---|
142 | clear: function(namespace){ |
---|
143 | if(this.isValidKey(namespace) == false){ |
---|
144 | throw new Error("Invalid namespace given: " + namespace); |
---|
145 | } |
---|
146 | this._sql("DELETE FROM " + this.TABLE_NAME + " WHERE namespace = :namespace", { ":namespace":namespace }); |
---|
147 | }, |
---|
148 | |
---|
149 | remove: function(key, namespace){ |
---|
150 | namespace = namespace||this.DEFAULT_NAMESPACE; |
---|
151 | this._sql("DELETE FROM " + this.TABLE_NAME + " WHERE namespace = :namespace AND key = :key", |
---|
152 | { ":namespace":namespace, ":key":key }); |
---|
153 | }, |
---|
154 | |
---|
155 | putMultiple: function(keys, values, resultsHandler, namespace) { |
---|
156 | if(this.isValidKeyArray(keys) === false |
---|
157 | || ! values instanceof Array |
---|
158 | || keys.length != values.length){ |
---|
159 | throw new Error("Invalid arguments: keys = [" + keys + "], values = [" + values + "]"); |
---|
160 | } |
---|
161 | |
---|
162 | if(namespace == null || typeof namespace == "undefined"){ |
---|
163 | namespace = this.DEFAULT_NAMESPACE; |
---|
164 | } |
---|
165 | |
---|
166 | if(this.isValidKey(namespace) == false){ |
---|
167 | throw new Error("Invalid namespace given: " + namespace); |
---|
168 | } |
---|
169 | |
---|
170 | this._statusHandler = resultsHandler; |
---|
171 | |
---|
172 | // try to store the value |
---|
173 | try{ |
---|
174 | this._beginTransaction(); |
---|
175 | for(var i=0;i<keys.length;i++) { |
---|
176 | this._sql("DELETE FROM " + this.TABLE_NAME + " WHERE namespace = :namespace AND key = :key", |
---|
177 | { ":namespace":namespace, ":key":keys[i] }); |
---|
178 | this._sql("INSERT INTO " + this.TABLE_NAME + " VALUES (:namespace, :key, :value)", |
---|
179 | { ":namespace":namespace, ":key":keys[i], ":value":values[i] }); |
---|
180 | } |
---|
181 | this._commitTransaction(); |
---|
182 | }catch(e){ |
---|
183 | // indicate we failed |
---|
184 | console.debug("dojox.storage.AirDBStorageProvider.putMultiple:", e); |
---|
185 | if(resultsHandler){ |
---|
186 | resultsHandler(this.FAILED, keys, e.toString(), namespace); |
---|
187 | } |
---|
188 | return; |
---|
189 | } |
---|
190 | |
---|
191 | if(resultsHandler){ |
---|
192 | resultsHandler(this.SUCCESS, keys, null); |
---|
193 | } |
---|
194 | }, |
---|
195 | |
---|
196 | getMultiple: function(keys, namespace){ |
---|
197 | if(this.isValidKeyArray(keys) === false){ |
---|
198 | throw new Error("Invalid key array given: " + keys); |
---|
199 | } |
---|
200 | |
---|
201 | if(namespace == null || typeof namespace == "undefined"){ |
---|
202 | namespace = this.DEFAULT_NAMESPACE; |
---|
203 | } |
---|
204 | |
---|
205 | if(this.isValidKey(namespace) == false){ |
---|
206 | throw new Error("Invalid namespace given: " + namespace); |
---|
207 | } |
---|
208 | |
---|
209 | var results = []; |
---|
210 | for(var i=0;i<keys.length;i++){ |
---|
211 | var result = this._sql("SELECT * FROM " + this.TABLE_NAME + " WHERE namespace = :namespace AND key = :key", |
---|
212 | { ":namespace":namespace, ":key":keys[i] }); |
---|
213 | results[i] = result.data && result.data.length ? result.data[0].value : null; |
---|
214 | } |
---|
215 | |
---|
216 | return results; |
---|
217 | }, |
---|
218 | |
---|
219 | removeMultiple: function(keys, namespace){ |
---|
220 | namespace = namespace||this.DEFAULT_NAMESPACE; |
---|
221 | |
---|
222 | this._beginTransaction(); |
---|
223 | for(var i=0;i<keys.length;i++){ |
---|
224 | this._sql("DELETE FROM " + this.TABLE_NAME + " WHERE namespace = namespace = :namespace AND key = :key", |
---|
225 | { ":namespace":namespace, ":key":keys[i] }); |
---|
226 | } |
---|
227 | this._commitTransaction(); |
---|
228 | }, |
---|
229 | |
---|
230 | isPermanent: function(){ return true; }, |
---|
231 | |
---|
232 | getMaximumSize: function(){ return this.SIZE_NO_LIMIT; }, |
---|
233 | |
---|
234 | hasSettingsUI: function(){ return false; }, |
---|
235 | |
---|
236 | showSettingsUI: function(){ |
---|
237 | throw new Error(this.declaredClass + " does not support a storage settings user-interface"); |
---|
238 | }, |
---|
239 | |
---|
240 | hideSettingsUI: function(){ |
---|
241 | throw new Error(this.declaredClass + " does not support a storage settings user-interface"); |
---|
242 | } |
---|
243 | }); |
---|
244 | |
---|
245 | dojox.storage.manager.register("dojox.storage.AirDBStorageProvider", new dojox.storage.AirDBStorageProvider()); |
---|
246 | dojox.storage.manager.initialize(); |
---|
247 | })(); |
---|
248 | } |
---|