|
| 1 | +--- |
| 2 | +title: "OQL Module" |
| 3 | +url: /appstore/modules/oql-module/ |
| 4 | +description: "Describes the configuration and usage of the OQL module, which is available in the Mendix Marketplace." |
| 5 | +--- |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +The [OQL Module](https://marketplace.mendix.com/link/component/66876) adds microflow actions which execute [OQL](https://docs.mendix.com/refguide/oql/) queries and return the results as a list of Mendix objects, a count of results in the database, or a CSV file. This means that you do not need to write your own Java actions to execute OQL queries. |
| 10 | + |
| 11 | +### Typical Use Cases |
| 12 | + |
| 13 | +This module allows users to run OQL queries directly from a string or from a [dataset](/refguide/data-sets/) without having to write Java code. |
| 14 | + |
| 15 | +It is also an alternative to [View Entities](/refguide/view-entities/) where you need support for running dynamic OQL queries and using OQL parameters. Unless these features are necessary, Mendix recommends that you use View Entities. |
| 16 | + |
| 17 | +You can also use this module to interactively investigate data problems in a running app. |
| 18 | + |
| 19 | +{{% alert color="warning" %}} |
| 20 | +The OQL Module does not validate the input query and does not apply user role access controls when executing queries. |
| 21 | +Care should be taken to guarantee that the query is valid and does not expose app data. |
| 22 | +{{% /alert %}} |
| 23 | + |
| 24 | +### Features |
| 25 | + |
| 26 | +The OQL module has the following features: |
| 27 | + |
| 28 | +* Executes an OQL query directly or loads a query from a dataset |
| 29 | +* Supports running queries with limit and offset |
| 30 | +* Supports OQL named query parameters |
| 31 | +* Returns data directly or generates a CSV file |
| 32 | +* If you opt to generate a CSV file, this can be created with the following characteristics: |
| 33 | + * Compressed in a Zip file |
| 34 | + * Customized to use different characters for quoting strings, separating values, and escaping special characters |
| 35 | + * Customized to remove new line sequences from string values |
| 36 | + |
| 37 | +## Available OQL Actions |
| 38 | + |
| 39 | +{{% alert color="info" %}} |
| 40 | + |
| 41 | +* Only `SELECT` queries are supported |
| 42 | +* All actions support defining named parameters for queries – see [Named Parameters](#named-parameters), below, for more information |
| 43 | +* All queries are executed without access rules |
| 44 | + |
| 45 | +{{% /alert %}} |
| 46 | + |
| 47 | +### ExecuteOQLStatement {#executeoqlstatement} |
| 48 | + |
| 49 | +This action executes an OQL query, either directly or by loading one from a dataset. If you wish to use a dataset, you need to provide its fully-qualified name (for example "MyFirstModule.Dataset", rather than just "Dataset"). |
| 50 | + |
| 51 | +This action takes the following parameters: |
| 52 | + |
| 53 | +* `statement`: OQL Query or fully qualified name of a Dataset (module and Dataset name) to be executed |
| 54 | +* `returnEntity`: Entity type to be used to store results |
| 55 | +* `amount`: If not `empty` or 0, will limit the number of results to at most this amount. It is recommended to use an explicit sorting order in the OQL query when using amount |
| 56 | +* `offset`: If not `empty` or 0, will offset the results of the query by this quantity. It is recommended to use an explicit sorting order in the OQL query when using offset |
| 57 | +* `preserveParameters`: If false, all parameters defined for this query will be cleared |
| 58 | + |
| 59 | +It returns the following: |
| 60 | + |
| 61 | +* A list of Mendix objects of type `returnEntity` using the following rules: |
| 62 | + * Each query result column is stored in an attribute or association of the same name |
| 63 | + * Attributes and associations of the `returnEntity` that are not present in the query are given the default value |
| 64 | + * If there is a query result column which cannot be matched to an attribute or association, then the module will throw an error |
| 65 | + * Only associations owned by the `returnEntity` are considered when setting associations |
| 66 | + |
| 67 | +### CountRowsOQLStatement {#countrowsoqlstatement} |
| 68 | + |
| 69 | +Similar to `ExecuteOQLStatement`, above, but returns only the number of results. Use this to obtain information from the database without the overhead of generating Mendix objects. |
| 70 | + |
| 71 | +This action does not support datasets. |
| 72 | + |
| 73 | +[Named parameters](#named-parameters) are always reset on completion. |
| 74 | + |
| 75 | +This action takes the following parameters: |
| 76 | + |
| 77 | +* `statement`: OQL Query to be executed |
| 78 | +* `amount`: Limits the number of results to at most this amount. If there are more results in the query than this limit, this amount is returned instead. If set to 0, no limit is applied |
| 79 | +* `offset`: This parameter is ignored and might be removed in a future version |
| 80 | + |
| 81 | +It returns the following: |
| 82 | + |
| 83 | +* The number of results of executing `statement` |
| 84 | + |
| 85 | +### ExportOQLToCSV {#exportoqltocsv} |
| 86 | + |
| 87 | +This action executes an OQL query and saves the result in a CSV file. |
| 88 | + |
| 89 | +[Named parameters](#named-parameters) are always reset on completion. |
| 90 | + |
| 91 | +This action takes the following parameters: |
| 92 | + |
| 93 | +* `statement`: OQL Query or fully qualified name of a Dataset (module and Dataset name) to be executed |
| 94 | +* `returnEntity`: A FileDocument or one of its specializations where the resulting file will be stored |
| 95 | +* `removeNewLinesFromValues`: Indicates if new lines inside string values should be replaced with spaces |
| 96 | +* `zipResult`: Indicates if the resulting file should be compressed inside a ZIP file |
| 97 | +* `exportHeaders`: Indicates if the first line of the result should contain a header with the names of each column |
| 98 | +* `separatorChar`: Indicates what character should be used to separate columns in the result |
| 99 | +* `quoteChar`: Indicates what character should be used to quote string values. May be left empty if `escapeChar` is defined |
| 100 | +* `escapeChar`: Indicates what character should be used to escape spaces and other special characters if string values are unquoted. Only applicable if `quoteChar` is not defined. If no escape character is defined, then the quote character is escaped in a quoted string by repeating it, following [RFC4180](https://www.ietf.org/rfc/rfc4180.txt) |
| 101 | + |
| 102 | +It returns the following: |
| 103 | + |
| 104 | +* An object of type specified in the `returnEntity` parameter containing the CSV file with the results of executing `statement` |
| 105 | + |
| 106 | +## Named Parameters {#named-parameters} |
| 107 | + |
| 108 | +If you wish to use named parameters inside an OQL query, you must call the following actions to set their values before calling the actions above: |
| 109 | + |
| 110 | +* `AddBooleanParameter`: For Boolean parameters |
| 111 | +* `AddDateTimeParameter`: For Date and Time parameters |
| 112 | +* `AddDecimalParameter`: For parameters of type Decimal |
| 113 | +* `AddIntegerLongValue`: For parameters of type Integer/Long |
| 114 | +* `AddObjectParameter`: For parameters that reference a Mendix Object |
| 115 | +* `AddStringParameter`: For String parameters |
| 116 | + |
| 117 | +{{% alert color="info" %}} |
| 118 | +If you wish to keep the same parameters across multiple calls to the [ExecuteOQLStatement](#executeoqlstatement) action, you must set `preserveParameters` to `true`. All other OQL actions above will clear all defined parameters after every call. |
| 119 | +{{% /alert %}} |
0 commit comments