KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > platforms > PlatformMsAccessImpl


1 package org.apache.ojb.broker.platforms;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.math.BigDecimal JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.Statement JavaDoc;
24 import java.sql.Types JavaDoc;
25
26 import org.apache.ojb.broker.query.LikeCriteria;
27 import org.apache.ojb.broker.util.logging.LoggerFactory;
28
29 /**
30  * @author <a HREF="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
31  * @version $Id: PlatformMsAccessImpl.java,v 1.11.2.1 2005/12/21 22:26:39 tomdz Exp $
32  */

33 public class PlatformMsAccessImpl extends PlatformDefaultImpl
34 {
35     /**
36      * @see Platform#setObjectForStatement(PreparedStatement, int, Object, int)
37      */

38     public void setObjectForStatement(PreparedStatement JavaDoc ps, int index, Object JavaDoc value, int sqlType)
39         throws SQLException JavaDoc
40     {
41         if (sqlType == Types.DECIMAL)
42         {
43             ps.setBigDecimal(index, (BigDecimal JavaDoc) value);
44         }
45         else if (sqlType == Types.FLOAT)
46         {
47             // this is because in repository_junit.xml price field is a double in the Article class
48
// but repository maps it to a sql type of FLOAT (any my ODBC/JDBC bridge/driver cannot
49
// cope with this conversion...possibility of a truncation error heer as well...
50
if (value instanceof Double JavaDoc)
51             {
52                 ps.setDouble(index, ((Double JavaDoc) value).doubleValue());
53             }
54             else
55             {
56                 super.setObjectForStatement(ps, index, value, sqlType);
57             }
58         }
59         // patch by Ralph Brandes to allow writing to memo fields
60
else if (sqlType == Types.LONGVARCHAR)
61         {
62             if (value instanceof String JavaDoc)
63             {
64                 String JavaDoc s = (String JavaDoc) value;
65                 // ps.setCharacterStream(index, new StringReader(s), s.length());
66
// for MSACCESS :
67
byte[] bytes = s.getBytes();
68                 ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
69                 ps.setAsciiStream(index, bais, bytes.length);
70             }
71             else
72             {
73                 super.setObjectForStatement(ps, index, value, sqlType);
74             }
75         }
76         // patch by Tino Schöllhorn
77
// Current ODBC-Implementation for Access (I use ODBC 4.0.xxxx) does not
78
// support the conversion of LONG values.
79
// Error is : "Optional feature not implemented"
80
// So I try to pass the LONG-value as an Integer even though it might be possible
81
// that the conversion fails - but I don't think that is an issues with Access anyway.
82
else if (value instanceof Long JavaDoc)
83         {
84             ps.setInt(index,((Long JavaDoc)value).intValue());
85         }
86         else
87         {
88             super.setObjectForStatement(ps, index, value, sqlType);
89         }
90     }
91
92     /**
93      * @see Platform#beforeStatementClose(Statement stmt, ResultSet rs)
94      */

95     public void beforeStatementClose(Statement JavaDoc stmt, ResultSet JavaDoc rs) throws PlatformException
96     {
97         if (rs != null)
98         {
99             try
100             {
101                 rs.close();
102             }
103             catch (SQLException JavaDoc e)
104             {
105                 LoggerFactory.getDefaultLogger().warn(
106                     "Resultset closing failed (can be ignored for MsAccess)");
107             }
108         }
109     }
110     
111     /**
112      * Answer the Character for Concatenation
113      */

114     protected String JavaDoc getConcatenationCharacter()
115     {
116         return "&";
117     }
118     
119     /**
120      * @see org.apache.ojb.broker.platforms.Platform#getEscapeClause(org.apache.ojb.broker.query.LikeCriteria)
121      */

122     public String JavaDoc getEscapeClause(LikeCriteria aCriteria)
123     {
124         // TODO: implement ms-access escaping
125
return "";
126     }
127 }
128
Popular Tags