提交 13af88a2 authored 作者: Thomas Mueller's avatar Thomas Mueller

--no commit message

--no commit message
上级 7eb40855
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.server.pg;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import org.h2.server.Service;
import org.h2.util.MathUtils;
import org.h2.util.NetUtils;
/**
* @author Thomas
*/
public class PgServer implements Service {
// public static final int DEFAULT_PORT = 5433; // also in the docs
public static final int DEFAULT_PORT = 5435; // also in the docs
private int port = PgServer.DEFAULT_PORT;
private boolean stop;
private boolean log;
private ServerSocket serverSocket;
private HashSet running = new HashSet();
private String baseDir;
private String url;
private boolean allowOthers;
private boolean ifExists;
public static void main(String[] args) throws Exception {
PgServer app = new PgServer();
app.init(args);
app.start();
app.listen();
}
boolean getLog() {
return log;
}
void log(String s) {
if (log) {
System.out.println(s);
}
}
synchronized void remove(PgServerThread t) {
running.remove(t);
}
void logError(Exception e) {
if (log) {
e.printStackTrace();
}
}
public void init(String[] args) throws Exception {
port = DEFAULT_PORT;
for (int i = 0; i < args.length; i++) {
String a = args[i];
if (a.equals("-log")) {
log = Boolean.valueOf(args[++i]).booleanValue();
} else if (a.equals("-pgPort")) {
port = MathUtils.decodeInt(args[++i]);
} else if (a.equals("-baseDir")) {
baseDir = args[++i];
} else if (a.equals("-pgAllowOthers")) {
allowOthers = Boolean.valueOf(args[++i]).booleanValue();
} else if (a.equals("-ifExists")) {
ifExists = Boolean.valueOf(args[++i]).booleanValue();
}
}
org.h2.Driver.load();
url = "odbc://localhost:" + port;
}
public String getURL() {
return url;
}
boolean allow(Socket socket) {
if(allowOthers) {
return true;
}
return NetUtils.isLoopbackAddress(socket);
}
public void start() throws SQLException {
serverSocket = NetUtils.createServerSocket(port, false);
}
public void listen() {
String threadName = Thread.currentThread().getName();
try {
while (!stop) {
Socket s = serverSocket.accept();
if(!allow(s)) {
log("Connection not allowed");
s.close();
} else {
PgServerThread c = new PgServerThread(s, this);
running.add(c);
c.setProcessId(running.size());
Thread thread = new Thread(c);
thread.setName(threadName+" thread");
c.setThread(thread);
thread.start();
}
}
} catch (Exception e) {
if(!stop) {
e.printStackTrace();
}
}
}
public void stop() {
// TODO server: share code between web and tcp servers
if(!stop) {
stop = true;
if(serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
// TODO log exception
e.printStackTrace();
}
serverSocket = null;
}
}
// TODO server: using a boolean 'now' argument? a timeout?
ArrayList list = new ArrayList(running);
for(int i=0; i<list.size(); i++) {
PgServerThread c = (PgServerThread) list.get(i);
c.close();
try {
c.getThread().join(100);
} catch(Exception e) {
// TODO log exception
e.printStackTrace();
}
}
}
public boolean isRunning() {
if(serverSocket == null) {
return false;
}
try {
Socket s = NetUtils.createSocket(InetAddress.getLocalHost(), serverSocket.getLocalPort(), false);
s.close();
return true;
} catch(Exception e) {
return false;
}
}
public String getBaseDir() {
return baseDir;
}
public boolean getAllowOthers() {
return allowOthers;
}
public String getType() {
return "PG";
}
public boolean getIfExists() {
return ifExists;
}
}
drop schema if exists pg_catalog;
create schema pg_catalog;
create view pg_catalog.pg_type(oid, typlen, typbasetype, typname, typnamespace) as
select
data_type oid,
precision,
data_type,
cast(type_name as varchar_ignorecase) typbasetype,
select id from information_schema.schemata where schema_name='PG_CATALOG'
from information_schema.type_info
union select
1111,
64,
1111,
'name',
select id from information_schema.schemata where schema_name='PG_CATALOG'
from dual;
create view pg_catalog.pg_namespace(oid, nspname) as
select id, cast(schema_name as varchar_ignorecase)
from information_schema.schemata;
create view pg_catalog.pg_class(oid, relname, relnamespace, relkind) as
select
id,
cast(table_name as varchar_ignorecase),
(select id from information_schema.schemata where schema_name = table_schema),
case table_type when 'TABLE' then 'r' else 'v' end
from information_schema.tables;
create view pg_catalog.pg_description(objoid, objsubid, classoid, description) as
select id, 0, -1, cast('' as varchar_ignorecase)
from information_schema.tables where 1=0;
create view pg_catalog.pg_attrdef(oid, adsrc, adrelid, adnum) as
select id, 0, 0, 0
from information_schema.tables where 1=0;
create view pg_catalog.pg_attribute(oid, attname, atttypid, attnotnull, atttypmod,
attlen, attnum, attrelid, attisdropped) as
select
t.id*10000 + ordinal_position, column_name, data_type, false, -1,
numeric_precision, ordinal_position, t.id, false
from
information_schema.tables t,
information_schema.columns c
where t.table_name = c.table_name
and t.table_schema = c.table_schema;
SELECT
NULL AS TABLE_CAT,
n.nspname AS TABLE_SCHEM,
ct.relname AS TABLE_NAME,
NOT i.indisunique AS NON_UNIQUE,
NULL AS INDEX_QUALIFIER,
ci.relname AS INDEX_NAME,
CASE i.indisclustered WHEN true THEN 1 ELSE CASE am.amname WHEN 'hash' THEN 2 ELSE 3 END END AS TYPE,
a.attnum AS ORDINAL_POSITION,
CASE i.indexprs
WHEN null THEN a.attname
ELSE pg_get_indexdef(ci.oid,a.attnum,false) END
AS COLUMN_NAME,
NULL AS ASC_OR_DESC,
ci.reltuples AS CARDINALITY,
ci.relpages AS PAGES,
NULL AS FILTER_CONDITION
FROM pg_catalog.pg_namespace n,
pg_catalog.pg_class ct,
pg_catalog.pg_class ci,
pg_catalog.pg_attribute a,
pg_catalog.pg_am am ,
pg_catalog.pg_index i
WHERE ct.oid=i.indrelid
AND ci.oid=i.indexrelid
AND a.attrelid=ci.oid
AND ci.relam=am.oid
AND n.oid = ct.relnamespace
AND n.nspname = 'PUBLIC'
AND ct.relname = 'TEST'
ORDER BY NON_UNIQUE, TYPE, INDEX_NAME, ORDINAL_POSITION ;
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论