001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.dialect;
022:
023: import java.sql.DatabaseMetaData;
024: import java.sql.SQLException;
025: import java.util.Collection;
026: import java.util.Collections;
027: import java.util.List;
028:
029: import net.sf.hajdbc.ColumnProperties;
030: import net.sf.hajdbc.QualifiedName;
031:
032: /**
033: * Dialect for <a href="http://www.mysql.com/products/database/mysql/">MySQL</a>
034: * @author Paul Ferraro
035: */
036: @SuppressWarnings("nls")
037: public class MySQLDialect extends StandardDialect {
038: /**
039: * @see net.sf.hajdbc.dialect.StandardDialect#getDefaultSchemas(java.sql.DatabaseMetaData)
040: */
041: @Override
042: public List<String> getDefaultSchemas(DatabaseMetaData metaData)
043: throws SQLException {
044: return Collections.singletonList(this .executeFunction(metaData
045: .getConnection(), "DATABASE()"));
046: }
047:
048: /**
049: * @see net.sf.hajdbc.dialect.StandardDialect#isIdentity(net.sf.hajdbc.ColumnProperties)
050: */
051: @Override
052: public boolean isIdentity(ColumnProperties properties) {
053: if (properties.getNativeType().equalsIgnoreCase("SERIAL"))
054: return true;
055:
056: String remarks = properties.getRemarks();
057:
058: return (remarks != null) && remarks.contains("AUTO_INCREMENT");
059: }
060:
061: /**
062: * @see net.sf.hajdbc.dialect.StandardDialect#parseSequence(java.lang.String)
063: */
064: @Override
065: public String parseSequence(String sql) {
066: return null;
067: }
068:
069: /**
070: * @see net.sf.hajdbc.dialect.StandardDialect#getSequences(java.sql.DatabaseMetaData)
071: */
072: @Override
073: public Collection<QualifiedName> getSequences(
074: DatabaseMetaData metaData) throws SQLException {
075: return Collections.emptyList();
076: }
077:
078: /**
079: * Deferrability clause is not supported.
080: * @see net.sf.hajdbc.dialect.StandardDialect#createForeignKeyConstraintFormat()
081: */
082: @Override
083: protected String createForeignKeyConstraintFormat() {
084: 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}";
085: }
086:
087: /**
088: * @see net.sf.hajdbc.dialect.StandardDialect#createUniqueConstraintFormat()
089: */
090: @Override
091: protected String createUniqueConstraintFormat() {
092: return "ALTER TABLE {1} ADD UNIQUE {0} ({2})";
093: }
094:
095: /**
096: * @see net.sf.hajdbc.dialect.StandardDialect#dropForeignKeyConstraintFormat()
097: */
098: @Override
099: protected String dropForeignKeyConstraintFormat() {
100: return "ALTER TABLE {1} DROP FOREIGN KEY {0}";
101: }
102:
103: /**
104: * @see net.sf.hajdbc.dialect.StandardDialect#dropUniqueConstraintFormat()
105: */
106: @Override
107: protected String dropUniqueConstraintFormat() {
108: return "ALTER TABLE {1} DROP INDEX {0}";
109: }
110:
111: /**
112: * @see net.sf.hajdbc.dialect.StandardDialect#alterIdentityColumnFormat()
113: */
114: @Override
115: protected String alterIdentityColumnFormat() {
116: return "ALTER TABLE {0} AUTO_INCREMENT = {2}";
117: }
118:
119: /**
120: * @see net.sf.hajdbc.dialect.StandardDialect#currentDatePattern()
121: */
122: @Override
123: protected String currentDatePattern() {
124: return super .currentDatePattern()
125: + "|(?<=\\W)CURDATE\\s*\\(\\s*\\)";
126: }
127:
128: /**
129: * @see net.sf.hajdbc.dialect.StandardDialect#currentTimePattern()
130: */
131: @Override
132: protected String currentTimePattern() {
133: return super .currentTimePattern()
134: + "|(?<=\\W)CURTIME\\s*\\(\\s*\\)";
135: }
136:
137: /**
138: * @see net.sf.hajdbc.dialect.StandardDialect#currentTimestampPattern()
139: */
140: @Override
141: protected String currentTimestampPattern() {
142: return super .currentTimestampPattern()
143: + "|(?<=\\W)NOW\\s*\\(\\s*\\)|(?<=\\W)SYSDATE\\s*\\(\\s*\\)";
144: }
145:
146: /**
147: * @see net.sf.hajdbc.dialect.StandardDialect#dateLiteralFormat()
148: */
149: @Override
150: protected String dateLiteralFormat() {
151: return this .timestampLiteralFormat();
152: }
153:
154: /**
155: * @see net.sf.hajdbc.dialect.StandardDialect#timeLiteralFormat()
156: */
157: @Override
158: protected String timeLiteralFormat() {
159: return this .timestampLiteralFormat();
160: }
161:
162: /**
163: * @see net.sf.hajdbc.dialect.StandardDialect#timestampLiteralFormat()
164: */
165: @Override
166: protected String timestampLiteralFormat() {
167: return "''{0}''";
168: }
169: }
|