SQL query

Feedback


SQL query is to find the records satisfying the SQL condition. This example constructs a simple SQL query to get the feature whose SmID is 2 in the China_Province_pl@China layer.

Two parameters need to be transferred: queryMode and queryParameters. Sample code:

//SQL query

function queryBySql()

{

    var commit=getcommit();

    var uri="http://localhost:8090/iserver/services/map-china400/rest/maps/China/queryResults.json";

    //Set the request body parameters

    var entry={};

    entry.queryMode="SqlQuery";

    entry.queryParameters={"networkType":null,"startRecord":0,"queryParams":[{"orderBy":null,"ids":null,"name":"China_Province_pl@China","attributeFilter":"SmID=2","groupBy":null,"linkItems":null,"joinItems":null,"fields":["SMID","Name"]}],"customParams":null,"expectCount":100000,"queryOption":"ATTRIBUTEANDGEOMETRY"}

    commit.open("POST",encodeURI(uri),false,"","");

    commit.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

    //alert(toJSON(entry));

    commit.send(toJSON(entry));

    

    //Parse the json string returned from the server to a JavaScript object

    var response = json_parse(commit.responseText, null);       

    //Get the Div container for display

    var container = document.getElementById('container');

    container.innerHTML="";

    //Determine whether the query is successful

    if(!response.succeed)

    {

        //Query failed

        container.innerHTML+="<p>Query failed</p>";

        container.innerHTML+="<p>Error code: "+response.error.code+"</p><p>Cause: "+response.error.errorMsg+"</p>";

    }else

    {

        //Query succeeded

        container.innerHTML+="<p>Query succeeded</p>";

        container.innerHTML+="<p>URI of queryResult resource: "+response.newResourceLocation+"</p>";

    }   

}

where queryMode="SqlQuery" specifies the query mode as SqlQuery, and queryParameters={...} specifies the query parameters. In queryparameters, "queryOption":"ATTRIBUTEANDGEOMETRY" indicates the query result contains both the attribute information and the spatial information. "queryParams":[{...}] is the parameter collection. In this example, the query parameter collection is ({{…,”name:"China_Province_pl@China","attributeFilter":"SmID=2",?"fields":["SMID","Name"]}), where name is the name of the layer to be queried, attributeFilter is the filter, similar to the content after where in SQL, and fields specifies which fields should be included in the query result, SmID and Name in this example.

Perform the query to get the queryResult resource at http://localhost:8090/iserver/services/components-rest/rest/maps/WorldMap/queryResults/1.json. The resource contains the spatial information (Geometry) and the attribute information, including the smID and Country fields.

The queryResult needs to be further parsed. Please refer to Parsing queryResult.