2016-09-26 09:28:47 +00:00
|
|
|
|
2020-05-03 20:35:24 +00:00
|
|
|
var Expr = require('./expr');
|
2016-09-26 09:28:47 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
/**
|
|
|
|
* The equivalent of a SQL value.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2020-05-03 20:35:24 +00:00
|
|
|
module.exports = new Class({
|
2022-05-30 01:30:33 +00:00
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
Extends: Expr
|
2022-05-30 01:30:33 +00:00
|
|
|
,Implements: Vn.ParamIface
|
2015-01-23 13:09:30 +00:00
|
|
|
,Tag: 'sql-value'
|
2022-05-28 19:27:36 +00:00
|
|
|
,Properties: {
|
2022-05-30 01:30:33 +00:00
|
|
|
value: {
|
|
|
|
type: null
|
|
|
|
,set: function(x) {
|
|
|
|
this._setValue(x);
|
|
|
|
}
|
|
|
|
,get: function() {
|
|
|
|
return this._value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
type: {
|
|
|
|
type: Type
|
|
|
|
,set: function(x) {
|
|
|
|
this._setType(x);
|
|
|
|
}
|
|
|
|
,get: function() {
|
|
|
|
return this._type;
|
|
|
|
}
|
|
|
|
},
|
2022-05-28 19:27:36 +00:00
|
|
|
param: {
|
2022-05-30 01:30:33 +00:00
|
|
|
type: Vn.ParamIface
|
2020-05-03 20:35:24 +00:00
|
|
|
,set: function(x) {
|
2022-05-30 01:30:33 +00:00
|
|
|
this._setParam(x);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2020-05-03 20:35:24 +00:00
|
|
|
,get: function() {
|
2015-01-23 13:09:30 +00:00
|
|
|
return this._param;
|
|
|
|
}
|
|
|
|
},
|
2022-05-30 01:30:33 +00:00
|
|
|
lot: {
|
|
|
|
type: Vn.LotIface
|
|
|
|
,set: function(x) {
|
|
|
|
this._setLot(x);
|
|
|
|
}
|
|
|
|
,get: function() {
|
|
|
|
return this._lot;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
name: {
|
2015-07-03 05:49:45 +00:00
|
|
|
type: String
|
2020-05-03 20:35:24 +00:00
|
|
|
,set: function(x) {
|
2022-05-30 01:30:33 +00:00
|
|
|
this._name = x;
|
|
|
|
this._onLotChange();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2020-05-03 20:35:24 +00:00
|
|
|
,get: function() {
|
2022-05-30 01:30:33 +00:00
|
|
|
return this._name;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
oneWay: {
|
|
|
|
type: Boolean
|
|
|
|
,set: function(x) {
|
|
|
|
this._oneWay = x;
|
|
|
|
}
|
|
|
|
,get: function() {
|
|
|
|
return this._oneWay;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2022-10-03 12:49:41 +00:00
|
|
|
},
|
|
|
|
oneTime: {
|
|
|
|
type: Boolean
|
|
|
|
,set: function(x) {
|
|
|
|
this._oneTime = x;
|
|
|
|
}
|
|
|
|
,get: function() {
|
|
|
|
return this._oneTime;
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 20:35:24 +00:00
|
|
|
,regexp: new RegExp('(\\\\)|\'', 'g')
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2020-05-03 20:35:24 +00:00
|
|
|
,isReady: function() {
|
2015-07-17 14:34:42 +00:00
|
|
|
return this._value !== undefined;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 20:35:24 +00:00
|
|
|
,replaceFunc: function(token) {
|
|
|
|
switch (token) {
|
2015-01-23 13:09:30 +00:00
|
|
|
case '\\': return '\\\\';
|
|
|
|
case '\'': return '\\\'';
|
|
|
|
}
|
|
|
|
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2020-05-03 20:35:24 +00:00
|
|
|
,render: function() {
|
2015-01-23 13:09:30 +00:00
|
|
|
var v = this._value;
|
|
|
|
|
2020-05-03 20:35:24 +00:00
|
|
|
switch (typeof v) {
|
|
|
|
case 'number':
|
|
|
|
return v;
|
|
|
|
case 'boolean':
|
|
|
|
return (v) ? 'TRUE' : 'FALSE';
|
|
|
|
case 'string':
|
|
|
|
return "'" + v.replace(this.regexp, this.replaceFunc) + "'";
|
|
|
|
default:
|
2021-01-23 16:15:02 +00:00
|
|
|
if (v instanceof Date) {
|
|
|
|
if (!isNaN(v.getTime())) {
|
|
|
|
var unixTime = parseInt(fixTz(v).getTime() / 1000);
|
|
|
|
return 'DATE(FROM_UNIXTIME('+ unixTime +'))';
|
|
|
|
} else
|
|
|
|
return '0000-00-00'
|
|
|
|
} else
|
2015-01-23 13:09:30 +00:00
|
|
|
return 'NULL';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-05-04 19:55:18 +00:00
|
|
|
|
2021-01-23 16:15:02 +00:00
|
|
|
// TODO: Read time zone from db configuration
|
|
|
|
var tz = {timeZone: 'Europe/Madrid'};
|
|
|
|
var isLocal = Intl
|
|
|
|
.DateTimeFormat()
|
|
|
|
.resolvedOptions()
|
|
|
|
.timeZone == tz.timeZone;
|
|
|
|
|
2020-05-04 19:55:18 +00:00
|
|
|
function fixTz(date) {
|
2021-01-23 16:15:02 +00:00
|
|
|
if (isLocal) return date;
|
|
|
|
|
|
|
|
var hasTime = date.getHours()
|
|
|
|
|| date.getMinutes()
|
|
|
|
|| date.getSeconds()
|
|
|
|
|| date.getMilliseconds();
|
|
|
|
|
|
|
|
if (!hasTime) {
|
|
|
|
date = new Date(date.getTime());
|
|
|
|
date.setHours(12, 0, 0, 0);
|
|
|
|
}
|
2020-05-04 19:55:18 +00:00
|
|
|
|
2021-01-23 16:15:02 +00:00
|
|
|
return date;
|
2020-05-04 19:55:18 +00:00
|
|
|
}
|