3 # implements support for geni records stored in db tables
5 # TODO: Use existing PLC database methods? or keep this separate?
13 GENI_TABLE_PREFIX = "geni$"
16 def __init__(self, create=False, hrn="unspecified.default.registry", cninfo=None):
17 global GENI_TABLE_PREFIX
21 # pgsql doesn't like table names with "." in them, to replace it with "$"
22 self.tablename = GENI_TABLE_PREFIX + self.hrn.replace(".", "$")
24 # establish a connection to the pgsql server
25 self.cnx = DB(cninfo['dbname'], cninfo['address'], port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password'])
27 # if asked to create the table, then create it
32 tableList = self.cnx.get_tables()
33 if 'public.' + self.tablename in tableList:
35 if 'public."' + self.tablename + '"' in tableList:
40 querystr = "CREATE TABLE " + self.tablename + " ( \
47 self.cnx.query('DROP TABLE IF EXISTS ' + self.tablename)
48 self.cnx.query(querystr)
50 def remove(self, record):
51 query_str = "DELETE FROM " + self.tablename + " WHERE key = '" + record.get_key() + "'"
52 self.cnx.query(query_str)
54 def insert(self, record):
55 fieldnames = ["key"] + record.get_field_names()
56 fieldvals = record.get_field_value_strings(fieldnames)
57 query_str = "INSERT INTO " + self.tablename + \
58 "(" + ",".join(fieldnames) + ") " + \
59 "VALUES(" + ",".join(fieldvals) + ")"
61 self.cnx.query(query_str)
63 def update(self, record):
64 names = record.get_field_names()
67 val = record.get_field_value_string(name)
68 pairs.append(name + " = " + val)
69 update = ", ".join(pairs)
71 query_str = "UPDATE " + self.tablename+ " SET " + update + " WHERE key = '" + record.get_key() + "'"
73 self.cnx.query(query_str)
75 def find_dict(self, type, value, searchfield):
76 query_str = "SELECT * FROM " + self.tablename + " WHERE " + searchfield + " = '" + str(value) + "'"
77 dict_list = self.cnx.query(query_str).dictresult()
79 for dict in dict_list:
80 if (type=="*") or (dict['type'] == type):
81 result_dict_list.append(dict)
82 return result_dict_list
84 def find(self, type, value, searchfield):
85 result_dict_list = self.find_dict(type, value, searchfield)
87 for dict in result_dict_list:
88 result_rec_list.append(GeniRecord(dict=dict))
89 return result_rec_list
91 def resolve_dict(self, type, hrn):
92 return self.find_dict(type, hrn, "name")
94 def resolve(self, type, hrn):
95 return self.find(type, hrn, "name")
98 query_str = "SELECT * FROM " + self.tablename
99 result_dict_list = self.cnx.query(query_str).dictresult()
100 return result_dict_list
103 result_dict_list = self.list_dict()
105 for dict in result_dict_list:
106 result_rec_list.append(GeniRecord(dict=dict))
107 return result_rec_list
109 def set_geni_table_prefix(x):
110 global GENI_TABLE_PREFIX
112 GENI_TABLE_PREFIX = x
114 def geni_records_purge(cninfo):
115 global GENI_TABLE_PREFIX
117 cnx = DB(cninfo['dbname'], cninfo['address'], port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password'])
118 tableList = cnx.get_tables()
119 for table in tableList:
120 if table.startswith(GENI_TABLE_PREFIX) or \
121 table.startswith('public.' + GENI_TABLE_PREFIX) or \
122 table.startswith('public."' + GENI_TABLE_PREFIX):
123 report.trace("dropping table " + table)
124 cnx.query("DROP TABLE " + table)