1 /*
2 * Copyright 2003 - 2013 The eFaps Team
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Revision: $Rev$
17 * Last Changed: $Date$
18 * Last Changed By: $Author$
19 */
20
21 package org.efaps.db.databases.information;
22
23 import java.io.Serializable;
24 import java.util.Set;
25
26 import org.apache.commons.lang3.builder.ToStringBuilder;
27 import org.efaps.db.Context;
28 import org.efaps.db.databases.AbstractDatabase.ColumnType;
29
30 /**
31 * Stores information about one column within a table.
32 *
33 * @author The eFaps Team
34 * @version $Id$
35 */
36 public class ColumnInformation
37 implements Serializable
38 {
39 /**
40 * Needed for serialization.
41 */
42 private static final long serialVersionUID = 1L;
43
44 /**
45 * Name of column in upper case.
46 *
47 * @see #getName()
48 */
49 private final String name;
50
51 /**
52 * Set of all possible column types.
53 */
54 private final Set<ColumnType> types;
55
56 /**
57 * Size of the column (for string). Precision of the column (for decimal).
58 *
59 * @see #getSize()
60 */
61 private final int size;
62
63 /**
64 * Could the column have a null value?
65 *
66 * @see #isNullable
67 */
68 private final boolean isNullable;
69
70 /**
71 * Scale of the column.
72 *
73 * @see #getScale()
74 */
75 private final int scale;
76
77 /**
78 * Constructor to initialize all instance variables.
79 *
80 * @param _name name of column
81 * @param _types set of column types
82 * @param _size size/precision of column
83 * @param _scale sclae of the column
84 * @param _isNullable is the column nullable
85 */
86 protected ColumnInformation(final String _name,
87 final Set<ColumnType> _types,
88 final int _size,
89 final int _scale,
90 final boolean _isNullable)
91 {
92 this.name = _name.toUpperCase();
93 this.types = _types;
94 this.size = _size;
95 this.scale = _scale;
96 this.isNullable = _isNullable;
97 }
98
99 /**
100 * Returns for the first found column type in {@link #types} the related
101 * SQL select statement for null values.
102 *
103 * @return null value select statement
104 * @see #types
105 */
106 public String getNullValueSelect()
107 {
108 String ret = null;
109 if (!this.types.isEmpty()) {
110 ret = Context.getDbType().getNullValueSelect(this.types.iterator().next());
111 }
112 return ret;
113 }
114
115 /**
116 * This is the getter method for instance variable {@link #name}. The
117 * method returns the name of the SQL column always in upper case.
118 *
119 * @return value of instance variable {@link #name}
120 * @see #name
121 */
122 public String getName()
123 {
124 return this.name;
125 }
126
127 /**
128 * This is the getter method for instance variable {@link #size}. The
129 * method returns the column size for this column (used for string column
130 * types defined for eFaps column types {@link ColumnType#STRING_LONG} and
131 * {@link ColumnType#STRING_SHORT}). Or the precision/size of this column
132 * (used for decimal column types {@link ColumnType#DECIMAL})
133 *
134 * @return value of instance variable {@link #size}
135 * @see #size
136 */
137 public int getSize()
138 {
139 return this.size;
140 }
141
142 /**
143 * Getter method for instance variable {@link #scale}. The method
144 * returns the scale of this column.
145 * The information is used for decimal column types
146 * {@link ColumnType#DECIMAL}.
147 *
148 * @return value of instance variable {@link #scale}
149 */
150 public int getScale()
151 {
152 return this.scale;
153 }
154
155 /**
156 * This is the getter method for instance variable {@link #isNullable}. The
157 * method returns true, if a null value for this column is allowed.
158 *
159 * @return value of instance variable {@link #isNullable}
160 * @see #isNullable
161 */
162 public boolean isNullable()
163 {
164 return this.isNullable;
165 }
166
167 /**
168 * Returns string representation of this class instance. The information
169 * includes {@link #name}, {@link #types}, {@link #size} and
170 * {@link #isNullable}.
171 * @return String representation of this class
172 */
173 @Override
174 public String toString()
175 {
176 return new ToStringBuilder(this)
177 .append("name", this.name)
178 .append("types", this.types)
179 .append("size", this.size)
180 .append("isNullable", this.isNullable)
181 .toString();
182 }
183 }