001: /*-------------------------------------------------------------------------
002: *
003: * Copyright (c) 2003-2005, PostgreSQL Global Development Group
004: *
005: * IDENTIFICATION
006: * $PostgreSQL: pgjdbc/org/postgresql/core/Field.java,v 1.11 2005/12/03 21:44:08 jurka Exp $
007: *
008: *-------------------------------------------------------------------------
009: */
010: package org.postgresql.core;
011:
012: import java.sql.*;
013:
014: /*
015: */
016: public class Field {
017: //The V3 protocol defines two constants for the format of data
018: public static final int TEXT_FORMAT = 0;
019: public static final int BINARY_FORMAT = 1;
020:
021: private final int length; // Internal Length of this field
022: private final int oid; // OID of the type
023: private final int mod; // type modifier of this field
024: private final String columnLabel; // Column label
025: private String columnName; // Column name; null if undetermined
026: private Integer nullable; // Is this column nullable? null if undetermined.
027: private Boolean autoIncrement; // Is this column automatically numbered?
028:
029: private int format = TEXT_FORMAT; // In the V3 protocol each field has a format
030: // 0 = text, 1 = binary
031: // In the V2 protocol all fields in a
032: // binary cursor are binary and all
033: // others are text
034:
035: private final int tableOid; // OID of table ( zero if no table )
036: private final int positionInTable;
037:
038: // cache-fields
039:
040: /*
041: * Construct a field based on the information fed to it.
042: *
043: * @param name the name (column name and label) of the field
044: * @param oid the OID of the field
045: * @param len the length of the field
046: */
047: public Field(String name, int oid, int length, int mod) {
048: this (name, name, oid, length, mod, 0, 0);
049: }
050:
051: /*
052: * Constructor without mod parameter.
053: *
054: * @param name the name (column name and label) of the field
055: * @param oid the OID of the field
056: * @param len the length of the field
057: */
058: public Field(String name, int oid) {
059: this (name, oid, 0, -1);
060: }
061:
062: /*
063: * Construct a field based on the information fed to it.
064: *
065: * @param columnLabel the column label of the field
066: * @param columnName the column label the name of the field
067: * @param oid the OID of the field
068: * @param length the length of the field
069: * @param tableOid the OID of the columns' table
070: * @param positionInTable the position of column in the table (first column is 1, second column is 2, etc...)
071: */
072: public Field(String columnLabel, String columnName, int oid,
073: int length, int mod, int tableOid, int positionInTable) {
074: this .columnLabel = columnLabel;
075: this .columnName = columnName;
076: this .oid = oid;
077: this .length = length;
078: this .mod = mod;
079: this .tableOid = tableOid;
080: this .positionInTable = positionInTable;
081: }
082:
083: /*
084: * @return the oid of this Field's data type
085: */
086: public int getOID() {
087: return oid;
088: }
089:
090: /*
091: * @return the mod of this Field's data type
092: */
093: public int getMod() {
094: return mod;
095: }
096:
097: /*
098: * @return the column label of this Field's data type
099: */
100: public String getColumnLabel() {
101: return columnLabel;
102: }
103:
104: /*
105: * @return the length of this Field's data type
106: */
107: public int getLength() {
108: return length;
109: }
110:
111: /*
112: * @return the format of this Field's data (text=0, binary=1)
113: */
114: public int getFormat() {
115: return format;
116: }
117:
118: /*
119: * @param format the format of this Field's data (text=0, binary=1)
120: */
121: public void setFormat(int format) {
122: this .format = format;
123: }
124:
125: /*
126: * @return the columns' table oid, zero if no oid available
127: */
128: public int getTableOid() {
129: return tableOid;
130: }
131:
132: public int getPositionInTable() {
133: return positionInTable;
134: }
135:
136: public int getNullable(Connection con) throws SQLException {
137: if (nullable != null)
138: return nullable.intValue();
139:
140: if (tableOid == 0 || positionInTable == 0) {
141: nullable = new Integer(
142: ResultSetMetaData.columnNullableUnknown);
143: return nullable.intValue();
144: }
145:
146: ResultSet res = null;
147: PreparedStatement ps = null;
148: try {
149: ps = con
150: .prepareStatement("SELECT attnotnull FROM pg_catalog.pg_attribute WHERE attrelid = ? AND attnum = ?;");
151: ps.setInt(1, tableOid);
152: ps.setInt(2, positionInTable);
153: res = ps.executeQuery();
154:
155: int nullResult = ResultSetMetaData.columnNullableUnknown;
156: if (res.next())
157: nullResult = res.getBoolean(1) ? ResultSetMetaData.columnNoNulls
158: : ResultSetMetaData.columnNullable;
159:
160: nullable = new Integer(nullResult);
161: return nullResult;
162: } finally {
163: if (res != null)
164: res.close();
165: if (ps != null)
166: ps.close();
167: }
168: }
169:
170: public boolean getAutoIncrement(Connection con) throws SQLException {
171: if (autoIncrement != null)
172: return autoIncrement.booleanValue();
173:
174: if (tableOid == 0 || positionInTable == 0) {
175: autoIncrement = Boolean.FALSE;
176: return autoIncrement.booleanValue();
177: }
178:
179: ResultSet res = null;
180: PreparedStatement ps = null;
181: try {
182: final String sql = "SELECT def.adsrc FROM pg_catalog.pg_class c "
183: + "JOIN pg_catalog.pg_attribute a ON (a.attrelid=c.oid) "
184: + "LEFT JOIN pg_catalog.pg_attrdef def ON (a.attrelid=def.adrelid AND a.attnum = def.adnum) "
185: + "WHERE c.oid = ? and a.attnum = ? AND def.adsrc LIKE '%nextval(%'";
186:
187: ps = con.prepareStatement(sql);
188:
189: ps.setInt(1, tableOid);
190: ps.setInt(2, positionInTable);
191: res = ps.executeQuery();
192:
193: if (res.next()) {
194: autoIncrement = Boolean.TRUE;
195: } else {
196: autoIncrement = Boolean.FALSE;
197: }
198: return autoIncrement.booleanValue();
199:
200: } finally {
201: if (res != null)
202: res.close();
203: if (ps != null)
204: ps.close();
205: }
206: }
207:
208: public String getColumnName(Connection con) throws SQLException {
209: if (columnName != null)
210: return columnName;
211:
212: columnName = "";
213: if (tableOid == 0 || positionInTable == 0) {
214: return columnName;
215: }
216:
217: ResultSet res = null;
218: PreparedStatement ps = null;
219: try {
220: ps = con
221: .prepareStatement("SELECT attname FROM pg_catalog.pg_attribute WHERE attrelid = ? AND attnum = ?");
222: ps.setInt(1, tableOid);
223: ps.setInt(2, positionInTable);
224: res = ps.executeQuery();
225: if (res.next())
226: columnName = res.getString(1);
227:
228: return columnName;
229: } finally {
230: if (res != null)
231: res.close();
232: if (ps != null)
233: ps.close();
234: }
235: }
236: }
|