hedera-web/js/db/connection.js

124 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-09-26 09:28:47 +00:00
/**
2016-09-24 14:32:31 +00:00
* Simulates a connection to a database by making asynchronous requests to a
* remote REST service that returns the results in JSON format.
* Using this class can perform any operation that can be done with a database,
* like open/close a connection or selecion/updating queries.
2016-09-24 14:32:31 +00:00
*
* Warning! You should set a well defined dababase level privileges to use this
* class or you could have a serious security hole in you application becasuse
* the user can send any statement to the server. For example: DROP DATABASE
**/
2020-05-03 20:35:24 +00:00
var Connection = new Class();
2016-09-26 09:28:47 +00:00
module.exports = Connection;
var Flag =
{
NOT_NULL : 1
,PRI_KEY : 2
,AI : 512 | 2 | 1
};
var Type =
{
BOOLEAN : 1
,INTEGER : 3
,DOUBLE : 4
,STRING : 5
,DATE : 8
,DATE_TIME : 9
};
Connection.extend
({
2016-09-26 09:28:47 +00:00
Flag: Flag
,Type: Type
});
2016-09-26 09:28:47 +00:00
Connection.implement
({
2016-09-23 22:47:34 +00:00
Extends: Vn.JsonConnection
/**
* Runs a SQL query on the database.
*
* @param {String} sql The SQL statement
* @param {Function} callback The function to call when operation is done
**/
2020-05-03 20:35:24 +00:00
,execSql: function(sql, callback) {
this.send('core/query', {'sql': sql},
this._onExec.bind(this, callback));
}
/**
* Runs a stmt on the database.
*
* @param {Sql.Stmt} stmt The statement
* @param {Function} callback The function to call when operation is done
* @param {Sql.Batch} batch The batch used to set the parameters
**/
2020-05-03 20:35:24 +00:00
,execStmt: function(stmt, callback, batch) {
this.execSql(stmt.render(batch), callback);
}
/**
* Runs a query on the database.
*
* @param {String} query The SQL statement
* @param {Function} callback The function to call when operation is done
* @param {Sql.Batch} batch The batch used to set the parameters
**/
2020-05-03 20:35:24 +00:00
,execQuery: function(query, callback, batch) {
this.execStmt(new Sql.String({query: query}), callback, batch);
}
/*
* Parses a value to date.
*/
2020-05-03 20:35:24 +00:00
,valueToDate: function(value) {
return new Date(value);
}
/*
* Called when a query is executed.
*/
2020-05-03 20:35:24 +00:00
,_onExec: function(callback, json, error) {
if (json)
try {
if (json && json instanceof Array)
for (var i = 0; i < json.length; i++)
2020-05-03 20:35:24 +00:00
if (json[i] !== true) {
var data = json[i].data;
var columns = json[i].columns;
2020-05-03 20:35:24 +00:00
for (var j = 0; j < columns.length; j++) {
var castFunc = null;
2020-05-03 20:35:24 +00:00
switch (columns[j].type) {
2016-09-26 09:28:47 +00:00
case Type.DATE:
case Type.DATE_TIME:
case Type.TIMESTAMP:
castFunc = this.valueToDate;
break;
}
2020-05-03 20:35:24 +00:00
if (castFunc !== null) {
if (columns[j].def != null)
2020-05-03 20:35:24 +00:00
columns[j].def = castFunc(columns[j].def);
for (var k = 0; k < data.length; k++)
if (data[k][j] != null)
2020-05-03 20:35:24 +00:00
data[k][j] = castFunc(data[k][j]);
}
}
}
2020-05-03 20:35:24 +00:00
} catch (e) {
error = e;
}
if (callback)
2020-05-03 20:35:24 +00:00
callback(new Db.ResultSet(json, error));
}
});