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
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
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
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)
def dropNodeType(id): """ @param id the type id into the table. """ st = ("SELECT tablename " "FROM gn____node_types " "WHERE id = " + str(id) + ";") tablename = db.query(st).getresult()[0][0] # Drop the table. st = "DROP TABLE IF EXISTS " + tablename + " CASCADE;" ret = db.query(st) # Delete entry into the types table. st = ("DELETE FROM gn____node_types " "WHERE id = " + str(id) + ";") ret = ret, db.query(st) return ret
def restartDatabase(): """ *** DANGER *** *** DANGER *** *** DANGER *** *** DANGER *** *** DANGER *** Restarts all the database. *** DANGER *** *** DANGER *** *** DANGER *** *** DANGER *** *** DANGER *** """ st = ("DROP SCHEMA public CASCADE;" "CREATE SCHEMA public AUTHORIZATION genet;") ret = st, db.query(st) ret = ret, db_schema.fillSchema() return ret
def listEdgeTypes(level=None): """List edge types from db. If level is passed returns from that level only, all are returned if not. @param level from which the edge types are @returns list the node types """ st = "SELECT id, description FROM gn____edge_types;" if level is not None: st = st[:-1] + " WHERE level = " + str(level) + ";" return db.query(st).getresult()
def listLevels(level=None): """List levels from db. If level is passed returns from that level only, all are returned if not. @param level from which gets information @returns list the levels """ st = "SELECT id, description FROM gn____levels;" if level is not None: st = st[:-1] + " WHERE id = " + str(level) + ";" return db.query(st).getresult()
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)
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
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
# Insert data. # N001 = class_node.node(1, 0, 0, 2) print N001.store(db) N002 = class_node.node(1, 0, 0, 2) print N002.store(db) N003 = class_node.node(1, 0, 0, 2) print N003.store(db) N003.idtype = 4 print N003.store(db) E001 = class_edge.edge(1, N001.id, N002.id, 0, 0, 3) print E001.store(db) E002 = class_edge.edge(1, N002.id, N003.id, 0, 0, 3) print E002.store(db) E003 = class_edge.edge(1, N003.id, N001.id, 0, 0, 3) print E003.store(db) E004 = class_edge.edge(1, N001.id, N002.id, 0, 0, 3) print E004.store(db) for r in db.query("SELECT * FROM gn_01_nodes").dictresult(): print 'id %(id)s, idtype %(idtype)s' % r for r in db.query("SELECT * FROM gn_01_edges").dictresult(): print 'id %(id)s, idnA %(idna)s, idnB %(idnb)s, idtype %(idtype)s' % r