Package stanford.netdb.field2sql
Class Field2SQL
- java.lang.Object
-
- stanford.netdb.field2sql.Field2SQL
-
public class Field2SQL extends java.lang.ObjectField2SQL 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 intDOMAIN_NAMEThis match is Stanford NetDB specific.static intLOG_NAMESame as DOMAIN_NAME, except that log names do not get a trailing dot added.static intNUMERICGenerated SQL assumes that column type is numeric.static intSTRINGGenerated SQL assumes that column type is string.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringbuildField(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 sqlstatic voidmain(java.lang.String[] args)SQLizer shell.static java.lang.StringparseField(int type, java.lang.String table, java.lang.String column, java.lang.String str, java.lang.String field_display_name)Same asparseField(...)with post_filter set to the default one, which performs no transormations on final field valuesstatic java.lang.StringparseField(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.StringparseIntField(java.lang.String table, java.lang.String column, java.lang.String str, java.lang.String field_display_name)Same asparseField(...)with type = INTstatic java.lang.StringparseStringField(java.lang.String table, java.lang.String column, java.lang.String str, java.lang.String field_display_name)Same asparseField(...)with type = STRING
-
-
-
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.ExceptionUses FieldPostFilter as an expression builder to build sql- Parameters:
type- one of NUMERIC, STRING, DOMAIN_NAME, LOG_NAMEtable- name of the table to be figured in reaulting SQL expressioncolumn- column to be used in '=' or 'LIKE' expressionstr- string to parsefield_display_name- parameter which is used only in error messages if givenexpr_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_NAMEtable- name of the table to be figured in reaulting SQL expressioncolumn- column to be used in '=' or 'LIKE' expressionstr- string to parsefield_display_name- parameter which is used only in error messages if givenpost_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.ExceptionSame asparseField(...)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.ExceptionSame asparseField(...)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.ExceptionSame asparseField(...)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.
-
-