<s> from django . utils . functional import cached_property <EOL> from django . utils import six <EOL> from django . db . models import Transform , Lookup , CharField <EOL> class KeyTransform ( Transform ) : <EOL> output_field = CharField ( ) <EOL> def __init__ ( self , key , base_field , * args , ** kwargs ) : <EOL> super ( KeyTransform , self ) . __init__ ( * args , ** kwargs ) <EOL> try : <EOL> self . key = int ( key ) <EOL> except ValueError : <EOL> self . key = key <EOL> self . base_field = base_field <EOL> def relabeled_clone ( self , relabels ) : <EOL> return self . __class__ ( <EOL> self . key , <EOL> self . base_field , <EOL> self . lhs . relabeled_clone ( relabels ) , <EOL> [ '<STR_LIT>' % self . key ] <EOL> ) <EOL> def as_sql ( self , qn , connection ) : <EOL> lhs , params = qn . compile ( self . lhs ) <EOL> if isinstance ( self . key , int ) : <EOL> return "<STR_LIT>" % ( lhs , self . key ) , params <EOL> return "<STR_LIT>" % ( lhs , self . key ) , params <EOL> @ cached_property <EOL> def output_type ( self ) : <EOL> return self . base_field <EOL> class KeyTransformFactory ( object ) : <EOL> def __init__ ( self , key , base_field ) : <EOL> self . key = key <EOL> self . base_field = base_field <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> return KeyTransform ( self . key , self . base_field , * args , ** kwargs ) <EOL> class ExactLookup ( Lookup ) : <EOL> lookup_name = '<STR_LIT>' <EOL> def as_sql ( self , qn , connection ) : <EOL> lhs , lhs_params = self . process_lhs ( qn , connection ) <EOL> rhs , rhs_params = self . process_rhs ( qn , connection ) <EOL> if len ( rhs_params ) == <NUM_LIT:1> and hasattr ( rhs_params [ <NUM_LIT:0> ] , "<STR_LIT>" ) : <EOL> adapted = rhs_params [ <NUM_LIT:0> ] . adapted <EOL> if isinstance ( adapted , six . string_types ) : <EOL> rhs_params [ <NUM_LIT:0> ] = adapted <EOL> params = lhs_params + rhs_params <EOL> return '<STR_LIT>' % ( lhs , rhs ) , params <EOL> class ArrayLengthLookup ( Lookup ) : <EOL> lookup_name = '<STR_LIT>' <EOL> def as_sql ( self , qn , connection ) : <EOL> lhs , lhs_params = self . process_lhs ( qn , connection ) <EOL> rhs , rhs_params = self . process_rhs ( qn , connection ) <EOL> params = lhs_params + rhs_params <EOL> return '<STR_LIT>' % ( lhs , rhs ) , params <EOL> class JsonBArrayLengthLookup ( Lookup ) : <EOL> lookup_name = '<STR_LIT>' <EOL> def as_sql ( self , qn , connection ) : <EOL> lhs , lhs_params = self . process_lhs ( qn , connection ) <EOL> rhs , rhs_params = self . process_rhs ( qn , connection ) <EOL> params = lhs_params + rhs_params <EOL> return '<STR_LIT>' % ( lhs , rhs ) , params <EOL> class JsonBContainsLookup ( Lookup ) : <EOL> """<STR_LIT>""" <EOL> lookup_name = '<STR_LIT>' <EOL> def as_sql ( self , qn , connection ) : <EOL> lhs , lhs_params = self . process_lhs ( qn , connection ) <EOL> rhs , rhs_params = self . process_rhs ( qn , connection ) <EOL> params = lhs_params + rhs_params <EOL> return "<STR_LIT>" . format ( lhs , rhs ) , params <EOL> class JsonBHasLookup ( Lookup ) : <EOL> """<STR_LIT>""" <EOL> lookup_name = '<STR_LIT>' <EOL> def as_sql ( self , qn , connection ) : <EOL> lhs , lhs_params = self . process_lhs ( qn , connection ) <EOL> rhs , rhs_params = self . process_rhs ( qn , connection ) <EOL> params = lhs_params + rhs_params <EOL> return "<STR_LIT>" . format ( lhs , rhs ) , params <EOL> class JsonBHasAnyLookup ( Lookup ) : <EOL> """<STR_LIT>""" <EOL> lookup_name = '<STR_LIT>' <EOL> def as_sql ( self , qn , connection ) : <EOL> lhs , lhs_params = self . process_lhs ( qn , connection ) <EOL> rhs , rhs_params = self . process_rhs ( qn , connection ) <EOL> params = lhs_params + rhs_params <EOL> return "<STR_LIT>" . format ( lhs , rhs ) , params <EOL> class JsonBHasAllLookup ( Lookup ) : <EOL> """<STR_LIT>""" <EOL> lookup_name = '<STR_LIT>' <EOL> def as_sql ( self , qn , connection ) : <EOL> lhs , lhs_params = self . process_lhs ( qn , connection ) <EOL> rhs , rhs_params = self . process_rhs ( qn , connection ) <EOL> params = lhs_params + rhs_params <EOL> return "<STR_LIT>" . format ( lhs , rhs ) , params </s>
