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.firebird;
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="firebird.sourceforge.net">Firebird</a>.
036 * @author Paul Ferraro
037 */
038@SuppressWarnings("nls")
039public class FirebirdDialect extends StandardDialect
040{
041        /**
042         * {@inheritDoc}
043         * @see net.sf.hajdbc.dialect.StandardDialect#vendorPattern()
044         */
045        @Override
046        protected String vendorPattern()
047        {
048                return "firebird";
049        }
050
051        /**
052         * @see net.sf.hajdbc.dialect.StandardDialect#dummyTable()
053         */
054        @Override
055        protected String dummyTable()
056        {
057                return "RDB$DATABASE";
058        }
059
060        /**
061         * Firebird 2.0 will support standard syntax.  Until then...
062         * @see net.sf.hajdbc.dialect.StandardDialect#alterSequenceFormat()
063         */
064        @Override
065        protected String alterSequenceFormat()
066        {
067                return "SET GENERATOR {0} TO {1}";
068        }
069
070        /**
071         * {@inheritDoc}
072         * @see net.sf.hajdbc.dialect.StandardDialect#getSequenceSupport()
073         */
074        @Override
075        public SequenceSupport getSequenceSupport()
076        {
077                return this;
078        }
079
080        @Override
081        public Collection<SequenceProperties> getSequences(DatabaseMetaData metaData, SequencePropertiesFactory factory) throws SQLException
082        {
083                Statement statement = metaData.getConnection().createStatement();
084                
085                try
086                {
087                        ResultSet resultSet = statement.executeQuery("SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS");
088                        
089                        List<SequenceProperties> sequences = new LinkedList<SequenceProperties>();
090                        
091                        while (resultSet.next())
092                        {
093                                sequences.add(factory.createSequenceProperties(null, resultSet.getString(1), 1));
094                        }
095                        
096                        return sequences;
097                }
098                finally
099                {
100                        Resources.close(statement);
101                }
102        }
103
104        /**
105         * Firebird 2.0 will support standard syntax.  Until then...
106         * @see net.sf.hajdbc.dialect.StandardDialect#sequencePattern()
107         */
108        @Override
109        protected String sequencePattern()
110        {
111                return "GEN_ID\\s*\\(\\s*([^\\s,]+)\\s*,\\s*\\d+\\s*\\)";
112        }
113
114        /**
115         * @see net.sf.hajdbc.dialect.StandardDialect#selectForUpdatePattern()
116         */
117        @Override
118        protected String selectForUpdatePattern()
119        {
120                return "SELECT\\s+.+\\s+WITH\\s+LOCK";
121        }
122
123        /**
124         * @see net.sf.hajdbc.dialect.StandardDialect#nextSequenceValueFormat()
125         */
126        @Override
127        protected String nextSequenceValueFormat()
128        {
129                return "GEN_ID({0}, 1)";
130        }
131}