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

139 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(x) {
this._setValue(x);
}
,get() {
return this._value;
}
},
type: {
type: Type
,set(x) {
this._setType(x);
}
,get() {
return this._type;
}
},
param: {
type: Vn.ParamIface
,set(x) {
this._setParam(x);
}
,get() {
return this._param;
}
},
lot: {
type: Vn.LotIface
,set(x) {
this._setLot(x);
}
,get() {
return this._lot;
}
},
name: {
type: String
,set(x) {
this._name = x;
this._onLotChange();
}
,get() {
return this._name;
}
},
oneWay: {
type: Boolean
,set(x) {
this._oneWay = x;
}
,get() {
return this._oneWay;
}
},
oneTime: {
type: Boolean
,set(x) {
this._oneTime = x;
}
,get() {
return this._oneTime;
}
}
}
,regexp: new RegExp('(\\\\)|\'', 'g')
,isReady() {
return this._value !== undefined;
}
,replaceFunc(token) {
switch (token) {
case '\\': return '\\\\';
case '\'': return '\\\'';
}
return token;
}
,render() {
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;
}