@@ -2912,62 +2912,6 @@ public java.sql.ResultSet getPrimaryKeys(String catalog, String schema, String t
29122912return connection .createStatement ().executeQuery (sql );
29132913}
29142914
2915- /*
2916- SELECT
2917- c.relname as primary,
2918- c2.relname as foreign,
2919- t.tgconstrname,
2920- ic.relname as fkeyname,
2921- af.attnum as fkeyseq,
2922- ipc.relname as pkeyname,
2923- ap.attnum as pkeyseq,
2924- t.tgdeferrable,
2925- t.tginitdeferred,
2926- t.tgnargs,t.tgargs,
2927- p1.proname as updaterule,
2928- p2.proname as deleterule
2929- FROM
2930- pg_trigger t,
2931- pg_trigger t1,
2932- pg_class c,
2933- pg_class c2,
2934- pg_class ic,
2935- pg_class ipc,
2936- pg_proc p1,
2937- pg_proc p2,
2938- pg_index if,
2939- pg_index ip,
2940- pg_attribute af,
2941- pg_attribute ap
2942- WHERE
2943- (t.tgrelid=c.oid
2944- AND t.tgisconstraint
2945- AND t.tgconstrrelid=c2.oid
2946- AND t.tgfoid=p1.oid
2947- and p1.proname like '%%upd')
2948-
2949- and
2950- (t1.tgrelid=c.oid
2951- and t1.tgisconstraint
2952- and t1.tgconstrrelid=c2.oid
2953- AND t1.tgfoid=p2.oid
2954- and p2.proname like '%%del')
2955-
2956- AND c2.relname='users'
2957-
2958- AND
2959- (if.indrelid=c.oid
2960- AND if.indexrelid=ic.oid
2961- and ic.oid=af.attrelid
2962- AND if.indisprimary)
2963-
2964- and
2965- (ip.indrelid=c2.oid
2966- and ip.indexrelid=ipc.oid
2967- and ipc.oid=ap.attrelid
2968- and ip.indisprimary)
2969-
2970- */
29712915/**
29722916 *
29732917 * @param catalog
@@ -3014,55 +2958,68 @@ protected java.sql.ResultSet getImportedExportedKeys(String primaryCatalog, Stri
30142958 */
30152959
30162960if (connection .haveMinimumServerVersion ("7.3" )) {
3017- select ="SELECT DISTINCT n.nspname as pnspname,n2.nspname as fnspname, " ;
3018- from =" FROM pg_catalog.pg_namespace n, pg_catalog.pg_namespace n2, pg_catalog.pg_trigger t, pg_catalog.pg_trigger t1, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_class ic, pg_catalog.pg_proc p1, pg_catalog.pg_proc p2, pg_catalog.pg_index i, pg_catalog.pg_attribute a " ;
3019- where =" AND c.relnamespace = n.oid AND c2.relnamespace=n2.oid " ;
2961+ select ="SELECT n1.nspname as pnspname,n2.nspname as fnspname, " ;
2962+ from =" FROM pg_catalog.pg_namespace n1 " +
2963+ " JOIN pg_catalog.pg_class c1 ON (c1.relnamespace = n1.oid) " +
2964+ " JOIN pg_catalog.pg_index i ON (c1.oid=i.indrelid) " +
2965+ " JOIN pg_catalog.pg_class ic ON (i.indexrelid=ic.oid) " +
2966+ " JOIN pg_catalog.pg_attribute a ON (ic.oid=a.attrelid), " +
2967+ " pg_catalog.pg_namespace n2 " +
2968+ " JOIN pg_catalog.pg_class c2 ON (c2.relnamespace=n2.oid), " +
2969+ " pg_catalog.pg_trigger t1 " +
2970+ " JOIN pg_catalog.pg_proc p1 ON (t1.tgfoid=p1.oid), " +
2971+ " pg_catalog.pg_trigger t2 " +
2972+ " JOIN pg_catalog.pg_proc p2 ON (t2.tgfoid=p2.oid) " ;
30202973if (primarySchema !=null && !"" .equals (primarySchema )) {
3021- where +=" ANDn .nspname = '" +escapeQuotes (primarySchema )+"' " ;
2974+ where +=" ANDn1 .nspname = '" +escapeQuotes (primarySchema )+"' " ;
30222975}
30232976if (foreignSchema !=null && !"" .equals (foreignSchema )) {
30242977where +=" AND n2.nspname = '" +escapeQuotes (foreignSchema )+"' " ;
30252978}
30262979}else {
3027- select ="SELECT DISTINCT NULL::text as pnspname, NULL::text as fnspname, " ;
3028- from =" FROM pg_trigger t, pg_trigger t1, pg_class c, pg_class c2, pg_class ic, pg_proc p1, pg_proc p2, pg_index i, pg_attribute a " ;
2980+ select ="SELECT NULL::text as pnspname, NULL::text as fnspname, " ;
2981+ from =" FROM pg_class c1 " +
2982+ " JOIN pg_index i ON (c1.oid=i.indrelid) " +
2983+ " JOIN pg_class ic ON (i.indexrelid=ic.oid) " +
2984+ " JOIN pg_attribute a ON (ic.oid=a.attrelid), " +
2985+ " pg_class c2, " +
2986+ " pg_trigger t1 " +
2987+ " JOIN pg_proc p1 ON (t1.tgfoid=p1.oid), " +
2988+ " pg_trigger t2 " +
2989+ " JOIN pg_proc p2 ON (t2.tgfoid=p2.oid) " ;
30292990}
30302991
30312992String sql =select
3032- +"c .relname as prelname, "
2993+ +"c1 .relname as prelname, "
30332994+"c2.relname as frelname, "
3034- +"t .tgconstrname, "
2995+ +"t1 .tgconstrname, "
30352996+"a.attnum as keyseq, "
30362997+"ic.relname as fkeyname, "
3037- +"t .tgdeferrable, "
3038- +"t .tginitdeferred, "
3039- +"t .tgnargs,t .tgargs, "
2998+ +"t1 .tgdeferrable, "
2999+ +"t1 .tginitdeferred, "
3000+ +"t1 .tgnargs,t1 .tgargs, "
30403001+"p1.proname as updaterule, "
30413002+"p2.proname as deleterule "
30423003+from
30433004+"WHERE "
30443005// isolate the update rule
3045- +"(t.tgrelid=c.oid "
3046- +"AND t.tgisconstraint "
3047- +"AND t.tgconstrrelid=c2.oid "
3048- +"AND t.tgfoid=p1.oid "
3049- +"and p1.proname like 'RI\\ \\ _FKey\\ \\ _%\\ \\ _upd') "
3006+ +"(t1.tgrelid=c1.oid "
3007+ +"AND t1.tgisconstraint "
3008+ +"AND t1.tgconstrrelid=c2.oid "
3009+ +"AND p1.proname LIKE 'RI\\ \\ _FKey\\ \\ _%\\ \\ _upd') "
30503010
3051- +"and "
3011+ +"AND "
30523012// isolate the delete rule
3053- +"(t1.tgrelid=c.oid "
3054- +"and t1.tgisconstraint "
3055- +"and t1.tgconstrrelid=c2.oid "
3056- +"AND t1.tgfoid=p2.oid "
3057- +"and p2.proname like 'RI\\ \\ _FKey\\ \\ _%\\ \\ _del') "
3058- +"AND i.indrelid=c.oid "
3059- +"AND i.indexrelid=ic.oid "
3060- +"AND ic.oid=a.attrelid "
3013+ +"(t2.tgrelid=c1.oid "
3014+ +"AND t2.tgisconstraint "
3015+ +"AND t2.tgconstrrelid=c2.oid "
3016+ +"AND p2.proname LIKE 'RI\\ \\ _FKey\\ \\ _%\\ \\ _del') "
3017+
30613018+"AND i.indisprimary "
30623019+where ;
30633020
30643021if (primaryTable !=null ) {
3065- sql +="ANDc .relname='" +escapeQuotes (primaryTable ) +"' " ;
3022+ sql +="ANDc1 .relname='" +escapeQuotes (primaryTable ) +"' " ;
30663023}
30673024if (foreignTable !=null ) {
30683025sql +="AND c2.relname='" +escapeQuotes (foreignTable ) +"' " ;
@@ -3076,8 +3033,14 @@ protected java.sql.ResultSet getImportedExportedKeys(String primaryCatalog, Stri
30763033// since when getting crossreference, primaryTable will be defined
30773034
30783035if (primaryTable !=null ) {
3036+ if (connection .haveMinimumServerVersion ("7.3" )) {
3037+ sql +="fnspname," ;
3038+ }
30793039sql +="frelname" ;
30803040}else {
3041+ if (connection .haveMinimumServerVersion ("7.3" )) {
3042+ sql +="pnspname," ;
3043+ }
30813044sql +="prelname" ;
30823045}
30833046
@@ -3160,6 +3123,7 @@ else if ("setdefault".equals(rule))
31603123// Parse the tgargs data
31613124String fkeyColumn ="" ;
31623125String pkeyColumn ="" ;
3126+ String fkName ="" ;
31633127// Note, I am guessing at most of this, but it should be close
31643128// if not, please correct
31653129// the keys are in pairs and start after the first four arguments
@@ -3172,9 +3136,16 @@ else if ("setdefault".equals(rule))
31723136// we are primarily interested in the column names which are the last items in the string
31733137
31743138StringTokenizer st =new StringTokenizer (targs ,"\\ 000" );
3139+ if (st .hasMoreTokens ()) {
3140+ fkName =st .nextToken ();
3141+ }
3142+
3143+ if (fkName .startsWith ("<unnamed>" )) {
3144+ fkName =targs ;
3145+ }
31753146
31763147int advance =4 + (keySequence -1 ) *2 ;
3177- for (int i =0 ;st .hasMoreTokens () &&i <advance ;i ++ )
3148+ for (int i =1 ;st .hasMoreTokens () &&i <advance ;i ++ )
31783149st .nextToken ();// advance to the key column of interest
31793150
31803151if (st .hasMoreTokens () )
@@ -3190,7 +3161,7 @@ else if ("setdefault".equals(rule))
31903161tuple [7 ] =fkeyColumn .getBytes ();//FKCOLUMN_NAME
31913162
31923163tuple [8 ] =rs .getBytes (6 );//KEY_SEQ
3193- tuple [11 ] =targs .getBytes ();//FK_NAME this will give us a unique name for the foreign key
3164+ tuple [11 ] =fkName .getBytes ();//FK_NAME this will give us a unique name for the foreign key
31943165tuple [12 ] =rs .getBytes (7 );//PK_NAME
31953166
31963167// DEFERRABILITY