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 */ 021package net.sf.hajdbc.dialect; 022 023import java.sql.DatabaseMetaData; 024import java.sql.SQLException; 025import java.util.Collection; 026import java.util.Collections; 027import java.util.List; 028 029import net.sf.hajdbc.QualifiedName; 030 031/** 032 * Dialect for <a href="http://www.mysql.com/products/database/mysql/">MySQL</a> 033 * @author Paul Ferraro 034 */ 035@SuppressWarnings("nls") 036public class MySQLDialect extends StandardDialect 037{ 038 /** 039 * @see net.sf.hajdbc.dialect.StandardDialect#getDefaultSchemas(java.sql.DatabaseMetaData) 040 */ 041 @Override 042 public List<String> getDefaultSchemas(DatabaseMetaData metaData) throws SQLException 043 { 044 return Collections.singletonList(this.executeFunction(metaData.getConnection(), "DATABASE()")); 045 } 046 047 /** 048 * @see net.sf.hajdbc.dialect.StandardDialect#parseSequence(java.lang.String) 049 */ 050 @Override 051 public String parseSequence(String sql) 052 { 053 return null; 054 } 055 056 /** 057 * @see net.sf.hajdbc.dialect.StandardDialect#getSequences(java.sql.DatabaseMetaData) 058 */ 059 @Override 060 public Collection<QualifiedName> getSequences(DatabaseMetaData metaData) throws SQLException 061 { 062 return Collections.emptyList(); 063 } 064 065 /** 066 * Deferrability clause is not supported. 067 * @see net.sf.hajdbc.dialect.StandardDialect#createForeignKeyConstraintFormat() 068 */ 069 @Override 070 protected String createForeignKeyConstraintFormat() 071 { 072 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}"; 073 } 074 075 /** 076 * @see net.sf.hajdbc.dialect.StandardDialect#createUniqueConstraintFormat() 077 */ 078 @Override 079 protected String createUniqueConstraintFormat() 080 { 081 return "ALTER TABLE {1} ADD UNIQUE {0} ({2})"; 082 } 083 084 /** 085 * @see net.sf.hajdbc.dialect.StandardDialect#dropForeignKeyConstraintFormat() 086 */ 087 @Override 088 protected String dropForeignKeyConstraintFormat() 089 { 090 return "ALTER TABLE {1} DROP FOREIGN KEY {0}"; 091 } 092 093 /** 094 * @see net.sf.hajdbc.dialect.StandardDialect#dropUniqueConstraintFormat() 095 */ 096 @Override 097 protected String dropUniqueConstraintFormat() 098 { 099 return "ALTER TABLE {1} DROP INDEX {0}"; 100 } 101 102 /** 103 * @see net.sf.hajdbc.dialect.StandardDialect#alterIdentityColumnFormat() 104 */ 105 @Override 106 protected String alterIdentityColumnFormat() 107 { 108 return "ALTER TABLE {0} AUTO_INCREMENT = {2}"; 109 } 110 111 /** 112 * @see net.sf.hajdbc.dialect.StandardDialect#currentDatePattern() 113 */ 114 @Override 115 protected String currentDatePattern() 116 { 117 return super.currentDatePattern() + "|(?<=\\W)CURDATE\\s*\\(\\s*\\)"; 118 } 119 120 /** 121 * @see net.sf.hajdbc.dialect.StandardDialect#currentTimePattern() 122 */ 123 @Override 124 protected String currentTimePattern() 125 { 126 return super.currentTimePattern() + "|(?<=\\W)CURTIME\\s*\\(\\s*\\)"; 127 } 128 129 /** 130 * @see net.sf.hajdbc.dialect.StandardDialect#currentTimestampPattern() 131 */ 132 @Override 133 protected String currentTimestampPattern() 134 { 135 return super.currentTimestampPattern() + "|(?<=\\W)NOW\\s*\\(\\s*\\)|(?<=\\W)SYSDATE\\s*\\(\\s*\\)"; 136 } 137 138 /** 139 * @see net.sf.hajdbc.dialect.StandardDialect#dateLiteralFormat() 140 */ 141 @Override 142 protected String dateLiteralFormat() 143 { 144 return this.timestampLiteralFormat(); 145 } 146 147 /** 148 * @see net.sf.hajdbc.dialect.StandardDialect#timeLiteralFormat() 149 */ 150 @Override 151 protected String timeLiteralFormat() 152 { 153 return this.timestampLiteralFormat(); 154 } 155 156 /** 157 * @see net.sf.hajdbc.dialect.StandardDialect#timestampLiteralFormat() 158 */ 159 @Override 160 protected String timestampLiteralFormat() 161 { 162 return "''{0}''"; 163 } 164}