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 String sub = string.substring(string.length() - 2, string.length() - 1);
114 if (string.length() > 1 && sub.equals(".")) //$NON-NLS-1$
115 string = string.substring(0, string.length() - 2); // strip the milliseconds
117 return "'" + string + "'"; //$NON-NLS-1$ //$NON-NLS-2$
124 * Indicates whether or not a particular type should be quoted in
125 * SQL statements. Some databases support non-standard types such as
126 * TEXT that aren't normally recognized as quoted types.
128 * @param type according to java.sql.Types
129 * @param typeString if the type is "Other".
132 protected boolean isTextType(int type, String typeString) {
133 return TypesHelper.isText(type);
138 * Makes an SQL insert string with the given table, names of columns and values
141 * @param valuesClause
144 public String getInsert(String tableName, String namesClause, String valuesClause) {
145 String query = "INSERT INTO " + filterTableName(tableName);
146 if (namesClause != "") {
147 query += " (" + namesClause + ")";
148 query += " VALUES " + "(" + valuesClause; //$NON-NLS-1$
149 query += " )"; //$NON-NLS-1$
154 * Changes the name of the table, usually to allow for lowercase situations.
155 * The parent implementation does nothing to the tableName, simply returns it.
159 public String filterTableName(String tableName) {
163 * Returns a query to get the number of registers from a table
167 public String getCountQuery(String tableName) {
168 return "SELECT COUNT(*) FROM " + filterTableName(tableName);
176 public void getUpdate(String tableName, String string2, StringBuffer whereClause, String string3) {
177 // TODO Auto-generated method stub
181 * Returns an SQL UPDATE statement
186 public String getUpdate(String tableName, String setClause, String whereClause) {
187 String query = "UPDATE " + filterTableName(tableName); //$NON-NLS-1$
188 query += " SET " + setClause; //$NON-NLS-1$
189 if (!whereClause.equals("")) query += " WHERE " + whereClause; //$NON-NLS-1$
193 * Returns an SQL DELETE statement
198 public String getDelete(String tableName, String whereClause) {
199 String query = "DELETE FROM " + filterTableName(tableName); //$NON-NLS-1$
200 if (!whereClause.equals("")) {
201 query += " WHERE " + whereClause; //$NON-NLS-1$
206 * @param userid - the userid used to connect to the database
207 * @return the default schema for the database
209 public String getDefaultSchema(String userid) {
213 * @return Returns the type.
215 public String getType() {
219 protected String getQualifiedName(String schema, String name) {
220 return (schema != null && schema.length() > 0) ? schema + "." + name : name;
223 public Map getDefaultConnectionParameters() {
224 return new HashMap();