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

PG Server: improved compatibility by using the type ids of the PostgreSQL driver.

上级 16380c5b
...@@ -15,6 +15,7 @@ import java.sql.PreparedStatement; ...@@ -15,6 +15,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.sql.Types;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
...@@ -32,6 +33,9 @@ import org.h2.util.Tool; ...@@ -32,6 +33,9 @@ import org.h2.util.Tool;
* http://developer.postgresql.org/pgdocs/postgres/protocol.html * http://developer.postgresql.org/pgdocs/postgres/protocol.html
* The PostgreSQL catalog is described here: * The PostgreSQL catalog is described here:
* http://www.postgresql.org/docs/7.4/static/catalogs.html * http://www.postgresql.org/docs/7.4/static/catalogs.html
*
* @author Thomas Mueller
* @author Sergi Vladykin 2009-07-03 (convertType)
*/ */
public class PgServer implements Service { public class PgServer implements Service {
...@@ -41,6 +45,24 @@ public class PgServer implements Service { ...@@ -41,6 +45,24 @@ public class PgServer implements Service {
*/ */
public static final int DEFAULT_PORT = 5435; public static final int DEFAULT_PORT = 5435;
private static final int PG_TYPE_BOOL = 16;
private static final int PG_TYPE_BYTEA = 17;
private static final int PG_TYPE_CHAR = 18;
private static final int PG_TYPE_INT8 = 20;
private static final int PG_TYPE_INT2 = 21;
private static final int PG_TYPE_INT4 = 23;
private static final int PG_TYPE_TEXT = 25;
private static final int PG_TYPE_OID = 26;
private static final int PG_TYPE_FLOAT4 = 700;
private static final int PG_TYPE_FLOAT8 = 701;
private static final int PG_TYPE_UNKNOWN = 705;
private static final int PG_TYPE_TEXTARRAY = 1009;
private static final int PG_TYPE_VARCHAR = 1043;
private static final int PG_TYPE_DATE = 1082;
private static final int PG_TYPE_TIME = 1083;
private static final int PG_TYPE_TIMESTAMP_NO_TMZONE = 1114;
private static final int PG_TYPE_NUMERIC = 1700;
private int port = PgServer.DEFAULT_PORT; private int port = PgServer.DEFAULT_PORT;
private boolean stop; private boolean stop;
private boolean trace; private boolean trace;
...@@ -373,4 +395,49 @@ public class PgServer implements Service { ...@@ -373,4 +395,49 @@ public class PgServer implements Service {
return 1; return 1;
} }
/**
* Convert the SQL type to a PostgreSQL type
*
* @param type the SQL type
* @return the PostgreSQL type
*/
public static int convertType(final int type) {
switch (type) {
case Types.BOOLEAN:
return PG_TYPE_BOOL;
case Types.VARCHAR:
return PG_TYPE_VARCHAR;
case Types.CLOB:
return PG_TYPE_TEXT;
case Types.CHAR:
return PG_TYPE_CHAR;
case Types.SMALLINT:
return PG_TYPE_INT2;
case Types.INTEGER:
return PG_TYPE_INT4;
case Types.BIGINT:
return PG_TYPE_INT8;
case Types.DECIMAL:
return PG_TYPE_NUMERIC;
case Types.REAL:
return PG_TYPE_FLOAT4;
case Types.DOUBLE:
return PG_TYPE_FLOAT8;
case Types.TIME:
return PG_TYPE_TIME;
case Types.DATE:
return PG_TYPE_DATE;
case Types.TIMESTAMP:
return PG_TYPE_TIMESTAMP_NO_TMZONE;
case Types.VARBINARY:
return PG_TYPE_BYTEA;
case Types.BLOB:
return PG_TYPE_OID;
case Types.ARRAY:
return PG_TYPE_TEXTARRAY;
default:
return PG_TYPE_UNKNOWN;
}
}
} }
...@@ -61,7 +61,7 @@ public class PgServerThread implements Runnable { ...@@ -61,7 +61,7 @@ public class PgServerThread implements Runnable {
private String userName; private String userName;
private String databaseName; private String databaseName;
private int processId; private int processId;
private String clientEncoding = "UTF-8"; private String clientEncoding = SysProperties.PG_DEFAULT_CLIENT_ENCODING;
private String dateStyle = "ISO"; private String dateStyle = "ISO";
private HashMap<String, Prepared> prepared = New.hashMap(); private HashMap<String, Prepared> prepared = New.hashMap();
private HashMap<String, Portal> portals = New.hashMap(); private HashMap<String, Portal> portals = New.hashMap();
......
...@@ -8,6 +8,9 @@ ...@@ -8,6 +8,9 @@
drop schema if exists pg_catalog; drop schema if exists pg_catalog;
create schema pg_catalog; create schema pg_catalog;
drop alias if exists pg_convertType;
create alias pg_convertType deterministic for "org.h2.server.pg.PgServer.convertType";
create table pg_catalog.pg_version as select 1 as version; create table pg_catalog.pg_version as select 1 as version;
create view pg_catalog.pg_roles -- (oid, rolname, rolcreaterole, rolcreatedb) create view pg_catalog.pg_roles -- (oid, rolname, rolcreaterole, rolcreatedb)
...@@ -36,17 +39,18 @@ create table pg_catalog.pg_type( ...@@ -36,17 +39,18 @@ create table pg_catalog.pg_type(
insert into pg_catalog.pg_type insert into pg_catalog.pg_type
select select
data_type oid, pg_convertType(data_type) oid,
cast(type_name as varchar_ignorecase) typname, cast(type_name as varchar_ignorecase) typname,
(select oid from pg_catalog.pg_namespace where nspname = 'pg_catalog') typnamespace, (select oid from pg_catalog.pg_namespace where nspname = 'pg_catalog') typnamespace,
-1 typlen, -1 typlen,
'c' typtype, 'c' typtype,
0 typbasetype 0 typbasetype
from information_schema.type_info from information_schema.type_info
where pos = 0; where pos = 0
and pg_convertType(data_type) <> 705; -- not unknown
merge into pg_catalog.pg_type values( merge into pg_catalog.pg_type values(
1111, 19,
'name', 'name',
(select oid from pg_catalog.pg_namespace where nspname = 'pg_catalog'), (select oid from pg_catalog.pg_namespace where nspname = 'pg_catalog'),
-1, -1,
...@@ -122,7 +126,7 @@ select ...@@ -122,7 +126,7 @@ select
t.id*10000 + c.ordinal_position oid, t.id*10000 + c.ordinal_position oid,
t.id attrelid, t.id attrelid,
c.column_name attname, c.column_name attname,
data_type atttypid, pg_convertType(data_type) atttypid,
-1 attlen, -1 attlen,
c.ordinal_position attnum, c.ordinal_position attnum,
-1 atttypmod, -1 atttypmod,
...@@ -137,7 +141,7 @@ select ...@@ -137,7 +141,7 @@ select
1000000 + t.id*10000 + c.ordinal_position oid, 1000000 + t.id*10000 + c.ordinal_position oid,
i.id attrelid, i.id attrelid,
c.column_name attname, c.column_name attname,
data_type atttypid, pg_convertType(data_type) atttypid,
-1 attlen, -1 attlen,
c.ordinal_position attnum, c.ordinal_position attnum,
-1 atttypmod, -1 atttypmod,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论