001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.dialect.maxdb;
019
020import java.sql.DatabaseMetaData;
021import java.sql.ResultSet;
022import java.sql.SQLException;
023import java.sql.Statement;
024import java.util.Collection;
025import java.util.LinkedList;
026import java.util.List;
027
028import net.sf.hajdbc.SequenceProperties;
029import net.sf.hajdbc.SequencePropertiesFactory;
030import net.sf.hajdbc.SequenceSupport;
031import net.sf.hajdbc.dialect.StandardDialect;
032import net.sf.hajdbc.util.Resources;
033
034/**
035 * Dialect for <a href="http://www.mysql.com/products/database/maxdb/">MySQL MaxDB</a>.
036 * @author  Paul Ferraro
037 * @since   1.1
038 */
039public class MaxDBDialect extends StandardDialect
040{
041        /**
042         * {@inheritDoc}
043         * @see net.sf.hajdbc.dialect.StandardDialect#vendorPattern()
044         */
045        @Override
046        protected String vendorPattern()
047        {
048                return "maxdb";
049        }
050
051        /**
052         * @see net.sf.hajdbc.dialect.StandardDialect#dummyTable()
053         */
054        @Override
055        protected String dummyTable()
056        {
057                return "DUAL";
058        }
059
060        /**
061         * {@inheritDoc}
062         * @see net.sf.hajdbc.dialect.StandardDialect#getSequenceSupport()
063         */
064        @Override
065        public SequenceSupport getSequenceSupport()
066        {
067                return this;
068        }
069
070        @Override
071        public Collection<SequenceProperties> getSequences(DatabaseMetaData metaData, SequencePropertiesFactory factory) throws SQLException
072        {
073                Statement statement = metaData.getConnection().createStatement();
074                
075                try
076                {
077                        ResultSet resultSet = statement.executeQuery("SELECT SEQUENCE_NAME, INCREMENT_BY FROM USER_SEQUENCES");
078                        
079                        List<SequenceProperties> sequences = new LinkedList<SequenceProperties>();
080                        
081                        while (resultSet.next())
082                        {
083                                sequences.add(factory.createSequenceProperties(null, resultSet.getString(1), resultSet.getInt(2)));
084                        }
085                        
086                        return sequences;
087                }
088                finally
089                {
090                        Resources.close(statement);
091                }
092        }
093
094        /**
095         * @see net.sf.hajdbc.dialect.StandardDialect#truncateTableFormat()
096         */
097        @Override
098        protected String truncateTableFormat()
099        {
100                return "TRUNCATE TABLE {0}";
101        }
102        
103        /**
104         * ON UPDATE and deferrability clauses are not supported.
105         * @see net.sf.hajdbc.dialect.StandardDialect#createForeignKeyConstraintFormat()
106         */
107        @Override
108        protected String createForeignKeyConstraintFormat()
109        {
110                return "ALTER TABLE {1} ADD CONSTRAINT {0} FOREIGN KEY ({2}) REFERENCES {3} ({4}) ON DELETE {5,choice,0#CASCADE|1#RESTRICT|2#SET NULL|3#NO ACTION|4#SET DEFAULT}";
111        }
112
113        /**
114         * @see net.sf.hajdbc.dialect.StandardDialect#sequencePattern()
115         */
116        @Override
117        protected String sequencePattern()
118        {
119                return "'?(\\w+)'?\\.(?:CURR|NEXT)VAL";
120        }
121
122        /**
123         * @see net.sf.hajdbc.dialect.StandardDialect#nextSequenceValueFormat()
124         */
125        @Override
126        protected String nextSequenceValueFormat()
127        {
128                return "{0}.NEXTVAL";
129        }
130}