2016-09-26 09:28:47 +00:00
|
|
|
|
|
|
|
var Connection = require ('./connection');
|
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
/**
|
|
|
|
* Class to handle the Database select results. Also allows
|
|
|
|
* updates, insertions and deletions on tables where the primary key is
|
|
|
|
* selected.
|
|
|
|
*
|
2015-03-27 19:10:49 +00:00
|
|
|
* Note that table and column names must be unique in the selection query,
|
|
|
|
* otherwise updates are not allowed on that table/column. If two tables or
|
|
|
|
* columns have the same name, an alias should be used to make it updatable.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2017-04-19 06:16:37 +00:00
|
|
|
var Klass = new Class ();
|
|
|
|
module.exports = Klass;
|
2016-09-26 09:28:47 +00:00
|
|
|
|
|
|
|
var Status =
|
|
|
|
{
|
|
|
|
CLEAN : 1
|
|
|
|
,LOADING : 2
|
|
|
|
,READY : 3
|
|
|
|
,ERROR : 4
|
|
|
|
};
|
|
|
|
|
|
|
|
var Mode =
|
|
|
|
{
|
|
|
|
ON_CHANGE : 1
|
|
|
|
,ON_DEMAND : 2
|
|
|
|
};
|
|
|
|
|
|
|
|
var Operation =
|
|
|
|
{
|
|
|
|
INSERT : 1 << 1
|
|
|
|
,UPDATE : 1 << 2
|
|
|
|
,DELETE : 1 << 3
|
|
|
|
};
|
|
|
|
|
|
|
|
var SortWay =
|
|
|
|
{
|
|
|
|
ASC : 1
|
|
|
|
,DESC : 2
|
|
|
|
};
|
|
|
|
|
2017-04-19 06:16:37 +00:00
|
|
|
Klass.extend
|
2015-01-23 13:09:30 +00:00
|
|
|
({
|
2016-09-26 09:28:47 +00:00
|
|
|
Status: Status
|
|
|
|
,Mode: Mode
|
|
|
|
,Operation: Operation
|
|
|
|
,SortWay: SortWay
|
2015-01-23 13:09:30 +00:00
|
|
|
});
|
|
|
|
|
2017-04-19 06:16:37 +00:00
|
|
|
Klass.implement
|
2015-01-23 13:09:30 +00:00
|
|
|
({
|
|
|
|
Extends: Vn.Object
|
|
|
|
,Tag: 'db-model'
|
|
|
|
,Properties:
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The connection used to execute the statement.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
conn:
|
|
|
|
{
|
2016-09-26 09:28:47 +00:00
|
|
|
type: Connection
|
2015-01-23 13:09:30 +00:00
|
|
|
,set: function (x)
|
|
|
|
{
|
|
|
|
this._conn = x;
|
2015-07-10 12:30:08 +00:00
|
|
|
this._autoLoad ();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this._conn;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* The result index.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
resultIndex:
|
|
|
|
{
|
|
|
|
type: Number
|
|
|
|
,set: function (x)
|
|
|
|
{
|
|
|
|
this._resultIndex = x;
|
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this._resultIndex;
|
|
|
|
}
|
|
|
|
},
|
2017-04-05 14:06:07 +00:00
|
|
|
/**
|
|
|
|
* The lot used to execute the statement.
|
|
|
|
*/
|
|
|
|
lot:
|
|
|
|
{
|
2017-04-10 16:23:40 +00:00
|
|
|
type: Vn.LotIface
|
2017-04-05 14:06:07 +00:00
|
|
|
,set: function (x)
|
|
|
|
{
|
2017-04-10 15:17:56 +00:00
|
|
|
this.link ({_lot: x}, {'change': this._onLotChange});
|
|
|
|
this._onLotChange ();
|
2017-04-05 14:06:07 +00:00
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this._lot;
|
|
|
|
}
|
|
|
|
},
|
2015-01-23 13:09:30 +00:00
|
|
|
/**
|
|
|
|
* The model select statement.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
stmt:
|
|
|
|
{
|
|
|
|
type: Sql.Stmt
|
|
|
|
,set: function (x)
|
|
|
|
{
|
|
|
|
this._stmt = x;
|
2015-07-10 12:30:08 +00:00
|
|
|
this._autoLoad ();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this._stmt;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* The model query.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
query:
|
|
|
|
{
|
|
|
|
type: String
|
|
|
|
,set: function (x)
|
|
|
|
{
|
2015-11-17 10:34:33 +00:00
|
|
|
this.stmt = new Sql.String ({query: x});
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
2015-11-17 10:34:33 +00:00
|
|
|
if (this._stmt)
|
|
|
|
return this._stmt.render (null);
|
|
|
|
else
|
|
|
|
return null;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* The main table.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
mainTable:
|
|
|
|
{
|
|
|
|
type: String
|
|
|
|
,set: function (x)
|
|
|
|
{
|
|
|
|
this._mainTable = null;
|
2016-01-07 12:58:29 +00:00
|
|
|
this._requestedMainTable = x;
|
2015-03-27 19:10:49 +00:00
|
|
|
this._refreshMainTable ();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this._mainTable;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Determines if the model is updatable.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
updatable:
|
|
|
|
{
|
|
|
|
type: Boolean
|
|
|
|
,set: function (x)
|
|
|
|
{
|
|
|
|
this._updatable = false;
|
2016-01-07 12:58:29 +00:00
|
|
|
this._requestedUpdatable = x;
|
2015-03-27 19:10:49 +00:00
|
|
|
this._refreshUpdatable ();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this._updatable;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* The number of rows in the model.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
numRows:
|
|
|
|
{
|
|
|
|
type: Number
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
if (this.data)
|
|
|
|
return this.data.length;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* The current status of the model.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
status:
|
|
|
|
{
|
|
|
|
type: Number
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this._status;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Checks if the model data is ready.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
ready:
|
|
|
|
{
|
|
|
|
type: Boolean
|
|
|
|
,get: function ()
|
|
|
|
{
|
2016-09-26 09:28:47 +00:00
|
|
|
return this._status == Status.READY;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-07-10 12:30:08 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Update mode.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-07-10 12:30:08 +00:00
|
|
|
mode:
|
|
|
|
{
|
2016-09-26 09:28:47 +00:00
|
|
|
enumType: Mode
|
|
|
|
,value: Mode.ON_CHANGE
|
2015-07-10 12:30:08 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Wether to execute the model query automatically.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-07-10 12:30:08 +00:00
|
|
|
autoLoad:
|
|
|
|
{
|
|
|
|
type: Boolean
|
|
|
|
,value: true
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 08:14:33 +00:00
|
|
|
,_conn: null
|
2015-01-23 13:09:30 +00:00
|
|
|
,_resultIndex: 0
|
2017-04-10 15:17:56 +00:00
|
|
|
,_lot: null
|
2015-01-23 13:09:30 +00:00
|
|
|
,_stmt: null
|
2016-09-26 09:28:47 +00:00
|
|
|
,_status: Status.CLEAN
|
2017-04-19 06:16:37 +00:00
|
|
|
,result: null
|
2015-01-23 13:09:30 +00:00
|
|
|
,data: null
|
2015-03-19 19:36:11 +00:00
|
|
|
,tables: null
|
2015-01-23 13:09:30 +00:00
|
|
|
,columns: null
|
|
|
|
,columnMap: null
|
2015-03-27 19:10:49 +00:00
|
|
|
,_updatable: false
|
2015-03-19 19:36:11 +00:00
|
|
|
|
2016-01-07 12:58:29 +00:00
|
|
|
,_requestedSortIndex: -1
|
|
|
|
,_requestedSortName: null
|
|
|
|
,_sortColumn: -1
|
|
|
|
,_sortWay: null
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2016-01-07 12:58:29 +00:00
|
|
|
,_requestedIndexes: {}
|
|
|
|
,_indexes: []
|
|
|
|
|
|
|
|
,_requestedUpdatable: false
|
|
|
|
,_operations: null
|
|
|
|
,_operationsMap: null
|
|
|
|
,_defaults: []
|
|
|
|
,_requestedMainTable: null
|
2015-03-19 19:36:11 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
,initialize: function (props)
|
|
|
|
{
|
|
|
|
this.parent (props);
|
2015-03-19 19:36:11 +00:00
|
|
|
this._cleanData ();
|
2016-09-26 09:28:47 +00:00
|
|
|
this._setStatus (Status.CLEAN);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-07-10 12:30:08 +00:00
|
|
|
|
2017-04-07 11:00:33 +00:00
|
|
|
,appendChild: function (child)
|
|
|
|
{
|
|
|
|
if (child.nodeType === Node.TEXT_NODE)
|
|
|
|
this.query = child.textContent;
|
|
|
|
}
|
2017-04-10 15:17:56 +00:00
|
|
|
|
|
|
|
,_getParams: function ()
|
|
|
|
{
|
|
|
|
if (!this._stmt)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
var ids = this._stmt.findHolders ();
|
|
|
|
|
|
|
|
if (!ids)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
var lotParams = this._lot ? this._lot.params : {};
|
2017-04-19 06:16:37 +00:00
|
|
|
|
|
|
|
if (lotParams == null)
|
|
|
|
lotParams = {};
|
|
|
|
|
2017-04-10 15:17:56 +00:00
|
|
|
var params = {};
|
|
|
|
|
|
|
|
for (var i = 0; i < ids.length; i++)
|
|
|
|
params[ids[i]] = lotParams[ids[i]];
|
|
|
|
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
,_onLotChange: function ()
|
|
|
|
{
|
|
|
|
var params = this._getParams ();
|
|
|
|
|
|
|
|
if (!Vn.Value.equals (params, this._lastParams))
|
|
|
|
this._autoLoad ();
|
|
|
|
}
|
|
|
|
|
2015-07-10 12:30:08 +00:00
|
|
|
,_autoLoad: function ()
|
|
|
|
{
|
|
|
|
if (this.autoLoad)
|
|
|
|
this.refresh ();
|
2015-11-17 10:34:33 +00:00
|
|
|
else
|
|
|
|
this.clean ();
|
2015-07-10 12:30:08 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2017-04-10 15:17:56 +00:00
|
|
|
,_isReady: function (params)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2017-04-07 11:00:33 +00:00
|
|
|
if (!this._stmt || !this._conn)
|
|
|
|
return false;
|
2017-04-08 11:42:27 +00:00
|
|
|
if (!params)
|
2017-04-10 15:17:56 +00:00
|
|
|
return true;
|
2017-04-07 11:00:33 +00:00
|
|
|
|
2017-04-10 15:17:56 +00:00
|
|
|
for (var key in params)
|
|
|
|
if (params[key] === undefined)
|
2017-04-07 11:00:33 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2017-04-07 11:00:33 +00:00
|
|
|
/**
|
|
|
|
* Refresh the model data reexecuting the query on the database.
|
|
|
|
*/
|
|
|
|
,refresh: function ()
|
|
|
|
{
|
2017-04-10 15:17:56 +00:00
|
|
|
var params = this._getParams ();
|
|
|
|
|
|
|
|
if (this._isReady (params))
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2017-04-10 15:17:56 +00:00
|
|
|
this._lastParams = params;
|
2016-09-26 09:28:47 +00:00
|
|
|
this._setStatus (Status.LOADING);
|
2017-04-07 11:00:33 +00:00
|
|
|
this._conn.execStmt (this._stmt, this._selectDone.bind (this), params);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
else
|
2015-09-28 15:21:37 +00:00
|
|
|
this.clean ();
|
|
|
|
}
|
|
|
|
|
|
|
|
,clean: function ()
|
|
|
|
{
|
|
|
|
this._cleanData ();
|
2016-09-26 09:28:47 +00:00
|
|
|
this._setStatus (Status.CLEAN);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-03-19 19:36:11 +00:00
|
|
|
|
|
|
|
,_selectDone: function (resultSet)
|
|
|
|
{
|
|
|
|
var result;
|
|
|
|
var dataResult;
|
|
|
|
|
|
|
|
this._cleanData ();
|
|
|
|
|
2016-10-11 14:45:10 +00:00
|
|
|
try {
|
|
|
|
for (var i = 0; result = resultSet.fetchResult (); i++)
|
2015-03-19 19:36:11 +00:00
|
|
|
if (i == this._resultIndex)
|
|
|
|
dataResult = result;
|
|
|
|
|
2016-10-11 14:45:10 +00:00
|
|
|
if (!dataResult || typeof dataResult !== 'object')
|
|
|
|
throw new Error ('The provided statement doesn\'t return a result set');
|
|
|
|
}
|
|
|
|
catch (e)
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
2016-10-11 14:45:10 +00:00
|
|
|
this._setStatus (Status.ERROR);
|
|
|
|
throw e;
|
|
|
|
}
|
2016-01-07 12:58:29 +00:00
|
|
|
|
2017-04-19 06:16:37 +00:00
|
|
|
this.result = dataResult;
|
2016-10-11 14:45:10 +00:00
|
|
|
this.data = dataResult.data;
|
|
|
|
this.tables = dataResult.tables;
|
|
|
|
this.columns = dataResult.columns;
|
|
|
|
this.columnMap = dataResult.columnMap;
|
|
|
|
this._repairColumns ();
|
|
|
|
this._refreshRowIndexes (0);
|
|
|
|
this._refreshMainTable ();
|
2016-01-07 12:58:29 +00:00
|
|
|
|
2016-10-11 14:45:10 +00:00
|
|
|
for (column in this._requestedIndexes)
|
|
|
|
this._buildIndex (column);
|
2016-01-07 12:58:29 +00:00
|
|
|
|
2016-10-11 14:45:10 +00:00
|
|
|
var sortColumn = -1;
|
2016-01-07 12:58:29 +00:00
|
|
|
|
2016-10-11 14:45:10 +00:00
|
|
|
if (this._requestedSortName)
|
|
|
|
sortColumn = this.getColumnIndex (this._requestedSortName);
|
|
|
|
else if (this._requestedSortIndex !== -1
|
|
|
|
&& this.checkColExists (this._requestedSortIndex))
|
|
|
|
sortColumn = this._requestedSortIndex;
|
|
|
|
|
|
|
|
if (sortColumn !== -1)
|
|
|
|
this._realSort (sortColumn, this._sortWay);
|
|
|
|
|
|
|
|
this._setStatus (Status.READY);
|
2015-03-19 19:36:11 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
,_refreshRowIndexes: function (start)
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
2015-03-27 19:10:49 +00:00
|
|
|
for (var i = start; i < this.data.length; i++)
|
2015-03-19 19:36:11 +00:00
|
|
|
this.data[i].index = i;
|
|
|
|
|
2016-01-07 12:58:29 +00:00
|
|
|
if (this._operationsMap)
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
2016-01-07 12:58:29 +00:00
|
|
|
this._operationsMap = {};
|
2015-03-19 19:36:11 +00:00
|
|
|
|
2016-01-07 12:58:29 +00:00
|
|
|
for (var i = 0; i < this._operations.length; i++)
|
|
|
|
this._operationsMap[i] = this._operations[i];
|
2015-03-19 19:36:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 15:17:56 +00:00
|
|
|
,_cleanData: function ()
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2017-04-19 06:16:37 +00:00
|
|
|
this.result = null;
|
2015-01-23 13:09:30 +00:00
|
|
|
this.data = null;
|
2015-03-27 19:10:49 +00:00
|
|
|
this.tables = null;
|
2015-01-23 13:09:30 +00:00
|
|
|
this.columns = null;
|
|
|
|
this.columnMap = null;
|
2016-01-07 12:58:29 +00:00
|
|
|
this._sortColumn = -1;
|
|
|
|
this._indexes = [];
|
2015-03-27 19:10:49 +00:00
|
|
|
this._resetOperations ();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
,_refreshUpdatable: function ()
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
var oldValue = this._updatable;
|
2016-01-07 12:58:29 +00:00
|
|
|
this._updatable = this._mainTable !== null && this._requestedUpdatable;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
if (oldValue != this._updatable)
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('updatable-changed');
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
,_refreshMainTable: function ()
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
var newMainTable = null;
|
2015-03-19 19:36:11 +00:00
|
|
|
var tables = this.tables;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
if (tables)
|
|
|
|
for (var i = 0; i < tables.length; i++)
|
|
|
|
if (tables[i].pks.length > 0)
|
2016-01-07 12:58:29 +00:00
|
|
|
if (!this._requestedMainTable
|
|
|
|
|| tables[i].name === this._requestedMainTable)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-19 19:36:11 +00:00
|
|
|
newMainTable = i;
|
2015-01-23 13:09:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._mainTable = newMainTable;
|
2015-03-27 19:10:49 +00:00
|
|
|
this._refreshUpdatable ();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
/**
|
|
|
|
* Sets the default value for inserted fields.
|
|
|
|
*
|
|
|
|
* @param {String} field The destination field name
|
|
|
|
* @param {String} table The destination table name
|
|
|
|
* @param {Sql.Expr} srcColumn The default value expression
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-03-19 19:36:11 +00:00
|
|
|
,setDefault: function (field, table, expr)
|
|
|
|
{
|
2016-01-07 12:58:29 +00:00
|
|
|
this._defaults.push
|
2015-03-19 19:36:11 +00:00
|
|
|
({
|
|
|
|
field: field
|
|
|
|
,table: table
|
|
|
|
,expr: expr
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
/**
|
|
|
|
* Sets the default value for inserted fields.
|
|
|
|
*
|
|
|
|
* @param {String} field The destination field name
|
|
|
|
* @param {String} table The destination table name
|
2017-03-30 11:44:53 +00:00
|
|
|
* @param {*} value The default value
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
,setDefaultFromValue: function (field, table, value)
|
|
|
|
{
|
2016-01-07 12:58:29 +00:00
|
|
|
this._defaults.push
|
2015-01-23 13:09:30 +00:00
|
|
|
({
|
|
|
|
field: field
|
|
|
|
,table: table
|
|
|
|
,value: value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the default value for inserted fields from another column in the
|
|
|
|
* model.
|
|
|
|
*
|
|
|
|
* @param {String} field The destination field name
|
|
|
|
* @param {String} table The destination table name
|
|
|
|
* @param {String} srcColumn The source column
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
,setDefaultFromColumn: function (field, table, srcColumn)
|
|
|
|
{
|
2016-01-07 12:58:29 +00:00
|
|
|
this._defaults.push
|
2015-01-23 13:09:30 +00:00
|
|
|
({
|
|
|
|
field: field
|
|
|
|
,table: table
|
|
|
|
,srcColumn: srcColumn
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the column exists.
|
|
|
|
*
|
|
|
|
* @param {integer} column The column index
|
|
|
|
* @return {Boolean} %true if column exists, %false otherwise
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
,checkColExists: function (column)
|
|
|
|
{
|
2015-03-27 19:10:49 +00:00
|
|
|
return this.columns
|
|
|
|
&& column >= 0
|
|
|
|
&& column < this.columns.length;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the row exists.
|
|
|
|
*
|
2015-03-19 19:36:11 +00:00
|
|
|
* @param {integer} rowIndex The row index
|
2015-01-23 13:09:30 +00:00
|
|
|
* @return {Boolean} %true if row exists, %false otherwise
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-03-19 19:36:11 +00:00
|
|
|
,checkRowExists: function (rowIndex)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-27 19:10:49 +00:00
|
|
|
return this.data
|
|
|
|
&& rowIndex >= 0
|
|
|
|
&& rowIndex < this.data.length;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-03-27 19:10:49 +00:00
|
|
|
|
|
|
|
,_checkTableUpdatable: function (tableIndex)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-04-01 08:24:15 +00:00
|
|
|
var tableUpdatable = tableIndex !== null
|
2015-03-27 19:10:49 +00:00
|
|
|
&& this.tables[tableIndex].pks.length > 0;
|
|
|
|
|
|
|
|
if (!tableUpdatable)
|
2015-07-03 05:49:45 +00:00
|
|
|
console.warn ("Db.Model: Table %s is not updatable",
|
2015-03-27 19:10:49 +00:00
|
|
|
this.tables[tableIndex].name);
|
|
|
|
|
|
|
|
return tableUpdatable;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the index of the column from its name.
|
|
|
|
*
|
|
|
|
* @param {string} columnName The column name
|
|
|
|
* @return {number} The column index or -1 if column not exists
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
,getColumnIndex: function (columnName)
|
|
|
|
{
|
|
|
|
var index;
|
|
|
|
|
|
|
|
if (this.columnMap
|
|
|
|
&& (index = this.columnMap[columnName]) !== undefined)
|
|
|
|
return index;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a value from the model.
|
|
|
|
*
|
2015-03-19 19:36:11 +00:00
|
|
|
* @param {number} rowIndex The row index
|
2017-03-30 11:44:53 +00:00
|
|
|
* @param {String} columnName The column name
|
|
|
|
* @return {*} The value
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-03-19 19:36:11 +00:00
|
|
|
,get: function (rowIndex, columnName)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
var index = this.getColumnIndex (columnName);
|
|
|
|
|
|
|
|
if (index != -1)
|
2015-03-19 19:36:11 +00:00
|
|
|
return this.getByIndex (rowIndex, index);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a value on the model.
|
|
|
|
*
|
2015-03-19 19:36:11 +00:00
|
|
|
* @param {number} rowIndex The row index
|
2017-03-30 11:44:53 +00:00
|
|
|
* @param {String} columnName The column name
|
|
|
|
* @param {*} value The new value
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-03-19 19:36:11 +00:00
|
|
|
,set: function (rowIndex, columnName, value)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
var index = this.getColumnIndex (columnName);
|
|
|
|
|
|
|
|
if (index != -1)
|
2015-03-19 19:36:11 +00:00
|
|
|
this.setByIndex (rowIndex, index, value);
|
2015-03-27 19:10:49 +00:00
|
|
|
else
|
|
|
|
console.warn ('Db.Model: Column %s doesn\'t exist', columnName);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-19 06:16:37 +00:00
|
|
|
* Gets a value using the column index.
|
2015-01-23 13:09:30 +00:00
|
|
|
*
|
2015-03-19 19:36:11 +00:00
|
|
|
* @param {number} rowIndex The row index
|
2015-01-23 13:09:30 +00:00
|
|
|
* @param {number} column The column index
|
2017-03-30 11:44:53 +00:00
|
|
|
* @return {*} The value
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-03-19 19:36:11 +00:00
|
|
|
,getByIndex: function (rowIndex, column)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-19 19:36:11 +00:00
|
|
|
if (this.checkRowExists (rowIndex) && this.checkColExists (column))
|
|
|
|
return this.data[rowIndex][column];
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2017-04-19 06:16:37 +00:00
|
|
|
/**
|
|
|
|
* Gets a row as an object using the column index.
|
|
|
|
*
|
|
|
|
* @param {number} rowIndex The row index
|
|
|
|
* @return {Object} The row as an object
|
|
|
|
*/
|
|
|
|
,getObject: function (rowIndex)
|
|
|
|
{
|
|
|
|
if (!this.checkRowExists (rowIndex))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return this.result.getObject (rowIndex);
|
|
|
|
}
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
/**
|
|
|
|
* Updates a value on the model using the column index.
|
|
|
|
*
|
2015-03-19 19:36:11 +00:00
|
|
|
* @param {number} rowIndex The row index
|
2015-01-23 13:09:30 +00:00
|
|
|
* @param {number} col The column index
|
2017-03-30 11:44:53 +00:00
|
|
|
* @param {*} value The new value
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-03-19 19:36:11 +00:00
|
|
|
,setByIndex: function (rowIndex, col, value)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-27 19:10:49 +00:00
|
|
|
if (!this.checkRowExists (rowIndex)
|
|
|
|
&& !this.checkColExists (col))
|
2015-01-23 13:09:30 +00:00
|
|
|
return;
|
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
var tableIndex = this.columns[col].table;
|
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
if (!this._checkTableUpdatable (tableIndex))
|
2015-01-23 13:09:30 +00:00
|
|
|
return;
|
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
var row = this.data[rowIndex];
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
var op = this._createOperation (rowIndex);
|
2016-09-26 09:28:47 +00:00
|
|
|
op.type |= Operation.UPDATE;
|
2015-03-27 19:10:49 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
if (!op.oldValues)
|
|
|
|
op.oldValues = [];
|
|
|
|
if (!op.tables)
|
|
|
|
op.tables = {};
|
|
|
|
|
|
|
|
var tableOp = op.tables[tableIndex];
|
|
|
|
|
|
|
|
if (!tableOp)
|
|
|
|
{
|
2016-09-26 09:28:47 +00:00
|
|
|
tableOp = Operation.UPDATE;
|
2015-03-27 19:10:49 +00:00
|
|
|
var pks = this.tables[tableIndex].pks;
|
2015-03-19 19:36:11 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < pks.length; i++)
|
|
|
|
if (!row[pks[i]] && !op.oldValues[pks[i]])
|
|
|
|
{
|
2016-09-26 09:28:47 +00:00
|
|
|
tableOp = Operation.INSERT;
|
2015-03-19 19:36:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
op.tables[tableIndex] = tableOp;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-03-19 19:36:11 +00:00
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
if (tableOp & Operation.UPDATE
|
2015-03-19 19:36:11 +00:00
|
|
|
&& op.oldValues[col] === undefined)
|
|
|
|
op.oldValues[col] = row[col];
|
|
|
|
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('row-updated-before', rowIndex);
|
2015-03-19 19:36:11 +00:00
|
|
|
row[col] = value;
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('row-updated', rowIndex, [col]);
|
2015-03-19 19:36:11 +00:00
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
if (this.mode == Mode.ON_CHANGE
|
|
|
|
&& !(op.type & Operation.INSERT))
|
2015-03-19 19:36:11 +00:00
|
|
|
this.performOperations ();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a row from the model.
|
|
|
|
*
|
2015-03-19 19:36:11 +00:00
|
|
|
* @param {number} rowIndex The row index
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-03-19 19:36:11 +00:00
|
|
|
,deleteRow: function (rowIndex)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-27 19:10:49 +00:00
|
|
|
if (!this.checkRowExists (rowIndex)
|
|
|
|
|| !this._checkTableUpdatable (this._mainTable))
|
2015-01-23 13:09:30 +00:00
|
|
|
return;
|
2015-03-19 19:36:11 +00:00
|
|
|
|
|
|
|
var op = this._createOperation (rowIndex);
|
2016-09-26 09:28:47 +00:00
|
|
|
op.type |= Operation.DELETE;
|
2015-03-27 19:10:49 +00:00
|
|
|
|
2016-01-07 12:58:29 +00:00
|
|
|
if (!this._requestedMainTable)
|
2015-03-27 19:10:49 +00:00
|
|
|
{
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('row-deleted-before', rowIndex);
|
2015-03-27 19:10:49 +00:00
|
|
|
this.data.splice (rowIndex, 1);
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('row-deleted', rowIndex);
|
2015-03-27 19:10:49 +00:00
|
|
|
this._refreshRowIndexes (rowIndex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('row-updated-before', rowIndex);
|
2015-03-27 19:10:49 +00:00
|
|
|
|
|
|
|
if (!op.oldValues)
|
|
|
|
op.oldValues = [];
|
|
|
|
|
|
|
|
var updatedCols = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < this.columns.length; i++)
|
|
|
|
if (this.columns[i].table == this._mainTable)
|
|
|
|
{
|
|
|
|
if (op.oldValues[i] === undefined)
|
|
|
|
op.oldValues[i] = op.row[i];
|
|
|
|
|
|
|
|
op.row[i] = null;
|
|
|
|
updatedCols.push (i);
|
|
|
|
}
|
|
|
|
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('row-updated', rowIndex, updatedCols);
|
2015-03-27 19:10:49 +00:00
|
|
|
}
|
2015-03-19 19:36:11 +00:00
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
if (this.mode === Mode.ON_CHANGE)
|
2015-03-19 19:36:11 +00:00
|
|
|
this.performOperations ();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts a new row on the model.
|
|
|
|
*
|
|
|
|
* @return The index of the inserted row
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
,insertRow: function ()
|
|
|
|
{
|
2015-03-27 19:10:49 +00:00
|
|
|
if (!this._checkTableUpdatable (this._mainTable))
|
2015-01-23 13:09:30 +00:00
|
|
|
return -1;
|
2015-03-19 19:36:11 +00:00
|
|
|
|
|
|
|
var cols = this.columns;
|
|
|
|
var newRow = new Array (cols.length);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
for (var i = 0; i < cols.length; i++)
|
|
|
|
if (cols[i].table === this._mainTable)
|
|
|
|
newRow[i] = cols[i].def;
|
2015-01-23 13:09:30 +00:00
|
|
|
else
|
|
|
|
newRow[i] = null;
|
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
var rowIndex = this.data.push (newRow) - 1;
|
|
|
|
newRow.index = rowIndex;
|
2015-03-19 19:36:11 +00:00
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
var op = this._createOperation (rowIndex);
|
2016-09-26 09:28:47 +00:00
|
|
|
op.type |= Operation.INSERT;
|
2015-03-19 19:36:11 +00:00
|
|
|
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('row-inserted', rowIndex);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
return rowIndex;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
/**
|
|
|
|
* Performs all model changes on the database.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
,performOperations: function ()
|
|
|
|
{
|
2016-01-07 12:58:29 +00:00
|
|
|
var ops = this._operations;
|
2015-03-27 19:10:49 +00:00
|
|
|
|
|
|
|
if (ops.length === 0)
|
2015-07-10 12:30:08 +00:00
|
|
|
{
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('operations-done');
|
2015-01-23 13:09:30 +00:00
|
|
|
return;
|
2015-07-10 12:30:08 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
var stmts = new Sql.MultiStmt ();
|
|
|
|
|
|
|
|
var query = new Sql.String ({query: 'START TRANSACTION'});
|
|
|
|
stmts.addStmt (query);
|
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
for (var i = 0; i < ops.length; i++)
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
|
|
|
query = null;
|
2015-03-27 19:10:49 +00:00
|
|
|
var op = ops[i];
|
2015-03-19 19:36:11 +00:00
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
if (op.type & Operation.DELETE)
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
2016-09-26 09:28:47 +00:00
|
|
|
if (op.type & Operation.INSERT)
|
2015-03-19 19:36:11 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
var where = this._createWhere (this._mainTable, op, true);
|
|
|
|
|
|
|
|
if (where)
|
|
|
|
{
|
|
|
|
query = new Sql.Delete ({where: where});
|
|
|
|
query.addTarget (this._createTarget (this._mainTable));
|
|
|
|
}
|
|
|
|
}
|
2016-09-26 09:28:47 +00:00
|
|
|
else if (op.type & (Operation.INSERT | Operation.UPDATE))
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
|
|
|
query = new Sql.MultiStmt ();
|
|
|
|
|
|
|
|
for (var tableIndex in op.tables)
|
|
|
|
{
|
|
|
|
var stmt = this._createDmlQuery (op, parseInt (tableIndex));
|
|
|
|
query.addStmt (stmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query)
|
|
|
|
{
|
|
|
|
stmts.addStmt (query);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
console.warn ('Db.Model: %s', _('ErrorSavingChanges'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-07-10 12:30:08 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
var query = new Sql.String ({query: 'COMMIT'});
|
|
|
|
stmts.addStmt (query);
|
|
|
|
|
|
|
|
this._conn.execStmt (stmts,
|
2015-03-27 19:10:49 +00:00
|
|
|
this._onOperationsDone.bind (this, ops));
|
|
|
|
|
|
|
|
this._resetOperations ();
|
2015-03-19 19:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,_createDmlQuery: function (op, tableIndex)
|
|
|
|
{
|
|
|
|
var where = this._createWhere (tableIndex, op, false);
|
|
|
|
|
|
|
|
if (!where)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
var multiStmt = new Sql.MultiStmt ();
|
|
|
|
var target = this._createTarget (tableIndex);
|
|
|
|
|
|
|
|
var select = new Sql.Select ({where: where});
|
|
|
|
select.addTarget (target);
|
2015-03-27 19:10:49 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
var row = op.row;
|
|
|
|
var cols = this.columns;
|
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
if (op.tables[tableIndex] & Operation.INSERT)
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
|
|
|
var dmlQuery = new Sql.Insert ();
|
2015-03-27 19:10:49 +00:00
|
|
|
var table = this.tables[tableIndex];
|
2015-03-19 19:36:11 +00:00
|
|
|
|
2016-01-07 12:58:29 +00:00
|
|
|
for (var i = 0; i < this._defaults.length; i++)
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
2016-01-07 12:58:29 +00:00
|
|
|
var def = this._defaults[i];
|
2015-03-19 19:36:11 +00:00
|
|
|
|
|
|
|
if (def.table === table.name)
|
|
|
|
{
|
|
|
|
if (def.value)
|
|
|
|
dmlQuery.addSet (def.field, def.value);
|
|
|
|
else if (def.expr)
|
|
|
|
dmlQuery.addExpr (def.field, def.expr);
|
|
|
|
else if (def.srcColumn)
|
|
|
|
{
|
|
|
|
var columnIndex = this.getColumnIndex (def.srcColumn);
|
|
|
|
dmlQuery.addSet (def.field, row[columnIndex]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < cols.length; i++)
|
|
|
|
if (cols[i].table === tableIndex)
|
|
|
|
{
|
|
|
|
if (row[i] !== null)
|
|
|
|
dmlQuery.addSet (cols[i].orgname, row[i]);
|
|
|
|
|
|
|
|
select.addField (cols[i].orgname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var updateWhere = this._createWhere (tableIndex, op, true);
|
|
|
|
|
|
|
|
if (!updateWhere)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
var dmlQuery = new Sql.Update ({where: updateWhere});
|
|
|
|
|
|
|
|
for (var i = 0; i < cols.length; i++)
|
|
|
|
if (cols[i].table === tableIndex && op.oldValues[i] !== undefined)
|
|
|
|
{
|
|
|
|
var fieldName = cols[i].orgname;
|
|
|
|
dmlQuery.addSet (fieldName, row[i]);
|
|
|
|
select.addField (fieldName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dmlQuery.addTarget (target);
|
|
|
|
|
|
|
|
multiStmt.addStmt (dmlQuery);
|
|
|
|
multiStmt.addStmt (select);
|
|
|
|
return multiStmt;
|
|
|
|
}
|
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
,_onOperationsDone: function (ops, resultSet)
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
2016-10-30 22:48:18 +00:00
|
|
|
var error = resultSet.getError ();
|
|
|
|
|
|
|
|
if (error)
|
2015-03-27 19:10:49 +00:00
|
|
|
{
|
2016-01-07 12:58:29 +00:00
|
|
|
this._operations = this._operations.concat (ops);
|
2015-03-27 19:10:49 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < ops.length; i++)
|
2016-01-07 12:58:29 +00:00
|
|
|
this._operationsMap[ops[i].row.index] = ops[i];
|
2015-07-21 14:16:07 +00:00
|
|
|
|
2016-10-30 22:48:18 +00:00
|
|
|
throw error;
|
2015-03-27 19:10:49 +00:00
|
|
|
}
|
2015-07-10 12:30:08 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
resultSet.fetchResult ();
|
2016-10-20 08:37:08 +00:00
|
|
|
var isOperation = false;
|
2015-03-19 19:36:11 +00:00
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
for (var i = 0; i < ops.length; i++)
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
2015-03-27 19:10:49 +00:00
|
|
|
var op = ops[i];
|
2015-03-19 19:36:11 +00:00
|
|
|
var row = op.row;
|
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
if (!(op.type & Operation.DELETE
|
|
|
|
&& op.type & Operation.INSERT))
|
2015-03-27 19:10:49 +00:00
|
|
|
isOperation = true;
|
2015-03-19 19:36:11 +00:00
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
if (op.type & Operation.DELETE)
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
|
|
|
resultSet.fetchResult ();
|
|
|
|
}
|
2016-09-26 09:28:47 +00:00
|
|
|
else if (op.type & (Operation.INSERT | Operation.UPDATE))
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('row-updated-before', row.index);
|
2015-03-19 19:36:11 +00:00
|
|
|
|
|
|
|
var updatedCols = [];
|
|
|
|
var cols = this.columns;
|
|
|
|
|
|
|
|
for (var tableIndex in op.tables)
|
|
|
|
{
|
|
|
|
var j = 0;
|
|
|
|
tableIndex = parseInt (tableIndex);
|
|
|
|
|
|
|
|
resultSet.fetchResult ();
|
|
|
|
var newValues = resultSet.fetchRow ();
|
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
if (op.tables[tableIndex] & Operation.INSERT)
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
|
|
|
for (var i = 0; i < cols.length; i++)
|
|
|
|
if (cols[i].table === tableIndex)
|
|
|
|
{
|
|
|
|
row[i] = newValues[j++];
|
|
|
|
updatedCols.push (i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (var i = 0; i < cols.length; i++)
|
|
|
|
if (cols[i].table === tableIndex
|
|
|
|
&& op.oldValues[i] !== undefined)
|
|
|
|
{
|
|
|
|
row[i] = newValues[j++];
|
|
|
|
updatedCols.push (i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('row-updated', row.index, updatedCols);
|
2015-03-19 19:36:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resultSet.fetchResult ();
|
|
|
|
|
2015-07-10 12:30:08 +00:00
|
|
|
// if (isOperation)
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('operations-done');
|
2015-03-19 19:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Undoes all unsaved changes made to the model.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-03-19 19:36:11 +00:00
|
|
|
,reverseOperations: function ()
|
|
|
|
{
|
2016-01-07 12:58:29 +00:00
|
|
|
for (var i = 0; i < this._operations.length; i++)
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
2016-01-07 12:58:29 +00:00
|
|
|
var op = this._operations[i];
|
2015-03-19 19:36:11 +00:00
|
|
|
var row = op.row;
|
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
if (op.type & Operation.DELETE
|
|
|
|
&& !(op.type & Operation.INSERT))
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
2015-03-27 19:10:49 +00:00
|
|
|
this.data.splice (row.index, 0, row);
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('row-inserted', row.index);
|
2015-03-19 19:36:11 +00:00
|
|
|
}
|
2016-09-26 09:28:47 +00:00
|
|
|
else if (op.type & Operation.UPDATE)
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('row-updated-before', row.index);
|
2015-03-19 19:36:11 +00:00
|
|
|
|
|
|
|
var updatedCols = [];
|
|
|
|
var cols = this.columns;
|
|
|
|
|
|
|
|
for (var i = 0; i < cols.length; i++)
|
|
|
|
if (op.oldValues[i] !== undefined)
|
|
|
|
{
|
|
|
|
row[i] = op.oldValues[i];
|
|
|
|
updatedCols.push (i);
|
|
|
|
}
|
|
|
|
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('row-updated', row.index, updatedCols);
|
2015-03-19 19:36:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._resetOperations ();
|
2015-03-27 19:10:49 +00:00
|
|
|
this._refreshRowIndexes (0);
|
2015-03-19 19:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,_resetOperations: function ()
|
|
|
|
{
|
2016-01-07 12:58:29 +00:00
|
|
|
this._operations = [];
|
|
|
|
this._operationsMap = {};
|
2015-03-19 19:36:11 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
/*
|
|
|
|
* Function used to sort the model ascending.
|
|
|
|
*/
|
|
|
|
,sortFunctionAsc: function (column, a, b)
|
2015-03-19 19:36:11 +00:00
|
|
|
{
|
2015-03-27 19:10:49 +00:00
|
|
|
if (a[column] < b[column])
|
|
|
|
return -1;
|
|
|
|
else if (a[column] > b[column])
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-03-27 19:10:49 +00:00
|
|
|
* Function used to sort the model descending.
|
2015-01-23 13:09:30 +00:00
|
|
|
*/
|
2015-03-27 19:10:49 +00:00
|
|
|
,sortFunctionDesc: function (column, a, b)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-27 19:10:49 +00:00
|
|
|
if (a[column] > b[column])
|
2015-01-23 13:09:30 +00:00
|
|
|
return -1;
|
2015-03-27 19:10:49 +00:00
|
|
|
else if (a[column] < b[column])
|
2015-01-23 13:09:30 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-11-17 10:34:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Orders the model by the specified column name.
|
|
|
|
*
|
|
|
|
* @param {integer} column The column name
|
2016-09-26 09:28:47 +00:00
|
|
|
* @param {SortWay} way The sort way
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-11-17 10:34:33 +00:00
|
|
|
,sortByName: function (columnName, way)
|
|
|
|
{
|
2016-01-07 12:58:29 +00:00
|
|
|
this._requestedSortIndex = -1;
|
|
|
|
this._requestedSortName = columnName;
|
|
|
|
|
2015-11-17 10:34:33 +00:00
|
|
|
var index = this.getColumnIndex (columnName);
|
|
|
|
|
|
|
|
if (index != -1)
|
2016-01-07 12:58:29 +00:00
|
|
|
this._sort (index, way);
|
2015-11-17 10:34:33 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Orders the model by the specified column.
|
|
|
|
*
|
2015-03-27 19:10:49 +00:00
|
|
|
* @param {integer} column The column index
|
2016-09-26 09:28:47 +00:00
|
|
|
* @param {SortWay} way The sort way
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-03-19 19:36:11 +00:00
|
|
|
,sort: function (column, way)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2016-01-07 12:58:29 +00:00
|
|
|
this._requestedSortIndex = column;
|
|
|
|
this._requestedSortName = null;
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
if (!this.checkColExists (column))
|
|
|
|
return;
|
|
|
|
|
2016-01-07 12:58:29 +00:00
|
|
|
this._sort (column, way);
|
|
|
|
}
|
|
|
|
|
|
|
|
,_sort: function (column, way)
|
|
|
|
{
|
2016-09-26 09:28:47 +00:00
|
|
|
this._setStatus (Status.LOADING);
|
2016-01-07 12:58:29 +00:00
|
|
|
this._realSort (column, way);
|
2016-09-26 09:28:47 +00:00
|
|
|
this._setStatus (Status.READY);
|
2016-01-07 12:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,_realSort: function (column, way)
|
|
|
|
{
|
|
|
|
if (column !== this._sortColumn)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2016-09-26 09:28:47 +00:00
|
|
|
if (way === SortWay.DESC)
|
2015-03-27 19:10:49 +00:00
|
|
|
var sortFunction = this.sortFunctionDesc;
|
|
|
|
else
|
|
|
|
var sortFunction = this.sortFunctionAsc;
|
2016-01-07 12:58:29 +00:00
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
this.data.sort (sortFunction.bind (this, column));
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2016-01-07 12:58:29 +00:00
|
|
|
else if (way !== this._sortWay)
|
2015-01-23 13:09:30 +00:00
|
|
|
this.data.reverse ();
|
2016-01-07 12:58:29 +00:00
|
|
|
|
|
|
|
this._sortColumn = column;
|
|
|
|
this._sortWay = way;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
this._refreshRowIndexes (0);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-02-08 15:38:38 +00:00
|
|
|
/**
|
|
|
|
* Builds an internal hash index for the specified column, this speeds
|
2015-03-27 19:10:49 +00:00
|
|
|
* significantly searches on that column, specially when model has a lot of
|
|
|
|
* rows.
|
|
|
|
*
|
|
|
|
* FIXME: Not fully implemented.
|
2015-02-08 15:38:38 +00:00
|
|
|
*
|
|
|
|
* @param {String} column The column name
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-02-08 15:38:38 +00:00
|
|
|
,indexColumn: function (column)
|
|
|
|
{
|
2016-01-07 12:58:29 +00:00
|
|
|
this._requestedIndexes[column] = true;
|
2015-02-08 15:38:38 +00:00
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
if (this._status === Status.READY)
|
2015-03-19 19:36:11 +00:00
|
|
|
this._buildIndex (column);
|
2015-02-08 15:38:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
,_buildIndex: function (column)
|
2015-02-08 15:38:38 +00:00
|
|
|
{
|
|
|
|
var columnIndex = this.getColumnIndex (column);
|
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
if (columnIndex !== -1)
|
2015-02-08 15:38:38 +00:00
|
|
|
{
|
|
|
|
var index = {};
|
|
|
|
var data = this.data;
|
|
|
|
|
|
|
|
switch (this.columns[columnIndex].type)
|
|
|
|
{
|
2016-09-26 09:28:47 +00:00
|
|
|
case Connection.Type.TIMESTAMP:
|
|
|
|
case Connection.Type.DATE_TIME:
|
|
|
|
case Connection.Type.DATE:
|
2015-02-08 15:38:38 +00:00
|
|
|
for (var i = 0; i < data.length; i++)
|
|
|
|
index[data[i][columnIndex].toString ()] = i;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
for (var i = 0; i < data.length; i++)
|
|
|
|
index[data[i][columnIndex]] = i;
|
|
|
|
}
|
|
|
|
|
2016-01-07 12:58:29 +00:00
|
|
|
this._indexes[columnIndex] = index;
|
2015-02-08 15:38:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
/**
|
|
|
|
* Searchs a value on the model and returns the row index of the first
|
|
|
|
* ocurrence.
|
2015-03-27 19:10:49 +00:00
|
|
|
* If an index have been built on that column, it will be used, for more
|
|
|
|
* information see the indexColumn() method.
|
2015-01-23 13:09:30 +00:00
|
|
|
*
|
|
|
|
* @param {String} column The column name
|
|
|
|
* @param {Object} value The value to search
|
|
|
|
* @return {integer} The column index
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
,search: function (column, value)
|
|
|
|
{
|
|
|
|
var index = this.getColumnIndex (column);
|
|
|
|
return this.searchByIndex (index, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Searchs a value on the model and returns the row index of the first
|
|
|
|
* ocurrence.
|
|
|
|
*
|
|
|
|
* @param {integer} col The column index
|
|
|
|
* @param {Object} value The value to search
|
|
|
|
* @return {integer} The column index
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
,searchByIndex: function (col, value)
|
|
|
|
{
|
|
|
|
if (!this.checkColExists (col))
|
|
|
|
return -1;
|
2015-02-08 15:38:38 +00:00
|
|
|
|
|
|
|
if (value)
|
|
|
|
switch (this.columns[col].type)
|
|
|
|
{
|
2016-09-26 09:28:47 +00:00
|
|
|
case Connection.Type.BOOLEAN:
|
2015-02-08 15:38:38 +00:00
|
|
|
value = !!value;
|
|
|
|
break;
|
2016-09-26 09:28:47 +00:00
|
|
|
case Connection.Type.INTEGER:
|
2015-02-08 15:38:38 +00:00
|
|
|
value = parseInt (value);
|
|
|
|
break;
|
2016-09-26 09:28:47 +00:00
|
|
|
case Connection.Type.DOUBLE:
|
2015-02-08 15:38:38 +00:00
|
|
|
value = parseFloat (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
value = value.toString ();
|
|
|
|
}
|
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
// Searchs the value using an internal index.
|
|
|
|
|
2016-01-07 12:58:29 +00:00
|
|
|
var index = this._indexes[col];
|
2015-02-08 15:38:38 +00:00
|
|
|
|
|
|
|
if (index)
|
|
|
|
{
|
2015-02-17 11:48:53 +00:00
|
|
|
if (index[value] !== undefined)
|
2015-02-08 15:38:38 +00:00
|
|
|
return index[value];
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2015-03-27 19:10:49 +00:00
|
|
|
|
|
|
|
// Searchs the value using a loop.
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
var data = this.data;
|
|
|
|
|
|
|
|
switch (this.columns[col].type)
|
|
|
|
{
|
2016-09-26 09:28:47 +00:00
|
|
|
case Connection.Type.TIMESTAMP:
|
|
|
|
case Connection.Type.DATE_TIME:
|
|
|
|
case Connection.Type.DATE:
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
for (var i = 0; i < data.length; i++)
|
2016-10-30 22:48:18 +00:00
|
|
|
if (value === data[i][col].toString ())
|
2015-01-23 13:09:30 +00:00
|
|
|
return i;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
for (var i = 0; i < data.length; i++)
|
|
|
|
if (value === data[i][col])
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
,_setStatus: function (status)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
this._status = status;
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('status-changed', status);
|
|
|
|
this.emit ('status-changed-after', status);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
,_createTarget: function (tableIndex)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-19 19:36:11 +00:00
|
|
|
var table = this.tables[tableIndex];
|
2015-03-31 15:14:44 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
return new Sql.Table
|
|
|
|
({
|
|
|
|
name: table.orgname
|
|
|
|
,schema: table.schema
|
|
|
|
});
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
,_createWhere: function (tableIndex, op, useOldValues)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-19 19:36:11 +00:00
|
|
|
var where = new Sql.Operation ({type: Sql.Operation.Type.AND});
|
|
|
|
var pks = this.tables[tableIndex].pks;
|
|
|
|
|
|
|
|
if (pks.length === 0)
|
|
|
|
return null;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
for (var i = 0; i < pks.length; i++)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-19 19:36:11 +00:00
|
|
|
var col = pks[i];
|
|
|
|
var column = this.columns[col];
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
var equalOp = new Sql.Operation ({type: Sql.Operation.Type.EQUAL});
|
2015-03-19 19:36:11 +00:00
|
|
|
equalOp.exprs.add (new Sql.Field ({name: column.orgname}));
|
|
|
|
where.exprs.add (equalOp);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
var pkValue = null;
|
|
|
|
|
|
|
|
if (useOldValues && op.oldValues
|
|
|
|
&& op.oldValues[col] !== undefined)
|
|
|
|
pkValue = op.oldValues[col];
|
2015-01-23 13:09:30 +00:00
|
|
|
else
|
2015-03-19 19:36:11 +00:00
|
|
|
pkValue = op.row[col];
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
if (pkValue)
|
|
|
|
equalOp.exprs.add (new Sql.Value ({value: pkValue}));
|
2016-09-26 09:28:47 +00:00
|
|
|
else if (column.flags & Connection.Flag.AI && !useOldValues)
|
2016-10-04 15:27:49 +00:00
|
|
|
equalOp.exprs.add (new Sql.Function ({name: 'LAST_INSERT_ID'}));
|
2015-03-19 19:36:11 +00:00
|
|
|
else
|
|
|
|
return null;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
return where;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
,_createOperation: function (rowIndex)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2016-01-07 12:58:29 +00:00
|
|
|
var op = this._operationsMap[rowIndex];
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
if (!op)
|
|
|
|
{
|
|
|
|
op = {
|
|
|
|
type: 0,
|
|
|
|
row: this.data[rowIndex]
|
|
|
|
};
|
2016-01-07 12:58:29 +00:00
|
|
|
this._operations.push (op);
|
|
|
|
this._operationsMap[rowIndex] = op;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-02-08 15:38:38 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
return op;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
/**
|
|
|
|
* Overrides information about a table and its columns. If a parameter is
|
|
|
|
* not provided, the original will be preserved. This method should be used
|
|
|
|
* primarily to avoid the mysql bug that causes this information will not
|
|
|
|
* be set correctly.
|
|
|
|
* For more information see the following links:
|
|
|
|
* - https://bugs.mysql.com/bug.php?id=44660
|
|
|
|
* - https://bugs.mysql.com/bug.php?id=26894
|
|
|
|
*
|
|
|
|
* @param {String} table The table alias
|
|
|
|
* @param {String} orgtable The original table name
|
|
|
|
* @param {String} schema The original table schema
|
|
|
|
* @param {Array} pks Array with the names of primary keys
|
|
|
|
* @param {String} ai The autoincrement column name
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-03-27 19:10:49 +00:00
|
|
|
,setInfo: function (table, orgname, schema, pks, ai)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-27 19:10:49 +00:00
|
|
|
if (!this.tableInfo)
|
|
|
|
this.tableInfo = {};
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
this.tableInfo[table] =
|
2015-03-31 15:14:44 +00:00
|
|
|
{
|
2015-03-27 19:10:49 +00:00
|
|
|
orgname: orgname,
|
|
|
|
schema: schema,
|
|
|
|
pks: pks,
|
|
|
|
ai: ai
|
2015-03-31 15:14:44 +00:00
|
|
|
};
|
2015-03-27 19:10:49 +00:00
|
|
|
|
|
|
|
this._repairColumns ();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
,_repairColumns: function ()
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-27 19:10:49 +00:00
|
|
|
// Repairs wrong table info
|
2015-03-31 15:14:44 +00:00
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
if (this.tableInfo && this.tables)
|
|
|
|
for (var i = 0; i < this.tables.length; i++)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-27 19:10:49 +00:00
|
|
|
var table = this.tables[i];
|
|
|
|
var tableInfo = this.tableInfo[table.name];
|
2015-03-31 15:14:44 +00:00
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
if (!tableInfo)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
table.orgname = tableInfo.orgname;
|
2015-03-31 15:14:44 +00:00
|
|
|
table.schema = tableInfo.schema;
|
2015-03-27 19:10:49 +00:00
|
|
|
|
|
|
|
if (tableInfo.pks)
|
|
|
|
{
|
|
|
|
table.pks = [];
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-03-27 19:10:49 +00:00
|
|
|
for (var j = 0; j < tableInfo.pks.length; j++)
|
|
|
|
{
|
|
|
|
var colIndex = this.getColumnIndex (tableInfo.pks[j]);
|
2015-12-10 23:24:14 +00:00
|
|
|
|
|
|
|
if (colIndex !== -1)
|
|
|
|
table.pks.push (colIndex);
|
|
|
|
else
|
|
|
|
console.warn ('Db.Model: Can\'t repair primary key: `%s`.`%s`'
|
|
|
|
,tableInfo.orgname
|
|
|
|
,tableInfo.pks[j]
|
|
|
|
);
|
2015-03-27 19:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tableInfo.ai)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-03-27 19:10:49 +00:00
|
|
|
var colIndex = this.getColumnIndex (tableInfo.ai);
|
2015-12-10 23:24:14 +00:00
|
|
|
|
|
|
|
if (colIndex !== -1)
|
2016-09-26 09:28:47 +00:00
|
|
|
this.columns[colIndex].flags |= Connection.Flag.AI;
|
2015-12-10 23:24:14 +00:00
|
|
|
else
|
|
|
|
console.warn ('Db.Model: Can\'t repair autoincrement column: `%s`.`%s`'
|
|
|
|
,tableInfo.orgname
|
|
|
|
,tableInfo.ai
|
|
|
|
);
|
2015-03-27 19:10:49 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|