001: /*
002: * Copyright 2003 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: PostgreSQLAdapter.java,v 1.4 2003/10/20 17:50:43 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import java.sql.Connection;
014: import java.sql.DatabaseMetaData;
015: import java.sql.ResultSet;
016: import java.sql.SQLException;
017: import java.sql.Types;
018: import javax.jdo.JDODataStoreException;
019: import javax.jdo.JDOFatalDataStoreException;
020:
021: /**
022: * Provides methods for adapting SQL language elements to the PostgreSQL
023: * database.
024: *
025: * @author <a href="mailto:slevente@yahoo.com">Levente Sántha</a>
026: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
027: */
028:
029: public class PostgreSQLAdapter extends DatabaseAdapter {
030: public PostgreSQLAdapter(DatabaseMetaData metadata) {
031: super (metadata);
032:
033: if (databaseMajorVersion < 7)
034: throw new JDODataStoreException("PostgreSQL version is "
035: + databaseMajorVersion + '.' + databaseMinorVersion
036: + ", 7.0 or later required");
037: else if (databaseMajorVersion == 7) {
038: if (databaseMinorVersion <= 2) {
039: /*
040: * The driver correctly reports the max table name length as 32.
041: * However, constraint names are apparently limited to 31. In
042: * this case we get better looking names by simply treating them
043: * all as limited to 31.
044: */
045: --maxTableNameLength;
046: --maxConstraintNameLength;
047: --maxIndexNameLength;
048: }
049: }
050: }
051:
052: public String getVendorID() {
053: return "postgresql";
054: }
055:
056: public SQLState getSQLState(SQLException se) {
057: String state = se.getSQLState();
058:
059: if (state == null)
060: return null;
061:
062: try {
063: return new PostgreSQLSQLState(state);
064: } catch (IllegalArgumentException e) {
065: return null;
066: }
067: }
068:
069: public TableExpression newTableExpression(QueryStatement qs,
070: Table table, SQLIdentifier rangeVar) {
071: return new TableExprAsSubquery(qs, table, rangeVar);
072: }
073:
074: public TypeInfo newTypeInfo(ResultSet rs) {
075: TypeInfo ti = new PostgreSQLTypeInfo(rs);
076:
077: /*
078: * Since PostgreSQL supports many user defined data types and uses
079: * many type aliases the default methods have trouble finding the
080: * right associations between JDBC and PostgreSQL data types. We
081: * filter the returned type info to be sure we use the appropriate
082: * base PostgreSQL types for the important JDBC types.
083: */
084:
085: switch (ti.dataType) {
086: case Types.BIT:
087: if (!ti.typeName.equalsIgnoreCase("bool"))
088: return null;
089: break;
090: case Types.CHAR:
091: if (!ti.typeName.equalsIgnoreCase("char"))
092: return null;
093: break;
094: case Types.SMALLINT:
095: if (!ti.typeName.equalsIgnoreCase("int2"))
096: return null;
097: break;
098: case Types.INTEGER:
099: if (!ti.typeName.equalsIgnoreCase("int4"))
100: return null;
101: break;
102: case Types.BIGINT:
103: if (!ti.typeName.equalsIgnoreCase("int8"))
104: return null;
105: break;
106: case Types.REAL:
107: if (!ti.typeName.equalsIgnoreCase("float4"))
108: return null;
109: break;
110: case Types.DOUBLE:
111: if (!ti.typeName.equalsIgnoreCase("float8"))
112: return null;
113: break;
114: case Types.NUMERIC:
115: if (!ti.typeName.equalsIgnoreCase("numeric"))
116: return null;
117: break;
118: case Types.DATE:
119: if (!ti.typeName.equalsIgnoreCase("date"))
120: return null;
121: break;
122: case Types.TIME:
123: if (!ti.typeName.equalsIgnoreCase("time"))
124: return null;
125: break;
126: case Types.TIMESTAMP:
127: if (!ti.typeName.equalsIgnoreCase("timestamptz"))
128: return null;
129: break;
130: case Types.VARCHAR:
131: if (!ti.typeName.equalsIgnoreCase("varchar"))
132: return null;
133: break;
134: case Types.OTHER:
135: /* Discard the boatload of Types.OTHER types. */
136: return null;
137: }
138:
139: return ti;
140: }
141:
142: public ColumnInfo newColumnInfo(ResultSet rs) {
143: return new PostgreSQLColumnInfo(rs);
144: }
145:
146: public ForeignKeyInfo newForeignKeyInfo(ResultSet rs) {
147: return new PostgreSQLForeignKeyInfo(rs);
148: }
149:
150: public boolean supportsAlterTableDropConstraint() {
151: return false;
152: }
153:
154: public String getDropTableStatement(BaseTable table) {
155: /* DROP TABLE t CASCADE is supported beginning in 7.3 */
156: if (databaseMajorVersion == 7 && databaseMinorVersion < 3)
157: return "DROP TABLE " + table.getName();
158: else
159: return "DROP TABLE " + table.getName() + " CASCADE";
160: }
161: }
|