1 package com.quantum.adapters;
 
   3 import com.quantum.sql.SQLHelper;
 
   4 import com.quantum.util.StringUtil;
 
   7  * Abstract base class for all the adapter classes. Most functions can be redefined in
 
   8  * the adapters for the different databases. If the functions is not redefined, the base
 
   9  * implementation (usually a direct call to the JDBC driver) is used.
 
  13 public abstract class DatabaseAdapter {
 
  16          * Returns the SQL Query to get a list of the tables for the current user (schema) 
 
  18          * @return - A String with the SQL query 
 
  20         public String getShowTableQuery(String schema, boolean isDefault) {
 
  24          * Returns the SQL Query to get a list of the queries for the current user (schema) 
 
  26          * @return - A String with the SQL query
 
  28         public String getShowViewQuery(String schema, boolean isDefault) {
 
  32          * Returns the SQL Query to get a list of the sequences for the current user (schema) 
 
  34          * @return - A String with the SQL query
 
  36         public String getShowSequenceQuery(String schema, boolean isDefault) {
 
  40         /** Returns the SQL Query to access all the columns of a table (SELECT)
 
  43          * @return - A String with the SQL query
 
  45         public String getTableQuery( String table) {
 
  46                         return "SELECT * FROM " + filterTableName(table); //$NON-NLS-1$
 
  50          * Gets the SQL query to get the next value of a sequence, (incrementing it).
 
  51          * @param sequence - The name of the sequence
 
  52          * @param owner - The owner (schema) of it
 
  53          * @return - A string with the SQL query
 
  55         public String getNextValue(String sequence, String owner) {
 
  59          * Gets the SQL query to get the actual value (previously used) of a sequence, (the sequence is not altered).
 
  60          * @param sequence - The name of the sequence
 
  61          * @param owner - The owner (schema) of it
 
  62          * @return - A string with the SQL query
 
  64         public String getPrevValue(String sequence, String owner) {
 
  68          * Returns a query to get the comments on the columns of a table. 
 
  69          * Should return a query to generate a recordset with one row where
 
  70          * the first and only column is the comment
 
  71          * @param tableName - The full name of the table qualified with schema if applicable
 
  72          * @param columnName - The name of the column
 
  74         public String getCommentsQuery(String tableName, String columnName) {
 
  80          * @return : A query to get an empty ResultSet (null if failed) for that table or view.
 
  81          * Subclassed if needed by the different database adapters
 
  83         public String getEmptySetQuery(String table){
 
  84                 return "SELECT * FROM " + filterTableName(table) + " WHERE (1 = 0)"; //$NON-NLS-1$ //$NON-NLS-2$
 
  88          * Quotes a string according to the type of the column 
 
  89          * @param string to be quoted
 
  90          * @param type according to java.sql.Types
 
  93         public String quote(String string, int type, String typeString) {
 
  94                 if (isTextType(type, typeString)) {
 
  95                         return "'" + StringUtil.substituteString(string, "'", "''") + "'";
 
  96                 } else if (type == java.sql.Types.DATE || type == java.sql.Types.TIMESTAMP){            
 
  97                         string = string.trim();
 
  98                         //Check if we have to strip the millisecods
 
  99                         String sub = string.substring(string.length() - 2, string.length() - 1);
 
 100                         if (string.length() > 1 && sub.equals(".")) //$NON-NLS-1$
 
 101                                 string = string.substring(0, string.length() - 2); // strip the milliseconds
 
 103                         return "'" + string + "'"; //$NON-NLS-1$ //$NON-NLS-2$
 
 110      * Indicates whether or not a particular type should be quoted in 
 
 111      * SQL statements.  Some databases support non-standard types such as
 
 112      * TEXT that aren't normally recognized as quoted types.
 
 114      * @param type according to java.sql.Types
 
 115      * @param typeString if the type is "Other".
 
 118     protected boolean isTextType(int type, String typeString) {
 
 119         return SQLHelper.isText(type);
 
 124          * Makes an SQL insert string with the given table, names of columns and values
 
 127          * @param valuesClause
 
 130         public String getInsert(String tableName, String namesClause, String valuesClause) {
 
 131                 String query = "INSERT INTO " + filterTableName(tableName);
 
 132                 if (namesClause != "") {
 
 133                                         query += " (" + namesClause + ")";
 
 134                                         query += " VALUES " + "(" + valuesClause; //$NON-NLS-1$
 
 135                                         query += " )"; //$NON-NLS-1$
 
 140          * Changes the name of the table, usually to allow for lowercase situations.
 
 141          * The parent implementation does nothing to the tableName, simply returns it.
 
 145         public String filterTableName(String tableName) {
 
 149          * Returns a query to get the number of registers from a table
 
 153         public String getCountQuery(String tableName) {
 
 154                 return "SELECT COUNT(*) FROM " + filterTableName(tableName);    
 
 162         public void getUpdate(String tableName, String string2, StringBuffer whereClause, String string3) {
 
 163                 // TODO Auto-generated method stub
 
 167          * Returns an SQL UPDATE statement
 
 172         public String getUpdate(String tableName, String setClause, String whereClause) {
 
 173                 String query = "UPDATE " + filterTableName(tableName); //$NON-NLS-1$
 
 174                 query += " SET " + setClause; //$NON-NLS-1$
 
 175                 if (!whereClause.equals("")) query += " WHERE " + whereClause; //$NON-NLS-1$
 
 179          * Returns an SQL DELETE statement
 
 184         public String getDelete(String tableName, String whereClause) {
 
 185                 String query = "DELETE FROM " + filterTableName(tableName); //$NON-NLS-1$
 
 186                 if (!whereClause.equals("")) {
 
 187                         query += " WHERE " + whereClause; //$NON-NLS-1$
 
 192      * @param userid - the userid used to connect to the database
 
 193      * @return the default schema for the database
 
 195     public String getDefaultSchema(String userid) {