View Javadoc

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;
19  import java.io.InputStreamReader;
20  import java.io.Reader;
21  import java.io.StringReader;
22  import java.sql.PreparedStatement;
23  import java.sql.SQLException;
24  import java.sql.Types;
25  import java.sql.CallableStatement;
26  import java.sql.Connection;
27  
28  import org.apache.ojb.broker.query.LikeCriteria;
29  
30  /**
31   * @version 1.0
32   * @author jakob br�uchi
33   * @version $Id: PlatformMySQLImpl.java,v 1.1 2007-08-24 22:17:35 ewestfal Exp $
34   */
35  public class PlatformMySQLImpl extends PlatformDefaultImpl
36  {
37      private static final String LAST_INSERT = "SELECT LAST_INSERT_ID() FROM ";
38      private static final String LIMIT = " LIMIT 1";
39      
40      /*
41  	 * @see Platform#setObjectForStatement(PreparedStatement, int, Object, int)
42  	 */
43      public void setObjectForStatement(PreparedStatement ps, int index, Object value, int sqlType) throws SQLException
44      {
45          switch (sqlType)
46          {
47              case Types.BIT :
48                  ps.setObject(index, value);
49                  break;
50  
51              case Types.BLOB :
52              case Types.LONGVARBINARY :
53              case Types.VARBINARY :
54                  if (value instanceof byte[])
55                  {
56                      byte buf[] = (byte[]) value;
57                      ByteArrayInputStream inputStream = new ByteArrayInputStream(buf);
58                      ps.setBinaryStream(index, inputStream, buf.length);
59  
60                      break;
61                  }
62  
63              case Types.CLOB :
64                  Reader reader = null;
65                  int length = 0;
66  
67                  if (value instanceof String)
68                  {
69                      reader = new StringReader((String) value);
70                      length = (((String) value)).length();
71                  }
72                  else if (value instanceof char[])
73                  {
74                      String string = new String((char[])value);
75                      reader = new StringReader(string);
76                      length = string.length();
77                  }
78                  else if (value instanceof byte[])
79                  {
80                      byte buf[] = (byte[]) value;
81                      ByteArrayInputStream inputStream = new ByteArrayInputStream(buf);
82                      reader = new InputStreamReader(inputStream);
83                  }
84  
85                  ps.setCharacterStream(index, reader, length);
86                  break;
87  
88              default :
89                  super.setObjectForStatement(ps, index, value, sqlType);
90  
91          }
92      }
93      /**
94  	 * Get join syntax type for this RDBMS - one on of the constants from
95  	 * JoinSyntaxType interface
96  	 */
97      public byte getJoinSyntaxType()
98      {
99          return SQL92_NOPAREN_JOIN_SYNTAX;
100     }
101 
102     public String getLastInsertIdentityQuery(String tableName)
103     {
104         return LAST_INSERT + tableName + LIMIT;
105     }
106 
107     /*
108 	 * (non-Javadoc)
109 	 * 
110 	 * @see org.apache.ojb.broker.platforms.Platform#addPagingSql(java.lang.StringBuffer)
111 	 */
112     public void addPagingSql(StringBuffer anSqlString)
113     {
114         anSqlString.append(" LIMIT ?,?");
115     }
116 
117     /* (non-Javadoc)
118      * @see org.apache.ojb.broker.platforms.Platform#createSequenceQuery(String)
119      */
120     public String createSequenceQuery(String sequenceName)
121     {
122         return "insert into ojb_nextval_seq (seq_name) " + 
123         		"values ('" + sequenceName + "')";
124     }
125 
126     /* (non-Javadoc)
127      * @see org.apache.ojb.broker.platforms.Platform#nextSequenceQuery(String)
128      */
129     public String nextSequenceQuery(String sequenceName)
130     {
131         return "select ojb_nextval_func ('" + sequenceName + "')";
132     }
133 
134     /* (non-Javadoc)
135      * @see org.apache.ojb.broker.platforms.Platform#dropSequenceQuery(String)
136      */
137     public String dropSequenceQuery(String sequenceName)
138     {
139         return "delete from ojb_nextval_seq where seq_name='" + 
140         		sequenceName + "'";
141     }
142     
143     /* (non-Javadoc)
144      * Copied over from the OJB implementations for Informix
145      */
146     public CallableStatement prepareNextValProcedureStatement (Connection con,
147     							String procedureName, String sequenceName) throws
148     							PlatformException
149     {
150     	try {
151     		String sp = " { call " + procedureName + " (?,?) } ";    		
152     		CallableStatement cs = con.prepareCall(sp);
153 		cs.registerOutParameter(1, Types.BIGINT);
154 		cs.setString(2, sequenceName);
155     		return cs;
156     	} catch (Exception e) {
157     		throw new PlatformException(e);
158     	}
159     }
160     
161     /*
162 	 * (non-Javadoc)
163 	 * 
164 	 * @see org.apache.ojb.broker.platforms.Platform#supportsPaging()
165 	 */
166     public boolean supportsPaging()
167     {
168         return true;
169     }
170 
171     /**
172      * @see org.apache.ojb.broker.platforms.Platform#concatenate(java.lang.String[])
173      */
174     public String concatenate(String[] theColumns)
175     {
176         if (theColumns.length == 1)
177         {
178             return theColumns[0];
179         }
180         
181         StringBuffer buf = new StringBuffer();
182         
183         buf.append("concat(");
184         for (int i = 0; i < theColumns.length; i++)
185         {
186             if (i > 0)
187             {
188                 buf.append(",");
189             }
190             buf.append(theColumns[i]);
191         }
192 
193         buf.append(")");
194         return buf.toString();
195     }    
196     
197     /**
198      * @see org.apache.ojb.broker.platforms.Platform#getEscapeClause(org.apache.ojb.broker.query.LikeCriteria)
199      */
200     public String getEscapeClause(LikeCriteria aCriteria)
201     {
202         if (LikeCriteria.getEscapeCharacter() != LikeCriteria.DEFAULT_ESCPAPE_CHARACTER)  
203         {
204             // the default escape character is \, so there's no need for an escape clause
205             return super.getEscapeClause(aCriteria);
206         }
207         else
208         {
209             return "";
210         }
211     }    
212 }