
    u                         S r SSKrSSKJrJr  SSKJr  SSKJr  SSKrSSK	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  \R,                  r " S S\R.                  5      rg)zThe BQ CLI `insert` command.    N)OptionalTextIO)app)flags)client_table)utils)bigquery_command)bq_cached_client)bq_error)bq_id_utils)bq_processor_utils)
stringutilc                      ^  \ rS rSrSrS\S\R                  4U 4S jjr SS\S\	\   S\	\
   4S	 jjr    SS\S
\S\	\   S\	\   S\	\
   S\	\   S\
4S jjrSrU =r$ )Insert   z8insert [-s] [-i] [-x=<suffix>] <table identifier> [file]namefvc                   > [         [        U ]  X5        [        R                  " SS SSUS9  [        R                  " SS SSUS9  [        R
                  " SS S	S
US9  [        R
                  " SS SUS9  U R                  U5        g )Nskip_invalid_rowszCAttempt to insert any valid rows, even if invalid rows are present.s)
short_nameflag_valuesignore_unknown_valuesz>Ignore any values in a row that are not present in the schema.itemplate_suffixzIf specified, treats the destination table as a base template, and inserts the rows into an instance table named "{destination}{templateSuffix}". BigQuery will manage creation of the instance table, using the schema of the base template table.x	insert_ida  Used to ensure repeat executions do not add unintended data. A present insert_id value will be appended to the row number of each row to be inserted and used as the insertId field for the row. Internally the insertId field is used for deduping of inserted rows.)r   )superr   __init__r   DEFINE_booleanDEFINE_string_ProcessCommandRc)selfr   r   	__class__s      &platform/bq/frontend/command_insert.pyr   Insert.__init__   s    	&$ *	M 
H 
	G 	 
	O  	2    
identifierfilenamereturnc           
      j   U(       aR  [        US5       nU R                  UUU R                  U R                  U R                  U R
                  S9sSSS5        $ U R                  U[        R                  U R                  U R                  U R                  U R
                  S9$ ! , (       d  f       g= f)a  Inserts rows in a table.

Inserts the records formatted as newline delimited JSON from file
into the specified table. If file is not specified, reads from stdin.
If there were any insert errors it prints the errors to stdout.

Examples:
  bq insert dataset.table /tmp/mydata.json
  echo '{"a":1, "b":2}' | bq insert dataset.table

Template table examples:
Insert to dataset.table_suffix table using dataset.table table as its
template.
  bq insert -x=_suffix dataset.table /tmp/mydata.json
r)r   r   r   r   N)open	_DoInsertr   r   r   r   sysstdin)r#   r(   r)   	json_files       r%   RunWithArgsInsert.RunWithArgsD   s    $ #)~~"44"&"<"< 00nn  
  ^^

)) 22 $ : :..NN    s   <B$$
B2r1   r   r   r   r   c                   ^^^^^^ [         R                  R                  5       m[        R                  " TUS9m[
        R                  " T[
        R                  R                  4SSS9  / mUUUUUU4S jn0 nSn	Sn
U H  n SnUb  US-   [        U
5      -   nTR                  [        R                  " X5      5        U
S-  n
["        R$                  (       a&  ['        T5      ["        R$                  :X  a	  U" 5       u  pU	(       d  M    O   T(       a  U	(       d	  U" 5       u  p[(        R*                  R,                  S
;   a  [.        R0                  " U5        O[(        R*                  R,                  S;   a  U	(       a  US    H  nUS   n[2        R4                  R7                  SUS   4-  5        U HP  n[9        S[:        R<                  " US   5      < S[:        R<                  " UR?                  S5      5      < 35        MR     M     U	(       a  S$ S$ ! [        R                   a)  n[        R                   " S	U
[        U5      4-  5      eSnAff = f)z-Insert the contents of the file into a table.)id_fallbacksr(   z+Must provide a table identifier for insert.T)is_usage_errorc            	         > [         R                  " TR                  5       TTTTTS9n TS S 2	 X R                  SS 5      4$ )N)insert_client
table_dictinsertsr   r   r   insertErrors)r   insert_table_rowsGetInsertApiClientget)resultbatchclientr   	referencer   r   s    r%   FlushInsert._DoInsert.<locals>.Flush   sK    --113- 5)f (ZZ555r'   N   _zLine %d: %s)
prettyjsonjson)Nsparseprettyr;   errorszrecord %d errors: index	reasonz: messager   ) r
   ClientGetbq_client_utilsGetReferencer   	typecheckApiClientHelperTableReferencestrappendr   JsonToInsertEntryr   BigqueryClientErrorr   
UsageErrorFLAGSmax_rows_per_requestlenbq_flagsFORMATvaluebq_utilsPrintFormattedJsonObjectr/   stdoutwriteprintr   
ensure_strr>   )r#   r(   r1   r   r   r   r   rC   r?   rK   linenolineunique_insert_ideentryentry_errorserrorr@   rA   rB   s      ```           @@@r%   r.   Insert._DoInsertj   s    $$((*F,,
I 		$	$	3	355	 E
6 
6 FFF	? &_s6{:
001AH	
 	! 
$
$%jE666	# $ Vwnf 66''/			"<	<	N+Ex,
**

/5>2CC
D#e ))%/:))%))I*>? $ , 1A7 )) ?nn]fc!f-==>>?s   A HI($II ) N)NNNN)__name__
__module____qualname____firstlineno__usagerW   r   
FlagValuesr   r   intr2   r   boolr.   __static_attributes____classcell__)r$   s   @r%   r   r      s    
H%#3 #E$4$4 #L =A$$,4SM$}$T +/.2'+!%II I "$	I
 &d^I  }I #I I Ir'   r   )__doc__r/   typingr   r   abslr   r   r_   rb   clientsr   r   rR   frontendr	   r
   r   r   r   pyglibr   r\   BigqueryCmdr   rp   r'   r%   <module>r      sP    " 
 #       , % %   $ W)) Wr'   