dont raise the excpetion
[sfa.git] / sfa / util / genitable.py
index 8155be4..942848f 100644 (file)
@@ -15,16 +15,13 @@ from sfa.trust.gid import *
 from sfa.util.record import *
 from sfa.util.debug import *
 
-GENI_TABLE_PREFIX = "sfa$"
-
 class GeniTable:
-    def __init__(self, create=False, hrn="unspecified.default.registry", cninfo=None):
-        global GENI_TABLE_PREFIX
 
-        self.hrn = hrn
+    GENI_TABLE_PREFIX = "sfa"
+
+    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'])
@@ -42,9 +39,13 @@ class GeniTable:
         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, \
+                authority text, \
                 hrn text, \
                 gid text, \
                 type text, \
@@ -53,7 +54,7 @@ class GeniTable:
                 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 ['key', 'hrn', 'type','pointer']]
+                   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)
@@ -72,7 +73,9 @@ class GeniTable:
         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) + ") " + \
@@ -81,7 +84,9 @@ class GeniTable:
         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)
@@ -135,19 +140,15 @@ class GeniTable:
             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)