MySQLDB_5_7.java

  1. /*
  2.  * Copyright 2013 Gregory Graham.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package nz.co.gregs.dbvolution.databases;

  17. import java.sql.SQLException;
  18. import java.sql.Statement;
  19. import java.util.regex.Pattern;
  20. import javax.sql.DataSource;
  21. import nz.co.gregs.dbvolution.databases.settingsbuilders.MySQL_5_7SettingsBuilder;
  22. import nz.co.gregs.dbvolution.databases.supports.SupportsPolygonDatatype;
  23. import nz.co.gregs.dbvolution.exceptions.ExceptionDuringDatabaseFeatureSetup;
  24. import nz.co.gregs.dbvolution.internal.mysql.MigrationFunctions;
  25. import nz.co.gregs.dbvolution.internal.query.StatementDetails;

  26. /**
  27.  * A DBDatabase tweaked for MySQL databases
  28.  *
  29.  * @author Gregory Graham
  30.  */
  31. public class MySQLDB_5_7 extends DBDatabaseImplementation implements SupportsPolygonDatatype {

  32.     public final static String MYSQLDRIVERNAME = "com.mysql.jdbc.Driver";
  33.     private static final long serialVersionUID = 1l;
  34.     public static final int DEFAULT_PORT = 3306;
  35.     private final MySQL_5_7SettingsBuilder urlProcessor = new MySQL_5_7SettingsBuilder();

  36.     /**
  37.      * Creates a {@link DBDatabase } instance for the data source.
  38.      *
  39.      * @param ds    ds
  40.      * @throws java.sql.SQLException database errors
  41.      */
  42.     public MySQLDB_5_7(DataSource ds) throws SQLException {
  43.         super(
  44.                 new MySQL_5_7SettingsBuilder().setDataSource(ds)
  45.         );
  46. //      super(new MySQLDBDefinition_5_7(), MYSQLDRIVERNAME, ds);
  47.     }
  48.    
  49.     /**
  50.      * Creates a {@link DBDatabase } instance for the data source.
  51.      *
  52.      * @param ds    ds
  53.      * @throws java.sql.SQLException database errors
  54.      */
  55.     public MySQLDB_5_7(MySQL_5_7SettingsBuilder ds) throws SQLException {
  56.         super(ds);
  57.     }

  58.     /**
  59.      * Creates a {@link DBDatabase } instance for the data source.
  60.      *
  61.      * @param dcs the settings required to connect to the database
  62.      * @throws java.sql.SQLException database errors
  63.      */
  64.     public MySQLDB_5_7(DatabaseConnectionSettings dcs) throws SQLException {
  65.         this(new MySQL_5_7SettingsBuilder().fromSettings(dcs));
  66.     }

  67.     /**
  68.      * Creates DBDatabase suitable for use with MySQL attached to the supplied
  69.      * JDBC URL, username, and password.
  70.      *
  71.      * @param jdbcURL jdbcURL
  72.      * @param password password
  73.      * @param username username
  74.      * @throws java.sql.SQLException database errors
  75.      */
  76.     public MySQLDB_5_7(String jdbcURL, String username, String password) throws SQLException {
  77.         this(new MySQL_5_7SettingsBuilder().fromJDBCURL(jdbcURL, username, password));
  78.     }

  79.     /**
  80.      * Creates DBDatabase suitable for use with MySQL attached to the supplied
  81.      * JDBC URL, username, and password.
  82.      *
  83.      * @param server the server to connect to.
  84.      * @param port the port to connect on.
  85.      * @param databaseName the database that is required on the server.
  86.      * @param username the user to login as.
  87.      * @param password the password required to login successfully.
  88.      * @throws java.sql.SQLException database errors
  89.      */
  90.     public MySQLDB_5_7(String server, long port, String databaseName, String username, String password) throws SQLException {
  91.         this(
  92. new MySQL_5_7SettingsBuilder()
  93.                         .setHost(server)
  94.                         .setPort(port)
  95.                         .setDatabaseName(databaseName)
  96.                         .setUsername(username)
  97.                         .setPassword(password)
  98.         //              "jdbc:mysql://" + server + ":" + port + "/" + databaseName + "?createDatabaseIfNotExist=true&useUnicode=yes&characterEncoding=utf8&characterSetResults=utf8&verifyServerCertificate=false&useSSL=true",
  99.         );
  100.     }

  101.     @Override
  102.     public DBDatabase clone() throws CloneNotSupportedException {
  103.         return super.clone(); //To change body of generated methods, choose Tools | Templates.
  104.     }

  105.     @Override
  106.     public void addDatabaseSpecificFeatures(Statement statement) throws ExceptionDuringDatabaseFeatureSetup {
  107.         for (MigrationFunctions fn : MigrationFunctions.values()) {
  108.             fn.add(statement);
  109.         }
  110.     }

  111.     @Override
  112.     public Integer getDefaultPort() {
  113.         return 3306;
  114.     }

  115.     private final static Pattern FUNCTION_DOES_NOT_EXISTS = Pattern.compile("FUNCTION [^ ]* does not exist");
  116.     private final static Pattern TABLE_ALREADY_EXISTS = Pattern.compile("Table '[^']*' already exists");

  117.     @Override
  118.     public ResponseToException addFeatureToFixException(Exception exp, QueryIntention intent, StatementDetails details) throws Exception {
  119.         if (intent.is(QueryIntention.DROP_TABLE) && TABLE_ALREADY_EXISTS.matcher(exp.getMessage()).matches()) {
  120.             return ResponseToException.SKIPQUERY;
  121.         } else if (FUNCTION_DOES_NOT_EXISTS.matcher(exp.getMessage()).matches()) {

  122.         }
  123.         return super.addFeatureToFixException(exp, intent, details);
  124.     }

  125.     @Override
  126.     public MySQL_5_7SettingsBuilder getURLInterpreter() {
  127.         return urlProcessor;
  128.     }

  129. }