def update(self, document_id, update_spec, namespace, timestamp): db, collection = db_and_collection(namespace) updated_document = self.get_document_by_id(db, collection, document_id) primary_key = self.mappings[db][collection]['pk'] mapped_field = self.mappings[db][collection].get(primary_key, {}) field_type = mapped_field.get('type') doc_id = to_sql_value(document_id, vtype=field_type) if updated_document is None: return for arrayField in get_any_array_fields(self.mappings, db, collection, updated_document): dest = self.mappings[db][collection][arrayField]['dest'] fk = self.mappings[db][collection][arrayField]['fk'] sql_delete_rows_where( self.pgsql.cursor(), dest, "{0} = {1}".format(fk, doc_id) ) self._upsert(namespace, updated_document, self.pgsql.cursor(), timestamp) self.commit()
def _upsert(self, namespace, document, cursor, timestamp): db, collection = db_and_collection(namespace) primary_key = self.mappings[db][collection]['pk'] sql_delete_rows_where( cursor, collection, '{0} = {1}'.format(primary_key, to_sql_value(document[primary_key]))) sql_bulk_insert(cursor, self.mappings, namespace, [document]) self.commit()
def remove(self, document_id, namespace, timestamp): if not is_mapped(self.mappings, namespace): return with self.pgsql.cursor() as cursor: db, collection = db_and_collection(namespace) primary_key = self.mappings[db][collection]['pk'] mapped_field = self.mappings[db][collection].get(primary_key, {}) field_type = mapped_field.get('type') doc_id = to_sql_value(document_id, vtype=field_type) cursor.execute("DELETE from {0} WHERE {1} = {2};".format( collection.lower(), primary_key, doc_id)) self.commit()
def update(self, document_id, update_spec, namespace, timestamp): db, collection = db_and_collection(namespace) updated_document = self.get_document_by_id(db, collection, document_id) if updated_document is None: return for arrayField in get_any_array_fields(self.mappings, db, collection, updated_document): dest = self.mappings[db][collection][arrayField]['dest'] fk = self.mappings[db][collection][arrayField]['fk'] sql_delete_rows_where(self.pgsql.cursor(), dest, "{0} = {1}".format(fk, to_sql_value(document_id))) self._upsert(namespace, updated_document, self.pgsql.cursor(), timestamp) self.commit()
def update(self, document_id, update_spec, namespace, timestamp): # TODO update this to grab doc, apply_update, and then update (return None if doc not in Postgres) db, collection = db_and_collection(namespace) updated_document = self.get_document_by_id(db, collection, document_id) if updated_document is None: return for arrayField in get_any_array_fields(self.mappings, db, collection, updated_document): dest = self.mappings[db][collection][arrayField]['dest'] fk = self.mappings[db][collection][arrayField]['fk'] sql_delete_rows_where(self.pgsql.cursor(), dest, "{0} = {1}".format(fk, to_sql_value(document_id))) self._upsert(namespace, updated_document, self.pgsql.cursor(), timestamp) self.commit()