plpipes.database.driver.transaction#
Transaction
#
The Transaction class represents a database transaction.
Attributes:
| Name | Type | Description |
|---|---|---|
driver |
object
|
The database driver object. |
conn |
object
|
The database connection object. |
Source code in src\plpipes\database\driver\transaction.py
1 2 3 4 5 6 7 8 9 10 11 12 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 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 | |
__init__(driver, conn)
#
Creates a new transaction object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
driver
|
object
|
The database driver object. |
required |
conn
|
object
|
The database connection object. |
required |
Note
Transaction objects should not be created calling the class
constructor directly but through Driver begin method.
Source code in src\plpipes\database\driver\transaction.py
connection()
#
Returns the database connection object associated with this transaction.
Returns:
| Name | Type | Description |
|---|---|---|
object |
The database connection object. |
copy_table(from_table_name, to_table_name, if_exists='replace', **kws)
#
Copies the contents of one table to another.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_table_name
|
str
|
The name of the table to copy from. |
required |
to_table_name
|
str
|
The name of the table to copy to. |
required |
if_exists
|
str
|
How to handle the destination table if it already exists. Valid options are "fail", "replace", and "append". |
'replace'
|
**kws
|
Additional keyword arguments to pass to the driver. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the source and destination table names are the same. |
Returns:
| Name | Type | Description |
|---|---|---|
int |
The number of rows copied. |
Source code in src\plpipes\database\driver\transaction.py
create_table(table_name, sql_or_df, parameters=None, if_exists='replace', **kws)
#
Creates a new table in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
The name of the table to create. |
required |
sql_or_df
|
str or DataFrame
|
The SQL statement or DataFrame defining the table schema. |
required |
parameters
|
dict
|
A dictionary containing values to fill in SQL statement placeholders. |
None
|
if_exists
|
str
|
How to handle the table if it already exists. Valid options are "fail", "replace", and "append". |
'replace'
|
**kws
|
Additional keyword arguments to pass to the driver. |
{}
|
Source code in src\plpipes\database\driver\transaction.py
create_view(view_name, sql, parameters=None, if_exists='replace', **kws)
#
Creates a new view in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view_name
|
str
|
The name of the view to create. |
required |
sql
|
str
|
The SQL statement defining the view. |
required |
parameters
|
dict
|
A dictionary containing values to fill in SQL statement placeholders. |
None
|
if_exists
|
str
|
How to handle the view if it already exists. Valid options are "fail", "replace", and "append". |
'replace'
|
**kws
|
Additional keyword arguments to pass to the driver. |
{}
|
Source code in src\plpipes\database\driver\transaction.py
db_name()
#
driver()
#
Returns the database driver object associated with this transaction.
Returns:
| Name | Type | Description |
|---|---|---|
object |
The database driver object. |
drop_table(table_name, only_if_exists=True)
#
Drops a table from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
The name of the table to drop. |
required |
only_if_exists
|
bool
|
If True, the table is only dropped if it exists. Otherwise, an error is raised if the table does not exist. |
True
|
Source code in src\plpipes\database\driver\transaction.py
execute(sql, parameters=None)
#
Executes an SQL statement with optional parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The SQL statement to execute. |
required |
parameters
|
dict
|
A dictionary containing values to fill in SQL statement placeholders. |
None
|
Source code in src\plpipes\database\driver\transaction.py
execute_script(sql_script)
#
Executes a script containing multiple SQL statements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql_script
|
str
|
The SQL script to execute. |
required |
list_tables()
#
Lists the tables in the database.
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
DataFrame with the list of tables. |
list_views()
#
Lists the views in the database.
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
DataFrame with the list of views. |
query(sql, parameters=None, backend=None, **kws)
#
Executes an SQL query and returns the result as a DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The SQL query to execute. |
required |
parameters
|
dict
|
A dictionary containing values to fill in SQL statement placeholders. |
None
|
backend
|
optional
|
The backend to use for executing the query. If None, the default backend is used. |
None
|
**kws
|
Additional keyword arguments to pass to the driver. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
A DataFrame containing the query result. |
Source code in src\plpipes\database\driver\transaction.py
query_chunked(sql, parameters=None, backend=None, **kws)
#
Executes an SQL query and returns the result as an iterator over chunks of rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The SQL query to execute. |
required |
parameters
|
dict
|
A dictionary containing values to fill in SQL statement placeholders. |
None
|
backend
|
optional
|
The backend to use for executing the query. If None, the default backend is used. |
None
|
**kws
|
Additional keyword arguments to pass to the driver. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
An iterator over chunks of rows. |
Source code in src\plpipes\database\driver\transaction.py
query_first(sql, parameters=None, backend=None, **kws)
#
Executes an SQL query and returns the first row of the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The SQL query to execute. |
required |
parameters
|
dict
|
A dictionary containing values to fill in SQL statement placeholders. |
None
|
backend
|
optional
|
The backend to use for executing the query. If None, the default backend is used. |
None
|
**kws
|
Additional keyword arguments to pass to the driver. |
{}
|
Returns:
| Type | Description |
|---|---|
|
DataFrame or dict: A dataframe/dictionary containing the result first row. |
Source code in src\plpipes\database\driver\transaction.py
query_first_value(sql, parameters=None, backend='tuple', **kws)
#
Executes an SQL query and returns the first value from the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The SQL query to execute. |
required |
parameters
|
dict
|
A dictionary containing values to fill in SQL statement placeholders. |
None
|
backend
|
str
|
The backend to use for executing the query. If None, the default backend is used. Defaults to |
'tuple'
|
**kws
|
Additional keyword arguments to pass to the driver. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
The first value from the result (first row, first column). |
Source code in src\plpipes\database\driver\transaction.py
query_group(sql, parameters=None, by=None, backend=None, **kws)
#
Executes an SQL query and returns the result as a DataFrame grouped by one or more columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The SQL query to execute. |
required |
parameters
|
dict
|
A dictionary containing values to fill in SQL statement placeholders. |
None
|
by
|
optional
|
The column(s) to group by. |
None
|
backend
|
optional
|
The backend to use for executing the query. If None, the default backend is used. |
None
|
**kws
|
Additional keyword arguments to pass to the driver. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
A DataFrame containing the grouped query result. |
Source code in src\plpipes\database\driver\transaction.py
read_table(table_name, backend=None, **kws)
#
Reads a table from the database into a DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
The name of the table to read. |
required |
backend
|
optional
|
The backend to use for reading the table. If None, the default backend for the driver is used. |
None
|
**kws
|
Additional keyword arguments to pass to the backend. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
A DataFrame containing the table data. |
Source code in src\plpipes\database\driver\transaction.py
read_table_chunked(table_name, backend=None, **kws)
#
Reads a table from the database in chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
The name of the table to read. |
required |
backend
|
optional
|
The backend to use for reading the table. If None, the default backend for the driver is used. |
None
|
**kws
|
Additional keyword arguments to pass to the backend. |
{}
|
Source code in src\plpipes\database\driver\transaction.py
table_exists_p(table_name)
#
Checks whether a table exists in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
The name of the table to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if the table exists, False otherwise. |