2016-09-26 09:28:47 +00:00
|
|
|
|
2015-01-23 13:09:30 +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.
|
2015-01-23 13:09:30 +00:00
|
|
|
* 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
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2016-09-26 09:28:47 +00:00
|
|
|
var Connection = new Class ();
|
|
|
|
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
|
2015-01-23 13:09:30 +00:00
|
|
|
({
|
2016-09-26 09:28:47 +00:00
|
|
|
Flag: Flag
|
|
|
|
,Type: Type
|
2015-01-23 13:09:30 +00:00
|
|
|
});
|
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
Connection.implement
|
2015-01-23 13:09:30 +00:00
|
|
|
({
|
2016-09-23 22:47:34 +00:00
|
|
|
Extends: Vn.JsonConnection
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs a SQL query on the database.
|
|
|
|
*
|
2017-04-21 10:53:15 +00:00
|
|
|
* @param {string} sql The SQL statement
|
|
|
|
* @param {function} callback The function to call when operation is done
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
,execSql: function (sql, callback)
|
|
|
|
{
|
2016-09-23 22:47:34 +00:00
|
|
|
this.send ('core/query', {'sql': sql},
|
2016-05-05 15:47:45 +00:00
|
|
|
this._onExec.bind (this, callback));
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs a stmt on the database.
|
|
|
|
*
|
|
|
|
* @param {Sql.Stmt} stmt The statement
|
2017-04-21 10:53:15 +00:00
|
|
|
* @param {function} callback The function to call when operation is done
|
2017-04-05 14:06:07 +00:00
|
|
|
* @param {Object} params The statement parameters
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2017-04-05 14:06:07 +00:00
|
|
|
,execStmt: function (stmt, callback, params)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2017-04-05 14:06:07 +00:00
|
|
|
this.execSql (stmt.render (params), callback);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs a query on the database.
|
|
|
|
*
|
2017-04-21 10:53:15 +00:00
|
|
|
* @param {string} query The SQL statement
|
|
|
|
* @param {function} callback The function to call when operation is done
|
2017-04-05 14:06:07 +00:00
|
|
|
* @param {Object} params The statement parameters
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2017-04-05 14:06:07 +00:00
|
|
|
,execQuery: function (query, callback, params)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2017-04-10 15:17:56 +00:00
|
|
|
this.execSql (this.renderQuery (query, params), callback);
|
|
|
|
}
|
|
|
|
|
2017-04-19 06:16:37 +00:00
|
|
|
/**
|
|
|
|
* Renders a query.
|
|
|
|
*
|
2017-04-21 10:53:15 +00:00
|
|
|
* @param {string} query The SQL statement
|
2017-04-19 06:16:37 +00:00
|
|
|
* @param {Object} params The statement parameters
|
2017-04-21 10:53:15 +00:00
|
|
|
* @return {string} The rendered statement
|
2017-04-19 06:16:37 +00:00
|
|
|
*/
|
2017-04-10 15:17:56 +00:00
|
|
|
,renderQuery: function (query, params)
|
|
|
|
{
|
|
|
|
return new Sql.String ({query: query}).render (params);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parses a value to date.
|
|
|
|
*/
|
|
|
|
,valueToDate: function (value)
|
|
|
|
{
|
|
|
|
return new Date (value * 1000);
|
|
|
|
}
|
2016-05-05 15:47:45 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
/*
|
|
|
|
* Called when a query is executed.
|
|
|
|
*/
|
2016-09-23 22:47:34 +00:00
|
|
|
,_onExec: function (callback, json, error)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2016-05-05 15:47:45 +00:00
|
|
|
if (json)
|
2015-01-23 13:09:30 +00:00
|
|
|
try {
|
2016-05-05 15:47:45 +00:00
|
|
|
if (json && json instanceof Array)
|
|
|
|
for (var i = 0; i < json.length; i++)
|
|
|
|
if (json[i] !== true)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2016-05-05 15:47:45 +00:00
|
|
|
var data = json[i].data;
|
|
|
|
var columns = json[i].columns;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
for (var j = 0; j < columns.length; j++)
|
|
|
|
{
|
|
|
|
var castFunc = null;
|
|
|
|
|
|
|
|
switch (columns[j].type)
|
|
|
|
{
|
2016-09-26 09:28:47 +00:00
|
|
|
case Type.DATE:
|
|
|
|
case Type.DATE_TIME:
|
|
|
|
case Type.TIMESTAMP:
|
2015-01-23 13:09:30 +00:00
|
|
|
castFunc = this.valueToDate;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (castFunc !== null)
|
|
|
|
{
|
|
|
|
if (columns[j].def != null)
|
|
|
|
columns[j].def = castFunc (columns[j].def);
|
|
|
|
|
|
|
|
for (var k = 0; k < data.length; k++)
|
|
|
|
if (data[k][j] != null)
|
|
|
|
data[k][j] = castFunc (data[k][j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 00:44:34 +00:00
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
error = e;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2016-05-05 15:47:45 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
if (callback)
|
2016-05-05 15:47:45 +00:00
|
|
|
callback (new Db.ResultSet (json, error));
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|