javatools.database
Class PostgresDatabase

java.lang.Object
  extended by javatools.database.Database
      extended by javatools.database.PostgresDatabase

public class PostgresDatabase
extends Database

This class is part of the Java Tools (see http://mpii.de/yago-naga/javatools). It is licensed under the Creative Commons Attribution License (see http://creativecommons.org/licenses/by/3.0) by the YAGO-NAGA team (see http://mpii.de/yago-naga). The class PostgresDatabase implements the Database-interface for a PostgreSQL data base. Make sure that the file "postgresql-version.jdbc3.jar" of the Postgres distribution is in the classpath. When using Eclipse, add the file via Project ->Properties ->JavaBuildPath ->Libraries ->ExternalJARFile.
Example:

     Database d=new PostgresDatabase("user","password");     
     d.queryColumn("SELECT foodname FROM food WHERE origin=\"Italy\"")
     -> [ "Pizza Romana", "Spaghetti alla Bolognese", "Saltimbocca"]
     Database.describe(d.query("SELECT * FROM food WHERE origin=\"Italy\"")
     -> foodname |origin  |calories |
        ------------------------------
        Pizza Rom|Italy   |10000    |
        Spaghetti|Italy   |8000     |
        Saltimboc|Italy   |8000     |        
This class also provides SQL datatypes (extensions of SQLType.java) that behave according to the conventions of Postgres. For example, VARCHAR string literals print inner quotes as doublequotes.


Nested Class Summary
static class PostgresDatabase.Postgretext
           
static class PostgresDatabase.PostgreVarchar
           
 
Nested classes/interfaces inherited from class javatools.database.Database
Database.CommitTransactionSQLException, Database.ConnectionBrokenSQLException, Database.InitTransactionSQLException, Database.Inserter, Database.RollbackTransactionSQLException, Database.StartAutoCommitSQLException, Database.TransactionSQLException
 
Field Summary
static PostgresDatabase.Postgretext postgretext
           
static PostgresDatabase.PostgreVarchar postgrevarchar
           
 
Fields inherited from class javatools.database.Database
java2SQL, MINCOLUMNWIDTH, SCREENWIDTH, type2SQL
 
Constructor Summary
PostgresDatabase()
          Constructs a non-functional PostgresDatabase for use of getSQLType
PostgresDatabase(java.lang.String user, java.lang.String password, java.lang.String database, java.lang.String host, java.lang.String port)
           
PostgresDatabase(java.lang.String user, java.lang.String password, java.lang.String database, java.lang.String host, java.lang.String port, boolean useSSL)
          Constructs a new Database from a user, a password and a host
PostgresDatabase(java.lang.String user, java.lang.String password, java.lang.String database, java.lang.String host, java.lang.String port, java.lang.String schema)
          Constructs a new Database from a user, a password and a host, setting also the (preferred) schema (public stays fallback schema)
PostgresDatabase(java.lang.String user, java.lang.String password, java.lang.String database, java.lang.String host, java.lang.String port, java.lang.String schema, boolean useSSL)
          Constructs a new Database from a user, a password and a host, setting also the (preferred) schema (public stays fallback schema)
 
Method Summary
 java.lang.String autoincrementColumn()
          Produces an SQL fragment representing column properties for an autoincrementing integer column s.t.
 java.lang.String cast(java.lang.String value, java.lang.String type)
          Produces an SQL fragment casting the given value to the given type *
 void connect()
          connects to the database specified
 void createTable(java.lang.String name, java.lang.Object... attributes)
          Creates or rewrites an SQL table.
 java.lang.String getSQLStmntIFNULL(java.lang.String a, java.lang.String b)
          returns the database system specific expression for ifnull functionality i.e.
 boolean jarAvailable()
          TRUE if the required JAR is there
 void lockTableReadAccess(java.util.Map<java.lang.String,java.lang.String> tablesAndAliases)
          Locks a table in read mode, i.e.
 void lockTableWriteAccess(java.util.Map<java.lang.String,java.lang.String> tablesAndAliases)
          Locks a table in write mode, i.e.
static void main(java.lang.String[] args)
           
 void releaseLocksAndEndTransaction()
          releases all locks the connection holds, commits the current transaction and ends it
 void setExclusiveSchema(java.lang.String s)
          Sets the default schema
 void setSchema(java.lang.String s)
          Sets the default schema
 void startTransaction()
          Initiates a transaction by disabling autocommit and enabling transaction mod Note: In Postgres transactions have a name e
 
Methods inherited from class javatools.database.Database
close, close, close, close, connected, createIndex, createIndexCommand, createIndices, createPrimaryKey, createView, describe, describe, dumpCSV, dumpQueryAsCSV, endTransaction, endTransaction, executeUpdate, exists, existsTable, finalize, flush, format, formatNullToNull, getConnection, getFetchsize, getResultSetConcurrency, getResultSetType, getSQLType, getSQLType, getSQLType, getValidityCheckTimeout, indexName, isAutoReconnectingOnSelect, isAutoReconnectingOnUpdate, limit, loadCSV, makeCSV, makeCSVForQuery, newInserter, newInserter, newInserter, offset, query, query, query, query, queryValue, reconnect, resetTransaction, runInterface, setAutoReconnectOnSelect, setAutoReconnectOnUpdate, setFetchsize, setResultSetConcurrency, setResultSetType, setValidityCheckTimeout, toString
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

postgrevarchar

public static PostgresDatabase.PostgreVarchar postgrevarchar

postgretext

public static PostgresDatabase.Postgretext postgretext
Constructor Detail

PostgresDatabase

public PostgresDatabase()
Constructs a non-functional PostgresDatabase for use of getSQLType


PostgresDatabase

public PostgresDatabase(java.lang.String user,
                        java.lang.String password,
                        java.lang.String database,
                        java.lang.String host,
                        java.lang.String port,
                        boolean useSSL)
                 throws java.lang.InstantiationException,
                        java.lang.IllegalAccessException,
                        java.lang.ClassNotFoundException,
                        java.sql.SQLException
Constructs a new Database from a user, a password and a host

Throws:
java.lang.ClassNotFoundException
java.lang.IllegalAccessException
java.lang.InstantiationException
java.sql.SQLException

PostgresDatabase

public PostgresDatabase(java.lang.String user,
                        java.lang.String password,
                        java.lang.String database,
                        java.lang.String host,
                        java.lang.String port)
                 throws java.lang.InstantiationException,
                        java.lang.IllegalAccessException,
                        java.lang.ClassNotFoundException,
                        java.sql.SQLException
Throws:
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.ClassNotFoundException
java.sql.SQLException

PostgresDatabase

public PostgresDatabase(java.lang.String user,
                        java.lang.String password,
                        java.lang.String database,
                        java.lang.String host,
                        java.lang.String port,
                        java.lang.String schema,
                        boolean useSSL)
                 throws java.lang.InstantiationException,
                        java.lang.IllegalAccessException,
                        java.lang.ClassNotFoundException,
                        java.sql.SQLException
Constructs a new Database from a user, a password and a host, setting also the (preferred) schema (public stays fallback schema)

Throws:
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.ClassNotFoundException
java.sql.SQLException

PostgresDatabase

public PostgresDatabase(java.lang.String user,
                        java.lang.String password,
                        java.lang.String database,
                        java.lang.String host,
                        java.lang.String port,
                        java.lang.String schema)
                 throws java.lang.InstantiationException,
                        java.lang.IllegalAccessException,
                        java.lang.ClassNotFoundException,
                        java.sql.SQLException
Constructs a new Database from a user, a password and a host, setting also the (preferred) schema (public stays fallback schema)

Throws:
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.ClassNotFoundException
java.sql.SQLException
Method Detail

connect

public void connect()
             throws java.sql.SQLException
connects to the database specified

Specified by:
connect in class Database
Throws:
java.sql.SQLException

setSchema

public void setSchema(java.lang.String s)
               throws java.sql.SQLException
Sets the default schema

Throws:
java.sql.SQLException

setExclusiveSchema

public void setExclusiveSchema(java.lang.String s)
                        throws java.sql.SQLException
Sets the default schema

Throws:
java.sql.SQLException

createTable

public void createTable(java.lang.String name,
                        java.lang.Object... attributes)
                 throws java.sql.SQLException
Creates or rewrites an SQL table. Attributes is an alternating sequence of a name (String) and a type (from java.sql.Type).

Overrides:
createTable in class Database
Throws:
java.sql.SQLException

jarAvailable

public boolean jarAvailable()
Description copied from class: Database
TRUE if the required JAR is there

Overrides:
jarAvailable in class Database

cast

public java.lang.String cast(java.lang.String value,
                             java.lang.String type)
Produces an SQL fragment casting the given value to the given type *

Overrides:
cast in class Database

getSQLStmntIFNULL

public java.lang.String getSQLStmntIFNULL(java.lang.String a,
                                          java.lang.String b)
returns the database system specific expression for ifnull functionality i.e. ifnull(a,b) returns b if a is null and a otherwise

Overrides:
getSQLStmntIFNULL in class Database

autoincrementColumn

public java.lang.String autoincrementColumn()
Produces an SQL fragment representing column properties for an autoincrementing integer column s.t. if used during table creation a column can declared to get by default an integer value assigned according to an internal self-incrementing sequence counter Example: createTable("tableWithSingleAutoIncrementingIDColumn", "ID", autoincrementColumn())

Overrides:
autoincrementColumn in class Database

startTransaction

public void startTransaction()
                      throws Database.InitTransactionSQLException
Initiates a transaction by disabling autocommit and enabling transaction mod Note: In Postgres transactions have a name e

Overrides:
startTransaction in class Database
Throws:
Database.InitTransactionSQLException

lockTableWriteAccess

public void lockTableWriteAccess(java.util.Map<java.lang.String,java.lang.String> tablesAndAliases)
                          throws java.sql.SQLException
Locks a table in write mode, i.e. other db connections can only read the table, but not write to it

Overrides:
lockTableWriteAccess in class Database
Throws:
java.sql.SQLException

lockTableReadAccess

public void lockTableReadAccess(java.util.Map<java.lang.String,java.lang.String> tablesAndAliases)
                         throws java.sql.SQLException
Locks a table in read mode, i.e. only this connection can read or write the table

Overrides:
lockTableReadAccess in class Database
Throws:
java.sql.SQLException

releaseLocksAndEndTransaction

public void releaseLocksAndEndTransaction()
                                   throws java.sql.SQLException
releases all locks the connection holds, commits the current transaction and ends it

Overrides:
releaseLocksAndEndTransaction in class Database
Throws:
java.sql.SQLException

main

public static void main(java.lang.String[] args)