Wiki source code of DB-Abfragen


Hide last authors
XIMA Admin 1.1 1 {{content/}}
2
gru 16.2 3 Database queries are similar to [[data sources>>doc:Sources]] (XML, CSV, JSON). The important difference is that they are not static, but read dynamically from a database. In principle, only queries or SELECT statements should be stored at this point. Other statements (INSERT, etc.) may work - but this varies from database type to database type and is not recommended. Instead, such statements should be made with the corresponding workflow action [[Database query>>doc:Formcycle.Designer.Workflow.Actions.SQLStatement]].
XIMA Admin 1.1 4
5 {{info}}
gru 16.2 6 Before you can create a new database query, you must create a [[database connection >>doc:DBConnections]] if you have not done so already.
XIMA Admin 1.1 7 {{/info}}
8
gru 16.2 9 == Creating a database query ==
XIMA Admin 1.1 10
11 {{id name="data_db_query"/}}
gru 16.2 12 {{figure image="data_db_query_de.png"}}Configuration screen for creating a database query in: (1) list of existing DB queries, (2) editor for defining the SQL query, (3) settings of the DB query, (4) console for testing the DB query.{{/figure}}
XIMA Admin 1.1 13
tlo 13.1 14
gru 16.2 15 * Open the module "Database queries" and click "New" {{ficon name="plus-circle-outline"/}} in the header of the list (see [[figure>>||anchor="fig_data_db_query"]]).
16 * The following data is needed for a Database query:
17 ** **Name**: A unique name for the database query
18 ** **Description**: An optional description for the database query
19 ** **Connection**: The [[Database connection>>doc:DBConnections]] that should be used
tlo 13.1 20
gru 16.2 21 * Enter the **SQL statement** to be executed for the query in the editor (see [[figure>>||anchor="data_db_query"]]).
tlo 13.1 22
gru 16.2 23 The entered SQL statement is executed as a prepared statement, which prevents SQL injection attacks. You should not and need not use inverted commas (` or '). Also, you can use question marks ( ? ) as placeholders to build queries dynamically.
tlo 13.1 24
gru 16.2 25 [[Variables>>doc:Formcycle.UserInterface.Variables.WebHome]] can be used in the SQL statements.
tlo 13.1 26
gru 16.2 27 == Using the database query ==
tlo 13.1 28
gru 16.2 29 You can access the database query by making a HTTP request to the corresponding database servlet and providing the required parameters as GET parameters. The URL to the database query servlet is as follows.
XIMA Admin 1.1 30
31 {{code language="none"}}
32 http://<server>/formcycle/datenabfragedb
33 {{/code}}
34
gru 16.2 35 The servlet URL is displayed beneath the settings (see [[figure>>||anchor="data_db_query"]]).
36 The following URL parameters are supported:
XIMA Admin 1.1 37
38 {{table dataTypeAlpha="0" preSort="0-asc"}}
gru 16.2 39 |=Name of the paramter|=Description|=Required
40 |name|Must match the name of the database query.|Yes
41 |clientName|Must match the name of the client used for creating this data source.|Yes, if //projektId// is not given
42 |projektId|Must match the ID of the form. This information can be seen by accessing the {{code language="javascript"}}XFC_METADATA.currentProject.id{{/code}} object from JavaScript.|Yes, if //mandantName// is not given
43 |--sqlParameter--|Alias for //queryParameter//. Should no longer be used in {{formcycle/}} version 6 and will most likely be removed in the next major release of {{formcycle/}}.|No
44 |queryParameter|When the query contains placeholders (question marks, {{code language="none"}}?{{/code}}), a list of parameters must be supplied for each placeholder. The number of items must match the number of parameters used in the SQL query. The items are separated with the delimiter as defined by the URL parameter //delimiter//.
XIMA Admin 1.1 45
gru 16.2 46 If possible the parameter //queryParameterValues// should be used for new projects instead of //queryParameter// because //queryParameter// will not be supported in a future version of {{formcycle/}}.|No
47 |queryParameterValues|Starting with {{formcycle/}} Version 6.6.3 this parameter can be used as an alternative to the parameters //queryParameter// and //delimiter//. Like these parameters, //queryParameterValues// is only required if placeholders in the form of a question mark {{code language="none"}}?{{/code}} are used within the SQL query. If this is the case, the individual query parameters are passed one after the other as a separate parameter //queryParameterValues//, which also eliminates the use of the parameter //delimiter//.|No
48 |varName|Allows you to change name of the JSON response object. If not given, a plain JSON object with the result data is returned.|No
49 |delimiter|The delimiter for the placeholder values, see //queryParameter//. Defaults to a comma {{code language="none"}},{{/code}}
50 |jsonPath {{version major="7" minor="3"/}}|This parameter allows specifying a JSON path expression which will be applied to the returned result of the query on the server side. The example //$.returnValue[0]// returns the values of the first result of the query.|No
Release Notes 16.1 51
gru 16.2 52 If possible the parameter //queryParameterValues// should be used for new projects instead of //delimiter// and //queryParameter// because //delimiter// will not be supported in a future version of {{formcycle/}}.|No
XIMA Admin 1.1 53 {{/table}}
54
gru 16.2 55 When accessing a database servlet from a form, always use the database URL contained in the global object //XFC_METADATA//, see also the [[metadata>>doc:Formcycle.Designer.Form.CodingPanel.ScriptTab.FormMetadata]]. For example: {{code language="javascript"}}XFC_METADATA.urls.datasource_db{{/code}}.
XIMA Admin 1.1 56
gru 16.2 57 Further we recommend you use the script function {{jsdoc page="xutil" name="getdataquery"/}}, so you do not have to setup the servlet request manually. The result of the database query is returned as //JSON//.
XIMA Admin 1.1 58
gru 16.2 59 == Testing the query ==
XIMA Admin 1.1 60
61 {{info}}
gru 16.2 62 For quick testing of the query the shortcut {{code language="none"}}Ctrl + Enter{{/code}} is provided.
XIMA Admin 1.1 63 {{/info}}
64
gru 16.2 65 Database queries can be tested directly from the configuration UI. For this purpose a test console is provided below the SQL editor (see [[figure>>||anchor="fig_data_db_query"]]).
66 In the header of the console there is a row of buttons for controling the query:
XIMA Admin 1.1 67
gru 16.2 68 * {{ficon name="database-search"/}}**Perform query**
69 Runs the database query. If //query parameters// ({{code language="none"}}?{{/code}}) are provided the user will be prompted to input values vor those parameters. Otherwise the result of the query will be displayed directly in the //table view//.{{lightbox image="data_db_query_test_en.png" title="Run the given query"/}}
70 * {{icon name="question"/}}**Query parameters**
71 Mask for inputting values for query parameters. This option is only available if query parameters ({{code language="none"}}?{{/code}}) are used in the SQL query. The individual parameters will be enumerated in the SQL query. Clicking "User parameters for query" {{ficon name="arrow-right-bold-circle-outline2"/}} executes the query with the given parameters. The result will be displayed in the //table view//.
72 {{lightbox image="data_db_query_test_param_en.png" title="Query parameters are enumerated"/}}
73 Actual Query in the SQL editor:{{lightbox image="data_db_query_example_de.png" title="Example query"/}}
74 * {{ficon name="table-large"/}}**Table view**
75 Query result in table view{{lightbox image="data_db_query_test_table_en.png" title="Query result in table view"/}}
76 * {{icon name="code"/}}**Source code view**
77 Query result in JSON format{{lightbox image="data_db_query_test_code_en.png" title="Query result in JSON format"/}}
78 * {{icon name="Terminal"/}}**Generated SQL**
79 Displays the generated SQL statement with input parameter values{{lightbox image="data_db_query_test_sql_en.png" title="Displays the generated SQL statement with input parameter values"/}}
XIMA Admin 1.1 80
gru 16.2 81 == Selection form elements ==
XIMA Admin 1.1 82
gru 16.2 83 If you want to display the returned data as options of a [[selection element>>doc:Formcycle.Designer.Form.FormElements.Selection]], you can do so easily by opening the {{designer/}} and selecting the database query as the data source of the selection element.
XIMA Admin 1.1 84
85 {{info}}
gru 16.2 86 Queries that are used in selection elements must not have a question mark "?". Queries with a "?" are not offered as a data source for selection elements.
XIMA Admin 1.1 87 {{/info}}
88
gru 16.2 89 {{figure image="data_db_query_designer_en.png"}}
90 Using the result of a database query as the data source a select element in the {{designer case="dat"/}}.
XIMA Admin 1.1 91 {{/figure}}
92
gru 16.2 93 The result of the database query is used to create the options of the [[selection element>>doc:Formcycle.Designer.Form.FormElements.Selection]] in the following order:
XIMA Admin 1.1 94
95 {{box}}
gru 16.2 96 Displayed value, submitted value, optional value 1, optional value 2, ...
XIMA Admin 1.1 97 {{/box}}
98
gru 16.2 99 All returned columns are added as the value of the HTML attribute //col0// (displayed value), //col1// (submitted value), //col2// (optional value 1), //col3// (optional value 2) etc. to the corresponding //option// element.
XIMA Admin 1.1 100
gru 16.2 101 The displayed value is visible to the user directly when selecting an option. The submitted value is the value of the HTML attribute //value// and it is sent when the form is submitted.
XIMA Admin 1.1 102
gru 16.2 103 Optional values returned by the database query may be access as follows with //JavaScript//.
XIMA Admin 1.1 104
105 {{code language="javascript"}}
gru 16.2 106 $('[name=sel2]').find('option:selected').attr('col2') // Selects the active option of the selection element named 'sel2' and returns the first optional value.
XIMA Admin 1.1 107 {{/code}}
108
gru 16.2 109 == Examples ==
XIMA Admin 1.1 110
111 {{code language="sql"}}
gru 16.2 112 select name, first_name from table where first_name like (?)
XIMA Admin 1.1 113 {{/code}}
114
gru 16.2 115 This SQL statement returns the names of all persons with a certain first name. The first name to search for is specified via an URL parameter.
XIMA Admin 1.1 116
gru 16.2 117 URL for running the query:
118 {{code language="none"}}http://myserver/formcycle/datenquelledb?mandantName=myself&name=demo&queryParameterValues=Robinson{{/code}}
XIMA Admin 1.1 119 \\
120
121 {{code language="sql"}}
gru 16.2 122 select name, first_name from table where id = ?
XIMA Admin 1.1 123 {{/code}}
124
gru 16.2 125 Retrieves the name of a person with a certain ID. The ID is given as an URL parameter.
XIMA Admin 1.1 126
gru 16.2 127 URL for running the query:
128 {{code language="none"}}http://myserver/formcycle/datenquelledb?mandantName=myself&name=demo&queryParameterValues=100{{/code}}
XIMA Admin 1.1 129 \\
130
131 {{code language="sql"}}
gru 16.2 132 select name, first_name from table where city like(?) AND zip = ?
XIMA Admin 1.1 133 {{/code}}
134
gru 16.2 135 Retrieves the names of all persons that live in certain city. The city and the ZIP code are given as URL parameters.
XIMA Admin 1.1 136
gru 16.2 137 URL for running the query:
138 {{code language="none"}}http://myserver/formcycle/datenquelledb?mandantName=myself&name=demo&queryParameterValues=Paris&queryParameterValues=75001{{/code}}
XIMA Admin 1.1 139 \\
140
141 {{code language="sql"}}
142 select name, vorname from tabelle where ort like concat(?, '%')
143 {{/code}}
144
gru 16.2 145 This SQL statement returns the names of all persons who live in a place that **starts **with the given characters. The '%' character serves as a wildcard for any number of characters. Depending on the DBMS used, the syntax may differ slightly (here: MySQL). The requested value is passed via URL parameters.
XIMA Admin 1.1 146
gru 16.2 147 URL for running the query:
148 {{code language="none"}}http://myserver/formcycle/datenquelledb?mandantName=myself&name=demo&queryParameterValues=Par{{/code}}
149 \\
XIMA Admin 1.1 150
151 {{code language="sql"}}
152 select name, vorname from tabelle where lower(ort) like concat('%', lower(?), '%')
153 {{/code}}
154
gru 16.2 155 This SQL statement returns the names of all persons who live in a place that **contains **the given characters. Upper/lower case spelling is irrelevant becauce the statement converts both, the value column and the actual filter value to lower case (lower(...)). The requested value is passed via URL parameters.
XIMA Admin 1.1 156
gru 16.2 157 URL for running the query:
158 {{code language="none"}}http://myserver/formcycle/datenquelledb?mandantName=myself&name=demo&queryParameterValues=aRi{{/code}}