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
static int CASE_SENSITIVE
          case-sensitive policy
static int LOWERCASE
           
protected  int mCaseSensivity
          case-sensivity
protected  ConnectionPool mConnectionPool
          Pool of connections
private  Cryptograph mCryptograph
          cryptograph used to encrypt/decrypt database ids
protected  int mDefaultCaching
          default caching mode
protected  boolean mDefaultReadOnly
          default access mode
protected  DriverInfo mDriverInfo
          driver infos (database vendor specific)
protected  java.util.Map mEntities
          map name->entity
protected  java.util.Map mEntitiesByTableName
           
protected  java.lang.String mError
          error string for the last error
protected  int mMaxConnections
           
protected  int mMinConnections
           
protected  java.lang.String mPassword
          database user's password
protected  PreparedStatementPool mPreparedStatementPool
          pool of prepared statements
protected  int mReverseMode
           
protected  Entity mRootEntity
          root entity that contains all root attributes and actions
protected  java.lang.String mSchema
          schema
private  java.lang.String mSeed
          random seed used to initialize the cryptograph
protected  StatementPool mStatementPool
          pool of statements
protected  ConnectionPool mTransactionConnectionPool
          Pool of connections for transactions
protected  PreparedStatementPool mTransactionPreparedStatementPool
          pool of prepared statements for transactions
protected  StatementPool mTransactionStatementPool
          pool of statements for transactions
protected  java.lang.String mUrl
          database url
protected  java.lang.String mUser
          database user
static int REVERSE_FULL
           
static int REVERSE_NONE
          reverse-enginering modes
static int REVERSE_PARTIAL
           
private static java.util.Map sConnectionsByConfigFile
          map config files -> instances
private static java.util.Map sConnectionsByParams
          map parameters -> instances
private static java.util.Map sSharedCatalog
          Shared catalog, to share entities among instances.
static int UPPERCASE
           
 
Constructor Summary
protected Database()
          builds a new connection
protected Database(java.lang.String inUser, java.lang.String inPassword, java.lang.String inUrl)
          builds a new connection
protected Database(java.lang.String inUser, java.lang.String inPassword, java.lang.String inUrl, java.lang.String inDriver)
          builds a new connection
protected Database(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  boolean checkSyntax(java.lang.String inParamName, java.lang.String inParamValue, java.lang.String[] inPossibleValues)
          check the syntax of a parameter in the config file
 void close()
          close the connection
protected  void connect()
           
 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 a 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 Database getInstance(java.io.InputStream inConfig)
          get a new connection
static Database getInstance(java.lang.String inConfigFilename)
          get a unique Database from config filename
static Database getInstance(java.lang.String inUser, java.lang.String inPassword, java.lang.String inUrl)
          get a unique Database from connection params
static Database getInstance(java.lang.String inUser, java.lang.String inPassword, java.lang.String inUrl, java.lang.String inDriver)
          get a unique Database from connection params
static Database getInstance(java.lang.String inUser, java.lang.String inPassword, java.lang.String inUrl, java.lang.String inDriver, java.lang.String inSchema)
          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
 java.lang.String getSchema()
          get the schema
 java.sql.Connection getTransactionConnection()
          get the underlying jdbc connection used for transactions
protected  void initCryptograph()
           
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, 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 readTableMetaData(java.sql.DatabaseMetaData meta, Entity entity, java.lang.String tableName)
           
 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, 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


mConnectionPool

protected ConnectionPool mConnectionPool
Pool of connections


mMinConnections

protected int mMinConnections

mMaxConnections

protected int mMaxConnections

mTransactionConnectionPool

protected ConnectionPool mTransactionConnectionPool
Pool of connections for transactions


mStatementPool

protected StatementPool mStatementPool
pool of statements


mTransactionStatementPool

protected StatementPool mTransactionStatementPool
pool of statements for transactions


mPreparedStatementPool

protected PreparedStatementPool mPreparedStatementPool
pool of prepared statements


mTransactionPreparedStatementPool

protected PreparedStatementPool mTransactionPreparedStatementPool
pool of prepared statements for transactions


mDefaultReadOnly

protected boolean mDefaultReadOnly
default access mode


mDefaultCaching

protected int mDefaultCaching
default caching mode


mError

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


mEntities

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


mEntitiesByTableName

protected java.util.Map mEntitiesByTableName

mRootEntity

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


mDriverInfo

protected DriverInfo mDriverInfo
driver infos (database vendor specific)


mSeed

private java.lang.String mSeed
random seed used to initialize the cryptograph


mCryptograph

private Cryptograph mCryptograph
cryptograph used to encrypt/decrypt database ids


CASE_SENSITIVE

public static final int CASE_SENSITIVE
case-sensitive policy

See Also:
Constant Field Values

UPPERCASE

public static final int UPPERCASE
See Also:
Constant Field Values

LOWERCASE

public static final int LOWERCASE
See Also:
Constant Field Values

mCaseSensivity

protected int mCaseSensivity
case-sensivity


REVERSE_NONE

public static final int REVERSE_NONE
reverse-enginering modes

See Also:
Constant Field Values

REVERSE_PARTIAL

public static final int REVERSE_PARTIAL
See Also:
Constant Field Values

REVERSE_FULL

public static final int REVERSE_FULL
See Also:
Constant Field Values

mReverseMode

protected int mReverseMode

sConnectionsByParams

private static java.util.Map sConnectionsByParams
map parameters -> instances


sConnectionsByConfigFile

private static java.util.Map sConnectionsByConfigFile
map config files -> instances


sSharedCatalog

private static java.util.Map sSharedCatalog
Shared catalog, to share entities among instances.
Key is hashcode of (name+password+url+schema), value is an entities map.

Constructor Detail

Database

protected Database()
            throws java.sql.SQLException
builds a new connection

Throws:
java.sql.SQLException - thrown by the database engine

Database

protected Database(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 ² * @exception SQLException thrown by the database engine
Throws:
java.sql.SQLException

Database

protected Database(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

Database

protected Database(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,
                    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

connect

protected void connect()
                throws java.sql.SQLException
Throws:
java.sql.SQLException

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

initCryptograph

protected void initCryptograph()

getInstance

public static Database getInstance(java.lang.String inUser,
                                   java.lang.String inPassword,
                                   java.lang.String inUrl)
                            throws java.sql.SQLException
get a unique Database from connection params

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

getInstance

public static Database getInstance(java.lang.String inUser,
                                   java.lang.String inPassword,
                                   java.lang.String inUrl,
                                   java.lang.String inDriver)
                            throws java.sql.SQLException
get a unique Database from connection params

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 Database getInstance(java.lang.String inUser,
                                   java.lang.String inPassword,
                                   java.lang.String inUrl,
                                   java.lang.String inDriver,
                                   java.lang.String inSchema)
                            throws java.sql.SQLException
get a unique Database from connection params

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

getInstance

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

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

getInstance

public static Database 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()
                                  throws java.sql.SQLException
get a jdbc connection

Returns:
jdbc connection
Throws:
java.sql.SQLException

getTransactionConnection

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

Returns:
jdbc connection
Throws:
java.sql.SQLException

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

readTableMetaData

protected void readTableMetaData(java.sql.DatabaseMetaData meta,
                                 Entity entity,
                                 java.lang.String tableName)
                          throws java.sql.SQLException
Throws:
java.sql.SQLException

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

checkSyntax

protected boolean checkSyntax(java.lang.String inParamName,
                              java.lang.String inParamValue,
                              java.lang.String[] inPossibleValues)
check the syntax of a parameter in the config file

Parameters:
inParamName - name of the parameter
inParamValue - value of the parameter
inPossibleValues - possible values for the parameter
Returns:
whether the syntax is correct

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

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

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


~ooOoo~