snowflake.core.view.ViewCollectionΒΆ
- class snowflake.core.view.ViewCollection(schema: SchemaResource)ΒΆ
Bases:
SchemaObjectCollectionParent
[ViewResource
]Represents the collection operations on the Snowflake View resource.
With this collection, you can create, iterate through, and search for views that you have access to in the current context.
Examples
Creating a view instance:
>>> views = root.databases["my_db"].schemas["my_schema"].views >>> new_view = View( ... name="my_view", ... columns=[ ... ViewColumn(name="col1"), ViewColumn(name="col2"), ViewColumn(name="col3"), ... ], ... query="SELECT * FROM my_table", ... ) >>> views.create(new_view)
Attributes
- databaseΒΆ
- rootΒΆ
Methods
- create(view: View, mode: CreateMode = CreateMode.error_if_exists, copy_grants: bool | None = False) ViewResource ΒΆ
Create a view in Snowflake.
- Parameters:
view (View) β The
View
object, together with theView
βs properties: name, columns, query; secure, kind, recursive, comment are optionalcopy_grants (bool, optional) β Whether to enable copy grants when creating the object. Default is
False
.mode (CreateMode, optional) β
One of the following enum values.
CreateMode.error_if_exists
: Throw ansnowflake.core.exceptions.ConflictError
if the view already exists in Snowflake. Equivalent to SQLcreate view <name> ...
.CreateMode.or_replace
: Replace if the view already exists in Snowflake. Equivalent to SQLcreate or replace view <name> ...
.CreateMode.if_not_exists
: Do nothing if the view already exists in Snowflake. Equivalent to SQLcreate view <name> if not exists...
Default is
CreateMode.error_if_exists
.
Examples
Creating a view, replacing any existing view with the same name:
>>> views = root.databases["my_db"].schemas["my_schema"].views >>> new_view = View( ... name="my_view", ... columns=[ ... ViewColumn(name="col1"), ViewColumn(name="col2"), ViewColumn(name="col3"), ... ], ... query="SELECT * FROM my_table", ... ) >>> views.create(new_view, mode=CreateMode.or_replace)
- items() ItemsView[str, T] ΒΆ
- iter(*, like: Annotated[str, Strict(strict=True)] | None = None, starts_with: Annotated[str, Strict(strict=True)] | None = None, show_limit: Annotated[int, FieldInfo(annotation=NoneType, required=True, metadata=[Strict(strict=True), Ge(ge=1), Le(le=10000)])] | None = None, from_name: Annotated[str, Strict(strict=True)] | None = None, deep: Annotated[bool, Strict(strict=True)] | None = None) Iterator[View] ΒΆ
Iterate through
View
objects from Snowflake, filtering on any optional βlikeβ pattern.- Parameters:
like (str, optional) β A case-insensitive string functioning as a filter, with support for SQL wildcard characters (% and _).
starts_with (str, optional) β String used to filter the command output based on the string of characters that appear at the beginning of the object name. Uses case-sensitive pattern matching.
show_limit (int, optional) β Limit of the maximum number of rows returned by iter(). The default is
None
, which behaves equivalently to show_limit=10000. This value must be between1
and10000
.from_name (str, optional) β Fetch rows only following the first row whose object name matches the specified string. This is case-sensitive and does not have to be the full name.
deep (bool, optional) β Optionally includes dependency information of the view. Default is
None
, which is equivalent toFalse
.
Examples
Showing all views that you have access to see:
>>> views = view_collection.iter()
Showing information of the exact view you want to see:
>>> views = view_collection.iter(like="your-view-name")
Showing views starting with βyour-view-name-β:
>>> views = view_collection.iter(like="your-view-name-%")
Using a for loop to retrieve information from iterator:
>>> for view in views: ... print(view.name, view.query)
- keys() KeysView[str] ΒΆ
- values() ValuesView[T] ΒΆ