KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > metadata > torque > TorqueFieldGenerator


1 package org.apache.ojb.broker.metadata.torque;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import org.apache.ojb.broker.metadata.FieldDescriptor;
19
20 import java.sql.Types JavaDoc;
21
22 public class TorqueFieldGenerator {
23
24     private boolean ignoreAutoIncrement;
25
26     public TorqueFieldGenerator(boolean ignoreAutoIncrement) {
27         this.ignoreAutoIncrement = ignoreAutoIncrement;
28     }
29
30     public void generateFieldDescriptors(FieldDescriptor[] descriptors, StringBuffer JavaDoc buffer) {
31         if (descriptors != null) {
32             for (int i = 0; i < descriptors.length; i++) {
33                 FieldDescriptor descriptor = descriptors[i];
34                 generateFieldDescriptor(descriptor, descriptor.getColumnName(), descriptor.isRequired(), descriptor.isAutoIncrement(), descriptor.isPrimaryKey(), buffer);
35             }
36         }
37     }
38
39     public void generateMappingFieldDescriptors(FieldDescriptor[] descriptors, StringBuffer JavaDoc buffer) {
40         if (descriptors != null) {
41             for (int i = 0; i < descriptors.length; i++) {
42                 FieldDescriptor descriptor = descriptors[i];
43                 StringBuffer JavaDoc fieldBuffer = new StringBuffer JavaDoc();
44                 generateFieldDescriptor(descriptor, descriptor.getColumnName(), true, false, true, fieldBuffer);
45                 buffer.append(fieldBuffer);
46             }
47         }
48     }
49
50     private void generateFieldDescriptor(FieldDescriptor descriptor, String JavaDoc fieldName, boolean isRequired, boolean isAutoIncrement, boolean isPrimaryKey, StringBuffer JavaDoc buffer) {
51         buffer.append(" <column name=\"");
52         buffer.append(fieldName);
53         buffer.append("\" required=\"");
54         buffer.append(isRequired || isPrimaryKey);
55         if (!this.ignoreAutoIncrement) {
56             buffer.append("\" autoIncrement=\"");
57             buffer.append(isAutoIncrement);
58         }
59         buffer.append("\" primaryKey=\"");
60         buffer.append(isPrimaryKey);
61         buffer.append("\" type=\"");
62         buffer.append(descriptor.getColumnType());
63
64         generateConstraints(descriptor, buffer);
65
66         buffer.append("\"/>\n");
67     }
68
69     private void generateConstraints(FieldDescriptor descriptor, StringBuffer JavaDoc buffer) {
70         int jdbcType = descriptor.getJdbcType().getType();
71
72         switch (jdbcType) {
73             case Types.BIGINT:
74             case Types.DECIMAL:
75             case Types.DOUBLE:
76             case Types.FLOAT:
77 // case Types.INTEGER:
78
case Types.NUMERIC:
79             case Types.REAL:
80             case Types.SMALLINT:
81             case Types.TINYINT:
82                 if (descriptor.isPrecisionSpecified()) {
83                     buffer.append("\" precision=\"");
84                     buffer.append(descriptor.getPrecision());
85                 }
86                 if (descriptor.isScaleSpecified()) {
87                     buffer.append("\" scale=\"");
88                     buffer.append(descriptor.getScale());
89                 }
90                 break;
91             default:
92                 if (descriptor.isLengthSpecified()) {
93                     buffer.append("\" size=\"");
94                     buffer.append(descriptor.getLength());
95                 }
96         }
97     }
98
99 }
100
Popular Tags