1 package com.quantum.adapters;
3 import java.util.HashMap;
6 import com.quantum.Messages;
7 import com.quantum.util.StringUtil;
8 import com.quantum.util.sql.TypesHelper;
11 * Abstract base class for all the adapter classes. Most functions can be redefined in
12 * the adapters for the different databases. If the functions is not redefined, the base
13 * implementation (usually a direct call to the JDBC driver) is used.
17 public abstract class DatabaseAdapter {
19 private final String type;
21 protected DatabaseAdapter(String type) {
25 public String getDisplayName() {
26 return Messages.getString(DatabaseAdapter.class, getType());
30 * Returns the SQL Query to get a list of the tables for the current user (schema)
32 * @return - A String with the SQL query
34 public String getShowTableQuery(String schema) {
38 * Returns the SQL Query to get a list of the queries for the current user (schema)
40 * @return - A String with the SQL query
42 public String getShowViewQuery(String schema) {
46 * Returns the SQL Query to get a list of the sequences for the current user (schema)
48 * @return - A String with the SQL query
50 public String getShowSequenceQuery(String schema) {
54 /** Returns the SQL Query to access all the columns of a table (SELECT)
57 * @return - A String with the SQL query
59 public String getTableQuery( String table) {
60 return "SELECT * FROM " + filterTableName(table); //$NON-NLS-1$
64 * Gets the SQL query to get the next value of a sequence, (incrementing it).
65 * @param sequence - The name of the sequence
66 * @param owner - The owner (schema) of it
67 * @return - A string with the SQL query
69 public String getNextValue(String sequence, String owner) {
73 * Gets the SQL query to get the actual value (previously used) of a sequence, (the sequence is not altered).
74 * @param sequence - The name of the sequence
75 * @param owner - The owner (schema) of it
76 * @return - A string with the SQL query
78 public String getPrevValue(String sequence, String owner) {
82 * Returns a query to get the comments on the columns of a table.
83 * Should return a query to generate a recordset with one row where
84 * the first and only column is the comment
85 * @param tableName - The full name of the table qualified with schema if applicable
86 * @param columnName - The name of the column
88 public String getCommentsQuery(String tableName, String columnName) {
94 * @return : A query to get an empty ResultSet (null if failed) for that table or view.
95 * Subclassed if needed by the different database adapters
97 public String getEmptySetQuery(String table){
98 return "SELECT * FROM " + filterTableName(table) + " WHERE (1 = 0)"; //$NON-NLS-1$ //$NON-NLS-2$
102 * Quotes a string according to the type of the column
103 * @param string to be quoted
104 * @param type according to java.sql.Types
107 public String quote(String string, int type, String typeString) {
108 if (isTextType(type, typeString)) {
109 return "'" + StringUtil.substituteString(string, "'", "''") + "'";
110 } else if (type == java.sql.Types.DATE || type == java.sql.Types.TIMESTAMP){
111 string = string.trim();
112 //Check if we have to strip the millisecods
113 if (string.length() > 2) {
114 String sub = string.substring(string.length() - 2, string.length() - 1);
115 if (string.length() > 1 && sub.equals(".")) //$NON-NLS-1$
116 string = string.substring(0, string.length() - 2); // strip the milliseconds
119 return "'" + string + "'"; //$NON-NLS-1$ //$NON-NLS-2$
126 * Indicates whether or not a particular type should be quoted in
127 * SQL statements. Some databases support non-standard types such as
128 * TEXT that aren't normally recognized as quoted types.
130 * @param type according to java.sql.Types
131 * @param typeString if the type is "Other".
134 protected boolean isTextType(int type, String typeString) {
135 return TypesHelper.isText(type);
140 * Makes an SQL insert string with the given table, names of columns and values
143 * @param valuesClause
146 public String getInsert(String tableName, String namesClause, String valuesClause) {
147 String query = "INSERT INTO " + filterTableName(tableName);
148 if (namesClause != "") {
149 query += " (" + namesClause + ")";
150 query += " VALUES " + "(" + valuesClause; //$NON-NLS-1$
151 query += " )"; //$NON-NLS-1$
156 * Changes the name of the table, usually to allow for lowercase situations.
157 * The parent implementation does nothing to the tableName, simply returns it.
161 public String filterTableName(String tableName) {
165 * Returns a query to get the number of registers from a table
169 public String getCountQuery(String tableName) {
170 return "SELECT COUNT(*) FROM " + filterTableName(tableName);
178 public void getUpdate(String tableName, String string2, StringBuffer whereClause, String string3) {
179 // TODO Auto-generated method stub
183 * Returns an SQL UPDATE statement
188 public String getUpdate(String tableName, String setClause, String whereClause) {
189 String query = "UPDATE " + filterTableName(tableName); //$NON-NLS-1$
190 query += " SET " + setClause; //$NON-NLS-1$
191 if (!whereClause.equals("")) query += " WHERE " + whereClause; //$NON-NLS-1$
195 * Returns an SQL DELETE statement
200 public String getDelete(String tableName, String whereClause) {
201 String query = "DELETE FROM " + filterTableName(tableName); //$NON-NLS-1$
202 if (!whereClause.equals("")) {
203 query += " WHERE " + whereClause; //$NON-NLS-1$
208 * @param userid - the userid used to connect to the database
209 * @return the default schema for the database
211 public String getDefaultSchema(String userid) {
215 * @return Returns the type.
217 public String getType() {
221 protected String getQualifiedName(String schema, String name) {
222 return (schema != null && schema.length() > 0) ? schema + "." + name : name;
225 public Map getDefaultConnectionParameters() {
226 return new HashMap();
230 * Returns the SQL Query to get a list of the Sysnonyms for the current user (schema), of the given type
231 * @param schema The schema to get the query for
232 * @param type The type of the synonym to get. Types can be one from the Entity types
233 * @return - A String with the SQL query
235 public String getShowSynonymsQuery(String schema, String type) {