
    Iil                     N   S SK rS SKJr  S SKJrJr  S SKJrJ	r	  S SK
Jr   " S S\5      r\R                  " \5      S 5       r " S	 S
\5      r\R                  " \5      S 5       r   SS jr  SS jr  SS jr " S S\	\5      r\R                  " \5      S 5       rSS jrg)    N)compiler)
DDLElementPrimaryKeyConstraint)ClauseElement
Executable)get_columnsc                       \ rS rSrSS jrSrg)
CreateView	   c                 f    U(       a  U(       a  [        S5      eXl        X l        X0l        X@l        g )Nz4Cannot use CREATE OR REPLACE with materialized views)
ValueErrorname
selectablematerializedreplace)selfr   r   r   r   s        ;/venv/lib/python3.13/site-packages/sqlalchemy_utils/view.py__init__CreateView.__init__
   s(    GSTT	$(    )r   r   r   r   N)FF__name__
__module____qualname____firstlineno__r   __static_attributes__ r   r   r
   r
   	   s    r   r
   c           
         SR                  U R                  (       a  SOSU R                  (       a  SOSUR                  R                  R                  U R                  5      UR                  R                  U R                  SS95      $ )NzCREATE {}{}VIEW {} AS {}zOR REPLACE  MATERIALIZED T)literal_binds)
formatr   r   dialectidentifier_preparerquoter   sql_compilerprocessr   elementr   kws      r    compile_create_materialized_viewr+      sj    %,, b"//R,,227<<@%%g&8&8%M	 r   c                       \ rS rSrSS jrSrg)DropView   c                 (    Xl         X l        X0l        g N)r   r   cascade)r   r   r   r1   s       r   r   DropView.__init__   s    	(r   )r1   r   r   N)FTr   r   r   r   r-   r-      s    r   r-   c                     SR                  U R                  (       a  SOSUR                  R                  R	                  U R
                  5      U R                  (       a  S5      $ S5      $ )NzDROP {}VIEW IF EXISTS {} {}r    r   CASCADE)r"   r   r#   r$   r%   r   r1   r(   s      r   compile_drop_materialized_viewr5   $   sX    (//"//R,,227<<@__	  +- r   c                 b   Uc  / nUc  [         R                  " 5       nUc  0 n[        U5       Vs/ s H\  n[         R                  " UR                  UR
                  UR                  UR                  UR                  5      UR                  S9PM^     snU-   n[         R                  " X/UQ70 UD6n[        [        U5       Vs/ s H  ofR                  PM     sn5      (       d:  UR                  [        [        U5       Vs/ s H  ofR                  PM     sn6 5        U$ s  snf s  snf s  snf )N)keyprimary_key)saMetaDatar   Columnr   typegetr8   Tableanyappend_constraintr   )	r   r   indexesmetadataaliaseskwargscargstables	            r   create_table_from_selectablerH   -   s    ;;= Z( )A 			FFFFAFFAFF+		
 ) 	D HHT5d5f5E{:'>?'>!'>?@@ ;z3J"K3Ja663J"KL	
 L @"Ks   A#D"D'D,c           
        ^ [        U UUSUS9m[        R                  R                  US[	        XSS95        [        R                  R                  US5      U4S j5       n[        R                  R                  US[        U SS95        T$ )a8  Create a view on a given metadata

:param name: The name of the view to create.
:param selectable: An SQLAlchemy selectable e.g. a select() statement.
:param metadata:
    An SQLAlchemy Metadata instance that stores the features of the
    database being described.
:param indexes: An optional list of SQLAlchemy Index instances.
:param aliases:
    An optional dictionary containing with keys as column names and values
    as column aliases.

Same as for ``create_view`` except that a ``CREATE MATERIALIZED VIEW``
statement is emitted instead of a ``CREATE VIEW``.

N)r   r   rA   rB   rC   after_createT)r   c                 N   > TR                    H  nUR                  U5        M     g r0   rA   createtarget
connectionr*   idxrG   s       r   create_indexes0create_materialized_view.<locals>.create_indexesr       ==CJJz" !r   before_droprH   r9   eventlistenr
   listens_forr-   )r   r   rB   rA   rC   rR   rG   s         @r   create_materialized_viewrZ   M   s    . )E HHOO4$7 XX(N3# 4# HHOOD)
 Lr   c           
        ^ [        U USS9m[        R                  R                  US[	        XUS95        [        R                  R                  US5      U4S j5       n[        R                  R                  US[        XS95        T$ )a  Create a view on a given metadata

:param name: The name of the view to create.
:param selectable: An SQLAlchemy selectable e.g. a select() statement.
:param metadata:
    An SQLAlchemy Metadata instance that stores the features of the
    database being described.
:param cascade_on_drop: If ``True`` the view will be dropped with
    ``CASCADE``, deleting all dependent objects as well.
:param replace: If ``True`` the view will be created with ``OR REPLACE``,
    replacing an existing view with the same name.

The process for creating a view is similar to the standard way that a
table is constructed, except that a selectable is provided instead of
a set of columns. The view is created once a ``CREATE`` statement is
executed against the supplied metadata (e.g. ``metadata.create_all(..)``),
and dropped when a ``DROP`` is executed against the metadata.

To create a view that performs basic filtering on a table. ::

    metadata = MetaData()
    users = Table('users', metadata,
            Column('id', Integer, primary_key=True),
            Column('name', String),
            Column('fullname', String),
            Column('premium_user', Boolean, default=False),
        )

    premium_members = select(users).where(users.c.premium_user == True)
    # sqlalchemy 1.3:
    # premium_members = select([users]).where(users.c.premium_user == True)
    create_view('premium_users', premium_members, metadata)

    metadata.create_all(engine) # View is created at this point

N)r   r   rB   rJ   )r   c                 N   > TR                    H  nUR                  U5        M     g r0   rL   rN   s       r   rR   #create_view.<locals>.create_indexes   rT   r   rU   )r1   rV   )r   r   rB   cascade_on_dropr   rR   rG   s         @r   create_viewr_      s    V )E HHOO4W5 XX(N3# 4# HHOO/
 Lr   c                       \ rS rSrSrS rSrg)RefreshMaterializedView   Tc                     Xl         X l        g r0   )r   concurrently)r   r   rd   s      r   r    RefreshMaterializedView.__init__   s    	(r   rd   r   N)r   r   r   r   inherit_cacher   r   r   r   r   ra   ra      s    M)r   ra   c                     SR                  U R                  (       a  SOSUR                  R                  R	                  U R
                  5      S9$ )Nz.REFRESH MATERIALIZED VIEW {concurrently}{name}zCONCURRENTLY r   rf   )r"   rd   r#   r$   r%   r   )r)   r   s     r   !compile_refresh_materialized_viewri      sF    ;BB(/(<(<_"1177E C  r   c                 X    U R                  5         U R                  [        X5      5        g)a+  Refreshes an already existing materialized view

:param session: An SQLAlchemy Session instance.
:param name: The name of the materialized view to refresh.
:param concurrently:
    Optional flag that causes the ``CONCURRENTLY`` parameter
    to be specified when the materialized view is refreshed.
N)flushexecutera   )sessionr   rd   s      r   refresh_materialized_viewrn      s     MMOOO+D?@r   )NNN)NN)TF)F)
sqlalchemyr9   sqlalchemy.extr   sqlalchemy.schemar   r   sqlalchemy.sql.expressionr   r   sqlalchemy_utils.functionsr   r
   compilesr+   r-   r5   rH   rZ   r_   ra   ri   rn   r   r   r   <module>ru      s     # > ? 2  
: z  
8  H /l AH)j- ) 
*+ ,Ar   