Skip to content

Commit

Permalink
debugged and optimized cascading delete, issue datajoint#15
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitri-yatsenko committed Aug 4, 2015
1 parent 9437e31 commit 1837dc8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
35 changes: 18 additions & 17 deletions datajoint/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,31 +209,32 @@ def delete(self):
relations = self.descendants
restrict_by_me = set()
for r in relations:
for ref in r.references:
restrict_by_me.add(ref)
restrict_by_me.update(r.references)
relations = OrderedDict((r.full_table_name, r) for r in relations)

if self.restrictions:
restrict_by_me.add(self.full_table_name)
relations[self.full_table_name] &= self.restrictions

for r in relations.values():
for name in relations:
r = relations[name]
for dep in (r.children + r.references):
relations[dep] &= r.project() if r.full_table_name in restrict_by_me else r.restrictions

if config['safemode']:
do_delete = False # indicate if there is anything to delete
print('The contents of the following tables are about to be deleted:')
for relation in relations.values():
count = len(relation)
if count:
do_delete = True
relations[dep] &= r.project() if name in restrict_by_me else r.restrictions

do_delete = False # indicate if there is anything to delete
print('The contents of the following tables are about to be deleted:')
for relation in relations.values():
count = len(relation)
if count:
do_delete = True
if config['safemode']:
print(relation.full_table_name, '(%d tuples)' % count)
if not do_delete or user_choice("Proceed?", default='no') != 'yes':
return
with self.connection.transaction:
for r in reversed(list(relations.values())):
r.delete_quick()
else:
relations.pop(relation.full_table_name)
if do_delete and (not config['safemode'] or user_choice("Proceed?", default='no') == 'yes'):
with self.connection.transaction:
for r in reversed(list(relations.values())):
r.delete_quick()

def drop_quick(self):
"""
Expand Down
18 changes: 9 additions & 9 deletions datajoint/relational_operand.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ def __sub__(self, restriction):
"""
return self & Not(restriction)

@abc.abstractmethod
def _repr_helper(self):
return "None"
pass

def __repr__(self):
ret = self._repr_helper()
Expand Down Expand Up @@ -286,7 +287,7 @@ def select_fields(self):


class Projection(RelationalOperand):
def __init__(self, arg, *attributes, _aggregate=False, **renamed_attributes):
def __init__(self, arg, *attributes, **renamed_attributes):
"""
See RelationalOperand.project()
"""
Expand All @@ -302,14 +303,16 @@ def __init__(self, arg, *attributes, _aggregate=False, **renamed_attributes):
self._renamed_attributes.update({d['alias']: d['sql_expression']})
else:
self._attributes.append(attribute)
self._aggregate = _aggregate

if arg.heading.computed:
self._arg = Subquery(arg)
else:
self._arg = arg
self._restrictions = arg.restrictions

def _repr_helper(self):
return "(%r).project(%r)" % (self._arg, self._attributes)

@property
def connection(self):
return self._arg.connection
Expand All @@ -326,19 +329,16 @@ def __and__(self, restriction):
"""
When projection has renamed attributes, it must be enclosed in a subquery before restriction
"""
if restriction:
return Subquery(self) & restriction if self.heading.computed else super().__and__(restriction)
return super().__and__(restriction) \
if not restriction or not self.heading.computed \
else Subquery(self) & restriction


class Aggregation(Projection):
@property
def _grouped(self):
return True

def _repr_helper(self):
# TODO: create better repr
return "project(%r, %r)" % (self._arg, self._attributes)


class Subquery(RelationalOperand):
"""
Expand Down

0 comments on commit 1837dc8

Please sign in to comment.