001: package net.sourceforge.squirrel_sql.fw.datasetviewer;
002:
003: /*
004: * Copyright (C) 2001-2004 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.sql.ResultSet;
022: import java.sql.ResultSetMetaData;
023: import java.sql.SQLException;
024: import java.sql.Types;
025:
026: import net.sourceforge.squirrel_sql.fw.datasetviewer.cellcomponent.CellComponentFactory;
027: import net.sourceforge.squirrel_sql.fw.datasetviewer.cellcomponent.DataTypeDate;
028:
029: /**
030: * This defines the display information for a column.
031: *
032: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
033: */
034: public class ColumnDisplayDefinition {
035: /** Number of characters to display. */
036: private int _displayWidth;
037:
038: /** Full name of the column, including the table Catalog, Schema and Table names. */
039: private String _fullTableColumnName;
040:
041: /** Column name to be used in SQL statements */
042: private String _columnName;
043:
044: /** Column heading. */
045: private String _label;
046:
047: /**
048: * Type of data displayed in column. When set to Types.NULL, the value is unknown.
049: * Value is from java.sql.Types.
050: * This is needed, for example, when editing the column to know
051: * what operations to apply during cell editing.
052: */
053: private int _sqlType;
054:
055: /**
056: * The name of the data type as know to the DBMS.
057: * This is used to identify a sub-type when multiple data types
058: * have been defined using the same SQL type code.
059: * This may occur when a DBMS defines several DBMS-specific types
060: * using SQL type OTHER (1111).
061: * The only time this is used is when a plugin has registered a
062: * handler for the data type.
063: */
064: private String _sqlTypeName;
065:
066: /**
067: * A boolean indicating whether this field is nullable/may-be-nullable vs. known
068: * to be not nullable.
069: */
070: private boolean _isNullable;
071:
072: /**
073: * The column's normal maximum width in characters as known to the DB.
074: * This is different from _columnWidth in that this is the size known to the
075: * DB whereas _columnWidth may be a different size used in the initial display
076: * of the data on the screen.
077: */
078: private int _columnSize;
079:
080: /**
081: * The number of decimal digits in the column.
082: */
083: private int _precision;
084:
085: /**
086: * The number of decimal digits to the right of the decimal point.
087: */
088: private int _scale;
089:
090: /**
091: * Flag for whether or not this column is signed or unsigned.
092: */
093: private boolean _isSigned;
094:
095: /**
096: * Flag for whether this column represents currency or not.
097: */
098: private boolean _isCurrency;
099:
100: /**
101: * Flag for whether the column is automatically numbered, thus read-only.
102: */
103: private boolean _isAutoIncrement;
104:
105: /**
106: * Ctor.
107: *
108: * @param displayWidth Number of characters to display.
109: * @param label Column heading.
110: */
111: public ColumnDisplayDefinition(int displayWidth, String label) {
112: super ();
113: init(displayWidth, null, null, label, Types.NULL, null, true,
114: 0, 0, 0, true, false, false);
115: }
116:
117: /**
118: * Constructor for use when the type of data in the column is known/needed.
119: *
120: * @param displayWidth Number of characters to display.
121: * @param label Column heading.
122: * @param className Name of the class for the type of data in the column.
123: */
124: public ColumnDisplayDefinition(int displayWidth,
125: String fullTableColumnName, String columnName,
126: String label, int sqlType, String sqlTypeName,
127: boolean isNullable, int columnSize, int precision,
128: int scale, boolean isSigned, boolean isCurrency,
129: boolean isAutoIncrement) {
130: super ();
131: init(displayWidth, fullTableColumnName, columnName, label,
132: sqlType, sqlTypeName, isNullable, columnSize,
133: precision, scale, isSigned, isCurrency, isAutoIncrement);
134: }
135:
136: /**
137: * Constructs a new ColumnDisplayDefinition using ResultSetMetaData from
138: * the specified ResultSet.
139: *
140: * @param rs the ResultSet to use
141: * @param idx the index of the column to build a display definition for.
142: *
143: * @throws SQLException
144: */
145: public ColumnDisplayDefinition(ResultSet rs, int idx)
146: throws SQLException {
147: super ();
148: ResultSetMetaData md = rs.getMetaData();
149:
150: String columnLabel = md.getColumnLabel(idx);
151: String columnName = md.getColumnName(idx);
152: int displayWidth = columnLabel.length();
153: String fullTableColumnName = new StringBuilder(md
154: .getTableName(idx)).append(":").append(columnName)
155: .toString();
156: int sqlType = md.getColumnType(idx);
157: String sqlTypeName = md.getColumnTypeName(idx);
158: boolean isNullable = md.isNullable(idx) == ResultSetMetaData.columnNullable;
159: int columnSize = md.getColumnDisplaySize(idx);
160: int precision = md.getPrecision(idx);
161: int scale = md.getScale(idx);
162: boolean isSigned = md.isSigned(idx);
163: boolean isCurrency = md.isCurrency(idx);
164: boolean isAutoIncrement = md.isAutoIncrement(idx);
165:
166: init(displayWidth, fullTableColumnName, columnName,
167: columnLabel, sqlType, sqlTypeName, isNullable,
168: columnSize, precision, scale, isSigned, isCurrency,
169: isAutoIncrement);
170: }
171:
172: /**
173: * Return the number of characters to display.
174: *
175: * @return The number of characters to display.
176: */
177: public int getDisplayWidth() {
178: return _displayWidth;
179: }
180:
181: /**
182: * Return the full name of the column including the table Catalog, Schema and Table names.
183: *
184: * @return The full table name.
185: */
186: public String getFullTableColumnName() {
187: return _fullTableColumnName;
188: }
189:
190: /**
191: * Return the column heading.
192: *
193: * @return The column heading.
194: */
195: public String getLabel() {
196: return _label;
197: }
198:
199: /**
200: * Return the column data type, which may be Types.NULL.
201: *
202: * @return The type of data in the column (may be Types.NULL).
203: */
204: public int getSqlType() {
205: return _sqlType;
206: }
207:
208: public void setSqlType(int sqlType) {
209: _sqlType = sqlType;
210: }
211:
212: /**
213: * Return the column data type name.
214: *
215: * @return The DBMS-specific name of the type of data in the column.
216: */
217: public String getSqlTypeName() {
218: return _sqlTypeName;
219: }
220:
221: public void setSqlTypeName(String sqlTypeName) {
222: _sqlTypeName = sqlTypeName;
223: }
224:
225: /**
226: * Return a boolean indicating column is nullable or not.
227: *
228: * @return true = column may contain null (with some uncertainty);
229: * false= definitely no nulls allowed.
230: */
231: public boolean isNullable() {
232: return _isNullable;
233: }
234:
235: /**
236: * Override the isNullable field after creation.
237: */
238: public void setIsNullable(boolean isNullable) {
239: _isNullable = isNullable;
240: }
241:
242: /**
243: * Return the size of the column as known to the DB in number of characters,
244: * For non-character fields (e.g. Integer) this will be the number of characters used
245: * in the DB to represent the data (e.g. 4 for an Int) rather than the number of
246: * characters (e.g. decimal digits) that the user may enter.
247: */
248: public int getColumnSize() {
249: return _columnSize;
250: }
251:
252: /**
253: * Return the number of decimal digits that may be entered into this field.
254: */
255: public int getPrecision() {
256: return _precision;
257: }
258:
259: /**
260: * Return the number of decimal digits to the right of the decimal point.
261: */
262: public int getScale() {
263: return _scale;
264: }
265:
266: /**
267: * Return the flag for whether this column is signed or unsigned.
268: */
269: public boolean isSigned() {
270: return _isSigned;
271: }
272:
273: /**
274: * Return the flag for whether this column represents currency or not.
275: */
276: public boolean isCurrency() {
277: return _isCurrency;
278: }
279:
280: /**
281: * Return the class name associated with the sql data type.
282: * When the type is unknown or cannot be edited we return
283: * "java.lang.Object".
284: *
285: * @return The java class name for the data type
286: */
287: public String getClassName() {
288: return CellComponentFactory.getClassName(this );
289: }
290:
291: /**
292: * Private initializer method for ctors. If the display width
293: * is less than the width of the heading then make the display
294: * width the same as the width of the heading.
295: *
296: * @param displayWidth Number of characters to display.
297: * @param label Column heading.
298: * @param sqlType Type of data (from java.sql.Types).
299: */
300: private void init(int displayWidth, String fullTableColumnName,
301: String columnName, String label, int sqlType,
302: String sqlTypeName, boolean isNullable, int columnSize,
303: int precision, int scale, boolean isSigned,
304: boolean isCurrency, boolean isAutoIncrement) {
305: if (label == null) {
306: label = " "; // Some drivers will give null.
307: }
308: _displayWidth = displayWidth;
309: if (_displayWidth < label.length()) {
310: _displayWidth = label.length();
311: }
312: _fullTableColumnName = fullTableColumnName;
313: _columnName = columnName;
314: // If all columns in a table have empty strings as the headings then the
315: // row height of the label row is zero. We dont want this.
316: _label = label.length() > 0 ? label : " ";
317:
318: _sqlType = sqlType;
319: _sqlTypeName = sqlTypeName;
320: if (sqlType == Types.DATE
321: && DataTypeDate.getReadDateAsTimestamp()) {
322: _sqlType = Types.TIMESTAMP;
323: _sqlTypeName = "TIMESTAMP";
324: }
325: _isNullable = isNullable;
326: _columnSize = columnSize;
327: _precision = precision;
328: _scale = scale;
329: _isSigned = isSigned;
330: _isCurrency = isCurrency;
331: _isAutoIncrement = isAutoIncrement;
332: }
333:
334: public String toString() {
335: StringBuilder result = new StringBuilder();
336: result.append("[ columnName=");
337: result.append(_columnName);
338: result.append(", sqlType=");
339: result.append(_sqlType);
340: result.append(", sqlTypeName=");
341: result.append(_sqlTypeName);
342: result.append(", className=");
343: result.append(getClassName());
344: result.append(" ]");
345: return result.toString();
346: }
347:
348: public void setIsAutoIncrement(boolean autoIncrement) {
349: _isAutoIncrement = autoIncrement;
350: }
351:
352: public boolean isAutoIncrement() {
353: return _isAutoIncrement;
354: }
355:
356: /**
357: * @param _columnName the _columnName to set
358: */
359: public void setColumnName(String _columnName) {
360: this ._columnName = _columnName;
361: }
362:
363: /**
364: * @return the _columnName
365: */
366: public String getColumnName() {
367: return _columnName;
368: }
369: }
|