from sfa.util.config import *
from sfa.util.filter import *
-class Row(dict):
-
- # Set this to the name of the table that stores the row.
- # e.g. table_name = "nodes"
- table_name = None
-
- # Set this to the name of the primary key of the table. It is
- # assumed that the this key is a sequence if it is not set when
- # sync() is called.
- # e.g. primary_key="record_id"
- primary_key = None
-
- # Set this to the names of tables that reference this table's
- # primary key.
- join_tables = []
-
- def validate(self):
- """
- Validates values. Will validate a value with a custom function
- if a function named 'validate_[key]' exists.
- """
- # Warn about mandatory fields
- # XX TODO: Support checking for mandatory fields later
- #mandatory_fields = self.db.fields(self.table_name, notnull = True, hasdef = False)
- #for field in mandatory_fields:
- # if not self.has_key(field) or self[field] is None:
- # raise GeniInvalidArgument, field + " must be specified and cannot be unset in class %s"%self.__class__.__name__
-
- # Validate values before committing
- for key, value in self.iteritems():
- if value is not None and hasattr(self, 'validate_' + key):
- validate = getattr(self, 'validate_' + key)
- self[key] = validate(value)
-
-
- def validate_timestamp(self, timestamp, check_future = False):
- """
- Validates the specified GMT timestamp string (must be in
- %Y-%m-%d %H:%M:%S format) or number (seconds since UNIX epoch,
- i.e., 1970-01-01 00:00:00 GMT). If check_future is True,
- raises an exception if timestamp is not in the future. Returns
- a GMT timestamp string.
- """
-
- time_format = "%Y-%m-%d %H:%M:%S"
-
- if isinstance(timestamp, StringTypes):
- # calendar.timegm() is the inverse of time.gmtime()
- timestamp = calendar.timegm(time.strptime(timestamp, time_format))
-
- # Human readable timestamp string
- human = time.strftime(time_format, time.gmtime(timestamp))
-
- if check_future and timestamp < time.time():
- raise GeniInvalidArgument, "'%s' not in the future" % human
-
- return human
-
class GeniTable(list):
GENI_TABLE_PREFIX = "sfa"
self.cnx.query(index)
def remove(self, record):
- query_str = "DELETE FROM %s WHERE record_id = %s" % (self.tablename, record['record_id'])
+ query_str = "DELETE FROM %s WHERE record_id = %s" % \
+ (self.tablename, record['record_id'])
self.cnx.query(query_str)
+
+ # if this is a site, remove all records where 'authority' == the
+ # site's hrn
+ if record['type'] == 'site':
+ sql = " DELETE FROM %s WHERE authority = %s" % \
+ (self.tablename, record['hrn'])
+ self.cnx.query(sql)
def insert(self, record):
db_fields = self.db_fields(record)
self.db.do(query_str, db_fields)
self.db.commit()
+ def quote_string(self, value):
+ return str(self.quote(value))
+
def quote(self, value):
"""
Returns quoted version of the specified value.
# The pgdb._quote function is good enough for general SQL
# quoting, except for array types.
if isinstance(value, (list, tuple, set)):
- return "ARRAY[%s]" % ", ".join(map, self.quote, value)
+ return "ARRAY[%s]" % ", ".join(map, self.quote_string, value)
else:
return pgdb._quote(value)
- def find(self, record_filter = None):
- sql = "SELECT * FROM %s WHERE True " % self.tablename
+ def find(self, record_filter = None, columns=None):
+ if not columns:
+ columns = "*"
+ else:
+ columns = ",".join(columns)
+ sql = "SELECT %s FROM %s WHERE True " % (columns, self.tablename)
if isinstance(record_filter, (list, tuple, set)):
ints = filter(lambda x: isinstance(x, (int, long)), record_filter)
results = [results]
return results
- def findObjects(self, record_filter = None):
+ def findObjects(self, record_filter = None, columns=None):
- results = self.find(record_filter)
+ results = self.find(record_filter, columns)
result_rec_list = []
for result in results:
if result['type'] in ['authority']: