001: package org.apache.ojb.broker.platforms;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.InputStreamReader;
020: import java.io.Reader;
021: import java.io.StringReader;
022: import java.sql.PreparedStatement;
023: import java.sql.SQLException;
024: import java.sql.Types;
025:
026: import org.apache.ojb.broker.query.LikeCriteria;
027:
028: /**
029: * @version 1.0
030: * @author jakob bräuchi
031: * @version $Id: PlatformMySQLImpl.java,v 1.15.2.1 2005/12/21 22:26:40 tomdz Exp $
032: */
033: public class PlatformMySQLImpl extends PlatformDefaultImpl {
034: private static final String LAST_INSERT = "SELECT LAST_INSERT_ID() FROM ";
035: private static final String LIMIT = " LIMIT 1";
036:
037: /*
038: * @see Platform#setObjectForStatement(PreparedStatement, int, Object, int)
039: */
040: public void setObjectForStatement(PreparedStatement ps, int index,
041: Object value, int sqlType) throws SQLException {
042: switch (sqlType) {
043: case Types.BIT:
044: ps.setObject(index, value);
045: break;
046:
047: case Types.BLOB:
048: case Types.LONGVARBINARY:
049: case Types.VARBINARY:
050: if (value instanceof byte[]) {
051: byte buf[] = (byte[]) value;
052: ByteArrayInputStream inputStream = new ByteArrayInputStream(
053: buf);
054: ps.setBinaryStream(index, inputStream, buf.length);
055:
056: break;
057: }
058:
059: case Types.CLOB:
060: Reader reader = null;
061: int length = 0;
062:
063: if (value instanceof String) {
064: reader = new StringReader((String) value);
065: length = (((String) value)).length();
066: } else if (value instanceof char[]) {
067: String string = new String((char[]) value);
068: reader = new StringReader(string);
069: length = string.length();
070: } else if (value instanceof byte[]) {
071: byte buf[] = (byte[]) value;
072: ByteArrayInputStream inputStream = new ByteArrayInputStream(
073: buf);
074: reader = new InputStreamReader(inputStream);
075: }
076:
077: ps.setCharacterStream(index, reader, length);
078: break;
079:
080: default:
081: super .setObjectForStatement(ps, index, value, sqlType);
082:
083: }
084: }
085:
086: /**
087: * Get join syntax type for this RDBMS - one on of the constants from
088: * JoinSyntaxType interface
089: */
090: public byte getJoinSyntaxType() {
091: return SQL92_NOPAREN_JOIN_SYNTAX;
092: }
093:
094: public String getLastInsertIdentityQuery(String tableName) {
095: return LAST_INSERT + tableName + LIMIT;
096: }
097:
098: /*
099: * (non-Javadoc)
100: *
101: * @see org.apache.ojb.broker.platforms.Platform#addPagingSql(java.lang.StringBuffer)
102: */
103: public void addPagingSql(StringBuffer anSqlString) {
104: anSqlString.append(" LIMIT ?,?");
105: }
106:
107: /*
108: * (non-Javadoc)
109: *
110: * @see org.apache.ojb.broker.platforms.Platform#supportsPaging()
111: */
112: public boolean supportsPaging() {
113: return true;
114: }
115:
116: /**
117: * @see org.apache.ojb.broker.platforms.Platform#concatenate(java.lang.String[])
118: */
119: public String concatenate(String[] theColumns) {
120: if (theColumns.length == 1) {
121: return theColumns[0];
122: }
123:
124: StringBuffer buf = new StringBuffer();
125:
126: buf.append("concat(");
127: for (int i = 0; i < theColumns.length; i++) {
128: if (i > 0) {
129: buf.append(",");
130: }
131: buf.append(theColumns[i]);
132: }
133:
134: buf.append(")");
135: return buf.toString();
136: }
137:
138: /**
139: * @see org.apache.ojb.broker.platforms.Platform#getEscapeClause(org.apache.ojb.broker.query.LikeCriteria)
140: */
141: public String getEscapeClause(LikeCriteria aCriteria) {
142: if (LikeCriteria.getEscapeCharacter() != LikeCriteria.DEFAULT_ESCPAPE_CHARACTER) {
143: // the default escape character is \, so there's no need for an escape clause
144: return super .getEscapeClause(aCriteria);
145: } else {
146: return "";
147: }
148: }
149: }
|