Exemplo n.º 1
0
 /**
  * @param function Function instance
  * @return the function ALIAS, name of the function in SQL engine
  */
 public static String getAlias(Function function) {
   String functionAlias = getStringProperty(function, Function.PROP_NAME);
   if (!functionAlias.isEmpty()) {
     return functionAlias;
   }
   return function.getClass().getSimpleName();
 }
Exemplo n.º 2
0
 /**
  * Remove the specified function from the provided DataBase connection
  *
  * @param st Active statement
  * @param function function to remove
  * @throws SQLException
  */
 public static void unRegisterFunction(Statement st, Function function) throws SQLException {
   String functionAlias = getStringProperty(function, Function.PROP_NAME);
   if (functionAlias.isEmpty()) {
     functionAlias = function.getClass().getSimpleName();
   }
   st.execute("DROP ALIAS IF EXISTS " + functionAlias);
 }
Exemplo n.º 3
0
  /**
   * Create java code to add function copy paste into GeoSpatialFunctionsAddRemove to upload it
   *
   * @param st SQL Statement
   * @param function Function instance
   * @param packagePrepend For OSGi environment only, use Bundle-SymbolicName:Bundle-Version:
   * @param dropAlias Drop alias if exists before define it.
   */
  public static void registerFunction(
      Statement st, Function function, String packagePrepend, boolean dropAlias)
      throws SQLException {
    String functionClass = function.getClass().getName();
    String functionAlias = getAlias(function);

    if (function instanceof ScalarFunction) {
      ScalarFunction scalarFunction = (ScalarFunction) function;
      String functionName = scalarFunction.getJavaStaticMethod();
      if (dropAlias) {
        st.execute("DROP ALIAS IF EXISTS " + functionAlias);
      }
      String deterministic = "";
      if (getBooleanProperty(function, ScalarFunction.PROP_DETERMINISTIC, false)) {
        deterministic = " DETERMINISTIC";
      }
      String nobuffer = "";
      if (getBooleanProperty(function, ScalarFunction.PROP_NOBUFFER, false)) {
        nobuffer = " NOBUFFER";
      }
      // Create alias, H2 does not support prepare statement on create alias
      st.execute(
          "CREATE ALIAS IF NOT EXISTS "
              + functionAlias
              + deterministic
              + nobuffer
              + " FOR \""
              + packagePrepend
              + functionClass
              + "."
              + functionName
              + "\"");
      // Set comment
      String functionRemarks = getStringProperty(function, Function.PROP_REMARKS);
      if (!functionRemarks.isEmpty()) {
        PreparedStatement ps =
            st.getConnection().prepareStatement("COMMENT ON ALIAS " + functionAlias + " IS ?");
        ps.setString(1, functionRemarks);
        ps.execute();
      }
    } else if (function instanceof Aggregate) {
      st.execute(
          "CREATE AGGREGATE IF NOT EXISTS "
              + functionAlias
              + " FOR \""
              + packagePrepend
              + functionClass
              + "\"");
    } else {
      throw new SQLException("Unsupported function " + functionClass);
    }
  }