KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > MSSQLServerAdapter


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: MSSQLServerAdapter.java,v 1.5 2003/03/17 07:02:52 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import java.math.BigInteger JavaDoc;
14 import java.sql.Connection JavaDoc;
15 import java.sql.DatabaseMetaData JavaDoc;
16 import java.sql.ResultSet JavaDoc;
17 import java.sql.SQLException JavaDoc;
18 import java.sql.Statement JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import javax.jdo.JDOFatalDataStoreException;
21
22
23 /**
24  * Provides methods for adapting SQL language elements to the Microsoft SQL
25  * Server database.
26  *
27  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
28  * @version $Revision: 1.5 $
29  *
30  * @see DatabaseAdapter
31  */

32
33 class MSSQLServerAdapter extends DatabaseAdapter
34 {
35     /**
36      * Constructs a Microsoft SQL Server adapter based on the given JDBC
37      * metadata.
38      *
39      * @param metadata the database metadata.
40      */

41
42     protected MSSQLServerAdapter(DatabaseMetaData JavaDoc metadata)
43     {
44         super(metadata);
45     }
46
47     public String JavaDoc getVendorID()
48     {
49         return "sqlserver";
50     }
51
52     public String JavaDoc getSchemaName(Connection JavaDoc conn) throws SQLException JavaDoc
53     {
54         /*
55          * As of version 7 there was no equivalent to the concept of "schema"
56          * in SQL Server. For DatabaseMetaData functions that include
57          * SCHEMA_NAME drivers usually return the user name that owns the table.
58          *
59          * So the default ProbeTable method for determining the current schema
60          * just ends up returning the current user. If we then use that name in
61          * performing metadata queries our results may get filtered down to just
62          * objects owned by that user. So instead we report the schema name as
63          * null which should cause those queries to return everything.
64          */

65         return null;
66     }
67
68     public boolean supportsBooleanComparison()
69     {
70         return false;
71     }
72
73     public boolean supportsDeferredConstraints()
74     {
75         return false;
76     }
77
78     public ColumnInfo newColumnInfo(ResultSet JavaDoc rs)
79     {
80         return new MSSQLServerColumnInfo(rs);
81     }
82
83     public TableExpression newTableExpression(QueryStatement qs, Table table, SQLIdentifier rangeVar)
84     {
85         return new TableExprAsJoins(qs, table, rangeVar);
86     }
87
88     public TypeInfo newTypeInfo(ResultSet JavaDoc rs)
89     {
90         TypeInfo ti = new TypeInfo(rs);
91
92         /*
93          * Discard the tinyint type because it doesn't support negative values.
94          */

95         if (ti.typeName.toLowerCase().startsWith("tinyint"))
96             return null;
97
98         return ti;
99     }
100     public String JavaDoc getDropTableStatement(BaseTable table)
101     {
102         return "DROP TABLE " + table.getName();
103     }
104
105     public NumericExpression lengthMethod(CharacterExpression str)
106     {
107         ArrayList JavaDoc args = new ArrayList JavaDoc();
108         args.add(str);
109
110         return new NumericExpression("LEN", args);
111     }
112
113     public CharacterExpression substringMethod(CharacterExpression str,
114                                                NumericExpression begin)
115     {
116         ArrayList JavaDoc args = new ArrayList JavaDoc();
117         args.add(str);
118         args.add(begin.add(new IntegerLiteral(str.getQueryStatement(), BigInteger.ONE)));
119         args.add(lengthMethod(str).sub(begin));
120
121         return new CharacterExpression("SUBSTRING", args);
122     }
123
124     public CharacterExpression substringMethod(CharacterExpression str,
125                                                NumericExpression begin,
126                                                NumericExpression end)
127     {
128         ArrayList JavaDoc args = new ArrayList JavaDoc();
129         args.add(str);
130         args.add(begin.add(new IntegerLiteral(str.getQueryStatement(), BigInteger.ONE)));
131         args.add(end.sub(begin));
132
133         return new CharacterExpression("SUBSTRING", args);
134     }
135 }
136
Popular Tags