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.ingres;
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;
027import java.util.regex.Pattern;
028
029import net.sf.hajdbc.SequenceProperties;
030import net.sf.hajdbc.SequencePropertiesFactory;
031import net.sf.hajdbc.SequenceSupport;
032import net.sf.hajdbc.dialect.StandardDialect;
033import net.sf.hajdbc.util.Resources;
034
035/**
036 * Dialect for <a href="http://opensource.ingres.com/projects/ingres/">Ingres</a>.
037 * 
038 * @author Paul Ferraro
039 */
040public class IngresDialect extends StandardDialect
041{
042        private final Pattern legacySequencePattern = Pattern.compile("'?(\\w+)'?\\.(?:(?:CURR)|(?:NEXT))VAL", Pattern.CASE_INSENSITIVE);
043        
044        /**
045         * {@inheritDoc}
046         * @see net.sf.hajdbc.dialect.StandardDialect#vendorPattern()
047         */
048        @Override
049        protected String vendorPattern()
050        {
051                return "ingres";
052        }
053
054        /**
055         * {@inheritDoc}
056         * @see net.sf.hajdbc.dialect.StandardDialect#getSequenceSupport()
057         */
058        @Override
059        public SequenceSupport getSequenceSupport()
060        {
061                return this;
062        }
063
064        @Override
065        public Collection<SequenceProperties> getSequences(DatabaseMetaData metaData, SequencePropertiesFactory factory) throws SQLException
066        {
067                Statement statement = metaData.getConnection().createStatement();
068                
069                try
070                {
071                        ResultSet resultSet = statement.executeQuery("SELECT seq_name FROM iisequence");
072                        
073                        List<SequenceProperties> sequences = new LinkedList<SequenceProperties>();
074                        
075                        while (resultSet.next())
076                        {
077                                sequences.add(factory.createSequenceProperties(null, resultSet.getString(1), 1));
078                        }
079                        
080                        return sequences;
081                }
082                finally
083                {
084                        Resources.close(statement);
085                }
086        }
087
088        /**
089         * @see net.sf.hajdbc.dialect.StandardDialect#parseSequence(java.lang.String)
090         */
091        @Override
092        public String parseSequence(String sql)
093        {
094                String sequence = super.parseSequence(sql);
095                
096                return (sequence != null) ? sequence : this.parse(this.legacySequencePattern, sql);
097        }
098
099        /**
100         * @see net.sf.hajdbc.dialect.StandardDialect#sequencePattern()
101         */
102        @Override
103        protected String sequencePattern()
104        {
105                return "(?:NEXT|CURRENT)\\s+VALUE\\s+FOR\\s+'?([^',\\s\\(\\)]+)";
106        }
107
108        /**
109         * @see net.sf.hajdbc.dialect.StandardDialect#currentDatePattern()
110         */
111        @Override
112        protected String currentDatePattern()
113        {
114                return "(?<=\\W)CURRENT_DATE(?=\\W)|(?<=\\W)DATE\\s*\\(\\s*'TODAY'\\s*\\)";
115        }
116
117        /**
118         * @see net.sf.hajdbc.dialect.StandardDialect#currentTimePattern()
119         */
120        @Override
121        protected String currentTimePattern()
122        {
123                return "(?<=\\W)CURRENT_TIME(?=\\W)|(?<=\\W)LOCAL_TIME(?=\\W)";
124        }
125
126        /**
127         * @see net.sf.hajdbc.dialect.StandardDialect#currentTimestampPattern()
128         */
129        @Override
130        protected String currentTimestampPattern()
131        {
132                return "(?<=\\W)CURRENT_TIMESTAMP(?=\\W)|(?<=\\W)LOCAL_TIMESTAMP(?=\\W)|(?<=\\W)DATE\\s*\\(\\s*'NOW'\\s*\\)";
133        }
134
135        /**
136         * @see net.sf.hajdbc.dialect.StandardDialect#randomPattern()
137         */
138        @Override
139        protected String randomPattern()
140        {
141                return "(?<=\\W)RANDOMF\\s*\\(\\s*\\)";
142        }
143}