from sfa.util.record import *
from sfa.util.debug import *
-GENI_TABLE_PREFIX = "sfa$"
-
class GeniTable:
+
+ GENI_TABLE_PREFIX = "sfa$"
+
def __init__(self, create=False, hrn="unspecified.default.registry", cninfo=None):
- global GENI_TABLE_PREFIX
self.hrn = hrn
# pgsql doesn't like table names with "." in them, to replace it with "$"
- self.tablename = GENI_TABLE_PREFIX + self.hrn.replace(".", "$")
+ self.tablename = GeniTable.GENI_TABLE_PREFIX + self.hrn.replace(".", "$")
# establish a connection to the pgsql server
self.cnx = DB(cninfo['dbname'], cninfo['address'], port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password'])
self.cnx.query(query_str)
def insert(self, record):
- fieldnames = ["key"] + record.get_field_names()
+ dont_insert = ['date_created', 'last_updated']
+ fields = [field for field in record.fields.keys() if field not in dont_insert]
+ fieldnames = ["key", "pointer"] + fields
fieldvals = record.get_field_value_strings(fieldnames)
query_str = "INSERT INTO " + self.tablename + \
"(" + ",".join(fieldnames) + ") " + \
self.cnx.query(query_str)
def update(self, record):
- names = record.get_field_names()
+ dont_update = ['date_created', 'last_updated']
+ fields = [field for field in record.fields.keys() if field not in dont_update]
+ fieldvals = record.get_field_value_strings(fields)
pairs = []
- for name in names:
- val = record.get_field_value_string(name)
- pairs.append(name + " = " + val)
+ for field in fields:
+ val = record.get_field_value_string(field)
+ pairs.append(field + " = " + val)
update = ", ".join(pairs)
query_str = "UPDATE " + self.tablename+ " SET " + update + " WHERE key = '" + record.get_key() + "'"
result_rec_list.append(GeniRecord(dict=dict).as_dict())
return result_rec_list
-def set_geni_table_prefix(x):
- global GENI_TABLE_PREFIX
-
- GENI_TABLE_PREFIX = x
-
-def geni_records_purge(cninfo):
- global GENI_TABLE_PREFIX
-
- cnx = DB(cninfo['dbname'], cninfo['address'], port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password'])
- tableList = cnx.get_tables()
- for table in tableList:
- if table.startswith(GENI_TABLE_PREFIX) or \
- table.startswith('public.' + GENI_TABLE_PREFIX) or \
- table.startswith('public."' + GENI_TABLE_PREFIX):
- report.trace("dropping table " + table)
- cnx.query("DROP TABLE " + table)
+ def drop(self):
+ try:
+ self.cnx.query('DROP TABLE IF EXISTS ' + self.tablename)
+ except ProgrammingError:
+ try:
+ self.cnx.query('DROP TABLE ' + self.tablename)
+ except ProgrammingError:
+ pass
+
+ @staticmethod
+ def geni_records_purge(cninfo):
+
+ cnx = DB(cninfo['dbname'], cninfo['address'],
+ port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password'])
+ tableList = cnx.get_tables()
+ for table in tableList:
+ if table.startswith(GeniTable.GENI_TABLE_PREFIX) or \
+ table.startswith('public.' + GeniTable.GENI_TABLE_PREFIX) or \
+ table.startswith('public."' + GeniTable.GENI_TABLE_PREFIX):
+ report.trace("dropping table " + table)
+ cnx.query("DROP TABLE " + table)