1
0
mirror of https://github.com/Bayselonarrend/OpenIntegrations.git synced 2025-11-27 22:18:36 +02:00
Files
OpenIntegrations/docs/en/md/PostgreSQL/Common-methods/Execute-sql-query.mdx

166 lines
5.5 KiB
Plaintext
Raw Normal View History

2025-02-12 19:33:53 +03:00
---
sidebar_position: 4
2025-05-05 11:15:20 +03:00
description: Execute SQL query and other functions to work with PostgreSQL in the Open Integration Package, a free open-source integration library for 1C:Enterprise 8, OneScript and CLI
2025-05-05 09:49:19 +03:00
keywords: [1C, 1С, 1С:Enterprise, 1С:Enterprise 8.3, API, Integration, Services, Exchange, OneScript, CLI, PostgreSQL]
2025-02-12 19:33:53 +03:00
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
# Execute SQL query
Executes an arbitrary SQL query
2025-03-11 21:09:03 +03:00
`Function ExecuteSQLQuery(Val QueryText, Val Parameters = "", Val ForceResult = False, Val Connection = "", Val Tls = "") Export`
2025-02-12 19:33:53 +03:00
| Parameter | CLI option | Type | Required | Description |
|-|-|-|-|-|
| QueryText | --sql | String | ✔ | Database query text |
| Parameters | --params | Array Of Arbitrary | ✖ | Array of positional parameters of the request |
| ForceResult | --force | Boolean | ✖ | Includes an attempt to retrieve the result, even for nonSELECT queries |
| Connection | --dbc | String, Arbitrary | ✖ | Connection or connection string |
2025-03-11 21:09:03 +03:00
| Tls | --tls | Structure Of KeyAndValue | ✖ | TLS settings, if necessary. See GetTlsSettings |
2025-02-12 19:33:53 +03:00
2025-02-17 17:16:27 +03:00
Returns: Map Of KeyAndValue - Result of query execution
2025-02-12 19:33:53 +03:00
<br/>
:::tip
2025-02-15 19:36:59 +03:00
Query parameters are specified as an array of structures of the following type: `{'Type': 'Value'}`. The list of available types is described on the initial page of the PostgreSQL library documentation
2025-02-12 19:33:53 +03:00
Without specifying the `ForcifyResult` flag, result data is returned only for queries beginning with `SELECT` keyword For other queries, `result:true` or `false` with error text is returned
:::
<br/>
```bsl title="1C:Enterprise/OneScript code example"
2025-10-21 11:36:43 +03:00
Image = "https://hut.openintegrations.dev/test_data/picture.jpg";
2025-02-13 21:31:59 +03:00
OPI_TypeConversion.GetBinaryData(Image); // Image - Type: BinaryData
2025-10-21 11:36:43 +03:00
Address = "127.0.0.1";
2025-02-13 21:31:59 +03:00
Login = "bayselonarrend";
2025-10-21 11:36:43 +03:00
Password = "12we...";
2025-02-13 21:31:59 +03:00
Base = "test_data";
2025-09-01 14:51:24 +03:00
TLS = True;
Port = 5432;
ConnectionString = OPI_PostgreSQL.GenerateConnectionString(Address, Base, Login, Password, Port);
If TLS Then
TLSSettings = OPI_PostgreSQL.GetTLSSettings(True);
Else
TLSSettings = Undefined;
EndIf;
Connection = OPI_PostgreSQL.CreateConnection(ConnectionString, TLSSettings);
2025-02-13 21:31:59 +03:00
// CREATE
QueryText = "
2025-03-06 20:58:59 +03:00
|CREATE TABLE test_table (
|id SERIAL PRIMARY KEY,
|name NAME,
|age INT,
|salary REAL,
|is_active BOOL,
|created_at DATE,
|data BYTEA
|);";
2025-02-13 21:31:59 +03:00
Result = OPI_PostgreSQL.ExecuteSQLQuery(QueryText, , , Connection);
// INSERT with parameters
QueryText = "
2025-03-06 20:58:59 +03:00
|INSERT INTO test_table (name, age, salary, is_active, created_at, data)
|VALUES ($1, $2, $3, $4, $5, $6);";
2025-02-13 21:31:59 +03:00
ParameterArray = New Array;
ParameterArray.Add(New Structure("NAME" , "Vitaly"));
ParameterArray.Add(New Structure("INT" , 25));
ParameterArray.Add(New Structure("REAL" , 1000.12));
ParameterArray.Add(New Structure("BOOL" , True));
ParameterArray.Add(New Structure("DATE" , OPI_Tools.GetCurrentDate()));
ParameterArray.Add(New Structure("BYTEA", Image));
Result = OPI_PostgreSQL.ExecuteSQLQuery(QueryText, ParameterArray, , Connection);
// SELECT (The result of this query is shown in the Result block)
QueryText = "SELECT id, name, age, salary, is_active, created_at, data FROM test_table;";
Result = OPI_PostgreSQL.ExecuteSQLQuery(QueryText, , , Connection);
// DO + Transaction
QueryText = "DO $$
2025-03-06 20:58:59 +03:00
|BEGIN
| CREATE TABLE users (
| id SMALLSERIAL,
| name TEXT NOT NULL,
| age INT NOT NULL
| );
| INSERT INTO users (name, age) VALUES ('Alice', 30);
| INSERT INTO users (name, age) VALUES ('Bob', 25);
| INSERT INTO users (name, age) VALUES ('Charlie', 35);
|END $$ LANGUAGE plpgsql;";
2025-02-15 19:36:59 +03:00
Result = OPI_PostgreSQL.ExecuteSQLQuery(QueryText, , , Connection);
// SQL query from file
2025-10-21 11:36:43 +03:00
SQLFile = "https://hut.openintegrations.dev/test_data/TEST_DATA.sql"; // Binary Data, URL or path to file
2025-02-15 19:36:59 +03:00
Result = OPI_PostgreSQL.ExecuteSQLQuery(SQLFile, , , Connection);
Closing = OPI_PostgreSQL.CloseConnection(Connection);
2025-02-12 19:33:53 +03:00
```
2025-02-18 14:08:09 +03:00
<Tabs>
<TabItem value="bash" label="Bash" default>
```bash
2025-10-07 19:27:04 +03:00
# JSON data can also be passed as a path to a .json file
2025-02-18 14:08:09 +03:00
oint postgres ExecuteSQLQuery \
2025-10-07 19:27:04 +03:00
--sql "https://hut.openintegrations.dev/test_data/TEST_DATA.sql" \
2025-09-16 09:07:33 +03:00
--dbc "postgresql://bayselonarrend:***@127.0.0.1:5432/" \
2025-10-07 19:27:04 +03:00
--tls "{'accept_invalid_certs':true,'ca_cert_path':'','use_tls':true}"
2025-02-18 14:08:09 +03:00
```
</TabItem>
<TabItem value="bat" label="CMD/Bat" default>
```batch
2025-10-07 19:27:04 +03:00
:: JSON data can also be passed as a path to a .json file
2025-02-18 14:08:09 +03:00
oint postgres ExecuteSQLQuery ^
2025-10-07 19:27:04 +03:00
--sql "https://hut.openintegrations.dev/test_data/TEST_DATA.sql" ^
2025-09-16 09:07:33 +03:00
--dbc "postgresql://bayselonarrend:***@127.0.0.1:5432/" ^
2025-10-07 19:27:04 +03:00
--tls "{'accept_invalid_certs':true,'ca_cert_path':'','use_tls':true}"
2025-02-18 14:08:09 +03:00
```
</TabItem>
</Tabs>
2025-06-17 16:20:59 +03:00
```json title="Result"
{
"data": [
{
"age": 25,
2025-10-15 14:32:00 +03:00
"created_at": "2025-10-15",
2025-06-17 16:20:59 +03:00
"data": {
"BYTEA": "Base64"
},
"id": 1,
"is_active": true,
"name": "Vitaly",
"salary": 1000.11999511719
}
],
"result": true
}
```