
    ɯeif                         d dl Z d dlmZmZmZ 	 d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ d dlZd dlmZ d dlmZmZ d dlm Z   G d d      Z!y# e$ rZ ed      edZ[ww xY w)    N)ListOptionalUnion)Root)Database)NotFoundError)	Procedure)Schema)TableTableColumn)UserDefinedFunction)Viewz.Missing optional dependency: 'snowflake.core'.)convert_sp_to_sf_type)lit
parse_json)DataTypec                      e Zd ZdZd:dZ	 d;deeeef      deeee	e
eeef      defdZ	 d;deeee	f      deeee
eeef      defd	Zd
eeeef   deee      defdZd<dZddddddedeeeef      deeee	f      dee   dee   f
dZddddee   dee   dee   fdZdddddeeeef      dee   dee   dee	   fdZddddddeeeef      deeee	f      dee   dee   dee
   f
dZddddddeeeef      deeee	f      dee   dee   dee   f
dZddddeee
f   deeeef      deeee	f      dee   fdZddddddeeeef      deeee	f      dee   dee   dee   f
dZddddddeeeef      deeee	f      dee   dee   dee   f
dZdee   fdZdee   fdZdedefd Zdd!dedeeeef      de	fd"Z ddddedeeeef      deeee	f      de
fd#Z!dddd$edeeeef      deeee	f      defd%Z"dddd&edee   deeeef      deeee	f      def
d'Z#dddd(edee   deeeef      deeee	f      def
d)Z$deeef   ddfd*Z%deee	f   ddfd+Z&deeef   de'fd,Z(dd!deee	f   deeeef      de'fd-Z)dddd.eee
f   deeeef      deeee	f      de'fd/Z*dddd0eeef   deeeef      deeee	f      de'fd1Z+	 d;dddd2eeef   deee      deeeef      deeee	f      de'f
d3Z,	 d;dddd4eeef   deee      deeeef      deeee	f      de'f
d5Z-deeef   ddfd6Z.dd!deee	f   deeeef      ddfd7Z/dddd.eee
f   deeeef      deeee	f      ddfd8Z0dddd0eeef   deeeef      deeee	f      ddfd9Z1eZ2eZ3eZ4eZ5eZ6eZ7eZ8eZ9eZ:eZ;e Z<e!Z=e"Z>e#Z?e$Z@e%ZAe&ZBe(ZCe)ZDe*ZEe+ZFe,ZGe-ZHe.ZIe/ZJe0ZKe1ZLy)=CatalogzThe Catalog class provides methods to interact with and manage the Snowflake objects.
    It allows users to list, get, and drop various database objects such as databases, schemas, tables,
    views, functions, etc.
    returnNc                 @    || _         t        |      | _        d | _        y N)_sessionr   _root_python_regex_udf)selfsessions     \/var/www/html/glpi_dashboard/venv/lib/python3.12/site-packages/snowflake/snowpark/catalog.py__init__zCatalog.__init__#   s    ']
!%    database	model_objc                 V   t        |t        t        t        t        t
        f      r|j                  }|J |S t        |t              r|r|S t        |t              r|j                  S |s)| j                  j                         }|t        d      |S t        dt        |       d      )Nz9No database detected. Please provide database to proceed.z0Unexpected type. Expected str or Database, got '')
isinstancer
   r   r   r	   r   database_namestrr   namer   get_current_database
ValueErrortype)r   r    r!   db_namecurrent_databases        r   _parse_databasezCatalog._parse_database(   s     i&%yBU!VW--G&&&Nh$Oh)== #}}AAC' O  $#>tH~>NaP
 	
r   schemac                 L   t        |t        t        t        t        f      r|j
                  }|J |S t        |t              r|r|S t        |t              r|j                  S |s)| j                  j                         }|t        d      |S t        dt        |       d      )Nz5No schema detected. Please provide schema to proceed.z.Unexpected type. Expected str or Schema, got 'r#   )r$   r   r   r	   r   schema_namer&   r
   r'   r   get_current_schemar)   r*   )r   r.   r!   r0   current_schemas        r   _parse_schemazCatalog._parse_schemaC   s     i%y:M!NO#//K***fc"vMff%;;!]]==?N% K  "!<T&\N!L
 	
r   fn	arg_typesc                    t        |t              r=|t        d      dj                  |D cg c]  }t	        |       c}      }| d| dS dj                  d |j
                  D              }|j                   d| dS c c}w )Nz>arg_types must be provided when function/procedure is a stringz, ()c              3   4   K   | ]  }|j                     y wr   )datatype).0args     r   	<genexpr>z7Catalog._parse_function_or_procedure.<locals>.<genexpr>m   s     !G3#,,!Gs   )r$   r&   r)   joinr   	argumentsr'   )r   r4   r5   arg_typearg_types_strs        r   _parse_function_or_procedurez$Catalog._parse_function_or_procedure^   s    
 b#  T  !IIAJKX&x0KM T=/++		!G",,!GG''!M?!,, Ls   Bc                    | j                   j                  5  | j                  
	 d d d        y dt        dt        dt        fd}| j                   j
                  j                  |      | _        d d d        y # 1 sw Y   y xY w)Npatterninputr   c                 @    t        t        j                  | |            S r   )boolrematch)rD   rE   s     r   python_regex_filterz:Catalog._initialize_regex_udf.<locals>.python_regex_filteru   s    BHHWe455r   )r   _lockr   r&   rG   udfregister)r   rJ   s     r   _initialize_regex_udfzCatalog._initialize_regex_udfp   s}    ]]   	U%%1	U 	U6S 6 6 6 &*]]%6%6%?%?@S%TD"	U 	U 	Us   A7A A77B )r    r.   rD   likeobject_namerD   rO   c                   | j                  |      }| j                  |      }|rd| dnd}	| j                  j                  d| d|	 d| d| d	      }
|rT| j	                          | j
                  J |
j                  | j                  t        |      t        d	      d
               }
t        t        fd|
j                                     S )NzLIKE 'r#    zSHOW AS RESOURCE  z IN .z -- catalog apiz"As Resource"r'   c                 ,    j                  | d         S )Nr   )	from_json)rowobject_classs    r   <lambda>z'Catalog._list_objects.<locals>.<lambda>   s    L$:$:3q6$B r   )r-   r3   r   sqlrN   r   filterr   r   listmapcollect)r   rP   rX   r    r.   rD   rO   r+   r0   like_strdfs     `        r   _list_objectszCatalog._list_objectsz   s     &&x0((0'+VD6#]]}AhZtG9Ak]Rab
 &&())555
 &&L*_"=f"EB CBBJJLQRRr   )rD   rO   c                    | j                   j                  j                  |      }rt        fd|      }t	        |      S )zList databases in the current session.

        Args:
            pattern: the python regex pattern of name to match. Defaults to None.
            like: the sql style pattern for name to match. Default to None.
        rO   c                 D    t        j                  | j                        S r   rH   rI   r'   xrD   s    r   rY   z(Catalog.list_databases.<locals>.<lambda>       BHHWaff$= r   )r   	databasesiterr[   r\   )r   rD   rO   rj   s    `  r   list_databaseszCatalog.list_databases   s<     zz##((d(3=tDDDzr   )r    rD   rO   c                    | j                  |      }| j                  j                  |   j                  j	                  |      }rt        fd|      }t        |      S )a  List schemas in the current session. If database is provided, list schemas in the
        database, otherwise list schemas in the current database.

        Args:
            database: database name or ``Database`` object. Defaults to None.
            pattern: the python regex pattern of name to match. Defaults to None.
            like: the sql style pattern for name to match. Default to None.
        rc   c                 D    t        j                  | j                        S r   re   rf   s    r   rY   z&Catalog.list_schemas.<locals>.<lambda>   rh   r   )r-   r   ri   schemasrj   r[   r\   )r   r    rD   rO   r+   rj   s     `   r   list_schemaszCatalog.list_schemas   sW     &&x0zz##G,4499t9D=tDDDzr   c                8    | j                  dt        ||||      S )a  List tables in the current session. If database or schema are provided, list tables
        in the given database or schema, otherwise list tables in the current database/schema.

        Args:
            database: database name or ``Database`` object. Defaults to None.
            schema: schema name or ``Schema`` object. Defaults to None.
            pattern: the python regex pattern of name to match. Defaults to None.
            like: the sql style pattern for name to match. Default to None.
        TABLESrP   rX   r    r.   rD   rO   )ra   r   r   r    r.   rD   rO   s        r   list_tableszCatalog.list_tables   s.    " !!  " 
 	
r   c                8    | j                  dt        ||||      S )a  List views in the current session. If database or schema are provided, list views
        in the given database or schema, otherwise list views in the current database/schema.

        Args:
            database: database name or ``Database`` object. Defaults to None.
            schema: schema name or ``Schema`` object. Defaults to None.
            pattern: the python regex pattern of name to match. Defaults to None.
            like: the sql style pattern for name to match. Default to None.
        VIEWSrr   )ra   r   rs   s        r   
list_viewszCatalog.list_views   s.    " !! " 
 	
r   r    r.   
table_namec                t    t        |t              r| j                  |||      }n|}|j                  }|J |S )zList columns in the given table.

        Args:
            table_name: table name.
            database: database name or ``Database`` object. Defaults to None.
            schema: schema name or ``Schema`` object. Defaults to None.
        rx   )r$   r&   	get_tablecolumns)r   ry   r    r.   tablecolss         r   list_columnszCatalog.list_columns   sA     j#&NN:NPEE}}r   c                8    | j                  dt        ||||      S )a  List of procedures in the given database and schema. If database or schema are not
        provided, list procedures in the current database and schema.

        Args:
            database: database name or ``Database`` object. Defaults to None.
            schema: schema name or ``Schema`` object. Defaults to None.
            pattern: the python regex pattern of name to match. Defaults to None.
            like: the sql style pattern for name to match. Default to None.
        
PROCEDURESrr   )ra   r	   rs   s        r   list_procedureszCatalog.list_procedures  s.    " !!$" " 
 	
r   c                8    | j                  dt        ||||      S )a  List of user defined functions in the given database and schema. If database or schema
        are not provided, list user defined functions in the current database and schema.
        Args:
            database: database name or ``Database`` object. Defaults to None.
            schema: schema name or ``Schema`` object. Defaults to None.
            pattern: the python regex pattern of name to match. Defaults to None.
            like: the sql style pattern for name to match. Default to None.
        zUSER FUNCTIONSrr   )ra   r   rs   s        r   list_user_defined_functionsz#Catalog.list_user_defined_functions(  s.      !!(, " 
 	
r   c                 6    | j                   j                         S )zGet the current database.)r   r(   r   s    r   r(   zCatalog.get_current_databaseB  s    }}1133r   c                 6    | j                   j                         S )zGet the current schema.)r   r1   r   s    r   r1   zCatalog.get_current_schemaF  s    }}//11r   c                 P    | j                   j                  |   j                         S )zName of the database to get)r   ri   fetch)r   r    s     r   get_databasezCatalog.get_databaseJ  s     zz##H-3355r   )r    c                    | j                  |      }| j                  j                  |   j                  |   j	                         S )zName of the schema to get.)r-   r   ri   rn   r   )r   r.   r    r+   s       r   
get_schemazCatalog.get_schemaN  s=     &&x0zz##G,44V<BBDDr   c                    | j                  |      }| j                  |      }| j                  j                  |   j                  |   j
                  |   j                         S )an  Get the table by name in given database and schema. If database or schema are not
        provided, get the table in the current database and schema.

        Args:
            table_name: name of the table.
            database: database name or ``Database`` object. Defaults to None.
            schema: schema name or ``Schema`` object. Defaults to None.
        )r-   r3   r   ri   rn   tablesr   )r   ry   r    r.   r+   r0   s         r   r{   zCatalog.get_tableU  s\     &&x0((0JJ  )W["VJ  UW		
r   	view_namec                    | j                  |      }| j                  |      }| j                  j                  |   j                  |   j
                  |   j                         S )aj  Get the view by name in given database and schema. If database or schema are not
        provided, get the view in the current database and schema.

        Args:
            view_name: name of the view.
            database: database name or ``Database`` object. Defaults to None.
            schema: schema name or ``Schema`` object. Defaults to None.
        )r-   r3   r   ri   rn   viewsr   )r   r   r    r.   r+   r0   s         r   get_viewzCatalog.get_viewm  sZ     &&x0((0JJ  )11+>DDYOUUW	
r   procedure_namec                    | j                  |      }| j                  |      }| j                  ||      }| j                  j                  |   j
                  |   j                  |   j                         S )a  Get the procedure by name and argument types in given database and schema. If database or
        schema are not provided, get the procedure in the current database and schema.

        Args:
            procedure_name: name of the procedure.
            arg_types: list of argument types to uniquely identify the procedure.
            database: database name or ``Database`` object. Defaults to None.
            schema: schema name or ``Schema`` object. Defaults to None.
        )r-   r3   rB   r   ri   rn   
proceduresr   )r   r   r5   r    r.   r+   r0   procedure_ids           r   get_procedurezCatalog.get_procedure  sn    " &&x0((088SJJ  )W["Z& UW		
r   udf_namec                    | j                  |      }| j                  |      }| j                  ||      }| j                  j                  |   j
                  |   j                  |   j                         S )a  Get the user defined function by name and argument types in given database and schema.
        If database or schema are not provided, get the user defined function in the current
        database and schema.

        Args:
            udf_name: name of the user defined function.
            arg_types: list of argument types to uniquely identify the user defined function.
            database: database name or ``Database`` object. Defaults to None.
            schema: schema name or ``Schema`` object. Defaults to None.
        )r-   r3   rB   r   ri   rn   user_defined_functionsr   )r   r   r5   r    r.   r+   r0   function_ids           r   get_user_defined_functionz!Catalog.get_user_defined_function  so    $ &&x0((077)LJJ  )W["##K1 UW		
r   c                 \    | j                  |      }| j                  j                  |       y)zSet the current default database for the session.

        Args:
            database: database name or ``Database`` object.
        N)r-   r   use_databaser   r    r+   s      r   set_current_databasezCatalog.set_current_database  s&     &&x0""7+r   c                 \    | j                  |      }| j                  j                  |       y)z}Set the current default schema for the session.

        Args:
            schema: schema name or ``Schema`` object.
        N)r3   r   
use_schema)r   r.   r0   s      r   set_current_schemazCatalog.set_current_schema  s&     ((0  -r   c                     | j                  |      }	 | j                  j                  |   j                          y# t        $ r Y yw xY w)zwCheck if the given database exists.

        Args:
            database: database name or ``Database`` object.
        TF)r-   r   ri   r   r   r   s      r   database_existszCatalog.database_exists  sI     &&x0	JJ  )//1 		s   '; 	AAc                    | j                  ||      }| j                  |      }	 | j                  j                  |   j                  |   j                          y# t        $ r Y yw xY w)a,  Check if the given schema exists in the given database. If database is not provided,
        check if the schema exists in the current database.

        Args:
            schema: schema name or ``Schema`` object.
            database: database name or ``Database`` object. Defaults to None.
        TF)r-   r3   r   ri   rn   r   r   r   r.   r    r+   r0   s        r   schema_existszCatalog.schema_exists  sf     &&x8((0	JJ  )11+>DDF 		s   4A 	A&%A&r}   c                *   | j                  ||      }| j                  ||      }t        |t              r|n|j                  }	 | j
                  j                  |   j                  |   j                  |   j                          y# t        $ r Y yw xY w)a  Check if the given table exists in the given database and schema. If database or schema
        are not provided, check if the table exists in the current database and schema.

        Args:
            table: table name or ``Table`` object.
            database: database name or ``Database`` object. Defaults to None.
            schema: schema name or ``Schema`` object. Defaults to None.
        TF)r-   r3   r$   r&   r'   r   ri   rn   r   r   r   r   r}   r    r.   r+   r0   ry   s          r   table_existszCatalog.table_exists  s     &&x7((7(4U%**
	JJ  )11+>EEeg 		   AB 	BBviewc                *   | j                  ||      }| j                  ||      }t        |t              r|n|j                  }	 | j
                  j                  |   j                  |   j                  |   j                          y# t        $ r Y yw xY w)a  Check if the given view exists in the given database and schema. If database or schema
        are not provided, check if the view exists in the current database and schema.

        Args:
            view: view name or ``View`` object.
            database: database name or ``Database`` object. Defaults to None.
            schema: schema name or ``Schema`` object. Defaults to None.
        TF)r-   r3   r$   r&   r'   r   ri   rn   r   r   r   r   r   r    r.   r+   r0   r   s          r   view_existszCatalog.view_exists	  s     &&x6((6&tS1Dtyy		JJ  )11+>DDYOUUW 		r   	procedurec                   | j                  ||      }| j                  ||      }| j                  ||      }	 | j                  j                  |   j
                  |   j                  |   j                          y# t        $ r Y yw xY w)a  Check if the given procedure exists in the given database and schema. If database or
        schema are not provided, check if the procedure exists in the current database and schema.

        Args:
            procedure: procedure name or ``Procedure`` object.
            arg_types: list of argument types to uniquely identify the procedure. Defaults to None.
            database: database name or ``Database`` object. Defaults to None.
            schema: schema name or ``Schema`` object. Defaults to None.
        TF)	r-   r3   rB   r   ri   rn   r   r   r   )r   r   r5   r    r.   r+   r0   r   s           r   procedure_existszCatalog.procedure_exists!  s    " &&x;((;88IN	JJ  )11+>IIeg 		   AA: :	BBrL   c                   | j                  ||      }| j                  ||      }| j                  ||      }	 | j                  j                  |   j
                  |   j                  |   j                          y# t        $ r Y yw xY w)aT  Check if the given user defined function exists in the given database and schema. If
        database or schema are not provided, check if the user defined function exists in the
        current database and schema.

        Args:
            udf: user defined function name or ``UserDefinedFunction`` object.
            arg_types: list of argument types to uniquely identify the user defined function.
                Defaults to None.
            database: database name or ``Database`` object. Defaults to None.
            schema: schema name or ``Schema`` object. Defaults to None.
        TF)	r-   r3   rB   r   ri   rn   r   r   r   )r   rL   r5   r    r.   r+   r0   r   s           r   user_defined_function_existsz$Catalog.user_defined_function_exists>  s    & &&x5((577YG	JJ  )11+>UUeg 		r   c                 t    | j                  |      }| j                  j                  |   j                          y)zlDrop the given database.

        Args:
            database: database name or ``Database`` object.
        N)r-   r   ri   dropr   s      r   drop_databasezCatalog.drop_database^  s/     &&x0

W%**,r   c                    | j                  ||      }| j                  |      }| j                  j                  |   j                  |   j                          y)a  Drop the given schema in the given database. If database is not provided, drop the
        schema in the current database.

        Args:
            schema: schema name or ``Schema`` object.
            database: database name or ``Database`` object. Defaults to None.
        N)r-   r3   r   ri   rn   r   r   s        r   drop_schemazCatalog.drop_schemag  sL     &&x8((0

W%--k:??Ar   c                
   | j                  ||      }| j                  ||      }t        |t              r|n|j                  }| j
                  j                  |   j                  |   j                  |   j                          y)az  Drop the given table in the given database and schema. If database or schema are not
        provided, drop the table in the current database and schema.

        Args:
            table: table name or ``Table`` object.
            database: database name or ``Database`` object. Defaults to None.
            schema: schema name or ``Schema`` object. Defaults to None.
        N)
r-   r3   r$   r&   r'   r   ri   rn   r   r   r   s          r   
drop_tablezCatalog.drop_tablex  sm     &&x7((7(4U%**


W%--k:AA*MRRTr   c                
   | j                  ||      }| j                  ||      }t        |t              r|n|j                  }| j
                  j                  |   j                  |   j                  |   j                          y)au  Drop the given view in the given database and schema. If database or schema are not
        provided, drop the view in the current database and schema.

        Args:
            view: view name or ``View`` object.
            database: database name or ``Database`` object. Defaults to None.
            schema: schema name or ``Schema`` object. Defaults to None.
        N)
r-   r3   r$   r&   r'   r   ri   rn   r   r   r   s          r   	drop_viewzCatalog.drop_view  sm     &&x6((6&tS1Dtyy	

W%--k:@@KPPRr   )r   z"snowflake.snowpark.session.Sessionr   Nr   )r   N)M__name__
__module____qualname____doc__r   r   r   r&   r   r
   r   r   r	   r   r-   r3   r   r   rB   rN   ra   rk   ro   rt   rw   r   r   r   r   r(   r1   r   r   r{   r   r   r   r   r   rG   r   r   r   r   r   r   r   r   r   r   listDatabaseslistSchemas
listTables	listViewslistColumnslistProcedureslistUserDefinedFunctionsgetCurrentDatabasegetCurrentSchemagetDatabase	getSchemagetTablegetViewgetProceduregetUserDefinedFunctionsetCurrentDatabasesetCurrentSchemadatabaseExistsschemaExiststableExists
viewExistsprocedureExistsuserDefinedFunctionExistsdropDatabase
dropSchema	dropTabledropView r   r   r   r      s	   
& 
5h/0
 #vudI7JJK

 

@ 
sF{+,
 #udI/BBC

 

6-#y"556- DN+- 
	-$U 48/3!%" S  S
 5h/0 S sF{+, S # S sm SL "&"	 # sm	
 
h* 48!%" 5h/0 #	
 sm 
f0 48/3!%"
 5h/0
 sF{+,	

 #
 sm
 
e
: 48/3!%"
 5h/0
 sF{+,	

 #
 sm
 
d
< 48/3#u*% 5h/0	
 sF{+, 
k	2 48/3!%"
 5h/0
 sF{+,	

 #
 sm
 
i
: 48/3!%"
 5h/0
 sF{+,	

 #
 sm
 
!	"
44hsm 42HSM 26S 6X 6
 JNEE(0sH}1E(FE	E 48/3

 5h/0	

 sF{+,
 

8 48/3

 5h/0	

 sF{+,
 

4 48/3

 >

 5h/0
 sF{+,
 

@ 48/3

 >

 5h/0
 sF{+,
 

:,U3=-A ,d ,.sF{); . .c8m(<  " 48	c6k" 5h/0	
 
2 48/3S%Z  5h/0	
 sF{+, 
< 48/3CI 5h/0	
 sF{+, 
6 /3
 48/3i( DN+
 5h/0 sF{+, 
@ /3
 48/33++, DN+
 5h/0 sF{+, 
@-eCM&: -t - 48	Bc6k"B 5h/0	B
 
B* 48/3US%Z U 5h/0	U
 sF{+,U 
U2 48/3SCIS 5h/0	S
 sF{+,S 
S, #MKJIK$N:-)KIHG L6-)$N LKJ&O < LJIHr   r   )"rH   typingr   r   r   snowflake.corer   snowflake.core.databaser   snowflake.core.exceptionsr   snowflake.core.procedurer	   snowflake.core.schemar
   snowflake.core.tabler   r   $snowflake.core.user_defined_functionr   snowflake.core.viewr   ImportErroresnowflake.snowpark	snowflake'snowflake.snowpark._internal.type_utilsr   snowflake.snowpark.functionsr   r   snowflake.snowpark.typesr   r   r   r   r   <module>r      sl   
 
 ( (#072,7H(  I 8 -d
 d
  
8s   2A% %A8*	A33A8