-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecute_sql.js
More file actions
119 lines (96 loc) · 2.21 KB
/
execute_sql.js
File metadata and controls
119 lines (96 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
module.exports.executer = executeSQL;
var mysql = require('mysql');
var transformer = require('./transform').transformer;
function executeSQL(hostname, port, db, username, password, statementsArray, callback){
var connection = mysql.createConnection({
host : hostname,
database: db,
user : username,
password : password,
port: port,
multipleStatements: true
});
var statements = statementsArray.join(";") + ";";
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
terminate(connection, err, null, callback);
}
console.log('connected as id ' + connection.threadId);
connection.beginTransaction(function(err) {
connection.query(statements, function(err, result) {
if (err) {
connection.rollback(function() {
terminate(connection, err, null, callback);
});
}
else{
connection.commit(function(err) {
if (err) {
connection.rollback(function() {
terminate(connection, err);
});
}
console.log('success!');
terminate(connection, null, result, callback);
});
}
});
});
});
}
function terminate(connection, err, result, callback){
connection.end(function(err) {
// The connection is terminated now
if (err){
console.log(err);
}
callback(err, result);
});
}
// testExecuteMySQL();
function testExecuteMySQL(){
var r = transformer([],
[
{
"name": "R",
"fields": {
"A": {
"type": "string",
"required": true
},
"B": {
"type": "float"
}
}
},
{
"name": "S",
"fields": {
"D": {
"type": "boolean"
},
"E": {
"type": "float"
}
}
},
{
"name": "U",
"fields": {
"F": {
"type": "binary"
},
"D": {
"type": "text"
}
}
},
],
0);
console.log(r);
// executeSQL("bk-prod-us1.cd2junihlkms.us-east-1.rds.amazonaws.com", 3306, "backandtestsqlxzhsfvrb", "lmlmez3renpyl4j", "S12nZ1bx5W3MncYAiciy6s", r.alter, function(err, result){
// console.log(err);
// console.log(result);
// });
}