立即注册
 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广州大学城网业务调整

[Web前端开发] SQL在线查询设计器 [复制链接] qrcode

查看: 2802 | 回复: 0

html580_com
发表于: 2012-12-26 19:12:54 | 显示全部楼层

1355724000_6343.jpg
  1. //http://www.html580.com  
  2.     Ext.define('Ext.ux.window.visualsqlquerybuilder.SQLTableSprite', {  
  3.         extend: 'Ext.draw.Sprite',  
  4.         alias: ['widget.sqltablesprite'],  
  5.         bConnections: false,  
  6.         startDrag: function(id){  
  7.             var me = this, win, sqlTablePanel, xyParentPos, xyChildPos;  
  8.               
  9.             // get a reference to a sqltable  
  10.             win = Ext.getCmp(id);  
  11.               
  12.             // get the main sqlTablePanel  
  13.             sqlTablePanel = Ext.getCmp('SQLTablePanel');  
  14.               
  15.             // get the main sqlTablePanel position  
  16.             xyParentPos = sqlTablePanel.el.getXY();  
  17.               
  18.             // get the size of the previously added sqltable  
  19.             xyChildPos = win.el.getXY();  
  20.               
  21.             me.prev = me.surface.transformToViewBox(xyChildPos[0] - xyParentPos[0] + 2, xyChildPos[1] - xyParentPos[1] + 2);  
  22.         },  
  23.          
  24.         onDrag: function(relPosMovement){  
  25.             var xy, me = this, attr = this.attr, newX, newY;  
  26.             // move the sprite  
  27.             // calculate new x and y position  
  28.             newX = me.prev[0] + relPosMovement[0];  
  29.             newY = me.prev[1] + relPosMovement[1];  
  30.             // set new x and y position and redraw sprite  
  31.             me.setAttributes({  
  32.                 x: newX,  
  33.                 y: newY  
  34.               
  35.             }, true);  
  36.         }  
  37.     });   
  38.       
  39.     Ext.define('Ext.ux.window.visualsqlquerybuilder.SQLTableModel', {  
  40.         extend: 'Ext.data.Model',  
  41.         fields: [{  
  42.             name: 'id',  
  43.             type: 'string'  
  44.         }, {  
  45.             name: 'tableName',  
  46.             type: 'string'  
  47.         }, {  
  48.             name: 'tableAlias',  
  49.             type: 'string'  
  50.         }]  
  51.     });  
  52.       
  53.     Ext.define('Ext.ux.window.visualsqlquerybuilder.SQLTableStore', {  
  54.         extend: 'Ext.data.Store',  
  55.         autoSync: true,  
  56.         model: 'Ext.ux.window.visualsqlquerybuilder.SQLTableModel',  
  57.         proxy: {  
  58.             type: 'memory'  
  59.         }  
  60.     });  
  61.       
  62.     Ext.define('Ext.ux.window.visualsqlquerybuilder.SQLJoin', {  
  63.         extend: 'Ext.data.Model',  
  64.         fields: [{  
  65.             name: 'id',  
  66.             type: 'string'  
  67.         }, {  
  68.             name: 'leftTableId',  
  69.             type: 'string'  
  70.         }, {  
  71.             name: 'rightTableId',  
  72.             type: 'string'  
  73.         }, {  
  74.             name: 'leftTableField',  
  75.             type: 'string'  
  76.         }, {  
  77.             name: 'rightTableField',  
  78.             type: 'string'  
  79.         }, {  
  80.             name: 'joinCondition',  
  81.             type: 'string'  
  82.         }, {  
  83.             name: 'joinType',  
  84.             type: 'string'  
  85.         }],  
  86.         createUUID: function(){  
  87.             // http://www.ietf.org/rfc/rfc4122.txt  
  88.             var s = [];  
  89.             var hexDigits = "0123456789abcdef";  
  90.             for (var i = 0; i < 36; i++) {  
  91.                 s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);  
  92.             }  
  93.             s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010  
  94.             s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01  
  95.             s[8] = s[13] = s[18] = s[23] = "-";  
  96.               
  97.             var uuid = s.join("");  
  98.             return uuid;  
  99.         }  
  100.     });  
  101.       
  102.     Ext.define('Ext.ux.window.visualsqlquerybuilder.JoinStore', {  
  103.         extend: 'Ext.data.Store',  
  104.         autoSync: true,  
  105.         model: 'Ext.ux.window.visualsqlquerybuilder.SQLJoin',  
  106.         proxy: {  
  107.             type: 'memory'  
  108.         }  
  109.     });  
  110.       
  111.     Ext.define('Ext.ux.window.visualsqlquerybuilder.SQLFieldsModel', {  
  112.         extend: 'Ext.data.Model',  
  113.         fields: [{  
  114.             name: 'id',  
  115.             type: 'string'  
  116.         }, {  
  117.             name: 'tableName',  
  118.             type: 'string'  
  119.         }, {  
  120.             name: 'tableId',  
  121.             type: 'string'  
  122.         }, {  
  123.             name: 'extCmpId',  
  124.             type: 'string'  
  125.         }, {  
  126.             name: 'tableAlias',  
  127.             type: 'string'  
  128.         }, {  
  129.             name: 'field',  
  130.             type: 'string'  
  131.         }, {  
  132.             name: 'output',  
  133.             type: 'boolean'  
  134.         }, {  
  135.             name: 'expression',  
  136.             type: 'string'  
  137.         }, {  
  138.             name: 'aggregate',  
  139.             type: 'string'  
  140.         }, {  
  141.             name: 'alias',  
  142.             type: 'string'  
  143.         }, {  
  144.             name: 'sortType',  
  145.             type: 'string'  
  146.         }, {  
  147.             name: 'sortOrder',  
  148.             type: 'int'  
  149.         }, {  
  150.             name: 'grouping',  
  151.             type: 'boolean'  
  152.         }, {  
  153.             name: 'criteria',  
  154.             type: 'string'  
  155.         }]  
  156.     });  
  157.       
  158.     Ext.define('Ext.ux.window.visualsqlquerybuilder.SQLFieldsStore', {  
  159.         extend: 'Ext.data.Store',  
  160.         autoSync: true,  
  161.         model: 'Ext.ux.window.visualsqlquerybuilder.SQLFieldsModel',  
  162.         proxy: {  
  163.             type: 'memory'  
  164.         }  
  165.     });  
  166.       
  167.     Ext.define('Ext.ux.window.visualsqlquerybuilder.SQLSelect', {  
  168.         config: {  
  169.             tables: '',  
  170.             fields: '',  
  171.             joins: ''  
  172.         },  
  173.         constructor: function(){  
  174.          
  175.             this.tables = Ext.create('Ext.ux.window.visualsqlquerybuilder.SQLTableStore', {  
  176.                 storeId: 'SQLTableStore'  
  177.             });  
  178.               
  179.             // handle all updates on sql tables  
  180.             this.tables.on('update', this.handleSQLTableUpdate, this);  
  181.             this.tables.on('add', this.handleSQLTableAdd, this);  
  182.             this.tables.on('remove', this.handleSQLTableRemove, this);  
  183.               
  184.             this.fields = Ext.create('Ext.ux.window.visualsqlquerybuilder.SQLFieldsStore', {  
  185.                 storeId: 'SQLFieldsStore'  
  186.             });  
  187.               
  188.             this.fields.on('update', this.handleSQLFieldChanges, this);  
  189.             this.fields.on('remove', this.handleSQLFieldRemove, this);  
  190.               
  191.             this.joins = Ext.create('Ext.ux.window.visualsqlquerybuilder.JoinStore', {  
  192.                 storeId: 'JoinStore'  
  193.             });  
  194.               
  195.             // this.joins.on('update', this.handleSQLJoinChanges, this);  
  196.             this.joins.on('add', this.handleSQLJoinChanges, this);  
  197.             this.joins.on('remove', this.handleSQLJoinChanges, this);  
  198.               
  199.             this.callParent(arguments);  
  200.         },  
  201.         handleSQLTableUpdate: function(tableStore, table, operation){  
  202.             if (operation == 'commit') {  
  203.                 this.updateFieldTableData(table);  
  204.                 this.updateJoinTableData(table);  
  205.                 this.updateSQLOutput();  
  206.             }  
  207.         },  
  208.         handleSQLTableAdd: function(tableStore, table, index){  
  209.             this.updateSQLOutput();  
  210.         },  
  211.         handleSQLTableRemove: function(tableStore, table, index){  
  212.             var aJoins = [];  
  213.             // get table joins and remove them  
  214.             aJoins = this.getJoinsByTableId(table.get('id'));  
  215.             // loop over the joins array  
  216.             for (var i = 0, l = aJoins.length; i < l; i++) {  
  217.                 // remove join from store  
  218.                 this.removeJoinById(aJoins[i].get('id'));  
  219.             }  
  220.             this.updateSQLOutput();  
  221.         },  
  222.         handleSQLJoinChanges: function(joinStore, join){  
  223.             this.updateSQLOutput();  
  224.         },  
  225.         updateFieldTableData: function(table){  
  226.             var tableId, expression, tableAlias, tableName;  
  227.             tableId = table.get('id');  
  228.             tableAlias = table.get('tableAlias');  
  229.             tableName = table.get('tableName');  
  230.             // loop over all fields of the fields store  
  231.             this.fields.each(function(field){  
  232.                 // check if current field belongs to sql table  
  233.                 if (field.get('tableId') == tableId) {  
  234.                     if (tableAlias != '') {  
  235.                         // we have a table alias  
  236.                         expression = tableAlias + '.' + field.get('field');  
  237.                     }  
  238.                     else {  
  239.                         // no table alias  
  240.                         expression = tableName + '.' + field.get('field');  
  241.                     };  
  242.                     field.beginEdit();  
  243.                     // update the field table alias  
  244.                     field.set('tableAlias', tableAlias);  
  245.                     // update the field expression  
  246.                     field.set('expression', expression);  
  247.                     field.commit(true);  
  248.                     field.endEdit();  
  249.                 }  
  250.             });  
  251.             return;  
  252.         },  
  253.         updateJoinTableData: function(table){  
  254.             var joins, tableId;  
  255.             tableId = table.get('id');  
  256.             joins = this.getJoinsByTableId(tableId);  
  257.             for (var i = 0, rightTable, leftTable, joinCondition = '',l = joins.length; i < l; i++) {  
  258.                 leftTable = this.getTableById(joins[i].get('leftTableId'));  
  259.                 rightTable = this.getTableById(joins[i].get('rightTableId'));  
  260.                   
  261.                 if (leftTable.get('tableAlias') != '') {  
  262.                     joinCondition = joinCondition + leftTable.get('tableAlias') + '.' + joins[i].get('leftTableField') + '=';  
  263.                 }  
  264.                 else {  
  265.                     joinCondition = joinCondition + leftTable.get('tableName') + '.' + joins[i].get('leftTableField') + '=';  
  266.                 }  
  267.                   
  268.                 if (rightTable.get('tableAlias') != '') {  
  269.                     joinCondition = joinCondition + rightTable.get('tableAlias') + '.' + joins[i].get('rightTableField');  
  270.                 }  
  271.                 else {  
  272.                     joinCondition = joinCondition + rightTable.get('tableName') + '.' + joins[i].get('rightTableField');  
  273.                 }  
  274.                 joins[i].beginEdit();  
  275.                 joins[i].set('joinCondition', joinCondition);  
  276.                 joins[i].commit(true);  
  277.                 joins[i].endEdit();  
  278.             }  
  279.         },  
  280.         handleSQLFieldChanges: function(fieldStore, model, operation){  
  281.             if (operation == 'commit') {  
  282.                 this.updateSQLOutput();  
  283.             }  
  284.         },  
  285.         handleSQLFieldRemove: function(fieldStore){  
  286.             this.updateSQLOutput();  
  287.         },  
  288.         updateSQLOutput: function(){  
  289.             var sqlOutput, sqlHTML, sqlQutputPanel;  
  290.             sqlOutput = this.toString();  
  291.             sqlHTML = '<pre class="brush: sql">' + sqlOutput + '</pre>';  
  292.             sqlQutputPanel = Ext.getCmp('SQLOutputPanel');  
  293.               
  294.             sqlQutputPanel.update(sqlHTML);  
  295.         },  
  296.         sortTablesByJoins: function(tables, oUsedTables){  
  297.             var aTables = [], aJoins = [], oUsedTables = oUsedTables ||  
  298.             {};  
  299.             // loop over tables  
  300.             for (var i = 0, aCondition = [], aJoin, l = tables.length; i < l; i++) {  
  301.                 // check if current table is a new one  
  302.                 if (!oUsedTables.hasOwnProperty(tables[i].get('id'))) {  
  303.                     // it is a new one  
  304.                     aTables.push(tables[i]);  
  305.                     // mark table as used  
  306.                     oUsedTables[tables[i].get('id')] = true;  
  307.                     // get any joins for the current table  
  308.                     aJoin = this.getJoinsByTableId(tables[i].get('id'));  
  309.                     // loop over the join tables  
  310.                     for (var j = 0, joinTable, len = aJoin.length; j < len; j++) {  
  311.                         // check if it is a new join  
  312.                         if (!oUsedTables.hasOwnProperty(aJoin[j].get('id'))) {  
  313.                             // mark join as used  
  314.                             oUsedTables[aJoin[j].get('id')] = true;  
  315.                             if (tables[i].get('id') != aJoin[j].get('leftTableId')) {  
  316.                                 joinTable = this.getTableById(aJoin[j].get('leftTableId'));  
  317.                                 this.changeLeftRightOnJoin(aJoin[j]);  
  318.                             }  
  319.                             else {  
  320.                                 joinTable = this.getTableById(aJoin[j].get('rightTableId'));  
  321.                             }  
  322.                             oTemp = this.sortTablesByJoins([joinTable], oUsedTables);  
  323.                             oUsedTables = oTemp.oUsedTables;  
  324.                             aTables = aTables.concat(oTemp.aTables);  
  325.                         }  
  326.                     }  
  327.                 }  
  328.             }  
  329.               
  330.             return {  
  331.                 aTables: aTables,  
  332.                 oUsedTables: oUsedTables  
  333.             };  
  334.         },  
  335.         changeLeftRightOnJoin: function(join){  
  336.             var leftTable, leftTableField, rightTable, rightTableField, joinCondition = '';  
  337.             // prepare new data  
  338.             leftTable = this.getTableById(join.get('rightTableId'));  
  339.             leftTableField = join.get('rightTableField');  
  340.             rightTable = this.getTableById(join.get('leftTableId'));  
  341.             rightTableField = join.get('leftTableField');  
  342.               
  343.             // construct new joinCondition  
  344.             if (leftTable.get('tableAlias') != '') {  
  345.                 joinCondition = joinCondition + leftTable.get('tableAlias') + '.' + join.get('rightTableField') + '=';  
  346.             }  
  347.             else {  
  348.                 joinCondition = joinCondition + leftTable.get('tableName') + '.' + join.get('rightTableField') + '=';  
  349.             }  
  350.               
  351.             if (rightTable.get('tableAlias') != '') {  
  352.                 joinCondition = joinCondition + rightTable.get('tableAlias') + '.' + join.get('leftTableField');  
  353.             }  
  354.             else {  
  355.                 joinCondition = joinCondition + rightTable.get('tableName') + '.' + join.get('leftTableField');  
  356.             }  
  357.               
  358.             // start transaction  
  359.             join.beginEdit();  
  360.             // change left and right join table data  
  361.             join.set('leftTableId', leftTable.get('id'));  
  362.             join.set('leftTableField', leftTableField);  
  363.             join.set('rightTableId', rightTable.get('id'));  
  364.             join.set('rightTableField', rightTableField);  
  365.             join.set('joinCondition', joinCondition);  
  366.             // silent commit without firing store events  
  367.             // this prevents endless loop  
  368.             join.commit(true);  
  369.             join.endEdit();  
  370.             // end transaction  
  371.             return;  
  372.         },  
  373.         toString: function(){  
  374.             var sqlOutput = 'SELECT ', aJoins = [], aOutputFields = [], oJoinTables = {}, aTables = [], aJoinTables = [], aCriteriaFields = [], aGroupFields = [], aOrderFields = [], selectFieldsSQL = '', fromSQL = '', aFromSQL = [], criteriaSQL = '', orderBySQL = '', groupBySQL = '', fieldSeperator = ', ', joinSQL = '', bFirst = true, bPartOfJoin = false;  
  375.             this.fields.each(function(field){  
  376.                 // should the field be a part of the output  
  377.                 if (field.get('output')) {  
  378.                     aOutputFields.push(field);  
  379.                 }  
  380.                 // any criteria  
  381.                 if (field.get('criteria') != '') {  
  382.                     aCriteriaFields.push(field);  
  383.                 }  
  384.                 // check for grouping  
  385.                 if (field.get('grouping')) {  
  386.                     aGroupFields.push(field);  
  387.                 }  
  388.                 // check for sorting  
  389.                 if (field.get('sortType') != '') {  
  390.                     aOrderFields.push(field);  
  391.                 }  
  392.             });  
  393.               
  394.             // tables  
  395.             // sorting of tables  
  396.             this.tables.each(function(table){  
  397.                 aTables.push(table);  
  398.             });  
  399.               
  400.             aTables = this.sortTablesByJoins(aTables).aTables;  
  401.               
  402.               
  403.             this.joins.each(function(join){  
  404.                 aJoins.push(join);  
  405.             });  
  406.               
  407.             //create fromSQL  
  408.             for (var k = 0, aJoin = [], oJoinTables = {}, joinCondition = '', joinType, leftTable, rightTable, l = aTables.length; k < l; k++) {  
  409.                 if (k == aTables.length - 1) {  
  410.                     fieldSeperator = '';  
  411.                 }  
  412.                 else {  
  413.                     fieldSeperator = ', ';  
  414.                 };  
  415.                   
  416.                 // is the current table the first one  
  417.                 if (bFirst) {  
  418.                     // yes it is the first  
  419.                      
  420.                     // table id merken  
  421.                     oJoinTables[aTables[k].get('id')] = true;  
  422.                      
  423.                     bFirst = false;  
  424.                      
  425.                     // check if current table is not the last one in the loop   
  426.                     if ((k + 1) < aTables.length) {  
  427.                         // get joins where joins leftTableID is a property of oJoinTables and joins rightTableID equal to aTables[i+1].get('id')  
  428.                         for (var h = 0, len = aJoins.length; h < len; h++) {  
  429.                             if (oJoinTables.hasOwnProperty(aJoins[h].get('leftTableId')) && aJoins[h].get('rightTableId') == aTables[k + 1].get('id')) {  
  430.                                 aJoin.push(aJoins[h]);  
  431.                             }  
  432.                             if (oJoinTables.hasOwnProperty(aJoins[h].get('rightTableId')) && aJoins[h].get('leftTableId') == aTables[k + 1].get('id')) {  
  433.                                 this.changeLeftRightOnJoin(aJoins[h]);  
  434.                                 aJoin.push(aJoins[h]);  
  435.                             }  
  436.                         }  
  437.                           
  438.                         // check if we have a join  
  439.                         if (aJoin.length > 0) {  
  440.                             // yes we have a join between aTables[k] and aTables[k+1] with at least one join condition  
  441.                               
  442.                             leftTable = aTables[k];  
  443.                             rightTable = aTables[k + 1];  
  444.                               
  445.                             // table id merken  
  446.                             oJoinTables[rightTable.get('id')] = true;  
  447.                               
  448.                             for (var j = 0, fieldSeperator = '', ln = aJoin.length; j < ln; j++) {  
  449.                                 if (j == aJoin.length - 1) {  
  450.                                     fieldSeperator = '';  
  451.                                 }  
  452.                                 else {  
  453.                                     fieldSeperator = '\nAND ';  
  454.                                 };  
  455.                                 joinType = aJoin[j].get('joinType');  
  456.                                 joinCondition = joinCondition + aJoin[j].get('joinCondition') + fieldSeperator;  
  457.                             }  
  458.                               
  459.                             // reset the join array   
  460.                             aJoin = [];  
  461.                               
  462.                             if (joinSQL != '') {  
  463.                                 joinSQL = joinSQL + ',\n';  
  464.                             }  
  465.                               
  466.                             if (leftTable.get('tableAlias') != '') {  
  467.                                 // we have an leftTable alias  
  468.                                 joinSQL = joinSQL + leftTable.get('tableName') + ' ' + leftTable.get('tableAlias') + ' ' + joinType + ' JOIN ';  
  469.                             }  
  470.                             else {  
  471.                                 //no alias  
  472.                                 joinSQL = joinSQL + leftTable.get('tableName') + ' ' + joinType + ' JOIN ';  
  473.                             }  
  474.                               
  475.                             if (rightTable.get('tableAlias') != '') {  
  476.                                 // we have an rightTable alias  
  477.                                 joinSQL = joinSQL + rightTable.get('tableName') + ' ' + rightTable.get('tableAlias') + ' ON ' + joinCondition;  
  478.                             }  
  479.                             else {  
  480.                                 //no alias  
  481.                                 joinSQL = joinSQL + rightTable.get('tableName') + ' ON ' + joinCondition;  
  482.                             }  
  483.                               
  484.                             // clear joinCondition  
  485.                             joinCondition = '';  
  486.                               
  487.                         }  
  488.                         else {  
  489.                             // no join between aTables[i+1] and the one before  
  490.                             bFirst = true;  
  491.                             oJoinTables = {};  
  492.                             // check for tableAlias  
  493.                             if (aTables[k].get('tableAlias') != '') {  
  494.                                 fromSQL = aTables[k].get('tableName') + ' ' + aTables[k].get('tableAlias');  
  495.                             }  
  496.                             else {  
  497.                                 fromSQL = aTables[k].get('tableName');  
  498.                             }  
  499.                             aFromSQL.push(fromSQL);  
  500.                         }  
  501.                     }  
  502.                     else {  
  503.                         // its the last and only one in the loop  
  504.                         // check for tableAlias  
  505.                         if (aTables[k].get('tableAlias') != '') {  
  506.                             fromSQL = aTables[k].get('tableName') + ' ' + aTables[k].get('tableAlias');  
  507.                         }  
  508.                         else {  
  509.                             fromSQL = aTables[k].get('tableName');  
  510.                         }  
  511.                         aFromSQL.push(fromSQL);  
  512.                     }  
  513.                 }  
  514.                 else {  
  515.                     // no, it is not the first table  
  516.                      
  517.                     bFirst = true;  
  518.                      
  519.                     // check if current table is not the last one in the loop   
  520.                     if ((k + 1) < aTables.length) {  
  521.                         // get joins where joins leftTableID is a property of oJoinTables and joins rightTableID equal to aTables[i+1].get('id')  
  522.                         for (var h = 0, len = aJoins.length; h < len; h++) {  
  523.                             if (oJoinTables.hasOwnProperty(aJoins[h].get('leftTableId')) && aJoins[h].get('rightTableId') == aTables[k + 1].get('id')) {  
  524.                                 aJoin.push(aJoins[h]);  
  525.                             }  
  526.                             if (oJoinTables.hasOwnProperty(aJoins[h].get('rightTableId')) && aJoins[h].get('leftTableId') == aTables[k + 1].get('id')) {  
  527.                                 this.changeLeftRightOnJoin(aJoins[h]);  
  528.                                 aJoin.push(aJoins[h]);  
  529.                             }  
  530.                         }  
  531.                           
  532.                         // check if we have a join  
  533.                         if (aJoin.length > 0) {  
  534.                             // yes we have a join between aTables[k] and aTables[k+1] with at least one join condition  
  535.                               
  536.                             rightTable = aTables[k + 1];  
  537.                               
  538.                             // table id merken  
  539.                             oJoinTables[rightTable.get('id')] = true;  
  540.                               
  541.                             for (var j = 0, fieldSeperator = '', ln = aJoin.length; j < ln; j++) {  
  542.                                 if (j == aJoin.length - 1) {  
  543.                                     fieldSeperator = '';  
  544.                                 }  
  545.                                 else {  
  546.                                     fieldSeperator = '\nAND ';  
  547.                                 };  
  548.                                 joinType = aJoin[j].get('joinType');  
  549.                                 joinCondition = joinCondition + aJoin[j].get('joinCondition') + fieldSeperator;  
  550.                             }  
  551.                               
  552.                             // reset the join array   
  553.                             aJoin = [];  
  554.                               
  555.                             bFirst = false;  
  556.                               
  557.                             if (rightTable.get('tableAlias') != '') {  
  558.                                 // we have an rightTable alias  
  559.                                 joinSQL = joinSQL + '\n' + joinType + ' JOIN ' + rightTable.get('tableName') + ' ' + rightTable.get('tableAlias') + ' ON ' + joinCondition;  
  560.                             }  
  561.                             else {  
  562.                                 //no alias  
  563.                                 joinSQL = joinSQL + '\n' + joinType + ' JOIN ' + rightTable.get('tableName') + ' ON ' + joinCondition;  
  564.                             }  
  565.                               
  566.                             // clear joinCondition  
  567.                             joinCondition = '';  
  568.                         }  
  569.                         else {  
  570.                             bFirst = true;  
  571.                             oJoinTables = {};  
  572.                         }  
  573.                     }  
  574.                     else {  
  575.                         // its the last and only one  
  576.                         // check for tableAlias  
  577.                         oJoinTables = {};  
  578.                     }  
  579.                 }  
  580.             }  
  581.               
  582.             fromSQL = aFromSQL.join(', ');  
  583.               
  584.             if (joinSQL != '' && fromSQL != '') {  
  585.                 joinSQL = joinSQL + ', ';  
  586.             }  
  587.               
  588.             fromSQL = '\nFROM ' + joinSQL + fromSQL;  
  589.               
  590.             // output fields  
  591.             for (var i = 0, l = aOutputFields.length; i < l; i++) {  
  592.                 // check if it is the last array member  
  593.                 if (i == aOutputFields.length - 1) {  
  594.                     fieldSeperator = '';  
  595.                 }  
  596.                 else {  
  597.                     fieldSeperator = ', ';  
  598.                 };  
  599.                 // yes, output  
  600.                 // check alias  
  601.                 if (aOutputFields[i].get('alias') != '') {  
  602.                     // yes, we have an field alias  
  603.                     selectFieldsSQL = selectFieldsSQL + aOutputFields[i].get('expression') + ' AS ' + aOutputFields[i].get('alias') + fieldSeperator;  
  604.                 }  
  605.                 else {  
  606.                     // no field alias  
  607.                     selectFieldsSQL = selectFieldsSQL + aOutputFields[i].get('expression') + fieldSeperator;  
  608.                 }  
  609.             }  
  610.               
  611.             // criteria  
  612.             for (var i = 0, l = aCriteriaFields.length; i < l; i++) {  
  613.                 if (i == 0) {  
  614.                     criteriaSQL = criteriaSQL + '\nWHERE ';  
  615.                 }  
  616.                 else {  
  617.                     criteriaSQL = criteriaSQL + 'AND ';  
  618.                 }  
  619.                 if (i == aCriteriaFields.length - 1) {  
  620.                     fieldSeperator = '';  
  621.                 }  
  622.                 else {  
  623.                     fieldSeperator = '\n';  
  624.                 }  
  625.                 criteriaSQL = criteriaSQL + aCriteriaFields[i].get('expression') + ' ' + aCriteriaFields[i].get('criteria') + fieldSeperator;  
  626.             }  
  627.               
  628.             // group by  
  629.             for (var i = 0, l = aGroupFields.length; i < l; i++) {  
  630.                 // check if it is the last array member  
  631.                 if (i == aGroupFields.length - 1) {  
  632.                     fieldSeperator = '';  
  633.                 }  
  634.                 else {  
  635.                     fieldSeperator = ', ';  
  636.                 }  
  637.                 if (i == 0) {  
  638.                     groupBySQL = '\nGROUP BY ';  
  639.                 }  
  640.                 groupBySQL = groupBySQL + aGroupFields[i].get('expression') + fieldSeperator;  
  641.             }  
  642.               
  643.             // order by  
  644.             for (var i = 0, l = aOrderFields.length; i < l; i++) {  
  645.                 // check if it is the last array member  
  646.                 if (i == aOrderFields.length - 1) {  
  647.                     fieldSeperator = '';  
  648.                 }  
  649.                 else {  
  650.                     fieldSeperator = ', ';  
  651.                 }  
  652.             }  
  653.               
  654.             return sqlOutput + selectFieldsSQL + fromSQL + criteriaSQL + groupBySQL + orderBySQL;  
  655.         },  
  656.         getJoinsByTableId: function(tableId){  
  657.             var aReturn = [];  
  658.             this.joins.each(function(join){  
  659.                 if (join.get('leftTableId') == tableId || join.get('rightTableId') == tableId) {  
  660.                     aReturn.push(join);  
  661.                 }  
  662.             });  
  663.             return aReturn;  
  664.         },  
  665.         removeTableById: function(tableID){  
  666.             var table;  
  667.             table = this.tables.getById(tableID);  
  668.             this.tables.remove(table);  
  669.         },  
  670.         getTableById: function(tableID){  
  671.             return this.tables.getById(tableID);  
  672.         },  
  673.         removeFieldById: function(id){  
  674.             var field;  
  675.             field = this.fields.getById(id);  
  676.             this.fields.remove(field);  
  677.         },  
  678.         removeFieldsByTableId: function(tableId){  
  679.             var aRecords = [];  
  680.             this.fields.each(function(model){  
  681.                 if (model.get('tableId') == tableId) {  
  682.                     aRecords.push(model);  
  683.                 }  
  684.             });  
  685.             this.fields.remove(aRecords);  
  686.         },  
  687.         addTable: function(table){  
  688.             this.tables.add(table);  
  689.         },  
  690.         addFieldRecord: function(record, bOutput){  
  691.             var tableAlias, model, expression;  
  692.             // get the tableAlias  
  693.             tableAlias = this.getTableById(record.get('tableId')).get('tableAlias');  
  694.             // build the expression  
  695.             // check if the tableAlias is not an empty string  
  696.             if (tableAlias != '') {  
  697.                 // alias is not an empty string  
  698.                 expression = tableAlias + '.' + record.get('field');  
  699.             }  
  700.             else {  
  701.                 // alias is an empty string  
  702.                 expression = record.get('tableName') + '.' + record.get('field');  
  703.             };  
  704.             // get a new field instance  
  705.             model = this.getNewField();  
  706.             // set the expression  
  707.             model.set('expression', expression);  
  708.             // set output to false per default  
  709.             model.set('output', bOutput);  
  710.             // set an id, so it is possible to remove rows if the associated table is removed  
  711.             model.set('id', record.get('id'));  
  712.             // set the field  
  713.             model.set('field', record.get('field'));  
  714.             // copy tableId to the new model instance  
  715.             model.set('tableId', record.get('tableId'));  
  716.             // copy cmp id of origin sqltable to the new model instance  
  717.             model.set('extCmpId', record.get('extCmpId'));  
  718.             this.addField(model);  
  719.         },  
  720.         addField: function(field){  
  721.             this.fields.add(field);  
  722.         },  
  723.         getNewField: function(){  
  724.             return Ext.create('Ext.ux.window.visualsqlquerybuilder.SQLFieldsModel');  
  725.         },  
  726.         removeJoinById: function(joinID){  
  727.             var join;  
  728.             join = this.joins.getById(joinID);  
  729.             this.joins.remove(join);  
  730.         },  
  731.         addJoin: function(join){  
  732.             this.joins.add(join);  
  733.         },  
  734.         arrayRemove: function(array, filterProperty, filterValue){  
  735.             var aReturn;  
  736.             aReturn = Ext.Array.filter(array, function(item){  
  737.                 var bRemove = true;  
  738.                 if (item[filterProperty] == filtervalue) {  
  739.                     bRemove = false;  
  740.                 }  
  741.                 return bRemove;  
  742.             });  
  743.             return aReturn  
  744.         }  
  745.     });  
  746.       
  747.     Ext.define('Ext.ux.window.visualsqlquerybuilder.SQLTablePanel', {  
  748.         extend: 'Ext.panel.Panel',  
  749.         alias: ['widget.sqltablepanel'],  
  750.         id: 'SQLTablePanel',  
  751.         items: [{  
  752.             xtype: 'draw',  
  753.             listeners: {  
  754.                 afterrender: function(){  
  755.                     this.initDropTarget();  
  756.                 }  
  757.             },  
  758.             initDropTarget: function(){  
  759.                 // init draw component inside qbwindow as a DropTarget  
  760.                 this.dropTarget = Ext.create('Ext.dd.DropTarget', this.el, {  
  761.                     ddGroup: 'sqlDDGroup',  
  762.                     notifyDrop: function(source, event, data){  
  763.                         var sqlTablePanel;  
  764.                         // add a sqltable to the sqlTablePanel component  
  765.                         sqlTablePanel = Ext.getCmp('SQLTablePanel');  
  766.                         sqlTablePanel.add({  
  767.                             xtype: 'sqltable',  
  768.                             constrain: true,  
  769.                             title: data.records[0].get('text')  
  770.                         }).show();  
  771.                         return true;  
  772.                     }  
  773.                 });  
  774.             }  
  775.         }]  
  776.     });  
  777.       
  778.     Ext.define('Ext.ux.window.visualsqlquerybuilder.SQLOutputPanel', {  
  779.         extend: 'Ext.panel.Panel',  
  780.         alias: ['widget.sqloutputpanel'],  
  781.         id: 'SQLOutputPanel',  
  782.         listeners: {  
  783.             afterlayout: function(){  
  784.                 SyntaxHighlighter.highlight();  
  785.             }  
  786.         },  
  787.         initComponent: function(){  
  788.             this.callParent(arguments);  
  789.         }  
  790.     });  
  791.       
  792.     Ext.define('Ext.ux.window.visualsqlquerybuilder.SQLFieldsGrid', {  
  793.         requires: ['Ext.ux.CheckColumn'],  
  794.         extend: 'Ext.grid.Panel',  
  795.         alias: ['widget.sqlfieldsgrid'],  
  796.         id: 'SQLFieldsGrid',  
  797.         store: 'SQLFieldsStore',  
  798.         columnLines: true,  
  799.         plugins: [Ext.create('Ext.grid.plugin.CellEditing', {  
  800.             clicksToEdit: 1  
  801.         })],  
  802.         viewConfig: {  
  803.             listeners: {  
  804.                 render: function(view){  
  805.                     this.dd = {};  
  806.                     this.dd.dropZone = new Ext.grid.ViewDropZone({  
  807.                         view: view,  
  808.                         ddGroup: 'SQLTableGridDDGroup',  
  809.                         handleNodeDrop: function(data, record, position){  
  810.                             // Was soll nach dem Drop passieren?  
  811.                         }  
  812.                     });  
  813.                 },  
  814.                 drop: function(node, data, dropRec, dropPosition){  
  815.                     // add new rows to the SQLFieldsGrid after a drop  
  816.                     for (var i = 0, l = data.records.length; i < l; i++) {  
  817.                         ux.vqbuilder.sqlSelect.addFieldRecord(data.records[i], false);  
  818.                     }  
  819.                 }  
  820.             }  
  821.         },  
  822.         columns: [{  
  823.             xtype: 'actioncolumn',  
  824.             menuDisabled: true,  
  825.             text: 'Action',  
  826.             width: 60,  
  827.             moveGridRow: function(grid, record, index, direction){  
  828.                 var store = grid.getStore();  
  829.                 if (direction < 0) {  
  830.                     index--;  
  831.                     if (index < 0) {  
  832.                         return;  
  833.                     }  
  834.                 }  
  835.                 else {  
  836.                     index++;  
  837.                     if (index >= grid.getStore().getCount()) {  
  838.                         return;  
  839.                     }  
  840.                 }  
  841.                 // prepare manual syncing  
  842.                 store.suspendAutoSync();  
  843.                 // disable firing store events  
  844.                 store.suspendEvents();  
  845.                 // remove record and insert record at new index  
  846.                 store.remove(record);  
  847.                 store.insert(index, record);  
  848.                 // enable firing store events  
  849.                 store.resumeEvents();  
  850.                 store.resumeAutoSync();  
  851.                 // manual sync the store  
  852.                 store.sync();  
  853.             },  
  854.             items: [{  
  855.                 icon: 'resources/images/up_arrow.gif',  
  856.                 tooltip: 'Move Column Up',  
  857.                 getClass: function(value, metadata, record){  
  858.                     var store, index;  
  859.                     store = record.store;  
  860.                     index = store.indexOf(record);  
  861.                     if (index == 0) {  
  862.                         return 'x-action-icon-disabled';  
  863.                     }  
  864.                     else {  
  865.                         return 'x-grid-center-icon';  
  866.                     }  
  867.                 },  
  868.                 handler: function(grid, rowIndex, colIndex){  
  869.                     var rec = grid.getStore().getAt(rowIndex);  
  870.                     this.moveGridRow(grid, rec, rowIndex, -1);  
  871.                 }  
  872.             }, {  
  873.                 icon: 'resources/images/down_arrow.gif',  
  874.                 getClass: function(value, metadata, record){  
  875.                     var store, index;  
  876.                     store = record.store;  
  877.                     index = store.indexOf(record);  
  878.                     if ((index + 1) == store.getCount()) {  
  879.                         return 'x-action-icon-disabled';  
  880.                     }  
  881.                     else {  
  882.                         return 'x-grid-center-icon';  
  883.                     }  
  884.                 },  
  885.                 tooltip: 'Move Column Down',  
  886.                 handler: function(grid, rowIndex, colIndex){  
  887.                     var rec = grid.getStore().getAt(rowIndex);  
  888.                     this.moveGridRow(grid, rec, rowIndex, 1);  
  889.                 }  
  890.             }, {  
  891.                 icon: 'resources/images/remove.gif',  
  892.                 iconCls: 'x-grid-center-icon',  
  893.                 tooltip: 'Delete Column',  
  894.                 handler: function(grid, rowIndex, colIndex){  
  895.                     var rec = grid.getStore().getAt(rowIndex), store, tableId, tableGrid, selectionModel, bDel = true;  
  896.                     // rec contains column grid model, the one to remove  
  897.                     // get tableId of original sqltable  
  898.                     tableId = rec.get('extCmpId');  
  899.                     // get the sql tables grid and its selection  
  900.                     tableGrid = Ext.getCmp(tableId).down('gridpanel');  
  901.                     selectionModel = tableGrid.getSelectionModel();  
  902.                     Ext.Array.each(selectionModel.getSelection(), function(selection){  
  903.                         // deselect the selection wich corresponds to the column   
  904.                         // we want to remove from the column grid  
  905.                         if (rec.get('id') == selection.get('id')) {  
  906.                             // deselect current selection  
  907.                             // deselection will lead to removal, look for method deselect at the SQLTableGrid  
  908.                             selectionModel.deselect(selection);  
  909.                             bDel = false;  
  910.                         }  
  911.                     });  
  912.                     if (bDel) {  
  913.                         store = grid.getStore();  
  914.                         store.remove(rec);  
  915.                     }  
  916.                 }  
  917.             }]  
  918.         }, {  
  919.             xtype: 'checkcolumn',  
  920.             sortable: false,  
  921.             text: 'Output',  
  922.             flex: 0.075,  
  923.             menuDisabled: true,  
  924.             dataIndex: 'output',  
  925.             align: 'center'  
  926.         }, {  
  927.             xtype: 'gridcolumn',  
  928.             text: 'Expression',  
  929.             sortable: false,  
  930.             menuDisabled: true,  
  931.             flex: 0.225,  
  932.             dataIndex: 'expression',  
  933.             editor: 'textfield'  
  934.         }, {  
  935.             xtype: 'gridcolumn',  
  936.             text: 'Aggregate',  
  937.             flex: 0.125,  
  938.             sortable: false,  
  939.             menuDisabled: true,  
  940.             dataIndex: 'aggregate',  
  941.             editor: 'textfield'  
  942.         }, {  
  943.             xtype: 'gridcolumn',  
  944.             text: 'Alias',  
  945.             flex: 0.125,  
  946.             sortable: false,  
  947.             menuDisabled: true,  
  948.             dataIndex: 'alias',  
  949.             editor: 'textfield'  
  950.         }, {  
  951.             xtype: 'gridcolumn',  
  952.             text: 'Sort Type',  
  953.             flex: 0.125,  
  954.             sortable: false,  
  955.             menuDisabled: true,  
  956.             dataIndex: 'sorttype'  
  957.         }, {  
  958.             xtype: 'gridcolumn',  
  959.             text: 'Sort Order',  
  960.             flex: 0.125,  
  961.             sortable: false,  
  962.             menuDisabled: true,  
  963.             dataIndex: 'sortorder'  
  964.         }, {  
  965.             xtype: 'checkcolumn',  
  966.             text: 'Grouping',  
  967.             flex: 0.075,  
  968.             sortable: false,  
  969.             menuDisabled: true,  
  970.             dataIndex: 'grouping',  
  971.             align: 'center'  
  972.         }, {  
  973.             xtype: 'gridcolumn',  
  974.             text: 'Criteria',  
  975.             flex: 0.125,  
  976.             sortable: false,  
  977.             menuDisabled: true,  
  978.             dataIndex: 'criteria',  
  979.             editor: 'textfield'  
  980.         }],  
  981.         initComponent: function(){  
  982.             this.callParent(arguments);  
  983.         }  
  984.     });  
  985.       
  986.     Ext.define('Ext.ux.window.visualsqlquerybuilder.SQLTableTree', {  
  987.         extend: 'Ext.tree.Panel',  
  988.         alias: ['widget.sqltabletree'],  
  989.         id: 'SQLTableTree',  
  990.         listeners: {  
  991.             afterrender: function(){  
  992.                 this.initTreeDragZone();  
  993.             },  
  994.             itemdblclick: function(view, record, el, index, event){  
  995.                 var sqlTablePanel;  
  996.                 // add a sqltable to the sqlTablePanel component  
  997.                 sqlTablePanel = Ext.getCmp('SQLTablePanel');  
  998.                 sqlTablePanel.add({  
  999.                     xtype: 'sqltable',  
  1000.                     constrain: true,  
  1001.                     title: record.get('text')  
  1002.                 }).show();  
  1003.                   
  1004.             }  
  1005.         },  
  1006.         initTreeDragZone: function(){  
  1007.             // init tree view as a ViewDragZone  
  1008.             this.view.dragZone = new Ext.tree.ViewDragZone({  
  1009.                 view: this.view,  
  1010.                 ddGroup: 'sqlDDGroup',  
  1011.                 dragText: '{0} ausgew盲hlte Tabelle{1}',  
  1012.                 repairHighlightColor: 'c3daf9',  
  1013.                 repairHighlight: Ext.enableFx  
  1014.             });  
  1015.         },  
  1016.         initComponent: function(){  
  1017.          
  1018.             this.store = Ext.create('Ext.data.TreeStore', {  
  1019.                 root: {  
  1020.                     text: 'Tables',  
  1021.                     expanded: true  
  1022.                 },  
  1023.                 proxy: {  
  1024.                     type: 'ajax',  
  1025.                     url: 'data/database.cfc?method=getTables'  
  1026.                 }  
  1027.             });  
  1028.               
  1029.             this.callParent(arguments);  
  1030.         }  
  1031.     });  
  1032.       
  1033.       
  1034. ......................
复制代码
跳转到指定楼层
快速回复 返回顶部 返回列表