def index(self, obj, attributes=None): if not self.supports_multiplex(obj): return # Always insert/update all attributes attributes = self.get_default_attributes(obj) if not attributes: logger.debug("SKIP. No attributes set for {}".format(repr(obj))) return logger.info("Multiplexer::Indexing {}".format(repr(obj))) # Insert the object to the SQL db operation, params = self.get_insert_update(obj, attributes) try: self.execute(operation, params) except mysql.connector.Error as err: if err.errno == errorcode.ER_NO_SUCH_TABLE: # TODO Better to do this out of here try: self.create_table_for(obj, attributes) # And retry self.execute(operation, params) except: return
def execute(self, operation, params=None, raise_error=True): """Executes the given database operation (query or command). The parameters found in the tuple params are bound to the variables in the operation. Specify variables using %s or %(name)s parameter style :param operation: SQL query or command :param data: variables for the operation """ cnx = self.mysql_connection cursor = cnx.cursor() succeed = False try: cursor.execute(operation, params=params) cnx.commit() succeed = True except mysql.connector.Error as err: host = self.get_connection_configuration().get("host") msg = "Multiplexer@{} ER#{} ({}): {}".format( host, err.errno, err.sqlstate, err.msg) logger.error(msg) if raise_error: raise err finally: try: logger.info(cursor.statement) except: pass cursor.close() cnx.close() return succeed
def setup_handler(context): """Generic setup handler """ if context.readDataFile('senaite.sqlmultiplex.install.txt') is None: return logger.info("setup handler [BEGIN]".format(PRODUCT_NAME.upper())) portal = context.getSite() # noqa logger.info("{} setup handler [DONE]".format(PRODUCT_NAME.upper()))
def post_install(portal_setup): """Runs after the last import step of the *default* profile This handler is registered as a *post_handler* in the generic setup profile :param portal_setup: SetupTool """ logger.info("{} install handler [BEGIN]".format(PRODUCT_NAME.upper())) context = portal_setup._getImportContext(PROFILE_ID) # noqa portal = context.getSite() # noqa logger.info("{} install handler [DONE]".format(PRODUCT_NAME.upper()))
def post_uninstall(portal_setup): """Runs after the last import step of the *uninstall* profile This handler is registered as a *post_handler* in the generic setup profile :param portal_setup: SetupTool """ logger.info("{} uninstall handler [BEGIN]".format(PRODUCT_NAME.upper())) # https://docs.plone.org/develop/addons/components/genericsetup.html#custom-installer-code-setuphandlers-py context = portal_setup._getImportContext(UNINSTALL_PROFILE_ID) # noqa portal = context.getSite() # noqa logger.info("{} uninstall handler [DONE]".format(PRODUCT_NAME.upper()))
def unindex(self, obj): if not self.supports_multiplex(obj): return # Delete the object from the SQL db uid = api.get_uid(obj) portal_type = api.get_portal_type(obj) operation = "DELETE FROM {} WHERE UID='{}'".format(portal_type, uid) self.execute(operation, raise_error=False) # Do something here logger.info("Multiplexer::Unindexing {}".format(repr(obj)))
def get_table_create(self, obj, attributes): portal_type = api.get_portal_type(obj) def to_column(attribute): # TODO We just always assume a varchar type here! return "`{}` varchar(191) NULL".format(attribute) # Build the create table operation base = "CREATE TABLE `{}` ({}, PRIMARY KEY (`UID`)) ENGINE=InnoDB " \ "DEFAULT CHARSET=utf8mb4" cols = ", ".join(map(to_column, attributes)) operation = base.format(portal_type, cols) logger.info(operation) return operation
def pre_install(portal_setup): """Runs before the first import step of the *default* profile This handler is registered as a *pre_handler* in the generic setup profile :param portal_setup: SetupTool """ logger.info("{} pre-install handler [BEGIN]".format(PRODUCT_NAME.upper())) context = portal_setup._getImportContext(PROFILE_ID) portal = context.getSite() # noqa # Only install senaite.lims once! qi = portal.portal_quickinstaller if not qi.isProductInstalled("senaite.lims"): profile_name = "profile-senaite.lims:default" portal_setup.runAllImportStepsFromProfile(profile_name) logger.info("{} pre-install handler [DONE]".format(PRODUCT_NAME.upper()))