source: trunk/web/addons/job_monarch/lib/extjs/air/src/sql/Proxy.js @ 619

Last change on this file since 619 was 619, checked in by ramonb, 15 years ago

lib/:

  • added new AJAX dependancies: ExtJS, pChart, Lightbox2
File size: 3.5 KB
Line 
1/*
2 * Ext JS Library 0.30
3 * Copyright(c) 2006-2009, Ext JS, LLC.
4 * licensing@extjs.com
5 *
6 * http://extjs.com/license
7 */
8
9/**
10 * @class Ext.sql.Proxy
11 * @extends Ext.data.DataProxy
12 * An implementation of {@link Ext.data.DataProxy} that reads from a SQLLite
13 * database.
14 *
15 * @constructor
16 * @param {Object} conn an {@link Ext.sql.Connection} object
17 * @param {String} table The name of the database table
18 * @param {String} keyName The primary key of the table
19 * @param {Ext.data.Store} store The datastore to bind to
20 * @param {Boolean} readonly By default all changes to the store will be persisted
21 * to the database. Set this to true to override to make the store readonly.
22 */
23Ext.sql.Proxy = function(conn, table, keyName, store, readonly){
24    Ext.sql.Proxy.superclass.constructor.call(this);
25    this.conn = conn;
26    this.table = this.conn.getTable(table, keyName);
27    this.store = store;
28
29        if (readonly !== true) {
30                this.store.on('add', this.onAdd, this);
31                this.store.on('update', this.onUpdate, this);
32                this.store.on('remove', this.onRemove, this);
33        }
34};
35
36Ext.sql.Proxy.DATE_FORMAT = 'Y-m-d H:i:s';
37
38Ext.extend(Ext.sql.Proxy, Ext.data.DataProxy, {
39    load : function(params, reader, callback, scope, arg){
40        if(!this.conn.isOpen()){ // assume that the connection is in the process of opening
41                this.conn.on('open', function(){
42                        this.load(params, reader, callback, scope, arg);
43                }, this, {single:true});
44                return;
45        };
46        if(this.fireEvent("beforeload", this, params, reader, callback, scope, arg) !== false){
47                        var clause = params.where || '';
48                        var args = params.args || [];
49                        var group = params.groupBy;
50                        var sort = params.sort;
51                        var dir = params.dir;
52
53                        if(group || sort){
54                                clause += ' ORDER BY ';
55                                if(group && group != sort){
56                                        clause += group + ' ASC, ';
57                                }
58                                clause += sort + ' ' + (dir || 'ASC');
59                        }
60
61                        var rs = this.table.selectBy(clause, args);
62                        this.onLoad({callback:callback, scope:scope, arg:arg, reader: reader}, rs);
63        }else{
64            callback.call(scope||this, null, arg, false);
65        }
66    },
67
68    onLoad : function(trans, rs, e, stmt){
69        if(rs === false){
70                this.fireEvent("loadexception", this, null, trans.arg, e);
71            trans.callback.call(trans.scope||window, null, trans.arg, false);
72            return;
73        }
74        var result = trans.reader.readRecords(rs);
75        this.fireEvent("load", this, rs, trans.arg);
76        trans.callback.call(trans.scope||window, result, trans.arg, true);
77    },
78
79    processData : function(o){
80        var fs = this.store.fields;
81        var r = {};
82        for(var key in o){
83                var f = fs.key(key), v = o[key];
84                        if(f){
85                                if(f.type == 'date'){
86                                        r[key] = v ? v.format(Ext.sql.Proxy.DATE_FORMAT,10) : '';
87                                }else if(f.type == 'boolean'){
88                                        r[key] = v ? 1 : 0;
89                                }else{
90                                        r[key] = v;
91                                }
92                        }
93                }
94                return r;
95    },
96
97    onUpdate : function(ds, record){
98        var changes = record.getChanges();
99        var kn = this.table.keyName;
100        this.table.updateBy(this.processData(changes), kn + ' = ?', [record.data[kn]]);
101        record.commit(true);
102    },
103
104    onAdd : function(ds, records, index){
105        for(var i = 0, len = records.length; i < len; i++){
106                this.table.insert(this.processData(records[i].data));
107        }
108    },
109
110    onRemove : function(ds, record, index){
111                var kn = this.table.keyName;
112        this.table.removeBy(kn + ' = ?', [record.data[kn]]);
113    }
114});
Note: See TracBrowser for help on using the repository browser.