Skip to main content

prefect_sqlalchemy.database

Tasks for querying a database with SQLAlchemy

Functions

check_make_url

Classes

SqlAlchemyConnector

Block used to manage authentication with a database using synchronous drivers. Upon instantiating, an engine is created and maintained for the life of the object until the close method is called. It is recommended to use this block as a context manager, which will automatically close the engine and its connections when the context is exited. It is also recommended that this block is loaded and consumed within a single task or flow because if the block is passed across separate tasks and flows, the state of the block’s connection and cursor could be lost. Attributes:
  • connection_info: SQLAlchemy URL to create the engine; either create from components or create from a string.
  • connect_args: The options which will be passed directly to the DBAPI’s connect() method as additional keyword arguments.
  • fetch_size: The number of rows to fetch at a time.
Methods:

block_initialization

Initializes the engine.

close

Closes sync connections and its cursors.

execute

Executes an operation on the database. This method is intended to be used for operations that do not return data, such as INSERT, UPDATE, or DELETE. Unlike the fetch methods, this method will always execute the operation upon calling. Args:
  • operation: The SQL query or other operation to be executed.
  • parameters: The parameters for the operation.
  • **execution_options: Options to pass to Connection.execution_options.
Examples: Create a table and insert one row into it.

execute_many

Executes many operations on the database. This method is intended to be used for operations that do not return data, such as INSERT, UPDATE, or DELETE. Unlike the fetch methods, this method will always execute the operation upon calling. Args:
  • operation: The SQL query or other operation to be executed.
  • seq_of_parameters: The sequence of parameters for the operation.
  • **execution_options: Options to pass to Connection.execution_options.
Examples: Create a table and insert two rows into it.

fetch_all

Fetch all results from the database. Repeated calls using the same inputs to any of the fetch methods of this block will skip executing the operation again, and instead, return the next set of results from the previous execution, until the reset_connections method is called. Args:
  • operation: The SQL query or other operation to be executed.
  • parameters: The parameters for the operation.
  • **execution_options: Options to pass to Connection.execution_options.
Returns:
  • A list of tuples containing the data returned by the database, where each row is a tuple and each column is a value in the tuple.
Examples: Create a table, insert three rows into it, and fetch all where name is ‘Me’.

fetch_many

Fetch a limited number of results from the database. Repeated calls using the same inputs to any of the fetch methods of this block will skip executing the operation again, and instead, return the next set of results from the previous execution, until the reset_connections method is called. Args:
  • operation: The SQL query or other operation to be executed.
  • parameters: The parameters for the operation.
  • size: The number of results to return; if None or 0, uses the value of fetch_size configured on the block.
  • **execution_options: Options to pass to Connection.execution_options.
Returns:
  • A list of tuples containing the data returned by the database, where each row is a tuple and each column is a value in the tuple.
Examples: Create a table, insert three rows into it, and fetch two rows repeatedly.

fetch_one

Fetch a single result from the database. Repeated calls using the same inputs to any of the fetch methods of this block will skip executing the operation again, and instead, return the next set of results from the previous execution, until the reset_connections method is called. Args:
  • operation: The SQL query or other operation to be executed.
  • parameters: The parameters for the operation.
  • **execution_options: Options to pass to Connection.execution_options.
Returns:
  • A tuple containing the data returned by the database, where each column is a value in the tuple.
Examples: Create a table, insert three rows into it, and fetch a row repeatedly.

get_client

Returns either an engine or connection that can be used to query from databases. Args:
  • client_type: Select from either ‘engine’ or ‘connection’.
  • **get_client_kwargs: Additional keyword arguments to pass to either get_engine or get_connection.
Returns:
  • The authenticated SQLAlchemy engine or connection.
Examples: Create an engine.
Create a context managed connection.

get_connection

Returns a connection that can be used to query from databases. Args:
  • begin: Whether to begin a transaction on the connection; if True, if any operations fail, the entire transaction will be rolled back.
  • **connect_kwargs: Additional keyword arguments to pass to either engine.begin or engine.connect`.
Returns:
  • The SQLAlchemy Connection.
Examples: Create a synchronous connection as a context-managed transaction.

get_engine

Returns an authenticated engine that can be used to query from databases. If an existing engine exists, return that one. Returns:
  • The authenticated SQLAlchemy Engine.
Examples: Create a synchronous engine to PostgreSQL using URL params.

reset_connections

Tries to close all opened connections and their results. Examples: Resets connections so fetch_* methods return new results.

AsyncSqlAlchemyConnector

Block used to manage authentication with a database using asynchronous drivers. Upon instantiating, an engine is created and maintained for the life of the object until the close method is called. It is recommended to use this block as an async context manager, which will automatically close the engine and its connections when the context is exited. It is also recommended that this block is loaded and consumed within a single task or flow because if the block is passed across separate tasks and flows, the state of the block’s connection and cursor could be lost. Attributes:
  • connection_info: SQLAlchemy URL to create the engine; either create from components or create from a string.
  • connect_args: The options which will be passed directly to the DBAPI’s connect() method as additional keyword arguments.
  • fetch_size: The number of rows to fetch at a time.
Methods:

block_initialization

Initializes the engine.

close

Closes async connections and its cursors.

execute

Executes an operation on the database. This method is intended to be used for operations that do not return data, such as INSERT, UPDATE, or DELETE. Unlike the fetch methods, this method will always execute the operation upon calling. Args:
  • operation: The SQL query or other operation to be executed.
  • parameters: The parameters for the operation.
  • **execution_options: Options to pass to Connection.execution_options.
Examples: Create a table and insert one row into it.

execute_many

Executes many operations on the database. This method is intended to be used for operations that do not return data, such as INSERT, UPDATE, or DELETE. Unlike the fetch methods, this method will always execute the operation upon calling. Args:
  • operation: The SQL query or other operation to be executed.
  • seq_of_parameters: The sequence of parameters for the operation.
  • **execution_options: Options to pass to Connection.execution_options.
Examples: Create a table and insert two rows into it.

fetch_all

Fetch all results from the database. Repeated calls using the same inputs to any of the fetch methods of this block will skip executing the operation again, and instead, return the next set of results from the previous execution, until the reset_connections method is called. Args:
  • operation: The SQL query or other operation to be executed.
  • parameters: The parameters for the operation.
  • **execution_options: Options to pass to Connection.execution_options.
Returns:
  • A list of tuples containing the data returned by the database, where each row is a tuple and each column is a value in the tuple.
Examples: Create a table, insert three rows into it, and fetch all where name is ‘Me’.

fetch_many

Fetch a limited number of results from the database. Repeated calls using the same inputs to any of the fetch methods of this block will skip executing the operation again, and instead, return the next set of results from the previous execution, until the reset_connections method is called. Args:
  • operation: The SQL query or other operation to be executed.
  • parameters: The parameters for the operation.
  • size: The number of results to return; if None or 0, uses the value of fetch_size configured on the block.
  • **execution_options: Options to pass to Connection.execution_options.
Returns:
  • A list of tuples containing the data returned by the database, where each row is a tuple and each column is a value in the tuple.
Examples: Create a table, insert three rows into it, and fetch two rows repeatedly.

fetch_one

Fetch a single result from the database. Repeated calls using the same inputs to any of the fetch methods of this block will skip executing the operation again, and instead, return the next set of results from the previous execution, until the reset_connections method is called. Args:
  • operation: The SQL query or other operation to be executed.
  • parameters: The parameters for the operation.
  • **execution_options: Options to pass to Connection.execution_options.
Returns:
  • A tuple containing the data returned by the database, where each column is a value in the tuple.
Examples: Create a table, insert three rows into it, and fetch a row repeatedly.

get_client

Returns either an engine or connection that can be used to query from databases. Args:
  • client_type: Select from either ‘engine’ or ‘connection’.
  • **get_client_kwargs: Additional keyword arguments to pass to either get_engine or get_connection.
Returns:
  • The authenticated SQLAlchemy engine or connection.
Examples: Create an engine.
Create a context managed connection.

get_connection

Returns a connection that can be used to query from databases. Args:
  • begin: Whether to begin a transaction on the connection; if True, if any operations fail, the entire transaction will be rolled back.
  • **connect_kwargs: Additional keyword arguments to pass to either engine.begin or engine.connect`.
Returns:
  • The SQLAlchemy AsyncConnection.
Examples: Create an asynchronous connection as a context-managed transaction.

get_engine

Returns an authenticated engine that can be used to query from databases. If an existing engine exists, return that one. Returns:
  • The authenticated SQLAlchemy AsyncEngine.
Examples: Create an asynchronous engine to PostgreSQL using URL params.

reset_connections

Tries to close all opened connections and their results. Examples: Resets connections so fetch_* methods return new results.