* @param primaryTable if provided will get the keys exported by this table
	 * @param foreignTable if provided will get the keys imported by this table
	 * @return ResultSet
	 * @throws SQLException
	 */

	protected java.sql.ResultSet getImportedExportedKeys(String primaryCatalog,
		String primarySchema, String primaryTable, String foreignCatalog,
		String foreignSchema, String foreignTable) throws SQLException
	{
		ResultSetField f[] = new ResultSetField[14];

		f[0] = new ResultSetField("PKTABLE_CAT", TypeOid.VARCHAR,
			getMaxNameLength());
		f[1] = new ResultSetField("PKTABLE_SCHEM", TypeOid.VARCHAR,
			getMaxNameLength());
		f[2] = new ResultSetField("PKTABLE_NAME", TypeOid.VARCHAR,
			getMaxNameLength());
		f[3] = new ResultSetField("PKCOLUMN_NAME", TypeOid.VARCHAR,
			getMaxNameLength());
		f[4] = new ResultSetField("FKTABLE_CAT", TypeOid.VARCHAR,
			getMaxNameLength());
		f[5] = new ResultSetField("FKTABLE_SCHEM", TypeOid.VARCHAR,
			getMaxNameLength());
		f[6] = new ResultSetField("FKTABLE_NAME", TypeOid.VARCHAR,
			getMaxNameLength());
		f[7] = new ResultSetField("FKCOLUMN_NAME", TypeOid.VARCHAR,
			getMaxNameLength());
		f[8] = new ResultSetField("KEY_SEQ", TypeOid.INT2, 2);
		f[9] = new ResultSetField("UPDATE_RULE", TypeOid.INT2, 2);
		f[10] = new ResultSetField("DELETE_RULE", TypeOid.INT2, 2);
		f[11] = new ResultSetField("FK_NAME", TypeOid.VARCHAR,
			getMaxNameLength());
		f[12] = new ResultSetField("PK_NAME", TypeOid.VARCHAR,
			getMaxNameLength());
		f[13] = new ResultSetField("DEFERRABILITY", TypeOid.INT2, 2);

		/*
		 * The addition of the pg_constraint in 7.3 table should have really
		 * helped us out here, but it comes up just a bit short. - The conkey,
		 * confkey columns aren't really useful without contrib/array unless we
		 * want to issues separate queries. - Unique indexes that can support
		 * foreign keys are not necessarily added to pg_constraint. Also
		 * multiple unique indexes covering the same keys can be created which
		 * make it difficult to determine the PK_NAME field.
		 */
		String sql = "SELECT NULL::text AS PKTABLE_CAT, pkn.nspname AS PKTABLE_SCHEM, pkc.relname AS PKTABLE_NAME, pka.attname AS PKCOLUMN_NAME, "
			+ "NULL::text AS FKTABLE_CAT, fkn.nspname AS FKTABLE_SCHEM, fkc.relname AS FKTABLE_NAME, fka.attname AS FKCOLUMN_NAME, "
			+ "pos.n::int2 AS KEY_SEQ, "
			+ "CASE con.confupdtype "
			+ " WHEN 'c' THEN "
			+ DatabaseMetaData.importedKeyCascade
			+ " WHEN 'n' THEN "
			+ DatabaseMetaData.importedKeySetNull
			+ " WHEN 'd' THEN "
			+ DatabaseMetaData.importedKeySetDefault
			+ " WHEN 'r' THEN "
			+ DatabaseMetaData.importedKeyRestrict
			+ " WHEN 'a' THEN "
			+ DatabaseMetaData.importedKeyNoAction
			+ " ELSE NULL END::int2 AS UPDATE_RULE, "
			+ "CASE con.confdeltype "
			+ " WHEN 'c' THEN "
			+ DatabaseMetaData.importedKeyCascade
			+ " WHEN 'n' THEN "
			+ DatabaseMetaData.importedKeySetNull
			+ " WHEN 'd' THEN "
			+ DatabaseMetaData.importedKeySetDefault
			+ " WHEN 'r' THEN "
			+ DatabaseMetaData.importedKeyRestrict
			+ " WHEN 'a' THEN "
			+ DatabaseMetaData.importedKeyNoAction
			+ " ELSE NULL END::int2 AS DELETE_RULE, "
			+ "con.conname AS FK_NAME, pkic.relname AS PK_NAME, "
			+ "CASE "
			+ " WHEN con.condeferrable AND con.condeferred THEN "
			+ DatabaseMetaData.importedKeyInitiallyDeferred
			+ " WHEN con.condeferrable THEN "
			+ DatabaseMetaData.importedKeyInitiallyImmediate
			+ " ELSE "
			+ DatabaseMetaData.importedKeyNotDeferrable
			+ " END::int2 AS DEFERRABILITY "
			+ " FROM "
			+ " pg_catalog.pg_namespace pkn, pg_catalog.pg_class pkc, pg_catalog.pg_attribute pka, "
			+ " pg_catalog.pg_namespace fkn, pg_catalog.pg_class fkc, pg_catalog.pg_attribute fka, "
			+ " pg_catalog.pg_constraint con, "
			+ " pg_catalog.generate_series(1, " + getMaxIndexKeys() + ") pos(n), "
			+ " pg_catalog.pg_depend dep, pg_catalog.pg_class pkic "
			+ " WHERE pkn.oid = pkc.relnamespace AND pkc.oid = pka.attrelid AND pka.attnum = con.confkey[pos.n] AND con.confrelid = pkc.oid "
			+ " AND fkn.oid = fkc.relnamespace AND fkc.oid = fka.attrelid AND fka.attnum = con.conkey[pos.n] AND con.conrelid = fkc.oid "
			+ " AND con.contype = 'f' AND con.oid = dep.objid AND pkic.oid = dep.refobjid AND pkic.relkind = 'i' AND dep.classid = 'pg_constraint'::regclass::oid AND dep.refclassid = 'pg_class'::regclass::oid " +
             " AND " + resolveSchemaCondition("pkn.nspname", primarySchema) +
             " AND " + resolveSchemaCondition("fkn.nspname", foreignSchema);

        if(primaryTable != null && !"".equals(primaryTable))
		{
			sql += " AND pkc.relname = '" + escapeQuotes(primaryTable)
				+ "' ";
		}
		if(foreignTable != null && !"".equals(foreignTable))
		{
			sql += " AND fkc.relname = '" + escapeQuotes(foreignTable)
				+ "' ";
		}

		if(primaryTable != null)
		{
			sql += " ORDER BY fkn.nspname,fkc.relname,pos.n";
		}
		else
		{
			sql += " ORDER BY pkn.nspname,pkc.relname,pos.n";
		}

		return createMetaDataStatement().executeQuery(sql);
	}