
    ¿#jb                    pB   d dl Z d dlZd dlmZ d dlmZmZ d dlZd dlZd dlm	Z	 d dl
Z
d dlmZmZmZ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Zd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl Z d	d
l!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. 	 d dl/Z/ e1ddg      Z2dZ3dji                         Z5 e         G d dejl                        Z7d Z8d Z9 ejt                  edde2       ejv                         d               Z<e/  e/jz                         e<      Z<e<j}                          ej~                  d ej                  dddd      d       ej                  dddd       ej                  dd dd       ej                  d!d"dd      e8 ej                  d#d$dd%       ej                  d&d'dd%      e9	 dd(                                                               ZBe<j}                          ej~                  d ej                  dddd      d       ej                  d!d)dd      e8 ej                  d#d*dd%       ej                  d&d+dd%      e9d,                                                  ZCe<j}                          ej~                  d ej                  dddd      d       ej~                  d-d./       ej                  d0d1dd      e9d2                                    ZDe<j}                  d34       ej~                  d ej                  dddd      d       ej~                  d-d./      e9d5                             ZEe<j}                          ej~                  d ej                  dddd      d       ej~                  d6d./      d7                      ZFe<j}                          ej~                  d ej                  dddd      d      d8               ZGe<j}                          ej~                  d ej                  dddd      d      e9d9                      ZHe<j}                  d:4       ej~                  d ej                  dddd      d       ej~                  d;       ej~                  d<       ej~                  d= ejl                  g d>d?      d       ej                  d@eIddAB       ej                  dCeIddDB       ej                  dEeIddFB       ej                  dGddHI      e9dJ                                                                       ZJe<j}                  dK4       ej~                  d ej                  dddd      d       ej~                  d;       ej~                  dL       ej~                  dMdN       ej~                  dOdN       ej                  dGddPI      e9dQ                                                         ZKe<j}                  dR4       ej~                  d ej                  dddd      d       ej~                  dSd./      e9dT                             ZLe<j}                  dU4       ej~                  d ej                  dddd      d      e9dV                      ZMe<j}                  dW4       ej~                  d ej                  dddd      d       ej~                  d;       ej~                  dLd.dX       ej                  dYdZ[       ej                  d\d]dd       ej                  d^dGd_dd       ej                  d`dadb      e9dc                                                                ZNe<j}                  dd4       ej~                  d ej                  dddd      d       ej~                  d;       ej~                  dLd.dX       ej                  ddedd       ej                  ddfdd       ej                  dgdh[       ej                  didjdd       ej                  dkddlI      e9dm                                                                       ZOe<j}                  dn4       ej~                  d ej                  dddd      d       ej~                  d;       ej~                  dLd.dX      e9do                                    ZPe<j}                  dp4       ej~                  d ej                  dddd      d       ej~                  d;      e9dq                             ZQe<j}                  dr4       ej~                  dd. ej                  dddd      ds      e9dt                      ZRe<j}                  du4       ej~                  dd. ej                  dddd      ds      e9dv                      ZSe<j}                  dw4       ej~                  d ej                  dddd      d       ej~                  d-d./      e9dx                             ZTe<j}                  dy4       ej~                  d ej                  dddd      d      e9dz                      ZU ej                  d{dd|I       ej                  d}dd~I       ej                  ddddI       ej                  dddI       ej                  dddI       ej                  dddI       ej                  dddI       ej                  dd[       ej                  ddeIdd       ej                  dd[       ej                  dd[       ej                  dddI       ej                  dddI       ej                  dd[      fZVd ZWdddZX	 	 	 	 	 	 	 	 	 	 	 	 ddZYd ZZe<j}                          eX        ej                  dGddd       ej                  dkddd       ej                  dddd      d                                    Z[e<j}                          eXd      d               Z\e<j}                          ej~                  d ej                  ddd      d       ej~                  d       ej~                  d ej                  d      d       ej                  de^dd       ej                  ddd      eWe9d                                                         Z_e<j}                  d4       ej~                  d ej                  ddd      d       ej                  dddI       ej                  dddI      e9d                                    Z`e<j}                  d4       ej~                  d ej                  ddd      d       ej~                  d;       ej~                  dd.dX       ej                  dddd       ej                  ddd       ej                  ddeIeIfd«       ej                  d@deIeIeIfdì«       ej                  dGddĬI       ej                  dkddŬI       ej                  dddǬI      e9 ej                  dddɬI      dʄ                                                                                            Zae<j}                  dˬ4       ej~                  d ej                  ddd      d       ej~                  d;       ej~                  d̫       ej                  dGddͬI      e9d΄                                           Zbe<j}                  dϬ4       ej~                  d ej                  ddd      d       ej~                  d;       ej~                  dЫ       ej                  dGddͬI      e9dф                                           Zce<j}                  dҬ4       ej~                  d ej                  ddd      d       ej~                  d;       ej                  dGddͬI      e9dӄ                                    Zde<j}                  dԬ4       ej~                  d ej                  ddd      d       ej~                  dի       ej~                  d֫       ej                  dGdd׬I       ej                  dkddجI      e9dل                                                  Zee<j}                  dڬ4       ej~                  d ej                  ddd      d       ej~                  dի       ej                  dGdd۬I      e9d܄                                    Zfe<j}                          ej~                  d ej                  ddd      d       ej~                  d       ej                  deI ej                  ddd      fddެ      e8 ej                  ddddI       ej                  dddI       ej                  dddeIeIfd«       ej                  ddd      e9d                                                                       Zge<j}                          ej~                  d ej                  ddd      dd.       ej~                  d       ej                  ddd       ej                  deI ej                  ddd      fddެ       ej                  d{ddI      e8 ej                  ddddI       ej                  dddI       ej                  dddeIeIfd«       ej                  dd[       ej                  ddddI       ej                  d&ddI       ej                  dddI       ej                  d ej                  ddd      d       ej                  d`ddI      e9	 dd                                                                                                                       Zhd Zie<j}                          ej~                  d ej                  ddd      d       ej~                  d       ej~                  d       ej                  ddeId       ej                  ddeIdd       ej                  de^d        ej                  ddddI       ej                  dddI      e8e9ej                  d                                                                                     Zke<j}                          ej~                  d ej                  ddd      d       ej~                  d       ej                  ddeIdd       ej                  dd[       ej                  ddeId       ej                  dddeIeIfd	«       ej                  de^d        ej                  d
e^d      e8e9ej                  d                                                                                     Zle<j}                          ej~                  d ej                  ddd      d       ej~                  d-d./      e8e9ej                  d                                           Zme<j}                          ej~                  d ej                  ddd      d       ej~                  d-d./       ej                  dddI      e8e9ej                  d                                                  Zne<j}                          ej~                  d ej                  ddd      d       ej~                  d-d.dX      e9d                             Zoe<j}                          ej~                  d ej                  ddd      d       ej~                  d;       ej                  deI ejl                  g dd?      fdd       ej                  deIdd       ej                  deIeIfdd       ej                  ddeIdd       ej                  deIdd       ej                  deIdd       ej                  deIdd       ej                  dddI       ej                  deIeIfdd        ej                  d!eIdd"       ej                  d#d$eIeIeIfdd%       ej                  d&d'eIdd(       ej                  ddd)I      e9d*                                                                                                                        Zpe<j}                          ej~                  d ej                  ddd      d       ej~                  d;       ej~                  dd.dX       ej                  d+dMd,[       ej                  d-d.[       ej                  deIeIfdd/      e9d0                                                         Zqe<j}                  d14       ej~                  d ej                  ddd      d       ej~                  d;       ej~                  d2d.d ej                  ddd      3       ej                  ddeIdd4       ej                  dddd       ej                  d5dd6I       ej                  dkdd7I       ej                  d8dd9I       ej                  dYeId:       ej                  ddd;I       ej                  dd[       ej                  d<d=dd>I      e9d?                                                                                                   Zre<j}                  d@4       ej~                  d ej                  ddddA      d       ej~                  d-d./       ej                  dddeIddB       ej                  dddCI       ej                  dDe^dEdF       ej                  dGdddH       ej                  dIdddJ      e9dK                                                                ZsddLZte<j}                          ej~                  dMd.dX       ej                  dNdOddPI       ej                  dQdRdS[      dT                             Zue<j}                          ej~                  dMd.dX       ej                  dUdVddWI      dX                      ZvdY Zwe<j}                   ew       [       ej~                  dZ ej                  ddd      d       ej~                  d;eI[       ej~                  deId.d\       ej~                  d]eI[       ej                  ddeIdd       ej                  d^dd_I       ej                  d`ddaI       ej                  dd[       ej                  dddeIeIfd	«       ej                  dbdc[       ej                  dddedf ejl                  g dg      h       ej                  dddiI       ej                  djddkI       ej                  d<d=dd>I       ej                  dldmddnI      do                                                                                                                 Zxe<j}                  dp       ej~                  dZ ej                  ddd      d       ej~                  d;eI[       ej~                  dqeI[       ej                  drdds ejl                  g dtd?      dudvdw       ej                  dxe^dyddz{       ej                  d|d}eId~d       ej                  ddddI      e9d                                                                Zye<j}                  d       ej~                  dZ ej                  ddd      d       ej~                  d;eI[       ej~                  dqeI[      e9d                                    Zze<j}                  d4      d        Z{ej                  j                  e<       d Z~ G d de      Zi dd dd dd dd dd dd dd dd dd dd dd dd dd dd dd dd Zd Zd Zd Zd Zd Zd Zd Zy# e0$ r dZ/Y  w xY w(      N)DefaultGroup)datetimetimezone)
run_module)
AlterErrorBadMultiValues	DescIndexNoTable)pmget_plugins)maximize_csv_field_size_limit)recipes   )OperationalError_compile_codechunksfile_progressfind_spatialiteflattensqlite3decode_base64_valuesprogressbarrows_from_fileFormatTypeTrackerz-hz--help)help_option_names)INTEGERTEXTFLOATBLOBa  
{}

The input you provided uses a character encoding other than utf-8.

You can fix this by passing the --encoding= option with the encoding of the file.

If you do not know the encoding, running 'file filename.csv' may tell you.

It's often worth trying: --encoding=latin-1
c                   (     e Zd Z fdZ fdZ xZS )CaseInsensitiveChoicec                 f    t         |   |D cg c]  }|j                          c}       y c c}w N)super__init__lower)selfchoiceschoice	__class__s      f/home/mi/.openclaw/workspace/fridge-app/backend/.venv/lib/python3.12/site-packages/sqlite_utils/cli.pyr&   zCaseInsensitiveChoice.__init__@   s$    w?V&,,.?@?s   .c                 B    t         |   |j                         ||      S r$   )r%   convertr'   )r(   valueparamctxr+   s       r,   r.   zCaseInsensitiveChoice.convertC   s    wu{{}eS99    )__name__
__module____qualname__r&   r.   __classcell__)r+   s   @r,   r"   r"   ?   s    A: :r2   r"   c                    t        t        j                  dddd      t        j                  dddd      t        j                  ddd	
      t        j                  ddd
      t        j                  ddd
      t        j                  dddd
      t        j                  ddj                  dj	                  t
        j                                    t        j                  dddd      f      D ]
  } ||       }  | S )N--nlzOutput newline-delimited JSONTFhelpis_flagdefaultz--arraysz(Output rows as arrays instead of objects--csvz
Output CSVr;   r:   --tsvz
Output TSV--no-headerszOmit CSV headers-t--tablezOutput as a formatted tablez--fmtzTable format - one of {}, r:   z--json-colsz=Detect JSON cols and output them as JSON, not escaped strings)reversedclickoptionformatjointabulatetabulate_formatsfn	decorators     r,   output_optionsrO   G   s    LL4	 LL?	 LL$\BLL$\BLL<NOLLi4Q LL/66IIh778 LLT	3	
! "	D r]E"F Ir2   c                 >     t        j                  ddd      |       S )Nz--load-extensionTz3Path to SQLite extension, with optional :entrypointmultipler:   )rF   rG   )rM   s    r,   load_extension_optionrS   n   s*    5<<B 		
 
r2   queryT)clsr<   default_if_no_argscontext_settingsc                       y)z/Commands for interacting with a SQLite databaseN rY   r2   r,   clirZ   v   s     	r2   pathF)exists	file_okaydir_okay
allow_dashtyperequiredz--fts4zJust show FTS4 enabled tables)r:   r<   r;   z--fts5zJust show FTS5 enabled tablesz--countszInclude row counts per tablez	--columnsz&Include list of columns for each tabler9   z--schemazInclude schema for each tablec                 d   t        j                  |       t        |       rdndg}r|j                  d       r|j                  d       r|j                  d       fd}|	s|
r+t	        t        j
                   |       ||
xs d             ys|rZt        j                  t        j                  |rd	nd
      }|s|j                  |        |       D ]  }|j                  |        yt         |       ||||      D ]  }t        j                  |        y)z^List the tables in the database

    Example:

    
        sqlite-utils tables trees.db
    viewtablecountcolumnsschemac               3     K   rj                         } nj                  	
      } | D ]  }|g}r|j                  |   j                         rZ|   j                  D cg c]  }|j
                   }}r!|j                  dj                  |             n|j                  |       r|j                  |   j                         |  y c c}w w)N)fts4fts5
)
view_namestable_namesappendrf   rg   namerI   rh   )itemsrp   rowccolsrg   countscsvdbrj   rk   rh   viewss        r,   _iterztables.<locals>._iter   s     MMOENN4N8E 	D&C

2d8>>*(*4(8(89199JJtyy/JJt$

2d8??+I	
 :s   A"C%C8A"Csimpleheaderstablefmt	excel-tabexceldialectN)sqlite_utilsDatabase_load_extensionsro   printrJ   csv_stdwritersysstdoutwriterowoutput_rowsrF   echo)r[   rj   rk   ru   nlarraysrv   tsv
no_headersre   fmt	json_colsrg   rh   load_extensionrx   r|   ry   r   rr   linerw   s    ```  `     `` `     @r,   tablesr      s    l 
		t	$BR(vG,Gwy!x  & h3?(ST	

3KGTOOG$7 	!COOC 	!  "fiH 	DJJt	r2   zInclude row counts per viewz%Include list of columns for each viewzInclude schema for each viewc                 N    t         j                  | dd|||||||||	|
||d       y)z\List the views in the database

    Example:

    
        sqlite-utils views trees.db
    FT)r[   rj   rk   ru   r   r   rv   r   r   re   r   r   rg   rh   r   rx   N)r   callback)r[   ru   r   r   rv   r   r   re   r   r   rg   rh   r   s                r,   rx   rx      sF    Z OO%!  r2   r   )nargsz--no-vacuumzDon't run VACUUMc                 2   t        j                  |       }t        ||       |s%|j                  d      |j                  d      z   }|j                  5  |D ]  }||   j                           	 ddd       |s|j                          yy# 1 sw Y   xY w)zOptimize all full-text search tables and then run VACUUM - should shrink the database file

    Example:

    
        sqlite-utils optimize chickens.db
    Trj   rk   N)r   r   r   rn   connoptimizevacuum)r[   r   	no_vacuumr   rw   re   s         r,   r   r   '  s    " 
		t	$BR(T*R^^^-FF	 ! 	!EuI 	!! 
		 ! !s   BBzrebuild-ftsrp   c                    t        j                  |       }t        ||       |s%|j                  d      |j                  d      z   }|j                  5  |D ]  }||   j                           	 ddd       y# 1 sw Y   yxY w)zRebuild all or specific full-text search tables

    Example:

    
        sqlite-utils rebuild-fts chickens.db chickens
    Tr   r   N)r   r   r   rn   r   rebuild_ftsr[   r   r   rw   re   s        r,   r   r   C  s|      
		t	$BR(T*R^^^-FF	 $ 	$EuI!!#	$$ $ $s   A::Bnamesc                     t        j                  |       }	 |r|D ]  }|j                  |        y|j                          y# t        $ r}t	        j
                  |      d}~ww xY w)zRun ANALYZE against the whole database, or against specific named indexes and tables

    Example:

    
        sqlite-utils analyze chickens.db
    N)r   r   analyzer   rF   ClickException)r[   r   rw   rp   es        r,   r   r   \  sc     
		t	$B& !

4 ! JJL &""1%%&s   A A 	A&A!!A&c                 J    t        j                  |       j                          y)zaRun VACUUM against the database

    Example:

    
        sqlite-utils vacuum chickens.db
    N)r   r   r   )r[   s    r,   r   r   v  s     $&&(r2   c                     t        j                  |       }t        ||       |j                         D ]  }t	        j
                  |        y)zOutput a SQL dump of the schema and full contents of the database

    Example:

    
        sqlite-utils dump chickens.db
    N)r   r   r   iterdumprF   r   )r[   r   rw   r   s       r,   dumpr     s?     
		t	$BR( 

4r2   z
add-columnre   col_namecol_type)integerintfloattextstrblobbytes)case_sensitivez--fkz#Table to reference as a foreign key)ra   rb   r:   z--fk-colz_Referenced column on that foreign key table - if omitted will automatically use the primary keyz--not-null-defaultz&Add NOT NULL DEFAULT 'TEXT' constraintz--ignorez$If column already exists, do nothingr>   c	                     t        j                  |       }	t        |	|       	 |	|   j                  |||||       y# t        $ r*}
|st        j                  t        |
            Y d}
~
yd}
~
ww xY w)zAdd a column to the specified table

    Example:

    
        sqlite-utils add-column chickens.db chickens weight float
    )fkfk_colnot_null_defaultN)r   r   r   
add_columnr   rF   r   r   )r[   re   r   r   r   r   r   ignorer   rw   exs              r,   r   r     sv    n 
		t	$BR(0
5	h2fGW 	 	
  0&&s2w// 0s   = 	A0 A++A0zadd-foreign-keycolumnother_table)rb   other_columnz)If foreign key already exists, do nothingc                     t        j                  |       }t        ||       	 ||   j                  ||||       y# t        $ r}t        j                  |      d}~ww xY w)z
    Add a new foreign key constraint to an existing table

    Example:

        sqlite-utils add-foreign-key my.db books author_id authors id
    r   N)r   r   r   add_foreign_keyr   rF   r   )	r[   re   r   r   r   r   r   rw   r   s	            r,   r   r     s_    4 
		t	$BR(&
5	!!&+|F!S &""1%%&s   < 	AAAzadd-foreign-keysforeign_keyc           	         t        j                  |       }t        ||       t        |      dz  dk7  rt	        j
                  d      g }t        t        |      dz        D ](  }|j                  t        ||dz  |dz  dz                 * 	 |j                  |       y# t        $ r}t	        j
                  |      d}~ww xY w)z
    Add multiple new foreign key constraints to a database

    Example:

    
        sqlite-utils add-foreign-keys my.db \
            books author_id authors id \
            authors country_id countries id
       r   zOEach foreign key requires four values: table, column, other_table, other_columnN)r   r   r   lenrF   r   rangero   tupleadd_foreign_keysr   )r[   r   r   rw   tuplesir   s          r,   r   r      s    & 
		t	$BR(
;!q ""]
 	
 F3{#q() ?eKAQ!<=>?&
F# &""1%%&s   B 	C(B==Czindex-foreign-keysc                 f    t        j                  |       }t        ||       |j                          y)z
    Ensure every foreign key column has an index on it

    Example:

    
        sqlite-utils index-foreign-keys chickens.db
    N)r   r   r   index_foreign_keysr[   r   rw   s      r,   r   r   "  s*      
		t	$BR(r2   zcreate-index)r   rb   z--namezExplicit name for the new indexrD   z--uniquezMake this a unique indexz--if-not-existszIgnore if index already exists	--analyzez$Run ANALYZE after creating the index)r:   r;   c                     t        j                  |       }t        ||       g }	|D ]2  }
|
j                  d      rt	        |
dd       }
|	j                  |
       4 ||   j                  |	||||       y)a  
    Add an index to the specified table for the specified columns

    Example:

    
        sqlite-utils create-index chickens.db chickens name

    To create an index in descending order:

    
        sqlite-utils create-index chickens.db chickens -- -name
    -r   N)
index_nameuniqueif_not_existsr   )r   r   r   
startswithr	   ro   create_index)r[   re   r   rp   r   r   r   r   rw   rg   cols              r,   r   r   7  s    N 
		t	$BR(G >>#CG$Cs uI#  r2   z
enable-ftszUse FTS4zUse FTS5z
--tokenizezTokenizer to use, e.g. porterz--create-triggerszGCreate triggers to update the FTS tables when the parent table changes.z	--replacez/Replace existing FTS configuration if it existsc	                 
   d}	|r|rt        j                  dd       y|rd}	t        j                  |       }
t	        |
|       	 |
|   j                  ||	|||       y# t        $ r}t        j                  |      d}~ww xY w)zEnable full-text search for specific table and columns"

    Example:

    
        sqlite-utils enable-fts chickens.db chickens name
    FTS5z$Can only use one of --fts4 or --fts5T)errNFTS4)fts_versiontokenizecreate_triggersreplace)rF   r   r   r   r   
enable_ftsr   r   )r[   re   r   rj   rk   r   r   r   r   r   rw   r   s               r,   r   r   o  s    B K

9tD				t	$BR(	'
5	#+ 	 	
  '""2&&'s   A 	B(A==Bzpopulate-ftsc                 n    t        j                  |       }t        ||       ||   j                  |       y)zRe-populate full-text search for specific table and columns

    Example:

    
        sqlite-utils populate-fts chickens.db chickens name
    N)r   r   r   populate_fts)r[   re   r   r   rw   s        r,   r   r     s0    " 
		t	$BR(uI6"r2   zdisable-ftsc                 l    t        j                  |       }t        ||       ||   j                          y)z{Disable full-text search for specific table

    Example:

    
        sqlite-utils disable-fts chickens.db chickens
    N)r   r   r   disable_fts)r[   re   r   rw   s       r,   r   r     s.      
		t	$BR(uIr2   z
enable-wal)r   ra   rb   c                 t    | D ]3  }t        j                  |      }t        ||       |j                          5 y)zcEnable WAL for database files

    Example:

    
        sqlite-utils enable-wal chickens.db
    N)r   r   r   
enable_walr[   r   path_rw   s       r,   r   r     s6       ""5)^,
r2   zdisable-walc                 t    | D ]3  }t        j                  |      }t        ||       |j                          5 y)zeDisable WAL for database files

    Example:

    
        sqlite-utils disable-wal chickens.db
    N)r   r   r   disable_walr   s       r,   r   r     s7       ""5)^,
r2   zenable-countsc                 <   t        j                  |       }t        ||       |s|j                          y|D cg c]  }||   j	                         r| }}|r$t        j                  dj                  |            |D ]  }||   j                           yc c}w )zConfigure triggers to update a _counts table with row counts

    Example:

    
        sqlite-utils enable-counts chickens.db
    zInvalid tables: {}N)r   r   r   enable_countsr\   rF   r   rH   )r[   r   r   rw   re   
bad_tabless         r,   r   r     s      
		t	$BR(
 *0Jr%y7G7G7IeJ
J&&';'B'B:'NOO 	&EuI##%	& Ks   BBzreset-countsc                 f    t        j                  |       }t        ||       |j                          y)ztReset calculated counts in the _counts table

    Example:

    
        sqlite-utils reset-counts chickens.db
    N)r   r   r   reset_countsr   s      r,   r   r     s(     
		t	$BR(OOr2   z	--flattenzBFlatten nested JSON objects, so {"a": {"b": 1}} becomes {"a_b": 1}r8   zExpect newline-delimited JSONz-cr=   zExpect CSV inputr?   zExpect TSV inputz--empty-nullzTreat empty strings as NULLz--linesz/Treat each line as a single value called 'line'z--textz+Treat input as a single value called 'text'z	--convertz Python code to convert each itemz--importimportszPython modules to import)ra   rR   r:   z--delimiterzDelimiter to use for CSV filesz--quotecharz"Quote character to use for CSV/TSVz--sniffz$Detect delimiter and quote characterr@   zCSV file has no header rowz
--encodingz/Character encoding for input, defaults to utf-8c                 >    t        t              D ]
  } ||       }  | S r$   )rE   _import_optionsrL   s     r,   import_optionsr   T  s$    o. 	r]Ir2   )
require_pkc                       fd}|S )Nc                 (   t        t        j                  dt        j                  ddd      d      t        j                  d      t        j                  dt        j                  d      d      t        j
                  d	d
d      ft        z   t        j
                  dt        dd      t        j
                  dt        d      t        j
                  ddd      t        j
                  ddd      t        j
                  ddt        t        fd      t        j
                  ddddd       t        j
                  d!dd"      t        t        j
                  d#dd$      t        j
                  d%ddd&'      f
z         D ]
  } ||       }  | S )(Nr[   TFr]   r^   r_   r`   re   filerb--pkz*Columns to use as the primary key, e.g. id)r:   rR   rb   --batch-sized   Commit every X recordsra   r<   r:   z--stop-afterzStop after X recordsra   r:   --alterz/Alter existing table to add any missing columnsr>   
--not-null*Columns that should be created as NOT NULLrQ   	--default-Default value that should be set for a columnrR   ra   r:   z-dz--detect-typesSQLITE_UTILS_DETECT_TYPESz(Detect types for columns in CSV/TSV data)r;   envvarr:   r   z(Run ANALYZE at the end of this operation--silentzDo not show progress bar--strict"Apply STRICT mode to created tabler;   r<   r:   )
rE   rF   argumentPathFilerG   r   r   r   rS   )rM   rN   r   s     r,   innerz$insert_upsert_options.<locals>.inner[  s~   !dUuU!
 w'vEJJt,<tLE!'	 " "c@X ^#<RS J
  !E
 !sH	 $ 6C  C
 &Z<VW !=	G)!9;
 <	Ix 2By<	z 	r2   rY   )r   r  s   ` r,   insert_upsert_optionsr  Z  s    >@ Lr2   c"           
      
  78 t        j                  |       }"t        |"|       | rt        |"|        |s|s|s|r|sd}||z   |z   dk\  rt	        j
                  d      |s|r|rt	        j
                  d      |r|s|st	        j
                  d      |r|s|st	        j
                  d      |rt        |      dk(  r|d   }|xs d	}d }#d }$|r/t        j                  |d
      }#t        j                  |#|      }$nt        j                  ||      }$d }%t        |$|      5 }&|s|r|rE|#j                  d      }'t        j                         j                  |'j                  |d            }(n|rdnd}(d|(i})|r||)d<   |r||)d<   t        j                   |&fi |)}*t#        |*      }+|rLt%        t        |+            D ,cg c]  },dj'                  |,dz          c},8t)        j*                  |+g|*      }*n|+8|r8fd|*D        }-n8fd|*D        }-|r~t-               }%|%j/                  |-      }-nb|	r
d |&D        }-nV|
rd|&j1                         if}-n@	 |r
d |&D        }-n(t3        j4                  |&      }-t7        |-t8              r|-g}-|r	d |-D        }-|rt)        j>                  |-|      }-|rnd}/|	rd}/n|
rd}/tA        |||/      7|	r7fd |-D        }-nG|
r:tC        |-      d   d   }0 7|0      }1t7        |1t8              r|1g}-n	 tE        |1      }-n7fd"|-D        }-|||||!d#}2|rtI        |      |2d$<   |rt9        |      |2d%<   |r||2d&<   d' |-D        }-d( |-D        }-|rb|rtK        |-|      }3n|-g}3|3D ]A  }4|"jL                  5  |"jL                  jO                         jQ                  ||4       d d d        C 	 d d d        y 	  |"|   jR                  |-f|||d)|2 |%|"|   ja                  |%jb                  /       |#r|#je                          |$r|$je                          d d d        y c c},w # t2        j:                  j<                  $ r)}.t	        j
                  dj'                  |.            d }.~.ww xY w# tF        $ r t	        j
                  d!      w xY w# 1 sw Y   1xY w# tT        $ r}5t7        |5tV              rN|5jX                  rBd*|5jX                  d   v r1t	        j
                  d+j'                  |5jX                  d               t[        |5j\                  d,d-g      }6d,|6v r9d-|6v r5t	        j
                  d.j'                  t_        |5      |6d,   |6d-                d }5~5ww xY w# 1 sw Y   y xY w)0NT   z$Use just one of --nl, --csv or --tsvz,--flatten cannot be used with --csv or --tsvz1--empty-null can only be used with --csv or --tsvz+--encoding must be used with --csv or --tsvr   r   z	utf-8-sigi   )buffer_sizeencodingsilenti   r   r~   r   r   	delimiter	quotecharzuntitled_{}c              3   |   K   | ].  }t        t        |D cg c]  }|d k(  rdn| c}             0 yc c}w w) Ndictzip).0rr   cellr|   s      r,   	<genexpr>z/insert_upsert_implementation.<locals>.<genexpr>  s?       WPS&Ttrztt'C&TUV&Ts   <7<c              3   H   K   | ]  }t        t        |              y wr$   r  )r!  rr   r|   s     r,   r#  z/insert_upsert_implementation.<locals>.<genexpr>  s     BCS#./Bs   "c              3   @   K   | ]  }d |j                         i  ywr   N)stripr!  r   s     r,   r#  z/insert_upsert_implementation.<locals>.<genexpr>  s     ?tVTZZ\*?s   r   c              3   h   K   | ]*  }|j                         st        j                  |       , y wr$   )r'  jsonloadsr(  s     r,   r#  z/insert_upsert_implementation.<locals>.<genexpr>  s!     QDJJLDJJt,Qs   22zGInvalid JSON - use --csv for CSV or --tsv for TSV files

JSON error: {}c              3   2   K   | ]  }t        |        y wr$   _flattenr!  docs     r,   r#  z/insert_upsert_implementation.<locals>.<genexpr>  s     6#6   rr   r   )variablec              3   4   K   | ]  } |d            ywr&  rY   r!  r0  rM   s     r,   r#  z/insert_upsert_implementation.<locals>.<genexpr>  s     8C3v;8s   z&--convert must return dict or iteratorc              3   6   K   | ]  } |      xs |  y wr$   rY   r4  s     r,   r#  z/insert_upsert_implementation.<locals>.<genexpr>-  s     73337s   )r   r   truncater   strictnot_nulldefaultsupsertc              3   2   K   | ]  }t        |        y wr$   )verify_is_dictr/  s     r,   r#  z/insert_upsert_implementation.<locals>.<genexpr>>  s     4s#4r1  c              3   2   K   | ]  }t        |        y wr$   )r   r/  s     r,   r#  z/insert_upsert_implementation.<locals>.<genexpr>A  s     :c$S):r1  )pk
batch_sizealterzhas no column namedz/{}

Try using --alter to add additional columnssql
parametersz{}

sql = {}
parameters = {}types)3r   r   r   !_register_functions_from_multiplerF   r   r   ioBufferedReaderTextIOWrapperr   peekr   Sniffersniffdecodereadernextr   rH   	itertoolschainr   wrapreadr*  load
isinstancer  decoderJSONDecodeErrorislicer   listiter	TypeErrorsetr   r   cursorexecutemany
insert_all	Exceptionr   args_find_variables__traceback__r   	transformrD  close)9r[   re   r   r>  r   r   rv   r   
empty_nulllinesr   r.   r   r  r  rK  r   r  r?  
stop_afterr@  r:  r   r   r6  r8  r<   detect_typesr   r   r  bulk_sql	functionsr7  rw   sniff_bufferdecoded_buffertrackerdecodedfirst_bytesr   csv_reader_argsrM  	first_rowr   docsr   r2  
text_value	fn_returnextra_kwargs
doc_chunks	doc_chunkr   	variablesrM   r|   s9                                                          @@r,   insert_upsert_implementationry    s   H 
		t	$BR()"i8Y%:s
S31""#IJJs""#QRR3#""#VWW""#PQQ	c"glU&;H LN((4@)),J))$BG	~f	5 P##*//5!//+11&&x: *-+'('2O/8,/8,^^G??FVI@Ec)n@UV1=//A6V")f=#%
 C6B%-||D)?w?DW\\^,.DQQD99W-D!$- $v 66##D*5DH!!w(CB848!$Z]62
zN	i.%;D#I 8$7  
 '*8}L$'+G}L$%+L" 5t4 ;T: #D*5
"V
' F	WW FGGNN$009EF FF ]P# P#`	 BuI  
%CO0 uIgmm4    "aP# P#$ W4 <<// **_ff < % #22D DF F  	1./FF)QVVAY6**GNNq	  (%9NOI	!li&?**5<<A	% 0)L2I  )	iP# P#s   %BU,P*A6U,4P/	A;U,Q8A2U,+R-U,R'AU,*U,/Q5$Q00Q55U,8RU,R$U,'	U)0B4U$$U))U,,U5c                     t        |      }i }|D ]H  }|| j                  j                  v s|j                  |       | j                  j                  |   ||<   J |r1| j                  r%|j                  t        | j                  |             |S r$   )rX  tb_framef_localsremovetb_nextupdatera  )tbvarsto_findfoundvars        r,   ra  ra  q  s|    4jGE 3"++&&&KK--c2E#J3 

_RZZ67Lr2   z#Ignore records if pk already existsr  z$Replace records if pk already existsz
--truncatez@Truncate table before inserting records, if table already existsc                     	 t        | |||||||||	|
|||||||||f|d||||||||||d y# t        $ r-}t        j                  t        j                  |            d}~ww xY w)a6  
    Insert records from FILE into a table, creating the table if it
    does not already exist.

    Example:

        echo '{"name": "Lila"}' | sqlite-utils insert data.db chickens -

    By default the input is expected to be a JSON object or array of objects.

    
    - Use --nl for newline-delimited JSON objects
    - Use --csv or --tsv for comma-separated or tab-separated input
    - Use --lines to write each incoming line to a column called "line"
    - Use --text to write the entire input to a column called "text"

    You can also use --convert to pass a fragment of Python code that will
    be used to convert each input.

    Your Python code will be passed a "row" variable representing the
    imported row, and can return a modified row.

    This example uses just the name, latitude and longitude columns from
    a CSV file, converting name to upper case and latitude and longitude
    to floating point numbers:

    
        sqlite-utils insert plants.db plants plants.csv --csv --convert '
          return {
            "name": row["name"].upper(),
            "latitude": float(row["latitude"]),
            "longitude": float(row["longitude"]),
          }'

    If you are using --lines your code will be passed a "line" variable,
    and for --text a "text" variable.

    When using --text your function can return an iterator of rows to
    insert. This example inserts one record per word in the input:

    
        echo 'A bunch of words' | sqlite-utils insert words.db words - \
          --text --convert '({"word": w} for w in text.split())'
    F)r@  r:  r   r   r6  rh  r   r   r  r8  r<   r7  Nry  UnicodeDecodeErrorrF   r   UNICODE_ERRORrH   ) r[   re   r   r>  r   r   rv   r   re  rf  r   r.   r   r  r  rK  r   r  r?  rg  r@  rh  r   r   r  r   r   r6  r8  r<   r7  r   s                                    r,   insertr  }  s    |$=$)!	
* %)A!	
D  =""=#7#7#;<<=s   *- 	A#(AA#c                     	 t        | |||||||||	|
|||||||||f|d|||||||d	 y# t        $ r-}t        j                  t        j                  |            d}~ww xY w)ao  
    Upsert records based on their primary key. Works like 'insert' but if
    an incoming record has a primary key that matches an existing record
    the existing record will be updated.

    Example:

    
        echo '[
            {"id": 1, "name": "Lila"},
            {"id": 2, "name": "Suna"}
        ]' | sqlite-utils upsert data.db chickens - --pk id
    T)	r@  r:  r8  r<   rh  r   r   r  r7  Nr  )r[   re   r   r>  r   r   rv   r   re  rf  r   r.   r   r?  rg  r  r  rK  r   r  r@  r8  r<   rh  r   r   r  r7  r   s                                r,   r:  r:    s    Z!=$)	
* %);	
>  =""=#7#7#;<<=s   '* 	A (AA r   rA  r   r   r   r   r   r   z--functionsz6Python code or file path defining custom SQL functions)r:   rR   c                 T   	 t        d i d| ddd|ddd|d|d|d	|d
|	d|
d|d|d|d|d|d|d|d|d|dddddddt               di ddd|ddd|d| y# t        t        j                  f$ r#}t        j                  t        |            d}~ww xY w)!a0  
    Execute parameterized SQL against the provided list of documents.

    Example:

    
        echo '[
            {"id": 1, "name": "Lila2"},
            {"id": 2, "name": "Suna2"}
        ]' | sqlite-utils bulk data.db '
            update chickens set name = :name where id = :id
        ' -
    r[   re   Nr   r>  r   r   rv   r   re  rf  r   r.   r   r  r  rK  r   r  r?  rg  r@  Fr:  r8  r<   rh  r   r  ri  rj  rY   )ry  r[  r   r   IntegrityErrorrF   r   r   )r[   rA  r   r?  rj  r   r   rv   r   re  rf  r   r.   r   r  r  rK  r   r  r   r   s                        r,   bulkr  S  sb   f!+$ 	
	
	
 	
 		

 	
 	
 	
 	
 "	
 	
 	
 	
 	
  	
  	
  !	
" "#	
$ %	
& "'	
( )	
* +	
, -	
. U/	
0 1	
2 3	
4 *5	
6 7	
8 9	
:  ;	
> g445 +""3q6**+s   A(A+ +B'B""B'zcreate-databasez--enable-walz'Enable WAL mode on the created databasez--init-spatialitez)Enable SpatiaLite on the created databasec                     t        j                  |       }|r|j                          |rt        ||       |r|j	                          |j                          y)zhCreate a new empty database file

    Example:

    
        sqlite-utils create-database trees.db
    N)r   r   r   r   init_spatialiter   )r[   r   r  r   rw   s        r,   create_databaser    sI    * 
		t	$B
 ^, 
IIKr2   zcreate-tablerg   pksr   zColumn to use as primary keyr  r  rQ   r  r  r  z9Column, other table, other column to set as a foreign keyz#If table already exists, do nothingz#If table already exists, replace itz--transformz4If table already exists, try to transform the schemar  r  c                    t        j                  |       }t        ||
       t        |      dz  dk(  rt	        j
                  d      i }t        |      }|rv|j                  d      }|j                  d      }|j                         t        vr(t	        j
                  dj                  t                    |j                         ||<   |rv||j                         v r*|s(|s&|	s$t	        j
                  dj                  |            ||   j                  |t        |      dk(  r|d   n||t        |      ||||	|	       y)	aT  
    Add a table with the specified columns. Columns should be specified using
    name, type pairs, for example:

    
        sqlite-utils create-table my.db people \
            id integer \
            name text \
            height float \
            photo blob --pk id

    Valid column types are text, integer, float and blob.
    r  r   z5columns must be an even number of 'name' 'type' pairsr   column types must be one of {}zBTable "{}" already exists. Use --replace to delete and replace it.)r>  r8  r9  foreign_keysr   r   rc  r7  N)r   r   r   r   rF   r   rX  popupperVALID_COLUMN_TYPESrH   rn   creater  )r[   re   rg   r  r8  r<   r   r   r   rc  r   r7  rw   coltypesrp   ctypes                   r,   create_tabler    s>   T 
		t	$BR(
7|a1""C
 	
 H7mG
{{1~A;;= 22&&0778JK      gi&&T[[ 
 uIX]3q6g  
r2   	duplicate	new_tablez#If table does not exist, do nothingc                     t        j                  |       }t        ||       	 ||   j                  |       y# t        $ r) |s$t        j                  dj                  |            Y yw xY w)zW
    Create a duplicate of this table, copying across the schema and all row data.
    Table "{}" does not existN)r   r   r   r  r
   rF   r   rH   )r[   re   r  r   r   rw   s         r,   r  r  =  sl     
		t	$BR(R
5	I& R&&'B'I'I%'PQQ Rs   8 /A*)A*zrename-tablenew_namec           	         t        j                  |       }t        ||       	 |j                  ||       y# t        j
                  $ r:}|s.t        j                  dj                  |t        |                  Y d}~yd}~ww xY w)z
    Rename this table.
    z#Table "{}" could not be renamed. {}N)
r   r   r   rename_tabler   r   rF   r   rH   r   )r[   re   r  r   r   rw   r   s          r,   r  r  T  sv     
		t	$BR(
x(## &&5<<UCGL  s   6 B	0A>>Bz
drop-tablec                     t        j                  |       }t        ||       	 ||   j                  |       y# t        $ r% t        j                  dj                  |            w xY w)zgDrop the specified table

    Example:

    
        sqlite-utils drop-table chickens.db chickens
    r   r  Nr   r   r   dropr   rF   r   rH   )r[   re   r   r   rw   s        r,   
drop_tabler  m  sd    " 
		t	$BR(N
5	f% N""#>#E#Ee#LMMN	   9 .A'zcreate-viewrd   selectz"If view already exists, do nothingz"If view already exists, replace itc                    t        j                  |       }t        ||       ||j                         v r=|ry|r||   j	                          n$t        j                  dj                  |            |j                  ||       y)zCreate a view for the provided SELECT query

    Example:

    
        sqlite-utils create-view chickens.db heavy_chickens \
          'select * from chickens where weight > 3'
    NzAView "{}" already exists. Use --replace to delete and replace it.)	r   r   r   rm   r  rF   r   rH   create_view)r[   rd   r  r   r   r   rw   s          r,   r  r    sv    8 
		t	$BR(r}}tHMMO&&SZZ 
 NN4 r2   z	drop-viewz"If view does not exist, do nothingc                     t        j                  |       }t        ||       	 ||   j                  |       y# t        $ r% t        j                  dj                  |            w xY w)zkDrop the specified view

    Example:

    
        sqlite-utils drop-view chickens.db heavy_chickens
    r   zView "{}" does not existNr  )r[   rd   r   r   rw   s        r,   	drop_viewr    sd    " 
		t	$BR(L
4V$ L""#=#D#DT#JKKLr  z--attachz;Additional databases to attach - specify alias and filepathz-rz--rawz%Raw output, first column of first rowz--raw-linesz$Raw output, first column of each rowz-pz--paramzNamed :parameters for SQL queryc                     t        j                  |       }|D ]  \  }}|j                  ||        t        ||       |j	                          |rt        ||       t        ||||||||||	|||
       y)zExecute SQL query and return the results as JSON

    Example:

    
        sqlite-utils data.db \
            "select * from chickens where age > :age" \
            -p age 1
    N)r   r   attachr   register_fts4_bm25rE  _execute_query)r[   rA  r  r   r   rv   r   r   re   r   r   raw	raw_linesr0   r   rj  rw   aliasattach_paths                      r,   rT   rT     s    p 
		t	$B$ &{
		%%&R()"i8

r2   paths)ra   rb   r   zJFlatten nested JSON objects, so {"foo": {"bar": 1}} becomes {"foo_bar": 1}z3Character encoding for CSV input, defaults to utf-8z-nz--no-detect-typesz!Treat all CSV/TSV columns as TEXTz&Show SQL schema for in-memory databasez--dumpzDump SQL for in-memory databasez--savez$Save in-memory database to this filer  z+Analyze resulting tables and output resultsc                    t        j                  d      }|s|s|s|r| s|g} d}i }t        |       D ]  \  }}d}d|v r`|j                  dd      d   j	                         t
        j                  v r-|j                  dd      \  }}t
        |j	                            }nd}|dv rt        j                  j                  }d}nst        j                  |      } | j                  }!|j                  |!      rd	j                  |!||!         }n|!}|j                  |!d      dz   ||!<   | j                  d
      }t!        |||      \  }"}#d}$|#t
        j"                  t
        j$                  fv r|st'               }$|$j)                  |"      }"|r	d |"D        }"||   j+                  |"d       |$||   j-                  |$j.                         dj                  |dz         g}%|dk(  r|%j1                  d       |%D ]7  }&||&   j3                         r|j5                  |&dj                  |             9 |s|j7                           |rt9        |ddd       y|r+|j;                         D ]  }'t=        j>                  |'        y|r t=        j>                  |j@                         y|r<t        j                  |      }(|j;                         D ]  }'|(jC                  |'        y|D ]  \  })}*|jE                  |)|*        tG        ||       |jI                          |rtK        ||       |r|S tM        ||||||
|||	||||       y)a  Execute SQL query against an in-memory database, optionally populated by imported data

    To import data from CSV, TSV or JSON files pass them on the command-line:

    
        sqlite-utils memory one.csv two.json \
            "select * from one join two on one.two_id = two.id"

    For data piped into the tool from standard input, use "-" or "stdin":

    
        cat animals.csv | sqlite-utils memory - \
            "select * from stdin where species = 'dog'"

    The format of the data will be automatically detected. You can specify the format
    explicitly using :json, :csv, :tsv or :nl (for newline-delimited JSON) - for example:

    
        cat animals.csv | sqlite-utils memory stdin:csv places.dat:nl \
            "select * from stdin where place_id in (select id from places)"

    Use --schema to view the SQL schema of any imported files:

    
        sqlite-utils memory animals.csv --schema
    TmemoryN:r   r   )r   stdinr  z{}_{}r   )rH   r  c              3   2   K   | ]  }t        |        y wr$   r-  )r!  rr   s     r,   r#  zmemory.<locals>.<genexpr>  s     2cHSM2r1  )r@  rC  zt{}r   tzselect * from [{}]F)r   rg   save)'r   r   	enumeratersplitr  r   __members__r   r  bufferpathlibr  stemgetrH   openr   CSVTSVr   rQ  r^  rc  rD  ro   r\   r  rd  _analyzer   rF   r   rh   executer  r   r  rE  r  )+r  rA  rj  r  r   r   r   rv   r   r   re   r   r   r  r  r0   r  no_detect_typesrh   r   r  r   r   	return_dbrw   stem_countsr   r[   fpsuffixrH   
file_table	file_pathr  rowsformat_usedrm  rm   	view_namer   db2r  r  s+                                              r,   r  r    s)   Z 
		d	+B 	'5KU# (4$;4;;sA.r288:f>P>PP;;sA.LD&FLLN+FF>!!!B JT*I>>Dt$$^^D+d2CD
!
 +a 81 <K%B*2fxPk6::vzz22?!mG<<%D2T2D
:!!$d!3zN$$7==$9ll1q5)*
6c"# 	SIi='')y*>*E*Ej*QR	S HHJQ(T D$U;KKM 	DJJt	

299##D)KKM 	DKK	$ &{
		%%&R()"i8	

r2   c           	         | j                   5  	 | j                  |t        |            }|j                  dg}|j                  gg}n|j                  D cg c]  }|d   	 }}|rx|j                         d   }t        |t              r+t        j                  j                  j                  |       nVt        j                  j                  t        |             n,|ru|D ]o  }|d   }t        |t              r-t        j                  j                  j                  |dz          Et        j                  j                  t        |      dz          q n|	s|r/t!        t#        j"                  t%        |      ||	xs d             n|s|rUt'        j(                  t        j                  |rdnd	      }|s|j+                  |       |D ]  }|j+                  |        n)t-        |||
||      D ]  }t	        j.                  |        d d d        y # t        $ r#}t	        j
                  t        |            d }~ww xY wc c}w # 1 sw Y   y xY w)
Nrows_affectedr      
rl   rz   r{   r~   r   r   )r   r  r  r   rF   r   r   descriptionrowcountfetchonerT  r   r   r   r  writer   rJ   rX  r   r   r   r   r   )rw   rA  r0   r  r  re   rv   r   r   r   r   r   r   r\  r   r|   rs   datarr   r   r   s                        r,   r  r    s    
 &!	/ZZT%[1F %&'G'(F%+%7%78qt8G8??$Q'D$&

!!''-

  T+ 71vdE*JJ%%++D5L9JJ$$SY%567 E!!L'CO8
 C^^CJJsPWXF( %$% $FGRK !

4 !K&! &!   	/&&s1v..	/ 9&! &!s:   IH,II"F(I	H?H::H??IIdbtableqz-oz--orderz$Order by ('column' or 'column desc')z--columnzColumns to returnz--limitz1Number of rows to return - defaults to everythingz--sqlshow_sqlz Show SQL query that would be runz--quotez&Apply FTS quoting rules to search termc                 ,   t        j                  |      }t        ||       ||   }|j                         s$t	        j
                  dj                  |            |j                         s$t	        j
                  dj                  |            |r<|j                  }|D ]+  }||vst	        j
                  dj                  ||             |j                  |||      }|rt	        j                  |       y|r|j                  |      }	 | j                  t        |||	|
||||||d|fg|       y# t        j
                  $ rM}dt        |      v sd	t        |      v r-t	        j
                  d
j                  t        |                   d}~ww xY w)zsExecute a full-text search against this table

    Example:

        sqlite-utils search data.db chickens lila
    zTable '{}' does not existz1Table '{}' is not configured for full-text searchzTable '{}' has no column '{})rg   order_bylimitNrT   )r[   rA  r   r   rv   r   r   re   r   r   r0   r   zmalformed MATCH expressionzunterminated stringz2{}

Try running this again with the --quote option)r   r   r   r\   rF   r   rH   
detect_ftscolumns_dict
search_sqlr   	quote_ftsinvokerT   r   )r1   r[   r  r  orderr  quoter   r  r   r   rv   r   r   re   r   r   r   rw   	table_objtable_columnsrs   rA  r   s                           r,   searchr  #  s   ` 
		t	$BR(7I""#>#E#Eg#NOO!""?FFwO
 	
 !.. 	A%**299'1E 	
 

vU

KC

3LLO

!Q<.) 	 	
  '3q615JcRSf5T&&FMMcRSfU  s   %D3 3FAFFz--wherezOptional where clausez"Named :parameters for where clausez--offsetzSQL offset to usec                 $   d}|rdj                  d |D              }dj                  ||      }|r|d|z   z  }|r|d|z   z  }|r|dj                  |      z  }|r|dj                  |      z  }| j                  t        |||	|
||||||||	       y
)ziOutput all rows in the specified table

    Example:

    
        sqlite-utils rows trees.db Trees
    *rC   c              3   >   K   | ]  }d j                  |        yw)z[{}]N)rH   )r!  rs   s     r,   r#  zrows.<locals>.<genexpr>  s     =FMM!,=s   zselect {} from [{}]z where z
 order by z	 limit {}z
 offset {})r[   rA  r   r   rv   r   r   re   r   r0   r   r   N)rI   rH   r  rT   )r1   r[   r  r   wherer  r0   r  offsetr   r   rv   r   r   re   r   r   r   rg   rA  s                       r,   r  r    s    r G))=f==

&
&w
8Cy5  |e##{!!%((|""6**JJ%  r2   c                     d}|rLt        j                  d      j                  |dj                  dj	                  fd|D                    z  }| j                  t        |||||||||	|
|       y)	zjShow triggers configured in this database

    Example:

    
        sqlite-utils triggers trees.db
    zOselect name, tbl_name as [table], sql from sqlite_master where type = 'trigger'Tr  z and [table] in ({})rC   c              3   .   K   | ]  } |        y wr$   rY   r!  re   r  s     r,   r#  ztriggers.<locals>.<genexpr>       7ueEl7   r[   rA  r   r   rv   r   r   re   r   r   r   Nr   r   r  rH   rI   r  rT   )r1   r[   r   r   r   rv   r   r   re   r   r   r   rA  r  s                @r,   triggersr    s    > \C%%T288%,,II777
 	
 JJ%  r2   z--auxzInclude auxiliary columnsc                     d}|rLt        j                  d      j                  |dj                  dj	                  fd|D                    z  }|s|dz  }| j                  t        ||||||||	|
||       y	)
z|Show indexes for the whole database or specific tables

    Example:

    
        sqlite-utils indexes trees.db Trees
    a  
    select
      sqlite_master.name as "table",
      indexes.name as index_name,
      xinfo.*
    from sqlite_master
      join pragma_index_list(sqlite_master.name) indexes
      join pragma_index_xinfo(index_name) xinfo
    where
      sqlite_master.type = 'table'
    Tr  z and sqlite_master.name in ({})rC   c              3   .   K   | ]  } |        y wr$   rY   r  s     r,   r#  zindexes.<locals>.<genexpr>>	  r  r  z and xinfo.key = 1r  Nr  )r1   r[   r   auxr   r   rv   r   r   re   r   r   r   rA  r  s                 @r,   indexesr  	  s    B
C %%T288077II777
 	
 ##JJ%  r2   c                     t        j                  |       }t        ||       |r*|D ]$  }t        j                  ||   j
                         & yt        j                  |j
                         y)zyShow full schema for this database or for specified tables

    Example:

    
        sqlite-utils schema trees.db
    N)r   r   r   rF   r   rh   r   s        r,   rh   rh   R	  sX    ( 
		t	$BR( 	)EJJr%y''(	) 	

299r2   z--typez2Change column type to INTEGER, TEXT, FLOAT or BLOBz--dropzDrop this columnz--renamezRename this column to Xz--column-orderzReorder columnszSet this column to NOT NULLz--not-null-falsez Remove NOT NULL from this columnz Make this column the primary keyz	--pk-nonez+Remove primary key (convert to rowid table)z!Set default value for this columnz--default-nonezRemove default from this columnr   z--add-foreign-keyzOAdd a foreign key constraint from a column to another table with another columndrop_foreign_keysz--drop-foreign-keyz+Drop foreign key constraint for this columnzOutput SQL without executing itc                    t        j                  |       }t        ||       i }i }|D ]V  \  }}|j                         t        vr(t        j                  dj                  t                    |j                         ||<   X i }|D ]  }d||<   	 |D ]  }d||<   	 i }|
D ]
  \  }}|||<    |D ]  }d||<   	 ||d<   t        |      |d<   t        |      |d<   |xs d|d<   ||d	<   |rt        |      d
k(  r	|d   |d<   n||d<   n|	rd|d<   ||d<   |r||d<   |r||d<   |r0 ||   j                  di |D ]  }t        j                  |        y ||   j                  di | y)zTransform a table beyond the capabilities of ALTER TABLE

    Example:

    
        sqlite-utils transform mydb.db mytable \
            --drop column1 \
            --rename column2 column_renamed
    r  TFNrD  r  renamecolumn_orderr8  r   r   r>  r9  r  r   rY   )r   r   r   r  r  rF   r   rH   r[  r  r   transform_sqlr   rc  )r[   re   ra   r  r  r  r8  not_null_falser>  pk_noner<   default_noner   r  rA  r   rw   rD  kwargsr   r  not_null_dictdefault_dictr/   r   s                            r,   rc  rc  o	  s   b 
		t	$BR(EF &;;= 22&&0778JK  f& M % $f%  & %f& L  %$V% $#V$ F7OYF6NF|F8)1TF>&F:	r7a<a5F4LF4L	t%F:&7"#%5!"
+BuI++5f5 	DJJt	 	5	%f%r2   rB   z-Name of the other table to extract columns toz--fk-columnz2Name of the foreign key column to add to the tablez%Rename this column in extracted tablec                     t        j                  |       }t        ||       t        |||t        |            } ||   j                  di | y)zExtract one or more columns into a separate table

    Example:

    
        sqlite-utils extract trees.db Street_Trees species
    )rg   re   	fk_columnr  NrY   )r   r   r   r  extract)	r[   re   rg   r   r  r  r   rw   r  s	            r,   r  r  	  sR    F 
		t	$BR(F|	F BuIr2   zinsert-filesfile_or_dir)r   rb   ra   z Column definitions for the tabler  z"Alter table to add missing columnsz'Replace files with matching primary keyz--upsertz&Upsert files with matching primary keyzFile name to usez$Store file content as TEXT, not BLOBz-sr
  zDon't show a progress barc           
      R  
 s|	rg dng d|sdg}fd}t         |             }t        ||      5 
fd}t        j                  |       }t	        ||       	 |j
                  5  ||   j                   |       t        |      dk(  r|d   n||||	       d
d
d
       d
d
d
       y
# 1 sw Y   xY w# t        $ rQ}t        j                  t        j                  dj                  |j                  |j                                    d
}~ww xY w# 1 sw Y   y
xY w)as  
    Insert one or more files using BLOB columns in the specified table

    Example:

    
        sqlite-utils insert-files pics.db images *.gif \
            -c name:name \
            -c content:content \
            -c content_hash:sha256 \
            -c created:ctime_iso \
            -c modified:mtime_iso \
            -c size:size \
            --pk name
    )	path:pathzcontent_text:content_text	size:size)r  zcontent:contentr  r[   c               3   "  K   D ]  } t        j                  |       }| dk(  rd "|j                         r=|j                  d      D ](  }|j	                         s||j                  |      f * o|j	                         s||f  y w)Nr   )r   r   r  )r  r  is_dirrglobis_filerelative_to)f_or_dr[   subpathr  s      r,   yield_paths_and_relative_pathsz4insert_files.<locals>.yield_paths_and_relative_pathsd
  s     ! 		!F<<'D}#zz# AG(%w':':4'@@@A Dj 		!s   AB(B	Br  c               3     	K   
D ]  \  } }i }fd}t        t        |      }| dk(  rHt        j                  j                  j                         	fdfd	fd	fd	fd	fd		fd
d}D ]?  }d|v r|j                  dd      \  }}n||}}	  ||   |       }|||<   |dk(  s8s;||<   A |  y # t        $ rC t        j                  dj                  |dj                  |j                                           w xY ww)Nc                     | j                         }	 |j                        S # t        $ r}t        ||      d }~ww xY w)Nr  )resolve	read_textr  UnicodeDecodeErrorForPath)presolvedr   r  s      r,   _content_textz6insert_files.<locals>.to_insert.<locals>._content_textz
  sG     yy{HE'1181DD- E78DDEs   % 	?:?)content_textr   c                     xs dS Nr   rY   r  rp   s    r,   <lambda>z1insert_files.<locals>.to_insert.<locals>.<lambda>
      $+# r2   c                     xs dS r#  rY   r$  s    r,   r%  z1insert_files.<locals>.to_insert.<locals>.<lambda>
  r&  r2   c                     S r$   rY   r  
stdin_datas    r,   r%  z1insert_files.<locals>.to_insert.<locals>.<lambda>
  s    Z r2   c                 .    j                  xs d      S )Nzutf-8)rL  )r  r  r*  s    r,   r%  z1insert_files.<locals>.to_insert.<locals>.<lambda>
  s    *2C2C$/3 r2   c                 J    t        j                        j                         S r$   )hashlibsha256	hexdigestr)  s    r,   r%  z1insert_files.<locals>.to_insert.<locals>.<lambda>
  s    GNN:,F,P,P,R r2   c                 J    t        j                        j                         S r$   )r-  md5r/  r)  s    r,   r%  z1insert_files.<locals>.to_insert.<locals>.<lambda>
  s    Z)@)J)J)L r2   c                     t              S r$   )r   r)  s    r,   r%  z1insert_files.<locals>.to_insert.<locals>.<lambda>
  s    #j/ r2   )rp   r[   contentr!  r.  r1  sizer  r   z6'{}' is not a valid column definition - options are {}rC   rp   )r  FILE_COLUMNSr   r  r  rR  r  KeyErrorrF   r   rH   rI   keys)r[   relative_pathrr   r   lookupscoldefcolnamecoltyper/   r*  barr   r  rp   s            @r,   	to_insertzinsert_files.<locals>.to_insertu
  s.    '* +#mE |-H3;!$!1!1!6!6!8J !6 5#7) #SL 9
G % ,Ff}+1==a+@(+16 0 0 6',G &(T'+G!," 	W+D $ #22T[[ '7<<>)B s+   BDB=&D,D/D=AD		Dr   r   )r>  r@  r   r:  Nz$Could not read file '{}' as text

{})rX  r   r   r   r   r   r^  r   r  rF   r   r  rH   r[   	exception)r[   re   r  r   r  r@  r   r:  rp   r   r  r  r   r  paths_and_relative_pathsr>  rw   r   r=  s     ``    ` `       @r,   insert_filesrA  
  s*   | LFBF(C
!  $$B$DE	-f	= @,	\ ""4(^,	 5	$$K!$SQs1vC#! % g@ @f  ) 	&&$$<CCAFFAKKX 	w@ @sB   *D%C 12B4#C 4B=	9C  	D	ADDDD&zanalyze-tables)r]   r^   r_   r\   zSpecific columns to analyzez%Save results to _analyze_tables tablez--common-limit
   zHow many common valuesz	--no-mostzSkip most common valuesz
--no-leastzSkip least common valuesc           	      h    t        j                  |       }t        ||       t        |||||||       y)zuAnalyze the columns in one or more tables

    Example:

    
        sqlite-utils analyze-tables data.db trees
    N)r   r   r   r  )	r[   r   rg   r  common_limitno_mostno_leastr   rw   s	            r,   analyze_tablesrG  
  s3    N 
		t	$BR(R$gxHr2   c                    |s| j                         }g }i }t               }	|D ]q  }
| |
   j                  ||
<   | |
   j                  D ]K  }|r|j                  |v s|j                  |
|j                  f       |	j                  |j                         M s |rVt        |      |	z
  rHt        j                  dj                  dj                  t        t        |      |	z
                          t        |      D ]   \  }\  }
}| |
   j                  ||||
   d| |       }|r%| d   j                  |j                         dd       d	}|j                   |j"                  k7  rt%        d
|j&                        }t%        d|j(                        } t+        j,                  d      j/                         j                  d|dz   t1        |      ||d|j                         dz   }t        j2                  |        y )Nz These columns were not found: {}rC   P   )rD  
total_rowsvalue_truncatemost_commonleast_common_analyze_tables_)re   r   T)r>  r   r  z

  Most common:z

  Least common:z
        {table}.{column}: ({i}/{total})

          Total rows: {total_rows}
          Null rows: {num_null}
          Blank rows: {num_blank}

          Distinct values: {num_distinct}{most_common_rendered}{least_common_rendered}
        r   )r   totalmost_common_renderedleast_common_renderedrl   rY   )rn   r[  rf   rg   rp   ro   addrF   r   rH   rI   sortedr  analyze_columnr  _asdictnum_nullrJ  _render_commonrL  rM  textwrapdedentr'  r   r   )rw   r   rg   r  rD  rE  rF  todotable_countsseen_columnsre   r   r   column_detailsrP  rQ  detailss                    r,   r  r  
  s   !DL5L . iooUi'' 	.FfkkW4UFKK01  -	.. CL</"".55		&W!<=>
 	
 (o -?E6E11%#E*#% 2 
 !"))&&(-@$ *   """n&?&??#1$n&@&@$  !/!>#>#>!


  !ed))=*?!" %,,.#( + 	0 	

7[-r2   packagesz-U	--upgradez"Upgrade packages to latest versionz-e
--editablez1Install a project in editable mode from this pathc                 z    ddg}|r|dgz  }|r|d|gz  }|t        |       z  }|t        _        t        dd       y)zDInstall packages from PyPI into the same environment as sqlite-utilspipinstallr`  ra  __main__run_nameNrX  r   argvr   )r_  upgradeeditabler`  s       r,   rd  rd  &  sN     9Dx((DNDCHuz*r2   -yz--yeszDon't ask for confirmationc                 `    ddgt        |       z   |rdgng z   t        _        t        dd       y)z;Uninstall Python packages from the sqlite-utils environmentrc  	uninstallrl  re  rf  Nrh  )r_  yess     r,   rn  rn  <  s.    
 {#d8n4#2NCHuz*r2   c            
      (   t        j                  d      j                         } t        t              D cg c]2  }|j                  d      s|dvrt        t        t        |            r|4 }}|D ]o  }t        t        |      }| dj                  |t        t        j                  |            t        j                  |j                  j                                     z  } q | dz  } | t        j                  d      j                         z  } | S c c}w )Na  
    Convert columns using Python code you supply. For example:

    
    sqlite-utils convert my.db mytable mycolumn \
        '"\n".join(textwrap.wrap(value, 10))' \
        --import=textwrap

    "value" is a variable with the column value to be converted.

    Use "-" for CODE to read Python code from standard input.

    The following common operations are available as recipe functions:
    _)r*  parserz

r.{}{}

{}z

z
    You can use these recipes like so:

    
    sqlite-utils convert my.db mytable mycolumn \
        'r.jsonsplit(value, delimiter=":")'
    )rX  rY  r'  dirr   r   callablegetattrrH   r   inspect	signature__doc__rstrip)r:   nrecipe_namesrp   rM   s        r,   _generate_convert_helpr|  E  s   ??	 eg 	$ W||C ''WWa()	 	
L   
Wd#$++#g''+,hoobjj>O>O>Q.R
 	


 	FNDHOO	 egD K-s   7Ddb_path)ra   )ra   r   rb   codez	--dry-runz2Show results of running this against first 10 rowsz--multiz0Populate columns for keys in returned dictionaryz--outputz4Optional separate column to populate with the outputz--output-typez(Column type to use for the output columnr   )r   r   r   r   )r:   r<   ra   zDrop original column afterwardsz--no-skip-falsezDon't skip falsey valuespdb_z--pdbz Open pdb debugger on first errorc                 d   t        j                  d       t        j                  |       }|	#t	        |      dkD  rt        j                  d      |r#t	        |      dkD  rt        j                  d      |r|	s|st        j                  d      |dk(  rt        j                  j                         }|rt        |      ng }	 t        ||      |r:|rfd}nfd}|j                  j                  d	d|       d
j!                  |d   ||dj!                  |      nd      }|j                  j#                  ||      j%                         D ]m  }t        j&                  t        |d                t        j&                  d       t        j&                  t        |d                t        j&                          o ||   j)                  ||      }t        j&                  dj!                  ||dk(  rd             y d             y |r	fd}|	 ||   j+                  ||||	|
|| || 
       y # t        $ r#}t        j                  t        |            d }~ww xY w# t,        $ r<}t        j                  dj!                  t/        |j0                                    d }~ww xY w)NTr   z-Cannot use --output with more than one columnz,Cannot use --multi with more than one columnz0--drop can only be used with --output or --multir   c                 N    | r!t        j                   |       t              S | S )Nr<   )r*  dumpsreprvrM   s    r,   previewzconvert.<locals>.preview  s    :;tzz"Q%6BBr2   c                     | r |       S | S r$   rY   r  s    r,   r  zconvert.<locals>.preview  s     !r!u(q(r2   preview_transformz
            select
                [{column}] as value,
                preview_transform([{column}]) as preview
            from [{table}]{where} limit 10
        r   z	 where {}r  )r   re   r  z --- becomes:)r  
where_argszWould affect {} row{}sc                     	  |       S # t         $ rJ}t        d|       t        j                  |j                         t        j                  d       Y d }~y d }~ww xY w)Nz(
Exception raised, dropping into pdb...:r   )r_  r   pdbpost_mortemrb  r   exit)r/   r   fn_s     r,   
wrapped_fnzconvert.<locals>.wrapped_fn  sL     u:%   ErJOOB$4$45HHQKK s    	AA AA)r  r  outputoutput_typer  
skip_falsemultishow_progresszFWhen using --multi code must return a Python dictionary - returned: {})r   enable_callback_tracebacksr   r   r   rF   r   r   r  rR  r  r   SyntaxErrorr   r   create_functionrH   r  fetchallr   count_wherer.   r   r  values)r}  re   rg   r~  r   dry_runr  r  r0   r  r  r  no_skip_falser  r  rw   r  r   r  rA  rr   rf   r  rM   r  s                          @@r,   r.   r.   o  s   l &&t,			w	'Bc'lQ.""#RSSW!""#QRRVu""#UVVs{yy~~ %e2J+4) C
) 	 3Q@
 F1:/4/@+$$U+b  
 	 77??3
3<<> 	CJJs3q6{#JJ'JJs3q6{#JJL		
 5	%%! & 
 	

*11%uzSTsST C  B	uI%',,"(j  e  +""3q6**+|  	&&X__N 	s0   H;  I* ;	I'I""I'*	J/37J**J/zadd-geometry-columncolumn_namerA   geometry_type)POINT
LINESTRINGPOLYGON
MULTIPOINTMULTILINESTRINGMULTIPOLYGONGEOMETRYCOLLECTIONGEOMETRYr  z(Specify a geometry type for this column.)ra   r<   r:   show_defaultz--sridi  z[Spatial Reference ID. See https://spatialreference.org for details on specific projections.)ra   r<   r  r:   z--dimensionscoord_dimensionXYz@Coordinate dimensions. Use XYZ for three-dimensional geometries.r8  zAdd a NOT NULL constraint.c                 (   t        j                  |       }||   j                         st        j                  d      |rt        ||       |j                          ||   j                  |||||      rt        j                  d| d| d|        yy)zAdd a SpatiaLite geometry column to an existing table. Requires SpatiaLite extension.
    


    By default, this command will try to load the SpatiaLite extension from usual paths.
    To load it from a specific path, use --load-extension.z7You must create a table before adding a geometry columnzAdded z column z to N)	r   r   r\   rF   r   r   r  add_geometry_columnr   )	r}  re   r  r  sridr  r8  r   rw   s	            r,   r  r    s    v 
		w	'Be9""E
 	

 ^,	%y$$]D/8 	

VM?(;-tE7KLr2   zcreate-spatial-indexc                 .   t        j                  |       }||   j                         st        j                  d      |rt        ||       |j                          |||   j                  vrt        j                  d      ||   j                  |       y)a-  Create a spatial index on a SpatiaLite geometry column.
    The table and geometry column must already exist before trying to add a spatial index.
    


    By default, this command will try to load the SpatiaLite extension from usual paths.
    To load it from a specific path, use --load-extension.zQYou must create a table and add a geometry column before creating a spatial indexz>You must add a geometry column before creating a spatial indexN)	r   r   r\   rF   r   r   r  r  create_spatial_index)r}  re   r  r   rw   s        r,   r  r  F  s     
		w	'Be9""_
 	

 ^,"U)000""L
 	
 uI"";/r2   pluginsc                  h    t        j                  t        j                  t	               d             y)zList installed pluginsr  )indentN)rF   r   r*  r  r   rY   r2   r,   plugins_listr  h  s     
JJtzz+-23r2   )rZ   c                     |y| g}|D ]&  \  }}|j                  dj                  ||             ( dj                  |      S )Nr  z
    {}: {}rl   )ro   rH   rI   )titler  rf  r/   rf   s        r,   rW  rW  q  sL    ~GE 8u\((67899Ur2   c                       e Zd Zd Zy)r  c                      || _         || _        y r$   )r?  r[   )r(   r?  r[   s      r,   r&   z"UnicodeDecodeErrorForPath.__init__{  s    "	r2   N)r3   r4   r5   r&   rY   r2   r,   r  r  z  s    r2   r  rp   c                     | j                   S r$   r   r  s    r,   r%  r%    
    aff r2   c                     t        |       S r$   )r   r  s    r,   r%  r%    s
    c!f r2   fullpathc                 4    t        | j                               S r$   )r   r  r  s    r,   r%  r%    s    #aiik* r2   r.  c                     t        j                  | j                         j                               j	                         S r$   )r-  r.  r  
read_bytesr/  r  s    r,   r%  r%    s'    qyy{'='='?@JJL r2   r1  c                     t        j                  | j                         j                               j	                         S r$   )r-  r1  r  r  r/  r  s    r,   r%  r%    s'    W[[!7!7!9:DDF r2   modec                 6    | j                         j                  S r$   )statst_moder  s    r,   r%  r%        affh&& r2   r3  c                 >    | j                         j                         S r$   )r  r  r  s    r,   r%  r%    s    //1 r2   mtimec                 6    | j                         j                  S r$   )r  st_mtimer  s    r,   r%  r%        qvvx(( r2   ctimec                 6    | j                         j                  S r$   )r  st_ctimer  s    r,   r%  r%    r  r2   	mtime_intc                 H    t        | j                         j                        S r$   )r   r  r  r  s    r,   r%  r%        3qvvx001 r2   	ctime_intc                 H    t        | j                         j                        S r$   )r   r  r  r  s    r,   r%  r%    r  r2   	mtime_isoc                     t        j                  | j                         j                  t        j
                        j                  d       j                         S N)tzinfo)r   fromtimestampr  r  r   utcr   	isoformatr  s    r,   r%  r%    9    811!&&(2C2CX\\RWDWY[ r2   	ctime_isoc                     t        j                  | j                         j                  t        j
                        j                  d       j                         S r  )r   r  r  r  r   r  r   r  r  s    r,   r%  r%    r  r2   r4  c                 6    | j                         j                  S r$   )r  st_sizer  s    r,   r%  r%    r  r2   r  c                     | j                   S r$   )r  r  s    r,   r%  r%    r  r2   r  c                     | j                   S r$   )r  r  s    r,   r%  r%    s
     r2   c              #     K   t        j                  | d      \  }}t        |d        d}t        j                  ||      D ]  \  }}	|	d u }
|}|r|D cg c]  }t	        |       }}|st        t        ||            }dj                  |s|rdndndt        j                  |t              |s|
sdnd|
r|sd	nd
      }| d} |rd y y c c}w w)Nr  Tz-{firstchar}{serialized}{maybecomma}{lastchar}[ r  r  ,])	firstchar
serialized
maybecommalastcharFz[])rO  teerN  zip_longest
maybe_jsonr  r   rH   r*  r  json_binary)iteratorr|   r   r   r   current_iter	next_iterfirstrr   next_rowis_lastr  r/   r   s                 r,   r   r     s      (mmHa8L)DE"..|YG Xd"378%Ju%8D8GT*+D>EE57esRzz$<#%gsB$RSb	 F 
 
  
  9s   ACC$A0Cc                     t        | t              s| S | j                         }|j                  d      s|j                  d      s| S 	 t	        j
                  |      S # t        $ r | cY S w xY w)N{r  )rT  r   r'  r   r*  r+  
ValueError)r/   strippeds     r,   r  r    sd    eS!{{}H$(;(;C(@zz(## s   A A+*A+c                 |    t        | t              r'dt        j                  |       j	                  d      dS t
        )NTzlatin-1)z$base64encoded)rT  r   base64	b64encoderL  rZ  )r/   s    r,   r  r    s2    %F,<,<U,C,J,J9,UVVr2   c                     t        | t              s0t        j                  dj	                  t        |       d d             | S )Nz&Rows must all be dictionaries, got: {}i  )rT  r  rF   r   rH   r  )r0  s    r,   r<  r<    s?    c4 ""4;;DIet<LM
 	
 Jr2   c                 P   |r| j                   j                  d       |D ]  }|dk(  r)t        j                  j	                  |      s
t               }d|v r4|j                  d      \  }}}| j                   j                  d||g       i| j                   j                  |        y y )NT
spatialiter  zSELECT load_extension(?, ?))	r   enable_load_extensionosr[   r\   r   	partitionr  r   )rw   r   extr[   rq  
entrypoints         r,   r   r     s    
%%d+! 	,Cl"277>>#+>%'cz&)mmC&8#a =j?QR&&s+	, r2   c                    d|vr5|j                  d      r$	 t        j                  |      j                         }t        j                  d       i }	 t        ||       |j                         D ]6  \  }}t        |      s|j                  d      r$| j                  ||       8 y # t        $ r% t        j                  dj                  |            w xY w# t        $ r)}t        j                  dj                  |            d }~ww xY w)Nrl   z.pyzFile not found: {}Tz!Error in functions definition: {}rq  r   )endswithr  r  r  FileNotFoundErrorrF   r   rH   r   r  execr  rq   rt  r   register_function)rw   rj  globalsr   rp   r/   s         r,   _register_functionsr    s     9!3!3E!:	OY/99;I &&t,GSY  }} 3eE?4??3#7  T 23 ! 	O&&';'B'B9'MNN	O  S""#F#M#Mb#QRRSs#   #B( C (.C	D"$DDc                 t    |sy|D ]0  }t        |t              s|j                         s%t        | |       2 y)z7Register functions from multiple --functions arguments.N)rT  r   r'  r  )rw   functions_listrj  s      r,   rE  rE    s4    # /	i%)//*;I./r2   )F)FFFNNNFNFNNF)rB  FF)r  rF   click_default_groupr   r   r   r-  r  runpyr   r   sqlite_utils.dbr   r   r	   r
   sqlite_utils.pluginsr   r   sqlite_utils.utilsr   r   rX  rv  rF  rO  r*  r  r  r   rv   r   rJ   utilsr   r   r   r   r   r   r.  r   r   r   r   r   r   trogonImportErrorr  CONTEXT_SETTINGSr  r'  r  Choicer"   rO   rS   groupversion_optionrZ   tuicommandr  r  rG   r   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  ry  ra  r  r:  r  r   r  r  r  r  r  r  r  r  rT   r  r  pass_contextr  r  r  r  rh   rc  r  rA  rG  r  rd  rn  r|  r.   r  r  r  hookregister_commandsrW  r_  r  r5  r   r  r  r<  r   r  rE  rY   r2   r,   <module>r     s%     , '     J J 0 <     	   	 
 
     
 4*:; 9 
 
EG   :ELL :$N
 %	 	 	
 

*&**,s
C 
	44%E	R
 2E4 2E4 3UD 	1	 	(	 " !@   :@F 
	44%E	R
 2E4 	0	 	'	 '   .'T 
	44%E	R
 #m"4eTR  S $ & - 
	44%E	R
 #$  $ !$" 
	44%E	R
 r"& # && 
	44%E	R
) ) 
	44%E	R
    ,
	44%E	R
 
	C
  
u+P 		j	 		1	 	/
 0     J0: #$
	44%E	R
 ./	4
 &  0 /   % &$ $%
	44%E	R
 R(&  ) &&4 &'
	44%E	R
   ( .!
	44%E	R
 T2h>?j95RVW	) 	/
   X @ 3  ".B ,
	44%E	R
 T2hZEhZEl!@A	R	 	:
 '  B F F 3   .'> .!
	44%E	R
 T2
#  3  "
# - 
	44%E	R
 
   !
 ,

	44%E	R	     - 

	44%E	R	   ! /"
	44%E	R
 #&  $ #&* .!
	44%E	R
 
  "
 ELLQ
 ELL,KLELLw3EFELL$-?@ELL4QRELL>
 ELL:
 ELL#EFELL' ELL%EFELL%IJELLD/UVELL4PQELL>A$N ). Av EP#f	 e2W 	/	 	K	q=  "q=h $'L= ( L=^ 
	dUu	E
 ZUZZ-=n3:RS	A
 D+   T >   D+N #$
	dUu	E
 D'P ,W   %. .!
	dUu	E
 	d3eV"@4P	5
 
s	8	 

sC	D	 	.
 	.
 	?
 	-
=  Q 4  "^=@ +
	dUu	E
 j$-RS
R  T   
R .!
	dUu	E
 
j$-RS  T   " ,
	dUu	E
 j$-RSN  T   N  - 
	dUu	E
 	-
 	-
 !    !&!4 +
	dUu	E
 j$-QRL  S  L  
	dUu	E
 
zuzzD5UK	L	F	 dGT0WXmT0VW
s	* 	A
 2  X Y   :2j 	dUt	D
	 	A
 
zuzzD5UK	L	F	 	U
 dGT0WXmT0VW
s	* 	> 	,	 j$-UVh+LM	dUu	E	/
 	6
 2 1Q  N W	 X Y   rQh5!p 
	dUu	E
 	dIC.TUdJS4>QR		<
 Z,N i,TUH    V S V   ,HV 
	dUu	E
 	dJS4>QRi56dIC.TU
s	- 		<
 		
 5    V 7 S  <5p 
	dUu	E
 #(    $ (V 
	dUu	E
 #gt*EF5    G $ 5p 
	dUu	E
 U3  4 * 
	dUu	E
 9%P
 	= hS46HIc3Z$5N d$3DUVlt:WXS46X f34VW$Q 
s	,	 34U 
sC	Z 		6 gt*KLH&  M X Y W J  lH&V 
	dUu	E
 	d3}#R m"VW
s	0	    X 4  & 6 .!
	dUu	E
 
	dTd	C	 		+ eV"@4Pi,PQk4.WXj$-UVhS'9:h+QR	: dJ3NOu  P	 S ; W Y R Q  "@up "#
	dUuT	R
 #		& h+RSS";STk4=VW$4N I  X U T $ $,I,AH 
"u5+t*N 	<
	+ 6 	+ 
"t4dGT0LM+ N 5 +'T (*+	dUu	E
 c"	2=S!	d9S $X t"T i56
s	- jUV	3	:	;	 h+LM4NOdJ3NOfgt2TUc V P P N W 7 " > # ,JcL "#	dUu	E
 c"C(			
 
 	3'* 		f 		K lJ;WXM  Y+ ) # $ZM> #$	dUu	E
 c"C(0  ) # %02 )4 4
   c  "	 

 * L	
 
F & 1 ( ( 1 1    $ &%& '(  )08	
,3*/Sf  Fs   BD* D*BD5D4BD5