1 package net.sourceforge.phpdt.sql.adapters;
3 import java.sql.Connection;
4 import java.sql.SQLException;
5 import java.util.ArrayList;
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;
18 * Abstract base class for all the adapter classes
20 public abstract class DatabaseAdapter {
22 public abstract String getShowTableQuery(DatabaseInfo info);
23 public String getShowViewQuery(DatabaseInfo info) {
26 public String getShowSequenceQuery(DatabaseInfo info) {
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$
34 return "SELECT * FROM " + info.getSchema() + "." + table; //$NON-NLS-1$ //$NON-NLS-2$
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$
46 // return "SELECT COUNT(*) FROM " + info.getSchema() + "." + table; //$NON-NLS-1$ //$NON-NLS-2$
49 public String getNextValue(String sequence) {
52 //Doesn't seem to be used at the moment
53 // public String getTableListFilter() {
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
62 public String getEmptySetQuery(String table){
63 return "SELECT * FROM " + table + " WHERE (1 = 0)"; //$NON-NLS-1$ //$NON-NLS-2$
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
72 public String quote(String string, int type) {
73 if (SQLHelper.isText(type)){
74 if (string.indexOf('\'') >= 0)
75 return '"' + string + '"';
77 return "'" + string + "'"; //$NON-NLS-1$ //$NON-NLS-2$
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$
92 * Get a list of entities (tables, views, sequences) for a particular
96 * the bookmark that describes the database that is being accessed.
98 * an array of entity objects representing the tables, views and sequences.
99 * @throws SQLException
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;
107 protected Entity[] getEntities(Bookmark bookmark, Connection connection) throws SQLException {
109 List list = new ArrayList();
111 DbElementsConstants.Table,
112 DbElementsConstants.View,
113 DbElementsConstants.Sequence };
115 for (int i = 0; i < types.length; i++) {
116 list.addAll(getEntitiesList(bookmark, connection, types[i]));
119 return (Entity[]) list.toArray(new Entity[0]);
122 protected List getEntitiesList(Bookmark bookmark, Connection connection, String type)
123 throws SQLException {
125 String sql = getSQL(bookmark, type);
126 List list = new ArrayList();
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));
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);