-- ----------------------------
-- Triggers structure for table product$users
-- ----------------------------
DROP TRIGGER IF EXISTS `sync$product_users_i`;
delimiter ;;
CREATE DEFINER = `root`@`%` TRIGGER `sync$product_users_i` AFTER INSERT ON `product$users` FOR EACH ROW begin
	  declare v_table_name varchar(64) default 'product$users';
    declare v_method_type varchar(6) default 'insert';
    declare v_data_key text;
    declare v_data text;
    declare v_priority int default 1;

    set v_data_key=concat('{"PU_ID": ', new.PU_ID, '}');
    select concat(
      concat('{'),
      -- varchar
      -- bit(1), smallint(6), int(11), bigint(20), double
      concat('"PU_USERUU": ', case when new.PU_USERUU is null then 'null' else new.PU_USERUU end),
      concat(',"PU_ENUU": ', case when new.PU_ENUU is null then 'null' else new.PU_ENUU end),
      concat(',"PU_PRID": ', case when new.PU_PRID is null then 'null' else new.PU_PRID end),
      concat(',"PU_ERPID": ', case when new.PU_ERPID is null then 'null' else new.PU_ERPID end),
      -- datetime
      concat(',"PU_DATE": ', case when new.PU_DATE is null then 'null' else concat('"', replace(new.PU_DATE, '"', '\\"'), '"') end),
      -- text
      -- json
      concat('}')
    ) into v_data;
    call sync$enqueue_message(v_table_name, v_method_type, v_data_key, v_data, v_priority);
end
;;
delimiter ;

-- ----------------------------
-- Triggers structure for table product$users
-- ----------------------------
DROP TRIGGER IF EXISTS `sync$product_users_u`;
delimiter ;;
CREATE DEFINER = `root`@`%` TRIGGER `sync$product_users_u` AFTER UPDATE ON `product$users` FOR EACH ROW begin
	declare v_table_name varchar(64) default 'product$users';
    declare v_method_type varchar(6) default 'update';
    declare v_data_key text;
    declare v_data text;
    declare v_priority int default 1;

    set v_data_key=concat('{"PU_ID": ', old.PU_ID, '}');
    select concat(
      concat('{'),
      -- varchar
      -- bit(1), smallint(6), int(11), bigint(20), double
      concat('"PU_USERUU": ', case when new.PU_USERUU is null then 'null' else new.PU_USERUU end),
      concat(',"PU_ENUU": ', case when new.PU_ENUU is null then 'null' else new.PU_ENUU end),
      concat(',"PU_PRID": ', case when new.PU_PRID is null then 'null' else new.PU_PRID end),
      concat(',"PU_ERPID": ', case when new.PU_ERPID is null then 'null' else new.PU_ERPID end),
      -- datetime
      concat(',"PU_DATE": ', case when new.PU_DATE is null then 'null' else concat('"', replace(new.PU_DATE, '"', '\\"'), '"') end),
      -- text
      -- json
      concat('}')
    ) into v_data;
    call sync$enqueue_message(v_table_name, v_method_type, v_data_key, v_data, v_priority);
end
;;
delimiter ;

-- ----------------------------
-- Triggers structure for table product$users
-- ----------------------------
DROP TRIGGER IF EXISTS `sync$product_users_d`;
delimiter ;;
CREATE DEFINER = `root`@`%` TRIGGER `sync$product_users_d` AFTER DELETE ON `product$users` FOR EACH ROW begin
	declare v_table_name varchar(64) default 'product$users';
    declare v_method_type varchar(6) default 'delete';
    declare v_data_key text;
    declare v_data text;
    declare v_priority int default 1;

    set v_data_key=concat('{"PU_ID": ', old.PU_ID, '}');
    call sync$enqueue_message(v_table_name, v_method_type, v_data_key, v_data, v_priority);
end
;;
delimiter ;