plpipes.database#
begin(db=None)
#
Begin a transaction on the specified database instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
str
|
The name of the database instance to begin a transaction. Defaults to "work". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Transaction |
A transaction object for the database. |
Source code in src\plpipes\database\__init__.py
copy_table(from_table_name, to_table_name=None, from_db=None, to_db=None, db=None, if_exists='replace', **kws)
#
Copy a table from one database instance to another.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_table_name
|
str
|
The name of the source table to copy. |
required |
to_table_name
|
str
|
The name of the destination table. Defaults to the source table name. |
None
|
from_db
|
str
|
The source database instance to copy from. |
None
|
to_db
|
str
|
The destination database instance to copy to. |
None
|
db
|
str
|
The current database instance. |
None
|
if_exists
|
str
|
What to do if the destination table already exists. Defaults to "replace". |
'replace'
|
**kws
|
Additional keyword arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in src\plpipes\database\__init__.py
create_table(table_name, sql_or_df, parameters=None, db=None, if_exists='replace', **kws)
#
Create a new table in the database from a DataFrame, SQL command, or SQLAlchemy select object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
The name of the table to create. |
required |
sql_or_df
|
DataFrame, str, or SQLAlchemy select object
|
The DataFrame, SQL command, or SQLAlchemy select object for creating the table. |
required |
parameters
|
dict
|
The parameters for creating the table. |
None
|
db
|
str
|
The database instance to use. |
None
|
if_exists
|
str
|
What to do if the table already exists. Defaults to "replace". |
'replace'
|
**kws
|
Additional keyword arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in src\plpipes\database\__init__.py
create_view(view_name, sql, parameters=None, db=None, if_exists='replace', **kws)
#
Create 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 command for creating the view. |
required |
parameters
|
dict
|
The parameters for creating the view. |
None
|
db
|
str
|
The database instance to use. |
None
|
if_exists
|
str
|
What to do if the view already exists. Defaults to "replace". |
'replace'
|
**kws
|
Additional keyword arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in src\plpipes\database\__init__.py
drop_table(table_name, db=None, only_if_exists=False)
#
Drop a table from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
The name of the table to drop. |
required |
db
|
str
|
The database instance to use. |
None
|
only_if_exists
|
bool
|
If True, do not raise an error if the table does not exist. |
False
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in src\plpipes\database\__init__.py
engine(db=None)
#
Retrieve the database engine for the specified database instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
str
|
The name of the database instance to retrieve the engine for. Defaults to "work". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
engine |
The database engine object. |
Source code in src\plpipes\database\__init__.py
execute(sql, parameters=None, db=None)
#
Execute a SQL command that does not return a result set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The SQL command to execute. |
required |
parameters
|
dict
|
The parameters for the SQL command. |
None
|
db
|
str
|
The database instance to use. |
None
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in src\plpipes\database\__init__.py
execute_script(sql_script, db=None)
#
Execute a sequence of SQL commands.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql_script
|
str
|
The SQL script to execute. |
required |
db
|
str
|
The database instance to use. |
None
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in src\plpipes\database\__init__.py
list_tables(db=None)
#
List all tables in the specified database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
str
|
The database instance to use. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
A list of table names in the database. |
Source code in src\plpipes\database\__init__.py
list_views(db=None)
#
List all views in the specified database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
str
|
The database instance to use. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
A list of view names in the database. |
Source code in src\plpipes\database\__init__.py
load_backend(name, db=None)
#
Load a specific backend into the database driver.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the backend to load. |
required |
db
|
str
|
The database instance to load the backend into. Defaults to "work". |
None
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in src\plpipes\database\__init__.py
lookup(db=None)
#
Lookup the database driver instance for the specified database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
str
|
The name of the database instance to look up. Defaults to "work". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
driver |
The database driver instance. |
Source code in src\plpipes\database\__init__.py
query(sql, parameters=None, db=None, backend=None, **kws)
#
Execute a SQL query and return the results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The SQL query to execute. |
required |
parameters
|
dict
|
The parameters for the SQL query. |
None
|
db
|
str
|
The database instance to use. |
None
|
backend
|
str
|
The backend to use. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
The results of the query as a DataFrame. |
Source code in src\plpipes\database\__init__.py
query_chunked(sql, parameters=None, db=None, backend=None, **kws)
#
Execute a SQL query and yield results in chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The SQL query to execute. |
required |
parameters
|
dict
|
The parameters for the SQL query. |
None
|
db
|
str
|
The database instance to use. |
None
|
backend
|
str
|
The backend to use. |
None
|
**kws
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
DataFrame |
Each chunk of the results as a DataFrame. |
Source code in src\plpipes\database\__init__.py
query_first(sql, parameters=None, db=None, backend=None, **kws)
#
Execute a SQL query and return the first result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The SQL query to execute. |
required |
parameters
|
dict
|
The parameters for the SQL query. |
None
|
db
|
str
|
The database instance to use. |
None
|
backend
|
str
|
The backend to use. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
object |
The first result of the query. |
Source code in src\plpipes\database\__init__.py
query_first_value(sql, parameters=None, db=None, backend='tuple', **kws)
#
Execute a SQL query and return the first value from the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The SQL query to execute. |
required |
parameters
|
dict
|
The parameters for the SQL query. |
None
|
db
|
str
|
The database instance to use. |
None
|
backend
|
str
|
The backend to use. Defaults to "tuple". |
'tuple'
|
Returns:
| Name | Type | Description |
|---|---|---|
object |
The first value from the query result. |
Source code in src\plpipes\database\__init__.py
query_group(sql, parameters=None, db=None, by=None, backend=None, **kws)
#
Execute a SQL query and yield results grouped by specified criteria.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The SQL query to execute. |
required |
parameters
|
dict
|
The parameters for the SQL query. |
None
|
db
|
str
|
The database instance to use. |
None
|
by
|
str
|
The column or columns to group by. |
None
|
backend
|
str
|
The backend to use. |
None
|
**kws
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
DataFrame |
Each group of results as a DataFrame. |
Source code in src\plpipes\database\__init__.py
read_table(table_name, db=None, backend=None, **kws)
#
Read the contents of a table and return it as a data frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
The name of the table to read. |
required |
db
|
str
|
The database instance to use. |
None
|
backend
|
str
|
The backend to use. |
None
|
**kws
|
Additional keyword arguments. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
The contents of the table as a DataFrame. |
Source code in src\plpipes\database\__init__.py
table_exists_p(table_name, db=None)
#
Check if a table exists in the specified database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
The name of the table to check. |
required |
db
|
str
|
The database instance to use. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if the table exists, False otherwise. |
Source code in src\plpipes\database\__init__.py
update_table(from_table_name, to_table_name=None, from_db=None, to_db=None, db=None, key=None, key_dir='>=', **kws)
#
Update a table in the destination database from the source table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_table_name
|
str
|
The name of the source table to update from. |
required |
to_table_name
|
str
|
The name of the destination table to update. Defaults to the source table name. |
None
|
from_db
|
str
|
The source database instance. |
None
|
to_db
|
str
|
The destination database instance. |
None
|
db
|
str
|
The current database instance. |
None
|
key
|
str
|
The key column used to identify new rows. |
None
|
key_dir
|
str
|
The direction for the key comparison. Defaults to ">=". |
'>='
|
**kws
|
Additional keyword arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
|
None |