UNPKG

1.88 kBJavaScriptView Raw
1/**
2 * joola.io
3 *
4 * Copyright Joola Smart Solutions, Ltd. <info@joo.la>
5 *
6 * Licensed under GNU General Public License 3.0 or later.
7 * Some rights reserved. See LICENSE, AUTHORS.
8 *
9 * @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
10 */
11
12var
13 uuid = require('node-uuid'),
14 mysql = require('./mysql'),
15 mssql = require('./mssql'),
16 pg = require('./postgres');
17
18var 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
40var exports = {};
41
42exports.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
59exports.executeQuery = function (query, callback) {
60 var db = createConnection(query);
61 //var connection = db.open(query);
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 //db.close(connection);
71};
72
73module.exports = exports;
\No newline at end of file