hedera-web/js/db/conn.js

230 lines
4.7 KiB
JavaScript

/**
* 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:
{
BOOLEAN : 1
,INTEGER : 3
,DOUBLE : 4
,STRING : 5
,DATE : 8
,DATE_TIME : 9
}
});
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
**/
,open: function (user, pass, remember, callback, guest)
{
this.signalEmit ('loading-changed', true);
if (user != null)
{
var params = {
'user': user
,'password': pass
,'remember': remember
};
}
else if (guest)
var params = {'guest': true};
var request = new Vn.JsonRequest ('core/login');
request.send (params,
this._onOpen.bind (this, callback));
}
/*
* Called when open operation is done.
*/
,_onOpen: function (callback, request, json, error)
{
this._connected = json == true;
this.signalEmit ('loading-changed', false);
if (this._connected)
this.signalEmit ('openned');
if (error)
this.signalEmit ('error', error);
if (callback)
callback (this, this._connected, error);
}
/**
* Closes the connection to the database.
*
* @param {Function} callback The function to call when operation is done
**/
,close: function (callback)
{
this.signalEmit ('loading-changed', true);
var request = new Vn.JsonRequest ('core/logout');
request.send (null,
this._onClose.bind (this, callback));
}
/*
* Called when close operation is done.
*/
,_onClose: function (callback, request, json, error)
{
this._connected = false;
this.signalEmit ('loading-changed', false);
this.signalEmit ('closed');
if (error)
this.signalEmit ('error', error);
if (callback)
callback (this, json == true, error);
}
/**
* 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);
var request = new Vn.JsonRequest ('core/query');
request.send ({'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
**/
,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.
*/
,_onExec: function (callback, request, json, error)
{
this._requestsCount--;
if (this._requestsCount === 0)
this.signalEmit ('loading-changed', false);
if (json)
try {
if (json && json instanceof Array)
for (var i = 0; i < json.length; i++)
if (json[i] !== true)
{
var data = json[i].data;
var columns = json[i].columns;
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]);
}
}
}
}
catch (e)
{
error = e;
}
if (error)
this.signalEmit ('error', error);
if (callback)
try {
callback (new Db.ResultSet (json, error));
}
catch (e)
{
this.signalEmit ('error', e);
}
}
});