diff --git a/aiida/backends/querybuild/dummy_model.py b/aiida/backends/querybuild/dummy_model.py index d90598e3f9..32f73da569 100644 --- a/aiida/backends/querybuild/dummy_model.py +++ b/aiida/backends/querybuild/dummy_model.py @@ -11,7 +11,7 @@ from sa_init import ( Column, Table, ForeignKey, Integer, String, DateTime, Float, Boolean, Text, # basic column types - UUID, JSONB, # Fancy column types + UUID, JSONB, array, # Fancy column types UniqueConstraint,aliased, select, func, join, and_, or_, not_, except_, # join and filter ops relationship, backref, column_property, # Table to table relationsships @@ -385,35 +385,78 @@ def get_aiida_class(self): ) -DbAttribute.value_str = column_property( - case([ - (DbAttribute.datatype == 'txt', DbAttribute.tval), - (DbAttribute.datatype == 'float', cast(DbAttribute.fval, String)), - (DbAttribute.datatype == 'int', cast(DbAttribute.ival, String)), - (DbAttribute.datatype == 'bool', cast(DbAttribute.bval, String)), - (DbAttribute.datatype == 'date', cast(DbAttribute.dval, String)), - (DbAttribute.datatype == 'txt', cast(DbAttribute.tval, String)), - (DbAttribute.datatype == 'float', cast(DbAttribute.fval, String)), - (DbAttribute.datatype == 'list', None), - (DbAttribute.datatype == 'dict', None), - ]) - ) -DbAttribute.value_float = column_property( - case([ - (DbAttribute.datatype == 'txt', cast(DbAttribute.tval, Float)), - (DbAttribute.datatype == 'float', DbAttribute.fval), - (DbAttribute.datatype == 'int', cast(DbAttribute.ival, Float)), - (DbAttribute.datatype == 'bool', cast(DbAttribute.bval, Float)), - (DbAttribute.datatype == 'date', cast(DbAttribute.dval, Float)), - (DbAttribute.datatype == 'txt', cast(DbAttribute.tval, Float)), - (DbAttribute.datatype == 'float', cast(DbAttribute.fval, Float)), - (DbAttribute.datatype == 'list', None), - (DbAttribute.datatype == 'dict', None), - ]) +node_aliased = aliased(DbNode) + +walk = select([ + DbNode.id.label('start'), + DbNode.id.label('end'), + cast(-1, Integer).label('depth'), + array([DbNode.id]).label('path') + ]).select_from(DbNode).cte(recursive=True) #, name="incl_aliased3") + + +descendants_beta = walk.union_all( + select([ + walk.c.start, + node_aliased.id, + walk.c.depth + cast(1, Integer), + (walk.c.path+array([node_aliased.id])).label('path'), + ]).select_from( + join( + node_aliased, + DbLink, + DbLink.output_id==node_aliased.id, + ) + ).where( + and_( + DbLink.input_id == walk.c.end, + ) + ) ) +class DbPathBeta(object): + + def __init__(self, start, end, depth): + self.start = start + self.out = end + self.depth = depth + + + +mapper(DbPathBeta, descendants_beta) + + +#~ DbAttribute.value_str = column_property( + #~ case([ + #~ (DbAttribute.datatype == 'txt', DbAttribute.tval), + #~ (DbAttribute.datatype == 'float', cast(DbAttribute.fval, String)), + #~ (DbAttribute.datatype == 'int', cast(DbAttribute.ival, String)), + #~ (DbAttribute.datatype == 'bool', cast(DbAttribute.bval, String)), + #~ (DbAttribute.datatype == 'date', cast(DbAttribute.dval, String)), + #~ (DbAttribute.datatype == 'txt', cast(DbAttribute.tval, String)), + #~ (DbAttribute.datatype == 'float', cast(DbAttribute.fval, String)), + #~ (DbAttribute.datatype == 'list', None), + #~ (DbAttribute.datatype == 'dict', None), + #~ ]) + #~ ) +#~ +#~ DbAttribute.value_float = column_property( + #~ case([ + #~ (DbAttribute.datatype == 'txt', cast(DbAttribute.tval, Float)), + #~ (DbAttribute.datatype == 'float', DbAttribute.fval), + #~ (DbAttribute.datatype == 'int', cast(DbAttribute.ival, Float)), + #~ (DbAttribute.datatype == 'bool', cast(DbAttribute.bval, Float)), + #~ (DbAttribute.datatype == 'date', cast(DbAttribute.dval, Float)), + #~ (DbAttribute.datatype == 'txt', cast(DbAttribute.tval, Float)), + #~ (DbAttribute.datatype == 'float', cast(DbAttribute.fval, Float)), + #~ (DbAttribute.datatype == 'list', None), + #~ (DbAttribute.datatype == 'dict', None), + #~ ]) + #~ ) + + profile = get_profile_config(settings.AIIDADB_PROFILE) diff --git a/aiida/backends/querybuild/querybuilder_base.py b/aiida/backends/querybuild/querybuilder_base.py index f8337dd3ce..a57475863e 100644 --- a/aiida/backends/querybuild/querybuilder_base.py +++ b/aiida/backends/querybuild/querybuilder_base.py @@ -1288,6 +1288,8 @@ def _join_inputs(self, joined_entity, entity_to_join, aliased_edge): aliased_edge.input_id == entity_to_join.id ) + #~ def _join_descendants_beta(self, joined_entity, entity_to_join, aliased_path): + def _join_descendants(self, joined_entity, entity_to_join, aliased_path): """ :param joined_entity: The (aliased) ORMclass that is an ancestor diff --git a/aiida/backends/querybuild/querybuilder_django.py b/aiida/backends/querybuild/querybuilder_django.py index f8f9f2e425..242693b4b2 100644 --- a/aiida/backends/querybuild/querybuilder_django.py +++ b/aiida/backends/querybuild/querybuilder_django.py @@ -20,8 +20,10 @@ DbGroup as DummyGroup, DbExtra as DummyExtra, DbAttribute as DummyAttribute, + descendants_beta as DummyDescendants_beta, table_groups_nodes as Dummy_table_groups_nodes, session, # session with DB + ) from aiida.backends.djsite.db.models import DbAttribute, DbExtra, ObjectDoesNotExist @@ -53,11 +55,13 @@ def __init__(self, *args, **kwargs): self.User = DummyUser self.Group = DummyGroup self.table_groups_nodes = Dummy_table_groups_nodes + self.descendants_beta = DummyDescendants_beta self.AiidaNode = AiidaNode self.AiidaGroup = AiidaGroup self.AiidaComputer = AiidaComputer self.AiidaUser = AiidaUser + super(QueryBuilder, self).__init__(*args, **kwargs) diff --git a/aiida/backends/querybuild/sa_init.py b/aiida/backends/querybuild/sa_init.py index f72e47fbf7..9976e91022 100644 --- a/aiida/backends/querybuild/sa_init.py +++ b/aiida/backends/querybuild/sa_init.py @@ -30,7 +30,7 @@ ) from sqlalchemy.orm.attributes import InstrumentedAttribute from sqlalchemy.sql.elements import Cast -from sqlalchemy.dialects.postgresql import UUID, JSONB +from sqlalchemy.dialects.postgresql import UUID, JSONB, INTEGER, array # TO COMPILE MY OWN FUNCTIONALITIES: from sqlalchemy.sql.expression import FunctionElement, cast from sqlalchemy.ext.compiler import compiles