2015-01-23 13:09:30 +00:00
|
|
|
/**
|
|
|
|
* Simulates a connection to a database by making asynchronous http requests to
|
|
|
|
* a remote PHP script 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.
|
|
|
|
**/
|
|
|
|
Db.Conn = new Class ().extend
|
|
|
|
({
|
|
|
|
Flag:
|
|
|
|
{
|
|
|
|
NOT_NULL : 1
|
|
|
|
,PRI_KEY : 2
|
|
|
|
,AI : 512 | 2 | 1
|
|
|
|
}
|
|
|
|
|
|
|
|
,Type:
|
|
|
|
{
|
2015-02-08 15:38:38 +00:00
|
|
|
BOOLEAN : 1
|
2016-01-15 12:31:08 +00:00
|
|
|
,INTEGER : 3
|
|
|
|
,DOUBLE : 4
|
|
|
|
,STRING : 5
|
2015-02-08 15:38:38 +00:00
|
|
|
,DATE : 8
|
|
|
|
,DATE_TIME : 9
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Db.Conn.implement
|
|
|
|
({
|
|
|
|
Extends: Vn.Object
|
|
|
|
|
|
|
|
,connected: false
|
|
|
|
,requestsCount: 0
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initilizes the connection object.
|
|
|
|
**/
|
|
|
|
,initialize: function ()
|
|
|
|
{
|
|
|
|
this.parent ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens the connection to the database.
|
|
|
|
*
|
|
|
|
* @param {string} user The user name
|
|
|
|
* @param {String} password The user password
|
|
|
|
* @param {Boolean} remember Specifies if the user should be remembered
|
|
|
|
* @param {Function} openCallback The function to call when operation is done
|
|
|
|
**/
|
2015-12-10 13:48:43 +00:00
|
|
|
,open: function (user, pass, remember, openCallback, guest)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
this.signalEmit ('loading-changed', true);
|
|
|
|
|
2015-03-06 23:33:54 +00:00
|
|
|
var request = new Vn.HttpRequest ();
|
2015-01-23 13:09:30 +00:00
|
|
|
request.add ({'action': 'login'});
|
|
|
|
|
|
|
|
if (user != null)
|
|
|
|
{
|
|
|
|
request.add
|
|
|
|
({
|
|
|
|
'user': user
|
|
|
|
,'password': pass
|
|
|
|
,'remember': remember
|
|
|
|
});
|
|
|
|
}
|
2015-12-10 13:48:43 +00:00
|
|
|
else if (guest)
|
|
|
|
request.add ({'guest': true});
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
request.send ('rest.php',
|
|
|
|
this.opened.bind (this, request, openCallback));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when open operation is done.
|
|
|
|
*/
|
|
|
|
,opened: function (request, openCallback, success)
|
|
|
|
{
|
2015-12-19 15:42:38 +00:00
|
|
|
var error = null;
|
2015-01-23 13:09:30 +00:00
|
|
|
var openSuccess = false;
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
try {
|
|
|
|
var json = request.getJson ();
|
2015-09-11 09:37:16 +00:00
|
|
|
openSuccess = json.data == true;
|
2015-12-19 15:42:38 +00:00
|
|
|
|
|
|
|
if (json.error)
|
|
|
|
error = new Vn.Error (
|
|
|
|
json.error.domain,
|
|
|
|
json.error.code,
|
|
|
|
json.error.message);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-12-19 15:42:38 +00:00
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
if (openSuccess)
|
2015-09-11 09:37:16 +00:00
|
|
|
{
|
2015-01-23 13:09:30 +00:00
|
|
|
this.connected = true;
|
2015-09-11 09:37:16 +00:00
|
|
|
this.signalEmit ('openned');
|
|
|
|
}
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
this.signalEmit ('loading-changed', false);
|
|
|
|
|
|
|
|
if (openCallback)
|
2015-12-19 15:42:38 +00:00
|
|
|
openCallback (this, openSuccess, error);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes the connection to the database.
|
|
|
|
*
|
|
|
|
* @param {Function} closeCallback The function to call when operation is done
|
|
|
|
**/
|
|
|
|
,close: function (closeCallback)
|
|
|
|
{
|
|
|
|
this.signalEmit ('loading-changed', true);
|
|
|
|
|
2015-03-06 23:33:54 +00:00
|
|
|
var request = new Vn.HttpRequest ();
|
2015-09-22 07:20:47 +00:00
|
|
|
request.add ({'action': 'logout'});
|
2015-01-23 13:09:30 +00:00
|
|
|
request.send ('rest.php',
|
|
|
|
this.closed.bind (this, closeCallback));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when close operation is done.
|
|
|
|
*/
|
|
|
|
,closed: function (closeCallback)
|
|
|
|
{
|
|
|
|
this.connected = false;
|
|
|
|
this.signalEmit ('closed');
|
|
|
|
|
|
|
|
this.signalEmit ('loading-changed', false);
|
|
|
|
|
|
|
|
if (closeCallback)
|
|
|
|
closeCallback (this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs a SQL query on the database.
|
|
|
|
*
|
|
|
|
* @param {String} sql The SQL statement
|
|
|
|
* @param {Function} callback The function to call when operation is done
|
|
|
|
**/
|
|
|
|
,execSql: function (sql, callback)
|
|
|
|
{
|
|
|
|
this.requestsCount++;
|
|
|
|
|
|
|
|
if (this.requestsCount == 1)
|
|
|
|
this.signalEmit ('loading-changed', true);
|
|
|
|
|
2015-03-06 23:33:54 +00:00
|
|
|
var httpRequest = new Vn.HttpRequest ()
|
2015-01-23 13:09:30 +00:00
|
|
|
httpRequest.add
|
|
|
|
({
|
|
|
|
'action': 'query'
|
|
|
|
,'sql': sql
|
|
|
|
});
|
|
|
|
httpRequest.send ('rest.php',
|
|
|
|
this.execDone.bind (this, httpRequest, 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
|
|
|
|
**/
|
|
|
|
,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
|
|
|
|
**/
|
|
|
|
,execQuery: function (query, callback, batch)
|
|
|
|
{
|
|
|
|
this.execStmt (new Sql.String ({query: query}), callback, batch);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parses a value to date.
|
|
|
|
*/
|
|
|
|
,valueToDate: function (value)
|
|
|
|
{
|
|
|
|
return new Date (value * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when a query is executed.
|
|
|
|
*/
|
|
|
|
,execDone: function (httpRequest, callback, success)
|
|
|
|
{
|
2015-08-28 00:44:34 +00:00
|
|
|
var e;
|
2015-01-23 13:09:30 +00:00
|
|
|
var error = null;
|
|
|
|
var results = null;
|
|
|
|
|
|
|
|
this.requestsCount--;
|
|
|
|
|
|
|
|
if (this.requestsCount == 0)
|
|
|
|
this.signalEmit ('loading-changed', false);
|
|
|
|
|
|
|
|
if (!success)
|
2015-08-28 00:44:34 +00:00
|
|
|
{
|
|
|
|
error = new Vn.Error ('Conn', 'connError', _('ConnError'));
|
|
|
|
this.signalEmit ('error', error);
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
else
|
|
|
|
try {
|
|
|
|
var json = httpRequest.getJson ();
|
|
|
|
results = json.data;
|
|
|
|
|
|
|
|
if (json.error !== null)
|
|
|
|
error = new Vn.Error (json.error.domain,
|
|
|
|
json.error.code, json.error.message);
|
|
|
|
|
|
|
|
if (results instanceof Array)
|
|
|
|
for (var i = 0; i < results.length; i++)
|
|
|
|
if (results[i] !== true)
|
|
|
|
{
|
|
|
|
var data = results[i].data;
|
2015-03-19 19:36:11 +00:00
|
|
|
var columns = results[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)
|
|
|
|
{
|
|
|
|
case Db.Conn.Type.DATE:
|
|
|
|
case Db.Conn.Type.DATE_TIME:
|
|
|
|
case Db.Conn.Type.TIMESTAMP:
|
|
|
|
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
|
|
|
}
|
2015-08-28 01:48:42 +00:00
|
|
|
|
|
|
|
if (error)
|
|
|
|
this.signalEmit ('error', error);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
if (callback)
|
2015-08-28 00:44:34 +00:00
|
|
|
try {
|
|
|
|
callback (new Db.ResultSet (results, error));
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
this.signalEmit ('error', e);
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|