org.h2.server.pg

## Class PgServer

- java.lang.Object
- - org.h2.server.pg.PgServer

All Implemented Interfaces:
  Service

    public class PgServer
    extends java.lang.Object
    implements Service

This class implements a subset of the PostgreSQL protocol as described here: http://developer.postgresql.org/pgdocs/postgres/protocol.html The PostgreSQL catalog is described here: http://www.postgresql.org/docs/7.4/static/catalogs.html

### Field Summary

Fields
`static int` `DEFAULT_PORT` The default port to use for the PG server.
`static int` `PG_TYPE_BOOL`
`static int` `PG_TYPE_BPCHAR`
`static int` `PG_TYPE_BYTEA`
`static int` `PG_TYPE_DATE`
`static int` `PG_TYPE_FLOAT4`
`static int` `PG_TYPE_FLOAT8`
`static int` `PG_TYPE_INT2`
`static int` `PG_TYPE_INT4`
`static int` `PG_TYPE_INT8`
`static int` `PG_TYPE_NUMERIC`
`static int` `PG_TYPE_OID`
`static int` `PG_TYPE_TEXT`
`static int` `PG_TYPE_TEXTARRAY`
`static int` `PG_TYPE_TIME`
`static int` `PG_TYPE_TIMESTAMP_NO_TMZONE`
`static int` `PG_TYPE_UNKNOWN`
`static int` `PG_TYPE_VARCHAR` The VARCHAR type.

### Constructor Summary

Constructors
Constructor and Description
`PgServer()`

### Method Summary

Methods
`java.lang.String` `checkKeyAndGetDatabaseName(java.lang.String db)` If no key is set, return the original database name.
`static int` `convertType(int type)` Convert the SQL type to a PostgreSQL type
`static java.lang.String` `formatType(java.sql.Connection conn, int pgType, int typeMod)` Check if the current session has access to this table.
`boolean` `getAllowOthers()` Check if remote connections are allowed.
`static int` `getCurrentTid(java.lang.String table, java.lang.String id)` Get the current transaction id.
`static java.lang.String` `getEncodingName(int code)` Get the name of this encoding code.
`static java.lang.String` `getIndexColumn(java.sql.Connection conn, int indexId, java.lang.Integer ordinalPosition, java.lang.Boolean pretty)` The Java implementation of the PostgreSQL function pg_get_indexdef.
`java.lang.String` `getName()` Get the human readable name of the service.
`static int` `getOid(java.sql.Connection conn, java.lang.String tableName)` Get the OID of an object.
`static java.lang.String` `getPgExpr(java.lang.String exprText, int relationOid)` A fake wrapper around pg_get_expr(expr_text, relation_oid), in PostgreSQL it "decompiles the internal form of an expression, assuming that any vars in it refer to the relation indicated by the second parameter".
`int` `getPort()` Gets the port this service is listening on.
`static java.sql.Timestamp` `getStartTime()` Get the current system time.
`java.lang.String` `getType()` Get the human readable short name of the service.
`java.lang.String` `getURL()` Get the URL of this service in a human readable form
`static java.lang.String` `getUserById(java.sql.Connection conn, int id)` Get the user name for this id.
`static java.lang.String` `getVersion()` Get the version.
`static boolean` `hasDatabasePrivilege(int id, java.lang.String privilege)` Check if the this session has the given database privilege.
`static boolean` `hasTablePrivilege(java.lang.String table, java.lang.String privilege)` Check if the current session has access to this table.
`void` `init(java.lang.String... args)` Initialize the service from command line options.
`boolean` `isDaemon()` Check if a daemon thread should be used.
`boolean` `isRunning(boolean traceError)` Check if the service is running.
`void` `listen()` Listen for incoming connections.
`void` `start()` Start the service.
`void` `stop()` Stop the service.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### DEFAULT_PORT

    public static final int DEFAULT_PORT

The default port to use for the PG server. This value is also in the documentation and in the Server javadoc.

See Also:
  Constant Field Values

#### PG_TYPE_VARCHAR

    public static final int PG_TYPE_VARCHAR

The VARCHAR type.

See Also:
  Constant Field Values

#### PG_TYPE_BOOL

    public static final int PG_TYPE_BOOL

See Also:
  Constant Field Values

#### PG_TYPE_BYTEA

    public static final int PG_TYPE_BYTEA

See Also:
  Constant Field Values

#### PG_TYPE_BPCHAR

    public static final int PG_TYPE_BPCHAR

See Also:
  Constant Field Values

#### PG_TYPE_INT8

    public static final int PG_TYPE_INT8

See Also:
  Constant Field Values

#### PG_TYPE_INT2

    public static final int PG_TYPE_INT2

See Also:
  Constant Field Values

#### PG_TYPE_INT4

    public static final int PG_TYPE_INT4

See Also:
  Constant Field Values

#### PG_TYPE_TEXT

    public static final int PG_TYPE_TEXT

See Also:
  Constant Field Values

#### PG_TYPE_OID

    public static final int PG_TYPE_OID

See Also:
  Constant Field Values

#### PG_TYPE_FLOAT4

    public static final int PG_TYPE_FLOAT4

See Also:
  Constant Field Values

#### PG_TYPE_FLOAT8

    public static final int PG_TYPE_FLOAT8

See Also:
  Constant Field Values

#### PG_TYPE_UNKNOWN

    public static final int PG_TYPE_UNKNOWN

See Also:
  Constant Field Values

#### PG_TYPE_TEXTARRAY

    public static final int PG_TYPE_TEXTARRAY

See Also:
  Constant Field Values

#### PG_TYPE_DATE

    public static final int PG_TYPE_DATE

See Also:
  Constant Field Values

#### PG_TYPE_TIME

    public static final int PG_TYPE_TIME

See Also:
  Constant Field Values

#### PG_TYPE_TIMESTAMP_NO_TMZONE

    public static final int PG_TYPE_TIMESTAMP_NO_TMZONE

See Also:
  Constant Field Values

#### PG_TYPE_NUMERIC

    public static final int PG_TYPE_NUMERIC

See Also:
  Constant Field Values

### Constructor Detail

#### PgServer

    public PgServer()

### Method Detail

#### init

    public void init(java.lang.String... args)

Description copied from interface: `Service`
Initialize the service from command line options.

Specified by:
  `init` in interface `Service`
Parameters:
  `args` - the command line options

#### getURL

    public java.lang.String getURL()

Description copied from interface: `Service`
Get the URL of this service in a human readable form

Specified by:
  `getURL` in interface `Service`
Returns:
  the url

#### getPort

    public int getPort()

Description copied from interface: `Service`
Gets the port this service is listening on.

Specified by:
  `getPort` in interface `Service`
Returns:
  the port

#### start

    public void start()

Description copied from interface: `Service`
Start the service. This usually means create the server socket. This method must not block.

Specified by:
  `start` in interface `Service`

#### listen

    public void listen()

Description copied from interface: `Service`
Listen for incoming connections. This method blocks.

Specified by:
  `listen` in interface `Service`

#### stop

    public void stop()

Description copied from interface: `Service`
Stop the service.

Specified by:
  `stop` in interface `Service`

#### isRunning

    public boolean isRunning(boolean traceError)

Description copied from interface: `Service`
Check if the service is running.

Specified by:
  `isRunning` in interface `Service`
Parameters:
  `traceError` - if errors should be written
Returns:
  if the server is running

#### getAllowOthers

    public boolean getAllowOthers()

Description copied from interface: `Service`
Check if remote connections are allowed.

Specified by:
  `getAllowOthers` in interface `Service`
Returns:
  true if remote connections are allowed

#### getType

    public java.lang.String getType()

Description copied from interface: `Service`
Get the human readable short name of the service.

Specified by:
  `getType` in interface `Service`
Returns:
  the type

#### getName

    public java.lang.String getName()

Description copied from interface: `Service`
Get the human readable name of the service.

Specified by:
  `getName` in interface `Service`
Returns:
  the name

#### getIndexColumn

    public static java.lang.String getIndexColumn(java.sql.Connection conn,
                                  int indexId,
                                  java.lang.Integer ordinalPosition,
                                  java.lang.Boolean pretty)
                                           throws java.sql.SQLException

The Java implementation of the PostgreSQL function pg_get_indexdef. The method is used to get CREATE INDEX command for an index, or the column definition of one column in the index.

Parameters:
  `conn` - the connection
  `indexId` - the index id
  `ordinalPosition` - the ordinal position (null if the SQL statement should be returned)
  `pretty` - this flag is ignored
Returns:
  the SQL statement or the column name
Throws:
  `java.sql.SQLException`

#### getOid

    public static int getOid(java.sql.Connection conn,
             java.lang.String tableName)
                      throws java.sql.SQLException

Get the OID of an object. This method is called by the database.

Parameters:
  `conn` - the connection
  `tableName` - the table name
Returns:
  the oid
Throws:
  `java.sql.SQLException`

#### getEncodingName

    public static java.lang.String getEncodingName(int code)

Get the name of this encoding code. This method is called by the database.

Parameters:
  `code` - the encoding code
Returns:
  the encoding name

#### getVersion

    public static java.lang.String getVersion()

Get the version. This method must return PostgreSQL to keep some clients happy. This method is called by the database.

Returns:
  the server name and version

#### getStartTime

    public static java.sql.Timestamp getStartTime()

Get the current system time. This method is called by the database.

Returns:
  the current system time

#### getUserById

    public static java.lang.String getUserById(java.sql.Connection conn,
                               int id)
                                        throws java.sql.SQLException

Get the user name for this id. This method is called by the database.

Parameters:
  `conn` - the connection
  `id` - the user id
Returns:
  the user name
Throws:
  `java.sql.SQLException`

#### hasDatabasePrivilege

    public static boolean hasDatabasePrivilege(int id,
                               java.lang.String privilege)

Check if the this session has the given database privilege. This method is called by the database.

Parameters:
  `id` - the session id
  `privilege` - the privilege to check
Returns:
  true

#### hasTablePrivilege

    public static boolean hasTablePrivilege(java.lang.String table,
                            java.lang.String privilege)

Check if the current session has access to this table. This method is called by the database.

Parameters:
  `table` - the table name
  `privilege` - the privilege to check
Returns:
  true

#### getCurrentTid

    public static int getCurrentTid(java.lang.String table,
                    java.lang.String id)

Get the current transaction id. This method is called by the database.

Parameters:
  `table` - the table name
  `id` - the id
Returns:
  1

#### getPgExpr

    public static java.lang.String getPgExpr(java.lang.String exprText,
                             int relationOid)

A fake wrapper around pg_get_expr(expr_text, relation_oid), in PostgreSQL it "decompiles the internal form of an expression, assuming that any vars in it refer to the relation indicated by the second parameter".

Parameters:
  `exprText` - the expression text
  `relationOid` - the relation object id
Returns:
  always null

#### formatType

    public static java.lang.String formatType(java.sql.Connection conn,
                              int pgType,
                              int typeMod)
                                       throws java.sql.SQLException

Check if the current session has access to this table. This method is called by the database.

Parameters:
  `conn` - the connection
  `pgType` - the PostgreSQL type oid
  `typeMod` - the type modifier (typically -1)
Returns:
  the name of the given type
Throws:
  `java.sql.SQLException`

#### convertType

    public static int convertType(int type)

Convert the SQL type to a PostgreSQL type

Parameters:
  `type` - the SQL type
Returns:
  the PostgreSQL type

#### checkKeyAndGetDatabaseName

    public java.lang.String checkKeyAndGetDatabaseName(java.lang.String db)

If no key is set, return the original database name. If a key is set, check if the key matches. If yes, return the correct database name. If not, throw an exception.

Parameters:
  `db` - the key to test (or database name if no key is used)
Returns:
  the database name
Throws:
  `DbException` - if a key is set but doesn't match

#### isDaemon

    public boolean isDaemon()

Description copied from interface: `Service`
Check if a daemon thread should be used.

Specified by:
  `isDaemon` in interface `Service`
Returns:
  true if a daemon thread should be used