Esempio n. 1
0
def listNodesId(level=None, node_type=None):
    """List nodes from db.

    If level is passed returns from that level only, if node type 
    is passed returns of that type only. If not all are returned.

    @param level from which the nodes are
    @param node_type
    @returns list the nodes
    """
    list_nodes = []

    if level is None:
        st = "SELECT id FROM gn____levels;"
        list_levels = db.query(st).getresult()
        for i in list_levels:
            tag_level = genet_global.tagName(i[0])
            st = "SELECT id, idtype FROM gn_" + tag_level + "_nodes;"
            if node_type is not None:
                st = st[:-1] + " WHERE idtype = " + str(node_type) + ";"
            list_nodes += db.query(st).getresult()
    else:
        tag_level = genet_global.tagName(level)
        st = "SELECT id, idtype FROM gn_" + tag_level + "_nodes;"
        if node_type is not None:
            st = st[:-1] + " WHERE idtype = " + str(node_type) + ";"
        list_nodes = db.query(st).getresult()

    return list_nodes
Esempio n. 2
0
def createEdgeType(level, description):
    """
    Create one edge type. Construct the new tablename of type with the new type.

    @param level in which the new type is added.
    @param description of the type.
    """
    st = ("INSERT INTO gn____edge_types(level, description) " 
        "VALUES (" + level + ", '" + description + "') " 
        "RETURNING ID;")
    id = db.query(st).getresult()[0][0]
    ret = st, id

    # Update tablename with the level and id.
    tag_level = genet_global.tagName(level)
    tag_id = genet_global.tagName(id)
    tablename = "gn_" + tag_level + "_edges_type_" + tag_id

    st = ("UPDATE gn____edge_types " 
        "SET tablename = '" + tablename + "' " 
        "WHERE id = " + str(id) + ";")
    ret = ret, st, db.query(st)

    # Create the table.
    ret = ret, db_schema.createEdgeTypeTable(tag_level, tablename)
    
    return ret
Esempio n. 3
0
def connectNodes(level_id, idnA, idnB, parent_id, parentlevel_id, type_id, attributes):
    """Create edge in db.

    Uses Edge class for the edge creation and coding for the edge type
    attributes store.

    TODO store any attributes len.

    @param level id
    @param node A id
    @param node B id
    @param parent id
    @param parentlevel id
    @param type id
    @param attributes tuple of edge attributes
    """
    E = class_edge.edge(level_id, idnA, idnB, parent_id, parentlevel_id, type_id)
    edge_id = E.store(db)

    tag_level = genet_global.tagName(level_id)
    tag_type =  genet_global.tagName(type_id)

    st = ("INSERT INTO gn_" + tag_level + "_edges_type_" + tag_type +  
          " VALUES(" + str(edge_id) + ", '" + attributes[0] + "');")
    db.query(st)
Esempio n. 4
0
def dropLevel(level):
    """
    @param level id to delete.
    """
    # Drop the layer tables.
    tag_level = genet_global.tagName(level)
    ret = db_schema.dropLevelTables(tag_level)
    
    # Drop all type tables in the level.
    # Edges.
    st = ("SELECT id " 
        "FROM gn____edge_types " 
        "WHERE level = " + level + ";")
    ids = db.query(st).getresult()
    if len(ids):
        for i in ids:
            dropEdgeType(i[0])        

    # Nodes.
    st = ("SELECT id " 
        "FROM gn____node_types " 
        "WHERE level = " + level + ";")
    ids = db.query(st).getresult()
    if len(ids):
        for i in ids:
            dropNodeType(i[0])        

    # Delete entry into the levels table.
    st = ("DELETE FROM gn____levels " 
        "WHERE id = " + level + ";")
    ret = ret, db.query(st)
    return ret
Esempio n. 5
0
    def store(self, db):
        """
        Stores data in db. 

        @param db is the database object.
        @returns the node id.
        TODO write log with db statements.
        """
        # Uniformly define the tablename level.
        tag_level = genet_global.tagName(self.level)

        # If the node already exists.
        if self.id:

            # Update the nodes table.
            st1 = (
                "UPDATE gn_" + tag_level + "_nodes SET "
                "idtype = " + str(self.idtype) + " "
                "WHERE id = " + str(self.id) + ";"
            )
            db.query(st1)

            # Update the tree table.
            st2 = (
                "UPDATE gn____tree SET "
                "idparent = " + str(self.idparent) + ", "
                "parentlevel = " + str(self.parentlevel) + ", "
                "childlevel = " + str(self.level) + " "
                "WHERE idchild = " + str(self.id) + ";"
            )
            db.query(st2)

            return self.id

        # If the node does not exist.
        else:

            # Insert the node into the table.
            st1 = "INSERT INTO gn_" + tag_level + "_nodes(idtype) " "VALUES(" + str(self.idtype) + ") " "RETURNING id;"
            self.id = db.query(st1).getresult()[0][0]

            # Insert the node into the tree.
            st2 = (
                "INSERT INTO gn____tree(idparent, idchild, "
                "parentlevel, childlevel) "
                "VALUES ("
                + str(self.idparent)
                + ", "
                + str(self.id)
                + ", "
                + str(self.parentlevel)
                + ", "
                + str(self.level)
                + ");"
            )
            db.query(st2)

            return self.id
Esempio n. 6
0
def listNodesAtt(level, node_type):
    """List node attributes from db.

    Returns data from all nodes from level and type passed.

    @param level from which the nodes are
    @param node_type
    @returns list the nodes
    """
    list_nodes = []

    tag_level = genet_global.tagName(level)
    tag_type = genet_global.tagName(node_type)

    st = ("SELECT * "
          "FROM gn_" + tag_level + "_nodes_type_" + tag_type + ";")
    list_nodes = db.query(st).getresult()

    return list_nodes
Esempio n. 7
0
def createNode(level_id, parent_id, parentlevel_id, type_id, attributes):
    """Create node in db.

    Uses Node class for the node creation and coding for the node type
    attributes store.

    TODO store any attributes len.

    @param level id
    @param parent id
    @param parentlevel id
    @param type id
    @param attributes tuple of node attributes
    """
    N = class_node.node(level_id, parent_id, parentlevel_id, type_id)
    node_id = N.store(db)

    tag_level = genet_global.tagName(level_id)
    tag_type =  genet_global.tagName(type_id)

    st = ("INSERT INTO gn_" + tag_level + "_nodes_type_" + tag_type +  
          " VALUES(" + str(node_id) + ", '" + attributes[0] + "');")
    db.query(st)
Esempio n. 8
0
def createLevel(description):
    """
    Create one level in the network data model. The level primary key is
    used to tag the table.

    @param description of the level.
    @returns queries statements and results.
    """
    st = ("INSERT INTO gn____levels(description) " 
        "VALUES ('" + description + "') " 
        "RETURNING id;")
    level = db.query(st).getresult()[0][0]
    tag_level = genet_global.tagName(level)
    
    # Create the layer tables.
    ret = db_schema.createLevelTables(tag_level)
    return ret