This repository was archived by the owner on May 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathdblib.js
82 lines (72 loc) · 2.02 KB
/
dblib.js
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
/*
* Misc database library functions
*/
/*
* Execute an SQL statement. This can be both DML (queries) or DDL (create/alter)
* @param {String} statement : The statement to run
* @param {Boolean} throwException : If true, throw exception on error. Default:false
* @returns {array} rows:The rows, if any returned by the query
*/
Devwik.SQL.execStatement = function(statement, transaction) {
var future = new Future();
if(transaction) {
console.log('tranaction');
if(transaction.cancelled) {
console.log('tranaction cancelled');
//not doing anything in this transaction
return([]);
}
}
query = Devwik.SQL.connection.query(statement, function(err, result) {
if (err) {
if(transaction) {
transaction.cancelled = true;
}
console.log(err);
console.log(err.stack);
}
future.ret(result);
});
return(future.wait());
};
/*
* Escape an SQL statement to try and catch SQL injections
*/
Devwik.SQL.escape = function(statement) {
statement = statement.toString();//For consistency let's convert to string
statement = Devwik.SQL.connection.escape(statement).toString();
statement = statement.substring(1, statement.length-1);
return(statement);
};
/*
* Wrap a function in an SQL Transaction
* @param {Function} func: the function that performs the SQL operations
* @param {Function} errFunc: optional function to call on error
*
*/
Devwik.SQL.Transaction = function(){
var connection = Devwik.SQL.connection;
if(!connection) {
console.log ("No database connection");
return null;
}
connection.query('START TRANSACTION');
return(this);
};
Devwik.SQL.Transaction.prototype.end = function() {
var self = this;
var connection = Devwik.SQL.connection;
if(self.cancelled) {
console.log('rollback');
connection.query('ROLLBACK');
} else {
console.log('commit');
connection.query('COMMIT');
}
};
Devwik.SQL.Transaction.prototype.commit = function() {
Devwik.SQL.connection.query('COMMIT');
};
Devwik.SQL.Transaction.prototype.rollback = function() {
Devwik.SQL.connection.query('ROLLBACK');
};