3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
8 // Asbtract base class for SqlDB classes
9 Ext.data.SqlDB = function(config){
10 Ext.apply(this, config);
11 Ext.data.SqlDB.superclass.constructor.call(this);
19 Ext.extend(Ext.data.SqlDB, Ext.util.Observable, {
24 open : function(file, cb, scope){
30 exec : function(sql, cb, scope){
33 execBy : function(sql, args, cb, scope){
36 query : function(sql, cb, scope){
39 queryBy : function(sql, args, cb, scope){
42 // protected/inherited method
44 return this.openState;
47 getTable : function(name, keyName){
48 return new Ext.data.SqlDB.Table(this, name, keyName);
51 createTable : function(o){
52 var tableName = o.name,
58 types = Ext.data.Types;
60 if(!(fs instanceof Array)){ // Ext fields collection
63 for(var i = 0, len = fs.length; i < len; i++){
77 if(f.allowNull === false || f.name == keyName){
80 if(f.name == keyName){
83 if(f.unique === true){
89 var sql = ['CREATE TABLE IF NOT EXISTS ', tableName, ' (', buf.join(','), ')'].join('');
90 this.exec(sql, cb, scope);
95 Ext.data.SqlDB.getInstance = function(db, config){
96 if(window.htmlControl){ // air
97 return new Ext.data.AirDB(config);
99 return new Ext.data.GearsDB(config);
104 Ext.data.SqlDB.Table = function(conn, name, keyName){
107 this.keyName = keyName;
110 Ext.data.SqlDB.Table.prototype = {
111 update : function(o, cb, scope){
112 var clause = this.keyName + " = :" + this.keyName;
113 this.updateBy(o, clause, {}, cb, scope);
116 updateBy : function(o, clause, args, cb, scope){
117 var sql = "UPDATE " + this.name + " set ";
120 if(o.hasOwnProperty(key)){
121 fs[fs.length] = key + ' = ?';
122 a[a.length] = o[key];
125 for(var key in args){
126 if(args.hasOwnProperty(key)){
127 a[a.length] = args[key];
130 sql = [sql, fs.join(','), ' WHERE ', clause].join('');
131 this.conn.execBy(sql, a, cb, scope);
134 insert : function(o, cb, scope){
135 var sql = "INSERT into " + this.name + " ";
136 var fs = [], vs = [], a = [];
138 if(o.hasOwnProperty(key)){
141 a[a.length] = o[key];
144 sql = [sql, '(', fs.join(','), ') VALUES (', vs.join(','), ')'].join('');
145 this.conn.execBy(sql, a, cb, scope);
148 select : function(clause, cb, scope){
149 this.selectBy(clause, null, cb, scope);
152 selectBy : function(clause, args, cb, scope){
153 var sql = "select * from " + this.name;
158 this.conn.queryBy(sql, args, cb, scope);
161 remove : function(clause, cb, scope){
162 this.deleteBy(clause, null, cb, scope);
165 removeBy : function(clause, args, cb, scope){
166 var sql = "delete from " + this.name;
168 sql += ' where ' + clause;
171 this.conn.execBy(sql, args, cb, scope);
175 Ext.data.SqlDB.Proxy = function(conn, table, keyName, store){
176 Ext.data.SqlDB.Proxy.superclass.constructor.call(this);
178 this.table = this.conn.getTable(table, keyName);
181 this.store.on('add', this.onAdd, this);
182 this.store.on('update', this.onUpdate, this);
183 this.store.on('remove', this.onRemove, this);
186 Ext.extend(Ext.data.SqlDB.Proxy, Ext.data.DataProxy, {
187 load : function(params, reader, callback, scope, arg){
188 if(!this.conn.isOpen()){ // assume that the connection is in the process of opening
189 this.conn.on('open', function(){
190 this.load(params, reader, callback, scope, arg);
191 }, this, {single:true});
194 if(this.fireEvent("beforeload", this, params, reader, callback, scope, arg) !== false){
195 var clause = params.where || '';
196 var args = params.args || [];
197 var group = params.groupBy;
198 var sort = params.sort;
199 var dir = params.dir;
202 clause += ' ORDER BY ';
203 if(group && group != sort){
204 clause += group + ' ASC, ';
206 clause += sort + ' ' + (dir || 'ASC');
209 this.table.selectBy(clause, args,
210 this.onLoad.createDelegate(this, [{callback:callback, scope:scope, arg:arg, reader: reader}], 0));
212 callback.call(scope||this, null, arg, false);
216 onLoad : function(trans, rs, e, stmt){
218 this.fireEvent("loadexception", this, null, trans.arg, e);
219 trans.callback.call(trans.scope||window, null, trans.arg, false);
222 var result = trans.reader.readRecords(rs);
223 this.fireEvent("load", this, rs, trans.arg);
224 trans.callback.call(trans.scope||window, result, trans.arg, true);
227 processData : function(o){
228 var fs = this.store.fields,
230 types = Ext.data.Types;
232 var f = fs.key(key), v = o[key];
234 if(f.type == types.DATE){
235 r[key] = v ? v.format('Y-m-d H:i:s') : '';
236 }else if(f.type == types.BOOL){
246 onUpdate : function(ds, record){
247 var changes = record.getChanges();
248 var kn = this.table.keyName;
249 this.table.updateBy(this.processData(changes), kn + ' = ?', [record.data[kn]]);
253 onAdd : function(ds, records, index){
254 for(var i = 0, len = records.length; i < len; i++){
255 this.table.insert(this.processData(records[i].data));
259 onRemove : function(ds, record, index){
260 var kn = this.table.keyName;
261 this.table.removeBy(kn + ' = ?', [record.data[kn]]);