jdbc
Class AbstractTable

java.lang.Object
  |
  +--jdbc.AbstractTable
Direct Known Subclasses:
GroepContainerTable, NDSExportTable, NTable, PersonTable, PTreeAttsTable, PTreeMemberTable, PTreeTable

public abstract class AbstractTable
extends java.lang.Object

designed to minimize sql-code in implements


Field Summary
protected  java.lang.String[] colNamesR
          reference to columnnames in implementing class initialized when getColNamesR() is first called
protected  java.util.Vector contents
          contents of table; copy of last call to getAll(); reset to null after every insert request
protected  java.util.Map contentsMap
          contents of table as Map keyString -> selectRec()
protected  boolean createID
          add COUNTER field at create; defaults to false
protected  DataSource dbname
          datasource this belongs to
protected  int defaultFieldLength
          default length for char field
protected  int ix_primaryKey
          0-based column number of primary key column; default -1 (no primary key)
(package private) static java.lang.String nl
           
 
Constructor Summary
protected AbstractTable(DataSource dbname)
          reference to table
 
Method Summary
 boolean contains(java.lang.Object o)
          returns true if object with same key is found in contents
 java.util.Vector get(java.lang.String select)
          return table contents, each record placed in Vector, selectRec() is called to convert ResultSet into Object connection is created with each call
 java.util.Vector getAll()
          return table contents, each record placed in Vector
protected  java.lang.String[] getColNamesR()
           
 int[] getColumnLengths()
          subclass should return lengths of varchar columns
abstract  java.lang.String[] getColumnNames()
          implement in subclass
 java.util.Vector getContents()
           
 java.util.Iterator getContentsIt()
           
 java.util.Map getContentsMap()
           
 DataSource getDataSource()
           
 java.util.Vector getDifference(java.util.Vector items)
           
 java.lang.String getKeyField()
          used in 'where' of update: update .. where 'getKeyField' == 'getKeyString'
 java.lang.String getKeyString(java.lang.Object o)
          (override in subclass) used to perform equal match in contains(); determines behaviour of insertDifference, updateStringFor() etc.
abstract  java.lang.String getName()
          implement in subclass
 boolean hasColumnName(java.lang.String s)
           
 void insertDifference(java.util.Vector list)
           
 void insertInto(java.util.Vector list)
          insert all object in vector call insertRec (which should be implemented in subclass)
protected  void insertRec(java.sql.PreparedStatement insertStmt, java.lang.Object rec)
          insert 1 rec using PreparedStatement, call-back for insertInto( Vector ) has to be implemented if user of the table needs to call public insertInto( Vector ) does nothing by default
 void insertStringRecord(java.lang.String[] vals)
          insert 1 record; object values all string values
protected  java.sql.PreparedStatement prepareInsertInto()
          create appropriate PreparedStatement
protected  void removeAll()
           
protected  java.sql.ResultSet selectAll()
          create connection and return all records in ResultSet
protected  java.lang.Object selectRec(java.sql.ResultSet result)
          assemble 1 Object based on 1 ResultSet item has to be implemented if client needs to use public Vector get( String sqlStatement ) returns null if not overridden in subclass
protected  void setColPrimaryKey(int i)
           
 java.lang.String sqlCreateTable()
          build sql statement create table; all fields VarChar( <defaultFieldLenght> ) for fieldnames ending in 'DATE' a Date field is created for fieldnames ending in '_INT', a (MSJet) Long field is created
 java.lang.String sqlInsert(java.lang.String[] record)
          build sql statement build statementstring to prepare insert for 1 full record: 'INSERT INTO <table> (<field1>, <field2> ...)
 java.lang.String sqlPrepareInsertInto()
          build sql statement prepare statement for 1 record: 'INSERT INTO <tabel> (<veld1>, <veld2>) VALUES ( ?
 java.lang.String sqlSelectAll()
          build sql statement
 java.lang.String sqlUpdateStringVal(java.lang.Object o, java.lang.String field, java.lang.String newVal)
          build sql statement build statementstring to update 1 string val in 1 record 'UPDATE <table> SET (<field1> = WHERE <field0> = 'key';'
 java.lang.String sqlUpdateVal(java.lang.String key, java.lang.String keyVal, java.lang.String field, java.lang.String newVal)
           
static java.lang.String squote(java.lang.String s)
           
 java.lang.String toString()
           
 int updateStringFor(java.lang.Object o, java.lang.String field, java.lang.String val)
          update string value for named field
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

contents

protected java.util.Vector contents
contents of table; copy of last call to getAll(); reset to null after every insert request


contentsMap

protected java.util.Map contentsMap
contents of table as Map keyString -> selectRec()


dbname

protected DataSource dbname
datasource this belongs to


createID

protected boolean createID
add COUNTER field at create; defaults to false


defaultFieldLength

protected int defaultFieldLength
default length for char field


ix_primaryKey

protected int ix_primaryKey
0-based column number of primary key column; default -1 (no primary key)

See Also:
sqlCreateTable()

colNamesR

protected java.lang.String[] colNamesR
reference to columnnames in implementing class initialized when getColNamesR() is first called


nl

static java.lang.String nl
Constructor Detail

AbstractTable

protected AbstractTable(DataSource dbname)
reference to table

Method Detail

setColPrimaryKey

protected void setColPrimaryKey(int i)

getColumnNames

public abstract java.lang.String[] getColumnNames()
implement in subclass

Returns:
String[] column names

getDataSource

public DataSource getDataSource()

getColNamesR

protected java.lang.String[] getColNamesR()

getName

public abstract java.lang.String getName()
implement in subclass

Returns:
String name

getColumnLengths

public int[] getColumnLengths()
subclass should return lengths of varchar columns

Returns:
null

removeAll

protected void removeAll()

insertRec

protected void insertRec(java.sql.PreparedStatement insertStmt,
                         java.lang.Object rec)
                  throws java.sql.SQLException
insert 1 rec using PreparedStatement, call-back for insertInto( Vector ) has to be implemented if user of the table needs to call public insertInto( Vector ) does nothing by default

java.sql.SQLException
See Also:
insertInto(java.util.Vector)

selectRec

protected java.lang.Object selectRec(java.sql.ResultSet result)
                              throws java.sql.SQLException
assemble 1 Object based on 1 ResultSet item has to be implemented if client needs to use public Vector get( String sqlStatement ) returns null if not overridden in subclass

Returns:
Object instance of corresponding domain object
java.sql.SQLException
See Also:
getAll()

sqlSelectAll

public java.lang.String sqlSelectAll()
build sql statement

Returns:
String 'select * from <name>'

sqlCreateTable

public java.lang.String sqlCreateTable()
build sql statement

create table; all fields VarChar( <defaultFieldLenght> )

for fieldnames ending in 'DATE' a Date field is created

for fieldnames ending in '_INT', a (MSJet) Long field is created

Returns:
String sql statement
See Also:
defaultFieldLength

sqlPrepareInsertInto

public java.lang.String sqlPrepareInsertInto()
build sql statement prepare statement for 1 record: 'INSERT INTO <tabel> (<veld1>, <veld2>) VALUES ( ? ? );'

Returns:
String sql statement

sqlInsert

public java.lang.String sqlInsert(java.lang.String[] record)
build sql statement build statementstring to prepare insert for 1 full record:

'INSERT INTO <table> (<field1>, <field2> ...) VALUES ( record[0], record[1] ... );'

Returns:
sql statement

sqlUpdateStringVal

public java.lang.String sqlUpdateStringVal(java.lang.Object o,
                                           java.lang.String field,
                                           java.lang.String newVal)
build sql statement build statementstring to update 1 string val in 1 record

'UPDATE <table> SET (<field1> = WHERE <field0> = 'key';'

Returns:
sql statement
See Also:
getKeyField()

sqlUpdateVal

public java.lang.String sqlUpdateVal(java.lang.String key,
                                     java.lang.String keyVal,
                                     java.lang.String field,
                                     java.lang.String newVal)

squote

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

hasColumnName

public boolean hasColumnName(java.lang.String s)

prepareInsertInto

protected java.sql.PreparedStatement prepareInsertInto()
                                                throws java.lang.Exception
create appropriate PreparedStatement

java.lang.Exception

updateStringFor

public int updateStringFor(java.lang.Object o,
                           java.lang.String field,
                           java.lang.String val)
                    throws java.sql.SQLException
update string value for named field

Returns:
number of records updated
java.sql.SQLException

insertStringRecord

public void insertStringRecord(java.lang.String[] vals)
                        throws java.lang.Exception
insert 1 record; object values all string values

Throws:
java.lang.Exception - "illegal insert" if vals.length != colNames.length
java.sql.SQLException
See Also:
sqlInsert(java.lang.String[]), getColumnNames()

insertInto

public void insertInto(java.util.Vector list)
                throws java.lang.Exception
insert all object in vector call insertRec (which should be implemented in subclass)

java.lang.Exception
See Also:
insertRec(java.sql.PreparedStatement, java.lang.Object)

getKeyString

public java.lang.String getKeyString(java.lang.Object o)
(override in subclass) used to perform equal match in contains(); determines behaviour of insertDifference, updateStringFor() etc.

Returns:
o.toString();

getKeyField

public java.lang.String getKeyField()
used in 'where' of update: update .. where 'getKeyField' == 'getKeyString'

Returns:
String name of column at ix_primaryKey; '_no key field_' if column names not set

insertDifference

public void insertDifference(java.util.Vector list)
                      throws java.lang.Exception
java.lang.Exception

selectAll

protected java.sql.ResultSet selectAll()
                                throws java.lang.Exception
create connection and return all records in ResultSet

Returns:
ResultSet
java.lang.Exception

get

public java.util.Vector get(java.lang.String select)
                     throws java.lang.Exception
return table contents, each record placed in Vector, selectRec() is called to convert ResultSet into Object connection is created with each call

Returns:
Vector v;
java.lang.Exception
See Also:
selectRec(java.sql.ResultSet)

getAll

public java.util.Vector getAll()
                        throws java.lang.Exception
return table contents, each record placed in Vector

Returns:
Vector v with contents or null on fail; xxException discarded
java.lang.Exception

contains

public boolean contains(java.lang.Object o)
returns true if object with same key is found in contents

See Also:
getKeyString(java.lang.Object)

getDifference

public java.util.Vector getDifference(java.util.Vector items)

getContents

public java.util.Vector getContents()
Returns:
Vector containing all records; item-type set in callback selectRec

getContentsIt

public java.util.Iterator getContentsIt()

getContentsMap

public java.util.Map getContentsMap()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns:
'<dbname-name>.<table-name>'