0
1
Fork 0
hedera-web-mindshore/js/sql/value.js

130 lines
2.1 KiB
JavaScript

var Expr = require('./expr');
/**
* The equivalent of a SQL value.
*/
module.exports = new Class({
Extends: Expr
,Implements: Vn.ParamIface
,Tag: 'sql-value'
,Properties: {
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;
}
},
param: {
type: Vn.ParamIface
,set: function(x) {
this._setParam(x);
}
,get: function() {
return this._param;
}
},
lot: {
type: Vn.LotIface
,set: function(x) {
this._setLot(x);
}
,get: function() {
return this._lot;
}
},
name: {
type: String
,set: function(x) {
this._name = x;
this._onLotChange();
}
,get: function() {
return this._name;
}
},
oneWay: {
type: Boolean
,set: function(x) {
this._oneWay = x;
}
,get: function() {
return this._oneWay;
}
}
}
,regexp: new RegExp('(\\\\)|\'', 'g')
,isReady: function() {
return this._value !== undefined;
}
,replaceFunc: function(token) {
switch (token) {
case '\\': return '\\\\';
case '\'': return '\\\'';
}
return token;
}
,render: function() {
var v = this._value;
switch (typeof v) {
case 'number':
return v;
case 'boolean':
return (v) ? 'TRUE' : 'FALSE';
case 'string':
return "'" + v.replace(this.regexp, this.replaceFunc) + "'";
default:
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
return 'NULL';
}
}
});
// TODO: Read time zone from db configuration
var tz = {timeZone: 'Europe/Madrid'};
var isLocal = Intl
.DateTimeFormat()
.resolvedOptions()
.timeZone == tz.timeZone;
function fixTz(date) {
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);
}
return date;
}