© 2002 BEA Systems, Inc.


com.bea.p13n.content.document.ref.loader
Class ColumnSizes

java.lang.Object
  |
  +--com.bea.p13n.content.document.ref.loader.ColumnSizes

public class ColumnSizes
extends java.lang.Object

Utility class to help determine JDBC table column sizes and to truncate data for inserting.

The general usage of this class should be truncate data fields before sending them to the database to avoid extraneous errors when data truncation would be a valid solution. For example,

 String id = request.getParameter("id");
 String name = request.getParameter("name");
 String value = request.getParameter("value");
 Connection con =
     DriverManager.getConnection("jdbc:weblogic:jts:commercePool");
 String sql = "INSERT INTO MYTABLE (ID, NAME, VALUE) VALUES (?, ?, ?)";
 PreparedStatement stmt = con.prepareStatment(sql);
 id = ColumnSizes.truncate(id, con, "MYTABLE", "ID");
 stmt.setString(1, id);
 name = ColumnSizes.truncate(name, con, "MYTABLE", "NAME");
 stmt.setString(2, name);
 value = ColumnSizes.truncate(value, con, "MYTABLE", "VALUE");
 stmt.setString(3, value);
 stmt.executeUpdate();
 stmt.close();
 con.close();
 

Additionally, you can use the getColumnInfo() method to determine the size of a table column. In this manner, you could warn the user about the max size or about data truncation.
When determining the size of columns, the following logic is used:

  1. If the property "p13n.jdbc.maxsize.<table>.<column>" in the loader.properties file is set to a numeric value, then it is assumed the column is a TEXT column with that maximum length.
  2. If the property "p13n.jdbc.maxsize.checkMetaData" in the loader.properties file is set to "true" and the specified Connection is not null, then the Connection's DatabaseMetaData will be used to determine the column information.
  3. If the column is not found or "p13n.jdbc.maxsize.checkMetaData" is not "true", then null is returned.
Additionally, the column information is cached to avoid extraneous database activity. Therefore, if the tables in the database are altered, the server will need to be restarted.

Note: Setting p13n.jdbc.maxsize.checkMetaData to true will cause the java.sql.DatabaseMetaData.getColumns() method to be invoked. Some JDBC drivers do not support this method correctly. Some Type 2 drivers might core dump on this method call.

Since:
3.1.0

Inner Class Summary
static class ColumnSizes.ColumnInfo
          Object describing a column.
 
Field Summary
static int BOOLEAN
          The constant for a boolean type (0).
static boolean CHECK_META_DATA
          Should we check with the DatabaseMetaData for getting column sizes.
static int DATETIME
          The constant for a datetime type (4).
static int FLOAT
          The constant for the floating-point numeric type (2).
static int INTEGER
          The constant for a numeric type (1).
static int MULTI_VALUED
          The constant for a multivalued type (6).
static int TEXT
          The constant for a text type (3).
static int USER_DEFINED
          The constant for a user defined type (5).
 
Constructor Summary
ColumnSizes()
           
 
Method Summary
static int fromSQLType(int sqlType)
          Convert a java.sql.Types constant into one of ours.
static ColumnSizes.ColumnInfo getColumnInfo(java.sql.Connection con, java.lang.String table, java.lang.String column)
          Get the ColumnInfo for the specified column.
static java.lang.String getTypeName(int type)
          Get the default descriptive type name.
static java.lang.Object truncate(java.lang.Object in, java.sql.Connection con, java.lang.String table, java.lang.String column)
          Truncate the specified object to the size of the specified table/column.
static java.lang.String truncate(java.lang.String in, java.sql.Connection con, java.lang.String table, java.lang.String column)
          Truncate the specified String to the size of the specified table/column.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BOOLEAN

public static final int BOOLEAN
The constant for a boolean type (0).

INTEGER

public static final int INTEGER
The constant for a numeric type (1).

FLOAT

public static final int FLOAT
The constant for the floating-point numeric type (2).

TEXT

public static final int TEXT
The constant for a text type (3).

DATETIME

public static final int DATETIME
The constant for a datetime type (4).

USER_DEFINED

public static final int USER_DEFINED
The constant for a user defined type (5).

MULTI_VALUED

public static final int MULTI_VALUED
The constant for a multivalued type (6).

CHECK_META_DATA

public static boolean CHECK_META_DATA
Should we check with the DatabaseMetaData for getting column sizes.

This is initialized from the "p13n.jdbc.maxsize.checkMetaData" property in the weblogiccommerce.properties.

Constructor Detail

ColumnSizes

public ColumnSizes()
Method Detail

getColumnInfo

public static ColumnSizes.ColumnInfo getColumnInfo(java.sql.Connection con,
                                                   java.lang.String table,
                                                   java.lang.String column)
                                            throws java.sql.SQLException
Get the ColumnInfo for the specified column.

If the column information is not found in the cache, it will be loaded (see class comments for details on how it is loaded).

Parameters:
con - the connection to use, null to not.
table - the table name.
column - the column name.
Returns:
the ColumnInfo or null if the column doesn't exist.
Throws:
java.sql.SQLException - if the information cannot be loaded.

truncate

public static java.lang.Object truncate(java.lang.Object in,
                                        java.sql.Connection con,
                                        java.lang.String table,
                                        java.lang.String column)
Truncate the specified object to the size of the specified table/column.

truncate

public static java.lang.String truncate(java.lang.String in,
                                        java.sql.Connection con,
                                        java.lang.String table,
                                        java.lang.String column)
Truncate the specified String to the size of the specified table/column.

See Also:
truncate(java.lang.Object, java.sql.Connection, java.lang.String, java.lang.String)

fromSQLType

public static int fromSQLType(int sqlType)
Convert a java.sql.Types constant into one of ours.

Parameters:
sqlType - value from java.sql.Types.
Returns:
the corresponding attribute type (BOOLEAN, NUMERIC, TEXT, DATETIME, MULTI_VALUED, or USER_DEFINED).

getTypeName

public static java.lang.String getTypeName(int type)
Get the default descriptive type name.

© 2002 BEA Systems, Inc.

Copyright © 2002 BEA Systems, Inc. All Rights Reserved