View Javadoc

1   // Copyright 2011 Leo Przybylski. All rights reserved.
2   //
3   // Redistribution and use in source and binary forms, with or without modification, are
4   // permitted provided that the following conditions are met:
5   //
6   //    1. Redistributions of source code must retain the above copyright notice, this list of
7   //       conditions and the following disclaimer.
8   //
9   //    2. Redistributions in binary form must reproduce the above copyright notice, this list
10  //       of conditions and the following disclaimer in the documentation and/or other materials
11  //       provided with the distribution.
12  //
13  // THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
14  // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
15  // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
16  // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17  // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19  // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20  // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
21  // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22  //
23  // The views and conclusions contained in the software and documentation are those of the
24  // authors and should not be interpreted as representing official policies, either expressed
25  // or implied, of Leo Przybylski.
26  package liquibase.database.typeconversion.ext;
27  
28  import liquibase.database.structure.Column;
29  import liquibase.database.structure.type.*;
30  import liquibase.database.Database;
31  import liquibase.database.core.H2Database;
32  import liquibase.database.core.HsqlDatabase;
33  import liquibase.util.StringUtils;
34  
35  import static liquibase.ext.Constants.EXTENSION_PRIORITY;
36  
37  /**
38   *
39   * @author Leo Przybylski
40   */
41  public class HsqlTypeConverter extends liquibase.database.typeconversion.core.HsqlTypeConverter {
42  
43      public int getPriority() {
44          return EXTENSION_PRIORITY;
45      }
46  
47      @Override
48      public NumberType getNumberType() {
49          return new NumberType("NUMERIC");
50      }
51  
52      public String convertToDatabaseTypeString(Column referenceColumn, Database database) {
53          if (referenceColumn.getTypeName().toLowerCase().indexOf("text") > -1) {
54              return getClobType().getDataTypeName();
55          }
56          else if (referenceColumn.getTypeName().toLowerCase().indexOf("varchar") > -1) {
57              final VarcharType type = getVarcharType();
58              type.setFirstParameter("" + referenceColumn.getColumnSize());
59              return type.toString();
60          }
61          else if (referenceColumn.getTypeName().toLowerCase().indexOf("num") > -1) {
62              final NumberType type = new NumberType("NUMERIC");
63              type.setFirstParameter("" + referenceColumn.getColumnSize());
64              return type.toString();
65          }
66  
67          return super.convertToDatabaseTypeString(referenceColumn, database);
68      }
69  
70  
71      /**
72       * Returns the database-specific datatype for the given column configuration.
73       * This method will convert some generic column types (e.g. boolean, currency) to the correct type
74       * for the current database.
75       */
76      public DataType getDataType(String columnTypeString, Boolean autoIncrement) {
77          // Parse out data type and precision
78          // Example cases: "CLOB", "java.sql.Types.CLOB", "CLOB(10000)", "java.sql.Types.CLOB(10000)
79          String dataTypeName = null;
80          String precision = null;
81          String additionalInformation = null;
82          if (columnTypeString.startsWith("java.sql.Types") && columnTypeString.contains("(")) {
83              precision = columnTypeString.substring(columnTypeString.indexOf("(") + 1, columnTypeString.indexOf(")"));
84              dataTypeName = columnTypeString.substring(columnTypeString.lastIndexOf(".") + 1, columnTypeString.indexOf("("));
85          } else if (columnTypeString.startsWith("java.sql.Types")) {
86              dataTypeName = columnTypeString.substring(columnTypeString.lastIndexOf(".") + 1);
87          } else if (columnTypeString.contains("(")) {
88              precision = columnTypeString.substring(columnTypeString.indexOf("(") + 1, columnTypeString.indexOf(")"));
89              dataTypeName = columnTypeString.substring(0, columnTypeString.indexOf("("));
90          } else {
91              dataTypeName = columnTypeString;
92          }
93          if (columnTypeString.contains(")")) {
94              additionalInformation = StringUtils.trimToNull(columnTypeString.replaceFirst(".*\\)", ""));
95          }
96  
97          return getDataType(columnTypeString, autoIncrement, dataTypeName, precision, additionalInformation);
98      }
99  
100 }