Skip to content

Commit ec07c18

Browse files
committed
basic connection methods
1 parent d86b30a commit ec07c18

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

ydb_dbapi/connection.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,54 @@
1+
from typing import (
2+
Any,
3+
Optional,
4+
)
5+
6+
import ydb
7+
8+
19
class Connection:
2-
def __init__(self):
3-
pass
10+
def __init__(
11+
self,
12+
host: str = "",
13+
port: str = "",
14+
database: str = "",
15+
**conn_kwargs: Any,
16+
):
17+
self.endpoint = f"grpc://{host}:{port}"
18+
self.database = database
19+
self.conn_kwargs = conn_kwargs
20+
self.credentials = self.conn_kwargs.pop("credentials", None)
21+
self.table_path_prefix = self.conn_kwargs.pop(
22+
"ydb_table_path_prefix", ""
23+
)
24+
25+
self.session_pool: ydb.aio.QuerySessionPool = None
26+
self.session: ydb.aio.QuerySession = None
27+
self.tx_context: Optional[ydb.QueryTxContext] = None
28+
self.tx_mode: ydb.BaseQueryTxMode = ydb.QuerySerializableReadWrite()
429

530
def cursor(self):
631
pass
732

33+
async def begin(self):
34+
self.tx_context = None
35+
self.session = await self.session_pool.acquire()
36+
self.tx_context = self.session.transaction(self.tx_mode)
37+
await self.tx_context.begin()
38+
839
async def commit(self):
9-
pass
40+
if self.tx_context and self.tx_context.tx_id:
41+
await self.tx_context.commit()
42+
await self.session_pool.release(self.session)
43+
self.session = None
44+
self.tx_context = None
1045

1146
async def rollback(self):
12-
pass
47+
if self.tx_context and self.tx_context.tx_id:
48+
await self.tx_context.rollback()
49+
await self.session_pool.release(self.session)
50+
self.session = None
51+
self.tx_context = None
1352

1453
async def close(self):
1554
pass

0 commit comments

Comments
 (0)