def resolve(self, tagging): self.logger.debug('resolve(): %s', repr(tagging)) if not isinstance(tagging, Tagging): raise ObjectCubeException('Function requires valid Tagging') if not tagging.plugin_set_id: raise ObjectCubeException('Function requires valid plugin set id') if tagging.id: db_tagging = self.update(tagging) else: db_tagging = self.add(tagging) # Delete all the other ones in the set # It is possible that this deletes nothing, which is OK # This could happen, for example, when confirming a tagging # without alternatives sql = 'DELETE ' \ 'FROM TAGGINGS ' \ 'WHERE PLUGIN_SET_ID = %s ' \ ' AND NOT ID = %s ' \ 'RETURNING *' params = (db_tagging.plugin_set_id, db_tagging.id) execute_sql_fetch_single(Tagging, sql, params) return db_tagging
def _delete_all(self, root_node): sql = 'DELETE ' \ 'FROM DIMENSIONS ' \ 'WHERE root_tag_id = %s ' \ 'RETURNING *' params = (root_node.root_tag_id, ) execute_sql_fetch_single(DimensionNode, sql, params) return None
def add(self, tagging): self.logger.debug('add(): %s ', repr(tagging)) if not isinstance(tagging, Tagging): raise ObjectCubeException('Function requires valid tagging') if tagging.id is not None: raise ObjectCubeException('Function must not get Tagging id') if tagging.plugin_set_id and not tagging.plugin_id: raise ObjectCubeException('Cannot have plugin set w/o plugin') # Build the SQL expression, starting with required attributes sql_attributes = 'TAG_ID, OBJECT_ID' sql_values = '%s, %s' params = (tagging.tag_id, tagging.object_id) # Build the SQL expression, continuing with optional attributes if tagging.meta: sql_attributes += ', META' sql_values += ', %s' params += (tagging.meta, ) if tagging.plugin_id: sql_attributes += ', PLUGIN_ID' sql_values += ',%s' params += (tagging.plugin_id, ) if tagging.plugin_set_id: sql_attributes += ', PLUGIN_SET_ID' sql_values += ',%s' params += (tagging.plugin_set_id, ) sql = 'INSERT INTO TAGGINGS (' + \ sql_attributes + \ ') VALUES (' \ + sql_values + \ ') RETURNING *' return execute_sql_fetch_single(Tagging, sql, params)
def update(self, tagging): self.logger.debug('update(): %s', repr(tagging)) if not isinstance(tagging, Tagging): raise ObjectCubeException('Function requires valid tagging') if not tagging.id: raise ObjectCubeException('Function requires valid id') # Get the old tag to verify that it exists, # and then run some business logic checks old = self.retrieve_by_id(tagging.id) if not old: raise ObjectCubeException('No Tag found to update') if tagging.tag_id != old.tag_id \ or tagging.object_id != old.object_id \ or tagging.plugin_id != old.plugin_id \ or tagging.plugin_set_id != old.plugin_set_id: raise ObjectCubeException('Can only update meta') if tagging.meta: sql = 'UPDATE TAGGINGS ' \ 'SET META = %s ' \ 'WHERE ID = %s ' \ 'RETURNING *' params = (tagging.meta, tagging.id) else: sql = 'UPDATE TAGGINGS ' \ 'SET META = NULL ' \ 'WHERE ID = %s ' \ 'RETURNING *' params = (tagging.id, ) return execute_sql_fetch_single(Tagging, sql, params)
def add(self, tag): self.logger.debug('add(): %s', repr(tag)) # Need to give a tag, but it cannot have an ID if not isinstance(tag, Tag): raise ObjectCubeException('Function requires valid Tag') if tag.id is not None: raise ObjectCubeException('Function must not get Tag id') # Build the SQL expression, starting with required attributes sql_attributes = 'VALUE, DESCRIPTION, TYPE, MUTABLE' sql_values = '%s, %s, %s, %s' params = (tag.value, tag.description, tag.type, tag.mutable) # Build the SQL expression, continuing with optional attributes if tag.concept_id is not None: sql_attributes += ', CONCEPT_ID' sql_values += ',%s' params += (tag.concept_id,) if tag.plugin_id is not None: sql_attributes += ', PLUGIN_ID' sql_values += ',%s' params += (tag.plugin_id,) sql = 'INSERT INTO TAGS (' + \ sql_attributes + \ ') VALUES (' \ + sql_values + \ ') RETURNING *' return execute_sql_fetch_single(Tag, sql, params)
def retrieve_by_title(self, title): self.logger.debug('retrieve_or_create(): %s', repr(title)) if not isinstance(title, UnicodeType): raise ObjectCubeException('Function requires valid title') sql = 'SELECT * ' \ 'FROM CONCEPTS ' \ 'WHERE TITLE = %s' params = (title, ) return execute_sql_fetch_single(Concept, sql, params)
def retrieve_by_name(self, name): self.logger.debug('retrieve_by_name(): %s', repr(name)) if not isinstance(name, UnicodeType): raise ObjectCubeException('Function requires valid name') sql = 'SELECT ID, NAME, MODULE ' \ 'FROM PLUGINS ' \ 'WHERE NAME = %s' params = (name, ) return execute_sql_fetch_single(Plugin, sql, params)
def retrieve_by_id(self, id_): self.logger.debug('retrieve_by_id(): %s', repr(id_)) if not isinstance(id_, LongType): raise ObjectCubeException('Function requires valid id') sql = 'SELECT * ' \ 'FROM TAGGINGS ' \ 'WHERE ID = %s' params = (id_, ) return execute_sql_fetch_single(Tagging, sql, params)
def _insert_node(self, node): # Input: A single tree node # Side effect: The tree node has been written to the database # Output: The Node written sql = 'INSERT ' \ 'INTO DIMENSIONS ( ' \ ' ROOT_TAG_ID, NODE_TAG_ID, LEFT_BORDER, RIGHT_BORDER' \ ') ' \ 'VALUES( %s, %s, %s, %s) ' \ 'RETURNING *' params = (node.root_tag_id, node.node_tag_id, node.left_border, node.right_border) return execute_sql_fetch_single(DimensionNode, sql, params)
def add(self, concept): self.logger.debug('add(): %s', repr(concept)) if not isinstance(concept, Concept): raise ObjectCubeException('Function requires valid concept') if concept.id is not None: raise ObjectCubeException('Function must not get id') sql = 'INSERT INTO ' \ 'CONCEPTS (TITLE, DESCRIPTION) ' \ 'VALUES (%s, %s) ' \ 'RETURNING *' params = (concept.title, concept.description) return execute_sql_fetch_single(Concept, sql, params)
def add(self, plugin): self.logger.debug('add(): %s', repr(plugin)) if not isinstance(plugin, Plugin): raise ObjectCubeException('Function requires valid plugin') if plugin.id is not None: raise ObjectCubeException('Function must not get id') sql = 'INSERT ' \ 'INTO PLUGINS (NAME, MODULE) ' \ 'VALUES (%s, %s) ' \ 'RETURNING *' params = (plugin.name, plugin.module) return execute_sql_fetch_single(Plugin, sql, params)
def add(self, object_): self.logger.debug('add(): %s', repr(object_)) if not isinstance(object_, Object): raise ObjectCubeException('Function requires valid Object') if object_.id is not None: raise ObjectCubeException('Function must not get id') sql = 'INSERT ' \ 'INTO OBJECTS (NAME, DIGEST) ' \ 'VALUES (%s, %s) ' \ 'RETURNING *' params = (object_.name, object_.digest) return execute_sql_fetch_single(Object, sql, params)
def delete_by_id(self, id_): self.logger.debug('delete_by_id(): %s', repr(id_)) if not isinstance(id_, LongType): raise ObjectCubeException('Function requires valid id') sql = 'DELETE FROM TAGGINGS ' \ 'WHERE ID = %s' \ 'RETURNING *' params = (id_, ) db_tagging = execute_sql_fetch_single(Tagging, sql, params) if not db_tagging: raise ObjectCubeException('No Tagging found to delete') return None
def update(self, tag): self.logger.debug('update(): %s', repr(tag)) # Need to give a tag, must have ID if not isinstance(tag, Tag): raise ObjectCubeException('Function requires valid Tag') if not tag.id: raise ObjectCubeException('Function requires valid Tag id') # Get the old tag to verify that it exists, # and then run some business logic checks old = self.retrieve_by_id(tag.id) if not old: raise ObjectCubeException('No Tag found to update') if not old.mutable: raise ObjectCubeException('Cannot change a non-mutable concept') if tag.plugin_id != old.plugin_id: raise ObjectCubeException('Cannot change generating plugin ') if old.plugin_id and tag.concept_id != old.concept_id: raise ObjectCubeException('Cannot change plugin-generated concept') # Build the SQL expression for the attributes that may be changed params = tuple() attributes = [] if tag.value != old.value: attributes.append('VALUE = %s') params += (tag.value, ) if tag.description != old.description: attributes.append('DESCRIPTION = %s') params += (tag.description, ) if tag.concept_id != old.concept_id: attributes.append('CONCEPT_ID = %s') params += (tag.concept_id, ) if tag.type != old.type: attributes.append('TYPE = %s') params += (tag.type, ) sql_attributes = ', ' params += (tag.id, ) sql = 'UPDATE TAGS SET ' + \ sql_attributes.join(attributes) + \ ' WHERE ID = %s RETURNING *' return execute_sql_fetch_single(Tag, sql, params)
def delete_by_id(self, id_): self.logger.debug('delete_by_id(): %s', repr(id_)) if not isinstance(id_, LongType): raise ObjectCubeException('Function requires valid id') sql = 'DELETE ' \ 'FROM CONCEPTS ' \ 'WHERE ID = %s ' \ 'RETURNING *' params = (id_, ) db_concept = execute_sql_fetch_single(Concept, sql, params) if not db_concept: raise ObjectCubeException('No Concept found to delete') return None
def delete_by_set_id(self, plugin_set_id): self.logger.debug('delete_by_set_id(): %s', repr(plugin_set_id)) if not isinstance(plugin_set_id, LongType): raise ObjectCubeException('Function requires valid plugin set id') sql = 'DELETE ' \ 'FROM TAGGINGS ' \ 'WHERE PLUGIN_SET_ID = %s ' \ 'RETURNING *' params = (plugin_set_id, ) db_tagging = execute_sql_fetch_single(Tagging, sql, params) if not db_tagging: raise ObjectCubeException('No Tagging found to delete') return None
def delete(self, tag): self.logger.debug('delete(): %s', repr(tag)) if not isinstance(tag, Tag): raise ObjectCubeException('Function requires valid Tag') if not tag.id: raise ObjectCubeException('Function requires valid Tag id') sql = 'DELETE ' \ 'FROM tags ' \ 'WHERE id = %s ' \ 'RETURNING *' params = (tag.id,) db_tag = execute_sql_fetch_single(Tag, sql, params) if not db_tag: raise ObjectCubeException('No Tag found to delete') return None
def delete(self, concept): self.logger.debug('delete(): %s', repr(concept)) if not isinstance(concept, Concept): raise ObjectCubeException('Function requires valid Concept') if not concept.id: raise ObjectCubeException('Function requires valid Concept id') sql = 'DELETE ' \ 'FROM CONCEPTS ' \ 'WHERE ID = %s ' \ 'RETURNING *' params = (concept.id, ) db_concept = execute_sql_fetch_single(Concept, sql, params) if not db_concept: raise ObjectCubeException('No Concept found to delete') return None
def update(self, concept): self.logger.debug('update(): %s', repr(concept)) if not isinstance(concept, Concept): raise ObjectCubeException('Function requires valid Concept') if not concept.id: raise ObjectCubeException('Function requires valid Concept id') sql = 'UPDATE CONCEPTS ' \ 'SET TITLE=%s, DESCRIPTION=%s ' \ 'WHERE ID=%s ' \ 'RETURNING *' params = (concept.title, concept.description, concept.id) db_concept = execute_sql_fetch_single(Concept, sql, params) if not db_concept: raise ObjectCubeException('No Concept found to update') return db_concept
def delete(self, object_): self.logger.debug('delete(): %s', repr(object_)) if not isinstance(object_, Object): raise ObjectCubeException('Function requires valid Object') if not object_.id: raise ObjectCubeException('Function requires valid id') sql = 'DELETE ' \ 'FROM OBJECTS ' \ 'WHERE ID = %s ' \ 'RETURNING *' params = (object_.id, ) db_object = execute_sql_fetch_single(Object, sql, params) if not db_object: raise ObjectCubeException('No Object found to delete') return None
def update(self, object_): self.logger.debug('update(): %s', repr(object_)) if not isinstance(object_, Object): raise ObjectCubeException('Function requires valid Object') if not object_.id: raise ObjectCubeException('Function requires valid id') sql = 'UPDATE OBJECTS ' \ 'SET NAME = %s, DIGEST = %s ' \ 'WHERE ID = %s ' \ 'RETURNING *' params = (object_.name, object_.digest, object_.id) db_object = execute_sql_fetch_single(Object, sql, params) if not db_object: raise ObjectCubeException('No Object found to update') return db_object
def count(self): self.logger.debug('count()') sql = 'SELECT COUNT(1) AS count ' \ 'FROM TAGGINGS' return execute_sql_fetch_single(lambda count: count, sql)
def count(self): self.logger.debug('count()') sql = 'SELECT ' \ 'COUNT(DISTINCT root_tag_id) AS count ' \ 'FROM DIMENSIONS' return execute_sql_fetch_single(lambda count: count, sql)