2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
10 // Asbtract base class for SqlDB classes
\r
11 Ext.data.SqlDB = function(config){
\r
12 Ext.apply(this, config);
\r
13 Ext.data.SqlDB.superclass.constructor.call(this);
\r
21 Ext.extend(Ext.data.SqlDB, Ext.util.Observable, {
\r
26 open : function(file, cb, scope){
\r
32 exec : function(sql, cb, scope){
\r
35 execBy : function(sql, args, cb, scope){
\r
38 query : function(sql, cb, scope){
\r
41 queryBy : function(sql, args, cb, scope){
\r
44 // protected/inherited method
\r
45 isOpen : function(){
\r
46 return this.openState;
\r
49 getTable : function(name, keyName){
\r
50 return new Ext.data.SqlDB.Table(this, name, keyName);
\r
53 createTable : function(o){
\r
54 var tableName = o.name;
\r
55 var keyName = o.key;
\r
57 var cb = o.callback;
\r
58 var scope = o.scope;
\r
59 if(!(fs instanceof Array)){ // Ext fields collection
\r
63 for(var i = 0, len = fs.length; i < len; i++){
\r
64 var f = fs[i], s = f.name;
\r
77 if(f.allowNull === false || f.name == keyName){
\r
80 if(f.name == keyName){
\r
81 s += ' PRIMARY KEY';
\r
83 if(f.unique === true){
\r
87 buf[buf.length] = s;
\r
89 var sql = ['CREATE TABLE IF NOT EXISTS ', tableName, ' (', buf.join(','), ')'].join('');
\r
90 this.exec(sql, cb, scope);
\r
95 Ext.data.SqlDB.getInstance = function(db, config){
\r
96 if(window.htmlControl){ // air
\r
97 return new Ext.data.AirDB(config);
\r
99 return new Ext.data.GearsDB(config);
\r
104 Ext.data.SqlDB.Table = function(conn, name, keyName){
\r
107 this.keyName = keyName;
\r
110 Ext.data.SqlDB.Table.prototype = {
\r
111 update : function(o, cb, scope){
\r
112 var clause = this.keyName + " = :" + this.keyName;
\r
113 this.updateBy(o, clause, {}, cb, scope);
\r
116 updateBy : function(o, clause, args, cb, scope){
\r
117 var sql = "UPDATE " + this.name + " set ";
\r
118 var fs = [], a = [];
\r
120 if(o.hasOwnProperty(key)){
\r
121 fs[fs.length] = key + ' = ?';
\r
122 a[a.length] = o[key];
\r
125 for(var key in args){
\r
126 if(args.hasOwnProperty(key)){
\r
127 a[a.length] = args[key];
\r
130 sql = [sql, fs.join(','), ' WHERE ', clause].join('');
\r
131 this.conn.execBy(sql, a, cb, scope);
\r
134 insert : function(o, cb, scope){
\r
135 var sql = "INSERT into " + this.name + " ";
\r
136 var fs = [], vs = [], a = [];
\r
138 if(o.hasOwnProperty(key)){
\r
139 fs[fs.length] = key;
\r
140 vs[vs.length] = '?';
\r
141 a[a.length] = o[key];
\r
144 sql = [sql, '(', fs.join(','), ') VALUES (', vs.join(','), ')'].join('');
\r
145 this.conn.execBy(sql, a, cb, scope);
\r
148 select : function(clause, cb, scope){
\r
149 this.selectBy(clause, null, cb, scope);
\r
152 selectBy : function(clause, args, cb, scope){
\r
153 var sql = "select * from " + this.name;
\r
155 sql += ' ' + clause;
\r
158 this.conn.queryBy(sql, args, cb, scope);
\r
161 remove : function(clause, cb, scope){
\r
162 this.deleteBy(clause, null, cb, scope);
\r
165 removeBy : function(clause, args, cb, scope){
\r
166 var sql = "delete from " + this.name;
\r
168 sql += ' where ' + clause;
\r
171 this.conn.execBy(sql, args, cb, scope);
\r
175 Ext.data.SqlDB.Proxy = function(conn, table, keyName, store){
\r
176 Ext.data.SqlDB.Proxy.superclass.constructor.call(this);
\r
178 this.table = this.conn.getTable(table, keyName);
\r
179 this.store = store;
\r
181 this.store.on('add', this.onAdd, this);
\r
182 this.store.on('update', this.onUpdate, this);
\r
183 this.store.on('remove', this.onRemove, this);
\r
186 Ext.extend(Ext.data.SqlDB.Proxy, Ext.data.DataProxy, {
\r
187 load : function(params, reader, callback, scope, arg){
\r
188 if(!this.conn.isOpen()){ // assume that the connection is in the process of opening
\r
189 this.conn.on('open', function(){
\r
190 this.load(params, reader, callback, scope, arg);
\r
191 }, this, {single:true});
\r
194 if(this.fireEvent("beforeload", this, params, reader, callback, scope, arg) !== false){
\r
195 var clause = params.where || '';
\r
196 var args = params.args || [];
\r
197 var group = params.groupBy;
\r
198 var sort = params.sort;
\r
199 var dir = params.dir;
\r
202 clause += ' ORDER BY ';
\r
203 if(group && group != sort){
\r
204 clause += group + ' ASC, ';
\r
206 clause += sort + ' ' + (dir || 'ASC');
\r
209 this.table.selectBy(clause, args,
\r
210 this.onLoad.createDelegate(this, [{callback:callback, scope:scope, arg:arg, reader: reader}], 0));
\r
212 callback.call(scope||this, null, arg, false);
\r
216 onLoad : function(trans, rs, e, stmt){
\r
218 this.fireEvent("loadexception", this, null, trans.arg, e);
\r
219 trans.callback.call(trans.scope||window, null, trans.arg, false);
\r
222 var result = trans.reader.readRecords(rs);
\r
223 this.fireEvent("load", this, rs, trans.arg);
\r
224 trans.callback.call(trans.scope||window, result, trans.arg, true);
\r
227 processData : function(o){
\r
228 var fs = this.store.fields;
\r
231 var f = fs.key(key), v = o[key];
\r
233 if(f.type == 'date'){
\r
234 r[key] = v ? v.format('Y-m-d H:i:s') : '';
\r
235 }else if(f.type == 'boolean'){
\r
236 r[key] = v ? 1 : 0;
\r
245 onUpdate : function(ds, record){
\r
246 var changes = record.getChanges();
\r
247 var kn = this.table.keyName;
\r
248 this.table.updateBy(this.processData(changes), kn + ' = ?', [record.data[kn]]);
\r
249 record.commit(true);
\r
252 onAdd : function(ds, records, index){
\r
253 for(var i = 0, len = records.length; i < len; i++){
\r
254 this.table.insert(this.processData(records[i].data));
\r
258 onRemove : function(ds, record, index){
\r
259 var kn = this.table.keyName;
\r
260 this.table.removeBy(kn + ' = ?', [record.data[kn]]);
\r