001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.sql.rowset;
019:
020: import java.io.Serializable;
021: import java.sql.ResultSetMetaData;
022: import java.sql.SQLException;
023:
024: import javax.sql.RowSetMetaData;
025:
026: import org.apache.harmony.sql.internal.nls.Messages;
027:
028: /**
029: * This class is a concrete implementation of javax.sql.RowSetMetatData, which
030: * provides methods that get and set column information.
031: *
032: * A RowSetMetaDataImpl object can be obtained by the getMetaData() method in
033: * javax.sql.RowSet.
034: *
035: */
036: public class RowSetMetaDataImpl implements RowSetMetaData, Serializable {
037:
038: private static final String EMPTY_STRING = ""; //$NON-NLS-1$
039:
040: private static final int DEFAULT_COLUMN_COUNT = 5;
041:
042: private static final long serialVersionUID = 6893806403181801867L;
043:
044: private int colCount;
045:
046: private ColInfo[] colInfo;
047:
048: /**
049: * The default constructor.
050: */
051: public RowSetMetaDataImpl() {
052: // do nothing
053: }
054:
055: private void checkNegativeValue(int value, String msg)
056: throws SQLException {
057: if (value < 0) {
058: throw new SQLException(Messages.getString(msg));
059: }
060: }
061:
062: private void checkColumnIndex(int columnIndex) throws SQLException {
063: if (null == colInfo || columnIndex < 1
064: || columnIndex >= colInfo.length) {
065: throw new SQLException(Messages.getString(
066: "sql.27", columnIndex + 1)); //$NON-NLS-1$
067: }
068: // lazy initialization
069: if (null == colInfo[columnIndex]) {
070: colInfo[columnIndex] = new ColInfo();
071: }
072: }
073:
074: /**
075: * {@inheritDoc}
076: *
077: * @see javax.sql.RowSetMetaData#setColumnCount(int)
078: */
079: public void setColumnCount(int columnCount) throws SQLException {
080: if (columnCount <= 0) {
081: throw new SQLException(Messages.getString("sql.26")); //$NON-NLS-1$
082: }
083: try {
084: if (columnCount + 1 > 0) {
085: colInfo = new ColInfo[columnCount + 1];
086: } else {
087: colInfo = new ColInfo[DEFAULT_COLUMN_COUNT];
088: }
089: } catch (OutOfMemoryError e) {
090: // For compatibility, use same default value as RI
091: colInfo = new ColInfo[DEFAULT_COLUMN_COUNT];
092: }
093: colCount = columnCount;
094: }
095:
096: /**
097: * {@inheritDoc}
098: *
099: * @see javax.sql.RowSetMetaData#setAutoIncrement(int, boolean)
100: */
101: public void setAutoIncrement(int columnIndex, boolean property)
102: throws SQLException {
103: checkColumnIndex(columnIndex);
104: colInfo[columnIndex].autoIncrement = property;
105: }
106:
107: /**
108: * {@inheritDoc}
109: *
110: * @see javax.sql.RowSetMetaData#setCaseSensitive(int, boolean)
111: */
112: public void setCaseSensitive(int columnIndex, boolean property)
113: throws SQLException {
114: checkColumnIndex(columnIndex);
115: colInfo[columnIndex].caseSensitive = property;
116: }
117:
118: /**
119: * {@inheritDoc}
120: *
121: * @see javax.sql.RowSetMetaData#setSearchable(int, boolean)
122: */
123: public void setSearchable(int columnIndex, boolean property)
124: throws SQLException {
125: checkColumnIndex(columnIndex);
126: colInfo[columnIndex].searchable = property;
127: }
128:
129: /**
130: * {@inheritDoc}
131: *
132: * @see javax.sql.RowSetMetaData#setCurrency(int, boolean)
133: */
134: public void setCurrency(int columnIndex, boolean property)
135: throws SQLException {
136: checkColumnIndex(columnIndex);
137: colInfo[columnIndex].currency = property;
138: }
139:
140: /**
141: * {@inheritDoc}
142: *
143: * @see javax.sql.RowSetMetaData#setNullable(int, int)
144: */
145: public void setNullable(int columnIndex, int property)
146: throws SQLException {
147: if (property != ResultSetMetaData.columnNoNulls
148: && property != ResultSetMetaData.columnNullable
149: && property != ResultSetMetaData.columnNullableUnknown) {
150: throw new SQLException(Messages.getString("sql.29")); //$NON-NLS-1$
151: }
152:
153: checkColumnIndex(columnIndex);
154: colInfo[columnIndex].nullable = property;
155: }
156:
157: /**
158: * {@inheritDoc}
159: *
160: * @see javax.sql.RowSetMetaData#setSigned(int, boolean)
161: */
162: public void setSigned(int columnIndex, boolean property)
163: throws SQLException {
164: checkColumnIndex(columnIndex);
165: colInfo[columnIndex].signed = property;
166: }
167:
168: /**
169: * {@inheritDoc}
170: *
171: * @see javax.sql.RowSetMetaData#setColumnDisplaySize(int, int)
172: */
173: public void setColumnDisplaySize(int columnIndex, int size)
174: throws SQLException {
175: checkNegativeValue(size, "sql.30"); //$NON-NLS-1$
176:
177: checkColumnIndex(columnIndex);
178: colInfo[columnIndex].columnDisplaySize = size;
179: }
180:
181: /**
182: * {@inheritDoc}
183: *
184: * @see javax.sql.RowSetMetaData#setColumnLabel(int, String)
185: */
186: public void setColumnLabel(int columnIndex, String label)
187: throws SQLException {
188: checkColumnIndex(columnIndex);
189: colInfo[columnIndex].columnLabel = label == null ? EMPTY_STRING
190: : label;
191: }
192:
193: /**
194: * {@inheritDoc}
195: *
196: * @see javax.sql.RowSetMetaData#setColumnName(int, String)
197: */
198: public void setColumnName(int columnIndex, String columnName)
199: throws SQLException {
200: checkColumnIndex(columnIndex);
201: colInfo[columnIndex].columnName = columnName == null ? EMPTY_STRING
202: : columnName;
203: }
204:
205: /**
206: * {@inheritDoc}
207: *
208: * @see javax.sql.RowSetMetaData#setSchemaName(int, String)
209: */
210: public void setSchemaName(int columnIndex, String schemaName)
211: throws SQLException {
212: checkColumnIndex(columnIndex);
213: colInfo[columnIndex].schemaName = schemaName == null ? EMPTY_STRING
214: : schemaName;
215: }
216:
217: /**
218: * {@inheritDoc}
219: *
220: * @see javax.sql.RowSetMetaData#setPrecision(int, int)
221: */
222: public void setPrecision(int columnIndex, int precision)
223: throws SQLException {
224: checkNegativeValue(precision, "sql.31"); //$NON-NLS-1$
225:
226: checkColumnIndex(columnIndex);
227: colInfo[columnIndex].precision = precision;
228: }
229:
230: /**
231: * {@inheritDoc}
232: *
233: * @see javax.sql.RowSetMetaData#setScale(int, int)
234: */
235: public void setScale(int columnIndex, int scale)
236: throws SQLException {
237: checkNegativeValue(scale, "sql.32"); //$NON-NLS-1$
238:
239: checkColumnIndex(columnIndex);
240: colInfo[columnIndex].scale = scale;
241: }
242:
243: /**
244: * {@inheritDoc}
245: *
246: * @see javax.sql.RowSetMetaData#setTableName(int, String)
247: */
248: public void setTableName(int columnIndex, String tableName)
249: throws SQLException {
250: checkColumnIndex(columnIndex);
251: colInfo[columnIndex].tableName = tableName == null ? EMPTY_STRING
252: : tableName;
253: }
254:
255: /**
256: * {@inheritDoc}
257: *
258: * @see javax.sql.RowSetMetaData#setCatalogName(int, String)
259: */
260: public void setCatalogName(int columnIndex, String catalogName)
261: throws SQLException {
262: checkColumnIndex(columnIndex);
263: colInfo[columnIndex].catalogName = catalogName == null ? EMPTY_STRING
264: : catalogName;
265: }
266:
267: /**
268: * {@inheritDoc}
269: *
270: * @see javax.sql.RowSetMetaData#setColumnType(int, int)
271: */
272: public void setColumnType(int columnIndex, int SQLType)
273: throws SQLException {
274: SqlUtil.validateType(SQLType);
275:
276: checkColumnIndex(columnIndex);
277: colInfo[columnIndex].colType = SQLType;
278: }
279:
280: /**
281: * {@inheritDoc}
282: *
283: * @see javax.sql.RowSetMetaData#setColumnTypeName(int, String)
284: */
285: public void setColumnTypeName(int columnIndex, String typeName)
286: throws SQLException {
287: checkColumnIndex(columnIndex);
288: colInfo[columnIndex].colTypeName = typeName == null ? EMPTY_STRING
289: : typeName;
290: }
291:
292: /**
293: * {@inheritDoc}
294: *
295: * @see java.sql.ResultSetMetaData#getColumnCount()
296: */
297: public int getColumnCount() throws SQLException {
298: return colCount;
299: }
300:
301: /**
302: * {@inheritDoc}
303: *
304: * @see java.sql.ResultSetMetaData#isAutoIncrement(int)
305: */
306: public boolean isAutoIncrement(int columnIndex) throws SQLException {
307: checkColumnIndex(columnIndex);
308: return colInfo[columnIndex].autoIncrement;
309: }
310:
311: /**
312: * {@inheritDoc}
313: *
314: * @see java.sql.ResultSetMetaData#isCaseSensitive(int)
315: */
316: public boolean isCaseSensitive(int columnIndex) throws SQLException {
317: checkColumnIndex(columnIndex);
318: return colInfo[columnIndex].caseSensitive;
319: }
320:
321: /**
322: * {@inheritDoc}
323: *
324: * @see java.sql.ResultSetMetaData#isSearchable(int)
325: */
326: public boolean isSearchable(int columnIndex) throws SQLException {
327: checkColumnIndex(columnIndex);
328: return colInfo[columnIndex].searchable;
329: }
330:
331: /**
332: * {@inheritDoc}
333: *
334: * @see java.sql.ResultSetMetaData#isCurrency(int)
335: */
336: public boolean isCurrency(int columnIndex) throws SQLException {
337: checkColumnIndex(columnIndex);
338: return colInfo[columnIndex].currency;
339: }
340:
341: /**
342: * {@inheritDoc}
343: *
344: * @see java.sql.ResultSetMetaData#isNullable(int)
345: */
346: public int isNullable(int columnIndex) throws SQLException {
347: checkColumnIndex(columnIndex);
348: return colInfo[columnIndex].nullable;
349: }
350:
351: /**
352: * {@inheritDoc}
353: *
354: * @see java.sql.ResultSetMetaData#isSigned(int)
355: */
356: public boolean isSigned(int columnIndex) throws SQLException {
357: checkColumnIndex(columnIndex);
358: return colInfo[columnIndex].signed;
359: }
360:
361: /**
362: * {@inheritDoc}
363: *
364: * @see java.sql.ResultSetMetaData#getColumnDisplaySize(int)
365: */
366: public int getColumnDisplaySize(int columnIndex)
367: throws SQLException {
368: checkColumnIndex(columnIndex);
369: return colInfo[columnIndex].columnDisplaySize;
370: }
371:
372: /**
373: * {@inheritDoc}
374: *
375: * @see java.sql.ResultSetMetaData#getColumnLabel(int)
376: */
377: public String getColumnLabel(int columnIndex) throws SQLException {
378: checkColumnIndex(columnIndex);
379: return colInfo[columnIndex].columnLabel;
380: }
381:
382: /**
383: * {@inheritDoc}
384: *
385: * @see java.sql.ResultSetMetaData#getColumnName(int)
386: */
387: public String getColumnName(int columnIndex) throws SQLException {
388: checkColumnIndex(columnIndex);
389: return colInfo[columnIndex].columnName;
390: }
391:
392: /**
393: * {@inheritDoc}
394: *
395: * @see java.sql.ResultSetMetaData#getSchemaName(int)
396: */
397: public String getSchemaName(int columnIndex) throws SQLException {
398: checkColumnIndex(columnIndex);
399: return colInfo[columnIndex].schemaName;
400: }
401:
402: /**
403: * {@inheritDoc}
404: *
405: * @see java.sql.ResultSetMetaData#getPrecision(int)
406: */
407: public int getPrecision(int columnIndex) throws SQLException {
408: checkColumnIndex(columnIndex);
409: return colInfo[columnIndex].precision;
410: }
411:
412: /**
413: * {@inheritDoc}
414: *
415: * @see java.sql.ResultSetMetaData#getScale(int)
416: */
417: public int getScale(int columnIndex) throws SQLException {
418: checkColumnIndex(columnIndex);
419: return colInfo[columnIndex].scale;
420: }
421:
422: /**
423: * {@inheritDoc}
424: *
425: * @see java.sql.ResultSetMetaData#getTableName(int)
426: */
427: public String getTableName(int columnIndex) throws SQLException {
428: checkColumnIndex(columnIndex);
429: return colInfo[columnIndex].tableName;
430: }
431:
432: /**
433: * {@inheritDoc}
434: *
435: * @see java.sql.ResultSetMetaData#getCatalogName(int)
436: */
437: public String getCatalogName(int columnIndex) throws SQLException {
438: checkColumnIndex(columnIndex);
439: return colInfo[columnIndex].catalogName;
440: }
441:
442: /**
443: * {@inheritDoc}
444: *
445: * @see java.sql.ResultSetMetaData#getColumnType(int)
446: */
447: public int getColumnType(int columnIndex) throws SQLException {
448: checkColumnIndex(columnIndex);
449: return colInfo[columnIndex].colType;
450: }
451:
452: /**
453: * {@inheritDoc}
454: *
455: * @see java.sql.ResultSetMetaData#getColumnTypeName(int)
456: */
457: public String getColumnTypeName(int columnIndex)
458: throws SQLException {
459: checkColumnIndex(columnIndex);
460: return colInfo[columnIndex].colTypeName;
461: }
462:
463: /**
464: * {@inheritDoc}
465: *
466: * @see java.sql.ResultSetMetaData#isReadOnly(int)
467: */
468: public boolean isReadOnly(int columnIndex) throws SQLException {
469: return !isWritable(columnIndex);
470: }
471:
472: /**
473: * {@inheritDoc}
474: *
475: * @see java.sql.ResultSetMetaData#isWritable(int)
476: */
477: public boolean isWritable(int columnIndex) throws SQLException {
478: checkColumnIndex(columnIndex);
479: return colInfo[columnIndex].writeable;
480: }
481:
482: /**
483: * {@inheritDoc}
484: *
485: * @see java.sql.ResultSetMetaData#isDefinitelyWritable(int)
486: */
487: public boolean isDefinitelyWritable(int columnIndex)
488: throws SQLException {
489: checkColumnIndex(columnIndex);
490: return colInfo[columnIndex].definiteWritable;
491: }
492:
493: /**
494: * {@inheritDoc}
495: *
496: * @see java.sql.ResultSetMetaData#getColumnClassName(int)
497: */
498: public String getColumnClassName(int columnIndex)
499: throws SQLException {
500: return SqlUtil.getClassNameByType(getColumnType(columnIndex));
501: }
502:
503: /**
504: * The inner class to store meta information of columns.
505: */
506: private class ColInfo implements Serializable {
507:
508: private static final long serialVersionUID = 5490834817919311283L;
509:
510: public boolean autoIncrement;
511:
512: public boolean caseSensitive;
513:
514: public boolean currency;
515:
516: public boolean signed;
517:
518: public boolean searchable;
519:
520: public boolean writeable = true;
521:
522: public boolean definiteWritable = true;
523:
524: public String columnLabel;
525:
526: public String columnName;
527:
528: public String schemaName = EMPTY_STRING;
529:
530: public String colTypeName;
531:
532: public int colType;
533:
534: public int nullable;
535:
536: public int columnDisplaySize;
537:
538: public int precision;
539:
540: public int scale;
541:
542: public String tableName = EMPTY_STRING;
543:
544: public String catalogName = EMPTY_STRING;
545: }
546: }
|