Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The query in a data service specifies the type of task that should be performed on the data in a particular data store. For example, consider the task of retrieving data from a data store, or posting, updating, and deleting data. Data consumers send requests to the data service by invoking the relevant operation (or REST resource) in the data service. Consequently, the query connected to the operation/resource is executed to perform the task.

...

ParameterDescription
Generate Input Mapping

If you have defined an SQL query, you can generate input mappings corresponding to the input fields specified in the query by clicking Generate Input Mappings. As shown in the example below, an input mapping is created for the emp_no field, which will allow you to invoke this query by specifying a value for this field as an input.
 

Parameter Type
This is the data type of the input mapping, which determines how the input mapping parameter will be given in the target query.
Note

Note that ARRAY parameter type cannot be used with the QUERY_STRING data type (SQL type).

SQL TypeThe data type of the corresponding SQL parameter can be selected from this menu. Note that the QUERY_STRING data type cannot be used if the parameter type is set to ARRAY. Find more from here about data types.
Default Value

Default values help you automatically assign a value to a parameter when a user has not entered a specific parameter value in a request. Default values are added when defining queries. Therefore, this value gets automatically added to the query if it is ignored by the user.

You can refer to Internal Property Values using Default Values. You can use special system variables that are defined as default values. At the moment, it only provides a variable for retrieving the username of the current user authenticated in a secured data service. You can access this variable as follows:

Code Block
#{USERNAME}: Dynamically replaces the input mapping with the current user's username when a data service request is processed.

#{NULL}: Sets the current input mapping value to null.
         It's the same as providing "xsi:nil" in the incoming message's input parameter element.

#{TENANT_ID}: Represents the current tenant ID.
              This is useful in a Stratos deployment where multiple tenants live in the same server.

#{USER_ROLES}: This value contains the list of user roles that the current calling user has.
               If the parameter mapped is of type ARRAY, it will have the full list of user roles.
               If it's a SCALAR, it will only contain the first user role of the user.

For a demonstration of the usage of default values, see Default Values Sample/wiki/spaces/EI6xx/pages/49611530.  

IN/OUT TypeThese are used in stored procedures which takes out parameters and in/out parameters. IN is the usual parameter we give to provide some value inside. OUT only returns a value from a stored procedure. INOUT does both.
Validators
Returning Generated Keys

The Return Generated Keys option appears under Input Mappings on the Queries page. 

It inserts data to a table that has auto increment key columns. The auto incremented key value of the record is mapped to the result output mappings of the data service. For example, the sample query below is used to insert values to a table by the name wes_teams, which has an auto increment column: 

INSERT INTO wes_teams(TEAM) VALUES(?)

Once the user selects Return Generated Keys option, an auto increment key is added as an output mapping as follows:

Returning Updated Row Count

With the current data services functionality, we don't have a way to indicate that the update operation did not affect any rows. But, we can return the updated row count as a response to the client in queries like update/insert to indicate how may rows are affected by the query execution.

...

ParameterDescription
Generate Output Mapping
Note

Note that this option is only available for SELECT statements excluding SELECT *, and for datasources such as RDBMS.

If you have defined an SQL query, you can generate output mappings corresponding to the fields specified in the query by clicking Generate Response. In the example shown below, there is an SQL query that needs to output values for the customernumber and customername fields in the customers table.


Use column numbers

If this option is selected the mapping will be done by the column number basis instead of the column name. The following screenshot provides an example for using column numbers

Escape non-printable charactersTick this option if the data in your database consists of characters that are not serializable to XML. Few examples are & < > " '. When you invoke services that access such data and produce responses, the sever throws errors. Ticking this option ensures that non-printable characters will be ignored when producing the responses.
Row NamespaceSee Defining Namespaces.
Query Result Export

When you click Add New Output Mapping in the Result(Output Mapping) section of the Queries page as explained above, the Edit Output Mapping page will open. You can specify the type of fields that will present the output of your query by giving the data source type, output field name, data source column name etc.

In the Edit Output Mapping page, you can define query result export options. Query Request Export feature must be used in conjunction with request box. It allows individual queries executed in a request r to communicate with each other. The concept is 'exporting' a specific result element so that the next calling query will get that result element as a query parameter. So, if you've two queries, namely, 'query1' and 'query2' that's executed sequentially in a request box, and if 'query1' has a specific result element and that element is exported with the name 'foo', then 'query2' also gets a query param named 'foo'. So when this request box session is executed, the query1's exported value will be passed into query2 as an input parameter.

This feature is very useful in situations where the result of an earlier-executed query is required for the execution of a subsequent query (e.g. a newly created primay key). 

The following figure shows how a result element can be declared to be exported with a given name when defining a query in a data service.

There are two export types that can be used. 

  • SCALAR : The single element value is exported. If there are multiple instances of this value, the last one will be exported.
  • ARRAY : An array of values will be exported. Each occurrence of the value is added to an array and exported. 

For a demonstration on the usage of export options, refer to request box sample/wiki/spaces/EI6xx/pages/49611519 . 

Events

This section can be used to trigger notifications from the query.

...