View Javadoc

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   * Copyright 2004 Stephen Cowx
14   * 
15   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
16   * use this file except in compliance with the License. You may obtain a copy of
17   * the License at
18   * 
19   * http://www.apache.org/licenses/LICENSE-2.0
20   * 
21   * Unless required by applicable law or agreed to in writing, software
22   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
23   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
24   * License for the specific language governing permissions and limitations under
25   * the License.
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          /* Do the catalog */
61          if (catalogName != null)
62          {
63              if (meta.supportsCatalogsInTableDefinitions())
64              {
65                  catalogName = prepareName(catalogName, meta);
66                  separator = meta.getCatalogSeparator(); 
67              }
68          }
69          
70          /* Do the schema */
71          if (schemaName != null)
72          {
73              if (meta.supportsSchemasInTableDefinitions())
74              {
75                  schemaName = prepareName(schemaName, meta);
76              }            
77          }
78  
79          /* Do the entity */
80          if (entityName != null)
81              entityName = prepareName(entityName, meta);
82  
83          /* Do a check */
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          /* Do the business */
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         /* Do the identifier */
107         if (identifier != null)
108             identifier = prepareName(identifier, meta);
109 
110         /* Do the entity */
111         if (columnName != null)
112             columnName = prepareName(columnName, meta);
113 
114         /* Do the business */
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         //Check the obvious
137         if (name == null)
138             return null;
139         
140         //Upper lower mixed
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         //Quotes
156 //        if (meta.storesLowerCaseQuotedIdentifiers())
157 //        {
158 //            log.debug("1");
159 //        }
160 //        else if (meta.storesUpperCaseQuotedIdentifiers())
161 //        {
162 //            log.debug("2");
163 //        }
164 //        else if (meta.storesMixedCaseQuotedIdentifiers())
165 //        {
166 //            log.debug("3");   
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 }