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.sybase;
019
020import java.sql.Connection;
021import java.sql.SQLException;
022import java.sql.Statement;
023
024import net.sf.hajdbc.IdentityColumnSupport;
025import net.sf.hajdbc.dialect.StandardDialect;
026import net.sf.hajdbc.util.Resources;
027
028/**
029 * Dialect for Sybase (commercial).
030 * @author Paul Ferraro
031 */
032@SuppressWarnings("nls")
033public class SybaseDialect extends StandardDialect
034{
035        /**
036         * {@inheritDoc}
037         * @see net.sf.hajdbc.dialect.StandardDialect#getIdentityColumnSupport()
038         */
039        @Override
040        public IdentityColumnSupport getIdentityColumnSupport()
041        {
042                return this;
043        }
044
045        /**
046         * {@inheritDoc}
047         * @see net.sf.hajdbc.dialect.StandardDialect#vendorPattern()
048         */
049        @Override
050        protected String vendorPattern()
051        {
052                return "sybase";
053        }
054
055        /**
056         * @see net.sf.hajdbc.dialect.StandardDialect#truncateTableFormat()
057         */
058        @Override
059        protected String truncateTableFormat()
060        {
061                return "TRUNCATE TABLE {0}";
062        }
063        
064        /**
065         * Deferrability clause is not supported.
066         * @see net.sf.hajdbc.dialect.StandardDialect#createForeignKeyConstraintFormat()
067         */
068        @Override
069        protected String createForeignKeyConstraintFormat()
070        {
071                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} ON UPDATE {6,choice,0#CASCADE|1#RESTRICT|2#SET NULL|3#NO ACTION|4#SET DEFAULT}";
072        }
073
074        /**
075         * @see net.sf.hajdbc.dialect.StandardDialect#currentDatePattern()
076         */
077        @Override
078        protected String currentDatePattern()
079        {
080                return "(?<=\\W)CURRENT\\s+DATE(?=\\W)|(?<=\\W)TODAY\\s*\\(\\s*\\*\\s*\\)";
081        }
082
083        /**
084         * @see net.sf.hajdbc.dialect.StandardDialect#currentTimePattern()
085         */
086        @Override
087        protected String currentTimePattern()
088        {
089                return "(?<=\\W)CURRENT\\s+TIME(?=\\W)";
090        }
091
092        /**
093         * @see net.sf.hajdbc.dialect.StandardDialect#currentTimestampPattern()
094         */
095        @Override
096        protected String currentTimestampPattern()
097        {
098                return "(?<=\\W)CURRENT\\s+TIMESTAMP(?=\\W)|(?<=\\W)GETDATE\\s*\\(\\s*\\)|(?<=\\W)NOW\\s*\\(\\s*\\*\\s*\\)";
099        }
100
101        /**
102         * @see net.sf.hajdbc.dialect.StandardDialect#dateLiteralFormat()
103         */
104        @Override
105        protected String dateLiteralFormat()
106        {
107                return this.timestampLiteralFormat();
108        }
109
110        /**
111         * @see net.sf.hajdbc.dialect.StandardDialect#timeLiteralFormat()
112         */
113        @Override
114        protected String timeLiteralFormat()
115        {
116                return this.timestampLiteralFormat();
117        }
118
119        /**
120         * @see net.sf.hajdbc.dialect.StandardDialect#timestampLiteralFormat()
121         */
122        @Override
123        protected String timestampLiteralFormat()
124        {
125                return "''{0}''";
126        }
127        
128        /**
129         * @see net.sf.hajdbc.dialect.StandardDialect#randomPattern()
130         */
131        @Override
132        protected String randomPattern()
133        {
134                return "(?<=\\W)RAND\\s*\\(\\s*\\d*\\s*\\)";
135        }
136
137        /**
138         * jTDS does not implement Connection.isValid(...)
139         */
140        @Override
141        public boolean isValid(Connection connection) throws SQLException
142        {
143                Statement statement = connection.createStatement();
144                try
145                {
146                        statement.executeQuery("SELECT GETDATE()");
147                        return true;
148                }
149                finally
150                {
151                        Resources.close(statement);
152                }
153        }
154}