provide installation instructions
[extjs.git] / air / src / sql / Proxy.js
1 /*\r
2  * Ext JS Library 0.30\r
3  * Copyright(c) 2006-2009, Ext JS, LLC.\r
4  * licensing@extjs.com\r
5  * \r
6  * http://extjs.com/license\r
7  */\r
8 \r
9 /**\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
13  * database.\r
14  *\r
15  * @constructor\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
22  */\r
23 Ext.sql.Proxy = function(conn, table, keyName, store, readonly){\r
24     Ext.sql.Proxy.superclass.constructor.call(this);\r
25     this.conn = conn;\r
26     this.table = this.conn.getTable(table, keyName);\r
27     this.store = store;\r
28 \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
33         }\r
34 };\r
35 \r
36 Ext.sql.Proxy.DATE_FORMAT = 'Y-m-d H:i:s';\r
37 \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
44                 return;\r
45         };\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
52 \r
53                         if(group || sort){\r
54                                 clause += ' ORDER BY ';\r
55                                 if(group && group != sort){\r
56                                         clause += group + ' ASC, ';\r
57                                 }\r
58                                 clause += sort + ' ' + (dir || 'ASC');\r
59                         }\r
60 \r
61                         var rs = this.table.selectBy(clause, args);\r
62                         this.onLoad({callback:callback, scope:scope, arg:arg, reader: reader}, rs);\r
63         }else{\r
64             callback.call(scope||this, null, arg, false);\r
65         }\r
66     },\r
67 \r
68     onLoad : function(trans, rs, e, stmt){\r
69         if(rs === false){\r
70                 this.fireEvent("loadexception", this, null, trans.arg, e);\r
71             trans.callback.call(trans.scope||window, null, trans.arg, false);\r
72             return;\r
73         }\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
77     },\r
78 \r
79     processData : function(o){\r
80         var fs = this.store.fields;\r
81         var r = {};\r
82         for(var key in o){\r
83                 var f = fs.key(key), v = o[key];\r
84                         if(f){\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
88                                         r[key] = v ? 1 : 0;\r
89                                 }else{\r
90                                         r[key] = v;\r
91                                 }\r
92                         }\r
93                 }\r
94                 return r;\r
95     },\r
96 \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
102     },\r
103 \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
107         }\r
108     },\r
109 \r
110     onRemove : function(ds, record, index){\r
111                 var kn = this.table.keyName;\r
112         this.table.removeBy(kn + ' = ?', [record.data[kn]]);\r
113     }\r
114 });