Skip to content

Commit 2549109

Browse files
authored
Merge pull request #157 from recloud-dev/fix/docs
Fixed code example of type usage docs
2 parents fcd691a + 5779b18 commit 2549109

File tree

8 files changed

+100
-118
lines changed

8 files changed

+100
-118
lines changed

docs/contribute.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ One of the best ways is follow [maturin offical documentation](https://www.matur
2222
```bash
2323
> python3 -m venv .venv
2424
> source .venv/bin/activate
25-
> pip install -U pip maturin
25+
> pip install -U pip maturin pre-commit pytest pytest-anyio pydantic pgpq
2626
```
2727

2828
Then you need to build `PSQLPy` project.

docs/usage/types/advanced_type_usage.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Due to an unavailability to support all possible types in PostgreSQL, we have a
66
This section has `Advanced` in the name because you'll need to work with raw bytes which can be difficult for some developers.
77

88
## Pass unsupported type into PostgreSQL
9-
If you are using some type that we don't support and want to insert it into PostgreSQL from PSQLPy, you must use `PyCustomType` class.
9+
If you are using some type that we don't support and want to insert it into PostgreSQL from PSQLPy, you must use `CustomType` class.
1010

1111
Let's assume we have table `for_test` in the database and `PSQLPy` doesn't support (only for demonstration) `VARCHAR` type:
1212
| database type | database column name |
@@ -16,24 +16,24 @@ Let's assume we have table `for_test` in the database and `PSQLPy` doesn't suppo
1616
from typing import Final
1717

1818
from psqlpy import ConnectionPool
19-
from psqlpy.extra_types import PyCustomType
19+
from psqlpy.extra_types import CustomType
2020

2121

2222
async def main() -> None:
2323
# It uses default connection parameters
2424
db_pool: Final = ConnectionPool()
2525

26-
await db_pool.execute(
27-
"INSERT INTO for_test (nickname) VALUES ($1)",
28-
[PyCustomType(b"SomeDataInBytes")],
29-
)
30-
db_pool.close()
26+
async with db_pool.acquire() as connection:
27+
await connection.execute(
28+
"INSERT INTO for_test (nickname) VALUES ($1)",
29+
[CustomType(b"SomeDataInBytes")],
30+
)
3131
```
3232

33-
Here we pass `PyCustomType` into the parameters. It accepts only bytes.
33+
Here we pass `CustomType` into the parameters. It accepts only bytes.
3434

3535
::: important
36-
You must make bytes passed into `PyCustomType` readable for `PostgreSQL`.
36+
You must make bytes passed into `CustomType` readable for `PostgreSQL`.
3737
If bytes will be wrong, you will get an exception.
3838
:::
3939

@@ -49,7 +49,7 @@ Let's assume we have table `for_test` in the database and `PSQLPy` doesn't suppo
4949
from typing import Final, Any
5050

5151
from psqlpy import ConnectionPool, QueryResult
52-
from psqlpy.extra_types import PyCustomType
52+
from psqlpy.extra_types import CustomType
5353

5454

5555
def nickname_decoder(bytes_from_psql: bytes | None) -> str:
@@ -60,17 +60,17 @@ async def main() -> None:
6060
# It uses default connection parameters
6161
db_pool: Final = ConnectionPool()
6262

63-
result: QueryResult = await db_pool.execute(
64-
"SELECT * FROM for_test",
65-
[PyCustomType(b"SomeDataInBytes")],
66-
)
63+
async with db_pool.acquire() as connection:
64+
result: QueryResult = await connection.execute(
65+
"SELECT * FROM for_test",
66+
[CustomType(b"SomeDataInBytes")],
67+
)
6768

6869
parsed_result: list[dict[str, Any]] = result.result(
6970
custom_decoders={
7071
"nickname": nickname_decoder,
7172
},
7273
)
73-
db_pool.close()
7474
```
7575

7676
::: important

docs/usage/types/array_types.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ from psqlpy.extra_types import TextArray
4949

5050
async def main() -> None:
5151
pool = ConnectionPool()
52-
result = await pool.execute(
53-
querystring="SELECT * FROM users WHERE name = ANY($1)",
54-
parameters=[
55-
TextArray(["Alex", "Dev", "Who"]),
56-
]
57-
)
52+
async with db_pool.acquire() as connection:
53+
result = await connection.execute(
54+
querystring="SELECT * FROM users WHERE name = ANY($1)",
55+
parameters=[
56+
TextArray(["Alex", "Dev", "Who"]),
57+
]
58+
)
5859
```

docs/usage/types/extra_types.md

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,11 @@ from psqlpy.extra_types import SmallInt, Integer, BigInt, Float32, Float64
6262
async def main() -> None:
6363
# It uses default connection parameters
6464
db_pool: Final = ConnectionPool()
65-
66-
await db_pool.execute(
67-
"INSERT INTO numbers (index, elf_life, elon_musk_money) VALUES ($1, $2, $3, $4, $5)",
68-
[SmallInt(101), Integer(10500), BigInt(300000000000), Float32(123.11), Float64(222.12)],
69-
)
70-
db_pool.close()
65+
async with db_pool.acquire() as connection:
66+
await connection.execute(
67+
"INSERT INTO numbers (index, elf_life, elon_musk_money) VALUES ($1, $2, $3, $4, $5)",
68+
[SmallInt(101), Integer(10500), BigInt(300000000000), Float32(123.11), Float64(222.12)],
69+
)
7170
```
7271

7372
::: important
@@ -96,16 +95,16 @@ async def main() -> None:
9695
# It uses default connection parameters
9796
db_pool: Final = ConnectionPool()
9897

99-
await db_pool.execute(
100-
"INSERT INTO banners (title, description) VALUES ($1, $2)",
101-
["SomeTitle", PyText("Very long description")],
102-
)
103-
# Alternatively, you can do this:
104-
await db_pool.execute(
105-
"INSERT INTO banners (title, description) VALUES ($1, $2)",
106-
[PyVarChar("SomeTitle"), PyText("Very long description")],
107-
)
108-
db_pool.close()
98+
async with db_pool.acquire() as connection:
99+
await connection.execute(
100+
"INSERT INTO banners (title, description) VALUES ($1, $2)",
101+
["SomeTitle", PyText("Very long description")],
102+
)
103+
# Alternatively, you can do this:
104+
await connection.execute(
105+
"INSERT INTO banners (title, description) VALUES ($1, $2)",
106+
[PyVarChar("SomeTitle"), PyText("Very long description")],
107+
)
109108
```
110109

111110
## PyJSON & PyJSONB
@@ -140,6 +139,7 @@ from psqlpy.extra_types import PyJSON
140139
async def main() -> None:
141140
# It uses default connection parameters
142141
db_pool: Final = ConnectionPool()
142+
143143
list_for_jsonb_field = [
144144
{"some": "dict"},
145145
[
@@ -154,16 +154,15 @@ async def main() -> None:
154154
]
155155
}
156156

157-
await db_pool.execute(
158-
"INSERT INTO users (additional_user_info) VALUES ($1)",
159-
[PyJSONB(list_for_jsonb_field)],
160-
)
161-
await db_pool.execute(
162-
"INSERT INTO users (additional_user_info) VALUES ($1)",
163-
[dict_for_jsonb_field,],
164-
)
165-
166-
db_pool.close()
157+
async with db_pool.acquire() as connection:
158+
await connection.execute(
159+
"INSERT INTO users (additional_user_info) VALUES ($1)",
160+
[PyJSONB(list_for_jsonb_field)],
161+
)
162+
await connection.execute(
163+
"INSERT INTO users (additional_user_info) VALUES ($1)",
164+
[dict_for_jsonb_field,],
165+
)
167166
```
168167

169168
## PyMacAddr6 & PyMacAddr8
@@ -186,15 +185,14 @@ async def main() -> None:
186185
# It uses default connection parameters
187186
db_pool: Final = ConnectionPool()
188187

189-
await db_pool.execute(
190-
"INSERT INTO devices (device_macaddr6, device_macaddr8) VALUES ($1, $2)",
191-
[
192-
PyMacAddr6("08:00:2b:01:02:03"),
193-
PyMacAddr8("08:00:2b:01:02:03:04:05"),
194-
],
195-
)
196-
197-
db_pool.close()
188+
async with db_pool.acquire() as connection:
189+
await connection.execute(
190+
"INSERT INTO devices (device_macaddr6, device_macaddr8) VALUES ($1, $2)",
191+
[
192+
PyMacAddr6("08:00:2b:01:02:03"),
193+
PyMacAddr8("08:00:2b:01:02:03:04:05"),
194+
],
195+
)
198196
```
199197

200198
## Geo Types
@@ -222,17 +220,16 @@ async def main() -> None:
222220
# It uses default connection parameters
223221
db_pool: Final = ConnectionPool()
224222

225-
await db_pool.execute(
226-
"INSERT INTO geo_info VALUES ($1, $2, $3, $4, $5, $6)",
227-
[
228-
Point([1.5, 2]),
229-
Box([(1.7, 2.8), (9, 9)]),
230-
Path([(3.5, 3), (9, 9), (8, 8)]),
231-
Line([1, -2, 3]),
232-
LineSegment([(5.6, 3.1), (4, 5)]),
233-
Circle([5, 1.8, 10]),
234-
],
235-
)
236-
237-
db_pool.close()
223+
async with db_pool.acquire() as connection:
224+
await connection.execute(
225+
"INSERT INTO geo_info VALUES ($1, $2, $3, $4, $5, $6)",
226+
[
227+
Point([1.5, 2]),
228+
Box([(1.7, 2.8), (9, 9)]),
229+
Path([(3.5, 3), (9, 9), (8, 8)]),
230+
Line([1, -2, 3]),
231+
LineSegment([(5.6, 3.1), (4, 5)]),
232+
Circle([5, 1.8, 10]),
233+
],
234+
)
238235
```

docs/usage/types/supported_types.md

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ async def main() -> None:
8787
# It uses default connection parameters
8888
db_pool: Final = ConnectionPool()
8989

90-
result = await db_pool.execute(
91-
"SELECT user_info FROM custom_table",
92-
)
93-
print(result.result()[0])
90+
async with db_pool.acquire() as connection:
91+
result = await connection.execute(
92+
"SELECT user_info FROM custom_table",
93+
)
94+
print(result.result()[0])
9495
```
9596
It will return:
9697
```json
@@ -132,22 +133,22 @@ class Weather(str, Enum):
132133
async def main() -> None:
133134
# It uses default connection parameters
134135
db_pool: Final = ConnectionPool()
135-
136-
# Insert new data
137-
await db_pool.execute(
138-
querystring="INSERT INTO weather_plus VALUES($1)",
139-
parameters=[Weather.SUN],
140-
)
141-
142-
# Or you can pass string directly
143-
await db_pool.execute(
144-
querystring="INSERT INTO weather_plus VALUES($1)",
145-
parameters=["sun"],
146-
)
147-
148-
result = await db_pool.execute(
149-
querystring="SELECT * FROM weather_plus",
150-
)
136+
async with db_pool.acquire() as connection:
137+
# Insert new data
138+
await connection.execute(
139+
querystring="INSERT INTO weather_plus VALUES($1)",
140+
parameters=[Weather.SUN],
141+
)
142+
143+
# Or you can pass string directly
144+
await connection.execute(
145+
querystring="INSERT INTO weather_plus VALUES($1)",
146+
parameters=["sun"],
147+
)
148+
149+
result = await connection.execute(
150+
querystring="SELECT * FROM weather_plus",
151+
)
151152
print(result.result()[0])
152153
```
153154
You will receive:

src/statement/parameters.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
},
1717
};
1818

19-
pub type QueryParameter = (dyn ToSql + Sync);
19+
pub type QueryParameter = dyn ToSql + Sync;
2020

2121
#[pyclass]
2222
#[derive(Default, Clone, Debug)]

src/value_converter/dto/impls.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,14 @@ impl ToSql for PythonDTO {
177177
<Vec<u8> as ToSql>::to_sql(pybytes, ty, out)?;
178178
}
179179
PythonDTO::PyBool(boolean) => types::bool_to_sql(*boolean, out),
180-
PythonDTO::PyVarChar(string) => {
181-
<&str as ToSql>::to_sql(&string.as_str(), ty, out)?;
182-
}
183-
PythonDTO::PyText(string) => {
180+
PythonDTO::PyVarChar(string)
181+
| PythonDTO::PyText(string)
182+
| PythonDTO::PyString(string) => {
184183
<&str as ToSql>::to_sql(&string.as_str(), ty, out)?;
185184
}
186185
PythonDTO::PyUUID(pyuuid) => {
187186
<Uuid as ToSql>::to_sql(pyuuid, ty, out)?;
188187
}
189-
PythonDTO::PyString(string) => {
190-
<&str as ToSql>::to_sql(&string.as_str(), ty, out)?;
191-
}
192188
PythonDTO::PyIntI16(int) => out.put_i16(*int),
193189
PythonDTO::PyIntI32(int) => out.put_i32(*int),
194190
PythonDTO::PyIntI64(int) | PythonDTO::PyMoney(int) => out.put_i64(*int),

src/value_converter/to_python.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,11 @@ fn postgres_bytes_to_py(
180180
}
181181
Ok(py.None())
182182
}
183-
Type::OID => Ok(
184-
composite_field_postgres_to_py::<Option<i32>>(type_, buf, is_simple)?
185-
.into_py_any(py)?,
186-
),
187-
Type::NAME => Ok(
188-
composite_field_postgres_to_py::<Option<String>>(type_, buf, is_simple)?
189-
.into_py_any(py)?,
190-
),
183+
// Convert Integer into i32, then into int
184+
Type::OID | Type::INT4 => Ok(composite_field_postgres_to_py::<Option<i32>>(
185+
type_, buf, is_simple,
186+
)?
187+
.into_py_any(py)?),
191188
// // ---------- String Types ----------
192189
// // Convert TEXT and VARCHAR type into String, then into str
193190
Type::TEXT | Type::VARCHAR | Type::XML => Ok(composite_field_postgres_to_py::<
@@ -206,11 +203,6 @@ fn postgres_bytes_to_py(
206203
composite_field_postgres_to_py::<Option<i16>>(type_, buf, is_simple)?
207204
.into_py_any(py)?,
208205
),
209-
// Convert Integer into i32, then into int
210-
Type::INT4 => Ok(
211-
composite_field_postgres_to_py::<Option<i32>>(type_, buf, is_simple)?
212-
.into_py_any(py)?,
213-
),
214206
// Convert BigInt into i64, then into int
215207
Type::INT8 | Type::MONEY => Ok(composite_field_postgres_to_py::<Option<i64>>(
216208
type_, buf, is_simple,
@@ -363,7 +355,8 @@ fn postgres_bytes_to_py(
363355
composite_field_postgres_to_py::<Option<Array<bool>>>(type_, buf, is_simple)?,
364356
)
365357
.into_py_any(py)?),
366-
Type::OID_ARRAY => Ok(postgres_array_to_py(
358+
// Convert ARRAY of Integer into Vec<i32>, then into list[int]
359+
Type::OID_ARRAY | Type::INT4_ARRAY => Ok(postgres_array_to_py(
367360
py,
368361
composite_field_postgres_to_py::<Option<Array<i32>>>(type_, buf, is_simple)?,
369362
)
@@ -381,12 +374,6 @@ fn postgres_bytes_to_py(
381374
composite_field_postgres_to_py::<Option<Array<i16>>>(type_, buf, is_simple)?,
382375
)
383376
.into_py_any(py)?),
384-
// Convert ARRAY of Integer into Vec<i32>, then into list[int]
385-
Type::INT4_ARRAY => Ok(postgres_array_to_py(
386-
py,
387-
composite_field_postgres_to_py::<Option<Array<i32>>>(type_, buf, is_simple)?,
388-
)
389-
.into_py_any(py)?),
390377
// Convert ARRAY of BigInt into Vec<i64>, then into list[int]
391378
Type::INT8_ARRAY | Type::MONEY_ARRAY => Ok(postgres_array_to_py(
392379
py,

0 commit comments

Comments
 (0)