root / trunk / web / dojo / dojox / storage / AirDBStorageProvider.js
History | View | Annotate | Download (5.55 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
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.AirDBStorageProvider"]){ |
||
9 | dojo._hasResource["dojox.storage.AirDBStorageProvider"]=true; |
||
10 | dojo.provide("dojox.storage.AirDBStorageProvider");
|
||
11 | dojo.require("dojox.storage.manager");
|
||
12 | dojo.require("dojox.storage.Provider");
|
||
13 | if(dojo.isAIR){
|
||
14 | (function(){
|
||
15 | if(!_1){
|
||
16 | var _1={};
|
||
17 | } |
||
18 | _1.File=window.runtime.flash.filesystem.File; |
||
19 | _1.SQLConnection=window.runtime.flash.data.SQLConnection; |
||
20 | _1.SQLStatement=window.runtime.flash.data.SQLStatement; |
||
21 | dojo.declare("dojox.storage.AirDBStorageProvider",[dojox.storage.Provider],{DATABASE_FILE:"dojo.db",TABLE_NAME:"__DOJO_STORAGE",initialized:false,_db:null,initialize:function(){ |
||
22 | this.initialized=false; |
||
23 | try{
|
||
24 | this._db=new _1.SQLConnection(); |
||
25 | this._db.open(_1.File.applicationStorageDirectory.resolvePath(this.DATABASE_FILE)); |
||
26 | this._sql("CREATE TABLE IF NOT EXISTS "+this.TABLE_NAME+"(namespace TEXT, key TEXT, value TEXT)"); |
||
27 | this._sql("CREATE UNIQUE INDEX IF NOT EXISTS namespace_key_index ON "+this.TABLE_NAME+" (namespace, key)"); |
||
28 | this.initialized=true; |
||
29 | } |
||
30 | catch(e){
|
||
31 | } |
||
32 | dojox.storage.manager.loaded(); |
||
33 | },_sql:function(_2,_3){ |
||
34 | var _4=new _1.SQLStatement(); |
||
35 | _4.sqlConnection=this._db;
|
||
36 | _4.text=_2; |
||
37 | if(_3){
|
||
38 | for(var _5 in _3){ |
||
39 | _4.parameters[_5]=_3[_5]; |
||
40 | } |
||
41 | } |
||
42 | _4.execute(); |
||
43 | return _4.getResult();
|
||
44 | },_beginTransaction:function(){ |
||
45 | this._db.begin();
|
||
46 | },_commitTransaction:function(){ |
||
47 | this._db.commit();
|
||
48 | },isAvailable:function(){ |
||
49 | return true; |
||
50 | },put:function(_6,_7,_8,_9){ |
||
51 | if(this.isValidKey(_6)==false){ |
||
52 | throw new Error("Invalid key given: "+_6); |
||
53 | } |
||
54 | _9=_9||this.DEFAULT_NAMESPACE;
|
||
55 | if(this.isValidKey(_9)==false){ |
||
56 | throw new Error("Invalid namespace given: "+_9); |
||
57 | } |
||
58 | try{
|
||
59 | this._sql("DELETE FROM "+this.TABLE_NAME+" WHERE namespace = :namespace AND key = :key",{":namespace":_9,":key":_6}); |
||
60 | this._sql("INSERT INTO "+this.TABLE_NAME+" VALUES (:namespace, :key, :value)",{":namespace":_9,":key":_6,":value":_7}); |
||
61 | } |
||
62 | catch(e){
|
||
63 | _8(this.FAILED,_6,e.toString());
|
||
64 | return;
|
||
65 | } |
||
66 | if(_8){
|
||
67 | _8(this.SUCCESS,_6,null,_9); |
||
68 | } |
||
69 | },get:function(_a,_b){ |
||
70 | if(this.isValidKey(_a)==false){ |
||
71 | throw new Error("Invalid key given: "+_a); |
||
72 | } |
||
73 | _b=_b||this.DEFAULT_NAMESPACE;
|
||
74 | var _c=this._sql("SELECT * FROM "+this.TABLE_NAME+" WHERE namespace = :namespace AND key = :key",{":namespace":_b,":key":_a}); |
||
75 | if(_c.data&&_c.data.length){
|
||
76 | return _c.data[0].value; |
||
77 | } |
||
78 | return null; |
||
79 | },getNamespaces:function(){ |
||
80 | var _d=[this.DEFAULT_NAMESPACE]; |
||
81 | var rs=this._sql("SELECT namespace FROM "+this.TABLE_NAME+" DESC GROUP BY namespace"); |
||
82 | if(rs.data){
|
||
83 | for(var i=0;i<rs.data.length;i++){ |
||
84 | if(rs.data[i].namespace!=this.DEFAULT_NAMESPACE){ |
||
85 | _d.push(rs.data[i].namespace); |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 | return _d;
|
||
90 | },getKeys:function(_e){ |
||
91 | _e=_e||this.DEFAULT_NAMESPACE;
|
||
92 | if(this.isValidKey(_e)==false){ |
||
93 | throw new Error("Invalid namespace given: "+_e); |
||
94 | } |
||
95 | var _f=[];
|
||
96 | var rs=this._sql("SELECT key FROM "+this.TABLE_NAME+" WHERE namespace = :namespace",{":namespace":_e}); |
||
97 | if(rs.data){
|
||
98 | for(var i=0;i<rs.data.length;i++){ |
||
99 | _f.push(rs.data[i].key); |
||
100 | } |
||
101 | } |
||
102 | return _f;
|
||
103 | },clear:function(_10){ |
||
104 | if(this.isValidKey(_10)==false){ |
||
105 | throw new Error("Invalid namespace given: "+_10); |
||
106 | } |
||
107 | this._sql("DELETE FROM "+this.TABLE_NAME+" WHERE namespace = :namespace",{":namespace":_10}); |
||
108 | },remove:function(key,_11){ |
||
109 | _11=_11||this.DEFAULT_NAMESPACE;
|
||
110 | this._sql("DELETE FROM "+this.TABLE_NAME+" WHERE namespace = :namespace AND key = :key",{":namespace":_11,":key":key}); |
||
111 | },putMultiple:function(_12,_13,_14,_15){ |
||
112 | if(this.isValidKeyArray(_12)===false||!_13 instanceof Array||_12.length!=_13.length){ |
||
113 | throw new Error("Invalid arguments: keys = ["+_12+"], values = ["+_13+"]"); |
||
114 | } |
||
115 | if(_15==null||typeof _15=="undefined"){ |
||
116 | _15=this.DEFAULT_NAMESPACE;
|
||
117 | } |
||
118 | if(this.isValidKey(_15)==false){ |
||
119 | throw new Error("Invalid namespace given: "+_15); |
||
120 | } |
||
121 | this._statusHandler=_14;
|
||
122 | try{
|
||
123 | this._beginTransaction();
|
||
124 | for(var i=0;i<_12.length;i++){ |
||
125 | this._sql("DELETE FROM "+this.TABLE_NAME+" WHERE namespace = :namespace AND key = :key",{":namespace":_15,":key":_12[i]}); |
||
126 | this._sql("INSERT INTO "+this.TABLE_NAME+" VALUES (:namespace, :key, :value)",{":namespace":_15,":key":_12[i],":value":_13[i]}); |
||
127 | } |
||
128 | this._commitTransaction();
|
||
129 | } |
||
130 | catch(e){
|
||
131 | if(_14){
|
||
132 | _14(this.FAILED,_12,e.toString(),_15);
|
||
133 | } |
||
134 | return;
|
||
135 | } |
||
136 | if(_14){
|
||
137 | _14(this.SUCCESS,_12,null); |
||
138 | } |
||
139 | },getMultiple:function(_16,_17){ |
||
140 | if(this.isValidKeyArray(_16)===false){ |
||
141 | throw new Error("Invalid key array given: "+_16); |
||
142 | } |
||
143 | if(_17==null||typeof _17=="undefined"){ |
||
144 | _17=this.DEFAULT_NAMESPACE;
|
||
145 | } |
||
146 | if(this.isValidKey(_17)==false){ |
||
147 | throw new Error("Invalid namespace given: "+_17); |
||
148 | } |
||
149 | var _18=[];
|
||
150 | for(var i=0;i<_16.length;i++){ |
||
151 | var _19=this._sql("SELECT * FROM "+this.TABLE_NAME+" WHERE namespace = :namespace AND key = :key",{":namespace":_17,":key":_16[i]}); |
||
152 | _18[i]=_19.data&&_19.data.length?_19.data[0].value:null; |
||
153 | } |
||
154 | return _18;
|
||
155 | },removeMultiple:function(_1a,_1b){ |
||
156 | _1b=_1b||this.DEFAULT_NAMESPACE;
|
||
157 | this._beginTransaction();
|
||
158 | for(var i=0;i<_1a.length;i++){ |
||
159 | this._sql("DELETE FROM "+this.TABLE_NAME+" WHERE namespace = namespace = :namespace AND key = :key",{":namespace":_1b,":key":_1a[i]}); |
||
160 | } |
||
161 | this._commitTransaction();
|
||
162 | },isPermanent:function(){ |
||
163 | return true; |
||
164 | },getMaximumSize:function(){ |
||
165 | return this.SIZE_NO_LIMIT; |
||
166 | },hasSettingsUI:function(){ |
||
167 | return false; |
||
168 | },showSettingsUI:function(){ |
||
169 | throw new Error(this.declaredClass+" does not support a storage settings user-interface"); |
||
170 | },hideSettingsUI:function(){ |
||
171 | throw new Error(this.declaredClass+" does not support a storage settings user-interface"); |
||
172 | }}); |
||
173 | dojox.storage.manager.register("dojox.storage.AirDBStorageProvider",new dojox.storage.AirDBStorageProvider()); |
||
174 | dojox.storage.manager.initialize(); |
||
175 | })(); |
||
176 | } |
||
177 | } |