UNPKG

2.93 kBJavaScriptView Raw
1/*
2
3 ----------------------------------------------------------------------------
4 | qewd-ripple: QEWD-based Middle Tier for Ripple OSI |
5 | |
6 | Copyright (c) 2016-17 Ripple Foundation Community Interest Company |
7 | All rights reserved. |
8 | |
9 | http://rippleosi.org |
10 | Email: code.custodian@rippleosi.org |
11 | |
12 | Author: Rob Tweed, M/Gateway Developments Ltd |
13 | |
14 | Licensed under the Apache License, Version 2.0 (the "License"); |
15 | you may not use this file except in compliance with the License. |
16 | You may obtain a copy of the License at |
17 | |
18 | http://www.apache.org/licenses/LICENSE-2.0 |
19 | |
20 | Unless required by applicable law or agreed to in writing, software |
21 | distributed under the License is distributed on an "AS IS" BASIS, |
22 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
23 | See the License for the specific language governing permissions and |
24 | limitations under the License. |
25 ----------------------------------------------------------------------------
26
27 26 January 2017
28
29*/
30
31var mysql = require('mysql');
32
33module.exports = {
34
35 connect: function(callback) {
36 var con = mysql.createConnection({
37 host: "localhost",
38 user: "root",
39 password: "password",
40 database: 'poc_legacy'
41 });
42
43 con.connect(function(err){
44 if (err) {
45 callback({error: 'Unable to connect to MySQL: ' + err});
46 return;
47 }
48 console.log('Connected to MySQL');
49 });
50 return con;
51 },
52
53 disconnect: function(connection) {
54 console.log('Disconnected from MySQL');
55 connection.end();
56 },
57
58 query: function(query, callback) {
59 var q = this;
60 var connection = this.connect(callback);
61 console.log('about to run query ' + query);
62 connection.query(query,function(err, rows) {
63 console.log('query results: ' + JSON.stringify(rows));
64 if(err) {
65 q.disconnect(connection);
66 if (callback) callback({error: 'MySQL query error: ' + err});
67 return;
68 }
69 q.disconnect(connection);
70 if (callback) callback(rows);
71 });
72 }
73
74};