org.intermine.sql
Class DatabaseUtil

java.lang.Object
  extended by org.intermine.sql.DatabaseUtil

public final class DatabaseUtil
extends java.lang.Object

Collection of commonly used Database utilities


Method Summary
static void analyse(Database db, boolean full)
          Analyse given database, perform vacuum full analyse if full parameter true.
static void analyse(Database db, ClassDescriptor cld, boolean full)
          Analyse database table for a given class and all associated indirection tables.
static void createBagTable(Database db, java.sql.Connection con, java.lang.String tableName, java.util.Collection<?> bag, java.lang.Class<?> c)
          Create a new table the holds the contents of the given Collection (bag).
static java.lang.String generateSqlCompatibleName(java.lang.String n)
          Convert any sql keywords to valid names for tables/columns.
static java.lang.String getColumnName(FieldDescriptor fd)
          Creates a column name for a field descriptor
static java.lang.String getIndirectionTableName(CollectionDescriptor col)
          Creates an indirection table name for a many-to-many collection descriptor
static java.util.Set<java.lang.String> getIndirectionTableNames(ClassDescriptor cld)
          Given a ClassDescriptor find names of all related indirection tables.
static java.lang.String getInwardIndirectionColumnName(CollectionDescriptor col, int version)
          Creates a column name for the "inward" key of a many-to-many collection descriptor.
static java.lang.String getOutwardIndirectionColumnName(CollectionDescriptor col, int version)
          Creates a column name for the "outward" key of a many-to-many collection descriptor.
static java.lang.String getTableName(ClassDescriptor cld)
          Creates a table name for a class descriptor
static void grant(Database db, java.lang.String user, java.lang.String perm)
          Grant permission on all tables for given user on specified database.
static java.lang.String objectToString(java.lang.Object o)
          Generate an SQL compatible representation of an object.
static void removeAllTables(java.sql.Connection con)
          Removes every single table from the database given.
static boolean tableExists(java.sql.Connection con, java.lang.String tableName)
          Tests if a table exists in the database
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

tableExists

public static boolean tableExists(java.sql.Connection con,
                                  java.lang.String tableName)
                           throws java.sql.SQLException
Tests if a table exists in the database

Parameters:
con - a connection to a database
tableName - the name of a table to test for
Returns:
true if the table exists, false otherwise
Throws:
java.sql.SQLException - if an error occurs in the underlying database
java.lang.NullPointerException - if tableName is null

removeAllTables

public static void removeAllTables(java.sql.Connection con)
                            throws java.sql.SQLException
Removes every single table from the database given.

Parameters:
con - the Connection to the database
Throws:
java.sql.SQLException - if an error occurs in the underlying database

getTableName

public static java.lang.String getTableName(ClassDescriptor cld)
Creates a table name for a class descriptor

Parameters:
cld - ClassDescriptor
Returns:
a valid table name

getColumnName

public static java.lang.String getColumnName(FieldDescriptor fd)
Creates a column name for a field descriptor

Parameters:
fd - FieldDescriptor
Returns:
a valid column name

getIndirectionTableName

public static java.lang.String getIndirectionTableName(CollectionDescriptor col)
Creates an indirection table name for a many-to-many collection descriptor

Parameters:
col - CollectionDescriptor
Returns:
a valid table name

getInwardIndirectionColumnName

public static java.lang.String getInwardIndirectionColumnName(CollectionDescriptor col,
                                                              int version)
Creates a column name for the "inward" key of a many-to-many collection descriptor.

Parameters:
col - CollectionDescriptor
version - the database version number
Returns:
a valid column name

getOutwardIndirectionColumnName

public static java.lang.String getOutwardIndirectionColumnName(CollectionDescriptor col,
                                                               int version)
Creates a column name for the "outward" key of a many-to-many collection descriptor.

Parameters:
col - CollectionDescriptor
version - the database version number
Returns:
a valid column name

generateSqlCompatibleName

public static java.lang.String generateSqlCompatibleName(java.lang.String n)
Convert any sql keywords to valid names for tables/columns.

Parameters:
n - the string to convert
Returns:
a valid sql name

objectToString

public static java.lang.String objectToString(java.lang.Object o)
Generate an SQL compatible representation of an object.

Parameters:
o - the Object
Returns:
a valid SQL String
Throws:
java.lang.IllegalArgumentException - if the object is not representable

analyse

public static void analyse(Database db,
                           boolean full)
                    throws java.sql.SQLException
Analyse given database, perform vacuum full analyse if full parameter true. WARNING: currently PostgreSQL specific

Parameters:
db - the database to analyse
full - if true perform VACUUM FULL ANALYSE
Throws:
java.sql.SQLException - if db problem

analyse

public static void analyse(Database db,
                           ClassDescriptor cld,
                           boolean full)
                    throws java.sql.SQLException
Analyse database table for a given class and all associated indirection tables. WARNING: currently PostgreSQL specific

Parameters:
db - the database to analyse
cld - description of class to analyse
full - if true perform VACUUM FULL ANALYSE
Throws:
java.sql.SQLException - if db problem

getIndirectionTableNames

public static java.util.Set<java.lang.String> getIndirectionTableNames(ClassDescriptor cld)
Given a ClassDescriptor find names of all related indirection tables.

Parameters:
cld - class to find tables for
Returns:
a set of all indirection table names

grant

public static void grant(Database db,
                         java.lang.String user,
                         java.lang.String perm)
                  throws java.sql.SQLException
Grant permission on all tables for given user on specified database.

Parameters:
db - the database to grant permissions on
user - the username to grant permission to
perm - permission to grant
Throws:
java.sql.SQLException - if db problem

createBagTable

public static void createBagTable(Database db,
                                  java.sql.Connection con,
                                  java.lang.String tableName,
                                  java.util.Collection<?> bag,
                                  java.lang.Class<?> c)
                           throws java.sql.SQLException
Create a new table the holds the contents of the given Collection (bag). The "Class c" parameter selects which objects from the bag are put in the new table. eg. if the bag contains Integers and Strings and the parameter is Integer.class then the table will contain only the Integers from the bag. A Class of InterMineObject is handled specially: the new table will contain the IDs of the objects, not the objects themselves. The table will have one column ("value").

Parameters:
db - the Database to access
con - the Connection to use
tableName - the name to use for the new table
bag - the Collection to create a table for
c - the type of objects to put in the new table
Throws:
java.sql.SQLException - if there is a database problem