1 19 package org.netbeans.modules.db.sql.visualeditor.querybuilder; 20 import org.netbeans.modules.db.sql.visualeditor.ui.ConnectionStatusPanel; 21 22 import java.awt.Component ; 23 import java.awt.Dialog ; 24 import java.awt.Cursor ; 25 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.awt.event.KeyEvent ; 29 import java.awt.event.KeyListener ; 30 31 import java.awt.datatransfer.Clipboard ; 32 import java.awt.datatransfer.ClipboardOwner ; 33 import java.awt.datatransfer.Transferable ; 34 35 import java.beans.PropertyChangeEvent ; 36 import java.beans.PropertyChangeListener ; 37 import java.sql.Connection ; 38 import java.util.Iterator ; 39 40 import javax.swing.JButton ; 41 import javax.swing.JComponent ; 42 import javax.swing.ImageIcon ; 43 import javax.swing.JDialog ; 44 import javax.swing.JOptionPane ; 45 import javax.swing.SwingUtilities ; 46 import javax.swing.AbstractAction ; 47 import javax.swing.KeyStroke ; 48 import javax.swing.InputMap ; 49 import javax.swing.ActionMap ; 50 import javax.swing.JFrame ; 51 import javax.swing.RepaintManager ; 52 53 import javax.swing.text.DefaultEditorKit ; 54 55 import java.sql.DatabaseMetaData ; 56 import java.sql.ResultSet ; 57 import java.sql.SQLException ; 58 59 import java.util.ArrayList ; 60 import java.util.List ; 61 import java.util.HashMap ; 62 import java.util.Map ; 63 64 import org.openide.DialogDescriptor; 65 import org.openide.ErrorManager; 66 import org.openide.NotifyDescriptor; 67 import org.openide.DialogDisplayer; 68 import org.openide.actions.DeleteAction; 69 import org.openide.nodes.Node; 70 71 import org.openide.util.HelpCtx ; 72 import org.openide.util.NbBundle; 73 import org.openide.util.actions.ActionPerformer; 74 import org.openide.util.actions.SystemAction; 75 76 import org.openide.windows.TopComponent; 77 import org.openide.windows.WindowManager; 78 79 import org.netbeans.modules.db.sql.visualeditor.Log; 80 81 import org.netbeans.modules.db.sql.visualeditor.querymodel.ColumnProvider; 82 import org.netbeans.modules.db.sql.visualeditor.querymodel.Column; 83 import org.netbeans.modules.db.sql.visualeditor.querymodel.JoinTable; 84 import org.netbeans.modules.db.sql.visualeditor.querymodel.OrderBy; 85 86 import org.netbeans.modules.db.sql.visualeditor.parser.ParseException; 87 import org.netbeans.modules.db.sql.visualeditor.parser.TokenMgrError; 88 90 93 import java.sql.PreparedStatement ; 94 import java.sql.ParameterMetaData ; 95 96 106 public class QueryBuilder extends TopComponent 107 implements 108 ClipboardOwner , 109 KeyListener , 110 ColumnProvider { 111 112 private static Map qbMap = new HashMap (); 114 115 private String lastQuery; 116 117 private static final boolean CACHE_QueryBulderInstances = false ; 119 120 private boolean firstTimeGenerateText = true; 125 126 128 QueryBuilderPane _queryBuilderPane; 129 QueryModel _queryModel; 130 boolean _updateModel = true; 131 boolean _updateText = true; 132 boolean _graphicsEnabled = true; 133 134 136 private boolean DEBUG = false; 137 139 142 private SqlStatementMetaDataCache metaDataCache = null ; 143 144 151 154 private String _parseErrorMessage = null ; 155 156 public boolean isSchemaName( String schemaName ) { 158 String [] schemas = metaDataCache.getSchemas() ; 159 for (int i = 0 ; i < schemas.length ; i++ ) { 160 if ( schemas[i].equals(schemaName)) { 161 Log.log(" found schema name "+schemaName) ; 162 return true ; 163 } 164 } 165 return false ; 166 } 167 168 public boolean isTableName( String tableName ) { 169 try { 170 String x = checkTableName( tableName ) ; 171 if ( x != null ) 172 return true ; 173 } catch( SQLException se) { 174 } 176 return false ; 177 } 178 179 public boolean isColumnName( String columnName ) { 180 181 try { 182 return metaDataCache.getAllColumnNames().containsKey(columnName) ; 183 }catch( SQLException se) { 184 } 186 return false ; 187 } 188 189 193 194 private final transient DeleteActionPerformer deleteActionPerformer = new DeleteActionPerformer(); 195 196 197 protected final transient CopyCutActionPerformer copyActionPerformer = 198 new CopyCutActionPerformer(true); 199 200 201 protected final transient CopyCutActionPerformer cutActionPerformer = 202 new CopyCutActionPerformer(false); 203 204 205 public void lostOwnership(Clipboard clipboard, Transferable contents) { 207 } 208 209 boolean isSelectionEmpty() { 210 Node[] nodes = getActivatedNodes(); 211 if ( (nodes != null) && (nodes.length != 0) ) 212 return false; 213 else 214 return true; 215 } 216 217 private boolean isActivated() { 218 return this == TopComponent.getRegistry().getActivated(); 219 } 220 221 222 public int getPersistenceType() { 223 return TopComponent.PERSISTENCE_NEVER ; 224 } 225 226 public String preferredID() { 227 return "QueryEditor" ; } 229 230 231 232 235 public void activateActions() { 236 if (isSelectionEmpty()) { 237 disableDelete(); 238 } else { 239 enableDelete(); 240 } 241 disableCutCopy(); 243 } 244 245 248 public void deactivateActions() { 249 if (deleteActionPerformer != null) { 250 } 252 } 253 254 256 void enableDelete() { 257 if (!isActivated()) { 258 return; 259 } 260 deleteActionPerformer.setEnabled(true); 261 } 262 263 264 void disableDelete() { 265 if (!isActivated()) { 266 return; 267 } 268 deleteActionPerformer.setEnabled(false); 269 DeleteAction da = (DeleteAction)SystemAction.get(DeleteAction.class); 270 da.setEnabled(false); 271 } 272 273 274 void disableCutCopy() { 275 if ( DEBUG ) 276 System.out.println(" disableCutCopy called. " + "\n" ); cutActionPerformer.setEnabled(false); 279 copyActionPerformer.setEnabled(false); 280 } 281 282 283 class DeleteActionPerformer extends AbstractAction implements ActionPerformer { 284 285 public void actionPerformed(ActionEvent e) { 286 performAction(null); 287 } 288 289 public void performAction(SystemAction action) { 291 SwingUtilities.invokeLater(new Runnable () { 293 public void run() { 294 deleteSelection(); 295 } 296 } 297 ); 298 } 299 } 300 301 302 class CopyCutActionPerformer extends AbstractAction implements ActionPerformer { 303 304 boolean isCopy; 305 306 307 public CopyCutActionPerformer(boolean b) { 308 isCopy = b; 309 } 310 311 public void actionPerformed(ActionEvent e) { 312 performAction(null); 313 } 314 315 316 public void performAction(SystemAction action) { 317 } 319 } 320 321 322 323 void deleteSelection() { 324 if ( DEBUG ) 325 System.out.println(" deleteSelection called. " + "\n" ); java.awt.KeyboardFocusManager kbfm = java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager(); 327 java.awt.Component c = kbfm.getFocusOwner(); 328 if ( c != null ) { 329 java.awt.Container p = c.getParent(); 330 while ( p != null ) { 331 if ( p instanceof QueryBuilderGraphFrame ) { 332 333 Node[] nodes = getActivatedNodes(); 334 if (nodes == null || nodes.length == 0) { 335 return; 336 } 337 for (int i = 0; i < nodes.length; i++) { 338 if ( ( nodes[i] instanceof CondNode ) ) { 339 _queryBuilderPane.getQueryBuilderGraphFrame().removeNode((CondNode)nodes[i]); 340 } else if ( ( nodes[i] instanceof JoinNode ) ) { 341 _queryBuilderPane.getQueryBuilderGraphFrame().removeNode((JoinNode)nodes[i]); 342 } else if ( ( nodes[i] instanceof TableNode ) ) { 343 _queryBuilderPane.getQueryBuilderGraphFrame().removeNode((TableNode)nodes[i]); 344 } 345 } 346 } 347 p = p.getParent(); 348 } 349 } 350 } 351 352 protected void installActions(ActionMap map, InputMap keys) { 353 359 map.put("delete", deleteActionPerformer); map.put(DefaultEditorKit.copyAction, copyActionPerformer); 361 map.put(DefaultEditorKit.cutAction, cutActionPerformer); 362 363 380 keys.put(KeyStroke.getKeyStroke("DELETE"), "delete"); 381 } 382 383 384 388 public static Component openCustomizerPanel( SqlStatement sqlStatement) { 389 390 Log.err.log(ErrorManager.INFORMATIONAL, 391 "Entering QueryBuilder.openCustomizerPanel"); 393 showBusyCursor( true ); 394 395 QueryBuilder qb ; 396 try { 397 qb = new QueryBuilder(sqlStatement); 398 } catch (SQLException sqle ) { 399 qb = null ; 400 ConnectionStatusPanel csp = new ConnectionStatusPanel() ; 402 csp.configureDisplay(sqlStatement.getConnectionInfo(), false,sqle.getLocalizedMessage(), "", 0, false ) ; 403 csp.displayDialog( sqlStatement.getConnectionInfo() ) ; 405 } 406 final QueryBuilder queryBuilder = qb ; 407 SwingUtilities.invokeLater( new Runnable () { 408 public void run() { 409 if ( queryBuilder != null) { 410 queryBuilder.open(); 411 queryBuilder.requestActive(); 412 } 413 showBusyCursor( false ); 414 } 415 }) ; 416 417 queryBuilder.getTextAreaFocusInvokeLater(); 418 419 return queryBuilder; 420 } 421 422 private SqlStatement sqlStatement = null ; 424 426 private QueryBuilder(SqlStatement sqlStatement) throws SQLException { 427 428 Log.err.log(ErrorManager.INFORMATIONAL, "Entering QueryBuilder ctor"); 430 this.sqlStatement = sqlStatement ; 432 433 this.metaDataCache = sqlStatement.getMetaDataCache() ; 434 435 String title = sqlStatement.getTitle() ; 436 437 setName(title); 439 setDisplayName(title); 440 441 setLayout(new java.awt.BorderLayout ()); 442 443 ImageIcon imgIcon = 444 new ImageIcon (getClass().getResource("/org/netbeans/modules/db/sql/visualeditor/resources/query-editor-tab.png")); if (imgIcon != null) 446 setIcon(imgIcon.getImage()); 447 448 _queryBuilderPane = new QueryBuilderPane(this); 449 450 add(_queryBuilderPane); 452 453 addKeyListener(this); 454 } 455 456 void getGraphFrameCanvasFocus() { 457 _queryBuilderPane.getQueryBuilderGraphFrame().getCanvasFocus (); 458 } 459 460 void getTextAreaFocusInvokeLater() { 461 javax.swing.SwingUtilities.invokeLater(new Runnable () { 462 public void run() { 463 _queryBuilderPane.getQueryBuilderSqlTextArea().requestFocus( true ); 464 _queryBuilderPane.getQueryBuilderSqlTextArea().requestFocusInWindow (); 465 } 466 }) ; 467 } 468 469 public void keyTyped(KeyEvent e) { 470 } 471 472 473 public void keyReleased(KeyEvent e) { 474 } 475 476 478 public void keyPressed(KeyEvent e) { 479 handleKeyPress(e); 480 } 481 482 public void handleKeyPress(KeyEvent e) { 483 if( e.isAltDown() ) { 484 int code = e.getKeyCode(); 485 switch(code) { 486 case KeyEvent.VK_1: 488 if ( DEBUG ) 489 System.out.println(" Alt + 1 pressed. "); getGraphFrameCanvasFocus(); 492 break; 493 case KeyEvent.VK_2: 495 if ( DEBUG ) 496 System.out.println(" Alt + 2 pressed. "); if ( _queryBuilderPane.getQueryBuilderInputTable().getRowCount() > 0 ) { 498 _queryBuilderPane.getQueryBuilderInputTable().setRowSelectionInterval(0, 0); 499 _queryBuilderPane.getQueryBuilderInputTable().requestFocus( true ); 500 } 501 break; 502 case KeyEvent.VK_3: 504 if ( DEBUG ) 505 System.out.println(" Alt + 3 pressed. "); _queryBuilderPane.getQueryBuilderSqlTextArea().requestFocus( true ); 507 break; 508 case KeyEvent.VK_4: 510 if ( DEBUG ) 511 System.out.println(" Alt + 4 pressed. "); _queryBuilderPane.getQueryBuilderResultTable().requestFocus( true ); 513 break; 514 } 515 } 516 } 517 518 public SqlStatement getSqlStatement() { 519 return this.sqlStatement ; 520 } 521 522 List getCachedAllTablesInDataSource() throws SQLException { 523 return metaDataCache.getTables() ; 524 530 } 531 532 541 542 String checkTableName( String tableName ) throws SQLException { 543 544 if ( tableName == null || tableName.length() < 1) { 545 return tableName ; 546 } 547 Log.log("checkTableName called. tableName = " + tableName); 549 String [] descrip = parseTableName(tableName) ; 550 String paramSchemaName = descrip[0] ; 551 String paramTableName = descrip[1] ; 552 553 if ( paramSchemaName != null ) { 554 return checkFullTableName( tableName ) ; 555 } 556 557 String returnTable = null ; 558 559 List tables = metaDataCache.getTables() ; 560 for ( Iterator i = tables.iterator(); i.hasNext() ; ) { 562 String fullNameDb = (String )i.next() ; 564 String tableNameDb = parseTableName(fullNameDb)[1] ; 565 if ( tableNameDb.equalsIgnoreCase( paramTableName ) ) { 566 returnTable = fullNameDb ; 567 break ; 568 } 569 } 570 571 if ( returnTable == null ) { 572 String fullAliasTableName = _queryModel.getFullTableName(paramTableName) ; 573 if ( fullAliasTableName != null && tableName.equals(fullAliasTableName)) { 574 return null; 575 576 } else if ( fullAliasTableName != null ) { 577 return checkTableName( fullAliasTableName ) ; 578 } 579 } 580 581 if ( returnTable != null ) 582 metaDataCache.getColumnNames(returnTable ) ; 583 584 return returnTable ; 586 } 587 588 606 607 String checkFullTableName( String fullTableName ) throws SQLException { 608 609 Log.log("checkFullTableName called. tableName = " + fullTableName ); 611 String returnTable = null ; 612 613 if ( parseTableName(fullTableName)[0] == null ) { 614 return checkTableName(fullTableName) ; 616 } 617 618 List tables = metaDataCache.getTables() ; 619 for ( Iterator i = tables.iterator(); i.hasNext() ; ) { 620 String fullNameDb = (String )i.next() ; 622 if ( fullNameDb.equalsIgnoreCase( fullTableName ) ) { 623 returnTable = fullNameDb ; 624 break ; 625 } 626 } 627 628 if ( returnTable != null ) metaDataCache.getColumnNames(returnTable ) ; 630 631 return returnTable ; 632 } 633 634 635 642 String checkColumnName( String tableName, String columnName ) throws SQLException { 643 Log.log("checkColumnName() " + tableName + ", " + columnName ); 645 String tabName = checkTableName( tableName ) ; 646 647 List columns = metaDataCache.getColumnNames(tabName) ; 648 649 if ( columns == null ) return null ; 650 651 for ( int k = 0; k < columns.size(); k++ ) { 652 String columnDB = (String ) columns.get(k); 653 if ( columnName.equals( columnDB ) ) { 655 return columnName; 656 } 657 else if ( columnName.equalsIgnoreCase( columnDB ) ) { 660 return columnDB ; 661 } 662 } 663 664 return null; 666 } 667 668 674 675 boolean checkColumnNameForTable( Column col, String tableName ) { 676 String columnName = col.getColumnName(); 677 678 Log.log("checkColumnNameForTable for " + tableName + " . " + columnName ); 680 String fullTableNameFromAlias = _queryModel.getFullTableName( tableName ); 681 if ( fullTableNameFromAlias != null ) { 682 tableName = fullTableNameFromAlias ; 683 } 684 boolean retVal = false ; 685 686 List cols ; 688 String checkedTable ; 689 try { 690 checkedTable = checkTableName( tableName ) ; 691 if (checkedTable == null) return false ; 692 cols = metaDataCache.getColumnNames(checkedTable) ; 693 } catch (SQLException sqle) { 694 Log.log(" ** problems getting metadata " + sqle.getMessage()) ; 695 return false ; 696 697 } 698 if ( "*".equals(columnName)) { retVal = true ; 700 if ( fullTableNameFromAlias == null && ! ( checkedTable.equals(col.getTableSpec()) ) ) { 701 col.setTableSpec(col.getTableSpec(), checkedTable ) ; 702 Log.log( " adjust table to " + checkedTable) ; 703 } 704 } else { 705 for ( int icnt = 0 ; icnt < cols.size() ; icnt++ ) { 706 if ( columnName.equalsIgnoreCase( (String )cols.get(icnt))) { 707 col.setColumnName(col.getColumnName(), (String )cols.get(icnt) ); 708 Log.log( " adjust colname to " + (String )cols.get(icnt) ) ; 709 if ( col.getTableSpec() == null ) { 710 col.setTableSpec(col.getTableSpec(), checkedTable ) ; 711 Log.log( " adjust table to " + checkedTable) ; 712 } 713 retVal = true ; 714 break ; 715 } 716 } 717 } 718 719 Log.log("checkColumnNameForTable found="+retVal ); 721 return retVal ; 722 723 774 } 775 776 boolean checkTableColumnName( Column col ) throws SQLException { 780 781 String tableSpec = col.getTableSpec() ; 782 String tableName = col.getFullTableName() ; 783 String colName = col.getColumnName() ; 784 785 Log.log("checkTableColumnName called. " + " tableSpec = " + tableSpec + 786 " tableName = " + tableName + " . " + colName ); 788 if ( "*".equals(col.getColumnName() ) && tableSpec == null ) { 789 Log.log("checkTableColunName col=*, notable ") ; 791 return true ; } 793 String checkedTableName = checkTableName( tableSpec ) ; 794 795 String fullTableNameFromAlias = null ; 796 fullTableNameFromAlias = _queryModel.getFullTableName( tableSpec ); 801 Log.log("checkTableColumnName called. " + 803 " checkedTableName = " + checkedTableName + 804 " fullTableNameFromAlias = " + fullTableNameFromAlias ); if ( checkedTableName == null ) { 806 return false; } 809 810 else if ( ( fullTableNameFromAlias != null ) && 814 ( ! fullTableNameFromAlias.equalsIgnoreCase( tableSpec ) ) ) { 815 if (DEBUG) 816 System.out.println("setColumnTableName called. " + 817 " checkedTableName = " + checkedTableName + 818 " tableSpec = " + tableSpec + 819 " fullTableNameFromAlias = " + fullTableNameFromAlias + "\n" ); col.setColumnTableName( checkedTableName ); 821 col.setColumnCorrName( tableSpec ); 822 } else if ( ! checkedTableName.equals( tableName ) ) { 823 if (DEBUG) 826 System.out.println("setTableSpec called. " + 827 " checkedTableName = " + checkedTableName + "\n" ); col.setTableSpec( tableName, checkedTableName ); 829 } 830 831 String columnName = col.getColumnName(); 832 833 if (columnName.equals("*")) return true ; 834 835 String checkedColumnName = checkColumnName( checkedTableName, 836 columnName ) ; 837 if (DEBUG) 838 System.out.println("column Name = " + columnName + "\n" + "checked column Name = " + checkedColumnName + "\n" ); if ( checkedColumnName == null ) { 841 return false; } else if ( ! checkedColumnName.equals( columnName ) ) { 844 if (DEBUG) 845 System.out.println("set column name called. oldColumnName = " + columnName + " newColumnName = " + checkedColumnName + "\n" ); col.setColumnName( columnName, checkedColumnName ); 850 } 851 852 return true; 853 } 854 855 861 public boolean checkDatabaseAndDisable(String query) { 862 if ( query == null ) query = _queryBuilderPane.getQueryBuilderSqlTextArea().getText() ; 863 if ( checkDatabaseConnection() == false ) { 864 Log.log("checkDatabaseConnection returns false ... \n " ); disableVisualEditing(query); 867 return false; 868 } 869 return true ; 870 } 871 877 boolean populate(String query) { 878 return populate( query, false) ; 879 } 880 881 boolean populate(String query, boolean forceParse ) { 882 883 Log.log("Entering populate, forceParse: " + forceParse); 885 if ( ! forceParse ) { 886 if ( query.trim().equals( _queryBuilderPane.getQueryBuilderSqlTextArea().getText().trim())) { 887 Log.log(" skipping populate(), no change") ; return true ; 890 } 891 } 892 893 if ( query.trim().equals( lastQuery ) ) { 895 Log.log(" skipping populate(), no change") ; return true ; 898 } 899 else { 900 lastQuery = new String (query.trim()); 901 } 902 903 905 try { 907 parseQuery(query); 908 909 915 if (!checkQuery()) 916 return false; 917 918 _queryBuilderPane.getQueryBuilderGraphFrame().setQBGFEnabled( true ) ; 919 _queryBuilderPane.setQueryBuilderInputTableEnabled( true ) ; 920 _queryBuilderPane.getQueryBuilderGraphFrame().setTableColumnValidity(false) ; 921 _queryBuilderPane.getQueryBuilderGraphFrame().setGroupBy(_queryModel.hasGroupBy() ); 922 _graphicsEnabled=true; 923 _queryBuilderPane.getQueryBuilderSqlTextArea().setQueryText(query); 924 } catch (ParseException pe) { 925 926 Log.err.log(ErrorManager.ERROR, "Parse error: " + pe.getLocalizedMessage()); promptForContinuation(pe.getMessage(), query); 928 return false; 929 } catch (TokenMgrError tme) { 930 931 Log.err.log(ErrorManager.ERROR, "Parse error: " + tme.getLocalizedMessage()); promptForContinuation(tme.getMessage(), query); 933 return false; 934 935 } catch (SQLException sqe) { 936 lastException = sqe ; 937 Log.err.log(ErrorManager.ERROR, "Parse error: " + sqe.getLocalizedMessage()); promptForContinuation(sqe.getMessage(), query); 939 return false; 940 } 941 942 _parseErrorMessage = null ; 943 945 this.generate(); 947 948 saveSqlCommand(query); 950 951 _queryBuilderPane.getQueryBuilderSqlTextArea().requestFocus(); 952 953 return true; 954 } 955 956 957 959 private boolean promptForContinuation(String msg, String query) { 960 961 968 Object [] options = { 969 NbBundle.getMessage(QueryBuilder.class, "CONTINUE"), NbBundle.getMessage(QueryBuilder.class, "CANCEL") }; 972 if ( _queryBuilderPane.getQueryBuilderSqlTextArea().queryChanged() ) { 973 int val = JOptionPane.showOptionDialog( 974 this, 975 (msg + "\n\n" + 976 NbBundle.getMessage(QueryBuilder.class, "PARSE_ERROR_MESSAGE") 977 + 978 NbBundle.getMessage(QueryBuilder.class, "PARSE_ERROR_MESSAGE_PROMPT")), 979 NbBundle.getMessage(QueryBuilder.class, "PARSE_ERROR"), 980 JOptionPane.YES_NO_OPTION, 981 JOptionPane.QUESTION_MESSAGE, 982 null, 983 options, 984 options[0]); 985 if (val==JOptionPane.NO_OPTION) { Log.err.log(ErrorManager.WARNING, "Query execution canceled"); _queryBuilderPane.getQueryBuilderSqlTextArea().restoreLastGoodQuery(); 988 _queryBuilderPane.getQueryBuilderGraphFrame().setQBGFEnabled( true ) ; 989 _queryBuilderPane.setQueryBuilderInputTableEnabled( true ) ; 990 _graphicsEnabled=true; 991 _parseErrorMessage = null ; 992 } else { _parseErrorMessage = NbBundle.getMessage(QueryBuilder.class, "PARSE_ERROR_MESSAGE"); disableVisualEditing(query); 995 } 996 } else { 997 _parseErrorMessage = NbBundle.getMessage(QueryBuilder.class, "PARSE_ERROR_MESSAGE"); disableVisualEditing(query); 1000 } 1001 return false; 1002 } 1003 1004 1005 void disableVisualEditing(String query) { 1008 _graphicsEnabled=false; 1009 _queryBuilderPane.clear(); 1010 _queryBuilderPane.getQueryBuilderGraphFrame().setQBGFEnabled( false ) ; 1011 _queryBuilderPane.setQueryBuilderInputTableEnabled( false ) ; 1012 1013 String command = sqlStatement.getCommand(); 1014 if ( query != null && query.trim().length() != 0) { 1015 _queryBuilderPane.getQueryBuilderSqlTextArea().setQueryText(query); 1016 setSqlCommand(query) ; 1017 } 1018 else { 1019 _queryBuilderPane.getQueryBuilderSqlTextArea().setQueryText(command); 1020 } 1021 } 1022 1023 void showTableColumnNameError( String error ) { 1024 String msg = NbBundle.getMessage(QueryBuilder.class, "TABLE_COLUMN_NAME_ERROR"); 1025 NotifyDescriptor d = 1026 new NotifyDescriptor.Message( error + " : " + msg + "\n\n", NotifyDescriptor.ERROR_MESSAGE); 1027 DialogDisplayer.getDefault().notify(d); 1028 _parseErrorMessage = error + " : " + msg + "\n\n" ; 1029 String query = getUnformattedSqlString(); 1030 disableVisualEditing(query); 1031 } 1032 1033 1034 boolean checkQuery() throws SQLException { 1035 1036 if ( ( getUnformattedSqlString() != null ) || 1037 ( _queryBuilderPane.getQueryBuilderGraphFrame().checkTableColumnValidity() ) ) { 1038 1039 if ( ! checkFrom() ) return false; 1041 1042 1065 1066 _queryModel.replaceStar(this); 1069 1070 if ( ! checkSelect() ) return false; 1072 1073 if ( ! checkWhere() ) return false; 1075 1076 if ( ! checkGroupBy() ) return false; 1078 1079 if ( ! checkHaving() ) return false; 1081 1082 if ( ! checkOrderBy() ) return false; 1084 1085 } else { 1086 _queryModel.replaceStar(this); 1089 } 1090 return true; 1091 } 1092 1093 1094 1097 boolean checkFrom() throws SQLException { 1098 1099 if (DEBUG) 1100 System.out.println("checkFrom called... \n " ); 1102 List fromTables; 1105 if ( _queryModel.getFrom() != null ) { 1107 1108 fromTables = _queryModel.getFrom().getTableList(); 1109 for ( int i = 0; i < fromTables.size(); i++ ) { 1110 1111 String fromTableName = (String ) 1112 ( (JoinTable) fromTables.get(i) ).getFullTableName(); 1113 String fromTableSpec = (String ) 1114 ( (JoinTable) fromTables.get(i) ).getTableSpec(); 1115 String checkedFullTableName = checkFullTableName( fromTableName ) ; 1116 1117 if (DEBUG) 1118 System.out.println("checkFullTableName called fromTableName = " + fromTableName + " returns " + 1119 checkedFullTableName + " \n " ); if ( checkedFullTableName == null ) { 1121 showTableColumnNameError( fromTableName ) ; 1123 return false; 1124 } else if ( ! checkedFullTableName.equals( fromTableName ) ) { 1125 if (DEBUG) 1128 System.out.println( 1129 " fromTableName = " + fromTableName + " fromTableSpec = " + fromTableSpec + " \n" ); _queryModel.getFrom().setTableSpec( 1133 fromTableSpec, checkedFullTableName ); 1134 } 1135 1136 List fromColumns = new ArrayList (); 1138 ( (JoinTable) fromTables.get(i) ).getReferencedColumns(fromColumns); 1139 for ( int j = 0; j < fromColumns.size(); j++ ) { 1140 Column fromColumn = (Column) fromColumns.get(j); 1141 if (! checkTableColumnName( fromColumn)) { 1142 showTableColumnNameError( fromColumn.getColumnName() ); 1143 return false; 1144 } 1145 } 1146 } 1147 } 1148 return true; 1149 } 1150 1151 1153 boolean checkSelect() throws SQLException { 1154 if (DEBUG) 1155 System.out.println("checkSelect called. _queryModel.getSelect() = " + _queryModel.getSelect() + "\n " ); if ( _queryModel.getSelect() != null ) { 1157 ArrayList selectColumns = new ArrayList (); 1158 _queryModel.getSelect().getReferencedColumns(selectColumns); 1159 if ( ! checkColumns( selectColumns ) ) 1160 return false; 1161 } 1162 return true; 1163 } 1164 1165 boolean checkWhere() throws SQLException { 1166 if (DEBUG) 1167 System.out.println("checkWhere called... \n " ); if ( _queryModel.getWhere() != null ) { 1169 ArrayList whereColumns = new ArrayList (); 1170 _queryModel.getWhere().getReferencedColumns(whereColumns); 1171 if ( ! checkColumns( whereColumns ) ) return false; 1172 } 1173 return true; 1174 } 1175 1176 boolean checkGroupBy() throws SQLException { 1177 if (DEBUG) 1178 System.out.println("checkGroupBy called... \n " ); if ( _queryModel.getGroupBy() != null ) { 1180 ArrayList groupByColumns = new ArrayList (); 1181 _queryModel.getGroupBy().getReferencedColumns(groupByColumns); 1182 if ( ! checkColumns( groupByColumns ) ) return false; 1183 } 1184 return true; 1185 } 1186 1187 1188 boolean checkHaving() throws SQLException { 1189 if (DEBUG) 1190 System.out.println("checkHaving called... \n " ); if ( _queryModel.getHaving() != null ) { 1192 ArrayList havingColumns = new ArrayList (); 1193 _queryModel.getHaving().getReferencedColumns(havingColumns); 1194 if ( ! checkColumns( havingColumns ) ) return false; 1195 } 1196 return true; 1197 } 1198 1199 1200 boolean checkOrderBy() throws SQLException { 1201 if (DEBUG) 1202 System.out.println("checkOrderBy called... \n " ); OrderBy orderBy = _queryModel.getOrderBy(); 1204 if ( orderBy != null ) { 1205 ArrayList orderByColumns = new ArrayList (); 1206 for ( int i = 0; i < orderBy.getSortSpecificationCount(); i++ ) { 1207 Column sortColumn = orderBy.getSortSpecification(i).getColumn(); 1208 orderByColumns.add( sortColumn); 1209 } 1210 if ( ! checkColumns( orderByColumns ) ) return false; 1211 } 1212 return true; 1213 } 1214 1215 1226 boolean checkColumns( ArrayList columns ) throws SQLException { 1227 Log.log ( " checkColumns called. columns.size = " + columns.size() ); for ( int i = 0; i < columns.size(); i++ ) { 1229 Column column = (Column) columns.get(i); 1230 String columnTableSpec = column.getTableSpec(); 1231 String columnFullTableName = column.getFullTableName(); 1232 1233 if ( ( columnFullTableName == null ) && 1237 ( _queryModel.getFrom() != null ) ) 1238 { 1239 List fromTables = _queryModel.getFrom().getTableList(); 1242 boolean found=false; 1243 for ( int j = 0; j < fromTables.size(); j++ ) { 1244 String fromTableName = (String ) ( (JoinTable) fromTables.get(j) ).getFullTableName(); 1245 String fromTableSpec = (String ) 1247 ( (JoinTable) fromTables.get(j) ).getTableSpec(); 1248 1249 if (DEBUG) 1250 System.out.println( 1251 " checkColumns called " + " fromTableName = " + fromTableName + " fromTableSpec = " + fromTableSpec + " \n" ); if ( checkColumnNameForTable( column, fromTableSpec )) { 1258 found=true; 1259 break; 1260 } 1261 } 1262 if (!found) { 1264 showTableColumnNameError( column.getColumnName() ) ; 1266 return false; 1267 } 1268 } 1269 1270 if (! checkTableColumnName(column) 1271 ) 1275 { 1276 showTableColumnNameError( column.getColumnName() ) ; 1277 return false; 1278 } 1279 } 1282 return true; 1283 } 1284 1285 1289 void parseQuery(String query) throws ParseException { 1290 1291 Log.err.log(ErrorManager.INFORMATIONAL, "Entering parseQuery, query: " + query); 1293 if (_queryModel==null) 1295 _queryModel = new QueryModel(); 1296 1297 _queryModel.parse(query); 1298 } 1299 1300 1301 1303 public void saveSqlCommand(String query) { 1304 1305 _queryBuilderPane.getQueryBuilderSqlTextArea().saveLastGoodQuery (_queryBuilderPane.getSqlTextAreaText()); 1306 1307 1310 String newQuery = getUnformattedSqlString(); 1311 if (!newQuery.equals( sqlStatement.getCommand())) { 1312 Log.log("QB: setting sql command to: " + newQuery) ; setSqlCommand( newQuery ) ; 1314 } 1315 } 1316 1317 void setSqlCommand(String query) { 1318 sqlStatement.setCommand(query) ; 1319 } 1320 1333 1337 1338 public void executeQuery(String query) { 1339 1340 Log.err.log(ErrorManager.INFORMATIONAL, 1341 "Entering QueryBuilder.executeQuery, query:\n" + query); 1343 String sqlCommand = _queryBuilderPane.getSqlTextAreaText() ; ResultSet result = null ; 1346 Connection connection = null ; 1347 PreparedStatement myStatement = null ; 1348 1349 showBusyCursor ( true ); 1350 1351 boolean canExecute = true ; 1352 1353 ParameterMetaData pmd = null; 1354 int paramCount = 0; 1355 1356 try { 1357 connection = sqlStatement.getConnection() ; 1358 myStatement = connection.prepareStatement(sqlCommand) ; 1359 pmd = myStatement.getParameterMetaData(); 1360 paramCount = pmd.getParameterCount(); 1361 if (DEBUG) { 1362 System.out.println(" Parameter Count = " + paramCount + "\n"); 1363 for (int i = 1; i <= paramCount; i++) { 1364 System.out.println(" Parameter Type = " + pmd.getParameterType(i) + "\n"); 1365 System.out.println(" Parameter Type Name = " + pmd.getParameterTypeName(i) + "\n"); 1366 } 1367 } 1368 } catch ( SQLException e) { 1369 reportDatabaseError(e); canExecute = false ; 1371 } catch ( AbstractMethodError e) { 1372 Log.log(ErrorManager.ERROR, 1374 "Error occurred when trying to retrieve table information: " + e); String title = NbBundle.getMessage(QueryBuilder.class, "PROCESSING_ERROR"); 1376 JOptionPane.showMessageDialog( this, e.toString() + "\n\n", title, JOptionPane.ERROR_MESSAGE ); 1377 canExecute = false ; 1378 } 1379 1380 if ( canExecute && ( _queryModel != null ) ) 1382 { 1383 if ( getParseErrorMessage() == null && _queryModel.isParameterized()) { 1384 1385 ArrayList list = new ArrayList (); 1386 _queryModel.getParameterizedPredicates(list); 1387 String [] parameters = new String [list.size()]; 1388 String [] values = new String [list.size()]; 1389 1390 for (int i = 0; i < parameters.length; i++) { 1391 parameters[i] = new String ((String ) list.get(i)); 1392 } 1393 ParameterizedQueryDialog pqDlg = new ParameterizedQueryDialog( 1394 parameters, true); 1395 if (pqDlg.getReturnStatus() == ParameterizedQueryDialog.RETURNED_OK) { 1397 values = pqDlg.getParameterValues(); 1398 1399 try { 1400 for (int i = 0; i < values.length; i++) { 1401 if (DEBUG) { 1402 System.out.println(" command = " + sqlCommand + "\n"); 1403 System.out.println("PreparedStatement i = " + i + " values = " + values[i] + "\n"); 1404 } 1405 myStatement.setObject(i+1, ((String )values[i]), 1406 pmd.getParameterType(i+1) ); 1407 } 1408 1409 } catch ( SQLException e) { 1410 reportDatabaseError(e); canExecute = false ; 1412 1413 } 1414 } else { 1415 canExecute = false ; 1417 } 1418 } 1419 else if (paramCount != 0) { 1420 ArrayList list = new ArrayList (paramCount); 1422 if (DEBUG) { 1423 System.out.println(" param count = " + paramCount + "\n"); 1424 System.out.println(" list size = " + list.size()+ "\n"); 1425 } 1426 String [] parameters = new String [paramCount]; 1427 String [] values = new String [paramCount]; 1428 if (DEBUG) { 1429 System.out.println(" parameters size = " + parameters.length+ "\n"); 1430 System.out.println(" values size = " + values.length+ "\n"); 1431 } 1432 1433 for (int i = 0; i < paramCount; i++) { 1434 parameters[i] = new String ( "Parameter " + new Integer (i).toString()); 1435 } 1436 if (DEBUG) { 1437 for (int i = 0; i < parameters.length; i++) { 1438 System.out.println(" parameter = " + parameters[i] + "\n"); 1439 } 1440 } 1441 ParameterizedQueryDialog pqDlg = new ParameterizedQueryDialog( 1442 parameters, true); 1443 if (pqDlg.getReturnStatus() == ParameterizedQueryDialog.RETURNED_OK) { 1445 values = pqDlg.getParameterValues(); 1446 1447 try { 1448 for (int i = 0; i < values.length; i++) { 1449 if (DEBUG) { 1450 System.out.println(" command = " + sqlCommand + "\n"); 1451 System.out.println("PreparedStatement i = " + i + " values = " + values[i] + "\n"); 1452 } 1453 myStatement.setObject(i+1, ((String )values[i]), 1454 pmd.getParameterType(i+1) ); 1455 } 1456 1457 } catch ( SQLException e) { 1458 reportDatabaseError(e); canExecute = false ; 1460 } 1461 } else { 1462 canExecute = false ; 1464 } 1465 } 1466 } 1467 1468 if ( canExecute ) { 1470 1471 try { 1472 boolean hasResults = myStatement.execute() ; 1473 if ( hasResults ) { 1474 result = myStatement.getResultSet() ; 1475 _queryBuilderPane.getQueryBuilderResultTable().displayResultSet(result); 1476 result.close() ; 1477 } 1478 1479 } catch (SQLException e) { 1480 reportDatabaseError(e); } 1482 } 1483 1484 try { 1486 if ( myStatement != null ) { 1487 myStatement.close() ; 1488 } 1489 } catch( SQLException se) { 1490 Log.err.log(ErrorManager.INFORMATIONAL, "Error Closing statement: " + se.getLocalizedMessage()); } 1492 1493 try { 1494 if ( connection != null ) { 1495 connection.close() ; 1496 } 1497 } catch( SQLException se) { 1498 Log.err.log(ErrorManager.INFORMATIONAL, "Error Closing connection: " + se.getLocalizedMessage()); } 1500 1501 showBusyCursor ( false ); 1502 Log.err.log(ErrorManager.INFORMATIONAL, "Returning from QueryBuilder.executeQuery"); 1504 } 1505 1506 1507 1510 void generate() { 1511 1512 Log.err.log(ErrorManager.INFORMATIONAL, "Entering QueryBuilder.generate"); 1514 _updateText = false; 1516 try { 1517 1518 _queryBuilderPane.clear(); 1521 1522 this.generateGraph(); 1524 1525 } finally { 1526 _updateText = true; 1527 } 1528 1529 this.generateText(); 1534 } 1535 1536 1537 1540 void generateGraph() { 1541 if (_graphicsEnabled) 1543 _queryBuilderPane.getQueryBuilderGraphFrame().generateGraph(_queryModel); 1544 } 1545 1546 1549 void generateText() { 1550 if (_graphicsEnabled) { 1552 _queryBuilderPane.getQueryBuilderSqlTextArea().setQueryText(_queryModel.genText()); 1553 if ( ! firstTimeGenerateText ) { 1554 saveSqlCommand(_queryBuilderPane.getSqlTextAreaText()); 1555 } else { 1556 firstTimeGenerateText = false; 1557 } 1558 } 1559 } 1560 1561 1562 void displayResultSet(ResultSet rs) { 1563 _queryBuilderPane.getQueryBuilderResultTable().displayResultSet(rs); 1564 } 1565 1566 1567 1573 1579 1580 1582 1585 QueryModel getQueryModel() { 1586 return _queryModel; 1587 } 1588 1589 1592 1593 QueryBuilderPane getQueryBuilderPane () 1594 { 1595 return ( _queryBuilderPane ); 1596 } 1597 1598 1601 boolean isParameterized() { 1602 return _queryModel.isParameterized(); 1603 } 1604 1605 1606 1609 List getAllTables() throws SQLException { 1610 1615 return metaDataCache.getTables() ; 1616 } 1617 1618 1649 1650 1651 1676 1680 List getAllTablesInDataSource() throws SQLException { 1681 1682 1684 return metaDataCache.getTables() ; 1685 1686 1708 } 1709 1710 1711 1714 1748 1749 1753 1791 1793 public void getColumnNamesFull(String fullTableName, List columnNames) { 1794 try { 1795 columnNames.addAll( getColumnNames(fullTableName)) ; 1796 } catch(SQLException sqle) { 1797 } 1799 } 1800 public List getColumnNames(String fullTableName) throws SQLException { 1801 1802 Log.err.log(ErrorManager.INFORMATIONAL, 1803 "Entering QueryBuilder.getColumnNames, fullTableName: " + fullTableName); 1805 return metaDataCache.getColumnNames( fullTableName ) ; 1806 1836 } 1837 1838 1839 1842 List getPrimaryKeys(String fullTableName) throws SQLException { 1843 1844 Log.log(ErrorManager.INFORMATIONAL, 1845 "Entering QueryBuilder.getPrimaryKeys, fullTableName: " + fullTableName ); 1847 return metaDataCache.getPrimaryKeys(fullTableName) ; 1848 1888 } 1889 1890 1891 1896 List getForeignKeys(String fullTableName) throws SQLException { 1897 1898 Log.err.log(ErrorManager.INFORMATIONAL, 1899 "Entering QB.getForeignKeys, fullTableName: " + fullTableName); 1901 1902 1904 1910 List keys = metaDataCache.getForeignKeys(fullTableName, true); 1911 keys.addAll( metaDataCache.getForeignKeys(fullTableName, false)); 1912 1913 return keys; 1914 1915 } 1916 1917 1920 1970 1975 List getImportedKeyColumns(String fullTableName) throws SQLException { 1976 1977 return metaDataCache.getImportedKeyColumns(fullTableName) ; 1978 2019 } 2020 2021 2026 String [] findForeignKey(String oldFullTableName, String newFullTableName, List foreignKeys) { 2027 2028 Log.err.log(ErrorManager.INFORMATIONAL, 2029 "Entering QB.findForeignKey, t1: "+oldFullTableName + " t2: "+newFullTableName); 2031 if (foreignKeys!=null) { 2032 for (int i=0; i<foreignKeys.size(); i++) { 2033 String [] key = (String [])foreignKeys.get(i); 2034 if ((key[0].equalsIgnoreCase(newFullTableName)&&key[2].equalsIgnoreCase(oldFullTableName)) || 2035 (key[0].equalsIgnoreCase(oldFullTableName)&&key[2].equalsIgnoreCase(newFullTableName))) 2036 return (String []) foreignKeys.get(i); 2037 } 2038 } 2039 Log.err.log(ErrorManager.INFORMATIONAL, "No key found"); return null; 2041 } 2042 2043 2044 2047 String [] findForeignKey(String fullTableName1, String colName1, 2048 String fullTableName2, String colName2) throws SQLException { 2049 2050 Log.err.log(ErrorManager.INFORMATIONAL, 2051 "Entering findForeignKey, t1: " + fullTableName1 + " c1: " + colName1 + " t2: " + fullTableName2 + " c2: " + colName2); 2054 List foreignKeys = getForeignKeys(fullTableName1); 2056 if (foreignKeys!=null) { 2057 for (int i=0; i<foreignKeys.size(); i++) { 2058 String [] key = (String [])foreignKeys.get(i); 2059 if ((key[0].equalsIgnoreCase(fullTableName1) && key[1].equalsIgnoreCase(colName1) 2060 && key[2].equalsIgnoreCase(fullTableName2) && key[3].equalsIgnoreCase(colName2)) || 2061 (key[0].equalsIgnoreCase(fullTableName2) && key[1].equalsIgnoreCase(colName2) 2062 && key[2].equalsIgnoreCase(fullTableName1) && key[3].equalsIgnoreCase(colName1))) 2063 return (String []) foreignKeys.get(i); 2064 } 2065 } 2066 Log.err.log(ErrorManager.INFORMATIONAL, "No key found"); return null; 2068 } 2069 2070 public String getConnectionInfo() { 2071 return sqlStatement.getConnectionInfo() ; 2072 } 2073 2074 2076 2083 protected void componentShowing() { 2084 2085 Log.err.log(ErrorManager.INFORMATIONAL, 2086 "Entering QueryBuilder.componentShowing()"); 2088 String command = sqlStatement.getCommand(); 2089 2090 if (_queryModel==null) 2091 _queryModel = new QueryModel(); 2092 2093 Log.log(" * command=" + command) ; 2094 2095 2097 if ((command==null) || (command.trim().length()==0)) { 2101 Log.log("QBShowing command is null") ; 2102 setVisible(true); 2103 this.repaint(); 2104 String msg = NbBundle.getMessage(QueryBuilder.class, "EMPTY_QUERY_ADD_TABLE"); 2105 NotifyDescriptor d = 2106 new NotifyDescriptor.Message(msg + "\n\n", NotifyDescriptor.ERROR_MESSAGE); DialogDisplayer.getDefault().notify(d); 2108 2109 _queryBuilderPane.getQueryBuilderGraphFrame().addTable() ; 2110 2111 } else { 2112 2113 2120 String queryText = getUnformattedSqlString(); 2121 if ( queryText == null || (! command.trim().equalsIgnoreCase( queryText.trim() ) ) ) { 2123 this.populate(command); 2124 setVisible(true); 2125 this.repaint(); 2126 } 2127 } 2128 activateActions(); 2129 2130 _queryBuilderPane.getQueryBuilderSqlTextArea().requestFocus(); 2131 2132 if ( DEBUG ) 2133 System.out.println(" _queryBuilderPane.getQueryBuilderSqlTextArea().requestFocus () called. " + "\n" ); 2135 } 2136 2137 2142 protected void componentHidden() { 2143 Log.err.log(ErrorManager.INFORMATIONAL, 2144 "QB.componentHidden, updating command to: " + _queryBuilderPane.getSqlTextAreaText()); String command = sqlStatement.getCommand(); 2146 if ((command!=null) && (command.trim().length()!=0)) { 2147 String queryText = getUnformattedSqlString(); 2148 2149 if ( queryText == null || (! command.trim().equalsIgnoreCase( queryText.trim() ) ) ) { 2151 if ( _graphicsEnabled ) { 2152 boolean good = this.populate(queryText, true); 2153 if ( !good) { 2154 setSqlCommand(queryText); } 2156 } else { 2157 setSqlCommand(queryText) ; 2158 } 2159 } 2160 } 2161 deactivateActions(); 2162 } 2163 2164 private String getUnformattedSqlString() { 2165 return _queryBuilderPane.getQueryBuilderSqlTextArea().getText() ; 2166 } 2168 2169 2170 protected void componentOpened() { 2171 Log.err.log(ErrorManager.INFORMATIONAL, 2172 "Entering QueryBuilder.componentOpened()"); 2174 activateActions(); 2175 ActionMap map = getActionMap(); 2176 InputMap keys = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 2177 installActions(map, keys); 2178 sqlStatement.addPropertyChangeListener(sqlStatementListener) ; 2179 2180 2183 } 2184 2185 2187 protected void componentClosed() { 2188 Log.err.log(ErrorManager.INFORMATIONAL, 2189 "QueryBuilder.componentClosed()"); 2191 deactivateActions(); 2192 2193 sqlStatement.close() ; 2194 lastQuery = null; 2195 } 2196 2197 2201 private PropertyChangeListener sqlStatementListener = new PropertyChangeListener () { 2202 public void propertyChange(PropertyChangeEvent evt) { 2203 String propName = evt.getPropertyName() ; 2205 Log.log("QB sqlStatement property change: " + propName ) ; 2206 if ( propName.equals(SqlStatement.COMMAND)) { 2207 Log.err.log(" newValue=" + sqlStatement.getCommand()) ; 2208 populate( sqlStatement.getCommand() ) ; 2209 _queryBuilderPane.getQueryBuilderSqlTextArea().requestFocus(); 2210 2211 } else if ( propName.equals(SqlStatement.CONNECTION_INFO)) { 2212 Log.err.log(" resetting connections") ; 2213 2215 } else if ( propName.equals(SqlStatement.TITLE) ) { 2216 Log.err.log(" title to " + sqlStatement.getTitle()) ; SwingUtilities.invokeLater(new Runnable () { 2218 public void run() { 2219 setDisplayName(sqlStatement.getTitle()) ; 2220 } 2221 }) ; 2222 2223 } else if ( propName.equals(SqlStatement.CLOSING)) { 2224 Log.err.log(" closing...") ; SwingUtilities.invokeLater(new Runnable () { 2226 public void run() { 2227 close() ; 2228 } 2229 }) ; 2230 } 2231 } 2232 } ; 2233 2234 private SQLException lastException = null ; 2236 2237 2238 protected boolean checkDatabaseConnection() { 2241 2242 Log.err.log( "checkDatabaseConnection()" ); boolean keepChecking = true ; 2244 boolean connected = false ; 2245 while ( keepChecking ) { 2246 2247 try { 2248 metaDataCache.checkDataBaseConnection() ; 2249 connected = true ; 2250 } catch (SQLException sqle ) { 2251 lastException = sqle ; 2252 connected = false ; 2253 } 2254 2255 if ( ! connected ) { 2256 boolean retry = showRetryDialog() ; 2258 if (! retry) { 2259 keepChecking = false ; 2260 } 2261 } else keepChecking = false ; 2262 } 2263 return connected ; 2264 } 2265 2266 2270 2275 int ii = 0 ; 2276 public boolean showRetryDialog() { 2277 Log.log("enter showRetryDialog() " + ii++ ) ; 2278 ConnectionStatusPanel csp = new ConnectionStatusPanel() ; 2279 csp.configureDisplay(sqlStatement.getConnectionInfo(), false, lastException.getLocalizedMessage(), "", 0, false ) ; 2280 csp.setGeneralInfo(NbBundle.getMessage(QueryBuilder.class, "DATABASE_CONNECTION_ERROR") ) ; csp.setFooterInfo(NbBundle.getMessage(QueryBuilder.class, "NO_DATABASE_CONNECTION") ) ; 2283 final JButton retryButton = new JButton (NbBundle.getMessage(QueryBuilder.class, "RETRY_AND_CONTINUE")) ; 2284 final JButton cancelButton = new JButton (NbBundle.getMessage(QueryBuilder.class, "CANCEL_AND_CONTINUE")) ; 2285 2286 final Object [] retVal = new Object [1] ; 2288 ActionListener listener = new ActionListener () { 2289 public void actionPerformed(java.awt.event.ActionEvent evt) { 2290 Log.log(" retry dialog event: " + evt) ; 2291 retVal[0] = evt.getSource() ; 2292 } 2293 2294 }; 2295 2296 DialogDescriptor dlg = new DialogDescriptor(csp, 2297 NbBundle.getMessage(ConnectionStatusPanel.class, "ConStat_title", sqlStatement.getConnectionInfo()), true, 2299 new Object [] {retryButton, cancelButton}, cancelButton, 2300 DialogDescriptor.DEFAULT_ALIGN, null, listener); 2301 2302 dlg.setClosingOptions( null ); 2303 2304 Dialog dialog = (Dialog ) DialogDisplayer.getDefault().createDialog(dlg); 2305 dialog.setResizable(true); 2306 dialog.pack() ; 2307 2308 dialog.show(); 2310 2311 boolean val = ( retVal[0] == retryButton ) ; 2312 Log.err.log( " * dlg says: Retry=" + val ) ; 2313 return val ; 2314 } 2315 2316 2317 private void checkMetaData() throws SQLException { 2319 2332 } 2333 2334 public void reportDatabaseError(SQLException e) { 2335 2336 Log.log(ErrorManager.ERROR, 2337 "Error occurred when trying to retrieve table information: " + e); 2339 String msg = 2340 (e.getErrorCode() == 17023) ? 2341 NbBundle.getMessage(QueryBuilder.class, "UNSUPPORTED_FEATURE") : 2343 e.getLocalizedMessage(); 2344 2345 String title = NbBundle.getMessage(QueryBuilder.class, "PROCESSING_ERROR"); 2347 2348 JOptionPane.showMessageDialog( this, msg + "\n\n", title, JOptionPane.ERROR_MESSAGE ); 2349 2355 2356 } 2357 2358 String getParseErrorMessage() { 2359 return _parseErrorMessage; 2360 } 2361 2362 2368 private static String [] parseTableName(String fullTableName) { 2369 2370 String [] retVal = new String [2] ; 2371 2372 String [] table = fullTableName.split("\\."); if (table.length>1) { 2374 retVal[0] = table[0]; 2375 retVal[1] = table[1]; 2376 } else { 2377 retVal[0] = null ; 2378 retVal[1] = table[0]; 2379 } 2380 return retVal ; 2381 } 2382 2383 2388 static void showBusyCursor(final boolean busy) { 2389 if(SwingUtilities.isEventDispatchThread()) { 2390 doShowBusyCursor(busy); 2391 } else { 2392 SwingUtilities.invokeLater(new Runnable () { 2393 public void run() { 2394 doShowBusyCursor(busy); 2395 } 2396 }); 2397 } 2398 } 2399 2400 private static void doShowBusyCursor(boolean busy) { 2401 JFrame mainWindow = (JFrame )WindowManager.getDefault().getMainWindow(); 2402 if(busy){ 2403 RepaintManager.currentManager(mainWindow).paintDirtyRegions(); 2404 mainWindow.getGlassPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 2405 mainWindow.getGlassPane().setVisible(true); 2406 mainWindow.repaint(); 2407 } else { 2408 mainWindow.getGlassPane().setVisible(false); 2409 mainWindow.getGlassPane().setCursor(null); 2410 mainWindow.repaint(); 2411 } 2412 } 2413 2414 public HelpCtx getHelpCtx() { 2415 return new HelpCtx( "projrave_ui_elements_editors_about_query_editor" ); } 2417 2418} 2419 | Popular Tags |