AbstractMSSQLServerSettingsBuilder.java

  1. /*
  2.  * Copyright 2019 Gregory Graham.
  3.  *
  4.  * Commercial licenses are available, please contact info@gregs.co.nz for details.
  5.  *
  6.  * This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
  7.  * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/
  8.  * or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
  9.  *
  10.  * You are free to:
  11.  *     Share - copy and redistribute the material in any medium or format
  12.  *     Adapt - remix, transform, and build upon the material
  13.  *
  14.  *     The licensor cannot revoke these freedoms as long as you follow the license terms.              
  15.  *     Under the following terms:
  16.  *                
  17.  *         Attribution -
  18.  *             You must give appropriate credit, provide a link to the license, and indicate if changes were made.
  19.  *             You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  20.  *         NonCommercial -
  21.  *             You may not use the material for commercial purposes.
  22.  *         ShareAlike -
  23.  *             If you remix, transform, or build upon the material,
  24.  *             you must distribute your contributions under the same license as the original.
  25.  *         No additional restrictions -
  26.  *             You may not apply legal terms or technological measures that legally restrict others from doing anything the
  27.  *             license permits.
  28.  *
  29.  * Check the Creative Commons website for any details, legalese, and updates.
  30.  */
  31. package nz.co.gregs.dbvolution.databases.settingsbuilders;

  32. import java.util.HashMap;
  33. import java.util.Map;
  34. import nz.co.gregs.dbvolution.databases.DBDatabase;
  35. import nz.co.gregs.dbvolution.databases.DatabaseConnectionSettings;
  36. import nz.co.gregs.dbvolution.databases.MSSQLServerDB;
  37. import nz.co.gregs.dbvolution.databases.definitions.DBDefinition;
  38. import nz.co.gregs.dbvolution.databases.definitions.MSSQLServerDBDefinition;

  39. /**
  40.  *
  41.  * @author gregorygraham
  42.  * @param <SELF> the class of the object returned by most methods, this should be the Class of "this"
  43.  * @param <DATABASE> the class returned by {@link #getDBDatabase}
  44.  */
  45. public abstract class AbstractMSSQLServerSettingsBuilder<SELF extends AbstractMSSQLServerSettingsBuilder<SELF, DATABASE>, DATABASE extends DBDatabase>
  46.         extends AbstractVendorSettingsBuilder<SELF, DATABASE>
  47.         implements InstanceCapableSettingsBuilder<SELF, DATABASE>,
  48.         RemoteCapableSettingsBuilder<SELF, DATABASE>,
  49.         NamedDatabaseCapableSettingsBuilder<SELF, DATABASE>,
  50.         ExtrasCapableSettingsBuilder<SELF, DATABASE> {

  51.     protected static final HashMap<String, String> DEFAULT_EXTRAS_MAP = new HashMap<>();
  52.     private static final long serialVersionUID = 1L;

  53.     @Override
  54.     public Map<String, String> getDefaultConfigurationExtras() {
  55.         return DEFAULT_EXTRAS_MAP;
  56.     }

  57.     @Override
  58.     public String getDefaultDriverName() {
  59.         return MSSQLServerDB.SQLSERVERDRIVERNAME;
  60.     }

  61.     @Override
  62.     public DBDefinition getDefaultDefinition() {
  63.         return new MSSQLServerDBDefinition();
  64.     }

  65.     @Override
  66.     public DatabaseConnectionSettings generateSettingsInternal(String jdbcURL, DatabaseConnectionSettings set) {
  67.         //      DatabaseConnectionSettings set = getEmptySettings();
  68.         String noPrefix = jdbcURL.replaceAll("^" + getJDBCURLPreamble(), "");
  69.         final String[] splitOnBackslash = noPrefix.split("\\\\", 2);
  70.         final String host = splitOnBackslash[0];
  71.         set.setHost(host);
  72.         if (splitOnBackslash.length > 1) {
  73.             final String[] splitOnColon = splitOnBackslash[1].split(":");
  74.             if (splitOnColon.length > 1) {
  75.                 final String port = splitOnColon[1];
  76.                 set.setPort(port);
  77.             }
  78.             final String instance = splitOnColon[0];
  79.             set.setInstance(instance);
  80.         }
  81.         if (jdbcURL.matches(";")) {
  82.             String extrasString = jdbcURL.split(";", 2)[1];
  83.             set.setExtras(DatabaseConnectionSettings.decodeExtras(extrasString, "", "=", ";", ""));
  84.         }
  85.         set.setSchema("");
  86.         return set;
  87.     }

  88.     protected String getJDBCURLPreamble() {
  89.         return "jdbc:sqlserver://";
  90.     }

  91.     @Override
  92.     protected String getJDBCURLPreamble(DatabaseConnectionSettings settings) {
  93.         return getJDBCURLPreamble();
  94.     }

  95.     @Override
  96.     public String encodeHost(DatabaseConnectionSettings settings) {
  97.         final String databaseName = settings.getDatabaseName();
  98.         final String instance = settings.getInstance();
  99.         final String encodeExtras = encodeExtras(settings, ";", "=", ";", "");
  100.         final String databaseNameClause = databaseName == null || databaseName.isEmpty() ? "" : "databaseName=" + databaseName + ";";
  101.         final String instanceClause = instance != null && !instance.isEmpty() ? "\\" + instance : "";
  102.         final String hostClause = settings.getHost();
  103.         final String port = settings.getPort();
  104.         final String urlFromSettings    = hostClause
  105.                 + instanceClause
  106.                 + ":" + port + ";"
  107.                 + databaseNameClause
  108.                 + encodeExtras;
  109.         return urlFromSettings;
  110.     }

  111.     @Override
  112.     public DatabaseConnectionSettings setDefaultsInternal(DatabaseConnectionSettings settings) {
  113.         return settings;
  114.     }

  115.     @Override
  116.     public Integer getDefaultPort() {
  117.         return 1433;
  118.     }

  119. }