# ExecuteQuery

Required Frends version: 5.5+

Required .NET version: 6.0

Compatible Agents: Crossplatform

Source code: <https://github.com/FrendsPlatform/Frends.PostgreSQL/tree/main/Frends.PostgreSQL.ExecuteQuery>

{% tabs %}
{% tab title="Parameter: Input" %}

| Name                          | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Query : `String`              | <p>Query.</p><p>Note: Normal query requires double quotes around Column and 2 single quotes around Value.</p><p><br>Default: <code>SELECT \* FROM Users</code><br>Example: <code>SELECT \* FROM Users</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| Parameters : `Parameter[]`    | <p>Query parameters.</p><p><br>Default: <code>-</code><br>Example: <code>\[{ Name = "Name", Value = "Erik" }, { Name = "Email", Value = "<erik.example@foobar.com>" }]</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| 🗝ConnectionString : `String` | <p>Connection string.</p><p><br>Default: <code>-</code><br>Example: <code>Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| ExecuteType : `ExecuteTypes`  | <p>Specifies how a command string is interpreted.</p><p>Auto: Automatically detects if the query returns data (SELECT or RETURNING clause) and uses ExecuteReader, otherwise uses NonQuery.</p><p>ExecuteReader: Use this to execute queries that return a result set (SELECT or INSERT/UPDATE/DELETE with RETURNING clause).</p><p>NonQuery: Use this to execute commands that don't return a result set (INSERT, UPDATE, DELETE without RETURNING). Returns the number of affected rows.</p><p>Possible values:</p><ul><li><code>Auto</code>:</li></ul><p>Specifies how a command string is interpreted.</p><p>Auto: Automatically detects if the query returns data (SELECT or RETURNING clause) and uses ExecuteReader, otherwise uses NonQuery.</p><p>ExecuteReader: Use this to execute queries that return a result set (SELECT or INSERT/UPDATE/DELETE with RETURNING clause).</p><p>NonQuery: Use this to execute commands that don't return a result set (INSERT, UPDATE, DELETE without RETURNING). Returns the number of affected rows.</p><ul><li><code>NonQuery</code>:</li></ul><p>Specifies how a command string is interpreted.</p><p>Auto: Automatically detects if the query returns data (SELECT or RETURNING clause) and uses ExecuteReader, otherwise uses NonQuery.</p><p>ExecuteReader: Use this to execute queries that return a result set (SELECT or INSERT/UPDATE/DELETE with RETURNING clause).</p><p>NonQuery: Use this to execute commands that don't return a result set (INSERT, UPDATE, DELETE without RETURNING). Returns the number of affected rows.</p><ul><li><code>ExecuteReader</code>:</li></ul><p>Specifies how a command string is interpreted.</p><p>Auto: Automatically detects if the query returns data (SELECT or RETURNING clause) and uses ExecuteReader, otherwise uses NonQuery.</p><p>ExecuteReader: Use this to execute queries that return a result set (SELECT or INSERT/UPDATE/DELETE with RETURNING clause).</p><p>NonQuery: Use this to execute commands that don't return a result set (INSERT, UPDATE, DELETE without RETURNING). Returns the number of affected rows.</p><p><br>Default: <code>0</code><br>Example: <code>ExecuteTypes.Auto</code></p> |
| {% endtab %}                  |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |

{% tab title="Parameter: Options" %}

| Name                                                       | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| ---------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| CommandTimeoutSeconds : `Int32`                            | <p>Timeout in seconds.</p><p><br>Default: <code>30</code><br>Example: <code>30</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| SqlTransactionIsolationLevel : `TransactionIsolationLevel` | <p>Transaction isolation level for the query.</p><p>Options:</p><p>- Default</p><p>- ReadCommited</p><p>- None</p><p>- Serializable</p><p>- ReadUncommited</p><p>- RepeatableRead</p><p>- Snapshot</p><p>Additional information can be found from <a href="https://www.postgresql.org/docs/current/transaction-iso.html">here</a></p><p>Possible values:</p><ul><li><code>Default</code>:</li></ul><p>Transaction isolation level for the query.</p><p>Options:</p><p>- Default</p><p>- ReadCommited</p><p>- None</p><p>- Serializable</p><p>- ReadUncommited</p><p>- RepeatableRead</p><p>- Snapshot</p><p>Additional information can be found from <a href="https://www.postgresql.org/docs/current/transaction-iso.html">here</a></p><ul><li><code>ReadCommited</code>:</li></ul><p>Transaction isolation level for the query.</p><p>Options:</p><p>- Default</p><p>- ReadCommited</p><p>- None</p><p>- Serializable</p><p>- ReadUncommited</p><p>- RepeatableRead</p><p>- Snapshot</p><p>Additional information can be found from <a href="https://www.postgresql.org/docs/current/transaction-iso.html">here</a></p><ul><li><code>None</code>:</li></ul><p>Transaction isolation level for the query.</p><p>Options:</p><p>- Default</p><p>- ReadCommited</p><p>- None</p><p>- Serializable</p><p>- ReadUncommited</p><p>- RepeatableRead</p><p>- Snapshot</p><p>Additional information can be found from <a href="https://www.postgresql.org/docs/current/transaction-iso.html">here</a></p><ul><li><code>Serializable</code>:</li></ul><p>Transaction isolation level for the query.</p><p>Options:</p><p>- Default</p><p>- ReadCommited</p><p>- None</p><p>- Serializable</p><p>- ReadUncommited</p><p>- RepeatableRead</p><p>- Snapshot</p><p>Additional information can be found from <a href="https://www.postgresql.org/docs/current/transaction-iso.html">here</a></p><ul><li><code>ReadUncommited</code>:</li></ul><p>Transaction isolation level for the query.</p><p>Options:</p><p>- Default</p><p>- ReadCommited</p><p>- None</p><p>- Serializable</p><p>- ReadUncommited</p><p>- RepeatableRead</p><p>- Snapshot</p><p>Additional information can be found from <a href="https://www.postgresql.org/docs/current/transaction-iso.html">here</a></p><ul><li><code>RepeatableRead</code>:</li></ul><p>Transaction isolation level for the query.</p><p>Options:</p><p>- Default</p><p>- ReadCommited</p><p>- None</p><p>- Serializable</p><p>- ReadUncommited</p><p>- RepeatableRead</p><p>- Snapshot</p><p>Additional information can be found from <a href="https://www.postgresql.org/docs/current/transaction-iso.html">here</a></p><ul><li><code>Snapshot</code>:</li></ul><p>Transaction isolation level for the query.</p><p>Options:</p><p>- Default</p><p>- ReadCommited</p><p>- None</p><p>- Serializable</p><p>- ReadUncommited</p><p>- RepeatableRead</p><p>- Snapshot</p><p>Additional information can be found from <a href="https://www.postgresql.org/docs/current/transaction-iso.html">here</a></p><p><br>Default: <code>0</code><br>Example: <code>Default</code></p> |
| {% endtab %}                                               |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |

{% tab title="Result: Result" %}

| Name                   | Description                                                                                                            |
| ---------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| QueryResult : `Object` | <p>Result of the query.<br>Example: <code>\[{ "id": 123, "Name": "Matti" }, { "id": 124, "Name": "Teppo" }]</code></p> |
| {% endtab %}           |                                                                                                                        |

{% tab title="Changelog" %}

## Changelog

### \[2.0.0] - 2025-12-02

#### Added

* Added `ExecuteType` parameter to Input class with options: Auto (default), ExecuteReader, and NonQuery.
* Added support for INSERT/UPDATE/DELETE statements with RETURNING clause to return actual column values instead of just AffectedRows.
* Added `ExecuteTypes` enum to provide explicit control over query execution behavior.

#### Changed

* Modified query execution logic to use ExecuteType parameter instead of simple string parsing.
* Auto mode now checks reader.FieldCount to determine if data is returned, providing more reliable detection than keyword matching.
* Transaction handling now only applies to write operations (Auto and NonQuery modes), not read-only queries (ExecuteReader mode).

#### Fixed

* Fixed issue where INSERT/UPDATE/DELETE with RETURNING clause only returned AffectedRows instead of the actual returned column values.

### \[1.1.0] - 2024-08-23

#### Changed

* Updated the Newtonsoft.Json package to version 13.0.3 and the Npgsql package to version 8.0.3.

### \[1.0.1] - 2023-02-02

#### Fixed

* Fixed memory leak issue by adding cleanup method to the main class.

### \[1.0.0] - 2022-10-13

#### Added

* Initial implementation
  {% endtab %}
  {% endtabs %}
