ext-db_wev8.js
6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Ext JS Library 2.0.2
* Copyright(c) 2006-2008, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
// Asbtract base class for SqlDB classes
Ext.data.SqlDB = function(config){
Ext.apply(this, config);
Ext.data.SqlDB.superclass.constructor.call(this);
this.addEvents({
open : true,
close: true
});
};
Ext.extend(Ext.data.SqlDB, Ext.util.Observable, {
maxResults: 10000,
openState : false,
// abstract methods
open : function(file, cb, scope){
},
close : function(){
},
exec : function(sql, cb, scope){
},
execBy : function(sql, args, cb, scope){
},
query : function(sql, cb, scope){
},
queryBy : function(sql, args, cb, scope){
},
// protected/inherited method
isOpen : function(){
return this.openState;
},
getTable : function(name, keyName){
return new Ext.data.SqlDB.Table(this, name, keyName);
},
createTable : function(o){
var tableName = o.name;
var keyName = o.key;
var fs = o.fields;
var cb = o.callback;
var scope = o.scope;
if(!(fs instanceof Array)){ // Ext fields collection
fs = fs.items;
}
var buf = [];
for(var i = 0, len = fs.length; i < len; i++){
var f = fs[i], s = f.name;
switch(f.type){
case "int":
case "bool":
case "boolean":
s += ' INTEGER';
break;
case "float":
s += ' REAL';
break;
default:
s += ' TEXT';
}
if(f.allowNull === false || f.name == keyName){
s += ' NOT NULL';
}
if(f.name == keyName){
s += ' PRIMARY KEY';
}
if(f.unique === true){
s += ' UNIQUE';
}
buf[buf.length] = s;
}
var sql = ['CREATE TABLE IF NOT EXISTS ', tableName, ' (', buf.join(','), ')'].join('');
this.exec(sql, cb, scope);
}
});
Ext.data.SqlDB.getInstance = function(db, config){
if(window.htmlControl){ // air
return new Ext.data.AirDB(config);
} else { // gears
return new Ext.data.GearsDB(config);
}
};
Ext.data.SqlDB.Table = function(conn, name, keyName){
this.conn = conn;
this.name = name;
this.keyName = keyName;
};
Ext.data.SqlDB.Table.prototype = {
update : function(o, cb, scope){
var clause = this.keyName + " = :" + this.keyName;
this.updateBy(o, clause, {}, cb, scope);
},
updateBy : function(o, clause, args, cb, scope){
var sql = "UPDATE " + this.name + " set ";
var fs = [], a = [];
for(var key in o){
if(o.hasOwnProperty(key)){
fs[fs.length] = key + ' = ?';
a[a.length] = o[key];
}
}
for(var key in args){
if(args.hasOwnProperty(key)){
a[a.length] = args[key];
}
}
sql = [sql, fs.join(','), ' WHERE ', clause].join('');
this.conn.execBy(sql, a, cb, scope);
},
insert : function(o, cb, scope){
var sql = "INSERT into " + this.name + " ";
var fs = [], vs = [], a = [];
for(var key in o){
if(o.hasOwnProperty(key)){
fs[fs.length] = key;
vs[vs.length] = '?';
a[a.length] = o[key];
}
}
sql = [sql, '(', fs.join(','), ') VALUES (', vs.join(','), ')'].join('');
this.conn.execBy(sql, a, cb, scope);
},
select : function(clause, cb, scope){
this.selectBy(clause, null, cb, scope);
},
selectBy : function(clause, args, cb, scope){
var sql = "select * from " + this.name;
if(clause){
sql += ' ' + clause;
}
args = args || {};
this.conn.queryBy(sql, args, cb, scope);
},
remove : function(clause, cb, scope){
this.deleteBy(clause, null, cb, scope);
},
removeBy : function(clause, args, cb, scope){
var sql = "delete from " + this.name;
if(clause){
sql += ' where ' + clause;
}
args = args || {};
this.conn.execBy(sql, args, cb, scope);
}
};
Ext.data.SqlDB.Proxy = function(conn, table, keyName, store){
Ext.data.SqlDB.Proxy.superclass.constructor.call(this);
this.conn = conn;
this.table = this.conn.getTable(table, keyName);
this.store = store;
this.store.on('add', this.onAdd, this);
this.store.on('update', this.onUpdate, this);
this.store.on('remove', this.onRemove, this);
};
Ext.extend(Ext.data.SqlDB.Proxy, Ext.data.DataProxy, {
load : function(params, reader, callback, scope, arg){
if(!this.conn.isOpen()){ // assume that the connection is in the process of opening
this.conn.on('open', function(){
this.load(params, reader, callback, scope, arg);
}, this, {single:true});
return;
};
if(this.fireEvent("beforeload", this, params, reader, callback, scope, arg) !== false){
var clause = params.where || '';
var args = params.args || [];
var group = params.groupBy;
var sort = params.sort;
var dir = params.dir;
if(group || sort){
clause += ' ORDER BY ';
if(group && group != sort){
clause += group + ' ASC, ';
}
clause += sort + ' ' + (dir || 'ASC');
}
this.table.selectBy(clause, args,
this.onLoad.createDelegate(this, [{callback:callback, scope:scope, arg:arg, reader: reader}], 0));
}else{
callback.call(scope||this, null, arg, false);
}
},
onLoad : function(trans, rs, e, stmt){
if(rs === false){
this.fireEvent("loadexception", this, null, trans.arg, e);
trans.callback.call(trans.scope||window, null, trans.arg, false);
return;
}
var result = trans.reader.readRecords(rs);
this.fireEvent("load", this, rs, trans.arg);
trans.callback.call(trans.scope||window, result, trans.arg, true);
},
processData : function(o){
var fs = this.store.fields;
var r = {};
for(var key in o){
var f = fs.key(key), v = o[key];
if(f){
if(f.type == 'date'){
r[key] = v ? v.format('Y-m-d H:i:s') : '';
}else if(f.type == 'boolean'){
r[key] = v ? 1 : 0;
}else{
r[key] = v;
}
}
}
return r;
},
onUpdate : function(ds, record){
var changes = record.getChanges();
var kn = this.table.keyName;
this.table.updateBy(this.processData(changes), kn + ' = ?', [record.data[kn]]);
record.commit(true);
},
onAdd : function(ds, records, index){
for(var i = 0, len = records.length; i < len; i++){
this.table.insert(this.processData(records[i].data));
}
},
onRemove : function(ds, record, index){
var kn = this.table.keyName;
this.table.removeBy(kn + ' = ?', [record.data[kn]]);
}
});