Skip to content

Commit a35a681

Browse files
committed
The getColumns() method in DataBaseMetaData.java returns a column size
of -1 for varchar's. From: CNT systemen BV <cntsys@cistron.nl>
1 parent 0e1a352 commit a35a681

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/interfaces/jdbc/postgresql/DatabaseMetaData.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
3535
static final int iBoolOid = 16; // OID for bool
3636
static final int iInt2Oid = 21; // OID for int2
3737
static final int iInt4Oid = 23; // OID for int4
38+
static final int VARHDRSZ = 4; // length for int4
3839

3940
public DatabaseMetaData(Connection conn)
4041
{
@@ -1848,13 +1849,14 @@ public java.sql.ResultSet getColumns(String catalog, String schemaPattern, Strin
18481849
f[17] = new Field(connection, new String("IS_NULLABLE"), iVarcharOid, 32);
18491850

18501851
// Now form the query
1851-
r = connection.ExecSQL("select a.oid,c.relname,a.attname,a.atttypid,a.attnum,a.attnotnull,a.attlen from pg_class c, pg_attribute a where a.attrelid=c.oid and c.relname like '"+tableNamePattern+"' and a.attname like '"+columnNamePattern+"' and a.attnum>0 order by c.relname,a.attnum");
1852+
r = connection.ExecSQL("select a.oid,c.relname,a.attname,a.atttypid,a.attnum,a.attnotnull,a.attlen,a.atttypmod from pg_class c, pg_attribute a where a.attrelid=c.oid and c.relname like '"+tableNamePattern+"' and a.attname like '"+columnNamePattern+"' and a.attnum>0 order by c.relname,a.attnum");
18521853

18531854
while(r.next()) {
18541855
byte[][] tuple = new byte[18][0];
18551856

18561857
String name = r.getString(1);
18571858
String remarks = new String("no remarks");
1859+
String columnSize;
18581860

18591861
// Fetch the description for the table (if any)
18601862
ResultSet dr = connection.ExecSQL("select description from pg_description where objoid="+r.getInt(1));
@@ -1875,8 +1877,16 @@ public java.sql.ResultSet getColumns(String catalog, String schemaPattern, Strin
18751877
dr.close();
18761878
tuple[4] = Integer.toString(Field.getSQLType(typname)).getBytes(); // Data type
18771879
tuple[5] = typname.getBytes(); // Type name
1878-
1879-
tuple[6] = r.getString(7).getBytes(); // Column size
1880+
1881+
// Looking at the psql source,
1882+
// I think the length of a varchar as specified when the table was created
1883+
// should be extracted from atttypmod which contains this length + sizeof(int32)
1884+
if (typname.equals("bpchar") || typname.equals("varchar")) {
1885+
int atttypmod = r.getInt(8);
1886+
columnSize = Integer.toString(atttypmod != -1 ? atttypmod - VARHDRSZ : 0);
1887+
} else
1888+
columnSize = r.getString(7);
1889+
tuple[6] = columnSize.getBytes(); // Column size
18801890

18811891
tuple[7] = null; // Buffer length
18821892

0 commit comments

Comments
 (0)