This section gives examples of writing queries using the features discussed so far.
Sample 1: Using input mappings in a query
This sample demonstrates how to write a simple query with input mappings.
Query ID : customerAddressSQL
SQL Statement : select * from Customers where contactLastName = ? and contactFirstName = ?
The following query needs two parameters for execution. The Input Mapping section is used to specify these input parameters.
Sample 2: Adding output mappings to a query
Following sample shows how to query the Cassandra data source created in Cassandra and add output mappings to it.
Query ID : getUsers
Data Source : CassandraDatasource
CQL : select 'key', 'username', 'password' from USER
You can define how the output of this query looks by adding output mappings as follows:
Sample 3: Using a result row namespace
This example shows how to use a results row namespace in output mappings. See Defining Namespaces for information on namespaces.
Sample 4: Querying a RDF data source
Following sample shows a query written for the RDF data source created in Exposing RDF Data as a Data Service, which is based on the added RDF file. This sample uses the following SPARQL query to extract movies information from the data source.
The input mapping section is used to specify parameters to the query. The above query extracts movie information according to the genre. Therefore, we add genre as an input parameter. Next, the output mappings are used to map the response to an output XML.
Sample 5: Querying a Web data source
Following sample shows a query written for the Web data source created in Web Resource. When you add a query to a Web data source, you must enter a Scraper Variable. This scraper variable must be the same as the output name in the Web data source configuration, which returns the output from the configuration. In this example, the var-def name in the configuration is weatherInfo (
>). Therefore, the Web resource output variable name is also specified as weatherInfo as shown below:
It defines output mappings as follows to specify how the output looks like.
Sample 6: Querying a google spreadsheet
Given below are sample queries that can be written for a google spreadsheet datasource. Note that SQL queries of this type can only be used for a google spreadsheet if the Query mode is enabled for the datasource in the data service.
You can also create new sheets in the Excel or drop existing sheets.
Sample 7: Calling a MySQL function
Assume you have the following MySQL function, which takes a string parameter and returns the same as output. Create a database before executing the query.
To call this function from the data service, add a query to the data service definition file (.dbs) pointing to an RDBMS datasource that connects to the MySQL database. For example,
For example, see the following data service configuration:
You can also call this function in the Query Details page of the management console as follows:
Sample 8: Calling an Oracle function
Assume you have the following Oracle stored function, which returns the total number of entries in a table:
Create a table before executing the query as follows:
To call this function from the data service, add a query to the data service definition file (.dbs). For example,
First input parameter carries the return value of the function. Other two parameters are inputs to the function. You must define an Input parameter with OUT type to get the result of function (i.e., the first parameter in the query above). Then, define a Output parameter to get this value as a result set from the data service. The following code segment does this:
For example, see the following data service configuration:
You can call this function in the Query Details page of the management console as before.
Sample 9: Defining a dynamic SQL query
Dynamic SQL query support allows you to change SQL queries (e.g., defining additional conditions in the SQL) in the runtime without changing the data service configuration. For this to work, you must specify required SQL query statements (e.g., with WHERE clause) as a
QUERY_STRING data type. These statements will be directed to the final SQL query in the runtime.
Dynamic query support can lead to SQL injection attacks. Therefore, we recommend that the clients validate the values set to
QUERY_STRING at runtime.
QUERY_STRING data type is available as an SQL type when creating Input Mappings for queries. For example,
You can add the SQL query using the mapping name:
To avoid any errors, the value we pass to the query param must be URL encoded. You may refer to https://www.w3schools.com/tags/ref_urlencode.ASP and do the encoding.
A sample configuration for the data service is shown below:
Sample 10: Defining Named parameters
Named Parameters enable reusability of parameters and reduce the complexity of database configurations. Named parameters are specified in SQL queries of data services.
A named parameter must have the same name as the input parameter along with a colon ':' in front. For example,
Sample 11: Grouping data into complex elements
Complex Elements help represent data in a structured manner. For example, let's take a table containing customer information. There can be several columns that keep data related to the address of employees such as number, street, city, postal code. You can group them into one element called address using complex elements and present them in a more structured manner.
Following example illustrates how to use complex elements:
After defining the query, proceed to adding an Output Mapping. In output mappings, select the Mapping Type as complex element. Specify an Element Name and click the Add Nested Element button. For example,
The added element appears under Complex Elements in the Output Mapping.
According to the figure below, addressline1, addressline2 and city are nested elements that come under address element.