Exemplo n.º 1
0
    def execute_transaction(self, transaction):
        updates = OrderedDict()
        inserts = OrderedDict()
        for command in transaction.commands:
            model = command.args
            table_name = model._meta.table_name
            data, action = self.model_data(model, command.action)
            group = inserts if action == Command.INSERT else updates
            if table_name not in group:
                group[table_name] = [], []
            group[table_name][0].append(data)
            group[table_name][1].append(model)
        #
        for table, docs_models in inserts.items():
            term = ast.Table(table).insert(docs_models[0])
            executed = yield from self.execute(term)

            errors = []
            for key, model in zip(executed['generated_keys'], docs_models[1]):
                model['id'] = key
                model[REV_KEY] = key
                model._modified.clear()
        #
        for table, docs_models in updates.items():
            for data, model in zip(docs_models[0], docs_models[1]):
                data[REV_KEY] = model[REV_KEY]
                model._modified.clear()

            term = ast.Table(table).update(docs_models[0])
            yield from self.execute(term)
Exemplo n.º 2
0
    def _build_term(self, aggregated):
        table = ast.Table(self._meta.table_name)
        if aggregated:
            assert len(aggregated) == 1, 'Cannot filter on multiple lookups'
            name, lookups = list(aggregated.items())[0]
            values = None
            row = ast.ImplicitVar()
            for lookup in lookups:
                if lookup.type == 'value':
                    v = row[name] == lookup.value
                else:
                    raise NotImplementedError
                if values is None:
                    values = v
                else:
                    values = values & v

            field = self._meta.dfields.get(name)
            if field and field.index:
                raise NotImplementedError
            else:
                term = table.filter(values)
        else:
            term = table
        return term
Exemplo n.º 3
0
def table(*arguments, **kwargs):
    """
    Return all documents in a table. Other commands may be chained after table
    to return a subset of documents (such as get and filter) or perform further
    processing.
    """
    return ast.Table(*arguments, **kwargs)
Exemplo n.º 4
0
 def update(cls, data, **kwargs):
     if getattr(cls, 'schema', None):
         cls.validate(data, partial=True)
     return ast.Table(cls._table).update(data,
                                         data=data,
                                         table=cls._table,
                                         return_changes=True,
                                         **kwargs)
Exemplo n.º 5
0
 def insert(cls, data, raw=False, validate=True, **kwargs):
     if validate and getattr(cls, 'schema', None):
         if isinstance(data, list):
             cls.validate(data, many=True)
         else:
             cls.validate(data)
     res = ast.Table(cls._table).insert(data,
                                        raw=raw,
                                        data=data,
                                        table=cls._table,
                                        return_changes=True,
                                        **kwargs)
     return res
Exemplo n.º 6
0
 def merge(cls, *args, **kwargs):
     return ast.Table(cls._table).merge(*args, **kwargs)
Exemplo n.º 7
0
 def table_index_all(self, table_name, **kw):
     return self.execute(ast.Table(table_name).index_list(), **kw)
Exemplo n.º 8
0
 def table_index_drop(self, table_name, index, **kw):
     return self.execute(ast.Table(table_name).index_drop(index), **kw)
Exemplo n.º 9
0
 def get_model(self, manager, pk):
     table_name = manager._meta.table_name
     data = yield from self.execute(ast.Table(table_name).get(pk))
     if not data:
         raise self.ModelNotFound
     return self._model_from_db(manager, **data)
Exemplo n.º 10
0
 def changes(cls, *args, **kwargs):
     return ast.Table(cls._table).changes(*args, **kwargs)
Exemplo n.º 11
0
 def prepend(cls, *args):
     return ast.Table(cls._table).prepend(*args)
Exemplo n.º 12
0
 def append(cls, *args):
     return ast.Table(cls._table).append(*args)
Exemplo n.º 13
0
 def all(cls):
     return ast.Table(cls._table, table=cls._table)
Exemplo n.º 14
0
 def count(cls, *args):
     return ast.Table(cls._table).count(*args)
Exemplo n.º 15
0
 def delete(cls, **kwargs):
     return ast.Table(cls._table).delete(table=cls._table,
                                         return_changes=True,
                                         **kwargs)
Exemplo n.º 16
0
 def filter(cls, *args, **kwargs):
     return ast.Table(cls._table).filter(*args, **kwargs)
Exemplo n.º 17
0
 def get(cls, *args):
     return ast.Table(cls._table).get(*args, table=cls._table)