alasql
Version:
AlaSQL.js - JavaScript SQL database library for relational and graph data manipulation with support of localStorage, IndexedDB, and Excel
153 lines (141 loc) • 7 kB
JavaScript
/*
//
// CAST and CONVERT functions
// Date: 03.11.2014
// (c) 2014, Andrey Gershun
//
*/
yy.Convert = function(params) { return yy.extend(this, params); };
yy.Convert.prototype.toString = function() {
var s = 'CONVERT(';
s += this.dbtypeid;
if(typeof this.dbsize != 'undefined') {
s += '('+this.dbsize;
if(this.dbprecision) s += ','+dbprecision;
s += ')';
}
s += ','+this.expression.toString();
if(this.style) s += ','+this.style;
s += ')';
return s;
};
yy.Convert.prototype.toJavaScript = function(context, tableid, defcols) {
// if(this.style) {
return 'alasql.stdfn.CONVERT('+this.expression.toJavaScript(context, tableid, defcols)
+',{dbtypeid:"'+this.dbtypeid+'",dbsize:'+this.dbsize+',style:'+
this.style+'})';
// }
/*
if(this.dbtypeid == 'INT') {
return '(('+this.expression.toJavaScript(context, tableid, defcols)+')|0)';
} if(this.dbtypeid == 'STRING') {
return '(""+'+this.expression.toJavaScript(context, tableid, defcols)+')';
} if(this.dbtypeid == 'NUMBER') {
return '(+('+this.expression.toJavaScript(context, tableid, defcols)+'))';
} if(this.dbtypeid == 'DATE') {
if(alasql.options.datetimeformat == 'javascript') {
return '(new Date('+this.expression.toJavaScript(context, tableid, defcols)+'))';
} else if(alasql.options.datetimeformat == 'sql') {
return this.expression.toJavaScript(context, tableid, defcols);
}
} if(this.dbtypeid == 'DATETIME') {
if(alasql.options.datetimeformat == 'javascript') {
return '(new Date('+this.expression.toJavaScript(context, tableid, defcols)+'))';
} else if(alasql.options.datetimeformat == 'sql') {
return this.expression.toJavaScript(context, tableid, defcols);
}
} else {
};
*/
throw new Error('There is not such type conversion for '+this.toString());
};
/**
Convert one type to another
*/
alasql.stdfn.CONVERT = function(value, args) {
var val = value;
// console.log(args);
if(args.style) {
// TODO 9,109, 20,120,21,121,126,130,131 conversions
var t;
if(/\d{8}/.test(val)) t = new Date(+val.substr(0,4),+val.substr(4,2)-1,+val.substr(6,2));
else t = new Date(val);
if(args.style == 1) { // mm/dd/yy
val = ("0"+(t.getMonth()+1)).substr(-2)+'/'+("0"+t.getDate()).substr(-2)+'/'+("0"+t.getYear()).substr(-2);
} else if(args.style == 2) { // yy.mm.dd
val = ("0"+t.getYear()).substr(-2)+'.'+("0"+(t.getMonth()+1)).substr(-2)+'.'+("0"+t.getDate()).substr(-2);
} else if(args.style == 3) { // dd/mm/yy
val = ("0"+t.getDate()).substr(-2)+'/'+("0"+(t.getMonth()+1)).substr(-2)+'/'+("0"+t.getYear()).substr(-2);
} else if(args.style == 4) { // dd.mm.yy
val = ("0"+t.getDate()).substr(-2)+'.'+("0"+(t.getMonth()+1)).substr(-2)+'.'+("0"+t.getYear()).substr(-2);
} else if(args.style == 5) { // dd-mm-yy
val = ("0"+t.getDate()).substr(-2)+'-'+("0"+(t.getMonth()+1)).substr(-2)+'-'+("0"+t.getYear()).substr(-2);
} else if(args.style == 6) { // dd mon yy
val = ("0"+t.getDate()).substr(-2)+' '+t.toString().substr(4,3).toLowerCase()+' '+("0"+t.getYear()).substr(-2);
} else if(args.style == 7) { // Mon dd,yy
val = t.toString().substr(4,3)+' '+("0"+t.getDate()).substr(-2)+','+("0"+t.getYear()).substr(-2);
} else if(args.style == 8) { // hh:mm:ss
val = ("0"+t.getHours()).substr(-2)+':'+("0"+(t.getMinutes()+1)).substr(-2)+':'+("0"+t.getSeconds()).substr(-2);
} else if(args.style == 10) { // mm-dd-yy
val = ("0"+(t.getMonth()+1)).substr(-2)+'-'+("0"+t.getDate()).substr(-2)+'-'+("0"+t.getYear()).substr(-2);
} else if(args.style == 11) { // yy/mm/dd
val = ("0"+t.getYear()).substr(-2)+'/'+("0"+(t.getMonth()+1)).substr(-2)+'/'+("0"+t.getDate()).substr(-2);
} else if(args.style == 12) { // yymmdd
val = ("0"+t.getYear()).substr(-2)+("0"+(t.getMonth()+1)).substr(-2)+("0"+t.getDate()).substr(-2);
} else if(args.style == 101) { // mm/dd/yy
val = ("0"+(t.getMonth()+1)).substr(-2)+'/'+("0"+t.getDate()).substr(-2)+'/'+t.getFullYear();
} else if(args.style == 102) { // yy.mm.dd
val = t.getFullYear()+'.'+("0"+(t.getMonth()+1)).substr(-2)+'.'+("0"+t.getDate()).substr(-2);
} else if(args.style == 103) { // dd/mm/yy
val = ("0"+t.getDate()).substr(-2)+'/'+("0"+(t.getMonth()+1)).substr(-2)+'/'+t.getFullYear();
} else if(args.style == 104) { // dd.mm.yy
val = ("0"+t.getDate()).substr(-2)+'.'+("0"+(t.getMonth()+1)).substr(-2)+'.'+t.getFullYear();
} else if(args.style == 105) { // dd-mm-yy
val = ("0"+t.getDate()).substr(-2)+'-'+("0"+(t.getMonth()+1)).substr(-2)+'-'+t.getFullYear();
} else if(args.style == 106) { // dd mon yy
val = ("0"+t.getDate()).substr(-2)+' '+t.toString().substr(4,3).toLowerCase()+' '+t.getFullYear();
} else if(args.style == 107) { // Mon dd,yy
val = t.toString().substr(4,3)+' '+("0"+t.getDate()).substr(-2)+','+t.getFullYear();
} else if(args.style == 108) { // hh:mm:ss
val = ("0"+t.getHours()).substr(-2)+':'+("0"+(t.getMinutes()+1)).substr(-2)+':'+("0"+t.getSeconds()).substr(-2);
} else if(args.style == 110) { // mm-dd-yy
val = ("0"+(t.getMonth()+1)).substr(-2)+'-'+("0"+t.getDate()).substr(-2)+'-'+t.getFullYear();
} else if(args.style == 111) { // yy/mm/dd
val = t.getFullYear()+'/'+("0"+(t.getMonth()+1)).substr(-2)+'/'+("0"+t.getDate()).substr(-2);
} else if(args.style == 112) { // yymmdd
val = t.getFullYear()+("0"+(t.getMonth()+1)).substr(-2)+("0"+t.getDate()).substr(-2);
} else {
throw new Error('The CONVERT style '+args.style+' is not realized yet.');
}
};
if(args.dbtypeid == 'Date') {
return new Date(val);
} else if(args.dbtypeid.toUpperCase() == 'DATE') {
var d = new Date(val);
var s = d.getFullYear()+"."+("0"+(d.getMonth()+1)).substr(-2)+"."+("0"+d.getDate()).substr(-2);
return s;
} else if(args.dbtypeid == 'DATETIME') {
var d = new Date(val);
var s = d.getFullYear()+"."+("0"+(d.getMonth()+1)).substr(-2)+"."+("0"+d.getDate()).substr(-2);
s += " "+("0"+d.getHours()).substr(-2)+":"+("0"+d.getMinutes()).substr(-2)+":"+("0"+d.getSeconds()).substr(-2);
s += '.'+("00"+d.getMilliseconds()).substr(-3)
return s;
} else if(args.dbtypeid.toUpperCase() == 'STRING') {
return ""+val;
} else if(args.dbtypeid.toUpperCase() == 'NUMBER' || args.dbtypeid == 'FLOAT') {
return +val;
} else if(args.dbtypeid.toUpperCase() == 'MONEY') {
var m = +val;
return (m|0)+((m*100)%100)/100;
} else if(args.dbtypeid.toUpperCase() == 'BOOLEAN') {
return !!val;
} else if(args.dbtypeid.toUpperCase() == 'INT') {
return val|0;
} else if(args.dbtypeid.toUpperCase() == 'VARCHAR' || args.dbtypeid == 'NVARCHAR') {
if(args.dbsize) return (""+val).substr(0,args.dbsize);
else return ""+val;
} else if(args.dbtypeid.toUpperCase() == 'CHAR' || args.dbtypeid == 'NCHAR') {
return (val+(new Array(args.dbsize+1).join(" "))).substr(0,args.dbsize);
//else return ""+val.substr(0,1);
}
};