make sure the README will appear on github
[extjs.git] / air / src / sql / Table.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 Ext.sql.Table = function(conn, name, keyName){\r
10         this.conn = conn;\r
11         this.name = name;\r
12         this.keyName = keyName;\r
13 };\r
14 \r
15 Ext.sql.Table.prototype = {\r
16         update : function(o){\r
17                 var clause = this.keyName + " = ?";\r
18                 return this.updateBy(o, clause, [o[this.keyName]]);\r
19         },\r
20 \r
21         updateBy : function(o, clause, args){\r
22                 var sql = "UPDATE " + this.name + " set ";\r
23                 var fs = [], a = [];\r
24                 for(var key in o){\r
25                         if(o.hasOwnProperty(key)){\r
26                                 fs[fs.length] = key + ' = ?';\r
27                                 a[a.length] = o[key];\r
28                         }\r
29                 }\r
30                 for(var key in args){\r
31                         if(args.hasOwnProperty(key)){\r
32                                 a[a.length] = args[key];\r
33                         }\r
34                 }\r
35                 sql = [sql, fs.join(','), ' WHERE ', clause].join('');\r
36                 return this.conn.execBy(sql, a);\r
37         },\r
38 \r
39         insert : function(o){\r
40                 var sql = "INSERT into " + this.name + " ";\r
41                 var fs = [], vs = [], a = [];\r
42                 for(var key in o){\r
43                         if(o.hasOwnProperty(key)){\r
44                                 fs[fs.length] = key;\r
45                                 vs[vs.length] = '?';\r
46                                 a[a.length] = o[key];\r
47                         }\r
48                 }\r
49                 sql = [sql, '(', fs.join(','), ') VALUES (', vs.join(','), ')'].join('');\r
50         return this.conn.execBy(sql, a);\r
51     },\r
52 \r
53         lookup : function(id){\r
54                 return this.selectBy('where ' + this.keyName + " = ?", [id])[0] || null;\r
55         },\r
56 \r
57         exists : function(id){\r
58                 return !!this.lookup(id);\r
59         },\r
60 \r
61         save : function(o){\r
62                 if(this.exists(o[this.keyName])){\r
63             this.update(o);\r
64         }else{\r
65             this.insert(o);\r
66         }\r
67         },\r
68 \r
69         select : function(clause){\r
70                 return this.selectBy(clause, null);\r
71         },\r
72 \r
73         selectBy : function(clause, args){\r
74                 var sql = "select * from " + this.name;\r
75                 if(clause){\r
76                         sql += ' ' + clause;\r
77                 }\r
78                 args = args || {};\r
79                 return this.conn.queryBy(sql, args);\r
80         },\r
81 \r
82         remove : function(clause){\r
83                 this.deleteBy(clause, null);\r
84         },\r
85 \r
86         removeBy : function(clause, args){\r
87                 var sql = "delete from " + this.name;\r
88                 if(clause){\r
89                         sql += ' where ' + clause;\r
90                 }\r
91                 args = args || {};\r
92                 this.conn.execBy(sql, args);\r
93         }\r
94 };