예제 #1
0
 def query(self):
     from simpledb.query import SimpleDBQuery
     manager = mock.Mock()
     return SimpleDBQuery(manager, M, None)
예제 #2
0
 def __init__(self, compiler, fields):
     super(BackendQuery, self).__init__(compiler, fields)
     # TODO: add your initialization code here
     domain = domain_for_model(self.query.model)
     self.db_query = SimpleDBQuery(
         self.connection.create_manager(domain), self.query.model)
예제 #3
0
 def __init__(self, compiler, fields):
     super(BackendQuery, self).__init__(compiler, fields)
     # TODO: add your initialization code here
     domain = domain_for_model(self.query.model)
     self.db_query = SimpleDBQuery(self.connection.create_manager(domain),
                                   self.query.model)
예제 #4
0
class BackendQuery(NonrelQuery):

    def __init__(self, compiler, fields):
        super(BackendQuery, self).__init__(compiler, fields)
        # TODO: add your initialization code here
        domain = domain_for_model(self.query.model)
        self.db_query = SimpleDBQuery(
            self.connection.create_manager(domain), self.query.model)

    # This is needed for debugging
    def __repr__(self):
        # TODO: add some meaningful query string for debugging
        return '<BackendQuery: %s>' % self.query.model._meta.db_table

    @safe_call
    def fetch(self, low_mark=None, high_mark=None):
        reslice = 0
        if high_mark > AWS_MAX_RESULT_SIZE:
            logger.warn('Requested result size %s, assuming infinite' % (
                high_mark))
            reslice = high_mark
            high_mark = None
        # TODO: run your low-level query here
        #low_mark, high_mark = self.limits
        if high_mark is None:
            # Infinite fetching
            results = self.db_query.fetch_infinite(offset=low_mark)
            if reslice:
                results = list(results)[:reslice]
        elif high_mark > low_mark:
            # Range fetching
            results = self.db_query.fetch_range(high_mark - low_mark, low_mark)
        else:
            results = ()

        for entity in results:
            entity[self.query.get_meta().pk.column] = entity['_id']
            del entity['_id']
            yield entity

    @safe_call
    def count(self, limit=None):
        # TODO: implement this
        return self.db_query.count(limit)

    @safe_call
    def delete(self):
        self.db_query.delete()

    @safe_call
    def order_by(self, ordering):
        # TODO: implement this
        for order in ordering:
            if order.startswith('-'):
                column, direction = order[1:], 'DESC'
            else:
                column, direction = order, 'ASC'
            if column == self.query.get_meta().pk.column:
                column = '_id'
            self.db_query.add_ordering(column, direction)

    # This function is used by the default add_filters() implementation which
    # only supports ANDed filter rules and simple negation handling for
    # transforming OR filters to AND filters:
    # NOT (a OR b) => (NOT a) AND (NOT b)
    @safe_call
    def add_filter(self, column, lookup_type, negated, db_type, value):
        # TODO: implement this or the add_filters() function (see the base
        # class for a sample implementation)

        # Emulated/converted lookups
        if column == self.query.get_meta().pk.column:
            column = '_id'

        # Special-case IN
        if lookup_type == 'in':
            if negated:
                # XXX needs testing, error for now.
                raise NotImplementedError
                #op = '!='
            else:
                op = '='
            # boto needs IN queries' values to be lists of lists.
            db_value = [[self.convert_value_for_db(db_type, v)] for v in value]
        else:
            if negated:
                try:
                    op = NEGATION_MAP[lookup_type]
                except KeyError:
                    raise DatabaseError("Lookup type %r can't be negated" % lookup_type)
            else:
                try:
                    op = OPERATORS_MAP[lookup_type]
                except KeyError:
                    raise DatabaseError("Lookup type %r isn't supported" % lookup_type)

            # Handle special-case lookup types
            if callable(op):
                op, value = op(lookup_type, value)

            db_value = self.convert_value_for_db(db_type, value)

        # XXX check this is right
        self.db_query.filter('%s %s' % (column, op), db_value)
예제 #5
0
class BackendQuery(NonrelQuery):
    def __init__(self, compiler, fields):
        super(BackendQuery, self).__init__(compiler, fields)
        # TODO: add your initialization code here
        domain = domain_for_model(self.query.model)
        self.db_query = SimpleDBQuery(self.connection.create_manager(domain),
                                      self.query.model)

    # This is needed for debugging
    def __repr__(self):
        # TODO: add some meaningful query string for debugging
        return '<BackendQuery: %s>' % self.query.model._meta.db_table

    @safe_call
    def fetch(self, low_mark=None, high_mark=None):
        reslice = 0
        if high_mark > AWS_MAX_RESULT_SIZE:
            logger.warn('Requested result size %s, assuming infinite' %
                        (high_mark))
            reslice = high_mark
            high_mark = None
        # TODO: run your low-level query here
        #low_mark, high_mark = self.limits
        if high_mark is None:
            # Infinite fetching
            results = self.db_query.fetch_infinite(offset=low_mark)
            if reslice:
                results = list(results)[:reslice]
        elif high_mark > low_mark:
            # Range fetching
            results = self.db_query.fetch_range(high_mark - low_mark, low_mark)
        else:
            results = ()

        for entity in results:
            entity[self.query.get_meta().pk.column] = entity['_id']
            del entity['_id']
            yield entity

    @safe_call
    def count(self, limit=None):
        # TODO: implement this
        return self.db_query.count(limit)

    @safe_call
    def delete(self):
        self.db_query.delete()

    @safe_call
    def order_by(self, ordering):
        # TODO: implement this
        for order in ordering:
            if order.startswith('-'):
                column, direction = order[1:], 'DESC'
            else:
                column, direction = order, 'ASC'
            if column == self.query.get_meta().pk.column:
                column = '_id'
            self.db_query.add_ordering(column, direction)

    # This function is used by the default add_filters() implementation which
    # only supports ANDed filter rules and simple negation handling for
    # transforming OR filters to AND filters:
    # NOT (a OR b) => (NOT a) AND (NOT b)
    @safe_call
    def add_filter(self, column, lookup_type, negated, db_type, value):
        # TODO: implement this or the add_filters() function (see the base
        # class for a sample implementation)

        # Emulated/converted lookups
        if column == self.query.get_meta().pk.column:
            column = '_id'

        # Special-case IN
        if lookup_type == 'in':
            if negated:
                # XXX needs testing, error for now.
                raise NotImplementedError
                #op = '!='
            else:
                op = '='
            # boto needs IN queries' values to be lists of lists.
            db_value = [[self.convert_value_for_db(db_type, v)] for v in value]
        else:
            if negated:
                try:
                    op = NEGATION_MAP[lookup_type]
                except KeyError:
                    raise DatabaseError("Lookup type %r can't be negated" %
                                        lookup_type)
            else:
                try:
                    op = OPERATORS_MAP[lookup_type]
                except KeyError:
                    raise DatabaseError("Lookup type %r isn't supported" %
                                        lookup_type)

            # Handle special-case lookup types
            if callable(op):
                op, value = op(lookup_type, value)

            db_value = self.convert_value_for_db(db_type, value)

        # XXX check this is right
        self.db_query.filter('%s %s' % (column, op), db_value)