velosurf.sql
Class DBConnection

java.lang.Object
  |
  +--velosurf.sql.DBConnection

public class DBConnection
extends java.lang.Object

this class encapsulates a connection to the database and contains all the stuff relative to it

Author:
Claude Brisson

Field Summary
static int CASE_SENSITIVE
          case-sensitive policy
static int LOWERCASE
           
protected  int mCaseSensivity
          case-sensivity
protected  java.sql.PreparedStatement mCheckStatement
          statement used to check database connection ("select 1")
protected  java.sql.PreparedStatement mCheckTransactionStatement
          statement used to check database transaction connection
protected  java.sql.Connection mConnection
          database connection
private  Cryptograph mCryptograph
          cryptograph used to encrypt/decrypt database ids
protected  int mDefaultCaching
          default caching mode
protected  boolean mDefaultReadOnly
          default access mode
protected  boolean mDefFileRead
          has the config file been read ?
protected  DriverInfo mDriverInfo
          driver infos (database vendor specific)
protected  java.util.Map mEntities
          map name->entity
protected  java.lang.String mError
          error string for the last error
protected  boolean mOpened
          is the database opened ?
protected  java.lang.String mPassword
          database user's password
protected  PreparedStatementPool mPreparedStatementPool
          pool of prepared statements
protected  Entity mRootEntity
          root entity that contains all root attributes and actions
protected  java.lang.String mSchema
          schema
protected  StatementPool mStatementPool
          pool of statements
protected  java.sql.Connection mTransactionConnection
          database connection used for transactions
protected  java.lang.String mUrl
          database url
protected  java.lang.String mUser
          database user
static int UPPERCASE
           
 
Constructor Summary
protected DBConnection()
          builds a new connection
protected DBConnection(java.lang.String inUser, java.lang.String inPassword, java.lang.String inUrl)
          builds a new connection
protected DBConnection(java.lang.String inUser, java.lang.String inPassword, java.lang.String inUrl, java.lang.String inDriver)
          builds a new connection
protected DBConnection(java.lang.String inUser, java.lang.String inPassword, java.lang.String inUrl, java.lang.String inDriver, java.lang.String inSchema)
          builds a new connection
 
Method Summary
 java.lang.String adaptCase(java.lang.String identifier)
          changes to lowercase or uppercase if needed
protected  void checkConnection()
          check connection
protected  void checkTransactionConnection()
          check transaction connection
 void close()
          close the connection
 java.lang.String deobfuscate(java.lang.Object value)
          de-obfuscate the given value
 void displayStats()
          display statistics about the statements pools
 java.lang.Object evaluate(java.lang.String inQuery)
          evaluate a query to a scalar
 Action getAction(java.lang.String inName)
          get a named action
 Attribute getAttribute(java.lang.String inName)
          get a named attribute
 int getCaseSensivity()
          get database case-sensivity
 java.sql.Connection getConnection()
          get the underlying jdbc connection
 DriverInfo getDriverInfo()
          get database vendor
 Entity getEntity(java.lang.String inName)
          get an existing entity
 Entity getEntityCreate(java.lang.String name)
          get a named entity or creeate it if it doesn't exist
 java.lang.String getError()
          get the error string
static DBConnection getInstance(java.io.InputStream inConfig)
          get a new connection
static DBConnection getInstance(java.lang.String inConfigFilename)
          get an instance to a new connection
static DBConnection getInstance(java.lang.String inUser, java.lang.String inPassword, java.lang.String inUrl, java.lang.String inDriver)
          get an instance to a new connection (meant to have a cache params->connections, not implemented, always return a new connection : similar to the equivalent constructor)
 java.sql.Connection getTransactionConnection()
          get the underlying jdbc connection used for transactions
protected static DriverInfo loadDriver(java.lang.String inUrl, java.lang.String inDriver)
          loads the appropriate driver
 java.lang.String obfuscate(java.lang.Object value)
          obfuscate the given value
protected  void open(java.lang.String inUser, java.lang.String inPassword, java.lang.String inUrl)
          open the connection
protected  void open(java.lang.String inUser, java.lang.String inPassword, java.lang.String inUrl, java.lang.String inDriver)
          open the connection
protected  void open(java.lang.String inUser, java.lang.String inPassword, java.lang.String inUrl, java.lang.String inDriver, java.lang.String inSchema)
          open the connection
private static int parseCaching(java.lang.String caching)
          parse a caching value
 PooledPreparedStatement prepare(java.lang.String inQuery)
          prepare a query
 RowIterator query(java.lang.String inQuery)
          issue a query
 RowIterator query(java.lang.String inQuery, Entity inEntity)
          issue a query, knowing the resulting entity
 void readConfigFile(java.io.InputStream inConfig)
          read configuration from the given input stream
 void readConfigFile(java.lang.String inConfigFile)
          read the given config file
protected  void readMetaData()
          read the meta data from the database : reverse engeenering
protected  void reopen()
          reopen the connection
 void setError(java.lang.String inError)
          set the error string
 PooledPreparedStatement transactionPrepare(java.lang.String inQuery)
          prepare a query which is part of a transaction
 int transactionUpdate(java.lang.String inQuery)
          issue an update query that is part of a transaction
 int update(java.lang.String inQuery)
          issues an update query
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

mUser

protected java.lang.String mUser
database user

mPassword

protected java.lang.String mPassword
database user's password

mUrl

protected java.lang.String mUrl
database url

mSchema

protected java.lang.String mSchema
schema

mConnection

protected java.sql.Connection mConnection
database connection

mTransactionConnection

protected java.sql.Connection mTransactionConnection
database connection used for transactions

mStatementPool

protected StatementPool mStatementPool
pool of statements

mPreparedStatementPool

protected PreparedStatementPool mPreparedStatementPool
pool of prepared statements

mDefaultReadOnly

protected boolean mDefaultReadOnly
default access mode

mDefaultCaching

protected int mDefaultCaching
default caching mode

mOpened

protected boolean mOpened
is the database opened ?

mDefFileRead

protected boolean mDefFileRead
has the config file been read ?

mError

protected java.lang.String mError
error string for the last error

mEntities

protected java.util.Map mEntities
map name->entity

mRootEntity

protected Entity mRootEntity
root entity that contains all root attributes and actions

mDriverInfo

protected DriverInfo mDriverInfo
driver infos (database vendor specific)

mCheckStatement

protected java.sql.PreparedStatement mCheckStatement
statement used to check database connection ("select 1")

mCheckTransactionStatement

protected java.sql.PreparedStatement mCheckTransactionStatement
statement used to check database transaction connection

mCryptograph

private Cryptograph mCryptograph
cryptograph used to encrypt/decrypt database ids

CASE_SENSITIVE

public static final int CASE_SENSITIVE
case-sensitive policy

UPPERCASE

public static final int UPPERCASE

LOWERCASE

public static final int LOWERCASE

mCaseSensivity

protected int mCaseSensivity
case-sensivity
Constructor Detail

DBConnection

protected DBConnection()
                throws java.sql.SQLException
builds a new connection
Throws:
java.sql.SQLException - thrown by the database engine

DBConnection

protected DBConnection(java.lang.String inUser,
                       java.lang.String inPassword,
                       java.lang.String inUrl)
                throws java.sql.SQLException
builds a new connection
Parameters:
inUser - user name
inPassword - password
inUrl - database url
Throws:
java.sql.SQLException - thrown by the database engine

DBConnection

protected DBConnection(java.lang.String inUser,
                       java.lang.String inPassword,
                       java.lang.String inUrl,
                       java.lang.String inDriver)
                throws java.sql.SQLException
builds a new connection
Parameters:
inUser - user name
inPassword - password
inUrl - database url
inDriver - driver java class name
Throws:
java.sql.SQLException - thrown by the database engine

DBConnection

protected DBConnection(java.lang.String inUser,
                       java.lang.String inPassword,
                       java.lang.String inUrl,
                       java.lang.String inDriver,
                       java.lang.String inSchema)
                throws java.sql.SQLException
builds a new connection
Parameters:
inUser - user name
inPassword - password
inUrl - database url
inDriver - driver java class name
inSchema - schema name to use
Throws:
java.sql.SQLException - thrown by the database engine
Method Detail

open

protected void open(java.lang.String inUser,
                    java.lang.String inPassword,
                    java.lang.String inUrl)
             throws java.sql.SQLException
open the connection
Parameters:
inUser - user name
inPassword - password
inUrl - database url
Throws:
java.sql.SQLException - thrown by the database engine

open

protected void open(java.lang.String inUser,
                    java.lang.String inPassword,
                    java.lang.String inUrl,
                    java.lang.String inDriver)
             throws java.sql.SQLException
open the connection
Parameters:
inUser - user name
inPassword - password
inUrl - database url
inDriver - driver java class name
Throws:
java.sql.SQLException - thrown by the database engine

open

protected void open(java.lang.String inUser,
                    java.lang.String inPassword,
                    java.lang.String inUrl,
                    java.lang.String inDriver,
                    java.lang.String inSchema)
             throws java.sql.SQLException
open the connection
Parameters:
inUser - user name
inPassword - password
inUrl - database url
inDriver - driver java class name
inSchema - schema name
Throws:
java.sql.SQLException - thrown by the database engine

loadDriver

protected static DriverInfo loadDriver(java.lang.String inUrl,
                                       java.lang.String inDriver)
loads the appropriate driver
Parameters:
inUrl - database url
inDriver - driver java class name
Returns:
vendor name

reopen

protected void reopen()
               throws java.sql.SQLException
reopen the connection
Throws:
java.sql.SQLException - thrown by the database engine

getInstance

public static DBConnection getInstance(java.lang.String inUser,
                                       java.lang.String inPassword,
                                       java.lang.String inUrl,
                                       java.lang.String inDriver)
                                throws java.sql.SQLException
get an instance to a new connection (meant to have a cache params->connections, not implemented, always return a new connection : similar to the equivalent constructor)
Parameters:
inUser - user name
inPassword - password
inUrl - database url
inDriver - driver java class name
Returns:
a new connection
Throws:
java.sql.SQLException - thrown by the database engine

getInstance

public static DBConnection getInstance(java.lang.String inConfigFilename)
                                throws java.sql.SQLException
get an instance to a new connection
Parameters:
inConfigFilename - config filename
Returns:
a new connection
Throws:
java.sql.SQLException - thrown by the database engine

getInstance

public static DBConnection getInstance(java.io.InputStream inConfig)
                                throws java.sql.SQLException
get a new connection
Parameters:
inConfig - config filename
Returns:
a new connection
Throws:
java.sql.SQLException - thrown by the database engine

query

public RowIterator query(java.lang.String inQuery)
issue a query
Parameters:
inQuery - an SQL query
Returns:
the resulting RowIterator

query

public RowIterator query(java.lang.String inQuery,
                         Entity inEntity)
issue a query, knowing the resulting entity
Parameters:
inQuery - an SQL query
inEntity - the resulting entity
Returns:
return the resulting row iterator

evaluate

public java.lang.Object evaluate(java.lang.String inQuery)
evaluate a query to a scalar
Parameters:
inQuery - an sql query
Returns:
the resulting scalar

prepare

public PooledPreparedStatement prepare(java.lang.String inQuery)
prepare a query
Parameters:
inQuery - an sql query
Returns:
the pooled prepared statement corresponding to the query

transactionPrepare

public PooledPreparedStatement transactionPrepare(java.lang.String inQuery)
prepare a query which is part of a transaction
Parameters:
inQuery - an sql query
Returns:
the prepared statemenet corresponding to the query

update

public int update(java.lang.String inQuery)
issues an update query
Parameters:
inQuery - an sql query
Returns:
the number of affected rows

transactionUpdate

public int transactionUpdate(java.lang.String inQuery)
issue an update query that is part of a transaction
Parameters:
inQuery - an sql query
Returns:
the number of affected rows

close

public void close()
           throws java.sql.SQLException
close the connection
Throws:
java.sql.SQLException - thrown by the database engine

displayStats

public void displayStats()
display statistics about the statements pools

getConnection

public java.sql.Connection getConnection()
get the underlying jdbc connection
Returns:
jdbc connection

getTransactionConnection

public java.sql.Connection getTransactionConnection()
get the underlying jdbc connection used for transactions
Returns:
jdbc connection

readMetaData

protected void readMetaData()
                     throws java.sql.SQLException
read the meta data from the database : reverse engeenering
Throws:
java.sql.SQLException - thrown by the database engine

readConfigFile

public void readConfigFile(java.lang.String inConfigFile)
                    throws java.sql.SQLException
read the given config file
Parameters:
inConfigFile - config file pathname
Throws:
java.sql.SQLException - thrown by the database engine

readConfigFile

public void readConfigFile(java.io.InputStream inConfig)
                    throws java.sql.SQLException
read configuration from the given input stream
Parameters:
inConfig - input stream on the config file
Throws:
java.sql.SQLException - thrown by the database engine

adaptCase

public java.lang.String adaptCase(java.lang.String identifier)
changes to lowercase or uppercase if needed
Parameters:
identifier -  
Returns:
changed identifier

parseCaching

private static int parseCaching(java.lang.String caching)
parse a caching value
Parameters:
caching - string describing the type of caching
Returns:
type of caching

getEntityCreate

public Entity getEntityCreate(java.lang.String name)
get a named entity or creeate it if it doesn't exist
Parameters:
name - name of an entity
Returns:
the named entity

getEntity

public Entity getEntity(java.lang.String inName)
get an existing entity
Parameters:
inName - the name of an entity
Returns:
the named entity

getAttribute

public Attribute getAttribute(java.lang.String inName)
get a named attribute
Parameters:
inName - name of an attribute
Returns:
the named attribute

getAction

public Action getAction(java.lang.String inName)
get a named action
Parameters:
inName - name of an attribute
Returns:
the named attribute

setError

public void setError(java.lang.String inError)
set the error string
Parameters:
inError - error string

getError

public java.lang.String getError()
get the error string
Returns:
error string

checkConnection

protected void checkConnection()
                        throws java.sql.SQLException
check connection
Throws:
java.sql.SQLException - thrown by the database engine

checkTransactionConnection

protected void checkTransactionConnection()
                                   throws java.sql.SQLException
check transaction connection
Throws:
java.sql.SQLException - thrown by the database engine

obfuscate

public java.lang.String obfuscate(java.lang.Object value)
obfuscate the given value
Parameters:
value - value to obfuscate
Returns:
obfuscated value

deobfuscate

public java.lang.String deobfuscate(java.lang.Object value)
de-obfuscate the given value
Parameters:
value - value to de-obfuscate
Returns:
obfuscated value

getDriverInfo

public DriverInfo getDriverInfo()
get database vendor
Returns:
the database vendor

getCaseSensivity

public int getCaseSensivity()
get database case-sensivity
Returns:
case-sensivity


ooOoo