postgresdb3 0.2.3__tar.gz → 0.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {postgresdb3-0.2.3 → postgresdb3-0.3.1}/PKG-INFO +54 -25
- {postgresdb3-0.2.3 → postgresdb3-0.3.1}/README.md +51 -22
- {postgresdb3-0.2.3 → postgresdb3-0.3.1}/postgresdb3/async_db.py +94 -39
- postgresdb3-0.3.1/postgresdb3/sync_db.py +368 -0
- {postgresdb3-0.2.3 → postgresdb3-0.3.1}/postgresdb3.egg-info/PKG-INFO +54 -25
- {postgresdb3-0.2.3 → postgresdb3-0.3.1}/setup.py +4 -2
- postgresdb3-0.2.3/postgresdb3/sync_db.py +0 -189
- {postgresdb3-0.2.3 → postgresdb3-0.3.1}/postgresdb3/__init__.py +0 -0
- {postgresdb3-0.2.3 → postgresdb3-0.3.1}/postgresdb3.egg-info/SOURCES.txt +0 -0
- {postgresdb3-0.2.3 → postgresdb3-0.3.1}/postgresdb3.egg-info/dependency_links.txt +0 -0
- {postgresdb3-0.2.3 → postgresdb3-0.3.1}/postgresdb3.egg-info/requires.txt +0 -0
- {postgresdb3-0.2.3 → postgresdb3-0.3.1}/postgresdb3.egg-info/top_level.txt +0 -0
- {postgresdb3-0.2.3 → postgresdb3-0.3.1}/pyproject.toml +0 -0
- {postgresdb3-0.2.3 → postgresdb3-0.3.1}/setup.cfg +0 -0
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: postgresdb3
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.1
|
|
4
4
|
Summary: Python uchun oddiy PostgreSQL wrapper
|
|
5
|
-
Home-page: https://github.com/AlijonovUz/PostgresDB
|
|
6
5
|
Author: Abdulbosit Alijonov
|
|
6
|
+
Project-URL: Source Code, https://github.com/AlijonovUz/PostgresDB
|
|
7
7
|
Classifier: Programming Language :: Python :: 3
|
|
8
8
|
Classifier: License :: OSI Approved :: MIT License
|
|
9
9
|
Classifier: Operating System :: OS Independent
|
|
@@ -15,7 +15,7 @@ Dynamic: author
|
|
|
15
15
|
Dynamic: classifier
|
|
16
16
|
Dynamic: description
|
|
17
17
|
Dynamic: description-content-type
|
|
18
|
-
Dynamic:
|
|
18
|
+
Dynamic: project-url
|
|
19
19
|
Dynamic: requires-dist
|
|
20
20
|
Dynamic: requires-python
|
|
21
21
|
Dynamic: summary
|
|
@@ -36,7 +36,7 @@ pip install postgresdb3
|
|
|
36
36
|
|
|
37
37
|
## Foydalanish (Sync)
|
|
38
38
|
|
|
39
|
-
```
|
|
39
|
+
```python
|
|
40
40
|
from postgresdb3 import PostgresDB
|
|
41
41
|
|
|
42
42
|
# Bazaga ulanish
|
|
@@ -44,16 +44,23 @@ db = PostgresDB(
|
|
|
44
44
|
database="mydb",
|
|
45
45
|
user="postgres",
|
|
46
46
|
password="mypassword",
|
|
47
|
-
host="localhost",
|
|
48
|
-
port=5432
|
|
47
|
+
host="localhost", # ixtiyoriy
|
|
48
|
+
port=5432 # ixtiyoriy
|
|
49
49
|
)
|
|
50
50
|
|
|
51
51
|
# Jadval yaratish
|
|
52
52
|
db.create("users", "id SERIAL PRIMARY KEY, name VARCHAR(100), age INT")
|
|
53
53
|
|
|
54
|
-
#
|
|
54
|
+
# Bitta qator qo'shish
|
|
55
55
|
db.insert("users", "name, age", ("Ali", 25))
|
|
56
56
|
|
|
57
|
+
# Bir nechta qator qo'shish
|
|
58
|
+
db.insert_many(
|
|
59
|
+
"users",
|
|
60
|
+
"name, age",
|
|
61
|
+
[("Vali", 30), ("Gulnoza", 22), ("Hasan", 28)]
|
|
62
|
+
)
|
|
63
|
+
|
|
57
64
|
# Ma'lumotlarni olish
|
|
58
65
|
users = db.select("users")
|
|
59
66
|
print(users)
|
|
@@ -66,10 +73,14 @@ print(user)
|
|
|
66
73
|
adults = db.select("users", where=("age > %s", [18]))
|
|
67
74
|
print(adults)
|
|
68
75
|
|
|
69
|
-
# Bir nechta shart
|
|
76
|
+
# Bir nechta shart bilan
|
|
70
77
|
specific_users = db.select("users", where=("age > %s AND name = %s", [18, "Ali"]))
|
|
71
78
|
print(specific_users)
|
|
72
79
|
|
|
80
|
+
# Faqat 2 ta qatorni olish
|
|
81
|
+
users = db.select("users", fetchmany=2)
|
|
82
|
+
print(users)
|
|
83
|
+
|
|
73
84
|
# Jadvaldagi ma'lumotni yangilash
|
|
74
85
|
db.update("users", "age", 26, "name", "Ali")
|
|
75
86
|
|
|
@@ -77,7 +88,14 @@ db.update("users", "age", 26, "name", "Ali")
|
|
|
77
88
|
db.delete("users", "name", "Ali")
|
|
78
89
|
|
|
79
90
|
# Jadvalni o'chirish
|
|
80
|
-
db.drop("users")
|
|
91
|
+
db.drop("users", cascade=False)
|
|
92
|
+
|
|
93
|
+
# To‘g‘ridan-to‘g‘ri SQL bajarish (raw)
|
|
94
|
+
result = db.raw("SELECT name, age FROM users WHERE age > %s", [25], fetchall=True)
|
|
95
|
+
print(result)
|
|
96
|
+
|
|
97
|
+
# Connectionni yopish
|
|
98
|
+
db.close()
|
|
81
99
|
```
|
|
82
100
|
|
|
83
101
|
## Foydalanish (Async)
|
|
@@ -87,21 +105,26 @@ import asyncio
|
|
|
87
105
|
from postgresdb3 import AsyncPostgresDB
|
|
88
106
|
|
|
89
107
|
async def main():
|
|
90
|
-
# Bazaga ulanish
|
|
91
108
|
db = AsyncPostgresDB(
|
|
92
109
|
database="mydb",
|
|
93
110
|
user="postgres",
|
|
94
111
|
password="mypassword",
|
|
95
|
-
host="localhost",
|
|
96
|
-
port=5432
|
|
112
|
+
host="localhost",
|
|
113
|
+
port=5432
|
|
97
114
|
)
|
|
98
115
|
|
|
99
116
|
# Jadval yaratish
|
|
100
117
|
await db.create("users", "id SERIAL PRIMARY KEY, name VARCHAR(100), age INT")
|
|
101
118
|
|
|
102
|
-
#
|
|
119
|
+
# Bitta qator qo'shish
|
|
103
120
|
await db.insert("users", "name, age", ["Ali", 25])
|
|
104
|
-
|
|
121
|
+
|
|
122
|
+
# Bir nechta qator qo'shish
|
|
123
|
+
await db.insert_many(
|
|
124
|
+
"users",
|
|
125
|
+
"name, age",
|
|
126
|
+
[["Vali", 30], ["Gulnoza", 22], ["Hasan", 28]]
|
|
127
|
+
)
|
|
105
128
|
|
|
106
129
|
# Barcha ma'lumotlarni olish
|
|
107
130
|
users = await db.select("users")
|
|
@@ -115,10 +138,14 @@ async def main():
|
|
|
115
138
|
adults = await db.select("users", where=("age > $1", [18]))
|
|
116
139
|
print("Kattalar:", adults)
|
|
117
140
|
|
|
118
|
-
# Bir nechta shart
|
|
141
|
+
# Bir nechta shart bilan
|
|
119
142
|
specific_users = await db.select("users", where=("age > $1 AND name = $2", [18, "Ali"]))
|
|
120
143
|
print("Maxsus foydalanuvchi:", specific_users)
|
|
121
|
-
|
|
144
|
+
|
|
145
|
+
# Faqat 2 ta qatorni olish
|
|
146
|
+
users = await db.select("users", fetchmany=2)
|
|
147
|
+
print(users)
|
|
148
|
+
|
|
122
149
|
# Jadvaldagi ma'lumotni yangilash
|
|
123
150
|
await db.update("users", "age", 26, "name", "Ali")
|
|
124
151
|
|
|
@@ -126,12 +153,15 @@ async def main():
|
|
|
126
153
|
await db.delete("users", "name", "Ali")
|
|
127
154
|
|
|
128
155
|
# Jadvalni o'chirish
|
|
129
|
-
await db.drop("users")
|
|
156
|
+
await db.drop("users", cascade=False)
|
|
157
|
+
|
|
158
|
+
# To‘g‘ridan-to‘g‘ri SQL bajarish (raw)
|
|
159
|
+
result = await db.raw("SELECT name, age FROM users WHERE age > $1", [25], fetchall=True)
|
|
160
|
+
print("Raw query result:", result)
|
|
130
161
|
|
|
131
162
|
# Connection poolni yopish
|
|
132
163
|
await db.close_pool()
|
|
133
164
|
|
|
134
|
-
# Asinxron loop ishga tushirish
|
|
135
165
|
asyncio.run(main())
|
|
136
166
|
```
|
|
137
167
|
|
|
@@ -143,10 +173,12 @@ asyncio.run(main())
|
|
|
143
173
|
**create(table, columns)** --- jadval yaratish, `columns` SQL
|
|
144
174
|
sintaksisida.
|
|
145
175
|
|
|
146
|
-
**drop(table)** --- jadvalni o'chirish.
|
|
176
|
+
**drop(table, cascade=False)** --- jadvalni o'chirish.
|
|
147
177
|
|
|
148
178
|
**insert(table, columns, values)** --- ma'lumot qo'shish.
|
|
149
179
|
|
|
180
|
+
**insert_many(table, columns, values_list)** --- bir nechta qator qo‘shish
|
|
181
|
+
|
|
150
182
|
**select(table, columns="\*", where=None, join=None, group_by=None,
|
|
151
183
|
order_by=None, limit=None, fetchone=False)** --- ma'lumotlarni olish.
|
|
152
184
|
|
|
@@ -168,19 +200,16 @@ ma'lumotni yangilash.
|
|
|
168
200
|
|
|
169
201
|
**delete(table, where_column, where_value)** --- ma'lumotni o'chirish.
|
|
170
202
|
|
|
171
|
-
**manager(sql, params=None, commit=False, fetchall=False,
|
|
172
|
-
fetchone=False)** --- barcha SQL amallarni bajaruvchi yagona metod.
|
|
173
|
-
|
|
174
203
|
## Qo‘shimcha
|
|
175
204
|
|
|
176
|
-
`%s` bilan parametrizatsiya qilish xavfsiz va SQL injection'dan himoya qiladi.
|
|
205
|
+
`%s`,`$1` bilan parametrizatsiya qilish xavfsiz va SQL injection'dan himoya qiladi.
|
|
177
206
|
|
|
178
|
-
`where` va `join`
|
|
207
|
+
`where` va `join` yordamida murakkab so‘rovlar yozish mumkin.
|
|
179
208
|
|
|
180
209
|
`limit` va `order_by` parametrlaridan foydalanib ma'lumotlarni tartiblash va cheklash oson.
|
|
181
210
|
|
|
182
211
|
`commit=True` bo‘lgan amallar darhol bazaga yoziladi, select() esa hech qachon o‘zgartirish kiritmaydi.
|
|
183
212
|
|
|
184
|
-
`
|
|
213
|
+
`raw()` — barcha SQL buyruqlarini bajaruvchi yagona metod bo‘lib, kerak bo‘lganda to‘g‘ridan‑to‘g‘ri SQL yozish imkonini beradi.
|
|
185
214
|
|
|
186
215
|
PostgreSQL bilan ishlash uchun `psycopg2` kutubxonasi talab qilinadi.
|
|
@@ -14,7 +14,7 @@ pip install postgresdb3
|
|
|
14
14
|
|
|
15
15
|
## Foydalanish (Sync)
|
|
16
16
|
|
|
17
|
-
```
|
|
17
|
+
```python
|
|
18
18
|
from postgresdb3 import PostgresDB
|
|
19
19
|
|
|
20
20
|
# Bazaga ulanish
|
|
@@ -22,16 +22,23 @@ db = PostgresDB(
|
|
|
22
22
|
database="mydb",
|
|
23
23
|
user="postgres",
|
|
24
24
|
password="mypassword",
|
|
25
|
-
host="localhost",
|
|
26
|
-
port=5432
|
|
25
|
+
host="localhost", # ixtiyoriy
|
|
26
|
+
port=5432 # ixtiyoriy
|
|
27
27
|
)
|
|
28
28
|
|
|
29
29
|
# Jadval yaratish
|
|
30
30
|
db.create("users", "id SERIAL PRIMARY KEY, name VARCHAR(100), age INT")
|
|
31
31
|
|
|
32
|
-
#
|
|
32
|
+
# Bitta qator qo'shish
|
|
33
33
|
db.insert("users", "name, age", ("Ali", 25))
|
|
34
34
|
|
|
35
|
+
# Bir nechta qator qo'shish
|
|
36
|
+
db.insert_many(
|
|
37
|
+
"users",
|
|
38
|
+
"name, age",
|
|
39
|
+
[("Vali", 30), ("Gulnoza", 22), ("Hasan", 28)]
|
|
40
|
+
)
|
|
41
|
+
|
|
35
42
|
# Ma'lumotlarni olish
|
|
36
43
|
users = db.select("users")
|
|
37
44
|
print(users)
|
|
@@ -44,10 +51,14 @@ print(user)
|
|
|
44
51
|
adults = db.select("users", where=("age > %s", [18]))
|
|
45
52
|
print(adults)
|
|
46
53
|
|
|
47
|
-
# Bir nechta shart
|
|
54
|
+
# Bir nechta shart bilan
|
|
48
55
|
specific_users = db.select("users", where=("age > %s AND name = %s", [18, "Ali"]))
|
|
49
56
|
print(specific_users)
|
|
50
57
|
|
|
58
|
+
# Faqat 2 ta qatorni olish
|
|
59
|
+
users = db.select("users", fetchmany=2)
|
|
60
|
+
print(users)
|
|
61
|
+
|
|
51
62
|
# Jadvaldagi ma'lumotni yangilash
|
|
52
63
|
db.update("users", "age", 26, "name", "Ali")
|
|
53
64
|
|
|
@@ -55,7 +66,14 @@ db.update("users", "age", 26, "name", "Ali")
|
|
|
55
66
|
db.delete("users", "name", "Ali")
|
|
56
67
|
|
|
57
68
|
# Jadvalni o'chirish
|
|
58
|
-
db.drop("users")
|
|
69
|
+
db.drop("users", cascade=False)
|
|
70
|
+
|
|
71
|
+
# To‘g‘ridan-to‘g‘ri SQL bajarish (raw)
|
|
72
|
+
result = db.raw("SELECT name, age FROM users WHERE age > %s", [25], fetchall=True)
|
|
73
|
+
print(result)
|
|
74
|
+
|
|
75
|
+
# Connectionni yopish
|
|
76
|
+
db.close()
|
|
59
77
|
```
|
|
60
78
|
|
|
61
79
|
## Foydalanish (Async)
|
|
@@ -65,21 +83,26 @@ import asyncio
|
|
|
65
83
|
from postgresdb3 import AsyncPostgresDB
|
|
66
84
|
|
|
67
85
|
async def main():
|
|
68
|
-
# Bazaga ulanish
|
|
69
86
|
db = AsyncPostgresDB(
|
|
70
87
|
database="mydb",
|
|
71
88
|
user="postgres",
|
|
72
89
|
password="mypassword",
|
|
73
|
-
host="localhost",
|
|
74
|
-
port=5432
|
|
90
|
+
host="localhost",
|
|
91
|
+
port=5432
|
|
75
92
|
)
|
|
76
93
|
|
|
77
94
|
# Jadval yaratish
|
|
78
95
|
await db.create("users", "id SERIAL PRIMARY KEY, name VARCHAR(100), age INT")
|
|
79
96
|
|
|
80
|
-
#
|
|
97
|
+
# Bitta qator qo'shish
|
|
81
98
|
await db.insert("users", "name, age", ["Ali", 25])
|
|
82
|
-
|
|
99
|
+
|
|
100
|
+
# Bir nechta qator qo'shish
|
|
101
|
+
await db.insert_many(
|
|
102
|
+
"users",
|
|
103
|
+
"name, age",
|
|
104
|
+
[["Vali", 30], ["Gulnoza", 22], ["Hasan", 28]]
|
|
105
|
+
)
|
|
83
106
|
|
|
84
107
|
# Barcha ma'lumotlarni olish
|
|
85
108
|
users = await db.select("users")
|
|
@@ -93,10 +116,14 @@ async def main():
|
|
|
93
116
|
adults = await db.select("users", where=("age > $1", [18]))
|
|
94
117
|
print("Kattalar:", adults)
|
|
95
118
|
|
|
96
|
-
# Bir nechta shart
|
|
119
|
+
# Bir nechta shart bilan
|
|
97
120
|
specific_users = await db.select("users", where=("age > $1 AND name = $2", [18, "Ali"]))
|
|
98
121
|
print("Maxsus foydalanuvchi:", specific_users)
|
|
99
|
-
|
|
122
|
+
|
|
123
|
+
# Faqat 2 ta qatorni olish
|
|
124
|
+
users = await db.select("users", fetchmany=2)
|
|
125
|
+
print(users)
|
|
126
|
+
|
|
100
127
|
# Jadvaldagi ma'lumotni yangilash
|
|
101
128
|
await db.update("users", "age", 26, "name", "Ali")
|
|
102
129
|
|
|
@@ -104,12 +131,15 @@ async def main():
|
|
|
104
131
|
await db.delete("users", "name", "Ali")
|
|
105
132
|
|
|
106
133
|
# Jadvalni o'chirish
|
|
107
|
-
await db.drop("users")
|
|
134
|
+
await db.drop("users", cascade=False)
|
|
135
|
+
|
|
136
|
+
# To‘g‘ridan-to‘g‘ri SQL bajarish (raw)
|
|
137
|
+
result = await db.raw("SELECT name, age FROM users WHERE age > $1", [25], fetchall=True)
|
|
138
|
+
print("Raw query result:", result)
|
|
108
139
|
|
|
109
140
|
# Connection poolni yopish
|
|
110
141
|
await db.close_pool()
|
|
111
142
|
|
|
112
|
-
# Asinxron loop ishga tushirish
|
|
113
143
|
asyncio.run(main())
|
|
114
144
|
```
|
|
115
145
|
|
|
@@ -121,10 +151,12 @@ asyncio.run(main())
|
|
|
121
151
|
**create(table, columns)** --- jadval yaratish, `columns` SQL
|
|
122
152
|
sintaksisida.
|
|
123
153
|
|
|
124
|
-
**drop(table)** --- jadvalni o'chirish.
|
|
154
|
+
**drop(table, cascade=False)** --- jadvalni o'chirish.
|
|
125
155
|
|
|
126
156
|
**insert(table, columns, values)** --- ma'lumot qo'shish.
|
|
127
157
|
|
|
158
|
+
**insert_many(table, columns, values_list)** --- bir nechta qator qo‘shish
|
|
159
|
+
|
|
128
160
|
**select(table, columns="\*", where=None, join=None, group_by=None,
|
|
129
161
|
order_by=None, limit=None, fetchone=False)** --- ma'lumotlarni olish.
|
|
130
162
|
|
|
@@ -146,19 +178,16 @@ ma'lumotni yangilash.
|
|
|
146
178
|
|
|
147
179
|
**delete(table, where_column, where_value)** --- ma'lumotni o'chirish.
|
|
148
180
|
|
|
149
|
-
**manager(sql, params=None, commit=False, fetchall=False,
|
|
150
|
-
fetchone=False)** --- barcha SQL amallarni bajaruvchi yagona metod.
|
|
151
|
-
|
|
152
181
|
## Qo‘shimcha
|
|
153
182
|
|
|
154
|
-
`%s` bilan parametrizatsiya qilish xavfsiz va SQL injection'dan himoya qiladi.
|
|
183
|
+
`%s`,`$1` bilan parametrizatsiya qilish xavfsiz va SQL injection'dan himoya qiladi.
|
|
155
184
|
|
|
156
|
-
`where` va `join`
|
|
185
|
+
`where` va `join` yordamida murakkab so‘rovlar yozish mumkin.
|
|
157
186
|
|
|
158
187
|
`limit` va `order_by` parametrlaridan foydalanib ma'lumotlarni tartiblash va cheklash oson.
|
|
159
188
|
|
|
160
189
|
`commit=True` bo‘lgan amallar darhol bazaga yoziladi, select() esa hech qachon o‘zgartirish kiritmaydi.
|
|
161
190
|
|
|
162
|
-
`
|
|
191
|
+
`raw()` — barcha SQL buyruqlarini bajaruvchi yagona metod bo‘lib, kerak bo‘lganda to‘g‘ridan‑to‘g‘ri SQL yozish imkonini beradi.
|
|
163
192
|
|
|
164
193
|
PostgreSQL bilan ishlash uchun `psycopg2` kutubxonasi talab qilinadi.
|
|
@@ -11,7 +11,7 @@ class AsyncPostgresDB:
|
|
|
11
11
|
Aiogram kabi async frameworklar bilan to‘g‘ridan-to‘g‘ri ishlash uchun mos.
|
|
12
12
|
"""
|
|
13
13
|
|
|
14
|
-
def __init__(self, database: str, user: str, password: str, host: str = "localhost", port: int = 5432):
|
|
14
|
+
def __init__(self, database: str, user: str, password: str, host: str = "localhost", port: int = 5432) -> None:
|
|
15
15
|
"""
|
|
16
16
|
PostgreSQL bazasiga ulanishni tayyorlaydi.
|
|
17
17
|
|
|
@@ -29,7 +29,7 @@ class AsyncPostgresDB:
|
|
|
29
29
|
self.port = port
|
|
30
30
|
self.pool: Optional[asyncpg.pool.Pool] = None
|
|
31
31
|
|
|
32
|
-
async def
|
|
32
|
+
async def _manager(self, sql: str, *params, fetchone=False, fetchall=False, commit=False) -> Any:
|
|
33
33
|
"""
|
|
34
34
|
Markaziy metod: barcha SQL so‘rovlarni bajaradi.
|
|
35
35
|
|
|
@@ -63,7 +63,7 @@ class AsyncPostgresDB:
|
|
|
63
63
|
if fetchall:
|
|
64
64
|
return await conn.fetch(sql, *params)
|
|
65
65
|
|
|
66
|
-
async def close_pool(self):
|
|
66
|
+
async def close_pool(self) -> None:
|
|
67
67
|
"""
|
|
68
68
|
Connection poolni yopadi.
|
|
69
69
|
"""
|
|
@@ -71,7 +71,7 @@ class AsyncPostgresDB:
|
|
|
71
71
|
await self.pool.close()
|
|
72
72
|
self.pool = None
|
|
73
73
|
|
|
74
|
-
async def create(self, table: str, columns: str):
|
|
74
|
+
async def create(self, table: str, columns: str) -> None:
|
|
75
75
|
"""
|
|
76
76
|
Jadval yaratadi (agar mavjud bo‘lsa o‘tkazib yuboradi).
|
|
77
77
|
|
|
@@ -79,26 +79,34 @@ class AsyncPostgresDB:
|
|
|
79
79
|
table (str): Jadval nomi.
|
|
80
80
|
columns (str): Ustunlar va turlari, misol: "id SERIAL PRIMARY KEY, name TEXT".
|
|
81
81
|
"""
|
|
82
|
-
await self.
|
|
82
|
+
await self._manager(f"CREATE TABLE IF NOT EXISTS {table} ({columns})", commit=True)
|
|
83
83
|
|
|
84
|
-
async def drop(self, table: str):
|
|
84
|
+
async def drop(self, table: str, cascade: bool = False) -> None:
|
|
85
85
|
"""
|
|
86
86
|
Jadvalni o‘chiradi (agar mavjud bo‘lsa).
|
|
87
87
|
|
|
88
88
|
Args:
|
|
89
89
|
table (str): Jadval nomi.
|
|
90
|
+
cascade (bool): Agar True bo‘lsa, jadval bilan bog‘liq barcha obyektlar ham o‘chiradi. Default: True
|
|
90
91
|
"""
|
|
91
|
-
|
|
92
|
+
sql = f"DROP TABLE IF EXISTS {table}"
|
|
93
|
+
if cascade:
|
|
94
|
+
sql += " CASCADE"
|
|
95
|
+
|
|
96
|
+
await self._manager(sql, commit=True)
|
|
92
97
|
|
|
93
98
|
async def select(
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
99
|
+
self,
|
|
100
|
+
table: str,
|
|
101
|
+
columns: str = "*",
|
|
102
|
+
where: Optional[List[Any]] = None,
|
|
103
|
+
join: Optional[List[tuple]] = None,
|
|
104
|
+
group_by: Optional[str] = None,
|
|
105
|
+
order_by: Optional[str] = None,
|
|
106
|
+
limit: Optional[int] = None,
|
|
107
|
+
offset: Optional[int] = None,
|
|
108
|
+
fetchone: bool = False
|
|
109
|
+
) -> Any:
|
|
102
110
|
"""
|
|
103
111
|
Jadvaldan ma'lumotlarni tanlash.
|
|
104
112
|
|
|
@@ -110,6 +118,7 @@ class AsyncPostgresDB:
|
|
|
110
118
|
group_by (Optional[str]): GROUP BY ustuni.
|
|
111
119
|
order_by (Optional[str]): ORDER BY qoidasi.
|
|
112
120
|
limit (Optional[int]): LIMIT qiymati.
|
|
121
|
+
offset (Optional[int]): OFFSET qiymati.
|
|
113
122
|
fetchone (bool): True bo‘lsa faqat bitta qator qaytaradi.
|
|
114
123
|
|
|
115
124
|
Returns:
|
|
@@ -137,9 +146,13 @@ class AsyncPostgresDB:
|
|
|
137
146
|
sql += f" LIMIT $%d" % (len(params) + 1)
|
|
138
147
|
params.append(limit)
|
|
139
148
|
|
|
140
|
-
|
|
149
|
+
if offset is not None:
|
|
150
|
+
sql += " OFFSET $%d" % (len(params) + 1)
|
|
151
|
+
params.append(offset)
|
|
152
|
+
|
|
153
|
+
return await self._manager(sql, params, fetchone=fetchone, fetchall=not fetchone)
|
|
141
154
|
|
|
142
|
-
async def insert(self, table: str, columns: str, values: List[Any]):
|
|
155
|
+
async def insert(self, table: str, columns: str, values: List[Any]) -> None:
|
|
143
156
|
"""
|
|
144
157
|
Jadvalga yangi qator qo‘shadi.
|
|
145
158
|
|
|
@@ -148,11 +161,52 @@ class AsyncPostgresDB:
|
|
|
148
161
|
columns (str): Ustunlar nomi, misol: "name, age".
|
|
149
162
|
values (List[Any]): Qiymatlar, misol: ["Ali", 25].
|
|
150
163
|
"""
|
|
151
|
-
placeholders = ", ".join(f"${i+1}" for i in range(len(values)))
|
|
164
|
+
placeholders = ", ".join(f"${i + 1}" for i in range(len(values)))
|
|
152
165
|
sql = f"INSERT INTO {table} ({columns}) VALUES ({placeholders})"
|
|
153
|
-
await self.
|
|
166
|
+
await self._manager(sql, *values, commit=True)
|
|
167
|
+
|
|
168
|
+
async def insert_many(self, table: str, columns: str, values_list: List[List[Any]]) -> None:
|
|
169
|
+
"""
|
|
170
|
+
Jadvalga bir nechta qator qo‘shish (bulk insert).
|
|
171
|
+
|
|
172
|
+
Args:
|
|
173
|
+
table (str): Jadval nomi.
|
|
174
|
+
columns (str): Ustunlar nomi, misol: "name, age".
|
|
175
|
+
values_list (List[List[Any]]): Qiymatlar ro‘yxati, misol: [["Ali", 25], ["Vali", 30]].
|
|
176
|
+
|
|
177
|
+
Raises:
|
|
178
|
+
ValueError: Agar `values_list` bo‘sh bo‘lsa.
|
|
179
|
+
|
|
180
|
+
Notes:
|
|
181
|
+
- `asyncpg` kutubxonasida har bir parametr uchun unikal `$n` indekslari kerak,
|
|
182
|
+
shuning uchun har bir qator uchun ketma-ket `$1, $2, ...` ishlatiladi.
|
|
183
|
+
- Metod barcha qatorlarni bir so‘rovda qo‘shadi.
|
|
184
|
+
- `fetchmany` kerak emas, chunki bu insert operatsiyasi natija qaytarmaydi.
|
|
185
|
+
|
|
186
|
+
Example:
|
|
187
|
+
await db.insert_many(
|
|
188
|
+
"users",
|
|
189
|
+
"name, age",
|
|
190
|
+
[["Ali", 25], ["Vali", 30], ["Guli", 22]]
|
|
191
|
+
)
|
|
192
|
+
"""
|
|
193
|
+
if not values_list:
|
|
194
|
+
raise ValueError("values_list bo'sh bo'lishi mumkin emas")
|
|
195
|
+
|
|
196
|
+
placeholders_list = []
|
|
197
|
+
flat_values = []
|
|
198
|
+
counter = 1
|
|
199
|
+
|
|
200
|
+
for values in values_list:
|
|
201
|
+
placeholders = ", ".join(f"${i}" for i in range(counter, counter + len(values)))
|
|
202
|
+
placeholders_list.append(f"({placeholders})")
|
|
203
|
+
flat_values.extend(values)
|
|
204
|
+
counter += len(values)
|
|
154
205
|
|
|
155
|
-
|
|
206
|
+
sql = f"INSERT INTO {table} ({columns}) VALUES {', '.join(placeholders_list)}"
|
|
207
|
+
await self._manager(sql, *flat_values, commit=True)
|
|
208
|
+
|
|
209
|
+
async def update(self, table: str, set_column: str, set_value: Any, where_column: str, where_value: Any) -> None:
|
|
156
210
|
"""
|
|
157
211
|
Jadvaldagi qatorni yangilaydi.
|
|
158
212
|
|
|
@@ -164,9 +218,9 @@ class AsyncPostgresDB:
|
|
|
164
218
|
where_value (Any): Filtrlash qiymati.
|
|
165
219
|
"""
|
|
166
220
|
sql = f"UPDATE {table} SET {set_column} = $1 WHERE {where_column} = $2"
|
|
167
|
-
await self.
|
|
221
|
+
await self._manager(sql, set_value, where_value, commit=True)
|
|
168
222
|
|
|
169
|
-
async def delete(self, table: str, where_column: str, where_value: Any):
|
|
223
|
+
async def delete(self, table: str, where_column: str, where_value: Any) -> None:
|
|
170
224
|
"""
|
|
171
225
|
Jadvaldan qator o‘chiradi.
|
|
172
226
|
|
|
@@ -176,7 +230,7 @@ class AsyncPostgresDB:
|
|
|
176
230
|
where_value (Any): Filtrlash qiymati.
|
|
177
231
|
"""
|
|
178
232
|
sql = f"DELETE FROM {table} WHERE {where_column} = $1"
|
|
179
|
-
await self.
|
|
233
|
+
await self._manager(sql, where_value, commit=True)
|
|
180
234
|
|
|
181
235
|
async def list_tables(self, schema: str = "public") -> List[str]:
|
|
182
236
|
"""
|
|
@@ -189,15 +243,15 @@ class AsyncPostgresDB:
|
|
|
189
243
|
List[str]: Jadval nomlari
|
|
190
244
|
"""
|
|
191
245
|
sql = """
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
result = await self.
|
|
246
|
+
SELECT table_name
|
|
247
|
+
FROM information_schema.tables
|
|
248
|
+
WHERE table_schema = $1
|
|
249
|
+
ORDER BY table_name;
|
|
250
|
+
"""
|
|
251
|
+
result = await self._manager(sql, schema, fetchall=True)
|
|
198
252
|
return [r["table_name"] for r in result]
|
|
199
253
|
|
|
200
|
-
async def describe_table(self, table: str, schema: str = "public"):
|
|
254
|
+
async def describe_table(self, table: str, schema: str = "public") -> List[str]:
|
|
201
255
|
"""
|
|
202
256
|
Jadval ustunlari haqida ma'lumot beradi.
|
|
203
257
|
|
|
@@ -209,14 +263,15 @@ class AsyncPostgresDB:
|
|
|
209
263
|
list[asyncpg.Record]: Har bir ustun bo‘yicha ma’lumot
|
|
210
264
|
"""
|
|
211
265
|
sql = """
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
266
|
+
SELECT column_name, data_type, is_nullable, column_default
|
|
267
|
+
FROM information_schema.columns
|
|
268
|
+
WHERE table_schema = $1
|
|
269
|
+
AND table_name = $2
|
|
270
|
+
ORDER BY ordinal_position; \
|
|
271
|
+
"""
|
|
272
|
+
return await self._manager(sql, schema, table, fetchall=True)
|
|
273
|
+
|
|
274
|
+
async def alter(self, table: str, action: str) -> None:
|
|
220
275
|
"""
|
|
221
276
|
Jadval strukturasi o‘zgartirish (ALTER TABLE).
|
|
222
277
|
|
|
@@ -225,4 +280,4 @@ class AsyncPostgresDB:
|
|
|
225
280
|
action (str): ALTER TABLE dan keyingi SQL qismi, misol: "ADD COLUMN age INT".
|
|
226
281
|
"""
|
|
227
282
|
sql = f"ALTER TABLE {table} {action}"
|
|
228
|
-
await self.
|
|
283
|
+
await self._manager(sql, commit=True)
|