001: /*
002: * Copyright 2002 (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: DB2Adapter.java,v 1.4 2003/02/26 00:22:40 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import java.math.BigInteger;
014: import java.sql.Connection;
015: import java.sql.DatabaseMetaData;
016: import java.sql.ResultSet;
017: import java.sql.Statement;
018: import java.sql.SQLException;
019: import java.util.ArrayList;
020: import javax.jdo.JDOFatalDataStoreException;
021:
022: /**
023: * Provides methods for adapting SQL language elements to the DB2 database.
024: *
025: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
026: * @version $Revision: 1.4 $
027: *
028: * @see DatabaseAdapter
029: */
030:
031: class DB2Adapter extends DatabaseAdapter {
032: /**
033: * Constructs a DB2 adapter based on the given JDBC metadata.
034: *
035: * @param metadata the database metadata.
036: */
037:
038: protected DB2Adapter(DatabaseMetaData metadata) {
039: super (metadata);
040:
041: maxConstraintNameLength = 18;
042: maxIndexNameLength = 18;
043: }
044:
045: public String getVendorID() {
046: return "db2";
047: }
048:
049: public String getSchemaName(Connection conn) throws SQLException {
050: Statement stmt = conn.createStatement();
051:
052: try {
053: String stmtText = "VALUES (CURRENT SCHEMA)";
054: ResultSet rs = stmt.executeQuery(stmtText);
055:
056: try {
057: if (!rs.next())
058: throw new JDOFatalDataStoreException(
059: "No result returned from " + stmtText);
060:
061: return rs.getString(1).trim();
062: } finally {
063: rs.close();
064: }
065: } finally {
066: stmt.close();
067: }
068: }
069:
070: public boolean supportsBooleanComparison() {
071: return false;
072: }
073:
074: public boolean supportsDeferredConstraints() {
075: return false;
076: }
077:
078: public boolean supportsNullsInCandidateKeys() {
079: return false;
080: }
081:
082: public ColumnInfo newColumnInfo(ResultSet rs) {
083: return new DB2ColumnInfo(rs);
084: }
085:
086: public TableExpression newTableExpression(QueryStatement qs,
087: Table table, SQLIdentifier rangeVar) {
088: return new TableExprAsSubquery(qs, table, rangeVar);
089: }
090:
091: public int getUnlimitedLengthPrecisionValue(TypeInfo typeInfo) {
092: if (typeInfo.dataType == java.sql.Types.BLOB
093: || typeInfo.dataType == java.sql.Types.CLOB)
094: return 1 << 30;
095: else
096: return super .getUnlimitedLengthPrecisionValue(typeInfo);
097: }
098:
099: public String getDropTableStatement(BaseTable table) {
100: return "DROP TABLE " + table.getName();
101: }
102:
103: public NumericExpression lengthMethod(CharacterExpression str) {
104: ArrayList args = new ArrayList();
105: args.add(str);
106:
107: return new NumericExpression("LENGTH", args);
108: }
109:
110: public CharacterExpression substringMethod(CharacterExpression str,
111: NumericExpression begin) {
112: ArrayList args = new ArrayList();
113: args.add(str);
114: args.add(begin.add(new IntegerLiteral(str.getQueryStatement(),
115: BigInteger.ONE)));
116:
117: return new CharacterExpression("SUBSTR", args);
118: }
119:
120: public CharacterExpression substringMethod(CharacterExpression str,
121: NumericExpression begin, NumericExpression end) {
122: ArrayList args = new ArrayList();
123: args.add(str);
124: args.add(begin.add(new IntegerLiteral(str.getQueryStatement(),
125: BigInteger.ONE)));
126: args.add(end.sub(begin));
127:
128: return new CharacterExpression("SUBSTR", args);
129: }
130: }
|