[ Avaa Bypassed ]




Upload:

Command:

hmhc3928@3.143.254.28: ~ $


R6�W‹�&@s�dZddlZddlZddlZddlZddlZddlZddlZyddlZ	Wne
k
r�ddl	Z	YnXyddlmZWne
k
r�YnXddl
Tddl
mZddl
mZddl
mZddl
mZdd	l
mZdd
l
mZddl
mZddl
mZdd
l
mZddl
mZddl
mZddl
mZyddlmZWne
k
r�dZYnXejddkr�eZdZ dZ!ej"dd�dddfko�dp�dZ#dddfZ$Gdd�de�Z%Gdd�de�Z&Gdd�de'�Z(Gd d!�d!e)�Z*Gd"d#�d#e+�Z,Gd$d%�d%e-�Z.Gd&d'�d'e.e+�Z/Gd(d)�d)e.e0�Z1Gd*d+�d+e.e2�Z3Gd,d-�d-e.e4�Z5Gd.d/�d/e6�Z7Gd0d1�d1e7�Z8Gd2d3�d3e8�Z9d4Z:e;d5d6d7d8e<d9�d:d;d<d=d>d?d@dAg
�e;dB�Be;e:�Be;e:j=��BZ>e;dCdD�e?dE�D��Z@ejAdF�ZBGdGdH�dHe8�ZCddIdJ�ZDGdKdL�dLe�ZEejFdMdN�dOdP��ZGGdQdR�dReH�ZIGdSdT�dTe�ZJdUe_KeIjLejKdVi�dWdU�ZMdXdY�ZNdZd[�ZOd\d]�ZPdS)^a�
Sqlite3 extensions
==================

* Define custom aggregates, collations and functions
* Basic support for virtual tables
* Basic support for FTS3/4
* Specify isolation level in transactions

Example usage of the Full-text search:

class Document(FTSModel):
    title = TextField()  # type affinities are ignored in FTS
    content = TextField()

Document.create_table(tokenize='porter')  # use the porter stemmer

# populate the documents using normal operations.
for doc in documents:
    Document.create(title=doc['title'], content=doc['content'])

# use the "match" operation for FTS queries.
matching_docs = Document.select().where(match(Document.title, 'some query'))

# to sort by best match, use the custom "rank" function.
best_docs = (Document
             .select(Document, Document.rank('score'))
             .where(match(Document.title, 'some query'))
             .order_by(SQL('score')))

# or use the shortcut method.
best_docs = Document.match('some phrase')
�N)�
TableFunction)�*)�EnclosedClause)�Entity)�
Expression)�Node)�OP)�SqliteQueryCompiler)�_AutoPrimaryKeyField)�sqlite3)�transaction)�_sqlite_date_part)�_sqlite_date_trunc)�_sqlite_regexp)�_sqlite_ext�ZpcnalxZpcx��ZFTS4ZFTS3�	c@seZdZdZdZdS)�
RowIDFieldz�
    Field used to access hidden primary key on FTS5 or any other SQLite
    table that does not have a separately-defined primary key.
    �rowidN)�__name__�
__module__�__qualname__�__doc__�_column_name�rr�/sqlite_ext.pyrOsrc@seZdZdZdZdS)�
DocIDFieldz9Field used to access hidden primary key on FTS3/4 tables.�docidN)rrrrrrrrrrWsrcs(eZdZdZ�fdd�Z�S)�PrimaryKeyAutoIncrementFielda
    SQLite by default uses MAX(primary key) + 1 to set the ID on a new row.
    Using the `AUTOINCREMENT` field, the IDs will increase monotonically
    even if rows are deleted. Use this if you need to guarantee IDs are not
    re-used in the event of deletion.
    cs)tt|�j|�}|td�gS)NZ
AUTOINCREMENT)�superr �__ddl__�SQL)�selfZcolumn_typeZddl)�	__class__rrr"csz$PrimaryKeyAutoIncrementField.__ddl__)rrrrr"rr)r%rr \sr c@s�eZdZdd�Zdd�Zdd�Zddd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
ddd�Zddd�Zddd�ZdS)�	JSONFieldcCs@|dk	r<ytj|�SWnttfk
r;|SYnXdS)N)�json�loads�	TypeError�
ValueError)r$�valuerrr�python_valueis
zJSONField.python_valuecCs|dk	rtj|�SdS)N)r'�dumps)r$r+rrr�db_valuepszJSONField.db_valuecCs&|jd�s|rd|Sd|S)N�[z$%sz$.%s)�
startswith)r$�pathrrr�
clean_pathtszJSONField.clean_pathNcCs,|rtj||j|��Stj|�S)N)�fnZjson_array_lengthr2)r$r1rrr�lengthyszJSONField.lengthcCstj||j|��S)N)r3Zjson_extractr2)r$r1rrr�extract~szJSONField.extractcCs2t|tttf�r.tjtj|��S|S)N)�
isinstance�list�tuple�dictr3r'r-)r$r+rrr�_value_for_insertion�szJSONField._value_for_insertioncCs�t|�}|ddkr(td��g}xRtd|d�D]>}|j|j||��|j|j||d��qAW|||�S)N�rz%Mismatched path and value parameters.�)�lenr*�range�appendr2r:)r$r3�pairsZnpairs�accum�irrr�_insert_like�s"zJSONField._insert_likecGs|jtj|�S)N)rCr3Zjson_insert)r$r@rrr�insert�szJSONField.insertcGs|jtj|�S)N)rCr3Zjson_replace)r$r@rrr�replace�szJSONField.replacecGs|jtj|�S)N)rCr3Zjson_set)r$r@rrr�set�sz
JSONField.setcs#tj��fdd�|D��S)Ncsg|]}�j|��qSr)r2)�.0r1)r$rr�
<listcomp>�s	z$JSONField.remove.<locals>.<listcomp>)r3Zjson_remove)r$�pathsr)r$r�remove�szJSONField.removecCs,|rtj||j|��Stj|�S)N)r3�	json_typer2)r$r1rrrrK�szJSONField.json_typecCs,|rtj||j|��Stj|�S)a?
        Schema of `json_each` and `json_tree`:

        key,
        value,
        type TEXT (object, array, string, etc),
        atom (value for primitive/scalar types, NULL for array and object)
        id INTEGER (unique identifier for element)
        parent INTEGER (unique identifier of parent element or NULL)
        fullkey TEXT (full path describing element)
        path TEXT (path to the container of the current element)
        json JSON hidden (1st input parameter to function)
        root TEXT hidden (2nd input parameter, path at which to start)
        )r3Z	json_eachr2)r$r1rrr�children�szJSONField.childrencCs,|rtj||j|��Stj|�S)N)r3Z	json_treer2)r$r1rrr�tree�szJSONField.tree)rrrr,r.r2r4r5r:rCrDrErFrJrKrLrMrrrrr&hs
r&csCeZdZdZddd�fdd�Z�fdd�Z�S)�SearchFielda-
    Field class to be used with full-text search extension. Since the FTS
    extensions do not support any field types besides `TEXT`, and furthermore
    do not support secondary indexes, using this field will prevent you
    from mistakenly creating the wrong kind of field on your FTS table.
    FNcsTddd|d|i}||_|r:td�g|d<tt|�j|�dS)NZnullT�	db_column�coerceZ	UNINDEXEDZconstraints)�
_unindexedr#r!rN�__init__)r$Z	unindexedrOrP�_�kwargs)r%rrrR�s
	zSearchField.__init__cs(tt|�j|�}|j|_|S)N)r!rN�
clone_baserQ)r$rT�clone)r%rrrU�szSearchField.clone_base)rrrrrRrUrr)r%rrN�srNcs(eZdZdZ�fdd�Z�S)�_VirtualFieldMixinzx
    Field mixin to support virtual table attributes that may not correspond
    to actual columns in the database.
    cs-tt|�j||�|jj|�dS)N)r!rW�add_to_class�_metaZremove_field)r$�model_class�name)r%rrrX�sz_VirtualFieldMixin.add_to_class)rrrrrXrr)r%rrW�srWc@seZdZdS)�VirtualFieldN)rrrrrrrr\�sr\c@seZdZdS)�VirtualIntegerFieldN)rrrrrrrr]�sr]c@seZdZdS)�VirtualCharFieldN)rrrrrrrr^�sr^c@seZdZdS)�VirtualFloatFieldN)rrrrrrrr_�sr_c@sJeZdZGdd�d�Zedd��Zeddd��ZdS)	�VirtualModelc@s"eZdZdZdZiZdS)zVirtualModel.MetaTN)rrrZ
virtual_table�extension_module�extension_optionsrrrr�Meta�srccKs|S)Nr)�cls�optionsrrr�
clean_options�szVirtualModel.clean_optionsFcKs=|r|j�rdS|jjj|d|�|j�dS)Nre)Ztable_existsrY�database�create_tableZ_create_indexes)rdZ
fail_silentlyrerrrrh�szVirtualModel.create_tableN)rrrrc�classmethodrfrhrrrrr`�sr`c@s"eZdZedd��ZdS)�BaseFTSModelcKs[|jd�}|jd�}|r2d||d<t|t�rW|dkrWd|d<|S)N�tokenize�contentz"%s"�z'')�getr6�
basestring)rdrerkrlrrrrf�s
zBaseFTSModel.clean_optionsN)rrrrirfrrrrrj�srjc@smeZdZdZe�ZGdd�d�Zedd��Zedd��Z	edd	��Z
ed
d��Zedd
��Zedddd��Z
eddd��Zedd��Zedd��Zedd��Zedd��Zedd��Zedd d!d d"d#��Zedd d!d d$d%��Zedd d!d d&d'��ZdS)(�FTSModela_
    VirtualModel class for creating tables that use either the FTS3 or FTS4
    search extensions. Peewee automatically determines which version of the
    FTS extension is supported and will use FTS4 if possible.

    Note: because FTS5 is significantly different from FTS3 and FTS4, there is
    a separate model class for FTS5 virtual tables.
    c@seZdZeZdS)z
FTSModel.MetaN)rrr�FTS_VERrarrrrrcsrccCs%|jjjdkr!td��dS)Nrz:FTSModel classes must use the default `docid` primary key.)rY�primary_keyr[�ImproperlyConfigured)rdrrr�validate_modelszFTSModel.validate_modelcCs8|jj}|jjjd|||f�}|j�S)Nz INSERT INTO %s(%s) VALUES('%s');)rY�db_tablerg�execute_sqlZfetchone)rd�cmd�tbl�resrrr�_fts_cmdszFTSModel._fts_cmdcCs
|jd�S)N�optimize)rz)rdrrrr{$szFTSModel.optimizecCs
|jd�S)N�rebuild)rz)rdrrrr|(szFTSModel.rebuildcCs
|jd�S)Nzintegrity-check)rz)rdrrr�integrity_check,szFTSModel.integrity_check���cCs|jd||f�S)Nzmerge=%s,%s)rz)rdZblocksZsegmentsrrr�merge0szFTSModel.mergeTcCs|jd|rdpd�S)Nzautomerge=%s�1�0)rz)rd�staterrr�	automerge4szFTSModel.automergecCst|j�|�S)zU
        Generate a `MATCH` expression appropriate for searching this table.
        )�match�	as_entity)rd�termrrrr�8szFTSModel.matchcGs"tjtj|j�t�|�S)N)r3�fts_rank�	matchinfor��FTS_MATCHINFO_FORMAT_SIMPLE)rd�weightsrrr�rank?sz
FTSModel.rankcGs(tj|j�t�}tj||�S)N)r3r�r��FTS_MATCHINFO_FORMAT�fts_bm25)rdr��
match_inforrr�bm25Dsz
FTSModel.bm25cGs(tj|j�t�}tj||�S)N)r3r�r�r��
fts_lucene)rdr�r�rrr�luceneIszFTSModel.lucenecCs�|s|�}not|t�rug}x<|jjD].}	|j|j|	|j|	jd���q4W||�}n||�}f}
|}|r�||j|�f}
|r�|r�t|�}|j	|
�j
|j|��j|�S)Ng�?)
r6r9rY�declared_fieldsr?rnr[�aliasr#�select�wherer��order_by)rdr�r��
with_score�score_aliasZscore_fn�explicit_orderingr��weight_args�field�	selectionr�rrr�_searchNs&&
	zFTSModel._searchNF�scorecCs|j|||||j|�S)z'Full-text search using selected `term`.)r�r�)rdr�r�r�r�r�rrr�searchhszFTSModel.searchcCs|j|||||j|�S)z:Full-text search for selected `term` using BM25 algorithm.)r�r�)rdr�r�r�r�r�rrr�search_bm25tszFTSModel.search_bm25cCs|j|||||j|�S)z:Full-text search for selected `term` using BM25 algorithm.)r�r�)rdr�r�r�r�r�rrr�
search_lucene�szFTSModel.search_lucene)rrrrrrrcrirtrzr{r|r}r�r�r�r�r�r�r�r�r�r�rrrrrps4		

rp�abcdefghijklmnopqrstuvwxyz�	� �,�"��(�)�{�}r�:rS�+�
0123456789cCs.g|]$}t|�tkrt|��qSr)�chr�	_alphanum)rG�prrrrH�s	rH�z(?:[^\s"]|"(?:\.|[^"])*")+c@s�eZdZdZe�ZGdd�d�Zdddddd	iZed
d��Z	edd
��Z
edd��Zee
d�dd��Zedd��Zedd��Zedd��Zedddddd��Zedddddd��Zed d!��Zed"d#��Zed$d%��Zed&d'��Zed(d)��Zed*d+��Zed,dd-d.��ZdS)/�	FTS5Modelan
    Requires SQLite >= 3.9.0.

    Table options:

    content: table name of external content, or empty string for "contentless"
    content_rowid: column name of external content primary key
    prefix: integer(s). Ex: '2' or '2 3 4'
    tokenize: porter, unicode61, ascii. Ex: 'porter unicode61'

    The unicode tokenizer supports the following parameters:

    * remove_diacritics (1 or 0, default is 1)
    * tokenchars (string of characters, e.g. '-_'
    * separators (string of characters)

    Parameters are passed as alternating parameter name and value, so:

    {'tokenize': "unicode61 remove_diacritics 0 tokenchars '-_'"}

    Content-less tables:

    If you don't need the full-text content in it's original form, you can
    specify a content-less table. Searches and auxiliary functions will work
    as usual, but the only values returned when SELECT-ing can be rowid. Also
    content-less tables do not support UPDATE or DELETE.

    External content tables:

    You can set up triggers to sync these, e.g.

    -- Create a table. And an external content fts5 table to index it.
    CREATE TABLE tbl(a INTEGER PRIMARY KEY, b);
    CREATE VIRTUAL TABLE ft USING fts5(b, content='tbl', content_rowid='a');

    -- Triggers to keep the FTS index up to date.
    CREATE TRIGGER tbl_ai AFTER INSERT ON tbl BEGIN
      INSERT INTO ft(rowid, b) VALUES (new.a, new.b);
    END;
    CREATE TRIGGER tbl_ad AFTER DELETE ON tbl BEGIN
      INSERT INTO ft(fts_idx, rowid, b) VALUES('delete', old.a, old.b);
    END;
    CREATE TRIGGER tbl_au AFTER UPDATE ON tbl BEGIN
      INSERT INTO ft(fts_idx, rowid, b) VALUES('delete', old.a, old.b);
      INSERT INTO ft(rowid, b) VALUES (new.a, new.b);
    END;

    Built-in auxiliary functions:

    * bm25(tbl[, weight_0, ... weight_n])
    * highlight(tbl, col_idx, prefix, suffix)
    * snippet(tbl, col_idx, prefix, suffix, ?, max_tokens)
    c@seZdZdZdS)zFTS5Model.Meta�fts5N)rrrrarrrrrc�src�
field_typezQBesides the implicit `rowid` column, all columns must be instances of SearchField�indexz3Secondary indexes are not supported for FTS5 models�pkz4FTS5 models must use the default `rowid` primary keycCs�|jjjdkr(t|jd��xB|jjj�D].}t|tt	f�s;t|jd��q;W|jj
r�t|jd��dS)Nrr�r�r�)rYrrr[rs�_error_messagesZfields�valuesr6rNrZindexes)rdr�rrrrt�szFTS5Model.validate_modelcCs�tjdd�tkrdStjd�}z`y|jd�WnHytjd�tjd�WndSYnX|jjjd�YnXWd|j	�XdS)NrFz:memory:z0CREATE VIRTUAL TABLE fts5test USING fts5 (data);Tr�)
r�sqlite_version_info�FTS5_MIN_VERSION�connect�execute�enable_load_extension�load_extensionrYrg�close)rdZtmp_dbrrr�fts5_installed�s
	zFTS5Model.fts5_installedcCsYtj|�}xC|D];}|jd�r=|jd�r=qt|�t@rdSqWdS)z�
        Simple helper function to indicate whether a search query is a
        valid FTS5 query. Note: this simply looks at the characters being
        used, and is not guaranteed to catch all problematic queries.
        r�FT)�	_quote_re�findallr0�endswithrF�_invalid_ascii)�query�tokens�tokenrrr�validate_query�s
zFTS5Model.validate_queryr�c	Cs�g}d}tj|�}x�|D]�}|jd�rV|jd�rV|j|�q"t|�}|t@}|r�d}x |D]}|j||�}qW|j|�q"W|r�dj|�S|S)z2
        Clean a query of invalid tokens.
        Fr�Tr�)	r�r�r0r�r?rFr�rE�join)	r�rErAZany_invalidr�r�Z	token_setZinvalid_for_token�crrr�clean_querys"




zFTS5Model.clean_querycCst|j�|�S)zU
        Generate a `MATCH` expression appropriate for searching this table.
        )r�r�)rdr�rrrr�$szFTS5Model.matchcGs!|r|j|�Std�SdS)Nr�)r�r#)rd�argsrrrr�+s
zFTS5Model.rankcGstj|j�|�S)N)r3r�r�)rdr�rrrr�2szFTS5Model.bm25NFr�cCs"|jtj|�||||�S)z'Full-text search using selected `term`.)r�r�r�)rdr�r�r�r�r�rrrr�6szFTS5Model.searchcCs
|std�}n�t|t�r�g}x<|jjD].}|j|j||j|jd���q7Wtj	|j
�|�}ntj	|j
�|�}f}	|}
|r�||j|�f}	|r�|r�t|�}
|j|	�j
|jtj|���j|
�S)z'Full-text search using selected `term`.r�g�?)r#r6r9rYr�r?rnr[r3r�r�r�r�r�r�r�r�r�)rdr�r�r�r�r�r�r�r�r�r�rrrr�As&&
	zFTS5Model.search_bm25c
Ks�|j�}|g}|g}x:|j�D],\}}|jt|��|j|�q+Wt|�}ttd�|j�t|�td�t|��}	|jjj	|	�S)NzINSERT INTOZVALUES)
r��itemsr?rr�Clauser#rYrgr�)
rdrwZextra_paramsrx�columnsr��keyr+Zinner_clause�clauserrrrz\s						zFTS5Model._fts_cmdcCs;d|kodkns(td��|jdd|�S)Nr�zlevel must be between 0 and 16r�r�)r*rz)rd�levelrrrr�nszFTS5Model.automergecCs|jdd|�S)Nr�r�)rz)rdZnpagesrrrr�tszFTS5Model.mergecCs|jdd|�S)N�pgszr�)rz)rdr�rrr�set_pgszxszFTS5Model.set_pgszcCs|jdd|�S)Nr�)rz)rdZrank_expressionrrr�set_rank|szFTS5Model.set_rankcCs
|jd�S)Nz
delete-all)rz)rdrrr�
delete_all�szFTS5Model.delete_all�rowc
s��dkrtd��d�}t�|�s�G���fdd�d�}dt�dt�d	t�d
t�d|i}�dkr�t�|d<d�j}t�|t|tf|��t	�|�S)
Nr��colz)table_type must be either "row" or "col".z_vocab_model_%scsPeZdZ�jjZ�p+�jjdZej�j�e	���Z
dS)z"FTS5Model.VocabModel.<locals>.MetaZ_vN)rrrrYrgrur3Z	fts5vocabr�r#rar)rd�
table_name�
table_typerrrc�s
	rcr��docZcntrz%sVocab)r�r�)
r*�hasattr�	BareField�IntegerFieldrr�setattr�typer`�getattr)rdr�r��attrrcZattrs�
class_namer)rdr�r�r�
VocabModel�s
				

zFTS5Model.VocabModel)rrrrrrrcr�rirtr��staticmethodr�r�r�r�r�r�r�r�rzr�r�r�r�r�r�rrrrr��s85			r�cs��dkrKx<�jjj�D]}|j�kr|�PqWtd���jj�G���fdd�dt�}G��fdd�d�}d�j}t||fd|i�S)z3Model factory for the transitive closure extension.Nz,Unable to find self-referential foreign key.cs�eZdZe�Ze�Ze�Ze�Ze�Ze	�Z
Gdd�d�Zedd��fdd��Z
edd��fdd��Zed���fd	d
��ZdS)z&ClosureTable.<locals>.BaseClosureTablec@seZdZdZdS)z+ClosureTable.<locals>.BaseClosureTable.MetaZtransitive_closureN)rrrrarrrrrc�srcNFcs��j�|jjd��j|d�|jk�j|j|k�}|dk	ro|j|j|k�}n|s�|j|jdk�}|S)N�depth�onr)r�r�r�r��idr��root)rd�noder��include_noder�)rZrrrr�descendants�sz2ClosureTable.<locals>.BaseClosureTable.descendantscs��j�|jjd��j|d�|jk�j|j|k�}|ri|j|j|k�}n|s�|j|jdk�}|S)Nr�r�r)r�r�r�r�r�r�r�)rdr�r�r�r�)rZrrrr�	ancestors�sz0ClosureTable.<locals>.BaseClosureTable.ancestorscsO|jj�j�}�j�j�|k�}|sK|j�|k�}|S)N)�_datarnr[r�r�)rdr�r�Zfk_valuer�)�foreign_keyrZrrrr�siblings�s
z/ClosureTable.<locals>.BaseClosureTable.siblings)rrrr]r�r��idcolumn�parentcolumnr�r^�	tablenamercrir�r�r�r)r�rZrrrr�BaseClosureTable�s						r�csLeZdZ�jjZd�jjd�jjjd�jiZdZdS)zClosureTable.<locals>.Metar�r�r�FN)	rrrrYrgrurrrOrbr)r�rZrrrc�s
rcz	%sClosure)	rYZrelr�Z	rel_modelr*rrr`rr�)rZr�Z	field_objr�rcr[r)r�rZrrr�ClosureTable�s"+
r�csLeZdZdZdd�fdd�Zdd�Zdddd	�Z�S)
�SqliteExtQueryCompilerzQ
    Subclass of QueryCompiler that can be used to construct virtual tables.
    FNc
stt|�j|d|�}t|t�r�d}|jj}t|t�r�|j	dd�t
d�|g}t|�}q�|j	jdt
d|��nd}|r�|d7}t
|�|j	d<|j
|||�}|r�|j	d}	x�t|j��D]�\}
}t|t�r7|j|d
k�}n�tj|�rdt|t�rd|j�}nUt|ttf�r�t
ddjtt|���}nt|t�s�t
|�}tt
|
�|�}d
|_|	j	j|�qWt|jdd�r|j	jt
d��|S)N�safezCREATE VIRTUAL TABLEr;ZUSINGzUSING %szCREATE TABLEz IF NOT EXISTSrr<r�z'%s'r��=Z
without_rowidz
WITHOUT ROWID���)r!r��
_create_table�
issubclassr`rYrar6rZnodesr#r�rDrf�sortedr�ZFieldr��inspectZisclass�Modelr7r8r��map�str�gluer?r�)
r$rZr�rer�Z	statement�	extension�partsZ
table_optionsZcolumns_constraints�k�vZoption)r%rrr��s># 

%	z$SqliteExtQueryCompiler._create_tablecCsSt|jdd�}|r-|j|�}ni}|rO|j|j|��|S)Nrb)r�rYrf�update)r$rZr�Z
extra_optionsZ
model_optionsrerrrrf sz$SqliteExtQueryCompiler.clean_optionscCs|j|j|||��S)N)Z
parse_noder�)r$rZr�rerrrrh*sz#SqliteExtQueryCompiler.create_table)rrrrr�rfrhrr)r%rr��s6
r�rVFcCstd|dd�S)Nr�rrm)r�)r$rrr�
disqualify.sr
csceZdZdZeZd�fdd�Zedd��Zdd�Z	d	d
�Z
dd�Zd
d�Zdd�Z
dd3dd�Zdd4dd�Zddd�Zddd�Zdd5dd�Zdd6dd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+dd,d-�Zd+�fd.d/�Zd0d1d2�Z�S)7�SqliteExtDatabasez�
    Database class which provides additional Sqlite-specific functionality:

    * Register custom aggregates, collations and functions
    * Specify a row factory
    * Advanced transactions (specify isolation level)
    Tcsmtt|�j|||�i|_i|_i|_tg�|_d|_t	r|rd|_
|jt	jdd�|jt	j
dd�|jt	jdd�|jt	jdd�|jt	jdd
�|jt	jd	d�|jt	jd
d�nhd|_
|jtdd�|jtdd�|jtdd�|jtdd�|jtd	d�dS)NTZ	date_partr;Z
date_truncZregexpr�r<r�r�Z
murmurhashFr�r�r�r�r�)r!rrR�_aggregates�_collations�
_functionsrF�_extensions�_row_factory�_c_ext�_using_c_extensions�register_functionZpeewee_date_partZpeewee_date_truncZ
peewee_regexpZpeewee_rankZ
peewee_luceneZpeewee_bm25Zpeewee_murmurhashr
rrr�r�)r$rgZc_extensionsr�rT)r%rrrR?s*						zSqliteExtDatabase.__init__cCs|jS)N)r)r$rrr�using_c_extensionsWsz$SqliteExtDatabase.using_c_extensionscCsc|j|�|j|�|j|�|j|�|jrI|j|_|jr_|j|�dS)N)Z_set_pragmas�_load_aggregates�_load_collations�_load_functionsr�row_factoryr�_load_extensions)r$�connrrr�_add_conn_hooks[s



		z!SqliteExtDatabase._add_conn_hookscCs=x6|jj�D]%\}\}}|j|||�qWdS)N)rr�Zcreate_aggregate)r$rr[�klass�
num_paramsrrrres"z"SqliteExtDatabase._load_aggregatescCs4x-|jj�D]\}}|j||�qWdS)N)r
r�Zcreate_collation)r$rr[r3rrrrisz"SqliteExtDatabase._load_collationscCs=x6|jj�D]%\}\}}|j|||�qWdS)N)rr�Zcreate_function)r$rr[r3rrrrrms"z!SqliteExtDatabase._load_functionscCs2|jd�x|jD]}|j|�qWdS)NT)r�rr�)r$rrrrrrqs
z"SqliteExtDatabase._load_extensionsNr<cCsE||f|j|p!|jj�<|j�sA|j|j��dS)N)rr�lower�	is_closedr�get_conn)r$rr[rrrr�register_aggregatevs"z$SqliteExtDatabase.register_aggregatecs���fdd�}|S)Ncs�j|���|S)N)r!)r)r[rr$rr�	decorator|sz.SqliteExtDatabase.aggregate.<locals>.decoratorr)r$r[rr"r)r[rr$r�	aggregate{szSqliteExtDatabase.aggregatecsZ�p|j��fdd�}||_||j�<|j�sV|j|j��dS)Ncs!|td��f}t|�S)Nz
collate %s)r#r�)r�Zexpressions)r[rr�
_collation�sz8SqliteExtDatabase.register_collation.<locals>._collation)r�	collationr
rrr )r$r3r[r$r)r[r�register_collation�s	
z$SqliteExtDatabase.register_collationcs��fdd�}|S)Ncs�j|��|S)N)r&)r3)r[r$rrr"�sz.SqliteExtDatabase.collation.<locals>.decoratorr)r$r[r"r)r[r$rr%�szSqliteExtDatabase.collationcCs?||f|j|p|j<|j�s;|j|j��dS)N)rrrrr )r$r3r[rrrrr�sz#SqliteExtDatabase.register_functioncs���fdd�}|S)Ncs�j|���|S)N)r)r3)r[rr$rrr"�sz)SqliteExtDatabase.func.<locals>.decoratorr)r$r[rr"r)r[rr$r�func�szSqliteExtDatabase.funccCsF|jj|�|j�sB|j�}|jd�|j|�dS)NT)r�addrr r�r�)r$rrrrrr��s

z SqliteExtDatabase.load_extensioncCs|j|=dS)N)r)r$r[rrr�unregister_aggregate�sz&SqliteExtDatabase.unregister_aggregatecCs|j|=dS)N)r
)r$r[rrr�unregister_collation�sz&SqliteExtDatabase.unregister_collationcCs|j|=dS)N)r)r$r[rrr�unregister_function�sz%SqliteExtDatabase.unregister_functioncCs|jj|�dS)N)rrJ)r$rrrr�unload_extension�sz"SqliteExtDatabase.unload_extensioncCs
||_dS)N)r)r$r3rrrr�szSqliteExtDatabase.row_factoryFcCs1|j�j|||�\}}|j||�S)N)Zcompilerrhrv)r$rZr�reZsqlZparamsrrrrh�s!zSqliteExtDatabase.create_tablecs/t|t�rdStt|�j|||�S)N)r�rpr!r�create_index)r$rZ�
field_name�unique)r%rrr-�szSqliteExtDatabase.create_index�deferredcCs%|j�dkst�t||�S)Nr0�	immediate�	exclusive)r0r1r2)r�AssertionError�granular_transaction)r$�	lock_typerrrr4�sz&SqliteExtDatabase.granular_transactionr�r�r�r�)rrrrr�Zcompiler_classrR�propertyrrrrrrr!r#r&r%rr'r�r)r*r+r,rrhr-r4rr)r%rr5s0

rc@s+eZdZddd�Zdd�ZdS)r4r0cCs(||_|jj�|_||_dS)N)�dbr rr5)r$r7r5rrrrR�s	zgranular_transaction.__init__cCs|jj|j�dS)N)r7Zbeginr5)r$rrr�_begin�szgranular_transaction._beginN)rrrrRr8rrrrr4�sr4r��MATCHcCst|tj|�S)N)rrr9)Zlhs�rhsrrrr��scs/t��}�fdd�td|d�D�S)Ncs4g|]*}tjd�||d��d�qS)z@Irr)�struct�unpack)rGrB)�bufrrrH�s	z%_parse_match_info.<locals>.<listcomp>rr)r=r>)r=�bufsizer)r=r�_parse_match_info�sr?cGst|�}d}|dd�\}}|s>dg|}n4dg|}x$t|�D]\}}|||<qXWx�t|�D]�}d||d}	xst|�D]e}
||
}|s�q�|	|
d}|||d�\}}
|dkr�||t|�|
7}q�WqW|S)Ngr;r<rr)r?�	enumerater>�float)�raw_match_infor�r�r�r�r�rB�weightZ
phrase_numZphrase_info_idxZcol_numZcol_idxZx1Zx2rrrr��s&

 r�cGs�t|�}d}d}d}td�\}}}}	||}
||}||}|	|}
|
|}|s~dg|}n8dg|}x(t|�D]\}}||||<q�Wx@t|
�D]2}x)t|�D]}||}|dkr�q�t||	|�}t||
|�}|dkr2d}nd||||}|d||d}t||�}t||d�}ttj||d	|d	�d�}|||}|dkr�d}n||d|}||||7}q�Wq�W|S)
z�
    Usage:

        # Format string *must* be pcnalx
        # Second parameter to bm25 specifies the index of the column, on
        # the table being queries.
        bm25(matchinfo(document_tbl, 'pcnalx'), 1) AS rank
    g333333�?g�?grr<rrr;g�?)r?r>r@rA�max�math�log)rBr�r��K�Br�ZP_OZC_OZN_OZA_OZ
term_countZ	col_countZ
total_docsZL_OZX_Or�rBrC�jZ
avg_lengthZ
doc_length�D�xZterm_frequencyZdocs_with_termZidfZdenomr:rrrr��sN	






			r�)QrZglobrrE�os�rer;�sysZ
simplejsonr'�ImportErrorZvtfuncrZpeeweerrrrrr	r
rrr
rrZ	playhouserr�version_inforror�r�r�rqr�rrZPrimaryKeyFieldr Z	TextFieldr&r�rN�objectrWr\r�r]Z	CharFieldr^Z
FloatFieldr_rr`rjrpZ	_alphabetrFr��upperr�r>r��compiler�r�r�r��extendr
ZSqliteDatabaserr4r9Zregister_opsr�r?r�r�rrrr�<module>!s�



.R�]�CH�
	

Filemanager

Name Type Size Permission Actions
__init__.cpython-35.opt-1.pyc File 95 B 0644
__init__.cpython-35.pyc File 95 B 0644
apsw_ext.cpython-35.opt-1.pyc File 7.47 KB 0644
apsw_ext.cpython-35.pyc File 7.47 KB 0644
berkeleydb.cpython-35.opt-1.pyc File 3.2 KB 0644
berkeleydb.cpython-35.pyc File 3.2 KB 0644
csv_loader.cpython-35.opt-1.pyc File 135 B 0644
csv_loader.cpython-35.pyc File 135 B 0644
csv_utils.cpython-35.opt-1.pyc File 11.43 KB 0644
csv_utils.cpython-35.pyc File 11.43 KB 0644
dataset.cpython-35.opt-1.pyc File 13.38 KB 0644
dataset.cpython-35.pyc File 13.38 KB 0644
db_url.cpython-35.opt-1.pyc File 3.16 KB 0644
db_url.cpython-35.pyc File 3.16 KB 0644
djpeewee.cpython-35.opt-1.pyc File 5.76 KB 0644
djpeewee.cpython-35.pyc File 5.76 KB 0644
fields.cpython-35.opt-1.pyc File 12.7 KB 0644
fields.cpython-35.pyc File 12.7 KB 0644
flask_utils.cpython-35.opt-1.pyc File 5.93 KB 0644
flask_utils.cpython-35.pyc File 5.97 KB 0644
gfk.cpython-35.opt-1.pyc File 6.02 KB 0644
gfk.cpython-35.pyc File 6.02 KB 0644
hybrid.cpython-35.opt-1.pyc File 2.15 KB 0644
hybrid.cpython-35.pyc File 2.15 KB 0644
kv.cpython-35.opt-1.pyc File 6.77 KB 0644
kv.cpython-35.pyc File 6.77 KB 0644
migrate.cpython-35.opt-1.pyc File 22.68 KB 0644
migrate.cpython-35.pyc File 22.68 KB 0644
pool.cpython-35.opt-1.pyc File 7.96 KB 0644
pool.cpython-35.pyc File 7.96 KB 0644
postgres_ext.cpython-35.opt-1.pyc File 16.86 KB 0644
postgres_ext.cpython-35.pyc File 16.86 KB 0644
read_slave.cpython-35.opt-1.pyc File 1.75 KB 0644
read_slave.cpython-35.pyc File 1.75 KB 0644
reflection.cpython-35.opt-1.pyc File 18.58 KB 0644
reflection.cpython-35.pyc File 18.58 KB 0644
shortcuts.cpython-35.opt-1.pyc File 5.65 KB 0644
shortcuts.cpython-35.pyc File 5.65 KB 0644
signals.cpython-35.opt-1.pyc File 3.04 KB 0644
signals.cpython-35.pyc File 3.04 KB 0644
sqlcipher_ext.cpython-35.opt-1.pyc File 4.52 KB 0644
sqlcipher_ext.cpython-35.pyc File 4.52 KB 0644
sqlite_ext.cpython-35.opt-1.pyc File 36.56 KB 0644
sqlite_ext.cpython-35.pyc File 36.65 KB 0644
sqlite_udf.cpython-35.opt-1.pyc File 16.76 KB 0644
sqlite_udf.cpython-35.pyc File 16.76 KB 0644
test_utils.cpython-35.opt-1.pyc File 4.03 KB 0644
test_utils.cpython-35.pyc File 4.08 KB 0644