- self.validate()
-
- # Fetch a new pcu_id if necessary
- if 'pcu_id' not in self:
- rows = self.api.db.selectall("SELECT NEXTVAL('pcu_pcu_id_seq') AS pcu_id")
- if not rows:
- raise PLCDBError, "Unable to fetch new pcu_id"
- self['pcu_id'] = rows[0]['pcu_id']
- insert = True
- else:
- insert = False
-
- # Filter out unknown fields
- fields = dict(filter(lambda (key, value): key in self.fields,
- self.items()))
-
- # Parameterize for safety
- keys = fields.keys()
- values = [self.api.db.param(key, value) for (key, value) in fields.items()]
-
- if insert:
- # Insert new row in pcu table
- sql = "INSERT INTO pcu (%s) VALUES (%s)" % \
- (", ".join(keys), ", ".join(values))
- else:
- # Update existing row in sites table
- columns = ["%s = %s" % (key, value) for (key, value) in zip(keys, values)]
- sql = "UPDATE pcu SET " + \
- ", ".join(columns) + \
- " WHERE pcu_id = %(pcu_id)d"
-
- self.api.db.do(sql, fields)
-
- if commit:
- self.api.db.commit()
-
- def delete(self, commit = True):
+ assert 'pcu_id' in self
+ assert self.table_name
+
+ self.api.db.do("UPDATE %s SET %s = CURRENT_TIMESTAMP " % (self.table_name, col_name) + \
+ " where pcu_id = %d" % (self['pcu_id']) )
+ self.sync(commit)
+
+ def update_last_updated(self, commit = True):
+ self.update_timestamp('last_updated', commit)
+
+ def add_node(self, node, port, commit = True):
+ """
+ Add node to existing PCU.
+ """
+
+ assert 'pcu_id' in self
+ assert isinstance(node, Node)
+ assert isinstance(port, (int, long))
+ assert 'node_id' in node
+
+ pcu_id = self['pcu_id']
+ node_id = node['node_id']
+
+ if node_id not in self['node_ids'] and port not in self['ports']:
+ self.api.db.do("INSERT INTO pcu_node (pcu_id, node_id, port)" \
+ " VALUES(%(pcu_id)d, %(node_id)d, %(port)d)",
+ locals())
+
+ if commit:
+ self.api.db.commit()
+
+ self['node_ids'].append(node_id)
+ self['ports'].append(port)
+
+ def remove_node(self, node, commit = True):