velosurf.sql
Class Database

java.lang.Object
  extended by velosurf.sql.Database

public class Database
extends java.lang.Object

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

To get a new instance, client classes should call one of the getInstance static methods.

Author:
Claude Brisson

Field Summary
private  int caching
          Default caching mode.
static int CASE_SENSITIVE
          'sensitive' case-sensitive policy.
static int CASE_UNKNOWN
          'unknown' case-sensitive policy.
private  int caseSensivity
          Case-sensivity.
private  boolean checkConnections
          Default connections checking
private  long checkInterval
          Default check interval
private  ConnectionPool connectionPool
          Pool of connections.
private static java.util.Map<java.lang.Integer,Database> connectionsByConfigFile
          Map config files -> instances.
private static java.util.Map<java.lang.Integer,Database> connectionsByParams
          Map parameters -> instances.
private static int contextCase
          Case-sensivity for context.
private  Cryptograph cryptograph
          Cryptograph used to encrypt/decrypt database ids.
private  java.lang.String driverClass
          Driver class name, if provided in the config file.
private  DriverInfo driverInfo
          Driver infos (database vendor specific).
private  boolean driverLoaded
          Whether the JDBC driver has been loaded.
private  java.util.Map<java.lang.String,Entity> entities
          Map name->entity.
static int LOWERCASE
          'lowercase' case-sensitive policy.
private  int maxConnections
          Max connections.
private  int minConnections
          Min connections.
private  java.lang.String password
          Database user's password.
private  PreparedStatementPool preparedStatementPool
          Pool of prepared statements.
private  boolean readOnly
          Default access mode.
private  ReverseEngineer reverseEngineer
          Reverse engineer.
private  Entity rootEntity
          Root entity that contains all root attributes and actions.
private  java.lang.String schema
          Schema.
private  java.lang.String seed
          Random seed used to initialize the cryptograph.
private static java.util.Map<java.lang.Integer,java.util.Map<java.lang.String,Entity>> sharedCatalog
          Shared catalog, to share entities among instances.
private  StatementPool statementPool
          Pool of statements.
private  ConnectionPool transactionConnectionPool
          Pool of connections for transactions.
private  PreparedStatementPool transactionPreparedStatementPool
          Pool of prepared statements for transactions.
private  StatementPool transactionStatementPool
          Pool of statements for transactions.
static int UPPERCASE
          'uppercase' case-sensitive policy.
private  java.lang.String url
          Database url.
private  java.lang.String user
          Database user.
private  java.lang.ThreadLocal<UserContext> userContext
          Thread-local user context.
 
Constructor Summary
private Database()
          Builds a new connection.
private Database(java.lang.String user, java.lang.String password, java.lang.String url, java.lang.String driver, java.lang.String schema)
          Builds a new connection.
 
Method Summary
 java.lang.String adaptCase(java.lang.String identifier)
          Changes to lowercase or uppercase if needed.
static java.lang.String adaptContextCase(java.lang.String str)
          Adapt a string to the context case.
 void addEntity(Entity entity)
          Add a new entity.
 void close()
          Close the connection.
private  void connect()
          Connect the database.
 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 query)
          Evaluate a query to a scalar.
 java.lang.Object filterParam(java.lang.Object value)
          Driver specific value filter
 Action getAction(java.lang.String name)
          Get a root action.
 Attribute getAttribute(java.lang.String name)
          Get a root attribute.
 int getCaseSensivity()
          Get database case-sensivity policy.
 ConnectionWrapper getConnection()
          Get a jdbc connection.
 DriverInfo getDriverInfo()
          Get database driver infos.
 java.util.Map<java.lang.String,Entity> getEntities()
          Entities map getter.
 Entity getEntity(java.lang.String name)
          Get an existing entity.
 Entity getEntityCreate(java.lang.String name)
          Get a named entity or creeate it if it doesn't exist
static Database getInstance(java.io.InputStream config)
          Get a new connection.
static Database getInstance(java.io.InputStream config, XIncludeResolver xincludeResolver)
          Get a new connection.
static Database getInstance(java.lang.String configFilename)
          Get a unique Database from config filename.
static Database getInstance(java.lang.String user, java.lang.String password, java.lang.String url)
          Get a unique Database from connection params.
static Database getInstance(java.lang.String user, java.lang.String password, java.lang.String url, java.lang.String driver)
          Get a unique Database from connection params.
static Database getInstance(java.lang.String user, java.lang.String password, java.lang.String url, java.lang.String driver, java.lang.String schema)
          Get a unique Database from connection params.
private  java.lang.Integer getMagicNumber(java.lang.String schema)
          Get the integer key used to share schema entities among instances.
 ReverseEngineer getReverseEngineer()
          Get reverse engineer.
 Entity getRootEntity()
          Get root entity.
 java.lang.String getSchema()
          Get the schema.
 ConnectionWrapper getTransactionConnection()
          Get the underlying jdbc connection used for transactions, and mark it right away as busy.
 UserContext getUserContext()
          Get this database user context (thread local)
protected  void initCryptograph()
          Init cryptograph.
protected  void loadDriver()
          oad the appropriate driver.
 java.lang.String obfuscate(java.lang.Object value)
          Obfuscate the given value.
private  void open(java.lang.String user, java.lang.String password, java.lang.String url, java.lang.String driver, java.lang.String schema)
          Open the connection.
 PooledPreparedStatement prepare(java.lang.String query)
          Prepare a query.
 RowIterator query(java.lang.String query)
          Issue a query.
 RowIterator query(java.lang.String query, Entity entity)
          Issue a query, knowing the resulting entity.
private  void readConfigFile(java.io.InputStream config, XIncludeResolver xincludeResolver)
          Read model configuration from the given input stream.
 void setCaching(int cachingMethod)
          Set the caching method.
 void setCase(int caseSensivity)
          Set the case policy.
 void setCheckConnections(boolean checkConnections)
          Set whether or not connections are to be checked or not
 void setCheckInterval(long checkInterval)
          Set the minimum check interval (in milliseconds) - connections are only checked when their last use is older than this interval.
 void setDriver(java.lang.String driverClass)
          Set driver class.
 void setError(java.lang.String errormsg)
          Set error message.
 void setMaxConnections(int maxConnections)
          Set the maximum number of connections.
 void setMinConnections(int minConnections)
          Set minimum number of connections.
 void setPassword(java.lang.String password)
          Set the database password.
 void setReadOnly(boolean readOnly)
          Set the read-only state.
 void setSchema(java.lang.String schema)
          Set schema name.
 void setSeed(java.lang.String seed)
          Set the encryption seed.
 void setURL(java.lang.String url)
          Set the database URL.
 void setUser(java.lang.String user)
          Set the database user.
 void setUserContext(UserContext userContext)
          Set this database user context (thread local)
 PooledPreparedStatement transactionPrepare(java.lang.String query)
          Prepare a query which is part of a transaction.
 int transactionUpdate(java.lang.String query)
          Issue an update query that is part of a transaction.
 int update(java.lang.String query)
          Issue an update query.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

user

private java.lang.String user
Database user.


password

private java.lang.String password
Database user's password.


url

private java.lang.String url
Database url.


schema

private java.lang.String schema
Schema.


driverLoaded

private boolean driverLoaded
Whether the JDBC driver has been loaded.


driverClass

private java.lang.String driverClass
Driver class name, if provided in the config file.


connectionPool

private ConnectionPool connectionPool
Pool of connections.


minConnections

private int minConnections
Min connections.


maxConnections

private int maxConnections
Max connections.


transactionConnectionPool

private ConnectionPool transactionConnectionPool
Pool of connections for transactions.


statementPool

private StatementPool statementPool
Pool of statements.


transactionStatementPool

private StatementPool transactionStatementPool
Pool of statements for transactions.


preparedStatementPool

private PreparedStatementPool preparedStatementPool
Pool of prepared statements.


transactionPreparedStatementPool

private PreparedStatementPool transactionPreparedStatementPool
Pool of prepared statements for transactions.


readOnly

private boolean readOnly
Default access mode.


checkConnections

private boolean checkConnections
Default connections checking


checkInterval

private long checkInterval
Default check interval


caching

private int caching
Default caching mode.


entities

private java.util.Map<java.lang.String,Entity> entities
Map name->entity.


rootEntity

private Entity rootEntity
Root entity that contains all root attributes and actions.


driverInfo

private DriverInfo driverInfo
Driver infos (database vendor specific).


seed

private java.lang.String seed
Random seed used to initialize the cryptograph.


cryptograph

private Cryptograph cryptograph
Cryptograph used to encrypt/decrypt database ids.


CASE_UNKNOWN

public static final int CASE_UNKNOWN
'unknown' case-sensitive policy.

See Also:
Constant Field Values

CASE_SENSITIVE

public static final int CASE_SENSITIVE
'sensitive' case-sensitive policy.

See Also:
Constant Field Values

UPPERCASE

public static final int UPPERCASE
'uppercase' case-sensitive policy.

See Also:
Constant Field Values

LOWERCASE

public static final int LOWERCASE
'lowercase' case-sensitive policy.

See Also:
Constant Field Values

caseSensivity

private int caseSensivity
Case-sensivity.


contextCase

private static int contextCase
Case-sensivity for context.


connectionsByParams

private static java.util.Map<java.lang.Integer,Database> connectionsByParams
Map parameters -> instances.


connectionsByConfigFile

private static java.util.Map<java.lang.Integer,Database> connectionsByConfigFile
Map config files -> instances.


sharedCatalog

private static java.util.Map<java.lang.Integer,java.util.Map<java.lang.String,Entity>> sharedCatalog

Shared catalog, to share entities among instances.

Key is hashcode of (name+password+url+schema), value is an entities map.


reverseEngineer

private ReverseEngineer reverseEngineer
Reverse engineer.


userContext

private java.lang.ThreadLocal<UserContext> userContext
Thread-local user context.

Constructor Detail

Database

private Database()
Builds a new connection.


Database

private Database(java.lang.String user,
                 java.lang.String password,
                 java.lang.String url,
                 java.lang.String driver,
                 java.lang.String schema)
          throws java.sql.SQLException
Builds a new connection.

Parameters:
user - user name
password - password
url - database url
driver - driver java class name
schema - schema name to use
Throws:
java.sql.SQLException - thrown by the database engine
Method Detail

getInstance

public static Database getInstance(java.lang.String user,
                                   java.lang.String password,
                                   java.lang.String url)
                            throws java.sql.SQLException
Get a unique Database from connection params.

Parameters:
user - user name
password - password
url - database url
Returns:
a new connection
Throws:
java.sql.SQLException - thrown by the database engine

getInstance

public static Database getInstance(java.lang.String user,
                                   java.lang.String password,
                                   java.lang.String url,
                                   java.lang.String driver)
                            throws java.sql.SQLException
Get a unique Database from connection params.

Parameters:
user - user name
password - password
url - database url
driver - driver java class name
Returns:
a new connection
Throws:
java.sql.SQLException - thrown by the database engine

getInstance

public static Database getInstance(java.lang.String user,
                                   java.lang.String password,
                                   java.lang.String url,
                                   java.lang.String driver,
                                   java.lang.String schema)
                            throws java.sql.SQLException
Get a unique Database from connection params.

Parameters:
user - user name
password - password
url - database url
driver - driver java class name
schema - schema
Returns:
a new connection
Throws:
java.sql.SQLException - thrown by the database engine

getInstance

public static Database getInstance(java.lang.String configFilename)
                            throws java.sql.SQLException,
                                   java.io.FileNotFoundException,
                                   java.io.IOException
Get a unique Database from config filename.

Parameters:
configFilename - config filename
Returns:
a new connection
Throws:
java.sql.SQLException - thrown by the database engine
java.io.FileNotFoundException
java.io.IOException

getInstance

public static Database getInstance(java.io.InputStream config)
                            throws java.sql.SQLException
Get a new connection.

Parameters:
config - config filename
Returns:
a new connection
Throws:
java.sql.SQLException - thrown by the database engine

getInstance

public static Database getInstance(java.io.InputStream config,
                                   XIncludeResolver xincludeResolver)
                            throws java.sql.SQLException
Get a new connection.

Parameters:
config - config filename
Returns:
a new connection
Throws:
java.sql.SQLException - thrown by the database engine

open

private void open(java.lang.String user,
                  java.lang.String password,
                  java.lang.String url,
                  java.lang.String driver,
                  java.lang.String schema)
           throws java.sql.SQLException
Open the connection.

Parameters:
user - user name
password - password
url - database url
driver - driver java class name
schema - schema name
Throws:
java.sql.SQLException - thrown by the database engine

connect

private void connect()
              throws java.sql.SQLException
Connect the database.

Throws:
java.sql.SQLException

setReadOnly

public void setReadOnly(boolean readOnly)
Set the read-only state.

Parameters:
readOnly - read-only state

setCaching

public void setCaching(int cachingMethod)
Set the caching method.

Parameters:
cachingMethod - caching method

setUser

public void setUser(java.lang.String user)
Set the database user.

Parameters:
user - user name.

setPassword

public void setPassword(java.lang.String password)
Set the database password.

Parameters:
password - password

setURL

public void setURL(java.lang.String url)
Set the database URL.

Parameters:
url - database url

setDriver

public void setDriver(java.lang.String driverClass)
Set driver class.

Parameters:
driverClass - driver class

setSchema

public void setSchema(java.lang.String schema)
Set schema name.

Parameters:
schema - schema name

setMinConnections

public void setMinConnections(int minConnections)
Set minimum number of connections.

Parameters:
minConnections - minimum number of connections

setMaxConnections

public void setMaxConnections(int maxConnections)
Set the maximum number of connections.

Parameters:
maxConnections - maximum number of connections

setSeed

public void setSeed(java.lang.String seed)
Set the encryption seed.

Parameters:
seed - encryption seed

setCase

public void setCase(int caseSensivity)
Set the case policy. Possible values are CASE_SENSITIVE, CASE_LOWERCASE and CASE_UPPERCASE.

Parameters:
caseSensivity - case policy

setCheckConnections

public void setCheckConnections(boolean checkConnections)
Set whether or not connections are to be checked or not

Parameters:
checkConnections - read-only check or not

setCheckInterval

public void setCheckInterval(long checkInterval)
Set the minimum check interval (in milliseconds) - connections are only checked when their last use is older than this interval.

Parameters:
checkInterval -

loadDriver

protected void loadDriver()
oad the appropriate driver.


initCryptograph

protected void initCryptograph()
Init cryptograph.


getReverseEngineer

public ReverseEngineer getReverseEngineer()
Get reverse engineer.

Returns:
reverse engineer.

query

public RowIterator query(java.lang.String query)
                  throws java.sql.SQLException
Issue a query.

Parameters:
query - an SQL query
Returns:
the resulting RowIterator
Throws:
java.sql.SQLException

query

public RowIterator query(java.lang.String query,
                         Entity entity)
                  throws java.sql.SQLException
Issue a query, knowing the resulting entity.

Parameters:
query - an SQL query
entity - the resulting entity
Returns:
return the resulting row iterator
Throws:
java.sql.SQLException

evaluate

public java.lang.Object evaluate(java.lang.String query)
Evaluate a query to a scalar.

Parameters:
query - an sql query
Returns:
the resulting scalar

prepare

public PooledPreparedStatement prepare(java.lang.String query)
Prepare a query.

Parameters:
query - an sql query
Returns:
the pooled prepared statement corresponding to the query

transactionPrepare

public PooledPreparedStatement transactionPrepare(java.lang.String query)
Prepare a query which is part of a transaction.

Parameters:
query - an sql query
Returns:
the prepared statemenet corresponding to the query

update

public int update(java.lang.String query)
Issue an update query.

Parameters:
query - an sql query
Returns:
the number of affected rows

transactionUpdate

public int transactionUpdate(java.lang.String query)
Issue an update query that is part of a transaction.

Parameters:
query - 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 ConnectionWrapper getConnection()
                                throws java.sql.SQLException
Get a jdbc connection.

Returns:
a jdbc connection wrapper (which extends java.sql.Connection)
Throws:
java.sql.SQLException

getTransactionConnection

public ConnectionWrapper getTransactionConnection()
                                           throws java.sql.SQLException
Get the underlying jdbc connection used for transactions, and mark it right away as busy.

Returns:
a jdbc connection wrapper (which extends java.sql.Connection)
Throws:
java.sql.SQLException

readConfigFile

private void readConfigFile(java.io.InputStream config,
                            XIncludeResolver xincludeResolver)
Read model configuration from the given input stream.

Parameters:
config - input stream on the config file

adaptCase

public java.lang.String adaptCase(java.lang.String identifier)
Changes to lowercase or uppercase if needed.

Parameters:
identifier -
Returns:
changed identifier

addEntity

public void addEntity(Entity entity)
Add a new entity.

Parameters:
entity - entity to add

getRootEntity

public Entity getRootEntity()
Get root entity.

Returns:
root entity

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 name)
Get an existing entity.

Parameters:
name - the name of an entity
Returns:
the named entity

getEntities

public java.util.Map<java.lang.String,Entity> getEntities()
Entities map getter.

Returns:
entities map

getAttribute

public Attribute getAttribute(java.lang.String name)
Get a root attribute.

Parameters:
name - name of an attribute
Returns:
the named attribute

getAction

public Action getAction(java.lang.String name)
Get a root action.

Parameters:
name - name of an attribute
Returns:
the named attribute

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 driver infos.

Returns:
the database vendor

getCaseSensivity

public int getCaseSensivity()
Get database case-sensivity policy.

Returns:
case-sensivity

getMagicNumber

private java.lang.Integer getMagicNumber(java.lang.String schema)
Get the integer key used to share schema entities among instances.


getSchema

public java.lang.String getSchema()
Get the schema.

Returns:
the schema

adaptContextCase

public static java.lang.String adaptContextCase(java.lang.String str)
Adapt a string to the context case.

Parameters:
str - string to adapt
Returns:
adapted string

setUserContext

public void setUserContext(UserContext userContext)
Set this database user context (thread local)

Parameters:
userContext - user context

getUserContext

public UserContext getUserContext()
Get this database user context (thread local)

Returns:
the thread local user context

setError

public void setError(java.lang.String errormsg)
Set error message.


filterParam

public java.lang.Object filterParam(java.lang.Object value)
Driver specific value filter

Parameters:
value - value to be filtered
Returns:
filtered value


~ooOoo~