
    _                        d Z ddlmZ ddlmZ ddlmZ ddlZddlZddl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 ddlmZ ddlmZ ddlmZ ddlmZ ddlZddiZd Zej>                   ej@                  ejB                  jD                  ejB                  jF                  ejB                  jH                        ejJ                   G d dejL                                             Z'y)zGCommand to query metadata against Dataproc Metastore services database.    )absolute_import)division)unicode_literalsN)
exceptions)operations_util)services_util)util)storage_api)storage_util)base)resource_args)log)resource_printerEXAMPLESa)            To query metadata against a Dataproc Metastore service with the name
          `my-metastore-service` in location `us-central1`, and the sql query
          "show tables;", run:

          $ {command} my-metastore-service --location=us-central1
          --query="show tables;"

        c                 `    t        j                  | dddd       | j                  ddd       y)	z|Parses provided arguments to add base arguments used for Alpha/Beta/GA.

  Args:
    parser: an argparse argument parser.
  zto query metadataFT)pluralrequired
positionalz--queryz            Use Google Standard SQL query for Cloud Spanner and MySQL query
            syntax for Cloud SQL. Cloud Spanner SQL is described at
            https://cloud.google.com/spanner/docs/query-syntax)"
        )r   helpN)r   AddServiceResourceArgadd_argumentparsers    0lib/surface/metastore/services/query_metadata.pyAddBaseArgsr   3   s=     %%!%$4Q      c                   0    e Zd ZdZeZed        Zd Zd Z	y)QueryzDExecute a SQL query against a Dataproc Metastore Service's metadata.c                 X    t        |        t        j                  j                  |        y)zSee base class.N)r   r   FORMAT_FLAGAddToParserr   s    r   Argsz
Query.ArgsP   s        (r   c                 6   |j                   j                  j                         }d}	 t        j                  |j                         |j                  | j                               }t        j                  j                  dj                  |j                               d}	 t#        j$                  |dj                  |j                               | j                               }|$|j.                  rt1        |j.                        d	k  ryd}|j.                  D ](  }|j2                  d
k(  s|j4                  j6                  }* |yt9        j:                         }t=        j>                  tA        jB                  |jE                  tF        jH                  jK                  |d            d            }	t        j                  j                  |	d   d   |       |	d   syt1        |	d         dkD  rt        j                  j                  d       |	d   d   }
t=        j>                  tA        jB                  |jE                  tF        jH                  jK                  tM        jN                  tM        jP                  |      |
      d            d            S # t        j                  $ r t        j                   d      w xY w# t        j&                  $ rE}t        j(                  |j                         ddt+        j,                  |             Y d}~/d}~ww xY w)zRuns this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      Some value that we want to have printed later.
    N)release_trackzwith operation [{}]zQuery did not succeed.zWaiting for [{}] to queryserviceF)kindis_asyncfailed   resultManifestUriTzutf-8)encodingstatusmessage	filenames   zcThe number of rows exceeds 1000 to display. Please find more results at the cloud storage location.r   ))CONCEPTSr%   Parseservices_api_utilQueryMetadataRelativeNamequeryReleaseTrackr   outPrintformatnameapitools_exceptions	HttpErrorapi_utilQueryMetadataErrorr   PollAndReturnOperationOperationErrorUpdatedResourcesix	text_typeadditionalPropertieslenkeyvaluestring_valuer
   StorageClientjsonloadioTextIOWrapper
ReadObjectr   ObjectReferenceFromUrl	posixpathjoindirname)selfargsenv_ref	operationoperation_resulteresult_manifest_urir-   
gcs_clientresult_manifest_jsonquery_result_file_names              r   Runz	Query.RunV   s    mm##))+GIB#11



 $**D<M<M<OQi	ggmm)00@A 
#(??

%
,
,W-A-A-C
D))+- 	 11112Q6#88	+	+%mm88 9 "**,J99
!!,,445H59;< 		 GGMM&x0;%',
,-1	ggmm N O1+>qA99
!!,,44NN!))*=>. 	 	
 S (( B''(@AAB "" #	



 q!	# ##s%   A6J !AK  )J= L:LLc                 H   |rd|vsd|d   vsd|vry|d   d   D cg c]  }|d   xs d }}dj                  d t        |      D              }|d   D cg c]  }d	|i }}t        j                  |d
j	                  |      t
        j                         yc c}w c c}w )aD  Displays the server response to a query.

    This is called higher up the stack to over-write default display behavior.
    What gets displayed depends on the mode in which the query was run.

    Args:
      args: The arguments originally passed to the command.
      result: The output of the command before display.
    metadatacolumnsrowsNr:   z(Unspecified),c              3   F   K   | ]  \  }}d j                  ||        yw)z!row.slice({0}).join():label="{1}"N)r9   ).0ifs      r   	<genexpr>z Query.Display.<locals>.<genexpr>   s*      ;(91 @FFq!L(9s   !rowz
table({0}))r7   )rR   	enumerater   r8   r9   r   r7   )rT   rU   resultfieldfieldstable_formatri   rb   s           r   DisplayzQuery.Display   s     Zv-&C 2V+ J'	22E 	f((2  
 88 ;(1&(9; ;L$*6N3NSUCLND34!4!4\!BP 4s   BBN)
__name__
__module____qualname____doc__DETAILED_HELPdetailed_helpstaticmethodr"   r^   ro    r   r   r   r   F   s,     M-) )
GRQr   r   )(rs   
__future__r   r   r   rL   rJ   rQ   apitools.base.pyr   r;    googlecloudsdk.api_lib.metastorer   r   r2   r	   r=   googlecloudsdk.api_lib.storager
   r   googlecloudsdk.callioper   $googlecloudsdk.command_lib.metastorer   googlecloudsdk.corer   googlecloudsdk.core.resourcer   rB   rt   r   UnicodeIsSupportedReleaseTracksr6   ALPHABETAGADefaultUniverseOnlyCommandr   rw   r   r   <module>r      s    N &  ' 	   > < O = 6 7 ( > # 9 
 	& T..33T5F5F5I5I jQDLL jQ  
jQr   