Skip to content

Commit

Permalink
Added selectable descendants_beta and declarative DbPathBeta that hav…
Browse files Browse the repository at this point in the history
…e the functionality to replace DbPath table
  • Loading branch information
lekah committed Sep 16, 2016
1 parent 709042d commit ab79236
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 27 deletions.
95 changes: 69 additions & 26 deletions aiida/backends/querybuild/dummy_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions aiida/backends/querybuild/querybuilder_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions aiida/backends/querybuild/querybuilder_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion aiida/backends/querybuild/sa_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ab79236

Please sign in to comment.