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.oracle;
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 Oracle (commercial).
036 * @author Paul Ferraro
037 */
038@SuppressWarnings("nls")
039public class OracleDialect extends StandardDialect
040{
041        /**
042         * {@inheritDoc}
043         * @see net.sf.hajdbc.dialect.StandardDialect#vendorPattern()
044         */
045        @Override
046        protected String vendorPattern()
047        {
048                return "oracle";
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        @Override
095        protected String schemaPattern(DatabaseMetaData metaData) throws SQLException
096        {
097                return metaData.getUserName();
098        }
099
100        /**
101         * @see net.sf.hajdbc.dialect.StandardDialect#truncateTableFormat()
102         */
103        @Override
104        protected String truncateTableFormat()
105        {
106                return "TRUNCATE TABLE {0}";
107        }
108        
109        /**
110         * ON UPDATE and deferrability clauses are not supported.
111         * @see net.sf.hajdbc.dialect.StandardDialect#createForeignKeyConstraintFormat()
112         */
113        @Override
114        protected String createForeignKeyConstraintFormat()
115        {
116                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}";
117        }
118
119        /**
120         * @see net.sf.hajdbc.dialect.StandardDialect#sequencePattern()
121         */
122        @Override
123        protected String sequencePattern()
124        {
125                return "'?(\\w+)'?\\.(?:CURR|NEXT)VAL";
126        }
127
128        /**
129         * @see net.sf.hajdbc.dialect.StandardDialect#nextSequenceValueFormat()
130         */
131        @Override
132        protected String nextSequenceValueFormat()
133        {
134                return "{0}.NEXTVAL";
135        }
136
137        /**
138         * {@inheritDoc}
139         * @see net.sf.hajdbc.dialect.StandardDialect#alterSequenceFormat()
140         */
141        @Override
142        protected String alterSequenceFormat()
143        {
144                return "DROP SEQUENCE {0}; CREATE SEQUENCE {0} START WITH {1} INCREMENT BY {2}";
145        }
146
147        @Override
148        protected boolean indicatesFailure(String sqlState)
149        {
150                // 66 class SQLStates indicate SQL*Net driver errors
151                // 69 class SQLStates indicate SQL*Connect errors
152                return super.indicatesFailure(sqlState) || sqlState.startsWith("66") || sqlState.startsWith("69");
153        }
154}