picopyn.connection
1import asyncpg 2 3 4class Connection: 5 """ 6 A representation of a database session. 7 8 :param dsn (str): The data source name (e.g., "postgresql://user:pass@host:port") for the picodata node. 9 """ 10 def __init__(self, dsn): 11 self.dsn = dsn 12 self.conn = None 13 14 @property 15 def dsn(self) -> str: 16 """Data Source Name for the connection (read-only).""" 17 return self._dsn 18 19 @property 20 def is_connected(self) -> bool: 21 """Check if the connection is active.""" 22 return self._conn is not None 23 24 async def connect(self): 25 """ 26 Create new connection to Picodata 27 """ 28 self.conn = await asyncpg.connect(self.dsn) 29 30 async def execute(self, *args, **kwargs): 31 """ 32 Execute an SQL command 33 """ 34 35 if not self.conn: 36 raise OSError('No active connection. Try to call .connect() before.') 37 38 return await self.conn.execute(*args, **kwargs) 39 40 async def fetchrow(self, *args, **kwargs): 41 """ 42 Run a query and return the first row. 43 """ 44 45 if not self.conn: 46 raise OSError('No active connection. Try to call .connect() before') 47 48 return await self.conn.fetchrow(*args, **kwargs) 49 50 async def fetch(self, *args, **kwargs): 51 """ 52 Run a query and return the results as a list. 53 """ 54 55 if not self.conn: 56 raise OSError('No active connection. Try to call .connect() before') 57 58 return await self.conn.fetch(*args, **kwargs) 59 60 async def close(self, *args, **kwargs): 61 """ 62 Close the connection gracefully. 63 """ 64 if self.conn: 65 return await self.conn.close(*args, **kwargs)
class
Connection:
5class Connection: 6 """ 7 A representation of a database session. 8 9 :param dsn (str): The data source name (e.g., "postgresql://user:pass@host:port") for the picodata node. 10 """ 11 def __init__(self, dsn): 12 self.dsn = dsn 13 self.conn = None 14 15 @property 16 def dsn(self) -> str: 17 """Data Source Name for the connection (read-only).""" 18 return self._dsn 19 20 @property 21 def is_connected(self) -> bool: 22 """Check if the connection is active.""" 23 return self._conn is not None 24 25 async def connect(self): 26 """ 27 Create new connection to Picodata 28 """ 29 self.conn = await asyncpg.connect(self.dsn) 30 31 async def execute(self, *args, **kwargs): 32 """ 33 Execute an SQL command 34 """ 35 36 if not self.conn: 37 raise OSError('No active connection. Try to call .connect() before.') 38 39 return await self.conn.execute(*args, **kwargs) 40 41 async def fetchrow(self, *args, **kwargs): 42 """ 43 Run a query and return the first row. 44 """ 45 46 if not self.conn: 47 raise OSError('No active connection. Try to call .connect() before') 48 49 return await self.conn.fetchrow(*args, **kwargs) 50 51 async def fetch(self, *args, **kwargs): 52 """ 53 Run a query and return the results as a list. 54 """ 55 56 if not self.conn: 57 raise OSError('No active connection. Try to call .connect() before') 58 59 return await self.conn.fetch(*args, **kwargs) 60 61 async def close(self, *args, **kwargs): 62 """ 63 Close the connection gracefully. 64 """ 65 if self.conn: 66 return await self.conn.close(*args, **kwargs)
A representation of a database session.
Parameters
- dsn (str): The data source name (e.g., "postgresql://user:pass@host: port") for the picodata node.
dsn: str
15 @property 16 def dsn(self) -> str: 17 """Data Source Name for the connection (read-only).""" 18 return self._dsn
Data Source Name for the connection (read-only).
is_connected: bool
20 @property 21 def is_connected(self) -> bool: 22 """Check if the connection is active.""" 23 return self._conn is not None
Check if the connection is active.
async def
connect(self):
25 async def connect(self): 26 """ 27 Create new connection to Picodata 28 """ 29 self.conn = await asyncpg.connect(self.dsn)
Create new connection to Picodata
async def
execute(self, *args, **kwargs):
31 async def execute(self, *args, **kwargs): 32 """ 33 Execute an SQL command 34 """ 35 36 if not self.conn: 37 raise OSError('No active connection. Try to call .connect() before.') 38 39 return await self.conn.execute(*args, **kwargs)
Execute an SQL command
async def
fetchrow(self, *args, **kwargs):
41 async def fetchrow(self, *args, **kwargs): 42 """ 43 Run a query and return the first row. 44 """ 45 46 if not self.conn: 47 raise OSError('No active connection. Try to call .connect() before') 48 49 return await self.conn.fetchrow(*args, **kwargs)
Run a query and return the first row.
async def
fetch(self, *args, **kwargs):
51 async def fetch(self, *args, **kwargs): 52 """ 53 Run a query and return the results as a list. 54 """ 55 56 if not self.conn: 57 raise OSError('No active connection. Try to call .connect() before') 58 59 return await self.conn.fetch(*args, **kwargs)
Run a query and return the results as a list.