001package com.hfg.sql.table;
002
003
004import java.util.HashSet;
005import java.util.Set;
006
007import com.hfg.exception.ProgrammingException;
008import com.hfg.util.StringUtil;
009
010
011//------------------------------------------------------------------------------
012/**
013 Database column object.
014 <div>
015 @author J. Alex Taylor, hairyfatguy.com
016 </div>
017 */
018//------------------------------------------------------------------------------
019// com.hfg XML/HTML Coding Library
020//
021// This library is free software; you can redistribute it and/or
022// modify it under the terms of the GNU Lesser General Public
023// License as published by the Free Software Foundation; either
024// version 2.1 of the License, or (at your option) any later version.
025//
026// This library is distributed in the hope that it will be useful,
027// but WITHOUT ANY WARRANTY; without even the implied warranty of
028// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
029// Lesser General Public License for more details.
030//
031// You should have received a copy of the GNU Lesser General Public
032// License along with this library; if not, write to the Free Software
033// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
034//
035// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
036// jataylor@hairyfatguy.com
037//------------------------------------------------------------------------------
038
039
040public class DatabaseCol implements Cloneable, Comparable<DatabaseCol>
041{
042   private DatabaseTable mTable;
043   private String        mName;
044   private String        mAlias;
045   private int           mType;
046   private boolean       mIsId;
047   private boolean       mNotRetrievedByDefault;
048   private DatabaseSequence mSequence;
049   private Set<String>   mTags;
050
051   // For comparison performance
052   private String  mQualifiedNameForCompareTo;
053   private Integer mCachedHashCode;
054
055   //###########################################################################
056   // CONSTRUCTORS
057   //###########################################################################
058
059   //---------------------------------------------------------------------------
060   public DatabaseCol(DatabaseTable inTable, String inName, int inType)
061   {
062      mTable = inTable;
063      mName  = inName;
064      mType  = inType;
065
066      mTable.addCol(this);
067   }
068
069   //###########################################################################
070   // PUBLIC METHODS
071   //###########################################################################
072
073   //---------------------------------------------------------------------------
074   public DatabaseCol clone()
075   {
076      DatabaseCol clone;
077      try
078      {
079         clone = (DatabaseCol) super.clone();
080      }
081      catch (CloneNotSupportedException e)
082      {
083         throw new ProgrammingException(e);
084      }
085
086      return clone;
087   }
088
089   //---------------------------------------------------------------------------
090   private String getQualifiedNameForCompareTo()
091   {
092      if (null == mQualifiedNameForCompareTo)
093      {
094         mQualifiedNameForCompareTo = getTable().getQualifiedName() + "." + name();
095      }
096
097      return mQualifiedNameForCompareTo;
098   }
099
100   //---------------------------------------------------------------------------
101   @Override
102   public int compareTo(DatabaseCol inObj2)
103   {
104      return (inObj2 != null ? getQualifiedNameForCompareTo().compareTo(inObj2.getQualifiedNameForCompareTo()) : -1);
105   }
106
107   //---------------------------------------------------------------------------
108   @Override
109   public boolean equals(Object inObj2)
110   {
111      return (inObj2 != null
112              && inObj2 instanceof DatabaseCol
113              && 0 == compareTo((DatabaseCol) inObj2));
114   }
115
116   //---------------------------------------------------------------------------
117   @Override
118   public int hashCode()
119   {
120      if (null == mCachedHashCode)
121      {
122         mCachedHashCode = getQualifiedNameForCompareTo().hashCode();
123      }
124
125      return mCachedHashCode;
126   }
127
128   //---------------------------------------------------------------------------
129   public String name()
130   {
131      return mName;
132   }
133
134   //---------------------------------------------------------------------------
135   public String qname(String inPrefix)
136   {
137      return (StringUtil.isSet(inPrefix) ? inPrefix + "." : "") + mName;
138   }
139
140   //---------------------------------------------------------------------------
141   public String getQualifiedName()
142   {
143      String prefix = null;
144      if (getTable() != null)
145      {
146         if (StringUtil.isSet(getTable().getAlias()))
147         {
148            prefix = getTable().getAlias();
149         }
150         else
151         {
152            prefix = getTable().name();
153         }
154      }
155
156      return (prefix != null ? prefix + "." : "") + name();
157   }
158
159   //---------------------------------------------------------------------------
160   @Override
161   public String toString()
162   {
163      return getQualifiedName();
164   }
165
166   //---------------------------------------------------------------------------
167   public DatabaseTable getTable()
168   {
169      return mTable;
170   }
171
172   //---------------------------------------------------------------------------
173   public DatabaseCol setTable(DatabaseTable inValue)
174   {
175      mTable = inValue;
176      mQualifiedNameForCompareTo = null;
177      return this;
178   }
179
180   //---------------------------------------------------------------------------
181   public int getType()
182   {
183      return mType;
184   }
185
186   //---------------------------------------------------------------------------
187   public DatabaseCol setIsId(boolean inValue)
188   {
189      mIsId = inValue;
190      return this;
191   }
192
193   //---------------------------------------------------------------------------
194   public boolean isId()
195   {
196      return mIsId;
197   }
198
199
200   //---------------------------------------------------------------------------
201   public DatabaseCol setAlias(String inValue)
202   {
203      mAlias = inValue;
204      return this;
205   }
206
207   //---------------------------------------------------------------------------
208   public String getAlias()
209   {
210      return mAlias;
211   }
212
213
214   //---------------------------------------------------------------------------
215   public DatabaseCol setRetrievedByDefault(boolean inValue)
216   {
217      mNotRetrievedByDefault = ! inValue;
218      return this;
219   }
220
221   //---------------------------------------------------------------------------
222   public boolean getRetrievedByDefault()
223   {
224      return ! mNotRetrievedByDefault;
225   }
226
227   //---------------------------------------------------------------------------
228   public DatabaseCol setSequence(DatabaseSequence inValue)
229   {
230      mSequence = inValue;
231      return this;
232   }
233
234   //---------------------------------------------------------------------------
235   public DatabaseSequence getSequence()
236   {
237      return mSequence;
238   }
239
240   //---------------------------------------------------------------------------
241   public DatabaseCol tag(String inValue)
242   {
243      if (null == mTags)
244      {
245         mTags = new HashSet<>(3);
246      }
247
248      mTags.add(inValue);
249
250      return this;
251   }
252
253   //---------------------------------------------------------------------------
254   public boolean tagged(String inValue)
255   {
256      return (mTags != null && mTags.contains(inValue));
257   }
258}