1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var
|
13 | uuid = require('node-uuid'),
|
14 | mysql = require('./mysql'),
|
15 | mssql = require('./mssql'),
|
16 | pg = require('./postgres');
|
17 |
|
18 | var createConnection = function (query) {
|
19 | var connection = null;
|
20 | switch (query.datasource.type) {
|
21 | case 'mysql':
|
22 | joola.logger.debug('open connection to mysql ' + query.datasource.id);
|
23 | connection = mysql;
|
24 | break;
|
25 | case 'postgres':
|
26 | joola.logger.debug('open connection to postgres ' + query.datasource.id);
|
27 | connection = pg;
|
28 | break;
|
29 | case 'mssql':
|
30 | joola.logger.debug('open connection to mssql ' + query.datasource.id);
|
31 | connection = mssql;
|
32 | break;
|
33 | default:
|
34 | break;
|
35 | }
|
36 |
|
37 | return connection;
|
38 | };
|
39 |
|
40 | var exports = {};
|
41 |
|
42 | exports.createQuery = function () {
|
43 | var query = {
|
44 | _id: null,
|
45 | id: function () {
|
46 | if (!this._id)
|
47 | this.id = uuid.v4();
|
48 | return this.id;
|
49 | }(),
|
50 | datasource: null,
|
51 | options: null,
|
52 | type: "read",
|
53 | sql: "",
|
54 | limit: null
|
55 | };
|
56 | return ce.extend(query, {});
|
57 | };
|
58 |
|
59 | exports.executeQuery = function (query, callback) {
|
60 | var db = createConnection(query);
|
61 |
|
62 |
|
63 | joola.logger.silly('Execute query... [' + query.datasource.id + ']: ' + query.sql);
|
64 | db.executeQuery(query, function (err, rows, fields) {
|
65 | if (err)
|
66 | return callback(null, null, null, err);
|
67 | joola.logger.silly('Result contains ' + rows.rows.length + ' rows and ' + rows.fields.length + ' fields.');
|
68 | return callback(query, rows, fields, err);
|
69 | });
|
70 |
|
71 | };
|
72 |
|
73 | module.exports = exports; |
\ | No newline at end of file |