1
- # Copyright (c) 2023 - 2023 , Oracle and/or its affiliates. All rights reserved.
1
+ # Copyright (c) 2023 - 2025 , Oracle and/or its affiliates. All rights reserved.
2
2
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3
3
4
4
# pylint: skip-file
16
16
from sqlalchemy import Connection
17
17
from sqlalchemy .ext import compiler
18
18
from sqlalchemy .schema import BaseDDLElement , DDLElement , MetaData , SchemaItem , Table
19
- from sqlalchemy .sql import Select , TableClause , table
19
+ from sqlalchemy .sql import Select
20
20
21
21
22
22
class CreateView (DDLElement ):
@@ -52,7 +52,32 @@ def view_exists(
52
52
state : Any | None = None ,
53
53
** kw : Any ,
54
54
) -> bool :
55
- """View exists."""
55
+ """Check if a view exists in the database.
56
+
57
+ This function checks whether a view, defined by the `CreateView` or `DropView`
58
+ object, already exists in the database. It relies on the provided `bind` connection
59
+ to inspect the existing views in the database.
60
+
61
+ Parameters
62
+ ----------
63
+ ddl : BaseDDLElement
64
+ The DDL element that represents the creation or dropping of a view.
65
+ target : SchemaItem
66
+ The target schema item (not directly used in this check).
67
+ bind : Connection | None
68
+ The database connection used to inspect the database for existing views.
69
+ tables : list[Table] | None, optional
70
+ A list of tables (not directly used in this check).
71
+ state : Any | None, optional
72
+ The state of the object (not directly used in this check).
73
+ kw : Any
74
+ Additional keyword arguments passed to the function (not directly used).
75
+
76
+ Returns
77
+ -------
78
+ bool
79
+ Returns `True` if the view exists in the database, `False` otherwise.
80
+ """
56
81
if isinstance (ddl , CreateView ) or isinstance (ddl , DropView ):
57
82
assert isinstance (bind , Connection )
58
83
return ddl .name in sa .inspect (bind ).get_view_names ()
@@ -67,19 +92,60 @@ def view_doesnt_exist(
67
92
state : Any | None = None ,
68
93
** kw : Any ,
69
94
) -> bool :
70
- """Not view exists."""
95
+ """Check if a view does not exist in the database.
96
+
97
+ This function is the inverse of `view_exists`. It returns `True` if the view
98
+ defined by the `CreateView` or `DropView` object does not exist in the database.
99
+
100
+ Parameters
101
+ ----------
102
+ ddl : BaseDDLElement
103
+ The DDL element that represents the creation or dropping of a view.
104
+ target : SchemaItem
105
+ The target schema item (not directly used in this check).
106
+ bind : Connection | None
107
+ The database connection used to inspect the database for existing views.
108
+ tables : list[Table] | None, optional
109
+ A list of tables (not directly used in this check).
110
+ state : Any | None, optional
111
+ The state of the object (not directly used in this check).
112
+ kw : Any
113
+ Additional keyword arguments passed to the function (not directly used).
114
+
115
+ Returns
116
+ -------
117
+ bool
118
+ Returns `True` if the view does not exist in the database, `False` otherwise.
119
+ """
71
120
return not view_exists (ddl , target , bind , ** kw )
72
121
73
122
74
- def create_view (name : str , metadata : MetaData , selectable : Select [Any ]) -> TableClause :
75
- """Create a view."""
76
- view = table (name )
77
- view ._columns ._populate_separate_keys (col ._make_proxy (view ) for col in selectable .selected_columns )
123
+ def create_view (name : str , metadata : MetaData , selectable : Select [Any ]) -> None :
124
+ """Create and manage a view for an existing table, including its lifecycle.
78
125
126
+ This function allows you to define a SQL view based on an existing table, as well as
127
+ handle operations like creation and deletion.
128
+
129
+ For an example implementation, see this Wiki page:
130
+ https://github.com/sqlalchemy/sqlalchemy/wiki/Views
131
+
132
+
133
+ Parameters
134
+ ----------
135
+ name : str
136
+ The name of the view to be created.
137
+ metadata : MetaData
138
+ The MetaData object that contains the schema and is used to listen for
139
+ the `after_create` and `before_drop` events.
140
+ selectable : Select[Any]
141
+ A table that defines the content of the view.
142
+ """
143
+ # Create the view after all tables are created:.
79
144
sqlalchemy .event .listen (
80
145
metadata ,
81
146
"after_create" ,
82
147
CreateView (name , selectable ).execute_if (callable_ = view_doesnt_exist ),
83
148
)
149
+
150
+ # Ensure the view is dropped before any tables in the database are dropped.
84
151
sqlalchemy .event .listen (metadata , "before_drop" , DropView (name ).execute_if (callable_ = view_exists ))
85
- return view
0 commit comments