001: package liquibase.database;
002:
003: import liquibase.database.sql.RawSqlStatement;
004: import liquibase.database.sql.SqlStatement;
005: import liquibase.exception.JDBCException;
006:
007: import java.sql.Connection;
008:
009: /**
010: * Encapsulates MySQL database support.
011: */
012: public class MySQLDatabase extends AbstractDatabase {
013: public static final String PRODUCT_NAME = "MySQL";
014:
015: public String getProductName() {
016: return "MySQL";
017: }
018:
019: public String getTypeName() {
020: return "mysql";
021: }
022:
023: public String getConnectionUsername() throws JDBCException {
024: return super .getConnectionUsername().replaceAll("\\@.*", "");
025: }
026:
027: public boolean isCorrectDatabaseImplementation(Connection conn)
028: throws JDBCException {
029: return PRODUCT_NAME
030: .equalsIgnoreCase(getDatabaseProductName(conn));
031: }
032:
033: public String getDefaultDriver(String url) {
034: if (url.startsWith("jdbc:mysql")) {
035: return "com.mysql.jdbc.Driver";
036: }
037: return null;
038: }
039:
040: public String getBooleanType() {
041: return "TINYINT(1)";
042: }
043:
044: public String getCurrencyType() {
045: return "DECIMAL";
046: }
047:
048: public String getUUIDType() {
049: return "CHAR(36)";
050: }
051:
052: public String getClobType() {
053: return "TEXT";
054: }
055:
056: public String getBlobType() {
057: return "BLOB";
058: }
059:
060: public String getDateTimeType() {
061: return "DATETIME";
062: }
063:
064: public boolean supportsSequences() {
065: return false;
066: }
067:
068: public boolean supportsInitiallyDeferrableColumns() {
069: return false;
070: }
071:
072: public String getCurrentDateTimeFunction() {
073: return "NOW()";
074: }
075:
076: public String getLineComment() {
077: return "--";
078: }
079:
080: public String getFalseBooleanValue() {
081: return "0";
082: }
083:
084: public String getTrueBooleanValue() {
085: return "1";
086: }
087:
088: public String getConcatSql(String... values) {
089: StringBuffer returnString = new StringBuffer();
090: returnString.append("CONCAT_WS(");
091: for (String value : values) {
092: returnString.append(value).append(", ");
093: }
094:
095: return returnString.toString().replaceFirst(", $", ")");
096: }
097:
098: public boolean supportsTablespaces() {
099: return false;
100: }
101:
102: protected String getDefaultDatabaseSchemaName()
103: throws JDBCException {
104: return super .getDefaultDatabaseSchemaName().replaceFirst(
105: "\\@.*", "");
106: }
107:
108: public String convertRequestedSchemaToSchema(String requestedSchema)
109: throws JDBCException {
110: if (requestedSchema == null) {
111: return getDefaultDatabaseSchemaName();
112: }
113: return requestedSchema;
114: }
115:
116: public String convertRequestedSchemaToCatalog(String requestedSchema)
117: throws JDBCException {
118: return requestedSchema;
119: }
120:
121: public SqlStatement getViewDefinitionSql(String schemaName,
122: String viewName) throws JDBCException {
123: return new RawSqlStatement(
124: "select view_definition from information_schema.views where table_name='"
125: + viewName + "' AND table_schema='"
126: + schemaName + "'");
127: }
128:
129: public String escapeTableName(String schemaName, String tableName) {
130: if (schemaName != null) {
131: return "`" + schemaName + "`.`" + tableName + "`";
132: }
133: return "`" + tableName + "`";
134: }
135:
136: public String escapeColumnName(String columnName) {
137: return "`" + columnName + "`";
138: }
139:
140: public String escapeColumnNameList(String columnNames) {
141: StringBuffer sb = new StringBuffer();
142: for (String columnName : columnNames.split(", ")) {
143: if (sb.length() > 0) {
144: sb.append(", ");
145: }
146: sb.append("`").append(columnName).append("`");
147: }
148: return sb.toString();
149: }
150: }
|