preparing new release
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / adapters / DatabaseAdapter.java
1 package com.quantum.adapters;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import com.quantum.Messages;
7 import com.quantum.util.StringUtil;
8 import com.quantum.util.sql.TypesHelper;
9
10 /**
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.
14  * 
15  * @author root
16  */
17 public abstract class DatabaseAdapter {
18     
19         private final String type;
20         
21         protected DatabaseAdapter(String type) {
22                 this.type = type;
23         }
24         
25         public String getDisplayName() {
26                 return Messages.getString(DatabaseAdapter.class, getType());
27         }
28         
29         /**
30          * Returns the SQL Query to get a list of the tables for the current user (schema) 
31          * @param info
32          * @return - A String with the SQL query 
33          */
34         public String getShowTableQuery(String schema) {
35                 return null;
36         }
37         /**
38          * Returns the SQL Query to get a list of the queries for the current user (schema) 
39          * @param info
40          * @return - A String with the SQL query
41          */
42         public String getShowViewQuery(String schema) {
43                 return null;
44         }
45         /**
46          * Returns the SQL Query to get a list of the sequences for the current user (schema) 
47          * @param info
48          * @return - A String with the SQL query
49          */
50         public String getShowSequenceQuery(String schema) {
51         return null;
52         }
53
54         /** Returns the SQL Query to access all the columns of a table (SELECT)
55          * @param info
56          * @param table
57          * @return - A String with the SQL query
58          */
59         public String getTableQuery( String table) {
60                         return "SELECT * FROM " + filterTableName(table); //$NON-NLS-1$
61         }
62     
63         /**
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
68          */
69         public String getNextValue(String sequence, String owner) {
70         return null;
71         }
72         /**
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
77          */
78         public String getPrevValue(String sequence, String owner) {
79                 return null;
80         }
81         /**
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
87          */
88         public String getCommentsQuery(String tableName, String columnName) {
89                 return null;
90         }
91
92         /**
93          * @param table
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
96          */
97         public String getEmptySetQuery(String table){
98                 return "SELECT * FROM " + filterTableName(table) + " WHERE (1 = 0)"; //$NON-NLS-1$ //$NON-NLS-2$
99         }
100     
101         /**
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
105          * @return
106          */
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
117                         }
118                                 
119                         return "'" + string + "'"; //$NON-NLS-1$ //$NON-NLS-2$
120
121                 }
122                 return string;
123         }
124     
125     /**
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.
129      * 
130      * @param type according to java.sql.Types
131      * @param typeString if the type is "Other".
132      * @return
133      */
134     protected boolean isTextType(int type, String typeString) {
135         return TypesHelper.isText(type);
136     }
137     
138     
139         /**
140          * Makes an SQL insert string with the given table, names of columns and values
141          * @param string
142          * @param namesClause
143          * @param valuesClause
144          * @return
145          */
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$
152                                 }
153                 return query;
154         }
155         /**
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.
158          * @param tableName
159          * @return
160          */
161         public String filterTableName(String tableName) {
162                 return tableName;
163         }
164         /**
165          * Returns a query to get the number of registers from a table
166          * @param tableName
167          * @return
168          */
169         public String getCountQuery(String tableName) {
170                 return "SELECT COUNT(*) FROM " + filterTableName(tableName);    
171         }
172         /**
173          * @param string
174          * @param string2
175          * @param whereClause
176          * @param string3
177          */
178         public void getUpdate(String tableName, String string2, StringBuffer whereClause, String string3) {
179                 // TODO Auto-generated method stub
180                 
181         }
182         /**
183          * Returns an SQL UPDATE statement
184          * @param string
185          * @param string2
186          * @param string3
187          */
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$
192                 return query;
193         }
194         /**
195          * Returns an SQL DELETE statement
196          * @param string
197          * @param string2
198          * @return
199          */
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$
204                 }
205                 return query;
206         }
207     /**
208      * @param userid - the userid used to connect to the database
209      * @return the default schema for the database
210      */
211     public String getDefaultSchema(String userid) {
212         return userid;
213     }
214         /**
215          * @return Returns the type.
216          */
217         public String getType() {
218                 return this.type;
219         }
220
221         protected String getQualifiedName(String schema, String name) {
222                 return (schema != null && schema.length() > 0) ? schema + "." + name : name;
223         }
224         
225         public Map getDefaultConnectionParameters() {
226                 return new HashMap();
227         }
228         
229         /**
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 
234          */
235         public String getShowSynonymsQuery(String schema, String type) {
236                 return null;
237         }
238 }