<s> from itertools import product <EOL> from funcy . py2 import map , cat <EOL> from django . db . models . query import QuerySet <EOL> from django . db . models . sql import OR <EOL> from django . db . models . sql . query import Query , ExtraWhere <EOL> from django . db . models . sql . where import NothingNode , SubqueryConstraint <EOL> from django . db . models . lookups import Lookup , Exact , In , IsNull <EOL> try : <EOL> from django . db . models . sql . where import EverythingNode <EOL> except ImportError : <EOL> class EverythingNode ( object ) : <EOL> pass <EOL> try : <EOL> from django . db . models . sql . expressions import SQLEvaluator <EOL> except ImportError : <EOL> class SQLEvaluator ( object ) : <EOL> pass <EOL> try : <EOL> from django . db . models . sql . datastructures import Join <EOL> except ImportError : <EOL> class Join ( object ) : <EOL> pass <EOL> from . utils import NOT_SERIALIZED_FIELDS <EOL> LONG_DISJUNCTION = <NUM_LIT:8> <EOL> def dnfs ( qs ) : <EOL> """<STR_LIT>""" <EOL> SOME = object ( ) <EOL> SOME_TREE = [ [ ( None , None , SOME , True ) ] ] <EOL> def negate ( term ) : <EOL> return ( term [ <NUM_LIT:0> ] , term [ <NUM_LIT:1> ] , term [ <NUM_LIT:2> ] , not term [ <NUM_LIT:3> ] ) <EOL> def _dnf ( where ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( where , Lookup ) : <EOL> if not hasattr ( where . lhs , '<STR_LIT:target>' ) : <EOL> return SOME_TREE <EOL> if isinstance ( where . rhs , ( QuerySet , Query , SQLEvaluator ) ) : <EOL> return SOME_TREE <EOL> if isinstance ( where . lhs . target , NOT_SERIALIZED_FIELDS ) : <EOL> return SOME_TREE <EOL> attname = where . lhs . target . attname <EOL> if isinstance ( where , Exact ) : <EOL> return [ [ ( where . lhs . alias , attname , where . rhs , True ) ] ] <EOL> elif isinstance ( where , IsNull ) : <EOL> return [ [ ( where . lhs . alias , attname , None , where . rhs ) ] ] <EOL> elif isinstance ( where , In ) and len ( where . rhs ) < LONG_DISJUNCTION : <EOL> return [ [ ( where . lhs . alias , attname , v , True ) ] for v in where . rhs ] <EOL> else : <EOL> return SOME_TREE <EOL> elif isinstance ( where , EverythingNode ) : <EOL> return [ [ ] ] <EOL> elif isinstance ( where , NothingNode ) : <EOL> return [ ] <EOL> elif isinstance ( where , ( ExtraWhere , SubqueryConstraint ) ) : <EOL> return SOME_TREE <EOL> elif len ( where ) == <NUM_LIT:0> : <EOL> return [ [ ] ] <EOL> else : <EOL> chilren_dnfs = map ( _dnf , where . children ) <EOL> if len ( chilren_dnfs ) == <NUM_LIT:0> : <EOL> return [ [ ] ] <EOL> elif len ( chilren_dnfs ) == <NUM_LIT:1> : <EOL> result = chilren_dnfs [ <NUM_LIT:0> ] <EOL> else : <EOL> if where . connector == OR : <EOL> result = cat ( chilren_dnfs ) <EOL> else : <EOL> result = map ( cat , product ( * chilren_dnfs ) ) <EOL> if where . negated : <EOL> result = [ map ( negate , p ) for p in product ( * result ) ] <EOL> return result <EOL> def clean_conj ( conj , for_alias ) : <EOL> return [ ( attname , value ) for alias , attname , value , negation in conj <EOL> if value is not SOME and negation and alias == for_alias ] <EOL> def clean_dnf ( tree , for_alias ) : <EOL> cleaned = [ clean_conj ( conj , for_alias ) for conj in tree ] <EOL> if not all ( cleaned ) : <EOL> return [ [ ] ] <EOL> return map ( sorted , cleaned ) <EOL> def table_for ( alias ) : <EOL> if alias == main_alias : <EOL> return model . _meta . db_table <EOL> join = qs . query . alias_map [ alias ] <EOL> return join . table_name if isinstance ( join , Join ) else join [ <NUM_LIT:0> ] <EOL> where = qs . query . where <EOL> model = qs . model <EOL> main_alias = model . _meta . db_table <EOL> dnf = _dnf ( where ) <EOL> aliases = { alias for alias , cnt in qs . query . alias_refcount . items ( ) if cnt } | { main_alias } - { '<STR_LIT>' } <EOL> return [ ( table_for ( alias ) , clean_dnf ( dnf , alias ) ) for alias in aliases ] <EOL> def attname_of ( model , col , cache = { } ) : <EOL> if model not in cache : <EOL> cache [ model ] = { f . db_column : f . attname for f in model . _meta . fields } <EOL> return cache [ model ] . get ( col , col ) </s>
