来自Toplink表达式的SQL查询

时间:2011-03-23 06:45:12

标签: toplink

我有一个oracle.toplink.expressions.Expression表达式对象,我使用oracle.toplink.expressions.ExpressionBuilder创建。我想找到它的等效SQL查询(比如来自employee emp的select emp.empname,emp.empId),它最终将被触发以获取数据。我想找到它等效的Statement / PreparedStatement或任何可以帮助我获取原始SQL语句的东西。

例如 以下是toplink表达式:

expressionBuilder.get("empName").equal("Eric");

我想获得等效的SQL查询:

select emp.empName,emp.emp_id,emp.sal from employee emp where emp.empName like 'Eric'

如果有任何API可以获取sol,请告诉我。我尝试了DatabaseQuery的implmentation类,但找不到任何可以实现此目的的方法。

以下是我的toplink表达式:

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

我希望SQL等价于

select otd.operationaltrx_id,otd.accountingCompany from OperationalTrxData otd where minEffectiveBegDate > to_date('','') ...

谢谢, Adithya。

2 个答案:

答案 0 :(得分:1)

您需要使用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);

答案 1 :(得分:1)

使用ReportQuery选择所需的字段,然后“准备”查询并打印出将生成的SQL:

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();