View Javadoc

1   package liquibase.database.core;
2   
3   import liquibase.database.AbstractDatabase;
4   import liquibase.database.DatabaseConnection;
5   import liquibase.exception.DatabaseException;
6   import liquibase.executor.ExecutorService;
7   import liquibase.statement.core.RawSqlStatement;
8   
9   /**
10   * Encapsulates MySQL database support.
11   */
12  public class MySQLDatabase extends AbstractDatabase {
13      public static final String PRODUCT_NAME = "MySQL";
14  
15      public String getTypeName() {
16          return "mysql";
17      }
18  
19      // todo: handle @Override
20      // public String getConnectionUsername() throws DatabaseException {
21      // return super.getConnection().getConnectionUserName().replaceAll("\\@.*", "");
22      // }
23  
24      public int getPriority() {
25          return PRIORITY_DEFAULT;
26      }
27  
28      public boolean isCorrectDatabaseImplementation(DatabaseConnection conn) throws DatabaseException {
29          return PRODUCT_NAME.equalsIgnoreCase(conn.getDatabaseProductName());
30      }
31  
32      public String getDefaultDriver(String url) {
33          if (url.startsWith("jdbc:mysql")) {
34              return "com.mysql.jdbc.Driver";
35          }
36          return null;
37      }
38  
39      @Override
40      public boolean supportsSequences() {
41          return false;
42      }
43  
44      public boolean supportsInitiallyDeferrableColumns() {
45          return false;
46      }
47  
48      public String getCurrentDateTimeFunction() {
49          if (currentDateTimeFunction != null) {
50              return currentDateTimeFunction;
51          }
52  
53          return "NOW()";
54      }
55  
56      @Override
57      public String getLineComment() {
58          return "--";
59      }
60  
61      @Override
62      public String getConcatSql(String... values) {
63          StringBuffer returnString = new StringBuffer();
64          returnString.append("CONCAT_WS(");
65          for (String value : values) {
66              returnString.append(value).append(", ");
67          }
68  
69          return returnString.toString().replaceFirst(", $", ")");
70      }
71  
72      public boolean supportsTablespaces() {
73          return false;
74      }
75  
76      @Override
77      protected String getDefaultDatabaseSchemaName() throws DatabaseException {
78          // return super.getDefaultDatabaseSchemaName().replaceFirst("\\@.*","");
79          return getConnection().getCatalog();
80      }
81  
82      @Override
83      public String convertRequestedSchemaToSchema(String requestedSchema) throws DatabaseException {
84          if (requestedSchema == null) {
85              return getDefaultDatabaseSchemaName();
86          }
87          return requestedSchema;
88      }
89  
90      @Override
91      public String convertRequestedSchemaToCatalog(String requestedSchema) throws DatabaseException {
92          return requestedSchema;
93      }
94  
95      @Override
96      public String escapeDatabaseObject(String objectName) {
97          return "`" + objectName + "`";
98      }
99  
100     @Override
101     public String escapeIndexName(String schemaName, String indexName) {
102         return escapeDatabaseObject(indexName);
103     }
104 
105     @Override
106     public boolean supportsForeignKeyDisable() {
107         return true;
108     }
109 
110     @Override
111     public boolean disableForeignKeyChecks() throws DatabaseException {
112         boolean enabled = ExecutorService.getInstance().getExecutor(this)
113                 .queryForInt(new RawSqlStatement("SELECT @@FOREIGN_KEY_CHECKS")) == 1;
114         ExecutorService.getInstance().getExecutor(this).execute(new RawSqlStatement("SET FOREIGN_KEY_CHECKS=0"));
115         return enabled;
116     }
117 
118     @Override
119     public void enableForeignKeyChecks() throws DatabaseException {
120         ExecutorService.getInstance().getExecutor(this).execute(new RawSqlStatement("SET FOREIGN_KEY_CHECKS=1"));
121     }
122 }