1 package com.quantum.adapters;
3 import com.quantum.Messages;
4 import com.quantum.sql.SQLHelper;
5 import com.quantum.util.StringUtil;
8 * Abstract base class for all the adapter classes. Most functions can be redefined in
9 * the adapters for the different databases. If the functions is not redefined, the base
10 * implementation (usually a direct call to the JDBC driver) is used.
14 public abstract class DatabaseAdapter {
16 private final String type;
18 protected DatabaseAdapter(String type) {
22 public String getDisplayName() {
23 return Messages.getString(DatabaseAdapter.class, getType());
27 * Returns the SQL Query to get a list of the tables for the current user (schema)
29 * @return - A String with the SQL query
31 public String getShowTableQuery(String schema) {
35 * Returns the SQL Query to get a list of the queries for the current user (schema)
37 * @return - A String with the SQL query
39 public String getShowViewQuery(String schema) {
43 * Returns the SQL Query to get a list of the sequences for the current user (schema)
45 * @return - A String with the SQL query
47 public String getShowSequenceQuery(String schema) {
51 /** Returns the SQL Query to access all the columns of a table (SELECT)
54 * @return - A String with the SQL query
56 public String getTableQuery( String table) {
57 return "SELECT * FROM " + filterTableName(table); //$NON-NLS-1$
61 * Gets the SQL query to get the next value of a sequence, (incrementing it).
62 * @param sequence - The name of the sequence
63 * @param owner - The owner (schema) of it
64 * @return - A string with the SQL query
66 public String getNextValue(String sequence, String owner) {
70 * Gets the SQL query to get the actual value (previously used) of a sequence, (the sequence is not altered).
71 * @param sequence - The name of the sequence
72 * @param owner - The owner (schema) of it
73 * @return - A string with the SQL query
75 public String getPrevValue(String sequence, String owner) {
79 * Returns a query to get the comments on the columns of a table.
80 * Should return a query to generate a recordset with one row where
81 * the first and only column is the comment
82 * @param tableName - The full name of the table qualified with schema if applicable
83 * @param columnName - The name of the column
85 public String getCommentsQuery(String tableName, String columnName) {
91 * @return : A query to get an empty ResultSet (null if failed) for that table or view.
92 * Subclassed if needed by the different database adapters
94 public String getEmptySetQuery(String table){
95 return "SELECT * FROM " + filterTableName(table) + " WHERE (1 = 0)"; //$NON-NLS-1$ //$NON-NLS-2$
99 * Quotes a string according to the type of the column
100 * @param string to be quoted
101 * @param type according to java.sql.Types
104 public String quote(String string, int type, String typeString) {
105 if (isTextType(type, typeString)) {
106 return "'" + StringUtil.substituteString(string, "'", "''") + "'";
107 } else if (type == java.sql.Types.DATE || type == java.sql.Types.TIMESTAMP){
108 string = string.trim();
109 //Check if we have to strip the millisecods
110 String sub = string.substring(string.length() - 2, string.length() - 1);
111 if (string.length() > 1 && sub.equals(".")) //$NON-NLS-1$
112 string = string.substring(0, string.length() - 2); // strip the milliseconds
114 return "'" + string + "'"; //$NON-NLS-1$ //$NON-NLS-2$
121 * Indicates whether or not a particular type should be quoted in
122 * SQL statements. Some databases support non-standard types such as
123 * TEXT that aren't normally recognized as quoted types.
125 * @param type according to java.sql.Types
126 * @param typeString if the type is "Other".
129 protected boolean isTextType(int type, String typeString) {
130 return SQLHelper.isText(type);
135 * Makes an SQL insert string with the given table, names of columns and values
138 * @param valuesClause
141 public String getInsert(String tableName, String namesClause, String valuesClause) {
142 String query = "INSERT INTO " + filterTableName(tableName);
143 if (namesClause != "") {
144 query += " (" + namesClause + ")";
145 query += " VALUES " + "(" + valuesClause; //$NON-NLS-1$
146 query += " )"; //$NON-NLS-1$
151 * Changes the name of the table, usually to allow for lowercase situations.
152 * The parent implementation does nothing to the tableName, simply returns it.
156 public String filterTableName(String tableName) {
160 * Returns a query to get the number of registers from a table
164 public String getCountQuery(String tableName) {
165 return "SELECT COUNT(*) FROM " + filterTableName(tableName);
173 public void getUpdate(String tableName, String string2, StringBuffer whereClause, String string3) {
174 // TODO Auto-generated method stub
178 * Returns an SQL UPDATE statement
183 public String getUpdate(String tableName, String setClause, String whereClause) {
184 String query = "UPDATE " + filterTableName(tableName); //$NON-NLS-1$
185 query += " SET " + setClause; //$NON-NLS-1$
186 if (!whereClause.equals("")) query += " WHERE " + whereClause; //$NON-NLS-1$
190 * Returns an SQL DELETE statement
195 public String getDelete(String tableName, String whereClause) {
196 String query = "DELETE FROM " + filterTableName(tableName); //$NON-NLS-1$
197 if (!whereClause.equals("")) {
198 query += " WHERE " + whereClause; //$NON-NLS-1$
203 * @param userid - the userid used to connect to the database
204 * @return the default schema for the database
206 public String getDefaultSchema(String userid) {
210 * @return Returns the type.
212 public String getType() {