File: components/webfan/webfat/js/ql-engine.js

Recommend this page to a friend!
  Classes of Till Wehowski   µ.Flow   components/webfan/webfat/js/ql-engine.js   Download  
File: components/webfan/webfat/js/ql-engine.js
Role: Class source
Content type: text/plain
Description: Class source
Class: µ.Flow
General purpose library of objects
Author: By
Last change: Update ql-engine.js
bugfix sql DELETE
Date: 7 years ago
Size: 26,117 bytes
 

Contents

Class file image Download
/* webfan (C) Till Wehowski, Webfan.de - All rights reserved. */ 'use strict'; exports = module.exports = { name : 'frdl sql web driver', hooks : /*this[Storage].apply*/{ 'update' : function(ar, dc){ if('undefined'===typeof dc)var dc=this; var doWhereFilter = require('nquery/forked-frdl-1/base/where'); var AstHelper = require('nquery/forked-frdl-1/lib/ast_helper'); var AstReader = AstHelper.Reader; var createBinaryExpr = AstHelper.createBinaryExpr; var ast = ar.getAst(); var res; var _GetRes=function(where){ return doWhereFilter(dc[ast.db][ast.table], where); }; try{ if(ast.where.type){ res=_GetRes(ast.where); }else{ frdl.each(ast.where, function(w, where){ if(where.type){ res=_GetRes(where); } }); } var row; if(0<res.data.length){ while((row = frdl.sql.fetch_row(res)) && row){ var _index =dc[ast.db][ast.table].data.indexOf(row); if(-1===_index)continue; var _row = frdl.clone(dc[ast.db][ast.table].data[_index]); frdl.each(ast.set, function(i, s){ if(s.column && s.value){ _row[dc[ast.db][ast.table].columns.indexOf(s.column)]=s.value.value || s.value; } }); dc[ast.db][ast.table].data[_index] = frdl.clone(_row); } } }catch(err){ throw err; } }, 'fnMap' : function(fnMap, name, context){ if('undefined'===typeof context)var context = require('frdl'); var _map = { //todo: dump() //todo: STDOUT() //todo: download() 'ALERT' : window.alert }; if('function' !== typeof fnMap[name] ){ if('function' === typeof _map[name])fnMap[name]=_map[name]; if('function' !== typeof fnMap[name] && 'function' === typeof context[name] )fnMap[name]=context[name]; if('function' !== typeof fnMap[name] && 'function' === typeof context[name.toLowerCase()])fnMap[name]=context[name.toLowerCase()]; } }, 'select.prepare' : function(ar, DC){ var dc = this; // var dc =DC || this; // var dc = this || DC; var fs = require('fs'); var ast = ar.getAst(); var DB = ar.getDB(); var type = ar.getType(); var tbName = ar.getTableOrig(); // var tbAlias = ar.getTableAlias(); var s = {}; // var alias = {}; s[DB] = []; s[DB].push(tbName); if(1<ast.from.length){ frdl.each(ast.from, function(i, t){ if(t.join){ var db2 = t.db; if('undefined'===typeof s[db2]){ s[db2] = []; } var tbName2 = t.table; var tbAlias2 = t.as; if(-1===s[db2].indexOf(tbName2)){ s[db2].push(tbName2); } // if('undefined'===typeof alias[db2+'.'+tbName2]){ // alias[db2+'.'+tbName2] = tbAlias2; // } } }); } var K; for(K in s){ frdl.each(s[K], function(i, tn){ var k = K; var tableName = tn; // if('undefined'===typeof dc[k] || 'undefined'===typeof dc[k][tableName]){ try{ process.cwd(frdl.fs5.polyfill.getFiler().fs.root); fs.readFile('1.3.6.1.4.1.37553.8.1.8.8.10.2.1'+frdl.DIRECTORY_SEPARATOR + 'dbs' +frdl.DIRECTORY_SEPARATOR + k +frdl.DIRECTORY_SEPARATOR + tableName +frdl.DIRECTORY_SEPARATOR + 'columns.json', function(colerr, coldata){ if(colerr){ return ; } if('undefined'===typeof dc[k]){ dc[k]={}; } if('undefined'===typeof dc[k][tableName]){ dc[k][tableName]={}; } if('undefined'===typeof dc[k][tableName].columns){ try{ dc[k][tableName].columns=JSON.parse(coldata); }catch(err){ console.warn(err); } } }); process.cwd(frdl.fs5.polyfill.getFiler().fs.root); fs.readFile('1.3.6.1.4.1.37553.8.1.8.8.10.2.1'+frdl.DIRECTORY_SEPARATOR + 'dbs' +frdl.DIRECTORY_SEPARATOR + k +frdl.DIRECTORY_SEPARATOR + tableName +frdl.DIRECTORY_SEPARATOR + 'KEYINDEX.json', function(ierr, idata){ if(ierr){ return; } if(!ierr && 'undefined'===typeof dc[k][tableName].KEYINDEX){ try{ dc[k][tableName].KEYINDEX = JSON.parse(idata); }catch(err){ console.warn(err); } } }); // dc[k][tableName].columns = JSON.parse(coldata); process.cwd(frdl.fs5.polyfill.getFiler().fs.root); fs.readdir( '1.3.6.1.4.1.37553.8.1.8.8.10.2.1'+frdl.DIRECTORY_SEPARATOR + 'dbs' +frdl.DIRECTORY_SEPARATOR + k +frdl.DIRECTORY_SEPARATOR + tableName +frdl.DIRECTORY_SEPARATOR + 'data' +frdl.DIRECTORY_SEPARATOR, function(_err, dataEntries){ var c =0; if(!_err && dataEntries && dataEntries.length){ frdl.each(dataEntries, function(i, dataFile){ if(!dataFile.isFile)return true; fs.readFile(dataFile, function(errf, TableData){ c++; if(errf){ // console.error(err); // if(0<frdl.debug.mode()) // alert(errf); return; } if('undefined'===typeof dc[k][tableName].data || (!dc[k][tableName].data.length) ){ dc[k][tableName].data= JSON.parse(TableData); } else if(dc[k][tableName].data && dc[k][tableName].data.length && 0 === dc[k][tableName].data.length){ dc[k][tableName].data= JSON.parse(TableData); } // TableData = JSON.parse(TableData); // frdl.each(TableData, function(i, row){ // if(-1=== dc[k][tableName].data.indexOf(row)) dc[k][tableName].data.push(row); // }); DC[k][tableName] = dc[k][tableName]; // if(0<frdl.debug.mode()) // alert(tableName + ' ' +JSON.stringify(DC[k][tableName].data)); }); }); } }); }catch(err){ if(0<frdl.debug.mode()){ console.log('(a)sql notice: ' +err); } //} } }); } }, 'rollback' : function(s, level){ }, 'delete' : function(sQL, words, words2){ var args = //Array.prototype.slice.call(arguments) [sQL, words, words2] , THAT = this; args[1].shift(); args[1].shift(); var _db = args[1].shift(); _db = frdl.explode('.', _db); var db = _db[0]; var table = _db[1]; var row, _sql = 'SELECT * FROM '+db+'.'+table+' '+args[1].join(' '); if(0<frdl.debug.mode())console.log(_sql); if(0<frdl.debug.mode()) console.log(db); if(0<frdl.debug.mode()) console.log(table); // var row, _sql = 'SELECT '+words2.join(' '); frdl.sql.query(_sql, function(err, resultset) { if (err) { console.error(err); return; } else { if(0<frdl.debug.mode()) console.log('sql--sql :'); if(0<frdl.debug.mode())console.log('' + _sql); if(0<frdl.debug.mode()) console.log('\nsql--data:'); if(0<frdl.debug.mode()) console.dir(resultset); } if(0<frdl.debug.mode()) console.dir(THAT[db][table].data); var r=false, _DATA = THAT[db][table].data, i = 0, Equal = true; while((row = frdl.sql.fetch_row(resultset)) && false !== row){ r = frdl.clone(row); for(i=0;i<_DATA.length;i++){ Equal = true; frdl.each(r, function(ri,v){ if(v !== _DATA[i][ri]){ Equal=false; return false; } }); if(true===Equal){ _DATA.splice(i, 1); } } } THAT[db][table].data = _DATA; if(0<frdl.debug.mode()) console.dir(THAT[db][table].data); }); }, 'insert' : function(ast){ var dc = this; if('undefined'===typeof dc[ast.db][ast.table]){ console.error('Table '+ast.db+'.'+ast.table+' does not exist'); return; } var transaction = frdl.clone(dc[ast.db][ast.table].data); var row = []; frdl.each(dc[ast.db][ast.table].columns, function(i, r) { row.push(''); }); frdl.each(ast.set, function(i, s) { var i = dc[ast.db][ast.table].columns.indexOf(s.column); if(0 <= i)row[i] = s.value.value; }); dc[ast.db][ast.table].data.push(row); var uniq = require('uniq'); var test = uniq(frdl.clone(dc[ast.db][ast.table].data), function(a, b){ if(dc[ast.db][ast.table].KEYINDEX && dc[ast.db][ast.table].KEYINDEX.PRIMARY){ var k = row; var _k = row; var _t = row; frdl.each(dc[ast.db][ast.table].KEYINDEX.PRIMARY, function(i, field){ var f = dc[ast.db][ast.table].columns.indexOf(field); _t[f]=frdl.clone(a[f]); k[f]=frdl.clone(a[f]); _k[f]=frdl.clone(b[f]); }); var _t1 = uniq(frdl.clone(k)); var _t2 = uniq(frdl.clone(_t)); return (false===(_t1.length === _t2.length === k.length === _t.length === _k.length)); } return true; }, false); if(test.length !== dc[ast.db][ast.table].data.length || dc[ast.db][ast.table].data.length === transaction.length){ dc[ast.db][ast.table].data = frdl.clone(transaction); console.dir(test); console.dir(dc[ast.db][ast.table].data); console.dir(transaction); throw 'DUBLICATE KEY in ' + ast.db + '.'+ast.table; } if(dc[ast.db][ast.table].KEYINDEX && dc[ast.db][ast.table].KEYINDEX.UNIQUE){ frdl.each(dc[ast.db][ast.table].KEYINDEX.UNIQUE, function(i, _Key){ test = uniq(frdl.clone(dc[ast.db][ast.table].data), function(a, b){ var k = row; var _k = row; var _t = row; frdl.each(_Key, function(i, field){ var f = dc[ast.db][ast.table].columns.indexOf(field); _t[f]=frdl.clone(a[f]); k[f]=frdl.clone(a[f]); _k[f]=frdl.clone(b[f]); }); var _t1 = uniq(frdl.clone(k)); var _t2 = uniq(frdl.clone(_t)); return (false===(_t1.length === _t2.length === k.length === _t.length === _k.length)); }, false); if(test.length !== dc[ast.db][ast.table].data.length || dc[ast.db][ast.table].data.length === transaction.length){ dc[ast.db][ast.table].data = frdl.clone(transaction); throw 'DUBLICATE KEY in ' + ast.db + '.'+ast.table; } }); } //...no Key error found ?! dc[ast.db][ast.table].data = frdl.clone(test); }, 'begin' : function(s, level){ }, 'commit' : function(s, level){ console.log('hook commit (todo?) ' + [s, level].join(',')); /* var k, t, fs = require('fs'); var dc = this; for(k in dc){ frdl.fs5.polyfill.getFiler().cd(frdl.fs5.polyfill.getFiler().fs.root); fs.mkdir('1.3.6.1.4.1.37553.8.1.8.8.10.2.1'+frdl.DIRECTORY_SEPARATOR + 'dbs' +frdl.DIRECTORY_SEPARATOR + k +frdl.DIRECTORY_SEPARATOR, '0755', function(err,dirEntry) { if(err){ console.error(err); // return; } for(t in dc[k]){ frdl.fs5.polyfill.getFiler().cd(frdl.fs5.polyfill.getFiler().fs.root); fs.mkdir('1.3.6.1.4.1.37553.8.1.8.8.10.2.1'+frdl.DIRECTORY_SEPARATOR + 'dbs' +frdl.DIRECTORY_SEPARATOR + k +frdl.DIRECTORY_SEPARATOR + t +frdl.DIRECTORY_SEPARATOR, '0755', function(err,dirEntry) { if(err){ console.error(err); //return; } frdl.fs5.polyfill.getFiler().cd(frdl.fs5.polyfill.getFiler().fs.root); fs.writeFile('1.3.6.1.4.1.37553.8.1.8.8.10.2.1'+frdl.DIRECTORY_SEPARATOR + 'dbs' +frdl.DIRECTORY_SEPARATOR + k +frdl.DIRECTORY_SEPARATOR + t +frdl.DIRECTORY_SEPARATOR + 'KEYINDEX.json', JSON.stringify(dc[k][t].KEYINDEX), function(err, data){ if(err){ console.error(err); // return; } }); frdl.fs5.polyfill.getFiler().cd(frdl.fs5.polyfill.getFiler().fs.root); fs.writeFile('1.3.6.1.4.1.37553.8.1.8.8.10.2.1'+frdl.DIRECTORY_SEPARATOR + 'dbs' +frdl.DIRECTORY_SEPARATOR + k +frdl.DIRECTORY_SEPARATOR + t +frdl.DIRECTORY_SEPARATOR + 'columns.json', JSON.stringify(dc[k][t].columns), function(err, data){ if(err){ console.error(err); // return; } }); process.cwd(frdl.fs5.polyfill.getFiler().fs.root); fs.mkdir('1.3.6.1.4.1.37553.8.1.8.8.10.2.1'+frdl.DIRECTORY_SEPARATOR + 'dbs' +frdl.DIRECTORY_SEPARATOR +frdl.DIRECTORY_SEPARATOR + k +frdl.DIRECTORY_SEPARATOR + t +frdl.DIRECTORY_SEPARATOR + 'data' +frdl.DIRECTORY_SEPARATOR, '0755', function(__err,__DataDirEntry) { if(err){ console.error(err); // return; } frdl.fs5.polyfill.getFiler().cd(frdl.fs5.polyfill.getFiler().fs.root); fs.writeFile('1.3.6.1.4.1.37553.8.1.8.8.10.2.1'+frdl.DIRECTORY_SEPARATOR + 'dbs' +frdl.DIRECTORY_SEPARATOR + k +frdl.DIRECTORY_SEPARATOR + t +frdl.DIRECTORY_SEPARATOR + 'data' +frdl.DIRECTORY_SEPARATOR + 'rows.1.json', JSON.stringify(dc[k][t].data), function(err, data){ if(err){ console.error(err); // return; } }); }); }); } }); } */ }, 'commit final' : function(s, level){ var K, T, fs = require('fs'); var dc = this; // for(K in dc){ frdl.each(dc, function(K, DB){ var k = K; // console.log('hook commit final (todo?) ' + [k].join(',')); process.cwd(frdl.fs5.polyfill.getFiler().fs.root); fs.mkdir('1.3.6.1.4.1.37553.8.1.8.8.10.2.1'+frdl.DIRECTORY_SEPARATOR + 'dbs' +frdl.DIRECTORY_SEPARATOR + k +frdl.DIRECTORY_SEPARATOR, '0755', function(err,dirEntry) { if(err){ console.error(err); // return; } //for(T in dc[k]){ frdl.each(DB, function(T, _Table){ var t = T; process.cwd(frdl.fs5.polyfill.getFiler().fs.root); fs.mkdir('1.3.6.1.4.1.37553.8.1.8.8.10.2.1'+frdl.DIRECTORY_SEPARATOR + 'dbs' +frdl.DIRECTORY_SEPARATOR + k +frdl.DIRECTORY_SEPARATOR + t +frdl.DIRECTORY_SEPARATOR, '0755', function(err,dirEntry) { if(err){ console.error(err); //return; } // console.log('mkdir 1.3.6.1.4.1.37553.8.1.8.8.10.2.1 ' + [k, t].join(',')); process.cwd(frdl.fs5.polyfill.getFiler().fs.root); fs.writeFile('1.3.6.1.4.1.37553.8.1.8.8.10.2.1'+frdl.DIRECTORY_SEPARATOR + 'dbs' +frdl.DIRECTORY_SEPARATOR + k +frdl.DIRECTORY_SEPARATOR + t +frdl.DIRECTORY_SEPARATOR + 'KEYINDEX.json', JSON.stringify(_Table.KEYINDEX), function(err, data){ if(err){ console.error(err); // return; } // console.log('writeFile 1.3.6.1.4.1.37553.8.1.8.8.10.2.1 KEYINDEX.json ' + [k, t].join(',')); }); process.cwd(frdl.fs5.polyfill.getFiler().fs.root); fs.writeFile('1.3.6.1.4.1.37553.8.1.8.8.10.2.1'+frdl.DIRECTORY_SEPARATOR + 'dbs' +frdl.DIRECTORY_SEPARATOR + k +frdl.DIRECTORY_SEPARATOR + t +frdl.DIRECTORY_SEPARATOR + 'columns.json', JSON.stringify(_Table.columns), function(err, data){ if(err){ console.error(err); // return; } // console.log('writeFile 1.3.6.1.4.1.37553.8.1.8.8.10.2.1 columns.json ' + [k, t].join(',')); }); process.cwd(frdl.fs5.polyfill.getFiler().fs.root); fs.mkdir('1.3.6.1.4.1.37553.8.1.8.8.10.2.1'+frdl.DIRECTORY_SEPARATOR + 'dbs' +frdl.DIRECTORY_SEPARATOR +frdl.DIRECTORY_SEPARATOR + k +frdl.DIRECTORY_SEPARATOR + t +frdl.DIRECTORY_SEPARATOR + 'data' +frdl.DIRECTORY_SEPARATOR, '0755', function(__err,__DataDirEntry) { if(__err){ console.error(__err); return; } // if(_Table.data && _Table.data.length && 0<_Table.data.length){ process.cwd(frdl.fs5.polyfill.getFiler().fs.root); fs.writeFile('1.3.6.1.4.1.37553.8.1.8.8.10.2.1'+frdl.DIRECTORY_SEPARATOR + 'dbs' +frdl.DIRECTORY_SEPARATOR + k +frdl.DIRECTORY_SEPARATOR + t +frdl.DIRECTORY_SEPARATOR + 'data' +frdl.DIRECTORY_SEPARATOR + 'rows.1.json', JSON.stringify(_Table.data), function(_err, data){ if(_err){ console.error(_err); return; } // console.log('writeFile 1.3.6.1.4.1.37553.8.1.8.8.10.2.1 data/rows.1.json '+JSON.stringify(_Table.data)); }); // } }); }); // } }); }); //} }); }, 'create database' : function(dbname){ console.log('hook create database ' +dbname); if(!this[dbname])this[dbname]={}; var fs = require('fs'); process.cwd(frdl.fs5.polyfill.getFiler().fs.root); fs.mkdir('1.3.6.1.4.1.37553.8.1.8.8.10.2.1'+frdl.DIRECTORY_SEPARATOR + 'dbs' +frdl.DIRECTORY_SEPARATOR + dbname +frdl.DIRECTORY_SEPARATOR, '0755', function(err,dirEntry) { if(err){ console.error(err); return; } }); }, 'create table' : function(db, table, colums){ console.log('hook create table (todo?) ' + [db, table, colums].join(',')); // frdl.sql.query('commit'); }, 'query' : function(dc, ar){ console.log('hook query (todo?) '); console.dir(ar); // console.dir(this); }, 'alter table' : function(sQL, words, words2){ // console.log('hook alter table'); // console.dir(arguments); // console.dir(this); var toks = Array.prototype.slice.call(words2); toks.shift(); var table_string = toks.shift(); var t = frdl.explode('.', table_string); var db = t[0]; var table = t[1]; var cmd = toks.shift(); var type = toks[0].toUpperCase(); if('undefined' === typeof this[db]){ throw 'database '+db+' does not exist'; } if('undefined' === typeof this[db][table]){ throw 'Table '+db+'.'+table+' does not exist'; } if( toks[1].toLowerCase() === 'key'){ var what = type+' KEY'; if('add' === cmd.toLowerCase()){ if(!this[db][table].KEYINDEX){ this[db][table].KEYINDEX={}; } if(!this[db][table].KEYINDEX[type]){ this[db][table].KEYINDEX[type]=[]; } toks.shift(); toks.shift(); if('primary' !== type){ var _index = []; while(0<toks.length){ var k = toks.shift(); k = frdl.str_replace('(', '', k); k = frdl.str_replace(')', '', k); k.trim(); if('' !== k){ // console.log(k); frdl.each(frdl.explode(',', k), function(i,t){ t=t.trim(); if(''===t)return true; if(-1===_index.indexOf(t)) _index.push(t); }); } } this[db][table].KEYINDEX[type].push(_index); } else{ while(0<toks.length){ var k = toks.shift(); k = frdl.str_replace('(', '', k); k = frdl.str_replace(')', '', k); k.trim(); if('' !== k){ frdl.each(frdl.explode(',', k), function(i,t){ t=t.trim(); if(''===t)return true; if(-1===this[db][table].KEYINDEX[type].indexOf(t))this[db][table].KEYINDEX[type].push(t); }); } } } }else if('drop' === cmd.toLowerCase()){ if(!this[db][table].KEYINDEX){ this[db][table].KEYINDEX={}; } this[db][table].KEYINDEX[type] = []; } }else if('add' === cmd.toLowerCase() ){ var col = type; if(-1===this[db][table].columns.indexOf(col))this[db][table].columns.push(col); }else if('drop' === cmd.toLowerCase() && 'COLUMN' === type ){ var col = toks[1]; this[db][table].columns.splice(this[db][table].columns.indexOf(col), 1); } } } };