2 * Ext JS Library 0.30
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
10 * @class Ext.sql.Proxy
\r
11 * @extends Ext.data.DataProxy
\r
12 * An implementation of {@link Ext.data.DataProxy} that reads from a SQLLite
\r
16 * @param {Object} conn an {@link Ext.sql.Connection} object
\r
17 * @param {String} table The name of the database table
\r
18 * @param {String} keyName The primary key of the table
\r
19 * @param {Ext.data.Store} store The datastore to bind to
\r
20 * @param {Boolean} readonly By default all changes to the store will be persisted
\r
21 * to the database. Set this to true to override to make the store readonly.
\r
23 Ext.sql.Proxy = function(conn, table, keyName, store, readonly){
\r
24 Ext.sql.Proxy.superclass.constructor.call(this);
\r
26 this.table = this.conn.getTable(table, keyName);
\r
29 if (readonly !== true) {
\r
30 this.store.on('add', this.onAdd, this);
\r
31 this.store.on('update', this.onUpdate, this);
\r
32 this.store.on('remove', this.onRemove, this);
\r
36 Ext.sql.Proxy.DATE_FORMAT = 'Y-m-d H:i:s';
\r
38 Ext.extend(Ext.sql.Proxy, Ext.data.DataProxy, {
\r
39 load : function(params, reader, callback, scope, arg){
\r
40 if(!this.conn.isOpen()){ // assume that the connection is in the process of opening
\r
41 this.conn.on('open', function(){
\r
42 this.load(params, reader, callback, scope, arg);
\r
43 }, this, {single:true});
\r
46 if(this.fireEvent("beforeload", this, params, reader, callback, scope, arg) !== false){
\r
47 var clause = params.where || '';
\r
48 var args = params.args || [];
\r
49 var group = params.groupBy;
\r
50 var sort = params.sort;
\r
51 var dir = params.dir;
\r
54 clause += ' ORDER BY ';
\r
55 if(group && group != sort){
\r
56 clause += group + ' ASC, ';
\r
58 clause += sort + ' ' + (dir || 'ASC');
\r
61 var rs = this.table.selectBy(clause, args);
\r
62 this.onLoad({callback:callback, scope:scope, arg:arg, reader: reader}, rs);
\r
64 callback.call(scope||this, null, arg, false);
\r
68 onLoad : function(trans, rs, e, stmt){
\r
70 this.fireEvent("loadexception", this, null, trans.arg, e);
\r
71 trans.callback.call(trans.scope||window, null, trans.arg, false);
\r
74 var result = trans.reader.readRecords(rs);
\r
75 this.fireEvent("load", this, rs, trans.arg);
\r
76 trans.callback.call(trans.scope||window, result, trans.arg, true);
\r
79 processData : function(o){
\r
80 var fs = this.store.fields;
\r
83 var f = fs.key(key), v = o[key];
\r
85 if(f.type == 'date'){
\r
86 r[key] = v ? v.format(Ext.sql.Proxy.DATE_FORMAT,10) : '';
\r
87 }else if(f.type == 'boolean'){
\r
97 onUpdate : function(ds, record){
\r
98 var changes = record.getChanges();
\r
99 var kn = this.table.keyName;
\r
100 this.table.updateBy(this.processData(changes), kn + ' = ?', [record.data[kn]]);
\r
101 record.commit(true);
\r
104 onAdd : function(ds, records, index){
\r
105 for(var i = 0, len = records.length; i < len; i++){
\r
106 this.table.insert(this.processData(records[i].data));
\r
110 onRemove : function(ds, record, index){
\r
111 var kn = this.table.keyName;
\r
112 this.table.removeBy(kn + ' = ?', [record.data[kn]]);
\r