ch.sahits.codegen.java.input.db
Class DBConnection

java.lang.Object
  extended by ch.sahits.codegen.java.input.db.DBConnection
Direct Known Subclasses:
MySQLConnection, OracleConnection, SQLServerConnection

public abstract class DBConnection
extends java.lang.Object

This abstract class handles the connection to the DB. It queries the DB for the structure of a table. Supported DBs are

Since:
0.9.0
Author:
Andi Hotz, (c) 2007

Field Summary
protected  java.sql.Connection conn
          Connection to the DB
protected  java.lang.String db
          DB on the server
protected  java.lang.String host
          Host name of the server where the DB is hosted
protected  java.lang.String password
          Password for the User
protected  int port
          Port number
protected  java.lang.String product
          What DB are we talking about MySQL, Oracle, MSSQL, PostgSQL, ...
protected  java.lang.String schema
          Schema name.
protected  java.lang.String table
          Table to query
protected  java.lang.String user
          User to query the DB
 
Constructor Summary
DBConnection()
           
 
Method Summary
protected  void addFields2Index(DataBaseTable struct, DataBaseTableIndex index, java.lang.String consteintName)
          Add fields to the index model
protected abstract  java.lang.Class convert2Java(java.lang.String type)
          Convert a database datatype into a Java type
protected abstract  DataBaseTable createDataBaseTable()
          Create the DataBaseTable with product specific data
protected abstract  java.lang.String getConnectionString()
          Retrieve the connection String for the specific database
protected abstract  java.lang.String getConstraintFieldSQL(java.lang.String constraintName)
          Retrieve the SQL that gets you all fields of a constraint
protected  java.util.List<java.lang.String> getConstraintsFields(java.lang.String constraintName)
          Retrieve a list of all of all fields of a constraint
protected abstract  java.lang.String getIndexConstraintSQL()
          Retrieve the SQL that gets you the Index constraints
 int getPort()
           
 java.lang.String getSchema()
           
protected  DataBaseTable getStructure()
          Generates a DBStructure Object for the table.
protected abstract  java.lang.String getTableSQL()
          Retrieve the SQL that gets you the table definition
protected  void init(java.lang.String _product, java.lang.String hostname, java.lang.String uname, java.lang.String pwd, java.lang.String database, java.lang.String tablename, int _port, java.lang.String _schema)
          Initializes the connection data
protected  boolean isAutGenKey(java.lang.String fieldName)
          Check if the field is an auto generated key field
protected abstract  void loadDriver()
          check if the driver is availabel
 void setHost(java.lang.String _host)
           
 void setPassword(java.lang.String _password)
           
 void setPort(int _port)
           
 void setProduct(java.lang.String _product)
           
 void setSchema(java.lang.String _schema)
           
 void setTableName(java.lang.String _table)
           
 void setUserName(java.lang.String _user)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

conn

protected java.sql.Connection conn
Connection to the DB


host

protected java.lang.String host
Host name of the server where the DB is hosted


product

protected java.lang.String product
What DB are we talking about MySQL, Oracle, MSSQL, PostgSQL, ...


user

protected java.lang.String user
User to query the DB


password

protected java.lang.String password
Password for the User


table

protected java.lang.String table
Table to query


db

protected java.lang.String db
DB on the server


port

protected int port
Port number


schema

protected java.lang.String schema
Schema name. Not all databases use the schema concept

Constructor Detail

DBConnection

public DBConnection()
Method Detail

init

protected final void init(java.lang.String _product,
                          java.lang.String hostname,
                          java.lang.String uname,
                          java.lang.String pwd,
                          java.lang.String database,
                          java.lang.String tablename,
                          int _port,
                          java.lang.String _schema)
                   throws java.sql.SQLException,
                          java.lang.ClassNotFoundException
Initializes the connection data

Parameters:
_product - what kind of DB (MySQL, PostgeSQL, Oracle, Derby, MS SQL Server, ...)
hostname - of the db eg. localhost
uname - user name to connect to the DB
pwd - password of the user
database - or schema of the table
tablename - table name
_port - number
_schema - name
Throws:
java.sql.SQLException - Connecting failed probably due to incorrect input
java.lang.ClassNotFoundException - JDBC driver not on classpath

getConnectionString

protected abstract java.lang.String getConnectionString()
Retrieve the connection String for the specific database

Returns:
connection string
Since:
0.9.4

loadDriver

protected abstract void loadDriver()
                            throws java.lang.ClassNotFoundException
check if the driver is availabel

Throws:
java.lang.ClassNotFoundException - JDBC driver not on classpath

setHost

public final void setHost(java.lang.String _host)
Parameters:
_host - the host to set
See Also:
host

setProduct

public final void setProduct(java.lang.String _product)
Parameters:
_product - the product to set
See Also:
product

setUserName

public final void setUserName(java.lang.String _user)
Parameters:
_user - the user to set
See Also:
user

setPassword

public final void setPassword(java.lang.String _password)
Parameters:
_password - the password to set
See Also:
password

setTableName

public final void setTableName(java.lang.String _table)
Parameters:
_table - the table to set
See Also:
table

getStructure

protected DataBaseTable getStructure()
                              throws java.sql.SQLException
Generates a DBStructure Object for the table.

Returns:
DataBaseTable
Throws:
java.sql.SQLException

createDataBaseTable

protected abstract DataBaseTable createDataBaseTable()
Create the DataBaseTable with product specific data

Returns:
new DataBaseTable
Since:
0.9.5

getTableSQL

protected abstract java.lang.String getTableSQL()
Retrieve the SQL that gets you the table definition

Returns:
SQL
Since:
0.9.5

convert2Java

protected abstract java.lang.Class convert2Java(java.lang.String type)
Convert a database datatype into a Java type

Parameters:
type - datatype
Returns:
Java type
Since:
0.9.5

getIndexConstraintSQL

protected abstract java.lang.String getIndexConstraintSQL()
Retrieve the SQL that gets you the Index constraints

Returns:
SQL
Since:
0.9.5

getPort

public final int getPort()
Returns:
the port
Since:
0.9.4

setPort

public final void setPort(int _port)
Parameters:
_port - the port to set
Since:
0.9.4

getSchema

public final java.lang.String getSchema()
Returns:
the schema
Since:
0.9.4

setSchema

public final void setSchema(java.lang.String _schema)
Parameters:
_schema - the schema to set
Since:
0.9.4

addFields2Index

protected final void addFields2Index(DataBaseTable struct,
                                     DataBaseTableIndex index,
                                     java.lang.String consteintName)
                              throws java.sql.SQLException
Add fields to the index model

Parameters:
struct - table model
index - model of the index
consteintName - name of the index
Throws:
java.sql.SQLException
Since:
0.9.5

getConstraintsFields

protected final java.util.List<java.lang.String> getConstraintsFields(java.lang.String constraintName)
                                                               throws java.sql.SQLException
Retrieve a list of all of all fields of a constraint

Parameters:
constraintName - name of the constraint
Returns:
List of the fields
Throws:
java.sql.SQLException - Error while reading KEY_COLUMN_USAGE
Since:
0.9.5

getConstraintFieldSQL

protected abstract java.lang.String getConstraintFieldSQL(java.lang.String constraintName)
Retrieve the SQL that gets you all fields of a constraint

Parameters:
constraintName - name of the constraint
Returns:
SQL
Since:
0.9.5

isAutGenKey

protected final boolean isAutGenKey(java.lang.String fieldName)
Check if the field is an auto generated key field

Parameters:
fieldName - name of the fiedl to be checked
Returns:
true if the field is an auto generated key
Since:
1.2.0