<s> from datetime import datetime , timedelta <EOL> import time <EOL> import six <EOL> from six . moves import filter <EOL> from cassandra . query import FETCH_SIZE_UNSET <EOL> from cassandra . cqlengine import columns <EOL> from cassandra . cqlengine import UnicodeMixin <EOL> from cassandra . cqlengine . functions import QueryValue <EOL> from cassandra . cqlengine . operators import BaseWhereOperator , InOperator , EqualsOperator <EOL> class StatementException ( Exception ) : <EOL> pass <EOL> class ValueQuoter ( UnicodeMixin ) : <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def __unicode__ ( self ) : <EOL> from cassandra . encoder import cql_quote <EOL> if isinstance ( self . value , bool ) : <EOL> return '<STR_LIT:true>' if self . value else '<STR_LIT:false>' <EOL> elif isinstance ( self . value , ( list , tuple ) ) : <EOL> return '<STR_LIT:[>' + '<STR_LIT:U+002CU+0020>' . join ( [ cql_quote ( v ) for v in self . value ] ) + '<STR_LIT:]>' <EOL> elif isinstance ( self . value , dict ) : <EOL> return '<STR_LIT:{>' + '<STR_LIT:U+002CU+0020>' . join ( [ cql_quote ( k ) + '<STR_LIT::>' + cql_quote ( v ) for k , v in self . value . items ( ) ] ) + '<STR_LIT:}>' <EOL> elif isinstance ( self . value , set ) : <EOL> return '<STR_LIT:{>' + '<STR_LIT:U+002CU+0020>' . join ( [ cql_quote ( v ) for v in self . value ] ) + '<STR_LIT:}>' <EOL> return cql_quote ( self . value ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return self . value == other . value <EOL> return False <EOL> class InQuoter ( ValueQuoter ) : <EOL> def __unicode__ ( self ) : <EOL> from cassandra . encoder import cql_quote <EOL> return '<STR_LIT:(>' + '<STR_LIT:U+002CU+0020>' . join ( [ cql_quote ( v ) for v in self . value ] ) + '<STR_LIT:)>' <EOL> class BaseClause ( UnicodeMixin ) : <EOL> def __init__ ( self , field , value ) : <EOL> self . field = field <EOL> self . value = value <EOL> self . context_id = None <EOL> def __unicode__ ( self ) : <EOL> raise NotImplementedError <EOL> def __hash__ ( self ) : <EOL> return hash ( self . field ) ^ hash ( self . value ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return self . field == other . field and self . value == other . value <EOL> return False <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def get_context_size ( self ) : <EOL> """<STR_LIT>""" <EOL> return <NUM_LIT:1> <EOL> def set_context_id ( self , i ) : <EOL> """<STR_LIT>""" <EOL> self . context_id = i <EOL> def update_context ( self , ctx ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( ctx , dict ) <EOL> ctx [ str ( self . context_id ) ] = self . value <EOL> class WhereClause ( BaseClause ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , field , operator , value , quote_field = True ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( operator , BaseWhereOperator ) : <EOL> raise StatementException ( <EOL> "<STR_LIT>" . format ( BaseWhereOperator , type ( operator ) ) <EOL> ) <EOL> super ( WhereClause , self ) . __init__ ( field , value ) <EOL> self . operator = operator <EOL> self . query_value = self . value if isinstance ( self . value , QueryValue ) else QueryValue ( self . value ) <EOL> self . quote_field = quote_field <EOL> def __unicode__ ( self ) : <EOL> field = ( '<STR_LIT>' if self . quote_field else '<STR_LIT>' ) . format ( self . field ) <EOL> return u'<STR_LIT>' . format ( field , self . operator , six . text_type ( self . query_value ) ) <EOL> def __hash__ ( self ) : <EOL> return super ( WhereClause , self ) . __hash__ ( ) ^ hash ( self . operator ) <EOL> def __eq__ ( self , other ) : <EOL> if super ( WhereClause , self ) . __eq__ ( other ) : <EOL> return self . operator . __class__ == other . operator . __class__ <EOL> return False <EOL> def get_context_size ( self ) : <EOL> return self . query_value . get_context_size ( ) <EOL> def set_context_id ( self , i ) : <EOL> super ( WhereClause , self ) . set_context_id ( i ) <EOL> self . query_value . set_context_id ( i ) <EOL> def update_context ( self , ctx ) : <EOL> if isinstance ( self . operator , InOperator ) : <EOL> ctx [ str ( self . context_id ) ] = InQuoter ( self . value ) <EOL> else : <EOL> self . query_value . update_context ( ctx ) <EOL> class AssignmentClause ( BaseClause ) : <EOL> """<STR_LIT>""" <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' . format ( self . field , self . context_id ) <EOL> def insert_tuple ( self ) : <EOL> return self . field , self . context_id <EOL> class ConditionalClause ( BaseClause ) : <EOL> """<STR_LIT>""" <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' . format ( self . field , self . context_id ) <EOL> def insert_tuple ( self ) : <EOL> return self . field , self . context_id <EOL> class ContainerUpdateTypeMapMeta ( type ) : <EOL> def __init__ ( cls , name , bases , dct ) : <EOL> if not hasattr ( cls , '<STR_LIT>' ) : <EOL> cls . type_map = { } <EOL> else : <EOL> cls . type_map [ cls . col_type ] = cls <EOL> super ( ContainerUpdateTypeMapMeta , cls ) . __init__ ( name , bases , dct ) <EOL> @ six . add_metaclass ( ContainerUpdateTypeMapMeta ) <EOL> class ContainerUpdateClause ( AssignmentClause ) : <EOL> def __init__ ( self , field , value , operation = None , previous = None ) : <EOL> super ( ContainerUpdateClause , self ) . __init__ ( field , value ) <EOL> self . previous = previous <EOL> self . _assignments = None <EOL> self . _operation = operation <EOL> self . _analyzed = False <EOL> def _analyze ( self ) : <EOL> raise NotImplementedError <EOL> def get_context_size ( self ) : <EOL> raise NotImplementedError <EOL> def update_context ( self , ctx ) : <EOL> raise NotImplementedError <EOL> class SetUpdateClause ( ContainerUpdateClause ) : <EOL> """<STR_LIT>""" <EOL> col_type = columns . Set <EOL> _additions = None <EOL> _removals = None <EOL> def __unicode__ ( self ) : <EOL> qs = [ ] <EOL> ctx_id = self . context_id <EOL> if ( self . previous is None and <EOL> self . _assignments is None and <EOL> self . _additions is None and <EOL> self . _removals is None ) : <EOL> qs += [ '<STR_LIT>' . format ( self . field , ctx_id ) ] <EOL> if self . _assignments is not None : <EOL> qs += [ '<STR_LIT>' . format ( self . field , ctx_id ) ] <EOL> ctx_id += <NUM_LIT:1> <EOL> if self . _additions is not None : <EOL> qs += [ '<STR_LIT>' . format ( self . field , ctx_id ) ] <EOL> ctx_id += <NUM_LIT:1> <EOL> if self . _removals is not None : <EOL> qs += [ '<STR_LIT>' . format ( self . field , ctx_id ) ] <EOL> return '<STR_LIT:U+002CU+0020>' . join ( qs ) <EOL> def _analyze ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . value is None or self . value == self . previous : <EOL> pass <EOL> elif self . _operation == "<STR_LIT>" : <EOL> self . _additions = self . value <EOL> elif self . _operation == "<STR_LIT>" : <EOL> self . _removals = self . value <EOL> elif self . previous is None : <EOL> self . _assignments = self . value <EOL> else : <EOL> self . _additions = ( self . value - self . previous ) or None <EOL> self . _removals = ( self . previous - self . value ) or None <EOL> self . _analyzed = True <EOL> def get_context_size ( self ) : <EOL> if not self . _analyzed : <EOL> self . _analyze ( ) <EOL> if ( self . previous is None and <EOL> not self . _assignments and <EOL> self . _additions is None and <EOL> self . _removals is None ) : <EOL> return <NUM_LIT:1> <EOL> return int ( bool ( self . _assignments ) ) + int ( bool ( self . _additions ) ) + int ( bool ( self . _removals ) ) <EOL> def update_context ( self , ctx ) : <EOL> if not self . _analyzed : <EOL> self . _analyze ( ) <EOL> ctx_id = self . context_id <EOL> if ( self . previous is None and <EOL> self . _assignments is None and <EOL> self . _additions is None and <EOL> self . _removals is None ) : <EOL> ctx [ str ( ctx_id ) ] = set ( ) <EOL> if self . _assignments is not None : <EOL> ctx [ str ( ctx_id ) ] = self . _assignments <EOL> ctx_id += <NUM_LIT:1> <EOL> if self . _additions is not None : <EOL> ctx [ str ( ctx_id ) ] = self . _additions <EOL> ctx_id += <NUM_LIT:1> <EOL> if self . _removals is not None : <EOL> ctx [ str ( ctx_id ) ] = self . _removals <EOL> class ListUpdateClause ( ContainerUpdateClause ) : <EOL> """<STR_LIT>""" <EOL> col_type = columns . List <EOL> _append = None <EOL> _prepend = None <EOL> def __unicode__ ( self ) : <EOL> if not self . _analyzed : <EOL> self . _analyze ( ) <EOL> qs = [ ] <EOL> ctx_id = self . context_id <EOL> if self . _assignments is not None : <EOL> qs += [ '<STR_LIT>' . format ( self . field , ctx_id ) ] <EOL> ctx_id += <NUM_LIT:1> <EOL> if self . _prepend is not None : <EOL> qs += [ '<STR_LIT>' . format ( self . field , ctx_id ) ] <EOL> ctx_id += <NUM_LIT:1> <EOL> if self . _append is not None : <EOL> qs += [ '<STR_LIT>' . format ( self . field , ctx_id ) ] <EOL> return '<STR_LIT:U+002CU+0020>' . join ( qs ) <EOL> def get_context_size ( self ) : <EOL> if not self . _analyzed : <EOL> self . _analyze ( ) <EOL> return int ( self . _assignments is not None ) + int ( bool ( self . _append ) ) + int ( bool ( self . _prepend ) ) <EOL> def update_context ( self , ctx ) : <EOL> if not self . _analyzed : <EOL> self . _analyze ( ) <EOL> ctx_id = self . context_id <EOL> if self . _assignments is not None : <EOL> ctx [ str ( ctx_id ) ] = self . _assignments <EOL> ctx_id += <NUM_LIT:1> <EOL> if self . _prepend is not None : <EOL> ctx [ str ( ctx_id ) ] = self . _prepend <EOL> ctx_id += <NUM_LIT:1> <EOL> if self . _append is not None : <EOL> ctx [ str ( ctx_id ) ] = self . _append <EOL> def _analyze ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . value is None or self . value == self . previous : <EOL> pass <EOL> elif self . _operation == "<STR_LIT>" : <EOL> self . _append = self . value <EOL> elif self . _operation == "<STR_LIT>" : <EOL> self . _prepend = self . value <EOL> elif self . previous is None : <EOL> self . _assignments = self . value <EOL> elif len ( self . value ) < len ( self . previous ) : <EOL> self . _assignments = self . value <EOL> elif len ( self . previous ) == <NUM_LIT:0> : <EOL> self . _assignments = self . value <EOL> else : <EOL> search_space = len ( self . value ) - max ( <NUM_LIT:0> , len ( self . previous ) - <NUM_LIT:1> ) <EOL> search_size = len ( self . previous ) <EOL> for i in range ( search_space ) : <EOL> j = i + search_size <EOL> sub = self . value [ i : j ] <EOL> idx_cmp = lambda idx : self . previous [ idx ] == sub [ idx ] <EOL> if idx_cmp ( <NUM_LIT:0> ) and idx_cmp ( - <NUM_LIT:1> ) and self . previous == sub : <EOL> self . _prepend = self . value [ : i ] or None <EOL> self . _append = self . value [ j : ] or None <EOL> break <EOL> if self . _prepend is self . _append is None : <EOL> self . _assignments = self . value <EOL> self . _analyzed = True <EOL> class MapUpdateClause ( ContainerUpdateClause ) : <EOL> """<STR_LIT>""" <EOL> col_type = columns . Map <EOL> _updates = None <EOL> def _analyze ( self ) : <EOL> if self . _operation == "<STR_LIT>" : <EOL> self . _updates = self . value . keys ( ) <EOL> else : <EOL> if self . previous is None : <EOL> self . _updates = sorted ( [ k for k , v in self . value . items ( ) ] ) <EOL> else : <EOL> self . _updates = sorted ( [ k for k , v in self . value . items ( ) if v != self . previous . get ( k ) ] ) or None <EOL> self . _analyzed = True <EOL> def get_context_size ( self ) : <EOL> if self . is_assignment : <EOL> return <NUM_LIT:1> <EOL> return len ( self . _updates or [ ] ) * <NUM_LIT:2> <EOL> def update_context ( self , ctx ) : <EOL> ctx_id = self . context_id <EOL> if self . is_assignment : <EOL> ctx [ str ( ctx_id ) ] = { } <EOL> else : <EOL> for key in self . _updates or [ ] : <EOL> val = self . value . get ( key ) <EOL> ctx [ str ( ctx_id ) ] = key <EOL> ctx [ str ( ctx_id + <NUM_LIT:1> ) ] = val <EOL> ctx_id += <NUM_LIT:2> <EOL> @ property <EOL> def is_assignment ( self ) : <EOL> if not self . _analyzed : <EOL> self . _analyze ( ) <EOL> return self . previous is None and not self . _updates <EOL> def __unicode__ ( self ) : <EOL> qs = [ ] <EOL> ctx_id = self . context_id <EOL> if self . is_assignment : <EOL> qs += [ '<STR_LIT>' . format ( self . field , ctx_id ) ] <EOL> else : <EOL> for _ in self . _updates or [ ] : <EOL> qs += [ '<STR_LIT>' . format ( self . field , ctx_id , ctx_id + <NUM_LIT:1> ) ] <EOL> ctx_id += <NUM_LIT:2> <EOL> return '<STR_LIT:U+002CU+0020>' . join ( qs ) <EOL> class CounterUpdateClause ( AssignmentClause ) : <EOL> col_type = columns . Counter <EOL> def __init__ ( self , field , value , previous = None ) : <EOL> super ( CounterUpdateClause , self ) . __init__ ( field , value ) <EOL> self . previous = previous or <NUM_LIT:0> <EOL> def get_context_size ( self ) : <EOL> return <NUM_LIT:1> <EOL> def update_context ( self , ctx ) : <EOL> ctx [ str ( self . context_id ) ] = abs ( self . value - self . previous ) <EOL> def __unicode__ ( self ) : <EOL> delta = self . value - self . previous <EOL> sign = '<STR_LIT:->' if delta < <NUM_LIT:0> else '<STR_LIT:+>' <EOL> return '<STR_LIT>' . format ( self . field , sign , self . context_id ) <EOL> class BaseDeleteClause ( BaseClause ) : <EOL> pass <EOL> class FieldDeleteClause ( BaseDeleteClause ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , field ) : <EOL> super ( FieldDeleteClause , self ) . __init__ ( field , None ) <EOL> def __unicode__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . field ) <EOL> def update_context ( self , ctx ) : <EOL> pass <EOL> def get_context_size ( self ) : <EOL> return <NUM_LIT:0> <EOL> class MapDeleteClause ( BaseDeleteClause ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , field , value , previous = None ) : <EOL> super ( MapDeleteClause , self ) . __init__ ( field , value ) <EOL> self . value = self . value or { } <EOL> self . previous = previous or { } <EOL> self . _analyzed = False <EOL> self . _removals = None <EOL> def _analyze ( self ) : <EOL> self . _removals = sorted ( [ k for k in self . previous if k not in self . value ] ) <EOL> self . _analyzed = True <EOL> def update_context ( self , ctx ) : <EOL> if not self . _analyzed : <EOL> self . _analyze ( ) <EOL> for idx , key in enumerate ( self . _removals ) : <EOL> ctx [ str ( self . context_id + idx ) ] = key <EOL> def get_context_size ( self ) : <EOL> if not self . _analyzed : <EOL> self . _analyze ( ) <EOL> return len ( self . _removals ) <EOL> def __unicode__ ( self ) : <EOL> if not self . _analyzed : <EOL> self . _analyze ( ) <EOL> return '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' . format ( self . field , self . context_id + i ) for i in range ( len ( self . _removals ) ) ] ) <EOL> class BaseCQLStatement ( UnicodeMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , table , timestamp = None , where = None , fetch_size = None , conditionals = None ) : <EOL> super ( BaseCQLStatement , self ) . __init__ ( ) <EOL> self . table = table <EOL> self . context_id = <NUM_LIT:0> <EOL> self . context_counter = self . context_id <EOL> self . timestamp = timestamp <EOL> self . fetch_size = fetch_size if fetch_size else FETCH_SIZE_UNSET <EOL> self . where_clauses = [ ] <EOL> for clause in where or [ ] : <EOL> self . _add_where_clause ( clause ) <EOL> self . conditionals = [ ] <EOL> for conditional in conditionals or [ ] : <EOL> self . add_conditional_clause ( conditional ) <EOL> def _update_part_key_values ( self , field_index_map , clauses , parts ) : <EOL> for clause in filter ( lambda c : c . field in field_index_map , clauses ) : <EOL> parts [ field_index_map [ clause . field ] ] = clause . value <EOL> def partition_key_values ( self , field_index_map ) : <EOL> parts = [ None ] * len ( field_index_map ) <EOL> self . _update_part_key_values ( field_index_map , ( w for w in self . where_clauses if w . operator . __class__ == EqualsOperator ) , parts ) <EOL> return parts <EOL> def add_where ( self , column , operator , value , quote_field = True ) : <EOL> value = column . to_database ( value ) <EOL> clause = WhereClause ( column . db_field_name , operator , value , quote_field ) <EOL> self . _add_where_clause ( clause ) <EOL> def _add_where_clause ( self , clause ) : <EOL> clause . set_context_id ( self . context_counter ) <EOL> self . context_counter += clause . get_context_size ( ) <EOL> self . where_clauses . append ( clause ) <EOL> def get_context ( self ) : <EOL> """<STR_LIT>""" <EOL> ctx = { } <EOL> for clause in self . where_clauses or [ ] : <EOL> clause . update_context ( ctx ) <EOL> return ctx <EOL> def add_conditional_clause ( self , clause ) : <EOL> """<STR_LIT>""" <EOL> clause . set_context_id ( self . context_counter ) <EOL> self . context_counter += clause . get_context_size ( ) <EOL> self . conditionals . append ( clause ) <EOL> def _get_conditionals ( self ) : <EOL> return '<STR_LIT>' . format ( '<STR_LIT>' . join ( [ six . text_type ( c ) for c in self . conditionals ] ) ) <EOL> def get_context_size ( self ) : <EOL> return len ( self . get_context ( ) ) <EOL> def update_context_id ( self , i ) : <EOL> self . context_id = i <EOL> self . context_counter = self . context_id <EOL> for clause in self . where_clauses : <EOL> clause . set_context_id ( self . context_counter ) <EOL> self . context_counter += clause . get_context_size ( ) <EOL> @ property <EOL> def timestamp_normalized ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . timestamp : <EOL> return None <EOL> if isinstance ( self . timestamp , six . integer_types ) : <EOL> return self . timestamp <EOL> if isinstance ( self . timestamp , timedelta ) : <EOL> tmp = datetime . now ( ) + self . timestamp <EOL> else : <EOL> tmp = self . timestamp <EOL> return int ( time . mktime ( tmp . timetuple ( ) ) * <NUM_LIT> + tmp . microsecond ) <EOL> def __unicode__ ( self ) : <EOL> raise NotImplementedError <EOL> def __repr__ ( self ) : <EOL> return self . __unicode__ ( ) <EOL> @ property <EOL> def _where ( self ) : <EOL> return '<STR_LIT>' . format ( '<STR_LIT>' . join ( [ six . text_type ( c ) for c in self . where_clauses ] ) ) <EOL> class SelectStatement ( BaseCQLStatement ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> table , <EOL> fields = None , <EOL> count = False , <EOL> where = None , <EOL> order_by = None , <EOL> limit = None , <EOL> allow_filtering = False , <EOL> distinct_fields = None , <EOL> fetch_size = None ) : <EOL> """<STR_LIT>""" <EOL> super ( SelectStatement , self ) . __init__ ( <EOL> table , <EOL> where = where , <EOL> fetch_size = fetch_size <EOL> ) <EOL> self . fields = [ fields ] if isinstance ( fields , six . string_types ) else ( fields or [ ] ) <EOL> self . distinct_fields = distinct_fields <EOL> self . count = count <EOL> self . order_by = [ order_by ] if isinstance ( order_by , six . string_types ) else order_by <EOL> self . limit = limit <EOL> self . allow_filtering = allow_filtering <EOL> def __unicode__ ( self ) : <EOL> qs = [ '<STR_LIT>' ] <EOL> if self . distinct_fields : <EOL> if self . count : <EOL> qs += [ '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' . format ( f ) for f in self . distinct_fields ] ) ) ] <EOL> else : <EOL> qs += [ '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' . format ( f ) for f in self . distinct_fields ] ) ) ] <EOL> elif self . count : <EOL> qs += [ '<STR_LIT>' ] <EOL> else : <EOL> qs += [ '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' . format ( f ) for f in self . fields ] ) if self . fields else '<STR_LIT:*>' ] <EOL> qs += [ '<STR_LIT>' , self . table ] <EOL> if self . where_clauses : <EOL> qs += [ self . _where ] <EOL> if self . order_by and not self . count : <EOL> qs += [ '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( six . text_type ( o ) for o in self . order_by ) ) ] <EOL> if self . limit : <EOL> qs += [ '<STR_LIT>' . format ( self . limit ) ] <EOL> if self . allow_filtering : <EOL> qs += [ '<STR_LIT>' ] <EOL> return '<STR_LIT:U+0020>' . join ( qs ) <EOL> class AssignmentStatement ( BaseCQLStatement ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> table , <EOL> assignments = None , <EOL> where = None , <EOL> ttl = None , <EOL> timestamp = None , <EOL> conditionals = None ) : <EOL> super ( AssignmentStatement , self ) . __init__ ( <EOL> table , <EOL> where = where , <EOL> conditionals = conditionals <EOL> ) <EOL> self . ttl = ttl <EOL> self . timestamp = timestamp <EOL> self . assignments = [ ] <EOL> for assignment in assignments or [ ] : <EOL> self . _add_assignment_clause ( assignment ) <EOL> def update_context_id ( self , i ) : <EOL> super ( AssignmentStatement , self ) . update_context_id ( i ) <EOL> for assignment in self . assignments : <EOL> assignment . set_context_id ( self . context_counter ) <EOL> self . context_counter += assignment . get_context_size ( ) <EOL> def partition_key_values ( self , field_index_map ) : <EOL> parts = super ( AssignmentStatement , self ) . partition_key_values ( field_index_map ) <EOL> self . _update_part_key_values ( field_index_map , self . assignments , parts ) <EOL> return parts <EOL> def add_assignment ( self , column , value ) : <EOL> value = column . to_database ( value ) <EOL> clause = AssignmentClause ( column . db_field_name , value ) <EOL> self . _add_assignment_clause ( clause ) <EOL> def _add_assignment_clause ( self , clause ) : <EOL> clause . set_context_id ( self . context_counter ) <EOL> self . context_counter += clause . get_context_size ( ) <EOL> self . assignments . append ( clause ) <EOL> @ property <EOL> def is_empty ( self ) : <EOL> return len ( self . assignments ) == <NUM_LIT:0> <EOL> def get_context ( self ) : <EOL> ctx = super ( AssignmentStatement , self ) . get_context ( ) <EOL> for clause in self . assignments : <EOL> clause . update_context ( ctx ) <EOL> return ctx <EOL> class InsertStatement ( AssignmentStatement ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> table , <EOL> assignments = None , <EOL> where = None , <EOL> ttl = None , <EOL> timestamp = None , <EOL> if_not_exists = False ) : <EOL> super ( InsertStatement , self ) . __init__ ( table , <EOL> assignments = assignments , <EOL> where = where , <EOL> ttl = ttl , <EOL> timestamp = timestamp ) <EOL> self . if_not_exists = if_not_exists <EOL> def __unicode__ ( self ) : <EOL> qs = [ '<STR_LIT>' . format ( self . table ) ] <EOL> fields = [ a . insert_tuple ( ) for a in self . assignments ] <EOL> columns , values = zip ( * fields ) <EOL> qs += [ "<STR_LIT>" . format ( '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' . format ( c ) for c in columns ] ) ) ] <EOL> qs += [ '<STR_LIT>' ] <EOL> qs += [ "<STR_LIT>" . format ( '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' . format ( v ) for v in values ] ) ) ] <EOL> if self . if_not_exists : <EOL> qs += [ "<STR_LIT>" ] <EOL> if self . ttl : <EOL> qs += [ "<STR_LIT>" . format ( self . ttl ) ] <EOL> if self . timestamp : <EOL> qs += [ "<STR_LIT>" . format ( self . timestamp_normalized ) ] <EOL> return '<STR_LIT:U+0020>' . join ( qs ) <EOL> class UpdateStatement ( AssignmentStatement ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> table , <EOL> assignments = None , <EOL> where = None , <EOL> ttl = None , <EOL> timestamp = None , <EOL> conditionals = None , <EOL> if_exists = False ) : <EOL> super ( UpdateStatement , self ) . __init__ ( table , <EOL> assignments = assignments , <EOL> where = where , <EOL> ttl = ttl , <EOL> timestamp = timestamp , <EOL> conditionals = conditionals ) <EOL> self . if_exists = if_exists <EOL> def __unicode__ ( self ) : <EOL> qs = [ '<STR_LIT>' , self . table ] <EOL> using_options = [ ] <EOL> if self . ttl : <EOL> using_options += [ "<STR_LIT>" . format ( self . ttl ) ] <EOL> if self . timestamp : <EOL> using_options += [ "<STR_LIT>" . format ( self . timestamp_normalized ) ] <EOL> if using_options : <EOL> qs += [ "<STR_LIT>" . format ( "<STR_LIT>" . join ( using_options ) ) ] <EOL> qs += [ '<STR_LIT>' ] <EOL> qs += [ '<STR_LIT:U+002CU+0020>' . join ( [ six . text_type ( c ) for c in self . assignments ] ) ] <EOL> if self . where_clauses : <EOL> qs += [ self . _where ] <EOL> if len ( self . conditionals ) > <NUM_LIT:0> : <EOL> qs += [ self . _get_conditionals ( ) ] <EOL> if self . if_exists : <EOL> qs += [ "<STR_LIT>" ] <EOL> return '<STR_LIT:U+0020>' . join ( qs ) <EOL> def get_context ( self ) : <EOL> ctx = super ( UpdateStatement , self ) . get_context ( ) <EOL> for clause in self . conditionals : <EOL> clause . update_context ( ctx ) <EOL> return ctx <EOL> def update_context_id ( self , i ) : <EOL> super ( UpdateStatement , self ) . update_context_id ( i ) <EOL> for conditional in self . conditionals : <EOL> conditional . set_context_id ( self . context_counter ) <EOL> self . context_counter += conditional . get_context_size ( ) <EOL> def add_update ( self , column , value , operation = None , previous = None ) : <EOL> value = column . to_database ( value ) <EOL> col_type = type ( column ) <EOL> container_update_type = ContainerUpdateClause . type_map . get ( col_type ) <EOL> if container_update_type : <EOL> previous = column . to_database ( previous ) <EOL> clause = container_update_type ( column . db_field_name , value , operation , previous ) <EOL> elif col_type == columns . Counter : <EOL> clause = CounterUpdateClause ( column . db_field_name , value , previous ) <EOL> else : <EOL> clause = AssignmentClause ( column . db_field_name , value ) <EOL> if clause . get_context_size ( ) : <EOL> self . _add_assignment_clause ( clause ) <EOL> class DeleteStatement ( BaseCQLStatement ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , table , fields = None , where = None , timestamp = None , conditionals = None , if_exists = False ) : <EOL> super ( DeleteStatement , self ) . __init__ ( <EOL> table , <EOL> where = where , <EOL> timestamp = timestamp , <EOL> conditionals = conditionals <EOL> ) <EOL> self . fields = [ ] <EOL> if isinstance ( fields , six . string_types ) : <EOL> fields = [ fields ] <EOL> for field in fields or [ ] : <EOL> self . add_field ( field ) <EOL> self . if_exists = if_exists <EOL> def update_context_id ( self , i ) : <EOL> super ( DeleteStatement , self ) . update_context_id ( i ) <EOL> for field in self . fields : <EOL> field . set_context_id ( self . context_counter ) <EOL> self . context_counter += field . get_context_size ( ) <EOL> for t in self . conditionals : <EOL> t . set_context_id ( self . context_counter ) <EOL> self . context_counter += t . get_context_size ( ) <EOL> def get_context ( self ) : <EOL> ctx = super ( DeleteStatement , self ) . get_context ( ) <EOL> for field in self . fields : <EOL> field . update_context ( ctx ) <EOL> for clause in self . conditionals : <EOL> clause . update_context ( ctx ) <EOL> return ctx <EOL> def add_field ( self , field ) : <EOL> if isinstance ( field , six . string_types ) : <EOL> field = FieldDeleteClause ( field ) <EOL> if not isinstance ( field , BaseClause ) : <EOL> raise StatementException ( "<STR_LIT>" ) <EOL> field . set_context_id ( self . context_counter ) <EOL> self . context_counter += field . get_context_size ( ) <EOL> self . fields . append ( field ) <EOL> def __unicode__ ( self ) : <EOL> qs = [ '<STR_LIT>' ] <EOL> if self . fields : <EOL> qs += [ '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' . format ( f ) for f in self . fields ] ) ] <EOL> qs += [ '<STR_LIT>' , self . table ] <EOL> delete_option = [ ] <EOL> if self . timestamp : <EOL> delete_option += [ "<STR_LIT>" . format ( self . timestamp_normalized ) ] <EOL> if delete_option : <EOL> qs += [ "<STR_LIT>" . format ( "<STR_LIT>" . join ( delete_option ) ) ] <EOL> if self . where_clauses : <EOL> qs += [ self . _where ] <EOL> if self . conditionals : <EOL> qs += [ self . _get_conditionals ( ) ] <EOL> if self . if_exists : <EOL> qs += [ "<STR_LIT>" ] <EOL> return '<STR_LIT:U+0020>' . join ( qs ) </s>
