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