Rev 1945 | Rev 1950 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1945 | Rev 1947 | ||
|---|---|---|---|
| Line 42... | Line 42... | ||
| 42 | this->ExportFunction("MySqlExport_Connect", MySql::Export_Connect); |
42 | this->ExportFunction("MySqlExport_Connect", MySql::Export_Connect); |
| 43 | this->ExportFunction("MySqlExport_Close", MySql::Export_Close); |
43 | this->ExportFunction("MySqlExport_Close", MySql::Export_Close); |
| 44 | this->ExportFunction("MySqlExport_Query", MySql::Export_Query); |
44 | this->ExportFunction("MySqlExport_Query", MySql::Export_Query); |
| 45 | this->ExportFunction("MySqlExport_SelectDb", MySql::Export_SelectDb); |
45 | this->ExportFunction("MySqlExport_SelectDb", MySql::Export_SelectDb); |
| 46 | this->ExportFunction("MySqlExport_AffectedRows", MySql::Export_AffectedRows); |
46 | this->ExportFunction("MySqlExport_AffectedRows", MySql::Export_AffectedRows); |
| - | 47 | this->ExportFunction("MySqlExport_InsertId", MySql::Export_InsertId); |
|
| 47 | } |
48 | } |
| 48 | 49 | ||
| 49 | MySql::~MySql() |
50 | MySql::~MySql() |
| 50 | { |
51 | { |
| 51 | } |
52 | } |
| Line 56... | Line 57... | ||
| 56 | } |
57 | } |
| 57 | 58 | ||
| 58 | void MySql::CallOutput(unsigned int request_id, std::string output) |
59 | void MySql::CallOutput(unsigned int request_id, std::string output) |
| 59 | { |
60 | { |
| 60 | atom::log::Info(log_module_, output); |
61 | atom::log::Info(log_module_, output); |
| - | 62 | } |
|
| - | 63 | ||
| - | 64 | MySql::ResourceId MySql::GetFreeResourceId() |
|
| - | 65 | { |
|
| - | 66 | ResourceId resource_id = 1; |
|
| - | 67 | ||
| - | 68 | while (NULL != MySql::resources_[resource_id]) |
|
| - | 69 | { |
|
| - | 70 | resource_id++; |
|
| - | 71 | } |
|
| - | 72 | ||
| - | 73 | return resource_id; |
|
| 61 | } |
74 | } |
| 62 | 75 | ||
| 63 | Value MySql::Export_Connect(const v8::Arguments& args) |
76 | Value MySql::Export_Connect(const v8::Arguments& args) |
| 64 | { |
77 | { |
| 65 | ATOM_VM_PLUGIN_SCOPE; |
78 | ATOM_VM_PLUGIN_SCOPE; |
| 66 | 79 | ||
| 67 | MYSQL* resource = NULL; |
80 | MYSQL* resource = NULL; |
| - | 81 | ResourceId resource_id; |
|
| 68 | 82 | ||
| 69 | atom |
83 | //atom::log::Debug(log_module_, "%s called!", __FUNCTION__); |
| 70 | 84 | ||
| 71 | try |
85 | try |
| 72 | { |
86 | { |
| 73 | ATOM_VM_PLUGIN_NUM_PARAMS(3); |
87 | ATOM_VM_PLUGIN_NUM_PARAMS(3); |
| 74 | 88 | ||
| Line 80... | Line 94... | ||
| 80 | /* Initialize MySQL resource */ |
94 | /* Initialize MySQL resource */ |
| 81 | resource = mysql_init(NULL); |
95 | resource = mysql_init(NULL); |
| 82 | 96 | ||
| 83 | if (NULL == resource) |
97 | if (NULL == resource) |
| 84 | { |
98 | { |
| 85 | throw atom::exception::initialization_failed; |
99 | throw atom::exception::initialization_failed(); |
| 86 | } |
100 | } |
| 87 | 101 | ||
| 88 | 102 | ||
| 89 | /* Connect to database */ |
103 | /* Connect to database */ |
| 90 | if (mysql_real_connect(resource, *server, *username, *password, NULL, 0, NULL, 0) == NULL) |
104 | if (mysql_real_connect(resource, *server, *username, *password, NULL, 0, NULL, 0) == NULL) |
| 91 | { |
105 | { |
| 92 | atom::log::Error(log_module_, "Failed to connect to database %s, error code %d, error message \"%s\".", *server, mysql_errno(resource), mysql_error(resource)); |
106 | atom::log::Error(log_module_, "Failed to connect to database %s, error code %d, error message \"%s\".", *server, mysql_errno(resource), mysql_error(resource)); |
| 93 | throw atom::exception::connect_failed; |
107 | throw atom::exception::connect_failed(); |
| 94 | } |
108 | } |
| 95 | 109 | ||
| 96 | 110 | ||
| 97 | /* Add resource to list */ |
111 | /* Add resource to list */ |
| - | 112 | resource_id = MySql::GetFreeResourceId(); |
|
| 98 | MySql::resources_[ |
113 | MySql::resources_[resource_id] = resource; |
| 99 | 114 | ||
| 100 | 115 | ||
| 101 | return handle_scope.Close(v8::Uint32::New( |
116 | return handle_scope.Close(v8::Uint32::New(resource_id)); |
| 102 | } |
117 | } |
| 103 | catch (std::exception& exception) |
118 | catch (std::exception& exception) |
| 104 | { |
119 | { |
| 105 | if (NULL != resource) |
120 | if (NULL != resource) |
| 106 | { |
121 | { |
| Line 114... | Line 129... | ||
| 114 | } |
129 | } |
| 115 | } |
130 | } |
| 116 | 131 | ||
| 117 | Value MySql::Export_Close(const v8::Arguments& args) |
132 | Value MySql::Export_Close(const v8::Arguments& args) |
| 118 | { |
133 | { |
| 119 | ATOM_VM_PLUGIN_SCOPE; |
134 | ATOM_VM_PLUGIN_SCOPE; |
| 120 | 135 | ||
| 121 | atom |
136 | //atom::log::Debug(log_module_, "%s called!", __FUNCTION__); |
| 122 | 137 | ||
| 123 | try |
138 | try |
| 124 | { |
139 | { |
| 125 | ATOM_VM_PLUGIN_NUM_PARAMS(1); |
140 | ATOM_VM_PLUGIN_NUM_PARAMS(1); |
| 126 | 141 | ||
| 127 | 142 | ||
| 128 | /* Check that the resource exist */ |
143 | /* Check that the resource exist */ |
| 129 | if (NULL == MySql::resources_[( |
144 | if (NULL == MySql::resources_[(ResourceId)args[0]->Uint32Value()]) |
| 130 | { |
145 | { |
| 131 | throw atom::exception::missing_resource; |
146 | throw atom::exception::missing_resource(); |
| 132 | } |
147 | } |
| 133 | 148 | ||
| 134 | 149 | ||
| 135 | /* Close connection */ |
150 | /* Close connection */ |
| 136 | mysql_close(MySql::resources_[( |
151 | mysql_close(MySql::resources_[(ResourceId)args[0]->Uint32Value()]); |
| 137 | MySql::resources_.erase(( |
152 | MySql::resources_.erase((ResourceId)args[0]->Uint32Value()); |
| 138 | 153 | ||
| 139 | 154 | ||
| 140 | return handle_scope.Close(v8::Boolean::New(true)); |
155 | return handle_scope.Close(v8::Boolean::New(true)); |
| 141 | } |
156 | } |
| 142 | catch (std::exception& exception) |
157 | catch (std::exception& exception) |
| 143 | { |
158 | { |
| 144 | atom::log::Exception(log_module_, exception); |
159 | atom::log::Exception(log_module_, exception); |
| 145 | 160 | ||
| 146 | return handle_scope.Close(v8::Boolean::New(false)); |
161 | return handle_scope.Close(v8::Boolean::New(false)); |
| 147 | } |
162 | } |
| 148 | } |
163 | } |
| 149 | 164 | ||
| 150 | Value MySql::Export_Query(const v8::Arguments& args) |
165 | Value MySql::Export_Query(const v8::Arguments& args) |
| 151 | { |
166 | { |
| 152 | ATOM_VM_PLUGIN_SCOPE; |
167 | ATOM_VM_PLUGIN_SCOPE; |
| 153 | 168 | ||
| 154 | MYSQL_RES* result = NULL; |
169 | MYSQL_RES* result = NULL; |
| 155 | 170 | ||
| 156 | atom |
171 | //atom::log::Debug(log_module_, "%s called!", __FUNCTION__); |
| 157 | 172 | ||
| 158 | try |
173 | try |
| 159 | { |
174 | { |
| 160 | ArgumentListPointer result_array = ArgumentListPointer(new ArgumentList); |
175 | ArgumentListPointer result_array = ArgumentListPointer(new ArgumentList); |
| 161 | 176 | ||
| 162 | ATOM_VM_PLUGIN_NUM_PARAMS(2); |
177 | ATOM_VM_PLUGIN_NUM_PARAMS(2); |
| 163 | 178 | ||
| 164 | v8::String::AsciiValue query(args[1]); |
179 | v8::String::AsciiValue query(args[1]); |
| 165 | 180 | ||
| 166 | 181 | ||
| 167 | /* Check that the resource exist */ |
182 | /* Check that the resource exist */ |
| 168 | if (NULL == MySql::resources_[( |
183 | if (NULL == MySql::resources_[(ResourceId)args[0]->Uint32Value()]) |
| - | 184 | { |
|
| - | 185 | throw atom::exception::missing_resource(); |
|
| - | 186 | } |
|
| - | 187 | ||
| - | 188 | ||
| - | 189 | /* Execute the query */ |
|
| - | 190 | if (0 != mysql_query(MySql::resources_[(ResourceId)args[0]->Uint32Value()], *query)) |
|
| 169 | { |
191 | { |
| 170 | throw atom::exception::missing_resource; |
- | |
| 171 | } |
- | |
| 172 | - | ||
| 173 | - | ||
| 174 | /* Execute the query */ |
- | |
| 175 | if (0 != mysql_query(MySql::resources_[(uintptr_t)args[0]->Uint32Value()], *query)) |
- | |
| 176 | { |
- | |
| 177 | throw atom::exception::action_failed; |
192 | throw atom::exception::action_failed(); |
| 178 | } |
193 | } |
| 179 | 194 | ||
| 180 | 195 | ||
| 181 | /* Get the result */ |
196 | /* Get the result */ |
| 182 | result = mysql_store_result(MySql::resources_[( |
197 | result = mysql_store_result(MySql::resources_[(ResourceId)args[0]->Uint32Value()]); |
| 183 | 198 | ||
| 184 | if (NULL == result) |
199 | if (NULL == result) |
| 185 | { |
200 | { |
| 186 | if (mysql_field_count(MySql::resources_[( |
201 | if (mysql_field_count(MySql::resources_[(ResourceId)args[0]->Uint32Value()]) > 0) |
| 187 | { |
202 | { |
| 188 | throw atom::exception::action_failed; |
203 | throw atom::exception::action_failed(); |
| 189 | } |
204 | } |
| 190 | } |
205 | } |
| 191 | else |
206 | else |
| 192 | { |
207 | { |
| 193 | 208 | ||
| 194 | MYSQL_ROW row; |
209 | MYSQL_ROW row; |
| 195 | uint32_t number_of_fields = mysql_num_fields(result); |
210 | uint32_t number_of_fields = mysql_num_fields(result); |
| 196 | MYSQL_FIELD* fields = mysql_fetch_fields(result); |
211 | MYSQL_FIELD* fields = mysql_fetch_fields(result); |
| 197 | uint32_t row_index = 0; |
212 | uint32_t row_index = 0; |
| 198 | 213 | ||
| 199 | v8::Local<v8::Array> result_array = v8::Array::New(); |
214 | v8::Local<v8::Array> result_array = v8::Array::New(); |
| 200 | 215 | ||
| 201 | 216 | ||
| 202 | while (NULL != (row = mysql_fetch_row(result))) |
217 | while (NULL != (row = mysql_fetch_row(result))) |
| 203 | { |
218 | { |
| 204 | v8::Local<v8::Object> vars = v8::Object::New(); |
219 | v8::Local<v8::Object> vars = v8::Object::New(); |
| 205 | 220 | ||
| 206 | 221 | ||
| Line 211... | Line 226... | ||
| 211 | // atom::log::Info(log_module_, "%s = %s\n", fields[index].name, row[index]); |
226 | // atom::log::Info(log_module_, "%s = %s\n", fields[index].name, row[index]); |
| 212 | } |
227 | } |
| 213 | 228 | ||
| 214 | result_array->Set(row_index, vars); |
229 | result_array->Set(row_index, vars); |
| 215 | row_index++; |
230 | row_index++; |
| 216 | } |
231 | } |
| 217 | 232 | ||
| 218 | mysql_free_result(result); |
233 | mysql_free_result(result); |
| 219 | result = NULL; |
234 | result = NULL; |
| 220 | 235 | ||
| 221 | return handle_scope.Close(v8::Handle<v8::Value>(result_array)); |
236 | return handle_scope.Close(v8::Handle<v8::Value>(result_array)); |
| 222 | } |
237 | } |
| 223 | 238 | ||
| 224 | return handle_scope.Close(v8::Boolean::New(true)); |
239 | return handle_scope.Close(v8::Boolean::New(true)); |
| 225 | } |
240 | } |
| 226 | catch (std::exception& exception) |
241 | catch (std::exception& exception) |
| 227 | { |
242 | { |
| 228 | if (NULL != result) |
243 | if (NULL != result) |
| 229 | { |
244 | { |
| 230 | mysql_free_result(result); |
245 | mysql_free_result(result); |
| 231 | result = NULL; |
246 | result = NULL; |
| 232 | } |
247 | } |
| 233 | 248 | ||
| 234 | atom::log::Exception(log_module_, exception); |
249 | atom::log::Exception(log_module_, exception); |
| 235 | 250 | ||
| 236 | return handle_scope.Close(v8::Boolean::New(false)); |
251 | return handle_scope.Close(v8::Boolean::New(false)); |
| 237 | } |
252 | } |
| 238 | } |
253 | } |
| 239 | 254 | ||
| 240 | Value MySql::Export_SelectDb(const v8::Arguments& args) |
255 | Value MySql::Export_SelectDb(const v8::Arguments& args) |
| 241 | { |
256 | { |
| 242 | ATOM_VM_PLUGIN_SCOPE; |
257 | ATOM_VM_PLUGIN_SCOPE; |
| 243 | 258 | ||
| 244 | atom |
259 | //atom::log::Debug(log_module_, "%s called!", __FUNCTION__); |
| 245 | 260 | ||
| 246 | try |
261 | try |
| 247 | { |
262 | { |
| 248 | ATOM_VM_PLUGIN_NUM_PARAMS(2); |
263 | ATOM_VM_PLUGIN_NUM_PARAMS(2); |
| 249 | 264 | ||
| 250 | v8::String::AsciiValue database(args[1]); |
265 | v8::String::AsciiValue database(args[1]); |
| 251 | 266 | ||
| 252 | 267 | ||
| 253 | /* Check that the resource exist */ |
268 | /* Check that the resource exist */ |
| 254 | if (NULL == MySql::resources_[( |
269 | if (NULL == MySql::resources_[(ResourceId)args[0]->Uint32Value()]) |
| 255 | { |
270 | { |
| 256 | throw atom::exception::missing_resource; |
271 | throw atom::exception::missing_resource(); |
| 257 | } |
272 | } |
| 258 | 273 | ||
| 259 | 274 | ||
| 260 | /* Select database */ |
275 | /* Select database */ |
| 261 | if (0 != mysql_select_db(MySql::resources_[( |
276 | if (0 != mysql_select_db(MySql::resources_[(ResourceId)args[0]->Uint32Value()], *database)) |
| 262 | { |
277 | { |
| 263 | throw atom::exception::action_failed; |
278 | throw atom::exception::action_failed(); |
| 264 | } |
279 | } |
| 265 | 280 | ||
| 266 | 281 | ||
| 267 | return handle_scope.Close(v8::Boolean::New(true)); |
282 | return handle_scope.Close(v8::Boolean::New(true)); |
| 268 | } |
283 | } |
| 269 | catch (std::exception& exception) |
284 | catch (std::exception& exception) |
| 270 | { |
285 | { |
| 271 | atom::log::Exception(log_module_, exception); |
286 | atom::log::Exception(log_module_, exception); |
| 272 | 287 | ||
| 273 | return handle_scope.Close(v8::Boolean::New(false)); |
288 | return handle_scope.Close(v8::Boolean::New(false)); |
| 274 | } |
289 | } |
| 275 | } |
290 | } |
| 276 | 291 | ||
| 277 | Value MySql::Export_AffectedRows(const v8::Arguments& args) |
292 | Value MySql::Export_AffectedRows(const v8::Arguments& args) |
| - | 293 | { |
|
| - | 294 | ATOM_VM_PLUGIN_SCOPE; |
|
| - | 295 | ||
| - | 296 | //atom::log::Debug(log_module_, "%s called!", __FUNCTION__); |
|
| - | 297 | ||
| - | 298 | try |
|
| - | 299 | { |
|
| - | 300 | ATOM_VM_PLUGIN_NUM_PARAMS(1); |
|
| - | 301 | ||
| - | 302 | ||
| - | 303 | /* Check that the resource exist */ |
|
| - | 304 | if (NULL == MySql::resources_[(ResourceId)args[0]->Uint32Value()]) |
|
| - | 305 | { |
|
| - | 306 | throw atom::exception::missing_resource(); |
|
| - | 307 | } |
|
| - | 308 | ||
| - | 309 | ||
| - | 310 | /* Select database */ |
|
| - | 311 | uint32_t number_of_rows = mysql_affected_rows(MySql::resources_[(ResourceId)args[0]->Uint32Value()]); |
|
| - | 312 | ||
| - | 313 | ||
| - | 314 | return handle_scope.Close(v8::Uint32::New(number_of_rows)); |
|
| - | 315 | } |
|
| - | 316 | catch (std::exception& exception) |
|
| - | 317 | { |
|
| - | 318 | atom::log::Exception(log_module_, exception); |
|
| - | 319 | ||
| - | 320 | return handle_scope.Close(v8::Boolean::New(false)); |
|
| - | 321 | } |
|
| - | 322 | } |
|
| - | 323 | ||
| - | 324 | Value MySql::Export_InsertId(const v8::Arguments& args) |
|
| 278 | { |
325 | { |
| 279 | ATOM_VM_PLUGIN_SCOPE; |
326 | ATOM_VM_PLUGIN_SCOPE; |
| 280 | 327 | ||
| 281 | atom |
328 | //atom::log::Debug(log_module_, "%s called!", __FUNCTION__); |
| 282 | 329 | ||
| 283 | try |
330 | try |
| 284 | { |
331 | { |
| 285 | ATOM_VM_PLUGIN_NUM_PARAMS(1); |
332 | ATOM_VM_PLUGIN_NUM_PARAMS(1); |
| 286 | 333 | ||
| 287 | 334 | ||
| 288 | /* Check that the resource exist */ |
335 | /* Check that the resource exist */ |
| 289 | if (NULL == MySql::resources_[( |
336 | if (NULL == MySql::resources_[(ResourceId)args[0]->Uint32Value()]) |
| 290 | { |
337 | { |
| 291 | throw atom::exception::missing_resource; |
338 | throw atom::exception::missing_resource(); |
| 292 | } |
339 | } |
| 293 | 340 | ||
| 294 | 341 | ||
| 295 | /* Select database */ |
342 | /* Select database */ |
| 296 | uint32_t |
343 | uint32_t insert_id = mysql_insert_id(MySql::resources_[(ResourceId)args[0]->Uint32Value()]); |
| 297 | 344 | ||
| 298 | 345 | ||
| 299 | return handle_scope.Close(v8::Uint32::New( |
346 | return handle_scope.Close(v8::Uint32::New(insert_id)); |
| 300 | } |
347 | } |
| 301 | catch (std::exception& exception) |
348 | catch (std::exception& exception) |
| 302 | { |
349 | { |
| 303 | atom::log::Exception(log_module_, exception); |
350 | atom::log::Exception(log_module_, exception); |
| 304 | 351 | ||
| 305 | return handle_scope.Close(v8::Boolean::New(false)); |
352 | return handle_scope.Close(v8::Boolean::New(false)); |
| 306 | } |
353 | } |
| 307 | } |
354 | } |
| 308 | - | ||
| 309 | 355 | ||
| 310 | }; // namespace plugin |
356 | }; // namespace plugin |
| 311 | }; // namespace vm |
357 | }; // namespace vm |
| 312 | }; // namespace atom |
358 | }; // namespace atom |