org.opennms.bb.common.filter.sql
Class SQLExpressionMapper

java.lang.Object
  |
  +--org.opennms.bb.common.filter.sql.SQLExpressionMapper

public class SQLExpressionMapper
extends Object

This class is responsible for mapping the different parts of the filter expressions into the SQL equivalent. As pieces of the expression are parsed the from and where clauses will be built. This information will be passed on to a SQLConstruct object by the parser when the expression has been fully parsed.

Version:
$Revision: 1.10 $
Author:
Jason Johns, OpenNMS

Field Summary
private  HashMap columnsNeeded
          This map holds the list of all columns that need to appear in the SELECT clause of the SQL statement being built.
private  Hashtable columnTableMap
          This table holds the list of all non-hidden columns that can appear in the tables in PollerDatabaseSchema.xml.
private  Hashtable columnViewRef
          This table holds the list of all columns that reference a view in PollerDatabaseSchema.xml.
static int IDENT
           
static int NUMBER
          Constants to identify the different types of terminals that can appear in an expression.
private  FilterSchemaParser schema
          This holds the instance of the parser of the PollerDatabaseSchema.xml file.
static int STRING
           
private  Hashtable tableList
          This table holds the list of table names and FilterTable objects associated with them that were extracted from the PollerDatabaseSchema.xml file.
private  HashMap tablesNeeded
          This map holds the list of all tables that need to appear in the FROM clause of the SQL statement being built.
private  Hashtable viewList
          This table holds the list of view names and FilterTable objects associated with all views that were extracted from the PollerDatabaseSchema.xml file.
private  Hashtable virtualColumns
          This table holds the list of all virtual columns that can appear in the tables in PollerDatabaseSchema.xml.
 
Constructor Summary
SQLExpressionMapper()
          Default constructor, initalizes the data structures and grabs an instance of the FilterSchemaParser to match table and column names.
 
Method Summary
 void addSelectColumn(String columnName)
          This method takes a column name and adds it to the data structure keeping the list of columns to include in the SELECT clause along with the table name associated with this column (later the column name will be appended to the table name in the actual SQL statement).
 void addTable(FilterTable aTable)
          This method adds a table keyed by its name to the tableList hashmap.
 void addTableByColumnName(String aColumnName)
          This method adds a table to the list of tables needed if only a column in the table name is known.
 void addTableByTableName(String aTableName)
          This method adds a table to the list of tables needed if only the table name is known.
 void checkIPNumber(Integer n)
          This method checks to ensure that a number appearing in an IP address is within the 0-255 range.
private  String formRangeCompare(Range range, String column)
          Deprecated.  
 HashMap getColumnsNeeded()
          This method returns the list of all columns that need to appear in the SELECT clause.
 StringBuffer getComparisonOp(int op)
          This method converts a comparision operator to the SQL equivalent.
 StringBuffer getIPSelect(String columnName, String ipLikeString)
          Deprecated.  
 StringBuffer getLogicalClause(int op)
          This method converts the logical operation symbols into the SQL equivalent.
 StringBuffer getSubSelect(String columnName, FilterTable view, String ipLikeString)
          Deprecated.  
 HashMap getTablesNeeded()
          This method returns the list of all tables that need to appear in the FROM clause.
 StringBuffer getValueString(int type, String value)
          This method converts a terminal value from an expression into what it should be in the SQL statement.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

NUMBER

public static final int NUMBER
Constants to identify the different types of terminals that can appear in an expression. These are used in the getValueString(int,String) method.

IDENT

public static final int IDENT

STRING

public static final int STRING

tableList

private Hashtable tableList
This table holds the list of table names and FilterTable objects associated with them that were extracted from the PollerDatabaseSchema.xml file.

viewList

private Hashtable viewList
This table holds the list of view names and FilterTable objects associated with all views that were extracted from the PollerDatabaseSchema.xml file.

tablesNeeded

private HashMap tablesNeeded
This map holds the list of all tables that need to appear in the FROM clause of the SQL statement being built. The tables that need to appear won't always be in the tableList hashtable above, for instance in the case of a virtual column linking to a table that is hidden.

columnsNeeded

private HashMap columnsNeeded
This map holds the list of all columns that need to appear in the SELECT clause of the SQL statement being built. The key is the column name and the value stored with it will be its table name.

columnTableMap

private Hashtable columnTableMap
This table holds the list of all non-hidden columns that can appear in the tables in PollerDatabaseSchema.xml.

virtualColumns

private Hashtable virtualColumns
This table holds the list of all virtual columns that can appear in the tables in PollerDatabaseSchema.xml.

columnViewRef

private Hashtable columnViewRef
This table holds the list of all columns that reference a view in PollerDatabaseSchema.xml.

schema

private FilterSchemaParser schema
This holds the instance of the parser of the PollerDatabaseSchema.xml file.
Constructor Detail

SQLExpressionMapper

public SQLExpressionMapper()
Default constructor, initalizes the data structures and grabs an instance of the FilterSchemaParser to match table and column names.
Method Detail

addSelectColumn

public void addSelectColumn(String columnName)
This method takes a column name and adds it to the data structure keeping the list of columns to include in the SELECT clause along with the table name associated with this column (later the column name will be appended to the table name in the actual SQL statement). The table is then added to the data structure keeping a list of tables needed for the where clause.
Parameters:
String - columnName, column name to be added to the select clause

getLogicalClause

public StringBuffer getLogicalClause(int op)
This method converts the logical operation symbols into the SQL equivalent.
Parameters:
int - op, constant BBRulesSymbol identifier for a logical operand
Returns:
StringBuffer, the SQL equivalent of the op param

getValueString

public StringBuffer getValueString(int type,
                                   String value)
                            throws FailedParseException
This method converts a terminal value from an expression into what it should be in the SQL statement. In the case of a column being discovered the associated table is added to the list of tables that need to appear in the FROM clause. In the case of a virtual column the substituion for the column name is returned with the appropriate tables added to the tables needed.
Parameters:
int - type, the type of data of the terminal
String - value, an terminal from the expression rule
Returns:
StringBuffer, a translation of the terminal
Throws:
FailedParseException -  

addTable

public void addTable(FilterTable aTable)
This method adds a table keyed by its name to the tableList hashmap. The hashmap is used to keep duplicate values from being entered into the data structure.
Parameters:
FilterTable - aTable, table object to be added to the hashmap

addTableByColumnName

public void addTableByColumnName(String aColumnName)
This method adds a table to the list of tables needed if only a column in the table name is known.
Parameters:
String - aColumnName, the name of the column of the table to be added to the hashmap

addTableByTableName

public void addTableByTableName(String aTableName)
This method adds a table to the list of tables needed if only the table name is known.
Parameters:
String - aTableName, the name of the table to be added to the hashmap

getComparisonOp

public StringBuffer getComparisonOp(int op)
This method converts a comparision operator to the SQL equivalent.
Parameters:
int - op, constant BBRulesSymbol identifier for a logical operand
Returns:
StringBuffer, the SQL equivalent for op

checkIPNumber

public void checkIPNumber(Integer n)
                   throws IPOutOfRangeException
This method checks to ensure that a number appearing in an IP address is within the 0-255 range.
Parameters:
Integer - n, an integer from an ip octet
Throws:
IPOutOfRangeException -  

getSubSelect

public StringBuffer getSubSelect(String columnName,
                                 FilterTable view,
                                 String ipLikeString)
Deprecated.  

This method constructs a sub select for the IPLIKE operator to determine the ip addresses that match the ipLikeString mask from an expression rule. The subselect will use views that are predefined in the database to compare each octet range to those in the view. The views will constructed such that the ip similar field being search on for a given table will become one column of the view with four octet columns. The view name will be taken from the PollerDatabaseSchema.xml.
Parameters:
String - columnName, the name of the ip similar column being searched
FilterTable - view, the view object from the schema parser
String - ipLikeString, the ip mask string
Returns:
StringBuffer, the completed subselect for the IPLIKE operator

formRangeCompare

private String formRangeCompare(Range range,
                                String column)
Deprecated.  

This method determines if an octet needs to be compared against a single value or be compared between a range.
Parameters:
Range - range, the range object encapulating the octet information
String - column, the name of the octet column being compared
Returns:
String, the completed comparision line

getIPSelect

public StringBuffer getIPSelect(String columnName,
                                String ipLikeString)
Deprecated.  

This method returns the subselect string which will select all ip addresses matching the ipLikeString.
Parameters:
String - columnName, the column name occuring in the IPLIKE operation
String - ipLikeString, the ip mask occuring in the IPLIKE operation
Returns:
StringBuffer, the complete conversion of an IPLIKE operator into SQL

getTablesNeeded

public HashMap getTablesNeeded()
This method returns the list of all tables that need to appear in the FROM clause.

getColumnsNeeded

public HashMap getColumnsNeeded()
This method returns the list of all columns that need to appear in the SELECT clause.