MultiPoint2DFunctions.java

  1. /*
  2.  * Copyright 2015 gregorygraham.
  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.internal.postgres;

  17. import nz.co.gregs.dbvolution.internal.FeatureAdd;

  18. /**
  19.  *
  20.  *
  21.  * @author gregorygraham
  22.  */
  23. public enum MultiPoint2DFunctions implements FeatureAdd {
  24.     //MULTIPOINT(2 3,3 4)
  25.     //[ ( 2, 3 ), ( 3 , 4 ) ]

  26.     /**
  27.      *
  28.      */
  29.     ASLINE2D(Language.plpgsql, "path", "path1 geometry", "DECLARE \n"
  30.             + " tempText text;\n"
  31.             + "BEGIN\n"
  32.             + " tempText = regexp_replace(ST_ASTEXT(path1), $$MULTIPOINT$$, $$$$, $$g$$); \n"
  33.             + " tempText = regexp_replace(tempText, $$[ ]*,[ ]*$$, $$), ($$, $$g$$); \n"
  34.             + " tempText = regexp_replace(tempText, $$([ -+0-9.]*)[ ]+([-+0-9.]*)$$, $$\\1, \\2$$, $$g$$); \n"
  35.             + " tempText = $$[$$||tempText||$$]$$; \n"
  36.             + "   RETURN tempText;\n"
  37.             + "END;");

  38. //  private final String functionName;
  39.     private final String returnType;
  40.     private final String parameters;
  41.     private final String code;
  42.     private final Language language;

  43.     MultiPoint2DFunctions(Language language, String returnType, String parameters, String code) {
  44.         this.language = language;
  45.         this.returnType = returnType;
  46.         this.parameters = parameters;
  47.         this.code = code;
  48.     }

  49.     @Override
  50.     public String toString() {
  51.         return "DBV_MPOINT2DFN_" + name();
  52.     }

  53. //  /**
  54. //   *
  55. //   * @param stmt the database
  56. //   * @throws ExceptionDuringDatabaseFeatureSetup database errors
  57. //   */
  58. //  @SuppressFBWarnings(value = "SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE",
  59. //          justification = "The strings are actually constant but made dynamically")
  60. //  public void add(Statement stmt) throws ExceptionDuringDatabaseFeatureSetup {
  61. //      try {
  62. //          final String drop = "DROP FUNCTION " + this + "(" + this.parameters + ");";
  63. //          stmt.execute(drop);
  64. //      } catch (SQLException sqlex) {
  65. //          ;
  66. //      }
  67. //      final String createFunctionStatement = "CREATE OR REPLACE FUNCTION " + this + "(" + this.parameters + ")\n" + "    RETURNS " + this.returnType + " AS\n" + "'\n" + this.code + "'\n" + "LANGUAGE '" + language + "' IMMUTABLE;";
  68. //      try {
  69. //          stmt.execute(createFunctionStatement);
  70. //      } catch (Exception ex) {
  71. //          throw new ExceptionDuringDatabaseFeatureSetup("FAILED TO ADD FEATURE: " + name(), ex);
  72. //      }
  73. //  }
  74.     @Override
  75.     public String[] createSQL() {
  76.         if (!this.code.isEmpty()) {
  77.             return new String[]{
  78.                 "CREATE OR REPLACE FUNCTION " + this + "(" + this.parameters + ")\n" + "    RETURNS " + this.returnType + " AS\n" + "'\n" + this.code + "'\n" + "LANGUAGE '" + language + "' IMMUTABLE;"
  79.             };
  80.         }
  81.         return new String[]{};
  82.     }

  83.     @Override
  84.     public String[] dropSQL() {
  85.         if (!this.code.isEmpty()) {
  86.             return new String[]{
  87.                 "DROP FUNCTION " + this + "(" + this.parameters + ");"
  88.             };
  89.         }
  90.         return new String[]{};
  91.     }

  92. }