1 4 5 package com.sqlmagic.tinysql; 6 7 33 import java.sql.SQLException ; 34 import java.sql.Connection ; 35 import java.sql.ResultSet ; 36 import java.sql.Types ; 37 import java.util.Vector ; 38 import java.io.File ; 39 40 41 47 public class dbfFileDatabaseMetaData extends tinySQLDatabaseMetaData { 48 private final String emptyString = ""; 49 50 public dbfFileDatabaseMetaData(Connection connection) { 51 super(connection); 52 } 53 54 public String getDatabaseProductName() 55 { 56 return "tinySQL"; 57 } 58 public String getDatabaseProductVersion() 59 { 60 return "2.0"; 61 } 62 String getDataDir() 63 { 64 String url = ((dbfFileConnection)getConnection()).url; 65 if (url.length() <= 13) 66 return null; 67 68 String dataDir = url.substring(13); 69 return dataDir; 70 } 71 72 118 public ResultSet getTypeInfo() throws SQLException { 119 tsResultSet jrs = new tsResultSet(null, null, null, null); 120 121 tsColumn jsc = new tsColumn("TYPE_NAME"); 122 jsc.type = Types.CHAR; 123 jsc.size = 10; 124 jrs.addColumn (jsc); 125 126 jsc = new tsColumn("DATA_TYPE"); 127 jsc.type = Types.INTEGER; 128 jsc.size = 6; 129 jrs.addColumn (jsc); 130 131 jsc = new tsColumn("PRECISION"); 132 jsc.type = Types.INTEGER; 133 jsc.size = 8; 134 jrs.addColumn (jsc); 135 136 jsc = new tsColumn("LITERAL_PREFIX"); 137 jsc.type = Types.CHAR; 138 jsc.size = 1; 139 jrs.addColumn (jsc); 140 141 jsc = new tsColumn("LITERAL_SUFFIX"); 142 jsc.type = Types.CHAR; 143 jsc.size = 1; 144 jrs.addColumn (jsc); 145 146 jsc = new tsColumn("CREATE_PARAMS"); 147 jsc.type = Types.CHAR; 148 jsc.size = 20; 149 jrs.addColumn (jsc); 150 151 jsc = new tsColumn("NULLABLE"); 152 jsc.type = Types.INTEGER; 153 jsc.size = 6; 154 jrs.addColumn (jsc); 155 156 jsc = new tsColumn("CASE_SENSITIVE"); 157 jsc.type = Types.BIT; 158 jsc.size = 1; 159 jrs.addColumn (jsc); 160 161 jsc = new tsColumn("SEARCHABLE"); 162 jsc.type = Types.INTEGER; 163 jsc.size = 6; 164 jrs.addColumn (jsc); 165 166 177 178 179 183 tsRow record = new tsRow(); 184 record.put("TYPE_NAME", dbfFile.typeToLiteral(Types.CHAR)); record.put("DATA_TYPE", new Integer (Types.CHAR).toString()); 186 record.put("PRECISION", new Integer (254).toString()); 187 record.put("LITERAL_PREFIX", "\""); 188 record.put("LITERAL_SUFFIX", "\""); 189 record.put("CREATE_PARAMS", new Integer (0).toString()); 190 record.put("NULLABLE", new Integer (typeNullableUnknown).toString()); 191 record.put("CASE_SENSITIVE", "N"); 192 record.put("SEARCHABLE", new Integer (typePredBasic).toString()); 193 jrs.addRow (record) ; 194 195 record = new tsRow(); 196 record.put("TYPE_NAME", dbfFile.typeToLiteral(Types.FLOAT)); record.put("DATA_TYPE", new Integer (Types.FLOAT).toString()); 198 record.put("PRECISION", new Integer (19).toString()); 199 record.put("LITERAL_PREFIX", emptyString); 200 record.put("LITERAL_SUFFIX", emptyString); 201 record.put("CREATE_PARAMS", new Integer (0).toString()); 202 record.put("NULLABLE", new Integer (typeNullableUnknown).toString()); 203 record.put("CASE_SENSITIVE", "N"); 204 record.put("SEARCHABLE", new Integer (typePredBasic).toString()); 205 jrs.addRow (record) ; 206 207 record = new tsRow(); 208 record.put("TYPE_NAME", dbfFile.typeToLiteral(Types.BIT)); record.put("DATA_TYPE", new Integer (Types.BIT).toString()); 210 record.put("PRECISION", new Integer (1).toString()); 211 record.put("LITERAL_PREFIX", "\""); 212 record.put("LITERAL_SUFFIX", "\""); 213 record.put("CREATE_PARAMS", new Integer (0).toString()); 214 record.put("NULLABLE", new Integer (typeNullableUnknown).toString()); 215 record.put("CASE_SENSITIVE", "N"); 216 record.put("SEARCHABLE", new Integer (typePredBasic).toString()); 217 jrs.addRow (record) ; 218 219 record = new tsRow(); 220 record.put("TYPE_NAME", dbfFile.typeToLiteral(Types.INTEGER)); record.put("DATA_TYPE", new Integer (Types.INTEGER).toString()); 222 record.put("PRECISION", new Integer (19).toString()); 223 record.put("LITERAL_PREFIX", emptyString); 224 record.put("LITERAL_SUFFIX", emptyString); 225 record.put("CREATE_PARAMS", new Integer (0).toString()); 226 record.put("NULLABLE", new Integer (typeNullableUnknown).toString()); 227 record.put("CASE_SENSITIVE", "N"); 228 record.put("SEARCHABLE", new Integer (typePredBasic).toString()); 229 jrs.addRow (record) ; 230 231 record = new tsRow(); 232 record.put("TYPE_NAME", dbfFile.typeToLiteral(Types.DATE)); record.put("DATA_TYPE", new Integer (Types.DATE).toString()); 234 record.put("PRECISION", new Integer (8).toString()); 235 record.put("LITERAL_PREFIX", "\""); 236 record.put("LITERAL_SUFFIX", "\""); 237 record.put("CREATE_PARAMS", new Integer (0).toString()); 238 record.put("NULLABLE", new Integer (typeNullableUnknown).toString()); 239 record.put("CASE_SENSITIVE", "N"); 240 record.put("SEARCHABLE", new Integer (typePredBasic).toString()); 241 jrs.addRow (record) ; 242 243 return new tinySQLResultSet(jrs, (tinySQLStatement)null); 244 } 245 280 public ResultSet getTables(String catalog, String schemaPattern, 281 String tableNamePattern, String types[]) 282 { 283 String dataDir = getDataDir(); 284 String tableName; 285 File tableFile; 286 tsColumn jsc; 287 int i,dotAt; 288 if (dataDir == null) return null; 289 if (types == null) 290 { 291 types = new String [1]; 292 types[0] = "TABLE"; 293 } 294 tsResultSet jrs = new tsResultSet(null, null, null, null); 295 298 try 299 { 300 jsc = new tsColumn("TABLE_CAT"); 301 jsc.type = Types.CHAR; jsc.size = 10; 303 jrs.addColumn (jsc); 304 305 jsc = new tsColumn("TABLE_SCHEM"); 306 jsc.type = Types.CHAR; jsc.size = 10; 308 jrs.addColumn (jsc); 309 310 jsc = new tsColumn("TABLE_NAME"); 311 jsc.type = Types.CHAR; jsc.size = 10; 313 jrs.addColumn (jsc); 314 315 jsc = new tsColumn("TABLE_TYPE"); 316 jsc.type = Types.CHAR; jsc.size = 40; 318 jsc.defaultVal = "TABLE"; 319 jrs.addColumn (jsc); 320 321 jsc = new tsColumn("TABLE_REMARKS"); 322 jsc.type = Types.CHAR; jsc.size = 254; 324 jrs.addColumn (jsc); 325 329 for ( int itype=0; itype < types.length; itype++ ) 330 { 331 String type = types[itype]; 332 if (type == null) continue; 333 String extension = null; 334 if (type.equalsIgnoreCase("TABLE")) 335 extension = dbfFileTable.dbfExtension; if (extension == null) continue; 337 Vector vec = Utils.getAllFiles(dataDir, extension); 338 for ( i = 0; i < vec.size(); i++) 339 { 340 tableFile = (File )vec.elementAt(i); 341 tableName = tableFile.getName().toUpperCase(); 342 dotAt = tableName.indexOf("."); 343 if ( dotAt > -1 ) tableName = tableName.substring(0,dotAt); 344 if (tableNamePattern == null ) tableNamePattern = "%"; 345 if ( tableNamePattern.equals("%") | 346 tableName.indexOf(tableNamePattern) >= 0) 347 { 348 if (tableName.length() > jsc.size) 349 jsc.size = tableName.length(); 350 tsRow record = new tsRow(); 351 record.put("TABLE_NAME", tableName.toUpperCase()); 352 record.put("TABLE_TYPE", "TABLE"); 353 jrs.addRow (record) ; 354 } 355 } 356 } 357 } catch (Exception ex ) { 358 System.out.println("Unable to create MetaData"); 359 } 360 361 return new tinySQLResultSet(jrs, (tinySQLStatement)null); 363 } 364 416 public ResultSet getColumns(String catalog, String schemaPattern, 417 String tableNamePattern, String columnNamePattern) 418 { 419 try { 420 String dataDir = getDataDir(); 421 422 Utils.log("Entering getColumns(tableNamePattern='" + tableNamePattern + "')"); 423 424 if (dataDir == null) return null; 425 426 ResultSet tableRs = getTables(catalog, schemaPattern, tableNamePattern, null); 427 428 tsResultSet jrs = new tsResultSet(null, null, null, null); 429 430 tsColumn jsc = new tsColumn("TABLE_CAT"); 431 jsc.type = Types.CHAR; 432 jsc.size = 9; 433 jrs.addColumn (jsc); 434 435 jsc = new tsColumn("TABLE_SCHEM"); 436 jsc.type = Types.CHAR; 437 jsc.size = 11; 438 jrs.addColumn (jsc); 439 440 jsc = new tsColumn("TABLE_NAME"); 441 jsc.type = Types.CHAR; 442 jsc.size = 10; 443 jrs.addColumn (jsc); 444 445 jsc = new tsColumn("COLUMN_NAME"); 446 jsc.type = Types.CHAR; 447 jsc.size = 11; 448 jrs.addColumn (jsc); 449 450 jsc = new tsColumn("DATA_TYPE"); 451 jsc.type = Types.INTEGER; 452 jsc.size = 6; 453 jrs.addColumn (jsc); 454 455 jsc = new tsColumn("TYPE_NAME"); 456 jsc.type = Types.CHAR; 457 jsc.size = 9; 458 jrs.addColumn (jsc); 459 460 jsc = new tsColumn("COLUMN_SIZE"); 461 jsc.type = Types.INTEGER; 462 jsc.size = 8; 463 jrs.addColumn (jsc); 464 465 jsc = new tsColumn("BUFFER_LENGTH"); 466 jsc.type = Types.INTEGER; 467 jsc.size = 8; 468 jrs.addColumn (jsc); 469 470 jsc = new tsColumn("DECIMAL_DIGITS"); 471 jsc.type = Types.INTEGER; 472 jsc.size = 8; 473 jrs.addColumn (jsc); 474 475 jsc = new tsColumn("NUM_PREC_RADIX"); 476 jsc.type = Types.INTEGER; 477 jsc.size = 8; 478 jrs.addColumn (jsc); 479 480 jsc = new tsColumn("NULLABLE"); 481 jsc.type = Types.INTEGER; 482 jsc.size = 8; 483 jrs.addColumn (jsc); 484 485 jsc = new tsColumn("REMARKS"); 486 jsc.type = Types.CHAR; 487 jsc.size = 128; 488 jrs.addColumn (jsc); 489 490 jsc = new tsColumn("COLUMN_DEF"); 491 jsc.type = Types.CHAR; 492 jsc.size = 128; 493 jrs.addColumn (jsc); 494 495 jsc = new tsColumn("SQL_DATA_TYPE"); 496 jsc.type = Types.INTEGER; 497 jsc.size = 128; 498 jrs.addColumn (jsc); 499 500 502 jsc = new tsColumn("IS_NULLABLE"); 503 jsc.type = Types.CHAR; 504 jsc.size = 3; 505 jrs.addColumn (jsc); 506 507 while (tableRs.next()) { 509 String tableName = tableRs.getString("TABLE_NAME"); 510 511 dbfFileTable tbl; 512 try { 513 tbl = new dbfFileTable(dataDir, tableName); 514 } catch (Exception e) { 515 continue; } 517 518 Utils.log("Accessing column info for table " + tableName); 519 520 java.util.Hashtable column_info = tbl.column_info; 521 for (java.util.Enumeration e = column_info.elements(); e.hasMoreElements() ;) { 522 tsColumn tsc = (tsColumn)e.nextElement(); 523 524 tsRow record = new tsRow(); 526 record.put("TABLE_CAT", ""); 527 record.put("TABLE_SCHEM", ""); 528 record.put("TABLE_NAME", tableName); 529 record.put("COLUMN_NAME", tsc.name); 530 record.put("DATA_TYPE", new Integer (tsc.type).toString()); 531 record.put("TYPE_NAME", dbfFile.typeToLiteral(tsc.type).toString()); 532 record.put("COLUMN_SIZE", new Integer (tsc.size).toString()); 533 record.put("DECIMAL_DIGITS", new Integer (tsc.decimalPlaces).toString()); 534 int nullable = columnNoNulls; 535 if (tsc.notNull == true) nullable = columnNullable; 536 record.put("NULLABLE", new Integer (nullable).toString()); 537 record.put("REMARKS", "noRemarks"); 538 String defaultVal = tsc.defaultVal; 539 if (defaultVal == null) defaultVal = ""; 540 record.put("COLUMN_DEF", defaultVal); 541 String isNullable = "NO"; 542 if (tsc.notNull == true) isNullable = "YES"; 543 record.put("IS_NULLABLE", isNullable); 544 545 jrs.addRow (record) ; 546 } 547 548 tbl.close(); 549 tbl = null; 550 } 551 552 return new tinySQLResultSet(jrs,(tinySQLStatement)null); 553 } 554 catch (Exception e) { 555 return null; 556 } 557 } 558 } 559 | Popular Tags |