from sfa.util.record import *
from sfa.util.debug import *
-GENI_TABLE_PREFIX = "geni$"
+class GeniTable:
-class GeniTable():
- def __init__(self, create=False, hrn="unspecified.default.registry", cninfo=None):
- global GENI_TABLE_PREFIX
+ GENI_TABLE_PREFIX = "sfa"
- self.hrn = hrn
+ def __init__(self, create=False, cninfo=None):
- # 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
# establish a connection to the pgsql server
self.cnx = DB(cninfo['dbname'], cninfo['address'], port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password'])
return False
def create(self):
+ seln't like table names with "." in them, to
+
+ # pgsql doesn't like table names with "." in them, to.hrn = hrn
querystr = "CREATE TABLE " + self.tablename + " ( \
key text, \
- name text, \
+ authority text, \
+ hrn text, \
gid text, \
type text, \
- pointer integer);"
+ pointer integer, \
+ date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, \
+ last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP);"
template = "CREATE INDEX %s_%s_idx ON %s (%s);"
indexes = [template % ( self.tablename, field, self.tablename, field) \
- for field in ['name', 'type' ]]
+ for field in ['key', 'authority', 'hrn', 'type','pointer']]
# IF EXISTS doenst exist in postgres < 8.2
try:
self.cnx.query('DROP TABLE IF EXISTS ' + self.tablename)
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):
+ dont_update = ['date_created', 'last_updated']
names = record.get_field_names()
+ names = [name for name in names if name not in dont_update]
pairs = []
for name in names:
val = record.get_field_value_string(name)
result_dict_list = []
for dict in dict_list:
if (type=="*") or (dict['type'] == type):
- dict['hrn'] = dict['name']
result_dict_list.append(dict)
return result_dict_list
return result_rec_list
def resolve_dict(self, type, hrn):
- return self.find_dict(type, hrn, "name")
+ return self.find_dict(type, hrn, "hrn")
def resolve(self, type, hrn):
- return self.find(type, hrn, "name")
+ return self.find(type, hrn, "hrn")
def list_dict(self):
query_str = "SELECT * FROM " + self.tablename
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)
+ @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)