org.makumba.db.makumba.sql
Class Database

java.lang.Object
  extended by org.makumba.db.makumba.Database
      extended by org.makumba.db.makumba.sql.Database
Direct Known Subclasses:
CloudscapeDatabase, DbcpDatabase, HsqldbDatabase, MsqlDatabase, MsSqlDatabase, MySqlDatabase, OracleDatabase, PgsqlDatabase, QedDatabase

public class Database
extends Database

An SQL database, using JDBC. This class should comply with SQL-92


Field Summary
(package private)  boolean addUnderscore
           
(package private)  Hashtable<String,Vector<Hashtable<String,Object>>> catalog
           
(package private)  Properties connectionConfig
           
(package private) static int DESIRED_TRANSACTION_LEVEL
           
protected static String eng
           
protected  com.mchange.v2.c3p0.ComboPooledDataSource pooledDataSource
           
(package private) static Properties sqlDrivers
           
(package private)  String url
           
 
Fields inherited from class org.makumba.db.makumba.Database
nr, requestForeignKeys, requestUTF8, tp
 
Constructor Summary
Database(Properties p)
          Initialize the database.
 
Method Summary
protected  void checkState(SQLException e, String state)
          check the sql state of a SQL exception and throw a DBError if it is not equal with the given state
protected  void checkState(SQLException e, String state, String command)
          check the sql state of a SQL exception and throw a DBError if it is not equal with the given state
protected  void closeResourcePool()
           
protected  int exec(PreparedStatement ps)
          execute a prepared statement and log it, and its exceptions. return the number of records affected, or -1 if a duplicate error appeared
 Map<String,String> getDuplicateFields(SQLException e)
           
 String getEngine()
           
static String getEngineProperty(String s)
           
protected  int getIdleConnections()
           
protected  String getJdbcUrl(Properties p)
          builds a JDBC Connection URL given the host, sqlEngine and database properties
 String getLimitSyntax()
           
 int getMaxPointerValue()
           
 int getMinPointerValue()
           
 NameResolver getNameResolverHook()
           
protected  int getOpenedConnections()
           
 Pointer getPointer(String type, int uid)
           
protected  DBConnection getPooledDBConnection()
           
protected  int getResourcePoolSize()
           
protected  Class<?> getTableClass()
           
protected  Class<?> getTableClassConfigured()
          this method should be redefined by database classes that have a default table class. this returns null
 String getWrappedStatementToString(PreparedStatement ps)
          Since we use c3p0 for connection pooling we don't get the raw prepared statement anymore but a proxy.
 boolean isDuplicateException(SQLException e)
          return whether the exception indicates a duplicate. may need to be specified differently for certain database engines
 boolean isForeignKeyViolationException(SQLException se)
           
 boolean isLimitOffsetFirst()
           
(package private) static void logException(SQLException e)
           
(package private) static void logException(SQLException e, DBConnection dbc)
           
(package private) static void logException(SQLException e, DBConnection dbc, Level lev)
           
protected  DBConnection makeDBConnection()
           
 String OQLDate(Date d)
          write a date into an OQL query
 String parseReadableForeignKeyErrorMessage(SQLException se)
          Implementing classes can override this method to extract a more readable error message on foreign key errors.
 Query prepareQueryImpl(String oqlQuery, String insertIn)
           
 Update prepareUpdateImpl(String type, String set, String where)
           
protected  void readCatalog(SQLDBConnection dbc)
           
static String SQLEscape(String s)
          escapes all apostrophes from a string and puts the string into apostrophes to be added in a sql command
static boolean supportsForeignKeys()
           
 boolean supportsLimitInQuery()
          whether specific engine supports LIMIT & OFFSET extensions to the SQL-92 syntax
static boolean supportsUTF8()
           
 
Methods inherited from class org.makumba.db.makumba.Database
checkForeignKeys, close, copyFrom, copyFrom, copyFrom, copyFrom, copyFrom, deleteFrom, deleteFrom, deleteFrom, deleteFrom, finalize, findConfig, getConfiguration, getConfigurationProperties, getDBConnection, getDBConnection, getDbsv, getFieldNameInSource, getName, getNameResolver, getTable, getTable, getTypeNameInSource, initialiseTables, isAutoIncrement, makePseudoTable, openTable, openTables, usesHibernateIndexes
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

connectionConfig

Properties connectionConfig

url

String url

eng

protected static String eng

addUnderscore

boolean addUnderscore

catalog

Hashtable<String,Vector<Hashtable<String,Object>>> catalog

DESIRED_TRANSACTION_LEVEL

static final int DESIRED_TRANSACTION_LEVEL
See Also:
Constant Field Values

sqlDrivers

static Properties sqlDrivers

pooledDataSource

protected com.mchange.v2.c3p0.ComboPooledDataSource pooledDataSource
Constructor Detail

Database

public Database(Properties p)
Initialize the database. Use Database.getDatabase to initilize a SQL database! This constructor will be invoked there (thus it should be hidden :). Besides the properties needed for the org.makumba.db.Database, the following are expected:
sql.user
the username needed to connect to the database
sql.password
the password needed to connect to the database
To accomodate existing tables:
addUnderscore
if "true", all table and field names have underscore appended to it, to prevent conflict with SQL keywords in some engines. Make it false for compatibility with older databases.
typename_root= replacement
Specify a rule how to make table names shorter. E.g. a rule best.minerva.student=bms and a type best.minerva.student->fields will create table with shoter name _bms__fields_ instead of _best_minerva_student__fields_ (last underscore depends on the addUnderscore setting)
alter#typename_root
if "true", allows alteration of the table(s) corresponding to the data definition, if the table structure does not correspond to the data definition. The longest indicated root for a type counts (e.g. the more generic root can have alteration on true and the more specific one on false)
typename
can specify the name of the SQL table coresponding to the indicated type
typename#field
can specify the name of the SQL field corresponding to the indicated field
encoding
if "utf8" the outputted html will be in the utf8 charset and the connection with mysql will also be made in utf8

Method Detail

getEngine

public String getEngine()

getEngineProperty

public static String getEngineProperty(String s)

supportsUTF8

public static boolean supportsUTF8()

supportsForeignKeys

public static boolean supportsForeignKeys()

makeDBConnection

protected DBConnection makeDBConnection()
Specified by:
makeDBConnection in class Database

closeResourcePool

protected void closeResourcePool()
Specified by:
closeResourcePool in class Database

getResourcePoolSize

protected int getResourcePoolSize()
Specified by:
getResourcePoolSize in class Database

getIdleConnections

protected int getIdleConnections()
Specified by:
getIdleConnections in class Database

getOpenedConnections

protected int getOpenedConnections()
Specified by:
getOpenedConnections in class Database

getPooledDBConnection

protected DBConnection getPooledDBConnection()
Specified by:
getPooledDBConnection in class Database

readCatalog

protected void readCatalog(SQLDBConnection dbc)
                    throws SQLException
Throws:
SQLException

getJdbcUrl

protected String getJdbcUrl(Properties p)
builds a JDBC Connection URL given the host, sqlEngine and database properties


prepareQueryImpl

public Query prepareQueryImpl(String oqlQuery,
                              String insertIn)
Specified by:
prepareQueryImpl in class Database

prepareUpdateImpl

public Update prepareUpdateImpl(String type,
                                String set,
                                String where)
Specified by:
prepareUpdateImpl in class Database

getMinPointerValue

public int getMinPointerValue()
Specified by:
getMinPointerValue in class Database

getMaxPointerValue

public int getMaxPointerValue()
Specified by:
getMaxPointerValue in class Database

getTableClassConfigured

protected Class<?> getTableClassConfigured()
Description copied from class: Database
this method should be redefined by database classes that have a default table class. this returns null

Overrides:
getTableClassConfigured in class Database

getTableClass

protected Class<?> getTableClass()

SQLEscape

public static String SQLEscape(String s)
escapes all apostrophes from a string and puts the string into apostrophes to be added in a sql command


checkState

protected void checkState(SQLException e,
                          String state)
check the sql state of a SQL exception and throw a DBError if it is not equal with the given state


checkState

protected void checkState(SQLException e,
                          String state,
                          String command)
check the sql state of a SQL exception and throw a DBError if it is not equal with the given state


exec

protected int exec(PreparedStatement ps)
execute a prepared statement and log it, and its exceptions. return the number of records affected, or -1 if a duplicate error appeared


isDuplicateException

public boolean isDuplicateException(SQLException e)
return whether the exception indicates a duplicate. may need to be specified differently for certain database engines

Specified by:
isDuplicateException in class Database

getDuplicateFields

public Map<String,String> getDuplicateFields(SQLException e)
Specified by:
getDuplicateFields in class Database

isForeignKeyViolationException

public boolean isForeignKeyViolationException(SQLException se)

logException

static void logException(SQLException e)

logException

static void logException(SQLException e,
                         DBConnection dbc)

logException

static void logException(SQLException e,
                         DBConnection dbc,
                         Level lev)

OQLDate

public String OQLDate(Date d)
write a date into an OQL query

Specified by:
OQLDate in class Database

getPointer

public Pointer getPointer(String type,
                          int uid)
Specified by:
getPointer in class Database

supportsLimitInQuery

public boolean supportsLimitInQuery()
whether specific engine supports LIMIT & OFFSET extensions to the SQL-92 syntax


getLimitSyntax

public String getLimitSyntax()

isLimitOffsetFirst

public boolean isLimitOffsetFirst()

parseReadableForeignKeyErrorMessage

public String parseReadableForeignKeyErrorMessage(SQLException se)
Implementing classes can override this method to extract a more readable error message on foreign key errors.


getNameResolverHook

public NameResolver getNameResolverHook()

getWrappedStatementToString

public String getWrappedStatementToString(PreparedStatement ps)
Since we use c3p0 for connection pooling we don't get the raw prepared statement anymore but a proxy. This method fetches the toString() result of the wrapped prepared statement