View Javadoc

1   package org.apache.torque.templates.platform;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.text.SimpleDateFormat;
23  import java.util.Collections;
24  import java.util.Date;
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.TimeZone;
28  
29  import org.apache.commons.lang.StringUtils;
30  import org.apache.torque.templates.typemapping.SchemaType;
31  import org.apache.torque.templates.typemapping.SqlType;
32  
33  
34  /**
35   * Default implementation for the Platform interface.
36   *
37   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
38   * @version $Id: PlatformDefaultImpl.java 1379244 2012-08-31 01:05:22Z tfischer $
39   */
40  public class PlatformDefaultImpl implements Platform
41  {
42      /** The date format for formatting database timestamps. */
43      private static final String TIMESTAMP_FORMAT = "''yyyy-MM-dd HH:mm:ss''";
44  
45      /**
46       * Maps the Torque schema types to sql types.
47       */
48      private Map<SchemaType, SqlType> schemaTypeToSqlTypeMap;
49  
50      /**
51       * Default constructor.
52       */
53      public PlatformDefaultImpl()
54      {
55          initialize();
56      }
57  
58      private void initialize()
59      {
60          schemaTypeToSqlTypeMap
61                  = Collections.synchronizedMap(
62                      new HashMap<SchemaType, SqlType>());
63          for (SchemaType schemaType : SchemaType.values())
64          {
65              setSchemaTypeToSqlTypeMapping(
66                      schemaType,
67                      new SqlType(schemaType.name()));
68          }
69          setSchemaTypeToSqlTypeMapping(
70                  SchemaType.BOOLEANCHAR,
71                  new SqlType("CHAR"));
72          setSchemaTypeToSqlTypeMapping(
73                  SchemaType.BOOLEANINT,
74                  new SqlType("INTEGER"));
75      }
76  
77      /**
78       * Adds a mapping to the torque schema type -> sql type map.
79       *
80       * @param schemaType the torque schema type which should be mapped,
81       *        not null.
82       * @param sqlType the sql type for the torque schema type, not null.
83       */
84      protected void setSchemaTypeToSqlTypeMapping(
85              SchemaType schemaType,
86              SqlType sqlType)
87      {
88          if (schemaType == null)
89          {
90              throw new NullPointerException("schemaType must not be null");
91          }
92          if (sqlType == null)
93          {
94              throw new NullPointerException("sqlType must not be null");
95          }
96          schemaTypeToSqlTypeMap.put(schemaType, sqlType);
97      }
98  
99      /**
100      * @see Platform#getSqlTypeForSchemaType(SchemaType)
101      */
102     public SqlType getSqlTypeForSchemaType(SchemaType schemaType)
103     {
104         return schemaTypeToSqlTypeMap.get(schemaType);
105     }
106 
107     /**
108      * @return Only produces a SQL fragment if null values are
109      * disallowed.
110      * @see Platform#getNullString(boolean)
111      */
112     public String getNullString(boolean notNull)
113     {
114         return (notNull ? "NOT NULL" : "");
115     }
116 
117     /**
118      * @see Platform#getAutoIncrement()
119      */
120     public String getAutoIncrement()
121     {
122         return "IDENTITY";
123     }
124 
125     /**
126      * @see Platform#hasScale(String)
127      * TODO collect info for all platforms
128      */
129     public boolean hasScale(String sqlType)
130     {
131         return true;
132     }
133 
134     /**
135      * @see Platform#hasSize(String)
136      * TODO collect info for all platforms
137      */
138     public boolean hasSize(String sqlType)
139     {
140         return true;
141     }
142 
143     /**
144      * Returns a possible SQL suffix for column definitions of certain
145      * SQL Types, e.g. for Oracle VARCHAR2 columns, it typically
146      * makes sense to use 'x CHAR' instead of 'x' as size.
147      *
148      * @param sqlType the SQL type to determine the suffix for.
149      *
150      * @return The size suffix, not null.
151      *         This implementation always returns the empty string.
152      */
153     public String getSizeSuffix(String sqlType)
154     {
155         return StringUtils.EMPTY;
156     }
157 
158    /**
159      * @see Platform#createNotNullBeforeAutoincrement()
160      */
161     public boolean createNotNullBeforeAutoincrement()
162     {
163         return true;
164     }
165 
166     /**
167      * @see Platform#quoteAndEscape(String)
168      */
169     public String quoteAndEscape(String text)
170     {
171         String result = text.replace("'", "''");
172         if (escapeBackslashes())
173         {
174             result = result.replace("\\", "\\\\");
175         }
176         return "'" + result + "'";
177     }
178 
179     /**
180      * Returns whether backslashes must be escaped in string literals.
181      *
182      * @return true if backslashes bust be escaped, false otherwise.
183      */
184     protected boolean escapeBackslashes()
185     {
186         return true;
187     }
188 
189     /**
190      * {@inheritDoc}
191      */
192     public String getDateString(Date date)
193     {
194         return getTimestampString(date);
195     }
196 
197     /**
198      * {@inheritDoc}
199      */
200     public String getTimeString(Date date)
201     {
202         return getTimestampString(date);
203     }
204 
205     /**
206      * {@inheritDoc}
207      */
208     public String getTimestampString(Date date)
209     {
210         SimpleDateFormat dateFormat = new SimpleDateFormat(TIMESTAMP_FORMAT);
211         dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
212         return dateFormat.format(date);
213     }
214 }