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: MSSQLServerAdapter.java,v 1.5 2003/03/17 07:02:52 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.SQLException;
018: import java.sql.Statement;
019: import java.util.ArrayList;
020: import javax.jdo.JDOFatalDataStoreException;
021:
022: /**
023: * Provides methods for adapting SQL language elements to the Microsoft SQL
024: * Server database.
025: *
026: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
027: * @version $Revision: 1.5 $
028: *
029: * @see DatabaseAdapter
030: */
031:
032: class MSSQLServerAdapter extends DatabaseAdapter {
033: /**
034: * Constructs a Microsoft SQL Server adapter based on the given JDBC
035: * metadata.
036: *
037: * @param metadata the database metadata.
038: */
039:
040: protected MSSQLServerAdapter(DatabaseMetaData metadata) {
041: super (metadata);
042: }
043:
044: public String getVendorID() {
045: return "sqlserver";
046: }
047:
048: public String getSchemaName(Connection conn) throws SQLException {
049: /*
050: * As of version 7 there was no equivalent to the concept of "schema"
051: * in SQL Server. For DatabaseMetaData functions that include
052: * SCHEMA_NAME drivers usually return the user name that owns the table.
053: *
054: * So the default ProbeTable method for determining the current schema
055: * just ends up returning the current user. If we then use that name in
056: * performing metadata queries our results may get filtered down to just
057: * objects owned by that user. So instead we report the schema name as
058: * null which should cause those queries to return everything.
059: */
060: return null;
061: }
062:
063: public boolean supportsBooleanComparison() {
064: return false;
065: }
066:
067: public boolean supportsDeferredConstraints() {
068: return false;
069: }
070:
071: public ColumnInfo newColumnInfo(ResultSet rs) {
072: return new MSSQLServerColumnInfo(rs);
073: }
074:
075: public TableExpression newTableExpression(QueryStatement qs,
076: Table table, SQLIdentifier rangeVar) {
077: return new TableExprAsJoins(qs, table, rangeVar);
078: }
079:
080: public TypeInfo newTypeInfo(ResultSet rs) {
081: TypeInfo ti = new TypeInfo(rs);
082:
083: /*
084: * Discard the tinyint type because it doesn't support negative values.
085: */
086: if (ti.typeName.toLowerCase().startsWith("tinyint"))
087: return null;
088:
089: return ti;
090: }
091:
092: public String getDropTableStatement(BaseTable table) {
093: return "DROP TABLE " + table.getName();
094: }
095:
096: public NumericExpression lengthMethod(CharacterExpression str) {
097: ArrayList args = new ArrayList();
098: args.add(str);
099:
100: return new NumericExpression("LEN", args);
101: }
102:
103: public CharacterExpression substringMethod(CharacterExpression str,
104: NumericExpression begin) {
105: ArrayList args = new ArrayList();
106: args.add(str);
107: args.add(begin.add(new IntegerLiteral(str.getQueryStatement(),
108: BigInteger.ONE)));
109: args.add(lengthMethod(str).sub(begin));
110:
111: return new CharacterExpression("SUBSTRING", args);
112: }
113:
114: public CharacterExpression substringMethod(CharacterExpression str,
115: NumericExpression begin, NumericExpression end) {
116: ArrayList args = new ArrayList();
117: args.add(str);
118: args.add(begin.add(new IntegerLiteral(str.getQueryStatement(),
119: BigInteger.ONE)));
120: args.add(end.sub(begin));
121:
122: return new CharacterExpression("SUBSTRING", args);
123: }
124: }
|