Block used to manage connections with Snowflake.Upon instantiating, a connection 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 will be lost.Args:
credentials: The credentials to authenticate with Snowflake.
database: The name of the default database to use.
warehouse: The name of the default warehouse to use.
schema: The name of the default schema to use;
this attribute is accessible through SnowflakeConnector(...).schema_.
fetch_size: The number of rows to fetch at a time.
poll_frequency_s: The number of seconds before checking query.
Examples:Load stored Snowflake connector as a context manager:
from prefect_snowflake.database import SnowflakeConnectorsnowflake_connector = SnowflakeConnector.load("BLOCK_NAME")
Insert data into database and fetch results.
from prefect_snowflake.database import SnowflakeConnectorwith SnowflakeConnector.load("BLOCK_NAME") as conn: conn.execute( "CREATE TABLE IF NOT EXISTS customers (name varchar, address varchar);" ) conn.execute_many( "INSERT INTO customers (name, address) VALUES (%(name)s, %(address)s);", seq_of_parameters=[ {"name": "Ford", "address": "Highway 42"}, {"name": "Unknown", "address": "Space"}, {"name": "Me", "address": "Myway 88"}, ], ) results = conn.fetch_all( "SELECT * FROM customers WHERE address = %(address)s", parameters={"address": "Space"} ) print(results)
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.
cursor_type: The class of the cursor to use when creating a Snowflake cursor.
**execute_kwargs: Additional options to pass to cursor.execute_async.
Examples:Create table named customers with two columns, name and address.
from prefect_snowflake.database import SnowflakeConnectorwith SnowflakeConnector.load("BLOCK_NAME") as conn: conn.execute( "CREATE TABLE IF NOT EXISTS customers (name varchar, address varchar);" )
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.
cursor_type: The class of the cursor to use when creating a Snowflake cursor.
**execute_kwargs: Additional options to pass to cursor.execute_async.
Examples:Create table named customers with two columns, name and address.
from prefect_snowflake.database import SnowflakeConnectorwith SnowflakeConnector.load("BLOCK_NAME") as conn: conn.execute( "CREATE TABLE IF NOT EXISTS customers (name varchar, address varchar);" )
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 operations
upon calling.Args:
operation: The SQL query or other operation to be executed.
seq_of_parameters: The sequence of parameters for the operation.
Examples:Create table and insert three rows into it.
from prefect_snowflake.database import SnowflakeConnectorwith SnowflakeConnector.load("BLOCK_NAME") as conn: conn.execute( "CREATE TABLE IF NOT EXISTS customers (name varchar, address varchar);" ) conn.execute_many( "INSERT INTO customers (name, address) VALUES (%(name)s, %(address)s);", seq_of_parameters=[ {"name": "Marvin", "address": "Highway 42"}, {"name": "Ford", "address": "Highway 42"}, {"name": "Unknown", "address": "Space"}, ], )
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 operations
upon calling.Args:
operation: The SQL query or other operation to be executed.
seq_of_parameters: The sequence of parameters for the operation.
Examples:Create table and insert three rows into it.
from prefect_snowflake.database import SnowflakeConnectorwith SnowflakeConnector.load("BLOCK_NAME") as conn: conn.execute( "CREATE TABLE IF NOT EXISTS customers (name varchar, address varchar);" ) conn.execute_many( "INSERT INTO customers (name, address) VALUES (%(name)s, %(address)s);", seq_of_parameters=[ {"name": "Marvin", "address": "Highway 42"}, {"name": "Ford", "address": "Highway 42"}, {"name": "Unknown", "address": "Space"}, ], )
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_cursors method is called.Args:
operation: The SQL query or other operation to be executed.
parameters: The parameters for the operation.
cursor_type: The class of the cursor to use when creating a Snowflake cursor.
**execute_kwargs: Additional options to pass to cursor.execute_async.
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.
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_cursors method is called.Args:
operation: The SQL query or other operation to be executed.
parameters: The parameters for the operation.
cursor_type: The class of the cursor to use when creating a Snowflake cursor.
**execute_kwargs: Additional options to pass to cursor.execute_async.
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:Fetch all rows from the database where address is Highway 42.
from prefect_snowflake.database import SnowflakeConnectorwith SnowflakeConnector.load("BLOCK_NAME") as conn: await conn.execute_async( "CREATE TABLE IF NOT EXISTS customers (name varchar, address varchar);" ) await conn.execute_many_async( "INSERT INTO customers (name, address) VALUES (%(name)s, %(address)s);", seq_of_parameters=[ {"name": "Marvin", "address": "Highway 42"}, {"name": "Ford", "address": "Highway 42"}, {"name": "Unknown", "address": "Highway 42"}, {"name": "Me", "address": "Myway 88"}, ], ) result = await conn.fetch_all_async( "SELECT * FROM customers WHERE address = %(address)s", parameters={"address": "Highway 42"}, ) print(result) # Marvin, Ford, Unknown
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_cursors 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.
cursor_type: The class of the cursor to use when creating a Snowflake cursor.
**execute_kwargs: Additional options to pass to cursor.execute_async.
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:Repeatedly fetch two rows from the database where address is Highway 42.
from prefect_snowflake.database import SnowflakeConnectorwith SnowflakeConnector.load("BLOCK_NAME") as conn: conn.execute( "CREATE TABLE IF NOT EXISTS customers (name varchar, address varchar);" ) conn.execute_many( "INSERT INTO customers (name, address) VALUES (%(name)s, %(address)s);", seq_of_parameters=[ {"name": "Marvin", "address": "Highway 42"}, {"name": "Ford", "address": "Highway 42"}, {"name": "Unknown", "address": "Highway 42"}, {"name": "Me", "address": "Highway 42"}, ], ) result = conn.fetch_many( "SELECT * FROM customers WHERE address = %(address)s", parameters={"address": "Highway 42"}, size=2 ) print(result) # Marvin, Ford result = conn.fetch_many( "SELECT * FROM customers WHERE address = %(address)s", parameters={"address": "Highway 42"}, size=2 ) print(result) # Unknown, Me
Fetch a limited number of results from the database asynchronously.
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_cursors 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.
cursor_type: The class of the cursor to use when creating a Snowflake cursor.
**execute_kwargs: Additional options to pass to cursor.execute_async.
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:Repeatedly fetch two rows from the database where address is Highway 42.
from prefect_snowflake.database import SnowflakeConnectorwith SnowflakeConnector.load("BLOCK_NAME") as conn: conn.execute( "CREATE TABLE IF NOT EXISTS customers (name varchar, address varchar);" ) conn.execute_many( "INSERT INTO customers (name, address) VALUES (%(name)s, %(address)s);", seq_of_parameters=[ {"name": "Marvin", "address": "Highway 42"}, {"name": "Ford", "address": "Highway 42"}, {"name": "Unknown", "address": "Highway 42"}, {"name": "Me", "address": "Highway 42"}, ], ) result = conn.fetch_many( "SELECT * FROM customers WHERE address = %(address)s", parameters={"address": "Highway 42"}, size=2 ) print(result) # Marvin, Ford result = conn.fetch_many( "SELECT * FROM customers WHERE address = %(address)s", parameters={"address": "Highway 42"}, size=2 ) print(result) # Unknown, Me
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_cursors method is called.Args:
operation: The SQL query or other operation to be executed.
parameters: The parameters for the operation.
cursor_type: The class of the cursor to use when creating a Snowflake cursor.
**execute_kwargs: Additional options to pass to cursor.execute_async.
Returns:
A tuple containing the data returned by the database,
where each row is a tuple and each column is a value in the tuple.
Examples:Fetch one row from the database where address is Space.
from prefect_snowflake.database import SnowflakeConnectorwith SnowflakeConnector.load("BLOCK_NAME") as conn: conn.execute( "CREATE TABLE IF NOT EXISTS customers (name varchar, address varchar);" ) conn.execute_many( "INSERT INTO customers (name, address) VALUES (%(name)s, %(address)s);", seq_of_parameters=[ {"name": "Ford", "address": "Highway 42"}, {"name": "Unknown", "address": "Space"}, {"name": "Me", "address": "Myway 88"}, ], ) result = conn.fetch_one( "SELECT * FROM customers WHERE address = %(address)s", parameters={"address": "Space"} ) print(result)
Fetch a single result from the database asynchronously.
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_cursors method is called.Args:
operation: The SQL query or other operation to be executed.
parameters: The parameters for the operation.
cursor_type: The class of the cursor to use when creating a Snowflake cursor.
**execute_kwargs: Additional options to pass to cursor.execute_async.
Returns:
A tuple containing the data returned by the database,
where each row is a tuple and each column is a value in the tuple.
Examples:Fetch one row from the database where address is Space.
from prefect_snowflake.database import SnowflakeConnectorwith SnowflakeConnector.load("BLOCK_NAME") as conn: conn.execute( "CREATE TABLE IF NOT EXISTS customers (name varchar, address varchar);" ) conn.execute_many( "INSERT INTO customers (name, address) VALUES (%(name)s, %(address)s);", seq_of_parameters=[ {"name": "Ford", "address": "Highway 42"}, {"name": "Unknown", "address": "Space"}, {"name": "Me", "address": "Myway 88"}, ], ) result = await conn.fetch_one_async( "SELECT * FROM customers WHERE address = %(address)s", parameters={"address": "Space"} ) print(result)