Class Field2SQL


  • public class Field2SQL
    extends java.lang.Object
    Field2SQL builds a SQL select statement from an input string. It supports string and numeric input and the logical operators 'and', 'or' and 'not'. Wildcards '*', '%', '?' and '_' are supported in string input. No wildcards are supported in numeric searches. Escape wildcard characters with a backslash, '\'. Escape backslashes it the same way. Strings containing spaces must be enclosed in double quotes. String regular expression searches are supported as well, enclose the string in forward slashes instead of double quotes. The resulting SQL is targeted toward Oracle regular expressions. Here are a few examples:

    a or b

        ( ( select id from Record where name = 'a' ) UNION ( select id from Record where name = 'b' ) )

    not a and not b

        ( ( select id from Record MINUS ( select id from Record where name = 'a' ) ) INTERSECT ( select id from Record MINUS ( select id from Record where name = 'b' ) ) )

    "a b c" or a\\*

        ( ( select id from Record where name = 'a b c' ) UNION ( select id from Record where name LIKE 'a\\%' ESCAPE '\' ) )

    /^.*[[:digit:]]$/i

        ( select id from Record where REGEXP_LIKE(name, '^.*[[:digit:]]$', 'i') )

    Note: Unions and intersections are used in order to accommodate searches for objects with multi-value attributes.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DOMAIN_NAME
      This match is Stanford NetDB specific.
      static int LOG_NAME
      Same as DOMAIN_NAME, except that log names do not get a trailing dot added.
      static int NUMERIC
      Generated SQL assumes that column type is numeric.
      static int STRING
      Generated SQL assumes that column type is string.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String buildField​(int type, java.lang.String table, java.lang.String column, java.lang.String str, java.lang.String field_display_name, FieldPostFilter expr_builder)
      Uses FieldPostFilter as an expression builder to build sql
      static void main​(java.lang.String[] args)
      SQLizer shell.
      static java.lang.String parseField​(int type, java.lang.String table, java.lang.String column, java.lang.String str, java.lang.String field_display_name)
      Same as parseField(...) with post_filter set to the default one, which performs no transormations on final field values
      static java.lang.String parseField​(int type, java.lang.String table, java.lang.String column, java.lang.String str, java.lang.String field_display_name, java.lang.Object post_filter)  
      static java.lang.String parseIntField​(java.lang.String table, java.lang.String column, java.lang.String str, java.lang.String field_display_name)
      Same as parseField(...) with type = INT
      static java.lang.String parseStringField​(java.lang.String table, java.lang.String column, java.lang.String str, java.lang.String field_display_name)
      Same as parseField(...) with type = STRING
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • NUMERIC

        public static int NUMERIC
        Generated SQL assumes that column type is numeric.
      • STRING

        public static int STRING
        Generated SQL assumes that column type is string.
      • DOMAIN_NAME

        public static int DOMAIN_NAME
        This match is Stanford NetDB specific. Generated SQL assumes that column type is string and few additional transformations are applied.
      • LOG_NAME

        public static int LOG_NAME
        Same as DOMAIN_NAME, except that log names do not get a trailing dot added.
    • Method Detail

      • buildField

        public static java.lang.String buildField​(int type,
                                                  java.lang.String table,
                                                  java.lang.String column,
                                                  java.lang.String str,
                                                  java.lang.String field_display_name,
                                                  FieldPostFilter expr_builder)
                                           throws java.lang.Exception
        Uses FieldPostFilter as an expression builder to build sql
        Parameters:
        type - one of NUMERIC, STRING, DOMAIN_NAME, LOG_NAME
        table - name of the table to be figured in reaulting SQL expression
        column - column to be used in '=' or 'LIKE' expression
        str - string to parse
        field_display_name - parameter which is used only in error messages if given
        expr_builder - once the parsing is complete and each field value separated from source string, expr_builder.filter(...) method is called and its result is used as a 'where' expression, e.g. select id from table where `expr_builder.filter(table, column, field)`
        Throws:
        java.lang.Exception
        See Also:
        FieldPostFilter.filter(String table, String column, String field)
      • parseField

        public static java.lang.String parseField​(int type,
                                                  java.lang.String table,
                                                  java.lang.String column,
                                                  java.lang.String str,
                                                  java.lang.String field_display_name,
                                                  java.lang.Object post_filter)
                                           throws java.lang.Exception
        Parameters:
        type - one of NUMERIC, STRING, DOMAIN_NAME, LOG_NAME
        table - name of the table to be figured in reaulting SQL expression
        column - column to be used in '=' or 'LIKE' expression
        str - string to parse
        field_display_name - parameter which is used only in error messages if given
        post_filter - once the parsing is complete and each field value separated from source string, post_filter.filter(...) method is called and its result is used as a final field value. This variable must be either instance of FieldPostFilter or PostFilter
        Throws:
        java.lang.Exception
        See Also:
        FieldPostFilter.filter(String table, String column, String field), PostFilter
      • parseField

        public static java.lang.String parseField​(int type,
                                                  java.lang.String table,
                                                  java.lang.String column,
                                                  java.lang.String str,
                                                  java.lang.String field_display_name)
                                           throws java.lang.Exception
        Same as parseField(...) with post_filter set to the default one, which performs no transormations on final field values
        Throws:
        java.lang.Exception
      • parseStringField

        public static java.lang.String parseStringField​(java.lang.String table,
                                                        java.lang.String column,
                                                        java.lang.String str,
                                                        java.lang.String field_display_name)
                                                 throws java.lang.Exception
        Same as parseField(...) with type = STRING
        Throws:
        java.lang.Exception
      • parseIntField

        public static java.lang.String parseIntField​(java.lang.String table,
                                                     java.lang.String column,
                                                     java.lang.String str,
                                                     java.lang.String field_display_name)
                                              throws java.lang.Exception
        Same as parseField(...) with type = INT
        Throws:
        java.lang.Exception
      • main

        public static void main​(java.lang.String[] args)
        SQLizer shell. Run this to bring up sqlizer shell, where you can try different input parameters and see generated SQL.