001: /*
002: * sqlc 1
003: * SQL Compiler
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/products/sqlc/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.sql.metadata;
024:
025: import java.sql.Types;
026: import java.util.StringTokenizer;
027:
028: /**
029: * @author Pavel Vlasov
030: * @version $Revision: 1.6 $
031: */
032: public class DefaultGenerationPolicy implements GenerationPolicy {
033:
034: private static final String GENERATE_PREFIX = "_SQLC$";
035: protected String packageName = "";
036: protected String wordSeparator = "_";
037: protected String keyWordSeparator = "$";
038:
039: /**
040: *
041: */
042: public DefaultGenerationPolicy() {
043: super ();
044: }
045:
046: public String generateEntityType(final String catalog,
047: final String schema, final String table) {
048: StringBuffer ret = new StringBuffer();
049: if (catalog != null) {
050: ret.append(catalog.toLowerCase());
051: ret.append(".");
052: }
053: if (schema != null) {
054: ret.append(schema.toLowerCase());
055: ret.append(".");
056: }
057: ret.append(tableName(table));
058: return ret.toString();
059: }
060:
061: /**
062: * @param table
063: * @return
064: */
065: protected String tableName(final String table) {
066: StringTokenizer st = new StringTokenizer(table, wordSeparator);
067: StringBuffer ret = new StringBuffer();
068: if (packageName != null && packageName.length() > 0) {
069: ret.append(packageName);
070: ret.append('.');
071: }
072: while (st.hasMoreTokens()) {
073: String token = st.nextToken();
074: ret.append(Character.toUpperCase(token.charAt(0)));
075: if (token.length() > 1) {
076: ret.append(token.substring(1).toLowerCase());
077: }
078: }
079: return ret.toString();
080: }
081:
082: /**
083: * Converts name to Java conventions.
084: * @param name
085: * @return
086: */
087: public static String convert(final String name, String separators) {
088: StringTokenizer st = new StringTokenizer(name, separators);
089: StringBuffer ret = new StringBuffer();
090: while (st.hasMoreTokens()) {
091: String token = st.nextToken();
092: ret.append(Character.toUpperCase(token.charAt(0)));
093: if (token.length() > 1) {
094: ret.append(token.substring(1).toLowerCase());
095: }
096: }
097: return ret.toString();
098: }
099:
100: public String generateFactoryType(final String catalog,
101: final String schema, final String table) {
102: return generateEntityType(catalog, schema, table) + "Factory";
103: }
104:
105: public String generatePKType(final String catalog,
106: final String schema, final String table) {
107: return generateEntityType(catalog, schema, table) + "PK";
108: }
109:
110: public String generateColumnName(String name) {
111: if (name.trim().length() == 0) {
112: return "Column";
113: }
114:
115: StringTokenizer st = new StringTokenizer(name, wordSeparator);
116: StringBuffer ret = new StringBuffer();
117: while (st.hasMoreTokens()) {
118: String token = st.nextToken();
119: ret.append(Character.toUpperCase(token.charAt(0)));
120: if (token.length() > 1) {
121: ret.append(token.substring(1).toLowerCase());
122: }
123: }
124: return ret.toString();
125: }
126:
127: public String getJavaType(int dataType) {
128: switch (dataType) {
129: case Types.BIT:
130: case Types.BOOLEAN:
131: return "java.lang.Boolean";
132:
133: case Types.TINYINT:
134: return "java.lang.Byte";
135:
136: case Types.SMALLINT:
137: return "java.lang.Short";
138:
139: case Types.INTEGER:
140: return "java.lang.Integer";
141:
142: case Types.BIGINT:
143: return "java.lang.Long";
144:
145: case Types.FLOAT:
146: case Types.REAL:
147: case Types.DOUBLE:
148: return "java.lang.Double";
149:
150: case Types.NUMERIC:
151: case Types.DECIMAL:
152: return "java.math.BigDecimal";
153:
154: case Types.DATE:
155: return "java.sql.Date";
156:
157: case Types.TIME:
158: return "java.sql.Time";
159:
160: case Types.TIMESTAMP:
161: return "java.sql.Timestamp";
162:
163: case Types.BINARY:
164: case Types.VARBINARY:
165: case Types.LONGVARBINARY:
166: return "byte[]";
167:
168: case Types.OTHER:
169: case Types.JAVA_OBJECT:
170: return "java.lang.Object";
171:
172: case Types.DISTINCT:
173: case Types.STRUCT:
174: case Types.ARRAY:
175: case Types.BLOB:
176: case Types.CLOB:
177: case Types.REF:
178: case Types.DATALINK:
179:
180: case Types.NULL:
181:
182: case Types.CHAR:
183: case Types.VARCHAR:
184: case Types.LONGVARCHAR:
185: default:
186: return "java.lang.String";
187: }
188: }
189:
190: public String generateKeyName(String keyName) {
191: int idx = keyName.indexOf(GENERATE_PREFIX);
192: if (idx == -1
193: || idx == keyName.length() - GENERATE_PREFIX.length()) {
194: return null;
195: }
196:
197: return tableName(keyName.substring(idx
198: + GENERATE_PREFIX.length()));
199: }
200:
201: public String getPackageName() {
202: return packageName;
203: }
204:
205: public String generateValueType(String catalog, String schema,
206: String table) {
207: return generateEntityType(catalog, schema, table) + "Value";
208: }
209:
210: public String generateEntityImplType(String catalog, String schema,
211: String table) {
212: return generateEntityType(catalog, schema, table) + "Impl";
213: }
214:
215: public String generateFactoryImplType(String catalog,
216: String schema, String table) {
217: return generateFactoryType(catalog, schema, table) + "Impl";
218: }
219:
220: public String generatePKImplType(String catalog, String schema,
221: String table) {
222: return generatePKType(catalog, schema, table) + "Impl";
223: }
224:
225: public String generateValueImplType(String catalog, String schema,
226: String table) {
227: return generateValueType(catalog, schema, table) + "Impl";
228: }
229:
230: public String generateEngineType() {
231: if (packageName == null || packageName.length() == 0) {
232: return "Engine";
233: }
234:
235: return packageName + ".Engine";
236: }
237:
238: public IndexInfo generateIndexInfo(String indexName,
239: TableDescriptor descriptor) {
240: if (indexName == null) {
241: return null;
242: }
243: int idx = indexName.indexOf(GENERATE_PREFIX);
244: if (idx == -1
245: || idx == indexName.length() - GENERATE_PREFIX.length()) {
246: return null;
247: }
248:
249: indexName = indexName.substring(idx + GENERATE_PREFIX.length());
250: idx = indexName.indexOf('_');
251: if (idx == -1 || idx == 0 || idx == indexName.length() - 1) {
252: return null;
253: }
254:
255: final String modeString = indexName.substring(0, idx);
256: final String javaName = tableName(indexName.substring(idx + 1));
257: return new IndexInfo() {
258:
259: public String getJavaName() {
260: return javaName;
261: }
262:
263: public boolean isOrdered() {
264: return modeString.indexOf('O') != -1;
265: }
266:
267: public boolean isEQ() {
268: return modeString.indexOf('E') != -1;
269: }
270:
271: public boolean isNE() {
272: return modeString.indexOf('N') != -1;
273: }
274:
275: public boolean isLT() {
276: return modeString.indexOf('L') != -1;
277: }
278:
279: public boolean isLE() {
280: return modeString.indexOf('M') != -1;
281: }
282:
283: public boolean isGT() {
284: return modeString.indexOf('G') != -1;
285: }
286:
287: public boolean isGE() {
288: return modeString.indexOf('H') != -1;
289: }
290:
291: };
292: }
293:
294: private static final int LOWER_CASE = 0;
295: private static final int UPPER_CASE = 1;
296: private static final int NON_LETTER = 2;
297:
298: private static int charMode(char ch) {
299: if (Character.isLetter(ch)) {
300: return Character.isUpperCase(ch) ? UPPER_CASE : LOWER_CASE;
301: }
302:
303: return NON_LETTER;
304: }
305:
306: public String generateLabel(String columnName) {
307: if (columnName == null) {
308: return columnName;
309: }
310:
311: StringBuffer ret = new StringBuffer();
312:
313: char[] ca = columnName.toCharArray();
314: for (int i = 0; i < ca.length; i++) {
315: if (i == 0) {
316: ret.append(Character.toUpperCase(ca[i]));
317: } else {
318: switch (charMode(ca[i])) {
319: case LOWER_CASE:
320: switch (charMode(ca[i - 1])) {
321: case LOWER_CASE:
322: case UPPER_CASE:
323: break;
324: case NON_LETTER:
325: ret.append(' ');
326: break;
327: }
328: break;
329: case UPPER_CASE:
330: switch (charMode(ca[i - 1])) {
331: case UPPER_CASE:
332: break;
333: case LOWER_CASE:
334: case NON_LETTER:
335: ret.append(' ');
336: break;
337: }
338: break;
339: case NON_LETTER:
340: switch (charMode(ca[i - 1])) {
341: case LOWER_CASE:
342: case UPPER_CASE:
343: ret.append(' ');
344: break;
345: case NON_LETTER:
346: break;
347: }
348: break;
349:
350: }
351: ret.append(Character.toLowerCase(ca[i]));
352: }
353: }
354:
355: return ret.toString();
356: }
357:
358: public static void main(String[] args) {
359: DefaultGenerationPolicy dgp = new DefaultGenerationPolicy();
360: System.out.println(dgp.generateLabel("FirstName33"));
361: }
362: }
|