|
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 | | - private void importLoop(Vector tuples, java.sql.ResultSet keyRelation) throws SQLException |
2276 | | - { |
2277 | | - String s, s2; |
2278 | | - String origTable = null, primTable = new String(""), schema; |
2279 | | - int i; |
2280 | | - Vector v = new Vector(); |
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 | | - case 0: |
2298 | | - origTable = s2; |
2299 | | - break; |
2300 | | - case 1: |
2301 | | - primTable = s2; |
2302 | | - break; |
2303 | | - case 2: |
2304 | | - schema = s2; |
2305 | | - break; |
2306 | | - default: |
2307 | | - v.addElement(s2); |
2308 | | - } |
2309 | | - } |
2310 | | - |
2311 | | - java.sql.ResultSet rstmp = connection.ExecSQL("select * from " + origTable + " where 1=0"); |
2312 | | - java.sql.ResultSetMetaData origCols = rstmp.getMetaData(); |
2313 | | - |
2314 | | - String stmp; |
2315 | | - // Vector tuples=new Vector(); |
2316 | | - byte tuple[][]; |
2317 | | - |
2318 | | - // the foreign keys are only on even positions in the Vector. |
2319 | | - for (i = 0;i < v.size();i += 2) |
| 2275 | + private void parseConstraint(java.sql.ResultSet keyRelation, Vector tuples) throws SQLException |
| 2276 | + { |
| 2277 | + byte tuple[][]=new byte[14][0]; |
| 2278 | + for (int k = 0;k < 14;k++) |
| 2279 | + tuple[k] = null; |
| 2280 | + String s=keyRelation.getString(1); |
| 2281 | + int pos=s.indexOf("\\000"); |
| 2282 | + if(pos>-1) |
| 2283 | + { |
| 2284 | + tuple[11]=s.substring(0,pos).getBytes();; // FK_NAME |
| 2285 | + int pos2=s.indexOf("\\000", pos+1); |
| 2286 | + if(pos2>-1) |
2320 | 2287 | { |
2321 | | - stmp = (String)v.elementAt(i); |
2322 | | - |
2323 | | - for (int j = 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 = new byte[14][0]; |
2328 | | - |
2329 | | - for (int k = 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 | + String rule=keyRelation.getString(2); |
| 2313 | + int action=importedKeyNoAction; |
| 2314 | + if("cascade".equals(rule)) action=importedKeyCascade; |
| 2315 | + else if("setnull".equals(rule)) action=importedKeySetNull; |
| 2316 | + else if("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 | + else if("setnull".equals(rule)) action=importedKeySetNull; |
| 2324 | + else if("setdefault".equals(rule)) action=importedKeySetDefault; |
| 2325 | + tuple[10]=Integer.toString(action).getBytes(); |
| 2326 | + |
| 2327 | + // DEFERRABILITY |
| 2328 | + int deferrability=importedKeyNotDeferrable; |
| 2329 | + boolean deferrable=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 (int i=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 | | - public java.sql.ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException |
2409 | | - { |
2410 | | - // Added by Ola Sundell <ola@miranda.org> |
2411 | | - // FIXME: error checking galore! |
2412 | | - java.sql.ResultSet rsret; |
2413 | | - Field f[] = new Field[14]; |
2414 | | - byte tuple[][]; |
2415 | | - |
2416 | | - f[0] = new Field(connection, "PKTABLE_CAT", iVarcharOid, 32); |
2417 | | - f[1] = new Field(connection, "PKTABLE_SCHEM", iVarcharOid, 32); |
2418 | | - f[2] = new Field(connection, "PKTABLE_NAME", iVarcharOid, 32); |
2419 | | - f[3] = new Field(connection, "PKCOLUMN_NAME", iVarcharOid, 32); |
2420 | | - f[4] = new Field(connection, "FKTABLE_CAT", iVarcharOid, 32); |
2421 | | - f[5] = new Field(connection, "FKTABLE_SCHEM", iVarcharOid, 32); |
2422 | | - f[6] = new Field(connection, "FKTABLE_NAME", iVarcharOid, 32); |
2423 | | - f[7] = new Field(connection, "FKCOLUMN_NAME", iVarcharOid, 32); |
2424 | | - f[8] = new Field(connection, "KEY_SEQ", iInt2Oid, 2); |
2425 | | - f[9] = new Field(connection, "UPDATE_RULE", iInt2Oid, 2); |
2426 | | - f[10] = new Field(connection, "DELETE_RULE", iInt2Oid, 2); |
2427 | | - f[11] = new Field(connection, "FK_NAME", iVarcharOid, 32); |
2428 | | - f[12] = new Field(connection, "PK_NAME", iVarcharOid, 32); |
2429 | | - f[13] = new Field(connection, "DEFERRABILITY", iInt2Oid, 2); |
2430 | | - |
2431 | | - java.sql.ResultSet rs = 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 | | - Vector tuples = new Vector(); |
2435 | | - |
2436 | | - while (rs.next()) |
2437 | | - { |
2438 | | - importLoop(tuples, rs); |
2439 | | - } |
2440 | | - |
2441 | | - rsret = new ResultSet(connection, f, tuples, "OK", 1); |
2442 | | - |
2443 | | - return rsret; |
2444 | | - } |
| 2394 | + public java.sql.ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException |
| 2395 | + { |
| 2396 | + Field f[]=new Field[14]; |
| 2397 | + |
| 2398 | + f[0]=new Field(connection, "PKTABLE_CAT", iVarcharOid, 32); |
| 2399 | + f[1]=new Field(connection, "PKTABLE_SCHEM", iVarcharOid, 32); |
| 2400 | + f[2]=new Field(connection, "PKTABLE_NAME", iVarcharOid, 32); |
| 2401 | + f[3]=new Field(connection, "PKCOLUMN_NAME", iVarcharOid, 32); |
| 2402 | + f[4]=new Field(connection, "FKTABLE_CAT", iVarcharOid, 32); |
| 2403 | + f[5]=new Field(connection, "FKTABLE_SCHEM", iVarcharOid, 32); |
| 2404 | + f[6]=new Field(connection, "FKTABLE_NAME", iVarcharOid, 32); |
| 2405 | + f[7]=new Field(connection, "FKCOLUMN_NAME", iVarcharOid, 32); |
| 2406 | + f[8]=new Field(connection, "KEY_SEQ", iInt2Oid, 2); |
| 2407 | + f[9]=new Field(connection, "UPDATE_RULE", iInt2Oid, 2); |
| 2408 | + f[10]=new Field(connection, "DELETE_RULE", iInt2Oid, 2); |
| 2409 | + f[11]=new Field(connection, "FK_NAME", iVarcharOid, 32); |
| 2410 | + f[12]=new Field(connection, "PK_NAME", iVarcharOid, 32); |
| 2411 | + f[13]=new Field(connection, "DEFERRABILITY", iInt2Oid, 2); |
| 2412 | + |
| 2413 | + java.sql.ResultSet rs = 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 | + Vector tuples = new Vector(); |
| 2433 | + |
| 2434 | + while (rs.next()) |
| 2435 | + { |
| 2436 | + |
| 2437 | + parseConstraint(rs,tuples); |
| 2438 | + } |
| 2439 | + |
| 2440 | + return new ResultSet(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 | public java.sql.ResultSet getExportedKeys(String catalog, String schema, String table) throws SQLException |
2500 | 2497 | { |
2501 | | - throw org.postgresql.Driver.notImplemented(); |
| 2498 | + Field f[] = new Field[14]; |
| 2499 | + |
| 2500 | + f[0] = new Field(connection, "PKTABLE_CAT", iVarcharOid, 32); |
| 2501 | + f[1] = new Field(connection, "PKTABLE_SCHEM", iVarcharOid, 32); |
| 2502 | + f[2] = new Field(connection, "PKTABLE_NAME", iVarcharOid, 32); |
| 2503 | + f[3] = new Field(connection, "PKCOLUMN_NAME", iVarcharOid, 32); |
| 2504 | + f[4] = new Field(connection, "FKTABLE_CAT", iVarcharOid, 32); |
| 2505 | + f[5] = new Field(connection, "FKTABLE_SCHEM", iVarcharOid, 32); |
| 2506 | + f[6] = new Field(connection, "FKTABLE_NAME", iVarcharOid, 32); |
| 2507 | + f[7] = new Field(connection, "FKCOLUMN_NAME", iVarcharOid, 32); |
| 2508 | + f[8] = new Field(connection, "KEY_SEQ", iInt2Oid, 2); |
| 2509 | + f[9] = new Field(connection, "UPDATE_RULE", iInt2Oid, 2); |
| 2510 | + f[10] = new Field(connection, "DELETE_RULE", iInt2Oid, 2); |
| 2511 | + f[11] = new Field(connection, "FK_NAME", iVarcharOid, 32); |
| 2512 | + f[12] = new Field(connection, "PK_NAME", iVarcharOid, 32); |
| 2513 | + f[13] = new Field(connection, "DEFERRABILITY", iInt2Oid, 2); |
| 2514 | + |
| 2515 | + java.sql.ResultSet rs = 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 | + Vector tuples = new Vector(); |
| 2532 | + |
| 2533 | + while (rs.next()) |
| 2534 | + { |
| 2535 | + parseConstraint(rs,tuples); |
| 2536 | + } |
| 2537 | + |
| 2538 | + return new ResultSet(connection, f, tuples, "OK", 1); |
2502 | 2539 | } |
2503 | 2540 |
|
2504 | 2541 | /** |
|
0 commit comments