use of com.sun.identity.log.QueryElement in project OpenAM by OpenRock.
the class LogReadDBHandler method doMySQLQueryElement.
//
// create the "where..." clause from the QueryElements...
// the part that comes after "where ".
//
// for mysql (<v4 doesn't support union):
// select * from tbl1, tbl2, ..., tbln where ((tbl1.fld1 = 'val1')
// and (tbl1.fldx = 'valx')) or ((tbl2.fld1 = 'val1') and
// (tbl2.fld2 = 'val2')) or ... ((tbln.fld1 = 'val1') and
// (tbln.fld2 = 'val2)) [limit n]
//
private String doMySQLQueryElement(String tblname, ArrayList qes, String opStr) {
//
// get the columns/values to search on
//
int qrySize = qes.size();
QueryElement qe;
boolean moreThanOneQuery = false;
StringBuilder whereSBuf = new StringBuilder();
if (qrySize > 1) {
moreThanOneQuery = true;
whereSBuf.append("(");
}
//
for (int i = 0; i < qrySize; i++) {
qe = (QueryElement) qes.get(i);
String fldName = tblname + "." + qe.getFieldName();
String fldValue = qe.getFieldValue();
String relation;
int iRelation = qe.getRelation();
switch(iRelation) {
case QueryElement.GT:
relation = " > ";
break;
case QueryElement.LT:
relation = " < ";
break;
case QueryElement.EQ:
relation = " = ";
break;
case QueryElement.NE:
relation = " != ";
break;
case QueryElement.GE:
relation = " >= ";
break;
case QueryElement.LE:
relation = " <= ";
break;
// contains
case QueryElement.CN:
// starts with
case QueryElement.SW:
case // ends with
QueryElement.EW:
relation = " like ";
break;
default:
// for now, anyway
relation = " = ";
}
if ((fldName != null) && (fldName.length() > 0) && (fldValue != null) && (fldValue.length() > 0)) {
if (moreThanOneQuery) {
whereSBuf.append(" (");
}
whereSBuf.append(fldName).append(relation);
if (iRelation == QueryElement.CN) {
whereSBuf.append(" '%").append(fldValue).append("%'");
} else if (iRelation == QueryElement.SW) {
whereSBuf.append(" '").append(fldValue).append("%'");
} else if (iRelation == QueryElement.EW) {
whereSBuf.append(" '%").append(fldValue).append("'");
} else {
whereSBuf.append(" '").append(fldValue).append("'");
}
if (moreThanOneQuery) {
whereSBuf.append(") ");
}
}
//
if ((i + 1) < qrySize) {
whereSBuf.append(opStr);
}
}
if (moreThanOneQuery) {
whereSBuf.append(")");
}
return (whereSBuf.toString());
}
use of com.sun.identity.log.QueryElement in project OpenAM by OpenRock.
the class LogReadDBHandler method lq2Select.
//
// LogQuery-to-SQL-Select converter
//
// tblName is the table to select on
// columns contains the comma-separated column names to return...
// e.g., "time, data, LoginID"
// lq is the LogQuery specified. if no logquery, then
// lq2Select returns "select <columns> from <tblName>;"
//
private String lq2Select(String tblName, String columns, LogQuery lq) {
ArrayList queries;
StringBuffer selectSBuf = null;
// select what from tblName?
String whatSBuf = "*";
// where ...
StringBuffer whereSBuf = null;
// default to "and"; could be "or"
String opStr = " and ";
int lqMatchAny = com.sun.identity.log.LogQuery.MATCH_ANY_CONDITION;
int qrySize = 0;
String sortStr = null;
boolean getAllRecs = false;
selectSBuf = new StringBuffer("select ");
if ((columns != null) && (columns.length() > 0)) {
whatSBuf = columns;
}
//
// if LogQuery.MOST_RECENT_MAX_RECORDS or
// LogQuery.ALL_RECORDS, need to retrieve all records
//
int numRecs = 0;
getAllRecs = false;
String numRecsStr = null;
String sortByField = null;
if (lq != null) {
numRecs = lq.getNumRecordsWanted();
if ((numRecs == LogQuery.MOST_RECENT_MAX_RECORDS) || (numRecs == LogQuery.ALL_RECORDS)) {
getAllRecs = true;
} else {
numRecsStr = Integer.toString(numRecs);
}
sortByField = lq.getSortingField();
} else {
getAllRecs = true;
}
if (Debug.messageEnabled()) {
Debug.message("lq2Select:getAllRecs = " + getAllRecs + ", numRecs = " + numRecs + ", sortByField = " + sortByField + ", numRecsStr = " + numRecsStr);
}
if (lq == null) {
selectSBuf.append(whatSBuf).append(" from ").append(tblName);
return selectSBuf.toString();
}
queries = (ArrayList) lq.getQueries();
if ((queries == null) || (qrySize = queries.size()) == 0) {
selectSBuf.append(whatSBuf).append(" from ").append(tblName);
if (!getAllRecs) {
if (isMySQL) {
selectSBuf.append(" limit ").append(numRecsStr);
} else {
selectSBuf.append(" where rownum < ").append(numRecsStr);
}
}
return selectSBuf.toString();
}
if (lq.getGlobalOperand() == lqMatchAny) {
opStr = " or ";
}
selectSBuf.append(whatSBuf).append(" from ").append(tblName).append(" where ");
if (!getAllRecs && !isMySQL) {
selectSBuf.append("rownum < ").append(numRecsStr).append(" and ");
}
//
// get the columns/values to search on
//
QueryElement qe;
whereSBuf = new StringBuffer();
boolean moreThanOneQuery = false;
if (qrySize > 1) {
moreThanOneQuery = true;
whereSBuf.append("(");
}
for (int i = 0; i < qrySize; i++) {
qe = (QueryElement) queries.get(i);
String fldName = qe.getFieldName();
String fldValue = qe.getFieldValue();
String relation;
int iRelation = qe.getRelation();
switch(iRelation) {
case QueryElement.GT:
relation = " > ";
break;
case QueryElement.LT:
relation = " < ";
break;
case QueryElement.EQ:
relation = " = ";
break;
case QueryElement.NE:
relation = " != ";
break;
case QueryElement.GE:
relation = " >= ";
break;
case QueryElement.LE:
relation = " <= ";
break;
// contains
case QueryElement.CN:
// starts with
case QueryElement.SW:
case // ends with
QueryElement.EW:
relation = " like ";
break;
default:
// for now, anyway
relation = " = ";
}
if ((fldName != null) && (fldName.length() > 0) && (fldValue != null) && (fldValue.length() > 0)) {
if (moreThanOneQuery) {
whereSBuf.append(" (");
}
whereSBuf.append(fldName).append(relation);
if (iRelation == QueryElement.CN) {
whereSBuf.append(" '%").append(fldValue).append("%'");
} else if (iRelation == QueryElement.SW) {
whereSBuf.append(" '").append(fldValue).append("%'");
} else if (iRelation == QueryElement.EW) {
whereSBuf.append(" '%").append(fldValue).append("'");
} else {
whereSBuf.append(" '").append(fldValue).append("'");
}
if (moreThanOneQuery) {
whereSBuf.append(") ");
}
}
//
if ((i + 1) < qrySize) {
whereSBuf.append(opStr);
}
}
if (moreThanOneQuery) {
whereSBuf.append(")");
}
selectSBuf.append(whereSBuf.toString());
//
// add the "order by" part
//
sortStr = lq.getSortingField();
if ((sortStr != null) && (sortStr.length() > 0)) {
selectSBuf.append(" order by ").append(sortStr);
}
if (!getAllRecs && isMySQL) {
selectSBuf.append(" limit ").append(numRecsStr);
}
if (Debug.messageEnabled()) {
Debug.message("lq2Select:select = " + selectSBuf.toString());
}
return (selectSBuf.toString());
}
use of com.sun.identity.log.QueryElement in project OpenAM by OpenRock.
the class LogReadFileHandler method applyQuery.
// applies query to find out whether the record matches or not
private boolean applyQuery(String[] recordToBeQueried) {
ArrayList queries = (ArrayList) queryChriteria.getQueries();
if (queries == null) {
return (true);
}
int qrySz = queries.size();
if (qrySz == 0) {
return (true);
}
int queryCondition = queryChriteria.getGlobalOperand();
boolean isMatch = false;
for (int i = 0; i < qrySz; i++) {
isMatch = false;
isMatch = doMatch(recordToBeQueried, (QueryElement) queries.get(i));
if (queryCondition == com.sun.identity.log.LogQuery.MATCH_ALL_CONDITIONS) {
if (isMatch == false) {
return (isMatch);
}
} else if (queryCondition == com.sun.identity.log.LogQuery.MATCH_ANY_CONDITION) {
if (isMatch == true) {
return (isMatch);
}
}
}
return (isMatch);
}
use of com.sun.identity.log.QueryElement in project OpenAM by OpenRock.
the class AMLogTest method readLogQuery.
private void readLogQuery(String fileName, SSOToken ssot, int recsWritten) throws AMLogException {
/*
* can have:
* LogQuery(), which sets:
* maxRecord = LogQuery.MOST_RECENT_MAX_RECORDS
* globalOperand = LogQuery.MATCH_ANY_CONDITION
* queries = null
* sortBy = null
* LogQuery(int max_record), which sets:
* maxRecord = max_record
* globalOperand = LogQuery.MATCH_ANY_CONDITION
* queries = null
* sortBy = null
* LogQuery(int max_record,
* int matchCriteria,
* String sortingBy), which sets:
* maxRecord = max_record
* globalOperand = matchCriteria
* sortBy = sortingBy
* queries = null
*
* use lq.addQuery(QueryElement qryElement) to
* add query elements to the LoqQuery's list of queries
* QueryElement(String fld, String val, int rel)
* fieldName = fld
* fieldValue = val
* relation = rel
* QueryElement()
* fieldName = new String()
* fieldValue = new String()
* relation = QueryElement.EQ
* use:
* QueryElement.setFieldName(String field)
* QueryElement.setFieldValue(String value)
* QueryElement.setRelation(int value)
*/
int totalRecsRead = 0;
/*
* test 1:
* all records, any condition, no sorting
* should be same as read all.
*/
LogQuery lq = new LogQuery(LogQuery.ALL_RECORDS, LogQuery.MATCH_ANY_CONDITION, null);
int numRecs = 0;
String[][] result = new String[1][1];
try {
result = LogReader.read(fileName, lq, ssot);
numRecs = Array.getLength(result);
} catch (AMLogException ale) {
throw new AMLogException("readLogQuery:AMLogException: " + ale.getMessage());
} catch (NoSuchFieldException nsfe) {
throw new AMLogException("readLogQuery:NoSuchField: " + nsfe.getMessage());
} catch (IllegalArgumentException iaex) {
throw new AMLogException("readLogQuery:IllegalArgumentException: " + iaex.getMessage());
} catch (IOException ioe) {
throw new AMLogException("readLogQuery:IOException: " + ioe.getMessage());
} catch (Exception e) {
throw new AMLogException("readLogQuery:Exception: " + e.getMessage());
}
if (numRecs != (recsWritten + 2)) {
throw new AMLogException("Number of records read test 1 (" + numRecs + ") doesn't match expected (" + (recsWritten + 1) + ")");
}
/*
* test 2:
* only read 2 most recent records
*/
lq = new LogQuery(2, LogQuery.MATCH_ANY_CONDITION, null);
result = new String[1][1];
numRecs = 0;
try {
result = LogReader.read(fileName, lq, ssot);
numRecs = Array.getLength(result);
} catch (AMLogException ale) {
throw new AMLogException("readLogQuery:AMLogException: " + ale.getMessage());
} catch (NoSuchFieldException nsfe) {
throw new AMLogException("readLogQuery:NoSuchField: " + nsfe.getMessage());
} catch (IllegalArgumentException iaex) {
throw new AMLogException("readLogQuery:IllegalArgumentException: " + iaex.getMessage());
} catch (IOException ioe) {
throw new AMLogException("readLogQuery:IOException: " + ioe.getMessage());
} catch (Exception e) {
throw new AMLogException("readLogQuery:Exception: " + e.getMessage());
}
if (numRecs != 3) {
throw new AMLogException("Number of records read test 2 (" + (numRecs - 1) + ") doesn't match expected (2)");
}
// two most recent should be "...data #1" and "...data #0"
String temp = result[1][1];
if (!temp.contains(msgDataPrefixData + "1")) {
throw new AMLogException("Read test 2: second most recent = " + temp + ", not " + msgDataPrefixData + "1");
}
temp = result[2][1];
if (!temp.contains(msgDataPrefixData + "0")) {
throw new AMLogException("Read test 2: most recent = " + temp + ", not " + msgDataPrefixData + "0");
}
/*
* test 3:
* get records that end with "XX2" in the MessageID field.
* should only be one.
*/
lq = new LogQuery(LogQuery.ALL_RECORDS, LogQuery.MATCH_ALL_CONDITIONS, null);
QueryElement qe = new QueryElement("MessageID", "XX2", QueryElement.EW);
lq.addQuery(qe);
result = new String[1][1];
numRecs = 0;
try {
result = LogReader.read(fileName, lq, ssot);
numRecs = Array.getLength(result);
} catch (AMLogException ale) {
throw new AMLogException("readLogQuery:AMLogException: " + ale.getMessage());
} catch (NoSuchFieldException nsfe) {
throw new AMLogException("readLogQuery:NoSuchField: " + nsfe.getMessage());
} catch (IllegalArgumentException iaex) {
throw new AMLogException("readLogQuery:IllegalArgumentException: " + iaex.getMessage());
} catch (IOException ioe) {
throw new AMLogException("readLogQuery:IOException: " + ioe.getMessage());
} catch (Exception e) {
throw new AMLogException("readLogQuery:Exception: " + e.getMessage());
}
if (numRecs != 2) {
throw new AMLogException("Number of records read test 3 (" + (numRecs - 1) + ") doesn't match expected (1)");
}
// assume MessageID is in column 8 (starting from 0)
temp = result[1][8];
if (!temp.contains("XX2")) {
throw new AMLogException("Read test 3: record = " + temp + ", not XX2");
}
/*
* test 4:
* get records that contain "data #4", "data #0" or "data #2"
* in the Data field. specify them in that order, with
* sort by Data field.
* msgDataPrefixData = "data #"
*/
lq = new LogQuery(LogQuery.ALL_RECORDS, LogQuery.MATCH_ANY_CONDITION, "Data");
qe = new QueryElement("Data", msgDataPrefixData + "4", QueryElement.CN);
lq.addQuery(qe);
qe = new QueryElement("Data", msgDataPrefixData + "0", QueryElement.CN);
lq.addQuery(qe);
qe = new QueryElement("Data", msgDataPrefixData + "2", QueryElement.CN);
lq.addQuery(qe);
result = new String[1][1];
numRecs = 0;
try {
result = LogReader.read(fileName, lq, ssot);
numRecs = Array.getLength(result);
} catch (AMLogException ale) {
throw new AMLogException("readLogQuery:AMLogException: " + ale.getMessage());
} catch (NoSuchFieldException nsfe) {
throw new AMLogException("readLogQuery:NoSuchField: " + nsfe.getMessage());
} catch (IllegalArgumentException iaex) {
throw new AMLogException("readLogQuery:IllegalArgumentException: " + iaex.getMessage());
} catch (IOException ioe) {
throw new AMLogException("readLogQuery:IOException: " + ioe.getMessage());
} catch (Exception e) {
throw new AMLogException("readLogQuery:Exception: " + e.getMessage());
}
if (numRecs != 4) {
throw new AMLogException("Number of records read test 4 (" + (numRecs - 1) + ") doesn't match expected (3)");
}
/*
* gonna expect Data column is in result[x][1]. also
* that with sorting, result[1][1] contains "...data #0",
* result[2][1] contains "...data #2", and result[3][1]
* contains "...data #4".
*/
temp = result[1][1];
if (!temp.contains(msgDataPrefixData + "0")) {
throw new AMLogException("Read test 4: first sorted record = " + temp + ", not " + msgDataPrefixData + "0");
}
temp = result[2][1];
if (!temp.contains(msgDataPrefixData + "2")) {
throw new AMLogException("Read test 4: second sorted record = " + temp + ", not " + msgDataPrefixData + "2");
}
temp = result[3][1];
if (!temp.contains(msgDataPrefixData + "4")) {
throw new AMLogException("Read test 4: third sorted record = " + temp + ", not " + msgDataPrefixData + "4");
}
}
use of com.sun.identity.log.QueryElement in project OpenAM by OpenRock.
the class LogReadDBHandler method doOracleQueryElements.
//
// create the "where..." clause from the QueryElements...
// the part that comes after "where ".
//
// select (for oracle) will look something like:
// select * from tbl1 where [rownum < n] and ((fld1 = 'val1') and
// (fldx = 'valx')) union
// select * from tbl2 where [rownum < n] and ((fld1 = 'val1') and
// (fldx = 'valx')) union
// ...
// select * from tbln where [rownum < n] and ((fld1 = 'val1') and
// (fldx = 'valx'))
//
private String doOracleQueryElements(ArrayList qes, String numRecsStr, String opStr, boolean getAllRecs, String sortStr) {
//
// get the columns/values to search on
//
int qrySize = qes.size();
QueryElement qe;
StringBuilder whereSBuf = new StringBuilder();
boolean moreThanOneQuery = false;
if ((numRecsStr != null) && (numRecsStr.length() > 0)) {
whereSBuf.append("rownum < ").append(numRecsStr).append(" and ");
}
if (qrySize > 1) {
moreThanOneQuery = true;
whereSBuf.append("(");
}
for (int i = 0; i < qrySize; i++) {
qe = (QueryElement) qes.get(i);
String fldName = qe.getFieldName();
String fldValue = qe.getFieldValue();
String relation;
int iRelation = qe.getRelation();
switch(iRelation) {
case QueryElement.GT:
relation = " > ";
break;
case QueryElement.LT:
relation = " < ";
break;
case QueryElement.EQ:
relation = " = ";
break;
case QueryElement.NE:
relation = " != ";
break;
case QueryElement.GE:
relation = " >= ";
break;
case QueryElement.LE:
relation = " <= ";
break;
// contains
case QueryElement.CN:
// starts with
case QueryElement.SW:
case // ends with
QueryElement.EW:
relation = " like ";
break;
default:
// for now, anyway
relation = " = ";
}
if ((fldName != null) && (fldName.length() > 0) && (fldValue != null) && (fldValue.length() > 0)) {
if (moreThanOneQuery) {
whereSBuf.append(" (");
}
whereSBuf.append(fldName).append(relation);
if (iRelation == QueryElement.CN) {
whereSBuf.append(" '%").append(fldValue).append("%'");
} else if (iRelation == QueryElement.SW) {
whereSBuf.append(" '").append(fldValue).append("%'");
} else if (iRelation == QueryElement.EW) {
whereSBuf.append(" '%").append(fldValue).append("'");
} else {
whereSBuf.append(" '").append(fldValue).append("'");
}
if (moreThanOneQuery) {
whereSBuf.append(") ");
}
}
//
if ((i + 1) < qrySize) {
whereSBuf.append(opStr);
}
}
if (moreThanOneQuery) {
whereSBuf.append(")");
}
if (Debug.messageEnabled()) {
Debug.message("doQueryElements:returning " + whereSBuf.toString());
}
return (whereSBuf.toString());
}
Aggregations