
    "jI                         S SK Jr  S SKJr  S SKJrJrJr  \" S5      r\" S\ S35      r\" \R                  5        \" \5      r
S rS rSS	 jrS
\S\S\S\4S jrg)    )
quote_plus)create_engine)r   inspecttextzgQube#@96740zpostgresql://postgres:z(@134.209.155.11:5432/greenqube_master_dbc                  *    [         R                  5       $ )z6
Returns a list of all tables in the master database.
)master_inspectorget_table_names     ,/var/www/html/database-metadata/db_master.pyget_master_tablesr      s     ++--r   c            
          [        5        V Vs0 s H,  n U [        R                  U 5       Vs/ s H  oS   PM	     sn_M.     snn $ s  snf s  snn f )u9   
Returns a dictionary of table → list of column names.
name)r   r   get_columns)tablecols     r   get_master_columnsr      sS     '((E 	'7'C'CE'JK'JF'JKK( Ks   A
AA
A
c           	          [         R                  5        nUR                  [        SU  SU 35      5      nU Vs/ s H  n[	        U5      PM     snsSSS5        $ s  snf ! , (       d  f       g= f)z=
Returns up to `limit` rows from the specified master table.
zSELECT * FROM z LIMIT N)master_engineconnectexecuter   dict)r   limitconnresultrows        r   fetch_master_datar      s\     
			 Dd^E7'%#IJK%+,VcS	V, 
!	 , 
!	 s   %A AA A  
A.r   key_col	value_colreturnc                     [         R                  5        nUR                  [        SU SU SU  35      5      nU Vs0 s H
  oUU   XR   _M     snsSSS5        $ s  snf ! , (       d  f       g= f)u   
Returns a dictionary mapping key_col → value_col from the given master table.
Useful for enriching values in join_generator.py.
zSELECT z, z FROM N)r   r   r   r   )r   r   r   r   r   r   s         r   get_master_mappingr"   &   sg    
 
			 DdWWIR	{&#PQR8>?Gcn,? 
!	 ? 
!	 s   (AAAA
A-N)d   )urllib.parser   
sqlalchemyr   r   r   passwordr   printurlr   r   r   r   strr   r"   r
   r   r   <module>r*      s    # $ 3 3
 n%XJ&NO m =) .-@c @C @C @D @r   