UNPKG

alasql

Version:

AlaSQL.js - JavaScript SQL database library for relational and graph data manipulation with support of localStorage, IndexedDB, and Excel

348 lines (306 loc) 9 kB
// // into functions // // (c) 2014 Andrey Gershun // alasql.into.SQL = function(filename, opts, data, columns, cb) { var res; if(typeof filename == 'object') { opts = filename; filename = undefined; } var opt = {}; alasql.utils.extend(opt, opts); if(typeof opt.tableid == 'undefined') { throw new Error('Table for INSERT TO is not defined.'); }; var s = ''; if(columns.length == 0) { if(typeof data[0] == "object") { columns = Object.keys(data[0]).map(function(columnid){return {columnid:columnid}}); } else { // What should I do? // columns = [{columnid:"_"}]; } } for(var i=0,ilen=data.length;i<ilen;i++) { s += 'INSERT INTO '+opts.tableid +'('; s += columns.map(function(col){return col.columnid}).join(","); s += ') VALUES ('; s += columns.map(function(col){ var val = data[i][col.columnid]; if(col.typeid) { if(col.typeid == 'STRING' || col.typeid == 'VARCHAR' || col.typeid == 'NVARCHAR' || col.typeid == 'CHAR' || col.typeid == 'NCHAR') { val = "'"+escapeqq(val)+"'"; } } else { if(typeof val == 'string') { val = "'"+escapeqq(val)+"'"; } } return val; }); s += ');\n'; } // if(filename === '') { // res = s; // } else { // res = data.length; res = alasql.utils.saveFile(filename,s); if(cb) res = cb(res); return res; }; alasql.into.HTML = function(selector, opts, data, columns, cb) { var res = 1; if(typeof exports != 'object') { var opt = {}; alasql.utils.extend(opt, opts); var sel = document.querySelector(selector); if(!sel) { throw new Error('Selected HTML element is not found'); }; if(columns.length == 0) { if(typeof data[0] == "object") { columns = Object.keys(data[0]).map(function(columnid){return {columnid:columnid}}); } else { // What should I do? // columns = [{columnid:"_"}]; } } var tbe = document.createElement('table'); var thead = document.createElement('thead'); tbe.appendChild(thead); if(opt.headers) { var tre = document.createElement('tr'); for(var i=0;i<columns.length;i++){ var the = document.createElement('th'); the.textContent = columns[i].columnid; tre.appendChild(the); } thead.appendChild(tre); } var tbody = document.createElement('tbody'); tbe.appendChild(tbody); for(var j=0;j<data.length;j++){ var tre = document.createElement('tr'); for(var i=0;i<columns.length;i++){ var the = document.createElement('td'); the.textContent = data[j][columns[i].columnid]; tre.appendChild(the); } tbody.appendChild(tre); }; alasql.utils.domEmptyChildren(sel); // console.log(tbe,columns); sel.appendChild(tbe); } if(cb) res = cb(res); return res; }; alasql.into.JSON = function(filename, opts, data, columns, cb) { var res = 1; if(typeof filename == 'object') { opts = filename; filename = undefined; } var opt = {}; var s = JSON.stringify(data); res = alasql.utils.saveFile(filename,s); if(cb) res = cb(res); return res; }; alasql.into.TXT = function(filename, opts, data, columns, cb) { // If columns is empty if(columns.length == 0 && data.length > 0) { columns = Object.keys(data[0]).map(function(columnid){return {columnid:columnid}}); }; // If one parameter if(typeof filename == 'object') { opts = filename; filename = undefined; }; var res = data.length; var s = ''; if(data.length > 0) { var key = columns[0].columnid; s += data.map(function(d){ return d[key]; }).join('\n'); } // } else { // if(typeof exports == 'object') { // process.stdout.write(s); // } else { // console.log(s); // }; // } res = alasql.utils.saveFile(filename,s); if(cb) res = cb(res); return res; }; alasql.into.TAB = alasql.into.TSV = function(filename, opts, data, columns, cb) { var opt = {}; alasql.utils.extend(opt, opts); opt.separator = '\t'; return alasql.into.CSV(filename, opt, data, columns, cb); } alasql.into.CSV = function(filename, opts, data, columns, cb) { if(columns.length == 0 && data.length > 0) { columns = Object.keys(data[0]).map(function(columnid){return {columnid:columnid}}); } if(typeof filename == 'object') { opts = filename; filename = undefined; } var opt = {}; opt.separator = ','; opt.quote = '"'; alasql.utils.extend(opt, opts); var res = data.length; var s = ''; if(opt.headers) { s += columns.map(function(col){ return col.columnid; }).join(opt.separator)+'\n'; } data.forEach(function(d, idx){ s += columns.map(function(col){ var s = d[col.columnid]; s = (s+"").replace(new RegExp('\\'+opt.quote,"g"),'""'); if((s+"").indexOf(opt.separator) > -1 || (s+"").indexOf(opt.quote) > -1) s = opt.quote + s + opt.quote; return s; }).join(opt.separator)+'\n'; }); res = alasql.utils.saveFile(filename,s); if(cb) res = cb(res); return res; }; alasql.into.XLSX = function(filename, opts, data, columns, cb) { if(columns.length == 0 && data.length > 0) { columns = Object.keys(data[0]).map(function(columnid){return {columnid:columnid}}); } if(typeof exports == 'object') { var XLSX = require('xlsx'); } else { var XLSX = window.XLSX; }; if(typeof filename == 'object') { opts = filename; filename = undefined; } var opt = {sheetid:'Sheet1',headers:true}; alasql.utils.extend(opt, opts); var res = 1; var wb = {SheetNames:[], Sheets:{}}; // Check overwrite flag if(opt.sourcefilename) { alasql.utils.loadBinaryFile(opt.sourcefilename,!!cb,function(data){ wb = XLSX.read(data,{type:'binary'}); res = doExport(); }); } else { res = doExport(); }; function doExport() { var cells = {}; if(wb.SheetNames.indexOf(opt.sheetid) > -1) { cells = wb.Sheets[opt.sheetid]; } else { wb.SheetNames.push(opt.sheetid); wb.Sheets[opt.sheetid] = {}; cells = wb.Sheets[opt.sheetid]; } var range = "A1"; if(opt.range) range = opt.range; var col0 = alasql.utils.xlscn(range.match(/[A-Z]+/)[0]); var row0 = +range.match(/[0-9]+/)[0]-1; if(wb.Sheets[opt.sheetid]['!ref']) { var rangem = wb.Sheets[opt.sheetid]['!ref']; var colm = alasql.utils.xlscn(rangem.match(/[A-Z]+/)[0]); var rowm = +rangem.match(/[0-9]+/)[0]-1; } else { var colm = 1, rowm = 1; } var colmax = Math.max(col0+columns.length,colm); var rowmax = Math.max(row0+data.length+2,rowm); // console.log(col0,row0); var i = row0+1; wb.Sheets[opt.sheetid]['!ref'] = 'A1:'+alasql.utils.xlsnc(colmax)+(rowmax); // var i = 1; if(opt.headers) { columns.forEach(function(col, idx){ cells[alasql.utils.xlsnc(col0+idx)+""+i] = {v:col.columnid}; }); i++; } for(var j=0;j<data.length;j++) { columns.forEach(function(col, idx){ var cell = {v:data[j][col.columnid]}; if(typeof data[j][col.columnid] == 'number') { cell.t = 'n'; } else if(typeof data[j][col.columnid] == 'string') { cell.t = 's'; } else if(typeof data[j][col.columnid] == 'boolean') { cell.t = 'b'; } else if(typeof data[j][col.columnid] == 'object') { if(data[j][col.columnid] instanceof Date) { cell.t = 'd'; } } cells[alasql.utils.xlsnc(col0+idx)+""+i] = cell; }); i++; } // console.log(wb); // console.log(wb); if(typeof filename == 'undefined') { res = wb; } else { if(typeof exports == 'object') { XLSX.writeFile(wb, filename); } else { var wopts = { bookType:'xlsx', bookSST:false, type:'binary' }; var wbout = XLSX.write(wb,wopts); function s2ab(s) { var buf = new ArrayBuffer(s.length); var view = new Uint8Array(buf); for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; return buf; } /* the saveAs call downloads a file on the local machine */ // saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), '"'+filename+'"') // saveAs(new Blob([s2ab(wbout)],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}), filename) // saveAs(new Blob([s2ab(wbout)],{type:"application/vnd.ms-excel"}), '"'+filename+'"'); if(isIE() == 9) { throw new Error('Cannot save XLSX files in IE9. Please use XLS() export function'); // var URI = 'data:text/plain;charset=utf-8,'; /* var testlink = window.open("about:blank", "_blank"); var s = ''; for(var i=0,ilen=wbout.length;i<ilen;i++) { var ch = wbout.charCodeAt(i); if(i<20) console.log('&#'+ch+';'); s += '&#x'+ch.toString(16)+';'; }; testlink.document.write(s); //fileData has contents for the file testlink.document.close(); testlink.document.execCommand('SaveAs', false, filename); testlink.close(); */ // alert('ie9'); } else { saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), filename); } } } // data.forEach(function(d){ // s += columns.map(function(col){ // return d[col.columnid]; // }).join(opts.separator)+'\n'; // }); // alasql.utils.saveFile(filename,s); if(cb) res = cb(res); return res; }; };