|
| 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) executes [OQL](https://docs.mendix.com/refguide/oql/) queries and returns the results as a list of Mendix objects, a count of results in the database or a CSV file. |
| 10 | + |
| 11 | +### Typical Use Cases |
| 12 | + |
| 13 | +This module allows users to run arbitrary OQL queries without having to write Java code. |
| 14 | +It is also an alternative to [View Entities](/refguide/view-entities/) with support for running dynamic OQL queries and using OQL parameters. |
| 15 | +Unless these features are necessary, it is recommended to use View Entities. |
| 16 | + |
| 17 | +Alternatively, this module can be used 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 | +* Execute an OQL query directly or load a query from a dataset |
| 27 | +* Supports running queries with limit and offset |
| 28 | +* Supports OQL named query parameters |
| 29 | +* Option to return data directly or generate a CSV file |
| 30 | +* Option to compress the generated CSV file in a Zip file |
| 31 | +* Customize CSV files to use different characters for quoting strings, separating values, and escaping special characters |
| 32 | +* Customize CSV files to remove new line sequences from string values |
| 33 | + |
| 34 | +## Available OQL Actions |
| 35 | + |
| 36 | +All actions support defining [Named Parameters](#named-parameters) for queries. |
| 37 | +Only `SELECT` queries are supported and all queries are executed without access rules. |
| 38 | + |
| 39 | +### ExecuteOQLStatement {#executeoqlstatement} |
| 40 | + |
| 41 | +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 (example "MyFirstModule.Dataset", instead of just "Dataset"). |
| 42 | + |
| 43 | +Returns: A list of Mendix objects of type `returnEntity` with each query column stored in an attribute or association of the same name. Note: |
| 44 | +- Attributes and associations of the `returnEntity` that are not present in the query are ignored |
| 45 | +- Only associations owned by the `returnEntity` are considered when setting associations |
| 46 | + |
| 47 | +This action takes the following parameters: |
| 48 | +* `statement`: OQL Query or fully qualified name of a Dataset (module and Dataset name) to be executed |
| 49 | +* `returnEntity`: Entity type to be used to store results |
| 50 | +* `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 |
| 51 | +* `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 |
| 52 | +* `preserveParameters`: If false, all parameters defined for this query will be cleared |
| 53 | + |
| 54 | +### CountRowsOQLStatement {#countrowsoqlstatement} |
| 55 | + |
| 56 | +Similar to `ExecuteOQLStatement` above, but returns only the number of results. Useful to obtain information from the database without the overhead of generating Mendix objects. |
| 57 | + |
| 58 | +Returns: The number of results of executing `statement` |
| 59 | + |
| 60 | +This action does not support datasets |
| 61 | + |
| 62 | +[Named parameters](#named-parameters) are always reset on completion. |
| 63 | + |
| 64 | +This action takes the following parameters: |
| 65 | +* `statement`: OQL Query to be executed |
| 66 | +* `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 |
| 67 | +* `offset`: This parameter is ignored and might be removed in a future version |
| 68 | + |
| 69 | +### ExportOQLToCSV {#exportoqltocsv} |
| 70 | + |
| 71 | +This action executes an OQL query and saves the result in a CSV file. |
| 72 | + |
| 73 | +Returns: An object of type `FileDocument` (or a specialization of it) containing the CSV file with the results of executing `statement` |
| 74 | + |
| 75 | +[Named parameters](#named-parameters) are always reset on completion. |
| 76 | + |
| 77 | +This action takes the following parameters: |
| 78 | +* `statement`: OQL Query or fully qualified name of a Dataset (module and Dataset name) to be executed |
| 79 | +* `returnEntity`: A FileDocument or one of its specializations where the resulting file will be stored |
| 80 | +* `removeNewLinesFromValues`: Indicates if new lines inside string values should be replaced with spaces |
| 81 | +* `zipResult`: Indicates if the resulting file should be compressed inside a ZIP file |
| 82 | +* `exportHeaders`: Indicates if the first line of the result should contain a header with the names of each column |
| 83 | +* `separatorChar`: Indicates what character should be used to separate columns in the result |
| 84 | +* `quoteChar`: Indicates what character should be used to quote string values. May be left empty if `escapeChar` is defined |
| 85 | +* `escapeChar`: Indicates what character should be used to escape spaces and other special characters if string values are unquoted. Only applicable if `escapeChar` is not defined |
| 86 | + |
| 87 | +## Named Parameters {#named-parameters} |
| 88 | + |
| 89 | +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: |
| 90 | +* `AddBooleanParameter`: For Boolean parameters |
| 91 | +* `AddDateTimeParameter`: For Date and Time parameters |
| 92 | +* `AddDecimalParameter`: For parameters of type Decimal |
| 93 | +* `AddIntegerLongValue`: For parameters of type Integer/Long |
| 94 | +* `AddObjectParameter`: For parameters that reference a Mendix Object |
| 95 | +* `AddStringParameter`: For String parameters |
| 96 | + |
| 97 | +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. |
0 commit comments