1 30 31 32 package org.hsqldb.util; 33 34 import java.sql.Connection ; 35 import java.sql.DatabaseMetaData ; 36 import java.sql.PreparedStatement ; 37 import java.sql.ResultSet ; 38 import java.sql.ResultSetMetaData ; 39 import java.sql.SQLException ; 40 import java.sql.Statement ; 41 import java.util.Vector ; 42 import java.awt.GridBagConstraints ; 43 import java.awt.GridBagLayout ; 44 import java.awt.Insets ; 45 import java.awt.Label ; 46 import java.awt.Panel ; 47 import java.awt.ScrollPane ; 48 import java.awt.event.ActionEvent ; 49 import java.awt.event.ActionListener ; 50 import java.awt.event.ItemEvent ; 51 import java.awt.event.ItemListener ; 52 import java.awt.event.TextEvent ; 53 import java.awt.event.TextListener ; 54 55 62 63 public class ZaurusTableForm extends ScrollPane 66 implements TextListener , ItemListener , ActionListener { 67 68 Connection cConn; 70 DatabaseMetaData dbmeta; 71 72 String tableName; 74 75 ZaurusComponent[] komponente; 77 78 String [] columns; 80 81 short[] columnTypes; 83 84 String [] primaryKeys; 86 87 int[] pkColIndex; 89 90 String [][] importedKeys; 93 94 int[][] imColIndex; 96 97 String [] refTables; 99 String [][] refColumns; 100 101 int[][] refColIndex; 103 104 Object [][] resultRowPKs; 107 108 int numberOfResult; 110 111 PreparedStatement pStmt; 113 114 int aktRowNr; 116 117 public ZaurusTableForm(String name, Connection con) { 118 119 super(); 120 121 tableName = name; 122 cConn = con; 123 124 this.fetchColumns(); 125 this.fetchPrimaryKeys(); 126 127 this.fetchImportedKeys(); 133 this.initGUI(); 134 } 135 136 public void cancelChanges() { 138 this.showAktRow(); 139 } 140 141 public int deleteRow() { 143 144 String deleteString = "DELETE FROM " + tableName 146 + this.generatePKWhere(); 147 148 try { 150 151 PreparedStatement ps = cConn.prepareStatement(deleteString); 153 154 ps.clearParameters(); 155 156 int i; 157 158 for (int j = 0; j < primaryKeys.length; j++) { 159 ps.setObject(j + 1, resultRowPKs[aktRowNr][j]); 160 } 162 ps.executeUpdate(); 163 } catch (SQLException e) { 164 ZaurusEditor.printStatus("SQL Exception: " + e.getMessage()); 165 166 return 0; 167 } 169 numberOfResult--; 171 172 for (int i = aktRowNr; i < numberOfResult; i++) { 173 for (int j = 0; j < primaryKeys.length; j++) { 174 resultRowPKs[i][j] = resultRowPKs[i + 1][j]; 175 } 176 } 177 178 200 201 int actionCode; 214 215 if (numberOfResult == 0) { 216 217 actionCode = 1; 219 220 ZaurusEditor.printStatus("Last row was deleted."); 221 222 return actionCode; 223 } else if (numberOfResult == aktRowNr) { 224 225 aktRowNr--; 228 229 if (aktRowNr == 0) { 230 231 actionCode = 2; 233 } else { 234 235 actionCode = 4; 237 } } else { 239 240 if (numberOfResult >= 2 && aktRowNr < numberOfResult - 1) { 242 243 actionCode = 4; 245 } else { 246 actionCode = 3; 247 } } 249 250 this.showAktRow(); 251 ZaurusEditor.printStatus("Row was deleted."); 252 253 return actionCode; 254 } 255 256 public String getPrimaryKeysString() { 258 259 String result = ""; 260 261 for (int i = 0; i < primaryKeys.length; i++) { 262 if (result != "") { 263 result += ", "; 264 } 265 266 result += primaryKeys[i]; 267 } 269 return result; 270 } 271 272 public void insertNewRow() { 274 275 for (int i = 0; i < komponente.length; i++) { 277 komponente[i].clearContent(); 278 } 280 for (int i = 0; i < primaryKeys.length; i++) { 282 komponente[pkColIndex[i]].setEditable(true); 283 } 284 285 ZaurusEditor.printStatus("enter a new row for table " + tableName); 286 } 287 288 public boolean nextRow() { 291 292 if (aktRowNr + 1 == numberOfResult) { 293 return false; 294 } 295 296 aktRowNr++; 297 298 this.showAktRow(); 299 300 return (aktRowNr + 1 < numberOfResult); 301 } 302 303 public boolean prevRow() { 306 307 if (aktRowNr == 0) { 308 return false; 309 } 310 311 aktRowNr--; 312 313 this.showAktRow(); 314 315 return (aktRowNr > 0); 316 } 317 318 public boolean saveChanges() { 321 322 int[] changedColumns = new int[columns.length]; 328 int countChanged = 0; 329 330 String updateString = ""; 332 333 for (int i = 0; i < columns.length; i++) { 334 if (komponente[i].hasChanged()) { 335 if (updateString != "") { 336 updateString += ", "; 337 } 338 339 updateString += columns[i] + "=?"; 340 changedColumns[countChanged++] = i; 341 } 342 } 344 if (countChanged > 0) { 345 updateString = "UPDATE " + tableName + " SET " + updateString 346 + this.generatePKWhere(); 347 348 try { 350 351 PreparedStatement ps = cConn.prepareStatement(updateString); 353 354 ps.clearParameters(); 355 356 int i; 357 358 for (i = 0; i < countChanged; i++) { 359 ps.setObject(i + 1, 360 komponente[changedColumns[i]].getContent()); 361 362 } 365 for (int j = 0; j < primaryKeys.length; j++) { 367 ps.setObject(i + j + 1, resultRowPKs[aktRowNr][j]); 368 } 370 ps.executeUpdate(); 371 ZaurusEditor.printStatus("changed row was saved to table " 372 + tableName); 373 374 return true; 375 } catch (SQLException e) { 376 ZaurusEditor.printStatus("SQL Exception: " + e.getMessage()); 377 378 return false; 379 } } else { 381 382 return true; 384 } } 386 387 public boolean saveNewRow() { 390 391 boolean onePKempty = false; 393 int tmp; 394 395 for (tmp = 0; tmp < primaryKeys.length; tmp++) { 396 if (komponente[pkColIndex[tmp]].getContent().equals("")) { 397 onePKempty = true; 398 399 break; 400 } 401 } 402 403 if (onePKempty) { 404 komponente[pkColIndex[tmp]].requestFocus(); 405 ZaurusEditor.printStatus("no value for primary key " 406 + primaryKeys[tmp]); 407 408 return false; 409 } 411 String insertString = "INSERT INTO " + tableName + " VALUES("; 413 414 for (int j = 0; j < columns.length; j++) { 415 if (j > 0) { 416 insertString += ", "; 417 } 418 419 insertString += "?"; 420 } 422 insertString += ")"; 423 424 try { 426 427 PreparedStatement ps = cConn.prepareStatement(insertString); 429 430 ps.clearParameters(); 431 432 int i; 433 434 for (i = 0; i < columns.length; i++) { 435 ps.setObject(i + 1, komponente[i].getContent()); 436 } 437 438 ps.executeUpdate(); 439 ZaurusEditor.printStatus("new row was saved to table " 440 + tableName); 441 442 return true; 443 } catch (SQLException e) { 444 ZaurusEditor.printStatus("SQL Exception: " + e.getMessage()); 445 446 return false; 447 } } 449 450 public int searchRows(String [] words, boolean allWords, 454 boolean ignoreCase, boolean noMatchWhole) { 455 456 String where = this.generateWhere(words, allWords, ignoreCase, 462 noMatchWhole); 463 Vector temp = new Vector (20); 464 465 try { 466 Statement stmt = cConn.createStatement(); 467 ResultSet rs = stmt.executeQuery("SELECT " 468 + this.getPrimaryKeysString() 469 + " FROM " + tableName + where); 470 471 while (rs.next()) { 472 Object [] pkValues = new Object [primaryKeys.length]; 473 474 for (int i = 0; i < primaryKeys.length; i++) { 475 pkValues[i] = rs.getObject(pkColIndex[i] + 1); 476 } 478 temp.addElement(pkValues); 479 } 480 481 rs.close(); 482 } catch (SQLException e) { 483 ZaurusEditor.printStatus("SQL Exception: " + e.getMessage()); 484 485 return -1; 486 } 488 resultRowPKs = new Object [temp.size()][primaryKeys.length]; 489 numberOfResult = temp.size(); 490 491 for (int i = 0; i < primaryKeys.length; i++) { 492 for (int j = 0; j < temp.size(); j++) { 493 resultRowPKs[j][i] = ((Object []) temp.elementAt(j))[i]; 494 } } 497 String stmtString = "SELECT * FROM " + tableName; 499 500 try { 501 pStmt = cConn.prepareStatement(stmtString 502 + this.generatePKWhere()); 503 } catch (SQLException e) { 504 System.out.println("SQL Exception: " + e.getMessage()); 505 } 507 if (numberOfResult > 0) { 509 this.disablePKFields(); 510 511 aktRowNr = 0; 512 513 this.showAktRow(); 514 } 516 return numberOfResult; 518 } 519 520 public void actionPerformed(ActionEvent e) {} 521 522 public void textValueChanged(TextEvent e) { 523 524 for (int i = 0; i < columns.length; i++) { 525 if (komponente[i] == e.getSource()) { 526 komponente[i].setChanged(); 527 528 break; 529 } 530 } 531 } 532 533 public void itemStateChanged(ItemEvent e) { 534 535 for (int i = 0; i < columns.length; i++) { 536 if (komponente[i] == e.getSource()) { 537 komponente[i].setChanged(); 538 539 break; 540 } 541 } 542 } 543 544 private void disablePKFields() { 549 550 for (int i = 0; i < primaryKeys.length; i++) { 551 komponente[pkColIndex[i]].setEditable(false); 552 } } 554 555 private void fillZChoice(ZaurusChoice zc, String tab, String col) { 559 560 try { 561 if (cConn == null) { 562 return; 563 } 564 565 Statement stmt = cConn.createStatement(); 566 ResultSet rs = stmt.executeQuery("SELECT * FROM " + tab 567 + " ORDER BY " + col); 568 ResultSetMetaData rsmd = rs.getMetaData(); 569 int numberOfColumns = rsmd.getColumnCount(); 570 int colIndex = rs.findColumn(col); 571 572 while (rs.next()) { 573 String tmp = ""; 574 575 for (int i = 1; i <= numberOfColumns; i++) { 576 if (i > 1) { 577 tmp += "; "; 578 } 579 580 tmp += rs.getString(i); 581 } 583 zc.add(tmp, rs.getString(colIndex)); 584 } 585 586 rs.close(); 587 } catch (SQLException e) { 588 System.out.println("SQL Exception: " + e.getMessage()); 589 } } 591 592 private void fetchColumns() { 594 595 Vector temp = new Vector (20); 596 Vector tempType = new Vector (20); 597 598 try { 599 if (cConn == null) { 600 return; 601 } 602 603 if (dbmeta == null) { 604 dbmeta = cConn.getMetaData(); 605 } 606 607 ResultSet colList = dbmeta.getColumns(null, null, tableName, "%"); 608 609 while (colList.next()) { 610 temp.addElement(colList.getString("COLUMN_NAME")); 611 tempType.addElement(new Short (colList.getShort("DATA_TYPE"))); 612 } 613 614 colList.close(); 615 } catch (SQLException e) { 616 ZaurusEditor.printStatus("SQL Exception: " + e.getMessage()); 617 } 618 619 columns = new String [temp.size()]; 620 621 temp.copyInto(columns); 622 623 columnTypes = new short[temp.size()]; 624 625 for (int i = 0; i < columnTypes.length; i++) { 626 columnTypes[i] = ((Short ) tempType.elementAt(i)).shortValue(); 627 } 628 } 629 630 private void fetchImportedKeys() { 632 633 Vector imKeys = new Vector (20); 634 Vector imKeyNames = null; 635 Vector refTabs = new Vector (20); 636 Vector refCols = new Vector (20); 637 Vector refColNames = null; 638 639 try { 640 if (cConn == null) { 641 return; 642 } 643 644 if (dbmeta == null) { 645 dbmeta = cConn.getMetaData(); 646 } 647 648 ResultSet colList = dbmeta.getImportedKeys(null, null, tableName); 649 String pkTable, pkColumn, fkColumn; 650 int keySeq; 651 652 while (colList.next()) { 653 pkTable = colList.getString("PKTABLE_NAME"); 654 pkColumn = colList.getString("PKCOLUMN_NAME"); 655 fkColumn = colList.getString("FKCOLUMN_NAME"); 656 keySeq = colList.getInt("KEY_SEQ"); 657 658 if (keySeq == 1) { 659 if (imKeyNames != null) { 660 imKeys.addElement(imKeyNames); 661 refCols.addElement(refColNames); 662 } 664 imKeyNames = new Vector (20); 665 refColNames = new Vector (20); 666 667 refTabs.addElement(pkTable); 668 } 670 imKeyNames.addElement(fkColumn); 671 refColNames.addElement(pkColumn); 672 } 673 674 if (imKeyNames != null) { 675 imKeys.addElement(imKeyNames); 676 refCols.addElement(refColNames); 677 } 679 colList.close(); 680 } catch (SQLException e) { 681 ZaurusEditor.printStatus("SQL Exception: " + e.getMessage()); 682 } 683 684 int numberOfConstraints = imKeys.size(); 686 687 importedKeys = new String [numberOfConstraints][]; 688 imColIndex = new int[numberOfConstraints][]; 689 refTables = new String [numberOfConstraints]; 690 refColumns = new String [numberOfConstraints][]; 691 refColIndex = new int[numberOfConstraints][]; 692 693 for (int i = 0; i < numberOfConstraints; i++) { 694 Vector keys = (Vector ) imKeys.elementAt(i); 695 Vector cols = (Vector ) refCols.elementAt(i); 696 int numberOfKeys = keys.size(); 697 698 importedKeys[i] = new String [numberOfKeys]; 699 imColIndex[i] = new int[numberOfKeys]; 700 refColumns[i] = new String [numberOfKeys]; 701 refColIndex[i] = new int[numberOfKeys]; 702 refTables[i] = (String ) refTabs.elementAt(i); 703 704 for (int j = 0; j < numberOfKeys; j++) { 706 importedKeys[i][j] = (String ) keys.elementAt(j); 707 imColIndex[i][j] = this.getColIndex(importedKeys[i][j]); 708 refColumns[i][j] = (String ) cols.elementAt(j); 709 refColIndex[i][j] = this.getColIndex(refColumns[i][j], 710 refTables[i]); 711 712 } } 715 } 716 717 private void fetchPrimaryKeys() { 718 719 Vector temp = new Vector (20); 720 721 try { 722 if (cConn == null) { 723 return; 724 } 725 726 if (dbmeta == null) { 727 dbmeta = cConn.getMetaData(); 728 } 729 730 ResultSet colList = dbmeta.getPrimaryKeys(null, null, tableName); 731 732 while (colList.next()) { 733 temp.addElement(colList.getString("COLUMN_NAME")); 734 } 735 736 colList.close(); 737 } catch (SQLException e) { 738 ZaurusEditor.printStatus("SQL Exception: " + e.getMessage()); 739 } 740 741 primaryKeys = new String [temp.size()]; 742 743 temp.copyInto(primaryKeys); 744 745 pkColIndex = new int[primaryKeys.length]; 746 747 for (int i = 0; i < primaryKeys.length; i++) { 748 pkColIndex[i] = this.getColIndex(primaryKeys[i]); 749 } } 751 752 private String generatePKWhere() { 753 754 String stmtString = " WHERE "; 755 756 for (int i = 0; i < primaryKeys.length; i++) { 757 if (i > 0) { 758 stmtString += " AND "; 759 } 760 761 stmtString += primaryKeys[i] + "=?"; 762 } 764 return stmtString; 765 } 766 767 private String generateWhere(String [] words, boolean allWords, 769 boolean ignoreCase, boolean noMatchWhole) { 770 771 String result = ""; 772 773 String join; 775 776 if (allWords) { 777 join = " AND "; 778 } else { 779 join = " OR "; 780 } 782 for (int wordInd = 0; wordInd < words.length; wordInd++) { 783 String oneCondition = ""; 784 785 for (int col = 0; col < columns.length; col++) { 786 if (oneCondition != "") { 787 oneCondition += " OR "; 788 } 789 790 if (ignoreCase) { 791 if (noMatchWhole) { 792 oneCondition += "LOWER(" + columns[col] + ") LIKE '%" 793 + words[wordInd].toLowerCase() + "%'"; 794 } else { 795 oneCondition += "LOWER(" + columns[col] + ") LIKE '" 796 + words[wordInd].toLowerCase() + "'"; 797 } 798 } else { 799 if (noMatchWhole) { 800 oneCondition += columns[col] + " LIKE '%" 801 + words[wordInd] + "%'"; 802 } else { 803 oneCondition += columns[col] + " LIKE '" 804 + words[wordInd] + "'"; 805 } 806 } 807 } 808 809 if (result != "") { 810 result += join; 811 } 812 813 result += "(" + oneCondition + ")"; 814 } 815 816 if (result != "") { 817 result = " WHERE " + result; 818 } 820 return result; 822 } 823 824 private int getColIndex(String name) { 826 827 for (int i = 0; i < columns.length; i++) { 828 if (name.equals(columns[i])) { 829 return i; 830 } } 833 return -1; 834 } 835 836 private int getColIndex(String colName, String tabName) { 838 839 int ordPos = 0; 840 841 try { 842 if (cConn == null) { 843 return -1; 844 } 845 846 if (dbmeta == null) { 847 dbmeta = cConn.getMetaData(); 848 } 849 850 ResultSet colList = dbmeta.getColumns(null, null, tabName, 851 colName); 852 853 colList.next(); 854 855 ordPos = colList.getInt("ORDINAL_POSITION"); 856 857 colList.close(); 858 } catch (SQLException e) { 859 System.out.println("SQL Exception: " + e.getMessage()); 860 } 861 862 return ordPos - 1; 863 } 864 865 private int getConstraintIndex(int colIndex) { 868 869 for (int i = 0; i < imColIndex.length; i++) { 870 for (int j = 0; j < imColIndex[i].length; j++) { 871 if (colIndex == imColIndex[i][j]) { 872 return i; 873 } } } 877 return -1; 878 } 879 880 private void initGUI() { 881 882 Panel pEntry = new Panel (); 883 884 pEntry.setLayout(new GridBagLayout ()); 885 886 GridBagConstraints c = new GridBagConstraints (); 887 888 c.fill = GridBagConstraints.HORIZONTAL; 889 c.insets = new Insets (3, 3, 3, 3); 890 c.gridwidth = 1; 891 c.gridheight = 1; 892 c.weightx = c.weighty = 1; 893 c.anchor = GridBagConstraints.WEST; 894 komponente = new ZaurusComponent[columns.length]; 895 896 for (int i = 0; i < columns.length; i++) { 897 c.gridy = i; 898 c.gridx = 0; 899 900 pEntry.add(new Label ((String ) columns[i]), c); 901 902 c.gridx = 1; 903 904 int constraint = this.getConstraintIndex(i); 905 906 if (constraint >= 0 && imColIndex[constraint].length == 1) { 907 908 ZaurusChoice tmp = new ZaurusChoice(); 910 911 this.fillZChoice(tmp, refTables[constraint], 912 refColumns[constraint][0]); 913 tmp.addItemListener(this); 914 915 komponente[i] = tmp; 916 917 pEntry.add(tmp, c); 918 } else if (columnTypes[i] == java.sql.Types.DATE) { 919 920 ZaurusTextField tmp = new ZaurusTextField(8); 922 923 tmp.addTextListener(this); 924 pEntry.add(tmp, c); 925 926 komponente[i] = tmp; 927 } else { 928 ZaurusTextField tmp = new ZaurusTextField(5); 929 930 tmp.addTextListener(this); 931 pEntry.add(tmp, c); 932 933 komponente[i] = tmp; 934 } 935 936 komponente[i].setEditable(true); 937 } 938 939 this.add(pEntry); 940 } 941 942 private void showAktRow() { 944 945 try { 946 pStmt.clearParameters(); 947 948 for (int i = 0; i < primaryKeys.length; i++) { 949 pStmt.setObject(i + 1, resultRowPKs[aktRowNr][i]); 950 } 952 ResultSet rs = pStmt.executeQuery(); 953 954 rs.next(); 955 956 for (int i = 0; i < columns.length; i++) { 957 komponente[i].setContent(rs.getString(i + 1)); 958 } 960 rs.close(); 961 } catch (SQLException e) { 962 ZaurusEditor.printStatus("SQL Exception: " + e.getMessage()); 963 } 965 for (int i = 0; i < columns.length; i++) { 966 komponente[i].clearChanges(); 967 } 968 } 969 } 970 | Popular Tags |