问题
I have a oracle.toplink.expressions.Expression expression object with me which has been created using oracle.toplink.expressions.ExpressionBuilder. I want to find its equivalent SQL query(say select emp.empname,emp.empId from employee emp) which will be fired eventually in order to fetch data. I want to find its equivalent Statement/PreparedStatement or anything which will help me obtain the raw SQL statement about to be fired.
For e.g. Following is the toplink expression:
expressionBuilder.get("empName").equal("Eric");
and i want to get the equivalent SQL query:
select emp.empName,emp.emp_id,emp.sal from employee emp where emp.empName like 'Eric'
Please let me know if there is any API to get the sol. I tried the implmentation classes of DatabaseQuery but couldn't find any method which could accomplish this.
Below is the toplink expression with me :
Logical operator AND
Logical operator AND
Logical operator AND
Logical operator AND
Logical operator AND
Logical operator AND
Logical operator AND
Logical operator AND
Relation operator <
Query Key minEffectiveBegDate
Base com.altra.common.data.OperationalTrxData
Constant Tue Feb 01 08:00:00 CST 2011
Relation operator >
Query Key maxEffectiveEndDate
Base com.altra.common.data.OperationalTrxData
Constant Sat Jan 01 08:00:00 CST 2011
Relation operator <
Query Key begDate
Query Key calculatedQuantityDataList
Query Key qtyClassificationDataList
Query Key operationalQuantityDataList
Base com.altra.common.data.OperationalTrxData
Constant Tue Feb 01 08:00:00 CST 2011
Relation operator >
Query Key endDate
Query Key calculatedQuantityDataList
Query Key qtyClassificationDataList
Query Key operationalQuantityDataList
Base com.altra.common.data.OperationalTrxData
Constant Sat Jan 01 08:00:00 CST 2011
Relation operator =
Query Key opsTrxClassification
Base com.altra.common.data.OperationalTrxData
Constant -5001579
Relation operator =
Query Key accountingCompany
Base com.altra.common.data.OperationalTrxData
Constant 1196
Logical operator OR
Logical operator AND
Relation operator =
Query Key sourceRelatedNodeObjType
Base com.altra.common.data.OperationalTrxData
Constant -2094
Relation operator IN
Query Key sourceRelatedNodeObjId
Base com.altra.common.data.OperationalTrxData
Constant [4187]
Logical operator AND
Relation operator =
Query Key dispositionRelatedNodeObjType
Base com.altra.common.data.OperationalTrxData
Constant -2094
Relation operator IN
Query Key dispositionRelatedNodeObjId
Base com.altra.common.data.OperationalTrxData
Constant [4187]
Relation operator =
Query Key qtyType
Query Key operationalQuantityDataList
Base com.altra.common.data.OperationalTrxData
Constant -5000328
Relation operator =
Query Key qtyStatus
Query Key qtyClassificationDataList
Query Key operationalQuantityDataList
Base com.altra.common.data.OperationalTrxData
Constant -5000316
I want the SQL equivalent like
select otd.operationaltrx_id,otd.accountingCompany from OperationalTrxData otd where minEffectiveBegDate > to_date('','') ...
Thanks, Adithya.
回答1:
You need to use a ReportQuery.
ExpressionBuilder emp = new ExpressionBuilder();
ReportQuery query = new ReportQuery(Employee.class, emp);
query.addAttribute("empName");
query.addAttribute("emp_id");
query.addAttribute("sal");
query.setSelectionCriteria(emp.get("empName").equal("Eric"););
List reports = (List) session.executeQuery(query);
回答2:
Using a ReportQuery to select the desired fields then "prepare" the query and print out the SQL that will be generated:
ExpressionBuilder emp = new ExpressionBuilder(Employee.class);
ReportQuery query = new ReportQuery(Employee.class, emp);
query.addAttribute("empName");
query.addAttribute("emp_id");
query.addAttribute("sal");
query.setSelectionCriteria(emp.get("empName").equal("Eric"););
query.prepareCall(session, new DatbaseRecord());
String sql = query.getSQLString();
来源:https://stackoverflow.com/questions/5401720/sql-query-from-toplink-expression