001: /*
002: * (c) Copyright 2006 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.platform.db.model;
028:
029: import org.databene.commons.ArrayFormat;
030:
031: import java.util.List;
032: import java.util.ArrayList;
033:
034: /**
035: * Represents a database column.<br/><br/>
036: * Created: 06.01.2007 08:58:49
037: * @author Volker Bergmann
038: */
039: public class DBColumn {
040:
041: private String name;
042: private DBColumnType type;
043: private int[] modifiers; // TODO v0.4.2 transform to 'size' and 'scale' attributes
044: private String doc;
045: private String defaultValue;
046: private DBTable table;
047: private boolean versionColumn;
048:
049: private List<DBConstraint> ukConstraints; // constraints may be unnamed, so a Map does not make sense
050: private DBConstraint notNullConstraint;
051:
052: // private DBForeignKeyConstraint fkConstraint;
053:
054: // constructors ----------------------------------------------------------------------------------------------------
055:
056: public DBColumn() {
057: this (null, null);
058: }
059:
060: public DBColumn(String name, DBColumnType type, int... modifiers) {
061: this (null, name, type, modifiers);
062: }
063:
064: public DBColumn(DBTable table, String name, DBColumnType type,
065: int... modifiers) {
066: this .table = table;
067: this .name = name;
068: this .type = type;
069: this .modifiers = modifiers;
070: this .doc = null;
071: this .defaultValue = null;
072: this .ukConstraints = new ArrayList<DBConstraint>();
073: this .notNullConstraint = null;
074: // this.fkConstraint = null;
075: this .versionColumn = false;
076: }
077:
078: // properties ------------------------------------------------------------------------------------------------------
079:
080: public DBTable getTable() {
081: return table;
082: }
083:
084: public void setTable(DBTable table) {
085: this .table = table;
086: }
087:
088: public String getName() {
089: return name;
090: }
091:
092: public DBColumnType getType() {
093: return type;
094: }
095:
096: public int[] getModifiers() {
097: return modifiers;
098: }
099:
100: public void setModifiers(int[] modifiers) {
101: this .modifiers = modifiers;
102: }
103:
104: public String getDoc() {
105: return doc;
106: }
107:
108: public void setDoc(String doc) {
109: this .doc = doc;
110: }
111:
112: public String getDefaultValue() {
113: return defaultValue;
114: }
115:
116: public void setDefaultValue(String defaultValue) {
117: this .defaultValue = defaultValue;
118: }
119:
120: public List<DBConstraint> getUkConstraints() {
121: return ukConstraints;
122: }
123:
124: public void addUkConstraint(DBConstraint constraint) {
125: this .ukConstraints.add(constraint);
126: }
127:
128: public DBConstraint getNotNullConstraint() {
129: return notNullConstraint;
130: }
131:
132: public void setNotNullConstraint(DBConstraint notNullConstraint) {
133: this .notNullConstraint = notNullConstraint;
134: }
135:
136: public boolean isNullable() {
137: return (notNullConstraint == null);
138: }
139:
140: public void setNullable(boolean nullable) {
141: if (nullable) {
142: // if a NotNullConstraint exists then remove it
143: notNullConstraint = null;
144: } else {
145: // if there needs to be a NotNullConstraint, check if there exists one, first
146: if (this .isNullable()) {
147: this .notNullConstraint = new DBNotNullConstraint(this );
148: }
149: }
150: }
151:
152: /*
153: public DBForeignKeyConstraint getFkConstraint() {
154: return fkConstraint;
155: }
156: */
157:
158: public boolean isVersionColumn() {
159: return versionColumn;
160: }
161:
162: public void setVersionColumn(boolean versionColumn) {
163: this .versionColumn = versionColumn;
164: }
165:
166: public int size() {
167: if (modifiers != null && modifiers.length > 0)
168: return modifiers[0]; // TODO v0.4.2 evaluate if byte or char
169: return 1;
170: }
171:
172: // java.lang.overrides ---------------------------------------------------------------------------------------------
173:
174: @Override
175: public boolean equals(Object o) {
176: if (this == o)
177: return true;
178: if (o == null || getClass() != o.getClass())
179: return false;
180: final DBColumn that = (DBColumn) o;
181: return this .table.equals(that.table) && name.equals(that.name);
182: }
183:
184: @Override
185: public int hashCode() {
186: return table.hashCode() * 29 + name.hashCode();
187: }
188:
189: @Override
190: public String toString() {
191: StringBuilder builder = new StringBuilder(name).append(" : ")
192: .append(type);
193: if (modifiers.length > 0) {
194: builder.append('(');
195: builder.append(ArrayFormat.formatInts(",", modifiers));
196: builder.append(')');
197: }
198: if (!isNullable())
199: builder.append(" NOT NULL");
200: return builder.toString();
201: }
202:
203: // static convenience methods --------------------------------------------------------------------------------------
204:
205: public static String formatColumnNames(DBColumn[] columns) {
206: StringBuilder builder = new StringBuilder(columns[0].getName());
207: for (int i = 1; i < columns.length; i++)
208: builder.append(", ").append(columns[i].getName());
209: return builder.toString();
210: }
211:
212: public static String formatColumnNames(List<DBColumn> columns) {
213: StringBuilder builder = new StringBuilder(columns.get(0)
214: .getName());
215: for (int i = 1; i < columns.size(); i++)
216: builder.append(", ").append(columns.get(i).getName());
217: return builder.toString();
218: }
219:
220: }
|