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    import java.sql.CallableStatement;
026    import java.sql.Connection;
027    
028    import org.apache.ojb.broker.query.LikeCriteria;
029    
030    /**
031     * @version 1.0
032     * @author jakob br�uchi
033     * @version $Id: PlatformMySQLImpl.java,v 1.1 2007-08-24 22:17:35 ewestfal Exp $
034     */
035    public class PlatformMySQLImpl extends PlatformDefaultImpl
036    {
037        private static final String LAST_INSERT = "SELECT LAST_INSERT_ID() FROM ";
038        private static final String LIMIT = " LIMIT 1";
039        
040        /*
041             * @see Platform#setObjectForStatement(PreparedStatement, int, Object, int)
042             */
043        public void setObjectForStatement(PreparedStatement ps, int index, Object value, int sqlType) throws SQLException
044        {
045            switch (sqlType)
046            {
047                case Types.BIT :
048                    ps.setObject(index, value);
049                    break;
050    
051                case Types.BLOB :
052                case Types.LONGVARBINARY :
053                case Types.VARBINARY :
054                    if (value instanceof byte[])
055                    {
056                        byte buf[] = (byte[]) value;
057                        ByteArrayInputStream inputStream = new ByteArrayInputStream(buf);
058                        ps.setBinaryStream(index, inputStream, buf.length);
059    
060                        break;
061                    }
062    
063                case Types.CLOB :
064                    Reader reader = null;
065                    int length = 0;
066    
067                    if (value instanceof String)
068                    {
069                        reader = new StringReader((String) value);
070                        length = (((String) value)).length();
071                    }
072                    else if (value instanceof char[])
073                    {
074                        String string = new String((char[])value);
075                        reader = new StringReader(string);
076                        length = string.length();
077                    }
078                    else if (value instanceof byte[])
079                    {
080                        byte buf[] = (byte[]) value;
081                        ByteArrayInputStream inputStream = new ByteArrayInputStream(buf);
082                        reader = new InputStreamReader(inputStream);
083                    }
084    
085                    ps.setCharacterStream(index, reader, length);
086                    break;
087    
088                default :
089                    super.setObjectForStatement(ps, index, value, sqlType);
090    
091            }
092        }
093        /**
094             * Get join syntax type for this RDBMS - one on of the constants from
095             * JoinSyntaxType interface
096             */
097        public byte getJoinSyntaxType()
098        {
099            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    }