com.evanmclean.evlib.sql
Class PostgresqlUtils

java.lang.Object
  extended by com.evanmclean.evlib.sql.PostgresqlUtils

public final class PostgresqlUtils
extends Object

Utilities specific to the Postgres JDBC database.

Author:
Evan McLean McLean Computer Services (see the overview for copyright and licensing.)

Method Summary
static int getCurrVal(String name, Connection conn)
          Read the current value from a sequence.
static int getNextVal(String name, Connection conn)
          Read the next value from a sequence.
static Array intToArray(int[] arr)
           Converts an array of integers to an Array object that can be used in a PreparedStatement for Postgresql to set an int4[] column.
static Array intToArray(int[] arr, String base_type_name)
           Converts an array of integers to an Array object that can be used in a PreparedStatement for Postgresql.
static Array stringToArray(String[] arr)
           Converts an array of Strings to an Array object that can be used in a PreparedStatement for Postgresql to set an varchar[] column.
static Array stringToArray(String[] arr, String base_type_name)
           Converts an array of Strings to an Array object that can be used in a PreparedStatement for Postgresql.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getCurrVal

public static int getCurrVal(String name,
                             Connection conn)
                      throws SQLException
Read the current value from a sequence.

Parameters:
name - The name of the sequence.
conn - The database connection to use.
Returns:
The current value of the sequence.
Throws:
SQLException

getNextVal

public static int getNextVal(String name,
                             Connection conn)
                      throws SQLException
Read the next value from a sequence.

Parameters:
name - The name of the sequence.
conn - The database connection to use.
Returns:
The next value of the sequence.
Throws:
SQLException

intToArray

public static Array intToArray(int[] arr)

Converts an array of integers to an Array object that can be used in a PreparedStatement for Postgresql to set an int4[] column.

FYI: When reading an int array column from Postgres, you can take the Array object returned by ResultSet.getArray and get the details by the code "(int[]) arrobject.getArray()", or you could use the getResultSet method instead.

NOTE: If you are using a JDBC driver that fully implements java 1.6 Connection objects then use Connection.createArrayOf() instead of this function.

Parameters:
arr - The array of integers.
Returns:
The SQL Array object, if a null array was specified.

intToArray

public static Array intToArray(int[] arr,
                               String base_type_name)

Converts an array of integers to an Array object that can be used in a PreparedStatement for Postgresql.

FYI: When reading an int array column from Postgres, you can take the Array object returned by ResultSet.getArray and get the details by the code "(int[]) arrobject.getArray()", or you could use the getResultSet method instead.

NOTE: If you are using a JDBC driver that fully implements java 1.6 Connection objects then use Connection.createArrayOf() instead of this function.

Parameters:
arr - The array of integers.
base_type_name - The base type of the integer array column you are trying to set. Will usually be int4, or one of the other integer types such as integer, int, int2, int8 , smallint or bigint.
Returns:
The SQL Array object, if a null array was specified.

stringToArray

public static Array stringToArray(String[] arr)

Converts an array of Strings to an Array object that can be used in a PreparedStatement for Postgresql to set an varchar[] column.

FYI: When reading a varchar array column from Postgres, you can take the Array object returned by ResultSet.getArray and get the details by the code "(String[]) arrobject.getArray() ", or you could use the getResultSet method instead.

NOTE: If you are using a JDBC driver that fully implements java 1.6 Connection objects then use Connection.createArrayOf() instead of this function.

Parameters:
arr - The array of Strings.
Returns:
The SQL Array object, if a null array was null.

stringToArray

public static Array stringToArray(String[] arr,
                                  String base_type_name)

Converts an array of Strings to an Array object that can be used in a PreparedStatement for Postgresql.

FYI: When reading a varchar array column from Postgres, you can take the Array object returned by ResultSet.getArray and get the details by the code "(String[]) arrobject.getArray() ", or you could use the getResultSet method instead.

NOTE: If you are using a JDBC driver that fully implements java 1.6 Connection objects then use Connection.createArrayOf() instead of this function.

Parameters:
arr - The array of Strings.
base_type_name - The base type of the String array column you are trying to set. Will usually be varchar.
Returns:
The SQL Array object, if a null array was null.