001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  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    package org.apache.activemq.store.jdbc.adapter;
018    
019    import java.util.ArrayList;
020    import java.util.Arrays;
021    
022    import org.apache.activemq.store.jdbc.Statements;
023    
024    /**
025     * 
026     * @org.apache.xbean.XBean element="mysql-jdbc-adapter"
027     * 
028     */
029    public class MySqlJDBCAdapter extends DefaultJDBCAdapter {
030    
031        // The transactional types..
032        public static final String INNODB = "INNODB";
033        public static final String NDBCLUSTER = "NDBCLUSTER";
034        public static final String BDB = "BDB";
035    
036        // The non transactional types..
037        public static final String MYISAM = "MYISAM";
038        public static final String ISAM = "ISAM";
039        public static final String MERGE = "MERGE";
040        public static final String HEAP = "HEAP";
041    
042        String engineType = INNODB;
043        String typeStatement = "ENGINE";
044    
045        @Override
046        public void setStatements(Statements statements) {
047            String type = engineType.toUpperCase();
048            if( !type.equals(INNODB) &&  !type.equals(NDBCLUSTER) ) {
049                // Don't use LOCK TABLE for the INNODB and NDBCLUSTER engine types...
050                statements.setLockCreateStatement("LOCK TABLE " + statements.getFullLockTableName() + " WRITE");
051            }
052    
053            statements.setBinaryDataType("LONGBLOB");
054            
055            
056            String typeClause = typeStatement + "=" + type;
057            if( type.equals(NDBCLUSTER) ) {
058                // in the NDBCLUSTER case we will create as INNODB and then alter to NDBCLUSTER
059                typeClause = typeStatement + "=" + INNODB;
060            }
061            
062            // Update the create statements so they use the right type of engine 
063            String[] s = statements.getCreateSchemaStatements();
064            for (int i = 0; i < s.length; i++) {
065                if( s[i].startsWith("CREATE TABLE")) {
066                    s[i] = s[i]+ " " + typeClause;
067                }
068            }
069            
070            if( type.equals(NDBCLUSTER) ) {
071                // Add the alter statements.
072                ArrayList<String> l = new ArrayList<String>(Arrays.asList(s));
073                l.add("ALTER TABLE "+statements.getFullMessageTableName()+" ENGINE="+NDBCLUSTER);
074                l.add("ALTER TABLE "+statements.getFullAckTableName()+" ENGINE="+NDBCLUSTER);
075                l.add("ALTER TABLE "+statements.getFullLockTableName()+" ENGINE="+NDBCLUSTER);
076                l.add("FLUSH TABLES");
077                s = l.toArray(new String[l.size()]);
078                statements.setCreateSchemaStatements(s);
079            }        
080            
081            super.setStatements(statements);
082        }
083    
084        public String getEngineType() {
085            return engineType;
086        }
087    
088        public void setEngineType(String engineType) {
089            this.engineType = engineType;
090        }
091    
092        public String getTypeStatement() {
093            return typeStatement;
094        }
095    
096        public void setTypeStatement(String typeStatement) {
097            this.typeStatement = typeStatement;
098        }
099    }