scone.netobjects
Class DBTableAdapter

java.lang.Object
  extended by scone.netobjects.DBTableAdapter

public class DBTableAdapter
extends java.lang.Object

stores the database meta information about a TableRecord.
i.e. it knows how to map a TableRecord obect to and from a database. Database fields are seperated into three cases:


DBTableAdapter knows the name, type and default value of each field.

Author:
Harald Weinreich, Volkert Buchmann

Field Summary
protected  int autoincrementLine
           
static int BLOB
          Column type in database is Blob: quote values
protected  java.sql.Connection con
           
static java.lang.String COPYRIGHT
           
protected static java.lang.String dbURI
           
protected static java.lang.String driverName
           
static int FIELD
          Ordinary field
protected  int fieldCount
          Number of fields per record
protected  java.lang.String[] fieldDefaults
          Array with default values of fields
protected  java.lang.String[] fieldNames
          Array of field names
protected  int[] fieldTypes
          Array of field types: STRING, NUMBER, BLOB
static int GENERATED
          Generated Key: AUTO_INCREMENT
protected  int generatedKey
          The generated key, used if database is not available (con==null || useDB==false)
static int KEY
          Key value field: Use to select record in database for update operation
protected  int keyNumber
           
protected  int[] keyTypes
           
static int NUMBER
          Column type in database is Numerical: do not quote values
protected static java.lang.String password
           
protected static boolean requireDb
           
static int STRING
          Column type in database is String: quote values
protected  java.lang.String tableName
          The name of the database table
protected static boolean useDb
           
protected static java.lang.String userName
           
 
Constructor Summary
DBTableAdapter(java.lang.String tableName)
          creates a new DBTableAdapter object
 
Method Summary
 void addField(java.lang.String fieldName, int fieldType, java.lang.String fieldDefault, int keyType)
          adds a field to the table definition.
 void createInDB(TableRecord set)
          Creates a record in the database and fills it with key values If a generated key exists, it reads the value from the database.
 void dbCheck(TableRecord set)
          tries to read from the database an fills the TableRecord if a set exists.
 void dbCreate(TableRecord set)
          creates a new Set in the database
protected static void dbError()
          this method should be called when an SQLException has been caught.
 void dbInit(TableRecord set)
          tries to find an equivalent set in the database or creates one if it does not exist.
protected static void dontUseDb()
          this method should be called when an SQLException has been caught.
static java.lang.String escape(java.lang.String s)
          Calls escape(StringBuffer s)
static java.lang.String escape(java.lang.StringBuffer s)
          Escape is used to escape characters, that cause problems with mySQL or mm.mysql The following strings are converted: null (0) -> \0 Backspace (8) -> \b New line (10) -> \n CR (13) -> \r Double qoutes " (34) -> \" Single quote ' (39) -> \' Percent % (92) -> \% Underscore _ (95) -> \_ Math brackets { } (123, 124) -> \( \) Pipe | (124) -> \|
protected  boolean fill(TableRecord set, java.sql.ResultSet results)
          tries to read from the ResultSet into the TableRecord.
static java.sql.Connection getConnection()
          returns a new Connection to the database
 java.lang.String getTableName()
          Returns the name of this table
 boolean getValuesFromDb(TableRecord set)
          tries to load the values corresponding to the TableRecord from the database.
 java.lang.String getWhereClause(TableRecord set)
          generates a where clause to find the specified TableRecord in the database.
static void init()
          initializes the DB class by parsing scone/config/db.ini
 void init(TableRecord set)
          initializes a TableRecord object.
protected  FieldValueHashTable initFields(TableRecord set)
          creates a new FieldValueHashTable with the names and defaults of the fields
static java.sql.ResultSet queryDb(java.sql.Connection con, java.lang.String tableName, java.lang.String sqlClause)
          tries to load the values corresponding to the TableRecord from the database.
static int rowCountDb(java.sql.Connection con, java.lang.String tableName, java.lang.String sqlClause)
          Tries to get the number of rows in the database to a given query.
static java.lang.String safeString(java.lang.String unsafe)
          Ensures that a String is not null
static java.lang.String unEscape(java.lang.String s)
           
static java.lang.String unEscape(java.lang.StringBuffer s)
          unEscape Strings read from DB to original values
 void updateDB(TableRecord set)
          writes the values from the TableRecord into the database
static boolean useDb()
          returns true, if the database is used, and false if local memory is used instead
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COPYRIGHT

public static final java.lang.String COPYRIGHT
See Also:
Constant Field Values

STRING

public static final int STRING
Column type in database is String: quote values

See Also:
Constant Field Values

NUMBER

public static final int NUMBER
Column type in database is Numerical: do not quote values

See Also:
Constant Field Values

BLOB

public static final int BLOB
Column type in database is Blob: quote values

See Also:
Constant Field Values

GENERATED

public static final int GENERATED
Generated Key: AUTO_INCREMENT

See Also:
Constant Field Values

KEY

public static final int KEY
Key value field: Use to select record in database for update operation

See Also:
Constant Field Values

FIELD

public static final int FIELD
Ordinary field

See Also:
Constant Field Values

fieldNames

protected java.lang.String[] fieldNames
Array of field names


fieldTypes

protected int[] fieldTypes
Array of field types: STRING, NUMBER, BLOB


fieldDefaults

protected java.lang.String[] fieldDefaults
Array with default values of fields


fieldCount

protected int fieldCount
Number of fields per record


keyTypes

protected int[] keyTypes

keyNumber

protected int keyNumber

autoincrementLine

protected int autoincrementLine

tableName

protected java.lang.String tableName
The name of the database table


con

protected java.sql.Connection con

generatedKey

protected int generatedKey
The generated key, used if database is not available (con==null || useDB==false)


driverName

protected static java.lang.String driverName

dbURI

protected static java.lang.String dbURI

userName

protected static java.lang.String userName

password

protected static java.lang.String password

useDb

protected static boolean useDb

requireDb

protected static boolean requireDb
Constructor Detail

DBTableAdapter

public DBTableAdapter(java.lang.String tableName)
creates a new DBTableAdapter object

Parameters:
tableName - the name of the table
Method Detail

getTableName

public java.lang.String getTableName()
Returns the name of this table

Returns:
table name in Scone database.

addField

public void addField(java.lang.String fieldName,
                     int fieldType,
                     java.lang.String fieldDefault,
                     int keyType)
adds a field to the table definition. This is called by scone.netobjects.Schema

Parameters:
fieldName - the name of the field
fieldType - the type of the field. Legal values are: String, Number
fieldDefault - the default value in String format
keyType - whether the field is part of a key.

initFields

protected FieldValueHashTable initFields(TableRecord set)
creates a new FieldValueHashTable with the names and defaults of the fields

Parameters:
set - the TableRecord
Returns:
the FieldValueHashTable object

init

public void init(TableRecord set)
initializes a TableRecord object.
the fieldValues are created and set to the defaults

Parameters:
set - the TableRecord object

dbInit

public void dbInit(TableRecord set)
tries to find an equivalent set in the database or creates one if it does not exist.
This method will only work correctly if the value of the autoincrement key
or the composed key in the TableRecord object have been set!

Parameters:
set - the TableRecord object

dbCheck

public void dbCheck(TableRecord set)
tries to read from the database an fills the TableRecord if a set exists. Otherwise it sets TableRecord.hasRecordInDB to false.

Parameters:
set - the TableRecord

dbCreate

public void dbCreate(TableRecord set)
creates a new Set in the database

Parameters:
set - the TableRecord

updateDB

public void updateDB(TableRecord set)
writes the values from the TableRecord into the database

Parameters:
set - the TableRecord

getValuesFromDb

public boolean getValuesFromDb(TableRecord set)
tries to load the values corresponding to the TableRecord from the database.

Parameters:
set - the TableRecord
Returns:
true if the method was successfull, false otherwise

queryDb

public static java.sql.ResultSet queryDb(java.sql.Connection con,
                                         java.lang.String tableName,
                                         java.lang.String sqlClause)
tries to load the values corresponding to the TableRecord from the database.

Parameters:
con - A connection to the DB
tableName - The name of the table in the scone database
sqlClause - A where or order by clause for the querey.
Returns:
A Resultset with the found objects

rowCountDb

public static int rowCountDb(java.sql.Connection con,
                             java.lang.String tableName,
                             java.lang.String sqlClause)
Tries to get the number of rows in the database to a given query.

Parameters:
con - A connection to the DB
tableName - The name of the table in the scone database
sqlClause - A where or order by clause for the querey.
Returns:
An integer with the number of found objects. Returns -1 for error.

fill

protected boolean fill(TableRecord set,
                       java.sql.ResultSet results)
tries to read from the ResultSet into the TableRecord.

Parameters:
set - the TableRecord
results - the ResultSet
Returns:
false, if the ResultSet conatined no data, true otherwise

createInDB

public void createInDB(TableRecord set)
Creates a record in the database and fills it with key values If a generated key exists, it reads the value from the database.

Parameters:
set - the TableRecord

getWhereClause

public java.lang.String getWhereClause(TableRecord set)
generates a where clause to find the specified TableRecord in the database.
Will return the autoincrement key, if a value is given, or the composed key otherwise.

Parameters:
set - the TableRecord
Returns:
the where clause corresponding to the TableRecord object

safeString

public static java.lang.String safeString(java.lang.String unsafe)
Ensures that a String is not null

Parameters:
unsafe - the String
Returns:
the same String or an empty String if the argument was null>

escape

public static java.lang.String escape(java.lang.String s)
Calls escape(StringBuffer s)


escape

public static java.lang.String escape(java.lang.StringBuffer s)
Escape is used to escape characters, that cause problems with mySQL or mm.mysql

The following strings are converted:


unEscape

public static java.lang.String unEscape(java.lang.String s)

unEscape

public static java.lang.String unEscape(java.lang.StringBuffer s)
unEscape Strings read from DB to original values


init

public static void init()
initializes the DB class by parsing scone/config/db.ini


getConnection

public static java.sql.Connection getConnection()
returns a new Connection to the database


dontUseDb

protected static void dontUseDb()
this method should be called when an SQLException has been caught. It disables database access and notifies the user.


dbError

protected static void dbError()
this method should be called when an SQLException has been caught. It disables database access and notifies the user.


useDb

public static boolean useDb()
returns true, if the database is used, and false if local memory is used instead

Returns:
whether the databse is used