| 1 33 34 package com.internetcds.jdbc.tds; 35 36 37 38 import java.sql.*; 39 40 41 66 public class DatabaseMetaData implements java.sql.DatabaseMetaData  67 { 68 public static final String cvsVersion = "$Id: DatabaseMetaData.java,v 1.1 2006/06/23 10:39:04 sinisa Exp $"; 69 70 final boolean verbose = true; 71 72 73 76 final int procedureResultUnknown = 0; 77 80 final int procedureNoResult = 1; 81 84 final int procedureReturnsResult = 2; 85 86 89 final int procedureColumnUnknown = 0; 90 91 94 final int procedureColumnIn = 1; 95 96 99 final int procedureColumnInOut = 2; 100 101 104 final int procedureColumnOut = 4; 105 108 final int procedureColumnReturn = 5; 109 110 113 final int procedureColumnResult = 3; 114 115 118 final int procedureNoNulls = 0; 119 120 123 final int procedureNullable = 1; 124 125 128 final int procedureNullableUnknown = 2; 129 130 131 134 final int columnNoNulls = 0; 135 136 139 final int columnNullable = 1; 140 141 144 final int columnNullableUnknown = 2; 145 146 149 final int bestRowTemporary = 0; 150 151 154 final int bestRowTransaction = 1; 155 156 159 final int bestRowSession = 2; 160 161 164 final int bestRowUnknown = 0; 165 166 169 final int bestRowNotPseudo = 1; 170 171 174 final int bestRowPseudo = 2; 175 176 179 final int versionColumnUnknown = 0; 180 181 184 final int versionColumnNotPseudo = 1; 185 186 189 final int versionColumnPseudo = 2; 190 191 196 final int importedKeyCascade = 0; 197 198 202 final int importedKeyRestrict = 1; 203 204 208 final int importedKeySetNull = 2; 209 210 214 final int importedKeyNoAction = 3; 215 216 220 final int importedKeySetDefault = 4; 221 222 225 final int importedKeyInitiallyDeferred = 5; 226 227 230 final int importedKeyInitiallyImmediate = 6; 231 232 235 final int importedKeyNotDeferrable = 7; 236 237 240 final int typeNoNulls = 0; 241 242 245 final int typeNullable = 1; 246 247 250 final int typeNullableUnknown = 2; 251 252 255 final int typePredNone = 0; 256 257 260 final int typePredChar = 1; 261 262 265 final int typePredBasic = 2; 266 267 270 final int typeSearchable = 3; 271 272 276 final short tableIndexStatistic = 0; 277 278 281 final short tableIndexClustered = 1; 282 283 286 final short tableIndexHashed = 2; 287 288 291 final short tableIndexOther = 3; 292 293 294 Tds tds; 298 299 300 301 302 java.sql.Connection connection; 303 304 305 private void debugPrintln(String s) 306 { 307 if (verbose) 308 { 309 System.out.println(s); 310 } 311 } 312 313 private void debugPrint(String s) 314 { 315 if (verbose) 316 { 317 System.out.print(s); 318 } 319 } 320 321 322 private void NotImplemented() throws SQLException 323 { 324 try 325 { 326 throw new SQLException("Not implemented"); 327 } 328 catch (SQLException e) 329 { 330 e.printStackTrace(); 331 } 332 throw new SQLException("Not implemented"); 333 } 334 335 336 public DatabaseMetaData( 337 Object connection_, 338 Tds tds_) 339 { 340 connection = (java.sql.Connection )connection_; 341 tds = tds_; 342 } 343 344 345 348 355 public boolean allProceduresAreCallable() throws SQLException 356 { 357 359 return true; } 361 362 363 370 public boolean allTablesAreSelectable() throws SQLException 371 { 372 374 return false; 377 } 378 379 380 387 public boolean dataDefinitionCausesTransactionCommit() 388 throws SQLException 389 { 390 NotImplemented(); return false; 391 } 392 393 394 400 public boolean dataDefinitionIgnoredInTransactions() 401 throws SQLException 402 { 403 NotImplemented(); return false; 404 } 405 406 407 408 415 public boolean doesMaxRowSizeIncludeBlobs() throws SQLException 416 { 417 return false; 418 } 419 420 421 422 458 public java.sql.ResultSet getBestRowIdentifier( 459 String catalog, 460 String schema, 461 String table, 462 int scope, 463 boolean nullable) 464 throws SQLException 465 { 466 debugPrintln("Inside getBestRowIdentifier with catalog=|" + catalog 467 + "|, schema=|" + schema + "|, table=|" + table +"|, " 468 + " scope=" + scope + ", nullable=" + nullable); 469 470 NotImplemented(); return null; 471 } 472 473 474 487 public java.sql.ResultSet getCatalogs() 488 throws SQLException 489 { 490 String tmpName = "#t#" + UniqueId.getUniqueId(); 492 final String sql = 493 " create table " + tmpName + " " + 494 " ( " + 495 " q char(30) not null, " + 496 " o char(30) null, " + 497 " n char(30) null, " + 498 " t char(30) null, " + 499 " r varchar(255) null " + 500 " ) " + 501 " " + 502 " insert into " + tmpName + " EXEC sp_tables ' ', ' ', '%', null " + 503 " " + 504 " select q from " + tmpName + " " + 505 ""; 506 java.sql.Statement stmt = connection.createStatement(); 507 java.sql.ResultSet rs; 508 509 510 if (stmt.execute(sql)) 511 { 512 throw new SQLException("Internal error. Confused"); 513 } 514 515 516 if (null != (rs = stmt.getResultSet())) 518 { 519 throw new SQLException("Internal error. Confused"); 520 } 521 522 if (null != (rs = stmt.getResultSet())) 524 { 525 527 531 return rs; 532 } 533 534 if (null == (rs = stmt.getResultSet())) 536 { 537 throw new SQLException("Internal error. Confused"); 538 } 539 return rs; 540 } 541 542 543 544 550 public String getCatalogSeparator() throws SQLException 551 { 552 return "."; 553 } 554 555 556 557 563 public String getCatalogTerm() throws SQLException 564 { 565 566 return "database"; 567 } 568 569 570 571 600 public java.sql.ResultSet getColumnPrivileges(String catalog, String schema, 601 String table, String columnNamePattern) 602 throws SQLException 603 { 604 NotImplemented(); return null; 605 } 606 607 608 658 public java.sql.ResultSet getColumns(String catalog, String schemaPattern, 659 String tableNamePattern, String columnNamePattern) 660 throws SQLException 661 { 662 debugPrintln("Inside of getColumn"); 663 debugPrintln(" catalog is |" + catalog + "|"); 664 debugPrintln(" schemaPattern is " + schemaPattern); 665 debugPrintln(" tableNamePattern is " + tableNamePattern); 666 debugPrintln(" columnNamePattern is " + columnNamePattern); 667 668 return getColumns_SQLServer65(catalog, schemaPattern, 669 tableNamePattern, columnNamePattern); 670 } 671 672 673 private java.sql.ResultSet getColumns_SQLServer65( 674 String catalog, 675 String schemaPattern, 676 String tableNamePattern, 677 String columnNamePattern) 678 throws SQLException 679 { 680 int i; 681 682 String sql = null; 683 java.sql.Statement tmpTableStmt = connection.createStatement(); 684 String catalogCriteria; 685 686 String tmpTableName = "##t#" + UniqueId.getUniqueId(); 690 String lookup = "#l#" + UniqueId.getUniqueId(); 691 692 sql = 694 "create table " + tmpTableName + " ( " + 695 " TABLE_CAT char(32) null, " + 696 " TABLE_SCHEM char(32) null, " + 697 " TABLE_NAME char(32) null, " + 698 " COLUMN_NAME char(32) null, " + 699 " DATA_TYPE integer null, " + 700 " TYPE_NAME char(32) null, " + 701 " COLUMN_SIZE integer null, " + 702 " BUFFER_LENGTH integer null, " + 703 " DECIMAL_DIGITS integer null, " + 704 " NUM_PREC_RADIX integer null, " + 705 " NULLABLE integer null, " + 706 " REMARKS char(255) null, " + 707 " COLUMN_DEF char(255) null, " + 708 " SQL_DATA_TYPE integer null, " + 709 " SQL_DATETIME_SUB integer null, " + 710 " CHAR_OCTET_LENGTH integer null, " + 711 " ORDINAL_POSITION integer null, " + 712 " IS_NULLABLE char(3)) " + 713 ""; 714 tmpTableStmt.execute(sql); 715 716 sql = 718 "create table " + lookup + " ( " + 719 " native_type integer primary key, " + 720 " jdbc_type integer not null) "; 721 tmpTableStmt.execute(sql); 722 723 sql = 724 "insert into " + lookup + " values ( 31, 1111) " + "insert into " + lookup + " values ( 34, 1111) " + "insert into " + lookup + " values ( 35, -1) " + "insert into " + lookup + " values ( 37, -3) " + "insert into " + lookup + " values ( 38, 4) " + "insert into " + lookup + " values ( 39, 12) " + "insert into " + lookup + " values ( 45, -2) " + "insert into " + lookup + " values ( 47, 1) " + "insert into " + lookup + " values ( 48, -6) " + "insert into " + lookup + " values ( 50, -7) " + "insert into " + lookup + " values ( 52, 5) " + "insert into " + lookup + " values ( 56, 4) " + "insert into " + lookup + " values ( 58, 93) " + "insert into " + lookup + " values ( 59, 7) " + "insert into " + lookup + " values ( 60, 1111) " + "insert into " + lookup + " values ( 61, 93) " + "insert into " + lookup + " values ( 62, 8) " + "insert into " + lookup + " values (106, 3) " + "insert into " + lookup + " values (108, 2) " + "insert into " + lookup + " values (109, 8) " + "insert into " + lookup + " values (110, 1111) " + "insert into " + lookup + " values (111, 93) " + "insert into " + lookup + " values (112, 1111) " + ""; 748 tmpTableStmt.execute(sql); 749 750 751 java.sql.ResultSet rs = getTables(null, "%", "%", null); 755 while(rs.next()) 756 { 757 String cat = rs.getString(1); 758 759 sql = 763 "insert into " + tmpTableName + " " + 764 "select " + 765 " TABLE_CAT='" + cat + "', " + 766 " TABLE_SCHEM=USER_NAME(o.uid), " + 767 " TABLE_NAME=o.name, " + 768 " COLUMN_NAME=c.name, " + 769 " DATA_TYPE=l.jdbc_type, " + 770 " TYPE_NAME=t.name, " + 771 " COLUMN_SIZE=c.prec, " + 772 " BUFFER_LENGTH=0, " + 773 " DECIMAL_DIGITS=c.scale, " + 774 " NUM_PREC_RADIX=10, " + 775 " NULLABLE=convert(integer, " + 776 " convert(bit, c.status&8)), " + 777 " REMARKS=null, " + 778 " COLUMN_DEF=null, " + 779 " SQL_DATATYPE=c.type, " + 780 " SQL_DATETIME_SUB=0, " + 781 " CHAR_OCTET_LENGTH=c.length, " + 782 " ORDINAL_POSITION=c.colid, " + 783 " IS_NULLABLE= " + 784 " convert(char(3), rtrim(substring " + 785 " ('NO YES', " + 786 " (c.status&8)+1,3))) " + 787 "from " + 788 " " + cat + ".dbo.sysobjects o, " + 789 " " + cat + ".dbo.syscolumns c, " + 790 " " + lookup + " l, " + 791 " systypes t " + 792 "where o.type in ('V', 'U') and o.id=c.id " + 793 " and t.type=c.type " + 794 " and l.native_type=c.type " + 795 ""; 796 try 798 { 799 tmpTableStmt.executeUpdate(sql); 800 } 801 catch (SQLException e) 802 { 803 804 } 805 } 806 rs.close(); 807 808 809 if (catalog == null) 810 { 811 catalog = ""; 812 catalogCriteria = " (TABLE_CAT like '%' or TABLE_CAT=?) "; 813 } 814 else 815 { 816 catalogCriteria = " TABLE_CAT=? "; 817 } 818 819 sql = 820 "select distinct * from " + tmpTableName + " where " + 821 catalogCriteria + " and " + 822 " TABLE_SCHEM like ? and TABLE_NAME like ? and " + 823 " COLUMN_NAME like ? " + 824 "order by TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION " ; 825 826 System.out.println("The query is \n" + sql); 827 828 java.sql.PreparedStatement ps = connection.prepareStatement(sql); 829 830 ps.setString(1, catalog); 831 ps.setString(2, schemaPattern); 832 ps.setString(3, tableNamePattern); 833 ps.setString(4, columnNamePattern); 834 rs = ps.executeQuery(); 835 836 tmpTableStmt.close(); 838 839 return rs; 840 } 841 842 843 919 public java.sql.ResultSet getCrossReference( 920 String primaryCatalog, String primarySchema, String primaryTable, 921 String foreignCatalog, String foreignSchema, String foreignTable 922 ) throws SQLException 923 { 924 NotImplemented(); return null; 925 } 926 927 928 934 public String getDatabaseProductName() throws SQLException 935 { 936 return tds.getDatabaseProductName(); 937 } 938 939 940 946 public String getDatabaseProductVersion() throws SQLException 947 { 948 return tds.getDatabaseProductVersion(); 949 } 950 951 952 954 962 public int getDefaultTransactionIsolation() throws SQLException 963 { 964 return Connection.TRANSACTION_READ_COMMITTED; 966 } 967 968 969 974 public int getDriverMajorVersion() 975 { 976 return DriverVersion.getDriverMajorVersion(); 977 } 978 979 980 985 public int getDriverMinorVersion() 986 { 987 return DriverVersion.getDriverMinorVersion(); 988 } 989 990 991 997 public String getDriverName() throws SQLException 998 { 999 return "InternetCDS Type 4 JDBC driver for MS SQLServer"; 1000 } 1001 1002 1003 1009 public
|