|
13 | 13 | /** |
14 | 14 | * This class provides information about the database as a whole. |
15 | 15 | * |
16 | | - * $Id: DatabaseMetaData.java,v 1.36 2001/10/30 05:05:25 barry Exp $ |
| 16 | + * $Id: DatabaseMetaData.java,v 1.37 2001/11/02 23:50:08 davec Exp $ |
17 | 17 | * |
18 | 18 | * <p>Many of the methods here return lists of information in ResultSets. You |
19 | 19 | * can use the normal ResultSet methods such as getString and getInt to |
@@ -2272,86 +2272,72 @@ public java.sql.ResultSet getPrimaryKeys(String catalog, String schema, String t |
2272 | 2272 | ); |
2273 | 2273 | } |
2274 | 2274 |
|
2275 | | -privatevoidimportLoop(Vectortuples,java.sql.ResultSetkeyRelation)throwsSQLException |
2276 | | -{ |
2277 | | -Strings,s2; |
2278 | | -StringorigTable =null,primTable =newString(""),schema; |
2279 | | -inti; |
2280 | | -Vectorv =newVector(); |
2281 | | - |
2282 | | -s =keyRelation.getString(1); |
2283 | | -s2 =s; |
2284 | | -//System.out.println(s); |
2285 | | - |
2286 | | -for (i =0;;i++) |
2287 | | -{ |
2288 | | -s =s.substring(s.indexOf("\\000") +4); |
2289 | | -if (s.compareTo("") ==0) |
2290 | | -{ |
2291 | | -//System.out.println(); |
2292 | | -break; |
2293 | | -} |
2294 | | -s2 =s.substring(0,s.indexOf("\\000")); |
2295 | | -switch (i) |
2296 | | -{ |
2297 | | -case0: |
2298 | | -origTable =s2; |
2299 | | -break; |
2300 | | -case1: |
2301 | | -primTable =s2; |
2302 | | -break; |
2303 | | -case2: |
2304 | | -schema =s2; |
2305 | | -break; |
2306 | | -default: |
2307 | | -v.addElement(s2); |
2308 | | -} |
2309 | | -} |
2310 | | - |
2311 | | -java.sql.ResultSetrstmp =connection.ExecSQL("select * from " +origTable +" where 1=0"); |
2312 | | -java.sql.ResultSetMetaDataorigCols =rstmp.getMetaData(); |
2313 | | - |
2314 | | -Stringstmp; |
2315 | | -// Vector tuples=new Vector(); |
2316 | | -bytetuple[][]; |
2317 | | - |
2318 | | -// the foreign keys are only on even positions in the Vector. |
2319 | | -for (i =0;i <v.size();i +=2) |
| 2275 | +privatevoidparseConstraint(java.sql.ResultSetkeyRelation,Vectortuples)throwsSQLException |
| 2276 | + { |
| 2277 | +bytetuple[][]=newbyte[14][0]; |
| 2278 | +for (intk =0;k <14;k++) |
| 2279 | +tuple[k] =null; |
| 2280 | +Strings=keyRelation.getString(1); |
| 2281 | +intpos=s.indexOf("\\000"); |
| 2282 | +if(pos>-1) |
| 2283 | + { |
| 2284 | +tuple[11]=s.substring(0,pos).getBytes();;// FK_NAME |
| 2285 | +intpos2=s.indexOf("\\000",pos+1); |
| 2286 | +if(pos2>-1) |
2320 | 2287 | { |
2321 | | -stmp = (String)v.elementAt(i); |
2322 | | - |
2323 | | -for (intj =1;j <=origCols.getColumnCount();j++) |
| 2288 | +tuple[2]=s.substring(pos+4,pos2).getBytes();;// PKTABLE_NAME |
| 2289 | +pos=s.indexOf("\\000",pos2+1); |
| 2290 | +if(pos>-1) |
| 2291 | + { |
| 2292 | +tuple[6]=s.substring(pos2+4,pos).getBytes();;// FKTABLE_NAME |
| 2293 | +pos=s.indexOf("\\000",pos+1);// Ignore MATCH type |
| 2294 | +if(pos>-1) |
2324 | 2295 | { |
2325 | | -if (stmp.compareTo(origCols.getColumnName(j)) ==0) |
| 2296 | +pos2=s.indexOf("\\000",pos+1); |
| 2297 | +if(pos2>-1) |
| 2298 | + { |
| 2299 | +tuple[3]=s.substring(pos+4,pos2).getBytes();;// PKCOLUMN_NAME |
| 2300 | +pos=s.indexOf("\\000",pos2+1); |
| 2301 | +if(pos>-1) |
2326 | 2302 | { |
2327 | | -tuple =newbyte[14][0]; |
2328 | | - |
2329 | | -for (intk =0;k <14;k++) |
2330 | | -tuple[k] =null; |
2331 | | - |
2332 | | -//PKTABLE_NAME |
2333 | | -tuple[2] =primTable.getBytes(); |
2334 | | -//PKTABLE_COLUMN |
2335 | | -stmp = (String)v.elementAt(i +1); |
2336 | | -tuple[3] =stmp.getBytes(); |
2337 | | -//FKTABLE_NAME |
2338 | | -tuple[6] =origTable.getBytes(); |
2339 | | -//FKCOLUMN_NAME |
2340 | | -tuple[7] =origCols.getColumnName(j).getBytes(); |
2341 | | -//KEY_SEQ |
2342 | | -tuple[8] =Integer.toString(j).getBytes(); |
2343 | | - |
2344 | | -tuples.addElement(tuple); |
2345 | | - |
2346 | | -//System.out.println(origCols.getColumnName(j)+ |
2347 | | -//": "+j+" -> "+primTable+": "+ |
2348 | | -//(String)v.elementAt(i+1)); |
2349 | | -break; |
| 2303 | +tuple[7]=s.substring(pos2+4,pos).getBytes();//FKCOLUMN_NAME |
2350 | 2304 | } |
| 2305 | + } |
2351 | 2306 | } |
| 2307 | + } |
2352 | 2308 | } |
2353 | | - |
2354 | | -//return tuples; |
| 2309 | + } |
| 2310 | + |
| 2311 | +// UPDATE_RULE |
| 2312 | +Stringrule=keyRelation.getString(2); |
| 2313 | +intaction=importedKeyNoAction; |
| 2314 | +if("cascade".equals(rule))action=importedKeyCascade; |
| 2315 | +elseif("setnull".equals(rule))action=importedKeySetNull; |
| 2316 | +elseif("setdefault".equals(rule))action=importedKeySetDefault; |
| 2317 | +tuple[9]=Integer.toString(action).getBytes(); |
| 2318 | + |
| 2319 | +// DELETE_RULE |
| 2320 | +rule=keyRelation.getString(3); |
| 2321 | +action=importedKeyNoAction; |
| 2322 | +if("cascade".equals(rule))action=importedKeyCascade; |
| 2323 | +elseif("setnull".equals(rule))action=importedKeySetNull; |
| 2324 | +elseif("setdefault".equals(rule))action=importedKeySetDefault; |
| 2325 | +tuple[10]=Integer.toString(action).getBytes(); |
| 2326 | + |
| 2327 | +// DEFERRABILITY |
| 2328 | +intdeferrability=importedKeyNotDeferrable; |
| 2329 | +booleandeferrable=keyRelation.getBoolean(4); |
| 2330 | +if(deferrable) |
| 2331 | + { |
| 2332 | +if(keyRelation.getBoolean(5)) |
| 2333 | +deferrability=importedKeyInitiallyDeferred; |
| 2334 | +else |
| 2335 | +deferrability=importedKeyInitiallyImmediate; |
| 2336 | + } |
| 2337 | +tuple[13]=Integer.toString(deferrability).getBytes(); |
| 2338 | +for (inti=0;i<14;i++){ |
| 2339 | +tuples.addElement(tuple[i]); |
| 2340 | + } |
2355 | 2341 | } |
2356 | 2342 |
|
2357 | 2343 | /** |
@@ -2405,43 +2391,54 @@ private void importLoop(Vector tuples, java.sql.ResultSet keyRelation) throws SQ |
2405 | 2391 | * @return ResultSet each row is a primary key column description |
2406 | 2392 | * @see #getExportedKeys |
2407 | 2393 | */ |
2408 | | -publicjava.sql.ResultSetgetImportedKeys(Stringcatalog,Stringschema,Stringtable)throwsSQLException |
2409 | | -{ |
2410 | | -// Added by Ola Sundell <ola@miranda.org> |
2411 | | -// FIXME: error checking galore! |
2412 | | -java.sql.ResultSetrsret; |
2413 | | -Fieldf[] =newField[14]; |
2414 | | -bytetuple[][]; |
2415 | | - |
2416 | | -f[0] =newField(connection,"PKTABLE_CAT",iVarcharOid,32); |
2417 | | -f[1] =newField(connection,"PKTABLE_SCHEM",iVarcharOid,32); |
2418 | | -f[2] =newField(connection,"PKTABLE_NAME",iVarcharOid,32); |
2419 | | -f[3] =newField(connection,"PKCOLUMN_NAME",iVarcharOid,32); |
2420 | | -f[4] =newField(connection,"FKTABLE_CAT",iVarcharOid,32); |
2421 | | -f[5] =newField(connection,"FKTABLE_SCHEM",iVarcharOid,32); |
2422 | | -f[6] =newField(connection,"FKTABLE_NAME",iVarcharOid,32); |
2423 | | -f[7] =newField(connection,"FKCOLUMN_NAME",iVarcharOid,32); |
2424 | | -f[8] =newField(connection,"KEY_SEQ",iInt2Oid,2); |
2425 | | -f[9] =newField(connection,"UPDATE_RULE",iInt2Oid,2); |
2426 | | -f[10] =newField(connection,"DELETE_RULE",iInt2Oid,2); |
2427 | | -f[11] =newField(connection,"FK_NAME",iVarcharOid,32); |
2428 | | -f[12] =newField(connection,"PK_NAME",iVarcharOid,32); |
2429 | | -f[13] =newField(connection,"DEFERRABILITY",iInt2Oid,2); |
2430 | | - |
2431 | | -java.sql.ResultSetrs =connection.ExecSQL("select t.tgargs " + |
2432 | | -"from pg_class as c, pg_trigger as t " + |
2433 | | -"where c.relname like '" +table +"' and c.relfilenode=t.tgrelid"); |
2434 | | -Vectortuples =newVector(); |
2435 | | - |
2436 | | -while (rs.next()) |
2437 | | -{ |
2438 | | -importLoop(tuples,rs); |
2439 | | -} |
2440 | | - |
2441 | | -rsret =newResultSet(connection,f,tuples,"OK",1); |
2442 | | - |
2443 | | -returnrsret; |
2444 | | -} |
| 2394 | +publicjava.sql.ResultSetgetImportedKeys(Stringcatalog,Stringschema,Stringtable)throwsSQLException |
| 2395 | + { |
| 2396 | +Fieldf[]=newField[14]; |
| 2397 | + |
| 2398 | +f[0]=newField(connection,"PKTABLE_CAT",iVarcharOid,32); |
| 2399 | +f[1]=newField(connection,"PKTABLE_SCHEM",iVarcharOid,32); |
| 2400 | +f[2]=newField(connection,"PKTABLE_NAME",iVarcharOid,32); |
| 2401 | +f[3]=newField(connection,"PKCOLUMN_NAME",iVarcharOid,32); |
| 2402 | +f[4]=newField(connection,"FKTABLE_CAT",iVarcharOid,32); |
| 2403 | +f[5]=newField(connection,"FKTABLE_SCHEM",iVarcharOid,32); |
| 2404 | +f[6]=newField(connection,"FKTABLE_NAME",iVarcharOid,32); |
| 2405 | +f[7]=newField(connection,"FKCOLUMN_NAME",iVarcharOid,32); |
| 2406 | +f[8]=newField(connection,"KEY_SEQ",iInt2Oid,2); |
| 2407 | +f[9]=newField(connection,"UPDATE_RULE",iInt2Oid,2); |
| 2408 | +f[10]=newField(connection,"DELETE_RULE",iInt2Oid,2); |
| 2409 | +f[11]=newField(connection,"FK_NAME",iVarcharOid,32); |
| 2410 | +f[12]=newField(connection,"PK_NAME",iVarcharOid,32); |
| 2411 | +f[13]=newField(connection,"DEFERRABILITY",iInt2Oid,2); |
| 2412 | + |
| 2413 | +java.sql.ResultSetrs =connection.ExecSQL("SELECT a.tgargs," |
| 2414 | + +"substring(a.proname from 9 for (char_length(a.proname)-12))," |
| 2415 | + +"substring(b.proname from 9 for (char_length(b.proname)-12))," |
| 2416 | + +"a.tgdeferrable," |
| 2417 | + +"a.tginitdeferred " |
| 2418 | + +"FROM " |
| 2419 | + +"(SELECT t.tgargs, t.tgconstrname, p.proname, t.tgdeferrable," |
| 2420 | + +"t.tginitdeferred " |
| 2421 | + +"FROM pg_class as c, pg_proc as p, pg_trigger as t " |
| 2422 | + +"WHERE c.relfilenode=t.tgrelid AND t.tgfoid = p.oid " |
| 2423 | + +"AND p.proname LIKE 'RI_FKey_%_upd') as a," |
| 2424 | + +"(SELECT t.tgconstrname, p.proname " |
| 2425 | + +"FROM pg_class as c, pg_proc as p, pg_trigger as t " |
| 2426 | + +"WHERE c.relfilenode=t.tgrelid AND t.tgfoid = p.oid " |
| 2427 | + +"AND p.proname LIKE 'RI_FKey_%_del') as b," |
| 2428 | + +"(SELECT t.tgconstrname FROM pg_class as c, pg_trigger as t " |
| 2429 | + +"WHERE c.relname like '"+table+"' AND c.relfilenode=t.tgrelid) as c " |
| 2430 | + +"WHERE a.tgconstrname=b.tgconstrname AND a.tgconstrname=c.tgconstrname" |
| 2431 | + ); |
| 2432 | +Vectortuples =newVector(); |
| 2433 | + |
| 2434 | +while (rs.next()) |
| 2435 | + { |
| 2436 | + |
| 2437 | +parseConstraint(rs,tuples); |
| 2438 | + } |
| 2439 | + |
| 2440 | +returnnewResultSet(connection,f,tuples,"OK",1); |
| 2441 | + } |
2445 | 2442 |
|
2446 | 2443 | /** |
2447 | 2444 | * Get a description of a foreign key columns that reference a |
@@ -2498,7 +2495,47 @@ public java.sql.ResultSet getImportedKeys(String catalog, String schema, String |
2498 | 2495 | */ |
2499 | 2496 | publicjava.sql.ResultSetgetExportedKeys(Stringcatalog,Stringschema,Stringtable)throwsSQLException |
2500 | 2497 | { |
2501 | | -throworg.postgresql.Driver.notImplemented(); |
| 2498 | +Fieldf[] =newField[14]; |
| 2499 | + |
| 2500 | +f[0] =newField(connection,"PKTABLE_CAT",iVarcharOid,32); |
| 2501 | +f[1] =newField(connection,"PKTABLE_SCHEM",iVarcharOid,32); |
| 2502 | +f[2] =newField(connection,"PKTABLE_NAME",iVarcharOid,32); |
| 2503 | +f[3] =newField(connection,"PKCOLUMN_NAME",iVarcharOid,32); |
| 2504 | +f[4] =newField(connection,"FKTABLE_CAT",iVarcharOid,32); |
| 2505 | +f[5] =newField(connection,"FKTABLE_SCHEM",iVarcharOid,32); |
| 2506 | +f[6] =newField(connection,"FKTABLE_NAME",iVarcharOid,32); |
| 2507 | +f[7] =newField(connection,"FKCOLUMN_NAME",iVarcharOid,32); |
| 2508 | +f[8] =newField(connection,"KEY_SEQ",iInt2Oid,2); |
| 2509 | +f[9] =newField(connection,"UPDATE_RULE",iInt2Oid,2); |
| 2510 | +f[10] =newField(connection,"DELETE_RULE",iInt2Oid,2); |
| 2511 | +f[11] =newField(connection,"FK_NAME",iVarcharOid,32); |
| 2512 | +f[12] =newField(connection,"PK_NAME",iVarcharOid,32); |
| 2513 | +f[13] =newField(connection,"DEFERRABILITY",iInt2Oid,2); |
| 2514 | + |
| 2515 | +java.sql.ResultSetrs =connection.ExecSQL("SELECT a.tgargs," |
| 2516 | + +"substring(a.proname from 9 for (char_length(a.proname)-12))," |
| 2517 | + +"substring(b.proname from 9 for (char_length(b.proname)-12))," |
| 2518 | + +"a.tgdeferrable," |
| 2519 | + +"a.tginitdeferred " |
| 2520 | + +"FROM " |
| 2521 | + +"(SELECT t.tgargs, t.tgconstrname, p.proname," |
| 2522 | + +"t.tgdeferrable, t.tginitdeferred " |
| 2523 | + +"FROM pg_class as c, pg_proc as p, pg_trigger as t " |
| 2524 | + +"WHERE c.relname like '"+table+"' AND c.relfilenode=t.tgrelid " |
| 2525 | + +"AND t.tgfoid = p.oid AND p.proname LIKE 'RI_FKey_%_upd') as a, " |
| 2526 | + +"(SELECT t.tgconstrname, p.proname " |
| 2527 | + +"FROM pg_class as c, pg_proc as p, pg_trigger as t " |
| 2528 | + +"WHERE c.relname like '"+table+"' AND c.relfilenode=t.tgrelid " |
| 2529 | + +"AND t.tgfoid = p.oid AND p.proname LIKE 'RI_FKey_%_del') as b " |
| 2530 | + +"WHERE a.tgconstrname=b.tgconstrname"); |
| 2531 | +Vectortuples =newVector(); |
| 2532 | + |
| 2533 | +while (rs.next()) |
| 2534 | +{ |
| 2535 | +parseConstraint(rs,tuples); |
| 2536 | +} |
| 2537 | + |
| 2538 | +returnnewResultSet(connection,f,tuples,"OK",1); |
2502 | 2539 | } |
2503 | 2540 |
|
2504 | 2541 | /** |
|