1 package com.explosion.expfmodules.rdbmsconn.dbom.dialect;
2
3 import java.sql.DatabaseMetaData;
4 import java.sql.SQLException;
5 import org.apache.log4j.LogManager;
6 import org.apache.log4j.Logger;
7 import com.explosion.expfmodules.rdbmsconn.dbom.DBEntity;
8 import com.explosion.expfmodules.rdbmsconn.dbom.utils.MetadataException;
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 /***
30 * I have to be honest, this is exactly the same concept as the hibernate dialect.
31 * Haven't stolen the actual code (YET!). May do at a later stage.
32 *
33 * For now this is just a moving in the same directio because it is a sensible idea
34 * to support the variations in platform. I guess I will extend it at a later stage.
35 *
36 * @author Stephen
37 * Created on 24-May-2005
38 */
39 public class Dialect
40 {
41 private static Logger log = LogManager.getLogger(Dialect.class);
42
43 private static String[] escapeMeCharacters = { " ", "\'"};
44
45 /***
46 * This method returns the correct identifier for an sql statement against the connected database.
47 * This may be fully qualified. Entity names and schema/ctalog names are prepared using the prepareName
48 * method. Entity names are delimited from the schema's ,catalogs etc with the relevant schema or
49 * catalog separator for the connected database.
50 * @throws MetadataException
51 * @throws SQLException
52 */
53 public String getFullEntityName(DBEntity dbed, DatabaseMetaData meta) throws SQLException, MetadataException
54 {
55 String entityName = dbed.getEntityName();
56 String schemaName = dbed.getSchemaName();
57 String catalogName = dbed.getCatalogName();
58 String separator = ".";
59
60
61 if (catalogName != null)
62 {
63 if (meta.supportsCatalogsInTableDefinitions())
64 {
65 catalogName = prepareName(catalogName, meta);
66 separator = meta.getCatalogSeparator();
67 }
68 }
69
70
71 if (schemaName != null)
72 {
73 if (meta.supportsSchemasInTableDefinitions())
74 {
75 schemaName = prepareName(schemaName, meta);
76 }
77 }
78
79
80 if (entityName != null)
81 entityName = prepareName(entityName, meta);
82
83
84 if (schemaName != null && catalogName != null)
85 throw new MetadataException("Interesting! This database has schema's and catalogs. Please log this as a bug and provide full details of your database platform. Thanks.");
86
87
88 if (schemaName != null)
89 return schemaName + (schemaName.trim().length() > 0 ? separator : "") + entityName;
90 else if (catalogName != null)
91 return catalogName + (catalogName.trim().length() > 0 ? separator : "") + entityName;
92 else
93 return prepareName(entityName, meta);
94 }
95
96 /***
97 * This method returns a fully qualified column name given the name and a
98 * identifier (tablename or whatever).
99 *
100 * Names are prepared using the prepareName method and are delimited from
101 * the identifier with a with a point (.) if the identifier is not null and not an empty string
102 * @throws SQLException
103 */
104 public String getFullColumnName(String columnName, String identifier, DatabaseMetaData meta) throws SQLException
105 {
106
107 if (identifier != null)
108 identifier = prepareName(identifier, meta);
109
110
111 if (columnName != null)
112 columnName = prepareName(columnName, meta);
113
114
115 if (identifier != null)
116 return identifier + (identifier.trim().length() > 0 ? "." : "") + columnName;
117 else
118 return columnName;
119 }
120
121 /***
122 * This takes an entity name and prepares it for use in an sql statement
123 * accordng to the rulews defined by the jdbc driver for entity names.
124 * If the names need to be in quotes it will be placed in quotes.
125 * If the identifiers are stored in the database in upper/lower/mixed case the name will be adjusted accordingly
126 *
127 * If the name contains one of the escapeMeCharacters, sorround it with [
128 * and ]. The scape characters are a space or a single quote
129 *
130 * @param name
131 * @return
132 * @throws SQLException
133 */
134 public String prepareName(String name, DatabaseMetaData meta) throws SQLException
135 {
136
137 if (name == null)
138 return null;
139
140
141 String preparedName = null;
142 if (meta.storesLowerCaseIdentifiers())
143 {
144 preparedName = name.toLowerCase();
145 }
146 else if (meta.storesUpperCaseIdentifiers())
147 {
148 preparedName = name.toUpperCase();
149 }
150 else
151 {
152 preparedName = name;
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 for (int i = 0; i < escapeMeCharacters.length; i++)
170 {
171 if (preparedName.trim().indexOf(escapeMeCharacters[i]) >= 0)
172 return "[" + preparedName.trim() + "]";
173 }
174 return preparedName;
175 }
176
177 /***
178 * Returns true if the DB supports transactions and savepoints.
179 * @return
180 */
181 public boolean supportsCommitAndRollback()
182 {
183 return true;
184 }
185
186 /***
187 * Returns true if the DB supports transactions and savepoints.
188 * @return
189 */
190 public boolean supportsSavePoints()
191 {
192 return false;
193 }
194 }