1 package org.apache.torque.templates.platform;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
37
38
39
40 public class PlatformDefaultImpl implements Platform
41 {
42
43 private static final String TIMESTAMP_FORMAT = "''yyyy-MM-dd HH:mm:ss''";
44
45
46
47
48 private Map<SchemaType, SqlType> schemaTypeToSqlTypeMap;
49
50
51
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
79
80
81
82
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
101
102 public SqlType getSqlTypeForSchemaType(SchemaType schemaType)
103 {
104 return schemaTypeToSqlTypeMap.get(schemaType);
105 }
106
107
108
109
110
111
112 public String getNullString(boolean notNull)
113 {
114 return (notNull ? "NOT NULL" : "");
115 }
116
117
118
119
120 public String getAutoIncrement()
121 {
122 return "IDENTITY";
123 }
124
125
126
127
128
129 public boolean hasScale(String sqlType)
130 {
131 return true;
132 }
133
134
135
136
137
138 public boolean hasSize(String sqlType)
139 {
140 return true;
141 }
142
143
144
145
146
147
148
149
150
151
152
153 public String getSizeSuffix(String sqlType)
154 {
155 return StringUtils.EMPTY;
156 }
157
158
159
160
161 public boolean createNotNullBeforeAutoincrement()
162 {
163 return true;
164 }
165
166
167
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
181
182
183
184 protected boolean escapeBackslashes()
185 {
186 return true;
187 }
188
189
190
191
192 public String getDateString(Date date)
193 {
194 return getTimestampString(date);
195 }
196
197
198
199
200 public String getTimeString(Date date)
201 {
202 return getTimestampString(date);
203 }
204
205
206
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 }