001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.ojb.broker.platforms;
017:
018: /* Copyright 2002-2005 The Apache Software Foundation
019: *
020: * Licensed under the Apache License, Version 2.0 (the "License");
021: * you may not use this file except in compliance with the License.
022: * You may obtain a copy of the License at
023: *
024: * http://www.apache.org/licenses/LICENSE-2.0
025: *
026: * Unless required by applicable law or agreed to in writing, software
027: * distributed under the License is distributed on an "AS IS" BASIS,
028: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
029: * See the License for the specific language governing permissions and
030: * limitations under the License.
031: */
032:
033: import java.io.ByteArrayInputStream;
034: import java.io.InputStreamReader;
035: import java.io.Reader;
036: import java.io.StringReader;
037: import java.sql.CallableStatement;
038: import java.sql.Connection;
039: import java.sql.PreparedStatement;
040: import java.sql.SQLException;
041: import java.sql.Types;
042:
043: import org.apache.ojb.broker.query.LikeCriteria;
044:
045: /**
046: * @version 1.0
047: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
048: * @version $Id: PlatformMySQLImpl.java,v 1.2.16.1.6.1 2007/10/17 20:32:06 agodert Exp $
049: */
050: public class PlatformMySQLImpl extends PlatformDefaultImpl {
051: private static final String LAST_INSERT = "SELECT LAST_INSERT_ID() FROM ";
052: private static final String LIMIT = " LIMIT 1";
053:
054: /*
055: * @see Platform#setObjectForStatement(PreparedStatement, int, Object, int)
056: */
057: public void setObjectForStatement(PreparedStatement ps, int index,
058: Object value, int sqlType) throws SQLException {
059: switch (sqlType) {
060: case Types.BIT:
061: ps.setObject(index, value);
062: break;
063:
064: case Types.BLOB:
065: case Types.LONGVARBINARY:
066: case Types.VARBINARY:
067: if (value instanceof byte[]) {
068: byte buf[] = (byte[]) value;
069: ByteArrayInputStream inputStream = new ByteArrayInputStream(
070: buf);
071: ps.setBinaryStream(index, inputStream, buf.length);
072:
073: break;
074: }
075:
076: case Types.CLOB:
077: Reader reader = null;
078: int length = 0;
079:
080: if (value instanceof String) {
081: reader = new StringReader((String) value);
082: length = (((String) value)).length();
083: } else if (value instanceof char[]) {
084: String string = new String((char[]) value);
085: reader = new StringReader(string);
086: length = string.length();
087: } else if (value instanceof byte[]) {
088: byte buf[] = (byte[]) value;
089: ByteArrayInputStream inputStream = new ByteArrayInputStream(
090: buf);
091: reader = new InputStreamReader(inputStream);
092: }
093:
094: ps.setCharacterStream(index, reader, length);
095: break;
096:
097: default:
098: super .setObjectForStatement(ps, index, value, sqlType);
099:
100: }
101: }
102:
103: /**
104: * Get join syntax type for this RDBMS - one on of the constants from
105: * JoinSyntaxType interface
106: */
107: public byte getJoinSyntaxType() {
108: return SQL92_NOPAREN_JOIN_SYNTAX;
109: }
110:
111: public String getLastInsertIdentityQuery(String tableName) {
112: return LAST_INSERT + tableName + LIMIT;
113: }
114:
115: /*
116: * (non-Javadoc)
117: *
118: * @see org.apache.ojb.broker.platforms.Platform#addPagingSql(java.lang.StringBuffer)
119: */
120: public void addPagingSql(StringBuffer anSqlString) {
121: anSqlString.append(" LIMIT ?,?");
122: }
123:
124: /* (non-Javadoc)
125: * @see org.apache.ojb.broker.platforms.Platform#createSequenceQuery(String)
126: */
127: public String createSequenceQuery(String sequenceName) {
128: return "insert into ojb_nextval_seq (seq_name) " + "values ('"
129: + sequenceName + "')";
130: }
131:
132: /* (non-Javadoc)
133: * @see org.apache.ojb.broker.platforms.Platform#nextSequenceQuery(String)
134: */
135: public String nextSequenceQuery(String sequenceName) {
136: return "select ojb_nextval_func ('" + sequenceName + "')";
137: }
138:
139: /* (non-Javadoc)
140: * @see org.apache.ojb.broker.platforms.Platform#dropSequenceQuery(String)
141: */
142: public String dropSequenceQuery(String sequenceName) {
143: return "delete from ojb_nextval_seq where seq_name='"
144: + sequenceName + "'";
145: }
146:
147: /* (non-Javadoc)
148: * Copied over from the OJB implementations for Informix
149: */
150: public CallableStatement prepareNextValProcedureStatement(
151: Connection con, String procedureName, String sequenceName)
152: throws PlatformException {
153: try {
154: String sp = " { call " + procedureName + " (?,?) } ";
155: CallableStatement cs = con.prepareCall(sp);
156: cs.registerOutParameter(1, Types.BIGINT);
157: cs.setString(2, sequenceName);
158: return cs;
159: } catch (Exception e) {
160: throw new PlatformException(e);
161: }
162: }
163:
164: /*
165: * (non-Javadoc)
166: *
167: * @see org.apache.ojb.broker.platforms.Platform#supportsPaging()
168: */
169: public boolean supportsPaging() {
170: return true;
171: }
172:
173: /**
174: * @see org.apache.ojb.broker.platforms.Platform#concatenate(java.lang.String[])
175: */
176: public String concatenate(String[] theColumns) {
177: if (theColumns.length == 1) {
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: if (i > 0) {
186: buf.append(",");
187: }
188: buf.append(theColumns[i]);
189: }
190:
191: buf.append(")");
192: return buf.toString();
193: }
194:
195: /**
196: * @see org.apache.ojb.broker.platforms.Platform#getEscapeClause(org.apache.ojb.broker.query.LikeCriteria)
197: */
198: public String getEscapeClause(LikeCriteria aCriteria) {
199: if (LikeCriteria.getEscapeCharacter() != LikeCriteria.DEFAULT_ESCPAPE_CHARACTER) {
200: // the default escape character is \, so there's no need for an escape clause
201: return super .getEscapeClause(aCriteria);
202: }
203: return "";
204: }
205: }
|