Sync
picopyn.synchronous
Sync API based on psycopg for connection management and pools.
connect = Connection.connect
module-attribute
Connection
A representation of a database session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dsn
|
The data source name (e.g., "postgresql://user:pass@host:port" or "postgresql://user:pass@host1:port1,host2:port2") for the picodata node. |
required | |
**connect_kwargs
|
Additional keyword arguments to pass to psycopg.connect().
SSL can be configured either via DSN query params or via kwargs such as
|
required |
Note
SSL should be configured using the appropriate parameters in a single source: either via DSN or by providing libpq SSL parameters in kwargs.
See Also
DB-API 2.0 Connection Objects specification.
Examples:
Simple usage:
conn = Connection.connect("postgresql://admin:pass@localhost:5432")
cur = conn.cursor()
cur.execute("SELECT 1")
conn.close()
Multi-host DSN string:
conn = Connection.connect("postgresql://admin:pass@host1:5432,host2:5432")
SSL (kwargs-based):
conn = Connection.connect(
"postgresql://admin:pass@localhost:5432",
sslmode="verify-ca",
sslrootcert="/path/to/ca.crt",
sslcert="/path/to/client.crt",
sslkey="/path/to/client.key",
)
SSL (DSN query params):
conn = Connection.connect(
"postgresql://admin:pass@localhost:5432/db"
"?sslmode=verify-ca&sslrootcert=/path/to/ca.crt"
"&sslcert=/path/to/client.crt&sslkey=/path/to/client.key"
)
Source code in picopyn/synchronous/connection.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
close()
Close the connection gracefully.
Source code in picopyn/synchronous/connection.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
commit()
No-op for SQLAlchemy compatibility.
DB-API defines this method as committing any pending transaction.
Source code in picopyn/synchronous/connection.py
154 155 156 157 158 159 160 | |
connect(dsn, **connect_kwargs)
classmethod
Create and return a new connection to Picodata.
Source code in picopyn/synchronous/connection.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
cursor()
Return a new cursor object using the connection, which is used to manage the context of a fetch operation.
Returns:
| Type | Description |
|---|---|
Cursor
|
A new Cursor bound to this connection. |
See Cursor for full documentation.
Source code in picopyn/synchronous/connection.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
rollback()
No-op for SQLAlchemy compatibility.
DB-API defines this method as rolling back to the start of any pending transaction.
Source code in picopyn/synchronous/connection.py
162 163 164 165 166 167 168 | |
Cursor
DB-API cursor wrapper.
Supports query execution, cursor metadata and fetch methods.
See Also
DB-API 2.0 Cursor Objects specification.
Source code in picopyn/synchronous/cursor.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
arraysize
property
writable
Number of rows to fetch at a time when fetchmany() is called without size.
Returns:
| Type | Description |
|---|---|
int
|
The current arraysize. |
description
property
Return DB-API column metadata for the current result set.
Returns:
| Type | Description |
|---|---|
Description | None
|
A tuple of per-column metadata (name, type_code, display_size, internal_size, precision, scale, null_ok), or None if there is no result set. |
Examples:
cursor.execute('SELECT * FROM "warehouse"')
[col[0] for col in cursor.description]
# ['id', 'item']
rowcount
property
Return the number of rows produced or affected by the last operation.
Returns:
| Type | Description |
|---|---|
int
|
The row count, or -1 if it cannot be determined. |
statusmessage
property
Return the command status from the last operation.
Returns:
| Type | Description |
|---|---|
str | None
|
The status message reported by the database, or None if no command |
str | None
|
status is available. |
Examples:
cursor.execute('INSERT INTO "warehouse" VALUES (%s, %s)', (1, "test"))
cursor.statusmessage
# 'INSERT 0 1'
__iter__()
Iterate over the current result set.
Returns:
| Type | Description |
|---|---|
Iterator[tuple[object, ...]]
|
An iterator over the remaining rows. |
Examples:
cursor.execute("SELECT id, name FROM users")
for row in cursor:
print(row)
Source code in picopyn/synchronous/cursor.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
close()
Close the cursor.
Source code in picopyn/synchronous/cursor.py
256 257 258 259 260 | |
execute(operation, parameters=None)
Prepare and execute a database operation (query or command).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
QueryNoTemplate
|
database operation to be prepared. |
required |
parameters
|
Params | None
|
sequence or mapping that will be bound to variables in the operation. |
None
|
Returns:
| Type | Description |
|---|---|
Cursor
|
This cursor, to allow chaining (e.g. |
Examples:
DDL example:
ddl = 'CREATE TABLE "warehouse" (id INTEGER NOT NULL, item TEXT NOT NULL, PRIMARY KEY (id)) USING memtx DISTRIBUTED BY (id);'
cursor.execute(ddl)
DML example:
cursor.execute('INSERT INTO "warehouse" VALUES (%s, %s)', (1, "test"))
Source code in picopyn/synchronous/cursor.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
executemany(operation, seq_of_parameters)
Prepare a database operation and execute it against all parameter sequences or mappings found in the sequence seq_of_parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
QueryNoTemplate
|
database operation to be prepared. |
required |
seq_of_parameters
|
Iterable[Params]
|
all parameter sequences or mappings that will be bound to variables in the operation. |
required |
Examples:
cursor.executemany(
'INSERT INTO "warehouse" VALUES (%s, %s)',
[(1, "test1"), (2, "test2")],
)
Source code in picopyn/synchronous/cursor.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
fetchall()
Fetch all remaining rows from the current result set.
Returns:
| Type | Description |
|---|---|
list[tuple[object, ...]]
|
A list of the remaining rows. Empty if no more rows are available. |
Examples:
cursor.execute('SELECT * FROM "warehouse"')
rows = cursor.fetchall()
# [(1, 'test')]
Source code in picopyn/synchronous/cursor.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
fetchmany(size=None)
Fetch the next set of rows from the current result set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int | None
|
Maximum number of rows to fetch. If omitted, |
None
|
Returns:
| Type | Description |
|---|---|
list[tuple[object, ...]]
|
A list of the fetched rows. Empty if no more rows are available. |
Examples:
Use the previously configured arraysize:
cursor.arraysize = 100
rows = cursor.fetchmany()
Override the batch size for a single call:
rows = cursor.fetchmany(50)
Source code in picopyn/synchronous/cursor.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
fetchone()
Fetch the next row from the current result set.
Returns:
| Type | Description |
|---|---|
tuple[object, ...] | None
|
The next row, or None if no more rows are available. |
Examples:
cursor.execute('SELECT * FROM "warehouse"')
row = cursor.fetchone()
# (1, 'test')
Source code in picopyn/synchronous/cursor.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
Errors
Warning
Bases: Exception
Exception raised for important warnings.
Source code in picopyn/synchronous/errors.py
6 7 | |
Error
Bases: Exception
Base class for all synchronous DB-API errors.
Source code in picopyn/synchronous/errors.py
10 11 | |
InterfaceError
Bases: Error
Error related to the database interface rather than the database itself.
Source code in picopyn/synchronous/errors.py
14 15 | |
DatabaseError
Bases: Error
Error related to the database.
Source code in picopyn/synchronous/errors.py
18 19 | |
DataError
Bases: DatabaseError
Error due to problems with processed data.
Source code in picopyn/synchronous/errors.py
22 23 | |
OperationalError
Bases: DatabaseError
Error related to database operation.
Source code in picopyn/synchronous/errors.py
26 27 | |
IntegrityError
Bases: DatabaseError
Error related to relational integrity.
Source code in picopyn/synchronous/errors.py
30 31 | |
InternalError
Bases: DatabaseError
Internal database error.
Source code in picopyn/synchronous/errors.py
34 35 | |
ProgrammingError
Bases: DatabaseError
Programming error, such as invalid SQL or invalid API usage.
Source code in picopyn/synchronous/errors.py
38 39 | |
NotSupportedError
Bases: DatabaseError
Error raised when an unsupported DB-API feature is requested.
Source code in picopyn/synchronous/errors.py
42 43 | |