01: /*
02: * Copyright 2006 Le Duc Bao, Ralf Joachim
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package org.castor.ddlgen.engine.postgresql;
17:
18: import org.castor.ddlgen.DDLWriter;
19: import org.castor.ddlgen.GeneratorException;
20: import org.castor.ddlgen.keygenerator.IdentityKeyGenerator;
21: import org.castor.ddlgen.schemaobject.Field;
22:
23: /**
24: * PostgreSQL field.
25: *
26: * @author <a href="mailto:leducbao AT gmail DOT com">Le Duc Bao</a>
27: * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
28: * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
29: * @since 1.1
30: */
31: public final class PostgresqlField extends Field {
32: //--------------------------------------------------------------------------
33:
34: /**
35: * {@inheritDoc}
36: */
37: public void toCreateDDL(final DDLWriter writer)
38: throws GeneratorException {
39: writer.print(getName());
40: writer.print(" ");
41:
42: if (isIdentity()
43: && (getKeyGenerator() instanceof IdentityKeyGenerator)) {
44: if ("integer".equalsIgnoreCase(getType().getSqlType())) {
45: writer.print("SERIAL");
46: } else {
47: writer.print("BIGSERIAL");
48: }
49: } else {
50: writer.print(getType().toDDL(this ));
51: }
52:
53: if (isIdentity() || isRequired()) {
54: writer.print(" NOT NULL");
55: }
56: }
57:
58: /**
59: * {@inheritDoc}
60: */
61: public String toCreateDDL() throws GeneratorException {
62: StringBuffer sb = new StringBuffer();
63: sb.append(getName()).append(" ");
64:
65: if (isIdentity()
66: && (getKeyGenerator() instanceof IdentityKeyGenerator)) {
67: if ("integer".equalsIgnoreCase(getType().getSqlType())) {
68: sb.append("SERIAL");
69: } else {
70: sb.append("BIGSERIAL");
71: }
72: } else {
73: sb.append(getType().toDDL(this ));
74: }
75:
76: if (isIdentity() || isRequired()) {
77: sb.append(" NOT NULL");
78: }
79:
80: return sb.toString();
81: }
82:
83: //--------------------------------------------------------------------------
84: }
|