How to execute complex SQL queries for below mentioned scenarios

混江龙づ霸主 提交于 2019-12-11 14:43:56

问题


XpcSecurity credentials = ud.getCredentials();
    Xpc xpc = new Xpc(credentials);
    xpc.start("training.getEmployees", "select");
    xpc.attrib("id", "10");
   XData result = xpc.run();

• What if we want get the employees whose id between 10 and 30 • What if I want to select a record based on timestamp/date • If want to join multiple tables and query it.


回答1:


It seems that you are using an XPC Plugin (XPC class file) based on the entity "training.getEmployees" in xpc.start("training.getEmployees", "select"). In this case, do the following:

XpcSecurity credentials = ud.getCredentials();
Xpc xpc = new Xpc(credentials);
xpc.start("training.getEmployees", "select");
xpc.attrib("idLower", "10");
xpc.attrib("idUpper", "30");
XData result = xpc.run();

In the Xpc plugin (probably named "GetEmployeesXpc") within the runMethod, do the following to get the data passed through the xpc code above

// Get the method passed through xpc.start
String method = elem.getAttribute("method"); 

// Get the attrib using the following
XData input = new XData(elem);
String upperLimit = input.getText("//idUpper");
String lowerLimit = input.getText("//idLower");

// process the query depending on how you access your data source
if (method.equals("select")) {
    // put your logic here to access your data source
    String query = "select * from employees where id>"+lowerLimit+" &&  id<"+upperLimit";"
     :
     :
} else {
     // Do something else
}

You can do the same way with date/timestamp or joins. Just pass the data and handle it in the Xpc plugin.




回答2:


My answer might be too late but for reference purposes you might try this:

XpcSecurity credentials = ud.getCredentials();
Xpc xpc = new Xpc(credentials);
xpc.start("training.getEmployees","select");

StringBuffer whereClause = new StringBuffer();

//In xpc this is the equivalent of "id between idLower and idUpper"
whereClause.append("<whereClause>\n");
whereClause.append("    <expr op='and' returnType='char'>\n");
whereClause.append("        <expr op='ge' returnType='char'>\n");
whereClause.append("            <operand>id</operand>\n");
whereClause.append("            <operand type='literal'>"+idLower+"</operand>\n");
whereClause.append("        </expr>\n");
whereClause.append("        <expr op='le' returnType='char'>\n");
whereClause.append("            <operand>id</operand>\n");
whereClause.append("            <operand type='literal'>"+idUpper+"</operand>\n");
whereClause.append("        </expr>\n"); whereClause.append("    </expr>\n");
whereClause.append("</whereClause>\n");

xpc.input(whereClause);
XData result = xpc.run();

For table joins:

I see that you're already using xpc class. In this class put this in initialize function:

mapEntity("training.getEmployees");

addLevel("employees");  
mapTable("employee", "employee", null);
mapColumn("employee_id", "employeeId", "employeeId", true, false);
mapColumn("first_name", "firstName", "firstName", false, false);
mapColumn("last_name", "lastName", "lastName", false, false);

mapTable("time_track", "timeTrack", null);
mapColumn("time_track_id", "timeTrackId", "timeTrackId", true, false);
mapColumn("time_in", "timeIn", "timeIn", false, false);
mapColumn("time_out", "timeOut", "timeOut", false, false);
mapColumn("employee_id", "employeeId", "employeeId", false, false);

addJoin("employee", "employeeId", "timeTrack", "employeeId");

which is the equivalent to Select * from employee a inner join time_track b on a.employee_id = b.employee_id

And you can now apply the condition on selecting



来源:https://stackoverflow.com/questions/13247554/how-to-execute-complex-sql-queries-for-below-mentioned-scenarios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!