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)
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
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)
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)
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
def merge(cls, *args, **kwargs): return ast.Table(cls._table).merge(*args, **kwargs)
def table_index_all(self, table_name, **kw): return self.execute(ast.Table(table_name).index_list(), **kw)
def table_index_drop(self, table_name, index, **kw): return self.execute(ast.Table(table_name).index_drop(index), **kw)
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)
def changes(cls, *args, **kwargs): return ast.Table(cls._table).changes(*args, **kwargs)
def prepend(cls, *args): return ast.Table(cls._table).prepend(*args)
def append(cls, *args): return ast.Table(cls._table).append(*args)
def all(cls): return ast.Table(cls._table, table=cls._table)
def count(cls, *args): return ast.Table(cls._table).count(*args)
def delete(cls, **kwargs): return ast.Table(cls._table).delete(table=cls._table, return_changes=True, **kwargs)
def filter(cls, *args, **kwargs): return ast.Table(cls._table).filter(*args, **kwargs)
def get(cls, *args): return ast.Table(cls._table).get(*args, table=cls._table)