提交 a193ddcb authored 作者: Thomas Mueller's avatar Thomas Mueller

When using the new H2 version to access a database over ODBC, the PostgreSQL…

When using the new H2 version to access a database over ODBC, the PostgreSQL catalog is automatically upgraded.
上级 189775e1
...@@ -589,7 +589,6 @@ public class PgServerThread implements Runnable { ...@@ -589,7 +589,6 @@ public class PgServerThread implements Runnable {
private void initDb() throws SQLException { private void initDb() throws SQLException {
Statement stat = null; Statement stat = null;
ResultSet rs = null; ResultSet rs = null;
Reader r = null;
try { try {
synchronized (server) { synchronized (server) {
// better would be: set the database to exclusive mode // better would be: set the database to exclusive mode
...@@ -597,30 +596,21 @@ public class PgServerThread implements Runnable { ...@@ -597,30 +596,21 @@ public class PgServerThread implements Runnable {
boolean tableFound = rs.next(); boolean tableFound = rs.next();
stat = conn.createStatement(); stat = conn.createStatement();
if (!tableFound) { if (!tableFound) {
try { installPgCatalog(stat);
r = new InputStreamReader(new ByteArrayInputStream(Utils }
.getResource("/org/h2/server/pg/pg_catalog.sql"))); rs = stat.executeQuery("SELECT * FROM PG_CATALOG.PG_VERSION");
} catch (IOException e) { if (!rs.next() || rs.getInt(1) < 2) {
throw DbException.convertIOException(e, "Can not read pg_catalog resource"); // installation incomplete, or old version
} installPgCatalog(stat);
ScriptReader reader = new ScriptReader(r); } else {
while (true) { // version 2 or newer: check the read version
String sql = reader.readStatement(); int versionRead = rs.getInt(2);
if (sql == null) { if (versionRead > 2) {
break; throw DbException.throwInternalError("Incompatible PG_VERSION");
}
stat.execute(sql);
} }
reader.close();
} }
} }
rs = stat.executeQuery("SELECT VERSION FROM PG_CATALOG.PG_VERSION");
if (!rs.next() || rs.getInt(1) != 1) {
throw DbException.throwInternalError("Invalid PG_VERSION");
}
stat.execute("set search_path = PUBLIC, pg_catalog"); stat.execute("set search_path = PUBLIC, pg_catalog");
HashSet<Integer> typeSet = server.getTypeSet(); HashSet<Integer> typeSet = server.getTypeSet();
if (typeSet.size() == 0) { if (typeSet.size() == 0) {
rs = stat.executeQuery("SELECT OID FROM PG_CATALOG.PG_TYPE"); rs = stat.executeQuery("SELECT OID FROM PG_CATALOG.PG_TYPE");
...@@ -631,6 +621,26 @@ public class PgServerThread implements Runnable { ...@@ -631,6 +621,26 @@ public class PgServerThread implements Runnable {
} finally { } finally {
JdbcUtils.closeSilently(stat); JdbcUtils.closeSilently(stat);
JdbcUtils.closeSilently(rs); JdbcUtils.closeSilently(rs);
}
}
private void installPgCatalog(Statement stat) throws SQLException {
Reader r = null;
try {
r = new InputStreamReader(new ByteArrayInputStream(Utils
.getResource("/org/h2/server/pg/pg_catalog.sql")));
ScriptReader reader = new ScriptReader(r);
while (true) {
String sql = reader.readStatement();
if (sql == null) {
break;
}
stat.execute(sql);
}
reader.close();
} catch (IOException e) {
throw DbException.convertIOException(e, "Can not read pg_catalog resource");
} finally {
IOUtils.closeSilently(r); IOUtils.closeSilently(r);
} }
} }
......
...@@ -14,7 +14,7 @@ create alias pg_convertType deterministic for "org.h2.server.pg.PgServer.convert ...@@ -14,7 +14,7 @@ create alias pg_convertType deterministic for "org.h2.server.pg.PgServer.convert
drop alias if exists pg_get_oid; drop alias if exists pg_get_oid;
create alias pg_get_oid deterministic for "org.h2.server.pg.PgServer.getOid"; create alias pg_get_oid deterministic for "org.h2.server.pg.PgServer.getOid";
create table pg_catalog.pg_version as select 1 as version; create table pg_catalog.pg_version as select 2 as version, 2 as version_read;
create view pg_catalog.pg_roles -- (oid, rolname, rolcreaterole, rolcreatedb) create view pg_catalog.pg_roles -- (oid, rolname, rolcreaterole, rolcreatedb)
as as
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论