annotate chrome/content/database.js @ 6:ac5648afee47 default tip

change hot key from ctrl to alt.
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Sat, 23 May 2009 11:56:41 +0900
parents c47ec96326ad
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
1 // Database
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
2 function Database(name, dir) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
3 name += '.sqlite';
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
4
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
5 var file = Database.getService('@mozilla.org/file/directory_service;1', 'nsIProperties')
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
6 .get('ProfD', Components.interfaces.nsIFile);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
7
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
8 if (dir) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
9 file.append(dir);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
10 if (!file.exists()) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
11 file.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
12 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
13 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
14
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
15 file.append(name);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
16
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
17 this.connection = Database.getService('@mozilla.org/storage/service;1', 'mozIStorageService').openDatabase(file);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
18 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
19
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
20 Database.getService = function(cls, interface) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
21 return Components.classes[cls].getService(Components.interfaces[interface]);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
22 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
23
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
24 Database.bindParams = function(wrapper, params) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
25
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
26 if (params == null) return;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
27
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
28 // Hash
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
29 if (typeof(params) == 'object' && params.length == null) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
30 var paramNames = this.getParamNames(wrapper);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
31 for each(var name in paramNames) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
32 var param = params[name];
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
33 if (typeof(param)=='undefined') continue;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
34
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
35 if (param instanceof Date){
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
36 wrapper.params[name] = param.getTime();
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
37 continue;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
38 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
39
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
40 wrapper.params[name] = param;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
41 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
42 return;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
43 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
44
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
45 // Array
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
46 if (typeof(params) == 'string' || params.length == null) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
47 params = [].concat(params);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
48 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
49
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
50 var statement = wrapper.statement;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
51 for (var i = 0, len = statement.parameterCount; i < len; i++) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
52 statement.bindUTF8StringParameter(i, params[i]);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
53 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
54 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
55
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
56 Database.getParamNames = function(wrapper) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
57 var paramNames = [];
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
58 var statement = wrapper.statement;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
59 for (var i=0, len=statement.parameterCount ; i<len ; i++) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
60 paramNames.push(statement.getParameterName(i).substr(1));
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
61 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
62 return paramNames;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
63 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
64
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
65 Database.getColumnNames = function(wrapper) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
66 var columnNames=[];
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
67 statement = wrapper.statement;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
68 for ( var i = 0, len = statement.columnCount; i < len; i++) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
69 columnNames.push(statement.getColumnName(i));
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
70 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
71 return columnNames;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
72 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
73
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
74 Database.getRow = function(row, columnNames){
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
75 var result = {};
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
76 for each(var name in columnNames) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
77 result[name] = row[name];
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
78 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
79 return result;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
80 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
81
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
82 Database.DatabaseException = function(db, exception){
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
83 this.code = db.connection.lastError;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
84 this.message = 'errorCode:' + this.code + '; ' + db.connection.lastErrorString;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
85 this.original = exception;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
86 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
87
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
88
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
89 Database.prototype = {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
90 createStatement: function(sql) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
91
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
92 try {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
93 var statement = this.connection.createStatement(sql);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
94 var wrapper = Components.classes["@mozilla.org/storage/statement-wrapper;1"]
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
95 .createInstance(Components.interfaces.mozIStorageStatementWrapper);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
96
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
97 wrapper.initialize(statement);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
98 return wrapper;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
99 } catch(e) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
100 this.throwException(e);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
101 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
102 },
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
103
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
104 execute: function(sql, params, handler) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
105 if (typeof(handler) == 'function') {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
106 var temp = {};
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
107 temp.process = handler;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
108 handler = temp;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
109 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
110
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
111 var statement = sql.initialize ? sql : this.createStatement(sql);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
112 try{
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
113 Database.bindParams(statement, params);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
114
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
115 if (!handler) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
116 statement.execute();
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
117 return;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
118 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
119
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
120 var columnNames;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
121 while (statement.step()) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
122 if (!columnNames)
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
123 columnNames = Database.getColumnNames(statement);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
124
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
125 handler.process(statement.row, columnNames);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
126 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
127
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
128 return statement;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
129
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
130 } catch(e if e==StopIteration) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
131 } catch(e) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
132 this.throwException(e);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
133 } finally {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
134 if (statement)
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
135 statement.reset();
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
136 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
137 },
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
138
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
139 throwException: function(exception){
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
140 if (this.connection.lastError != 0) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
141 throw new Database.DatabaseException(this, exception);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
142 } else {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
143 throw exception;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
144 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
145 },
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
146
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
147 transaction: function(handler) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
148 var connection = this.connection;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
149 var error = false;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
150 connection.beginTransaction();
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
151 try {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
152 handler();
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
153 connection.commitTransaction();
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
154 } catch(e) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
155 error = true;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
156 this.throwException(e);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
157 } finally {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
158 if(error)
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
159 connection.rollbackTransaction();
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
160 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
161 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
162 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
163
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
164 // ResultArrayHandler
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
165 function ResultArrayHandler(database, sql) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
166 this.database = database;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
167 this.statement = this.database.createStatement(sql);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
168 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
169
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
170 ResultArrayHandler.prototype = {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
171 execute: function(params) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
172 this.result = [];
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
173 this.database.execute(this.statement, params, this);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
174 return this.result;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
175 },
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
176
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
177 createRowResult: function(row, columnNames) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
178 var result = {};
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
179 for (var i = 0, len = columnNames.length; i < len; i++) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
180 result[columnNames[i]] = row[columnNames[i]];
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
181 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
182 return result;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
183 },
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
184
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
185 process: function(row, columnNames) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
186 this.result.push(Database.getRow(row, columnNames));
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
187 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
188 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
189
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
190 // UpdateHandler
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
191 function UpdateHandler(database, sql) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
192 this.database = database;
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
193 this.statement = this.database.createStatement(sql);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
194 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
195
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
196 UpdateHandler.prototype = {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
197 execute: function(params) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
198 if (params && params.constructor == Array) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
199 for (var i = 0, len = params.length; i < len; i++) {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
200 this.database.execute(this.statement, params[i], null);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
201 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
202 } else {
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
203 this.database.execute(this.statement, params, null);
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
204 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
205 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
206 }
c47ec96326ad initial import
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
diff changeset
207