Fixed #1721079 - Can not set breakpoint in different files on the same line
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / adapters / DatabaseAdapter.java
1 package net.sourceforge.phpdt.sql.adapters;
2
3 import java.sql.Connection;
4 import java.sql.SQLException;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import net.sourceforge.phpdt.sql.bookmarks.Bookmark;
9 import net.sourceforge.phpdt.sql.model.Entity;
10 import net.sourceforge.phpdt.sql.model.EntityFactory;
11 import net.sourceforge.phpdt.sql.sql.DbElementsConstants;
12 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
13 import net.sourceforge.phpdt.sql.sql.SQLHelper;
14 import net.sourceforge.phpdt.sql.sql.SQLResults;
15
16 /**
17  * @author root
18  * Abstract base class for all the adapter classes
19  */
20 public abstract class DatabaseAdapter {
21     
22         public abstract String getShowTableQuery(DatabaseInfo info);
23         public String getShowViewQuery(DatabaseInfo info) {
24                 return null;
25         }
26         public String getShowSequenceQuery(DatabaseInfo info) {
27         return null;
28         }
29         public final String getShowTableQuery(DatabaseInfo info, String table) {
30                 String schema = info.getSchema();
31                 if (schema.equals("")) { //$NON-NLS-1$
32                         return "SELECT * FROM " + table; //$NON-NLS-1$
33                 } else {
34                         return "SELECT * FROM " + info.getSchema() + "." + table; //$NON-NLS-1$ //$NON-NLS-2$
35                 }
36         }
37     
38     
39     
40     // BCH: Not used
41 //      public final String getTableCountQuery(DatabaseInfo info, String table) {
42 //              String schema = info.getSchema();
43 //              if (schema.equals("")) { //$NON-NLS-1$
44 //                      return "SELECT COUNT(*) FROM " + table; //$NON-NLS-1$
45 //              } else {
46 //                      return "SELECT COUNT(*) FROM " + info.getSchema() + "." + table; //$NON-NLS-1$ //$NON-NLS-2$
47 //              }
48 //      }
49         public String getNextValue(String sequence) {
50         return null;
51         }
52         //Doesn't seem to be used at the moment
53 //      public String getTableListFilter() {
54 //              return null;
55 //      }
56
57         /**
58          * @param table
59          * @return : A query to get an empty ResultSet (null if failed) for that table or view.
60          * Subclassed if needed by the different database adapters
61          */
62         public String getEmptySetQuery(String table){
63                 return "SELECT * FROM " + table + " WHERE (1 = 0)"; //$NON-NLS-1$ //$NON-NLS-2$
64         }
65     
66         /**
67          * Quotes a string according to the type of the column 
68          * @param string to be quoted
69          * @param type according to java.sql.Types
70          * @return
71          */
72         public String quote(String string, int type) {
73                 if (SQLHelper.isText(type)){
74                         if (string.indexOf('\'') >= 0)
75                                 return '"' + string + '"';
76                         else
77                                 return "'" + string + "'";                                               //$NON-NLS-1$ //$NON-NLS-2$
78                 }
79                 else if (type == java.sql.Types.DATE || type == java.sql.Types.TIMESTAMP){              
80                         string = string.trim();
81                         String sub = string.substring(string.length() - 2, string.length() - 1);
82                         if (string.length() > 1 && sub.equals(".")) //$NON-NLS-1$
83                                 string = string.substring(0, string.length() - 2);
84                         return "'" + string + "'"; //$NON-NLS-1$ //$NON-NLS-2$
85
86                 }
87                 return string;
88         }
89     
90     
91     /**
92      * Get a list of entities (tables, views, sequences) for a particular
93      * bookmark.
94      * 
95      * @param bookmark -
96      *     the bookmark that describes the database that is being accessed.
97      * @return 
98      *     an array of entity objects representing the tables, views and sequences.
99      * @throws SQLException
100      */
101     public Entity[] getEntities(Bookmark bookmark) throws SQLException {
102         Connection connection = bookmark.getConnection();
103         Entity[] result = getEntities(bookmark, connection);
104         return (result == null) ? new Entity[0] : result;
105     }
106     
107     protected Entity[] getEntities(Bookmark bookmark, Connection connection) throws SQLException {
108         
109         List list = new ArrayList();
110         String[] types = { 
111             DbElementsConstants.Table, 
112             DbElementsConstants.View, 
113             DbElementsConstants.Sequence };
114             
115         for (int i = 0; i < types.length; i++) {
116             list.addAll(getEntitiesList(bookmark, connection, types[i]));
117         }
118
119         return (Entity[]) list.toArray(new Entity[0]);
120     }
121
122     protected List getEntitiesList(Bookmark bookmark, Connection connection, String type) 
123         throws SQLException {
124             
125         String sql = getSQL(bookmark, type);
126         List list = new ArrayList();
127         
128         if (sql != null) {
129             SQLResults results = MultiSQLServer.getInstance().execute(connection, sql);
130             for (int i = 1, size = (results == null) ? 0 : results.getRowCount(); i <= size; i++) {
131                 list.add(EntityFactory.getInstance().create(
132                     bookmark, bookmark.getSchema(), results.getElement(1, i).toString(), type));
133             }
134         }
135         return list;
136     }
137
138     private String getSQL(Bookmark bookmark, String type) {
139         DatabaseInfo info = DatabaseInfo.create(bookmark);
140         if (DbElementsConstants.Table.equals(type)) {
141             return getShowTableQuery(info);
142         } else if (DbElementsConstants.View.equals(type)) {
143             return getShowViewQuery(info);
144         } else if (DbElementsConstants.Sequence.equals(type)) {
145             return getShowSequenceQuery(info);
146         } else {
147             return null;
148         }
149     }
150 }