
    !                     4   S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSK	Jr  SS	K	Jr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SS0rSS jrS rS r\R8                  \R:                   " S S\R<                  5      5       5       rg)z$Command for spanner databases query.    )absolute_import)division)unicode_literals)database_sessions)apis)arg_parsers)base)
exceptions)resource_args)sql)QueryHasDml)	arg_utils)log)	resourcesEXAMPLESz      To execute a SQL SELECT statement against example-database under
      example-instance, run:

        $ {command} example-database --instance=example-instance --sql='SELECT * FROM MyTable WHERE MyKey = 1'
    Nc                     [         R                  " U R                  R                  R	                  5       U5      n[
        R                  R                  UR                  SS9$ )a  Creates a session.

Args:
  args: an argparse namespace. All the arguments that were provided to the
    command invocation.
  database_role: Cloud Spanner database role which owns this session.

Returns:
  A session reference to be used to execute the sql.
z-spanner.projects.instances.databases.sessions)relative_name
collection)	r   CreateCONCEPTSdatabaseParser   REGISTRYParseRelativeNamename)argsdatabase_rolesession_names      ,lib/surface/spanner/databases/execute_sql.pyCreateSessionr    -   sX     #))$--*@*@*F*F*H*79,				-	- %%@ 
. 
B B    c                    [         R                  " U S5        U R                  SSSS9  SSSS	S
S.nU R                  SSS USS9  U R                  SSSS9  U R                  S[        R                  " 5       SSS9  [
        R                  " SS5      n[        U5      R                  R                  U 5        U R                  SSS9nUR                  SSSS9  UR                  SS S!S"9  U R                  S#S$S%9  g&)'zyParses provided arguments to add base arguments used for both Beta and GA.

Args:
  parser: an argparse argument parser.
z to execute the SQL query againstz--sqlTz|The SQL query to issue to the database. Cloud Spanner SQL is described at https://cloud.google.com/spanner/docs/query-syntax)requiredhelpzLReturns only the query result, without any information about the query plan.zYReturns only the query plan, without any result rows or execution statistics information.zvReturns the query plan, overall execution statistics, operator-level execution statistics, along with the result rows.zYReturns the overall (but not operator-level) execution statistics along with the results.zfReturns the query plan, overall (but not operator-level) execution statistics, along with the results.)NORMALPLANPROFILE
WITH_STATSWITH_PLAN_AND_STATSz--query-moder%   c                 "    U R                  5       $ N)upper)xs    r   <lambda>AddBaseArgs.<locals>.<lambda>g   s
    QWWYr!   z*Mode in which the query must be processed.)defaulttypechoicesr$   z--enable-partitioned-dml
store_truez+Execute DML statement using Partitioned DML)actionr$   z	--timeout10mzuMaximum time to wait for the SQL query to complete. See $ gcloud topic datetimes for information on duration formats.)r1   r0   r$   spannerv1zuRead-only query timestamp bound. The default is --strong. See https://cloud.google.com/spanner/docs/timestamp-bounds.)mutexr$   --strongzPerform a strong query.--read-timestamp	TIMESTAMPz'Perform a query at the given timestamp.)metavarr$   z--database-rolez8Database role user assumes while accessing the database.)r$   N)r   AddDatabaseResourceArgadd_argumentr   Durationr   GetMessagesModuleGetRequestPriorityMapper
choice_argAddToParseradd_argument_group)parserquery_mode_choicesmsgstimestamp_bound_groups       r   AddBaseArgsrI   ?   so    &&v'IKH  I.M$0%. 	 7  9 	 8  :
 	!B	  C 
			4	0$4 ++77? 33E 4 F $$$ % & $$4 % 6 	E  Gr!   c           	      b    [         R                  " SU R                  R                  SSSSS.SS9$ )	Nz
--prioritylowmediumhighunspecified)PRIORITY_LOWPRIORITY_MEDIUMPRIORITY_HIGHPRIORITY_UNSPECIFIEDz)The priority for the execute SQL request.)custom_mappingshelp_str)r   ChoiceEnumMapperRequestOptionsPriorityValueValuesEnum)messagess    r   rA   rA      s=    		#	#55%!"/	 ;

 
r!   c                   >    \ rS rSrSr\r\S 5       rS r	S r
S rSrg)	Query   z6Executes a SQL query against a Cloud Spanner database.c                     [        U 5        g)zSee base class.N)rI   )rE   s    r   Args
Query.Args   s     r!   c           	         [         R                  " SS5      nUR                  [        U5      R	                  UR
                  5      S9nU R                  U5      n[        XR                  5      n [        R                  " UR                  UR                  UUUUR                  UR                  5      [        R                  " U5        $ ! [        R                  " U5        f = f)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.
r6   r7   )priority)r   r@   rV   rA   GetEnumForChoicer`   ParseReadOnlyOptionsr    r   r   
ExecuteSqlr   
query_modeenable_partitioned_dmltimeoutDelete)selfr   rG   request_optionsread_only_optionssessions         r   Run	Query.Run   s     !!)T2D)))$/@@O * O 11$7D"4"45G
())
((
//




%
%
,, w'w's   0AC
 
C"c                 0   [        UR                  5      (       a[  UR                  S5      (       a  [        R                  " SS5      eUR                  S5      (       a  [        R                  " SS5      eg[
        R                  " SS5      nUR                  S5      (       a  UR                  UR                  S	9$ UR                  S5      (       a7  UR                  (       d  [        R                  " SS
5      eUR                  SS9$ UR                  SS9$ )zParses the options for a read-only request from command line arguments.

Args:
  args: Command line arguments.

Returns:
  A ReadOnly message if the query is read-only (not DML), otherwise None.
strongr9   z:A timestamp bound cannot be specified for a DML statement.read_timestampr:   Nr6   r7   )readTimestampzW`--strong` cannot be set to false. Instead specify a different type of timestamp bound.T)ro   )
r   r   IsSpecifiedc_exceptionsInvalidArgumentExceptionr   r@   ReadOnlyrp   ro   )rh   r   rG   s      r   rb   Query.ParseReadOnlyOptions   s    488			(	#	#33H
 	
 
		*	+	+33H
 	
 ##It4d			*	+	+}}4+>+>}??H%%{{55E  d+
+ }}D}))r!   c                 0   UR                   S:H  =(       d%    UR                   S:H  =(       d    UR                   S:H  nUR                   S:g  n[        R                  " U5      (       a9  [        R                  " UR                  R
                  [        R                  5        U(       a%  [        R                  " U[        R                  5        U(       aE  [        R                  " X!R                   S:X  a  [        R                  O[        R                  5        gg)a  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.
'NORMAL': query result rows
'PLAN': query plan without execution statistics
'PROFILE': query result rows and the query plan with execution statistics

Args:
  args: The arguments originally passed to the command.
  result: The output of the command before display.
r&   r'   r)   N)rd   r   QueryHasAggregateStatsDisplayQueryAggregateStatsstats
queryStatsr   outDisplayQueryPlanDisplayQueryResultsstatus)rh   r   resultdisplay_plandisplay_resultss        r   DisplayQuery.Display   s     	6! 	4??i'	4??33 
 oo/O
!!&))	$$V\\%<%<cggF	6377+	
9 <#**#'' r!    N)__name__
__module____qualname____firstlineno____doc__DETAILED_HELPdetailed_helpstaticmethodr]   rl   rb   r   __static_attributes__r   r!   r   rZ   rZ      s.     ?- (8$*Lr!   rZ   r+   ) r   
__future__r   r   r   googlecloudsdk.api_lib.spannerr   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   r	   r
   rs   "googlecloudsdk.command_lib.spannerr   r   &googlecloudsdk.command_lib.spanner.sqlr   $googlecloudsdk.command_lib.util.apisr   googlecloudsdk.corer   r   r   r    rI   rA   DefaultUniverseOnlyUnicodeIsSupportedCommandrZ   r   r!   r   <module>r      s    + &  ' < , / ( > < 2 > : # ) 	B$IGX eDLL e  er!   