1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
/*-------------------------------------------------------------------------
*
* Copyright (c) 2003-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgjdbc/org/postgresql/core/Field.java,v 1.13 2009/03/12 03:59:50 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.core;
import java.sql.*;
/*
*/
public class Field
{
//The V3 protocol defines two constants for the format of data
public static final int TEXT_FORMAT = 0;
public static final int BINARY_FORMAT = 1;
private final int length; // Internal Length of this field
private final int oid; // OID of the type
private final int mod; // type modifier of this field
private final String columnLabel; // Column label
private String columnName; // Column name; null if undetermined
private Integer nullable; // Is this column nullable? null if undetermined.
private Boolean autoIncrement; // Is this column automatically numbered?
private int format = TEXT_FORMAT; // In the V3 protocol each field has a format
// 0 = text, 1 = binary
// In the V2 protocol all fields in a
// binary cursor are binary and all
// others are text
private final int tableOid; // OID of table ( zero if no table )
private final int positionInTable;
// cache-fields
/*
* Construct a field based on the information fed to it.
*
* @param name the name (column name and label) of the field
* @param oid the OID of the field
* @param len the length of the field
*/
public Field(String name, int oid, int length, int mod)
{
this(name, name, oid, length, mod, 0, 0);
}
/*
* Constructor without mod parameter.
*
* @param name the name (column name and label) of the field
* @param oid the OID of the field
* @param len the length of the field
*/
public Field(String name, int oid)
{
this(name, oid, 0, -1);
}
/*
* Construct a field based on the information fed to it.
*
* @param columnLabel the column label of the field
* @param columnName the column label the name of the field
* @param oid the OID of the field
* @param length the length of the field
* @param tableOid the OID of the columns' table
* @param positionInTable the position of column in the table (first column is 1, second column is 2, etc...)
*/
public Field(String columnLabel, String columnName, int oid, int length, int mod, int tableOid, int positionInTable)
{
this.columnLabel = columnLabel;
this.columnName = columnName;
this.oid = oid;
this.length = length;
this.mod = mod;
this.tableOid = tableOid;
this.positionInTable = positionInTable;
}
/*
* @return the oid of this Field's data type
*/
public int getOID()
{
return oid;
}
/*
* @return the mod of this Field's data type
*/
public int getMod()
{
return mod;
}
/*
* @return the column label of this Field's data type
*/
public String getColumnLabel()
{
return columnLabel;
}
/*
* @return the length of this Field's data type
*/
public int getLength()
{
return length;
}
/*
* @return the format of this Field's data (text=0, binary=1)
*/
public int getFormat()
{
return format;
}
/*
* @param format the format of this Field's data (text=0, binary=1)
*/
public void setFormat(int format)
{
this.format = format;
}
/*
* @return the columns' table oid, zero if no oid available
*/
public int getTableOid()
{
return tableOid;
}
public int getPositionInTable()
{
return positionInTable;
}
public int getNullable(Connection con) throws SQLException
{
if (nullable != null)
return nullable.intValue();
if (tableOid == 0 || positionInTable == 0)
{
nullable = new Integer(ResultSetMetaData.columnNullableUnknown);
return nullable.intValue();
}
ResultSet res = null;
PreparedStatement ps = null;
try
{
ps = con.prepareStatement("SELECT attnotnull FROM pg_catalog.pg_attribute WHERE attrelid = ? AND attnum = ?;");
ps.setInt(1, tableOid);
ps.setInt(2, positionInTable);
res = ps.executeQuery();
int nullResult = ResultSetMetaData.columnNullableUnknown;
if (res.next())
nullResult = res.getBoolean(1) ? ResultSetMetaData.columnNoNulls : ResultSetMetaData.columnNullable;
nullable = new Integer(nullResult);
return nullResult;
}
finally
{
if (res != null)
res.close();
if (ps != null)
ps.close();
}
}
public boolean getAutoIncrement(Connection con) throws SQLException
{
if (autoIncrement != null)
return autoIncrement.booleanValue();
if (tableOid == 0 || positionInTable == 0)
{
autoIncrement = Boolean.FALSE;
return autoIncrement.booleanValue();
}
ResultSet res = null;
PreparedStatement ps = null;
try
{
final String sql = "SELECT 1 "
+ " FROM pg_catalog.pg_attrdef "
+ " WHERE adrelid = ? AND adnum = ? "
+ " AND pg_catalog.pg_get_expr(adbin, adrelid) "
+ " LIKE '%nextval(%'";
ps = con.prepareStatement(sql);
ps.setInt(1, tableOid);
ps.setInt(2, positionInTable);
res = ps.executeQuery();
if (res.next())
{
autoIncrement = Boolean.TRUE;
}
else
{
autoIncrement = Boolean.FALSE;
}
return autoIncrement.booleanValue();
}
finally
{
if (res != null)
res.close();
if (ps != null)
ps.close();
}
}
public String getColumnName(Connection con) throws SQLException
{
if (columnName != null)
return columnName;
columnName = "";
if (tableOid == 0 || positionInTable == 0)
{
return columnName;
}
ResultSet res = null;
PreparedStatement ps = null;
try
{
ps = con.prepareStatement("SELECT attname FROM pg_catalog.pg_attribute WHERE attrelid = ? AND attnum = ?");
ps.setInt(1, tableOid);
ps.setInt(2, positionInTable);
res = ps.executeQuery();
if (res.next())
columnName = res.getString(1);
return columnName;
}
finally
{
if (res != null)
res.close();
if (ps != null)
ps.close();
}
}
}
|