KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.InputStreamReader JavaDoc;
20 import java.io.Reader JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import java.sql.PreparedStatement JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.sql.Types JavaDoc;
25
26 import org.apache.ojb.broker.query.LikeCriteria;
27
28 /**
29  * @version 1.0
30  * @author jakob bräuchi
31  * @version $Id: PlatformMySQLImpl.java,v 1.15.2.1 2005/12/21 22:26:40 tomdz Exp $
32  */

33 public class PlatformMySQLImpl extends PlatformDefaultImpl
34 {
35     private static final String JavaDoc LAST_INSERT = "SELECT LAST_INSERT_ID() FROM ";
36     private static final String JavaDoc LIMIT = " LIMIT 1";
37     
38     /*
39      * @see Platform#setObjectForStatement(PreparedStatement, int, Object, int)
40      */

41     public void setObjectForStatement(PreparedStatement JavaDoc ps, int index, Object JavaDoc value, int sqlType) throws SQLException JavaDoc
42     {
43         switch (sqlType)
44         {
45             case Types.BIT :
46                 ps.setObject(index, value);
47                 break;
48
49             case Types.BLOB :
50             case Types.LONGVARBINARY :
51             case Types.VARBINARY :
52                 if (value instanceof byte[])
53                 {
54                     byte buf[] = (byte[]) value;
55                     ByteArrayInputStream JavaDoc inputStream = new ByteArrayInputStream JavaDoc(buf);
56                     ps.setBinaryStream(index, inputStream, buf.length);
57
58                     break;
59                 }
60
61             case Types.CLOB :
62                 Reader JavaDoc reader = null;
63                 int length = 0;
64
65                 if (value instanceof String JavaDoc)
66                 {
67                     reader = new StringReader JavaDoc((String JavaDoc) value);
68                     length = (((String JavaDoc) value)).length();
69                 }
70                 else if (value instanceof char[])
71                 {
72                     String JavaDoc string = new String JavaDoc((char[])value);
73                     reader = new StringReader JavaDoc(string);
74                     length = string.length();
75                 }
76                 else if (value instanceof byte[])
77                 {
78                     byte buf[] = (byte[]) value;
79                     ByteArrayInputStream JavaDoc inputStream = new ByteArrayInputStream JavaDoc(buf);
80                     reader = new InputStreamReader JavaDoc(inputStream);
81                 }
82
83                 ps.setCharacterStream(index, reader, length);
84                 break;
85
86             default :
87                 super.setObjectForStatement(ps, index, value, sqlType);
88
89         }
90     }
91     /**
92      * Get join syntax type for this RDBMS - one on of the constants from
93      * JoinSyntaxType interface
94      */

95     public byte getJoinSyntaxType()
96     {
97         return SQL92_NOPAREN_JOIN_SYNTAX;
98     }
99
100     public String JavaDoc getLastInsertIdentityQuery(String JavaDoc tableName)
101     {
102         return LAST_INSERT + tableName + LIMIT;
103     }
104
105     /*
106      * (non-Javadoc)
107      *
108      * @see org.apache.ojb.broker.platforms.Platform#addPagingSql(java.lang.StringBuffer)
109      */

110     public void addPagingSql(StringBuffer JavaDoc anSqlString)
111     {
112         anSqlString.append(" LIMIT ?,?");
113     }
114
115     /*
116      * (non-Javadoc)
117      *
118      * @see org.apache.ojb.broker.platforms.Platform#supportsPaging()
119      */

120     public boolean supportsPaging()
121     {
122         return true;
123     }
124
125     /**
126      * @see org.apache.ojb.broker.platforms.Platform#concatenate(java.lang.String[])
127      */

128     public String JavaDoc concatenate(String JavaDoc[] theColumns)
129     {
130         if (theColumns.length == 1)
131         {
132             return theColumns[0];
133         }
134         
135         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
136         
137         buf.append("concat(");
138         for (int i = 0; i < theColumns.length; i++)
139         {
140             if (i > 0)
141             {
142                 buf.append(",");
143             }
144             buf.append(theColumns[i]);
145         }
146
147         buf.append(")");
148         return buf.toString();
149     }
150     
151     /**
152      * @see org.apache.ojb.broker.platforms.Platform#getEscapeClause(org.apache.ojb.broker.query.LikeCriteria)
153      */

154     public String JavaDoc getEscapeClause(LikeCriteria aCriteria)
155     {
156         if (LikeCriteria.getEscapeCharacter() != LikeCriteria.DEFAULT_ESCPAPE_CHARACTER)
157         {
158             // the default escape character is \, so there's no need for an escape clause
159
return super.getEscapeClause(aCriteria);
160         }
161         else
162         {
163             return "";
164         }
165     }
166 }
167
Popular Tags