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

105 lines
1.8 KiB
JavaScript
Raw Normal View History

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
/**
* The equivalent of a SQL value.
**/
2020-05-03 20:35:24 +00:00
module.exports = new Class({
2016-09-26 09:28:47 +00:00
Extends: Expr
,Tag: 'sql-value'
,Properties:
{
/**
* The master param.
**/
param:
{
type: Vn.Param
2020-05-03 20:35:24 +00:00
,set: function(x) {
this.link({_param: x}, {'changed': this.onParamChange});
this.onParamChange();
}
2020-05-03 20:35:24 +00:00
,get: function() {
return this._param;
}
},
/**
* The value.
**/
value:
{
2015-07-03 05:49:45 +00:00
type: String
2020-05-03 20:35:24 +00:00
,set: function(x) {
if (Vn.Value.compare(x, this._value))
return;
if (x instanceof Date)
2020-05-03 20:35:24 +00:00
x = x.clone();
this._value = x;
2020-05-03 20:35:24 +00:00
if (this._param && !this.paramLock) {
this.paramLock = true;
this._param.value = x;
this.paramLock = false;
}
2020-05-03 20:35:24 +00:00
this.signalEmit('changed');
}
2020-05-03 20:35:24 +00:00
,get: function() {
return this._value;
}
}
}
,_value: undefined
,_param: null
2020-05-03 20:35:24 +00:00
,regexp: new RegExp('(\\\\)|\'', 'g')
,paramLock: false
2020-05-03 20:35:24 +00:00
,onParamChange: function() {
2015-07-15 13:39:07 +00:00
if (this.paramLock || !this._param)
return;
this.paramLock = true;
this.value = this._param.value;
this.paramLock = false;
}
2020-05-03 20:35:24 +00:00
,isReady: function() {
2015-07-17 14:34:42 +00:00
return this._value !== undefined;
}
2020-05-03 20:35:24 +00:00
,replaceFunc: function(token) {
switch (token) {
case '\\': return '\\\\';
case '\'': return '\\\'';
}
return token;
}
2020-05-03 20:35:24 +00:00
,render: function() {
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:
if (v instanceof Date) {
// TODO: Read time zone from db configuration
var tz = {timeZone: 'Europe/Madrid'};
var localeDate = new Date(v.toLocaleString('en-US', tz));
return Vn.Date.strftime(localeDate, '\'%Y-%m-%d\'');
} else {
return 'NULL';
2020-05-03 20:35:24 +00:00
}
}
}
});