From 374093bcc707ecc06b84058ef0c7aa6f096f7048 Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Tue, 2 Aug 2022 16:19:31 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=E4=B8=A6=E5=88=97=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=81=A7=E3=81=8D=E3=81=AA=E3=81=84=E3=82=B3=E3=83=A1=E3=83=B3?= =?UTF-8?q?=E3=83=88=E8=BF=BD=E5=8A=A0=E3=80=81=E4=BE=8B=E5=A4=96=E3=81=A7?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E3=81=99=E3=82=8B=E3=83=A6=E3=83=BC=E3=82=B6?= =?UTF-8?q?=E5=A4=89=E6=95=B0=E3=81=AE=E5=88=9D=E6=9C=9F=E5=8C=96=E3=80=81?= =?UTF-8?q?$$=E7=B5=82=E7=AB=AF=E6=96=87=E5=AD=97=E3=81=AE=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rds_mysql/stored_procedure/crm_history.sql | 34 ++++++++++++---------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/rds_mysql/stored_procedure/crm_history.sql b/rds_mysql/stored_procedure/crm_history.sql index 7573bf9b..b60e2681 100644 --- a/rds_mysql/stored_procedure/crm_history.sql +++ b/rds_mysql/stored_procedure/crm_history.sql @@ -1,5 +1,6 @@ -- A5M2で実行時にSQL区切り文字を「;」以外にすること --- $$から始まる文字は後からREPLACEする文字を示す独自ルール +-- $$から始まり$$で終わる文字は後からREPLACEする文字を示す独自ルール +-- crm_historyストアドプロシージャは、同一セッション内での並列処理を実行することができない CREATE PROCEDURE crm_history(target_table VARCHAR(255), target_column VARCHAR(255)) BEGIN -- 例外処理 @@ -15,21 +16,22 @@ BEGIN SET MYSQL_ERRNO = @error_state, MESSAGE_TEXT = @error_msg; END; + SET @error_state = NULL, @error_msg = NULL; START TRANSACTION; -- ②-3で利用する一時テーブル削除のSQLを準備 -- 例外処理でも利用するため先に記述 SET @drop_tmp_table = ' - DROP TEMPORARY TABLE IF EXISTS $$target_table_make_history_tmp + DROP TEMPORARY TABLE IF EXISTS $$target_table$$_make_history_tmp '; - SET @drop_tmp_table = REPLACE(@drop_tmp_table, "$$target_table", target_table); + SET @drop_tmp_table = REPLACE(@drop_tmp_table, "$$target_table$$", target_table); PREPARE drop_tmp_table_stmt from @drop_tmp_table; -- ①-1 Salesforce側で更新されたデータの適用開始日時と適用終了日時を設定する SET @new_history_save = ' - UPDATE $$target_table + UPDATE $$target_table$$ SET - start_datetime = $$target_column + start_datetime = $$target_column$$ , end_datetime = "9999-12-31 00:00:00" , upd_user = CURRENT_USER() , upd_date = CURRENT_TIMESTAMP() @@ -37,21 +39,21 @@ BEGIN start_datetime IS NULL AND end_datetime IS NULL '; - SET @new_history_save = REPLACE(@new_history_save, "$$target_table", target_table); - SET @new_history_save = REPLACE(@new_history_save, "$$target_column", target_column); + SET @new_history_save = REPLACE(@new_history_save, "$$target_table$$", target_table); + SET @new_history_save = REPLACE(@new_history_save, "$$target_column$$", target_column); PREPARE new_history_save_stmt from @new_history_save; EXECUTE new_history_save_stmt; DEALLOCATE PREPARE new_history_save_stmt; -- ②-1 Salesforce側で更新されたデータを検出用一時テーブルの作成 SET @make_history_tmp_create = ' - CREATE TEMPORARY TABLE $$target_table_make_history_tmp + CREATE TEMPORARY TABLE $$target_table$$_make_history_tmp SELECT id - , MIN($$target_column) AS min_start_datetime + , MIN($$target_column$$) AS min_start_datetime , MAX(start_datetime) AS max_start_datetime FROM - $$target_table + $$target_table$$ WHERE end_datetime = "9999-12-31 00:00:00" GROUP BY @@ -59,16 +61,16 @@ BEGIN HAVING count(id) = 2 '; - SET @make_history_tmp_create = REPLACE(@make_history_tmp_create, "$$target_table", target_table); - SET @make_history_tmp_create = REPLACE(@make_history_tmp_create, "$$target_column", target_column); + SET @make_history_tmp_create = REPLACE(@make_history_tmp_create, "$$target_table$$", target_table); + SET @make_history_tmp_create = REPLACE(@make_history_tmp_create, "$$target_column$$", target_column); PREPARE make_history_tmp_create_stmt from @make_history_tmp_create; EXECUTE make_history_tmp_create_stmt; DEALLOCATE PREPARE make_history_tmp_create_stmt; -- ②-2 「②-1」で取得した全件に更新処理を行う SET @update_end_datetime = ' - UPDATE $$target_table tt - LEFT JOIN $$target_table_make_history_tmp mht + UPDATE $$target_table$$ tt + LEFT JOIN $$target_table$$_make_history_tmp mht ON tt.id = mht.id AND tt.start_datetime = mht.min_start_datetime SET @@ -78,8 +80,8 @@ BEGIN WHERE mht.id IS NOT NULL '; - SET @update_end_datetime = REPLACE(@update_end_datetime, "$$target_table", target_table); - SET @update_end_datetime = REPLACE(@update_end_datetime, "$$target_column", target_column); + SET @update_end_datetime = REPLACE(@update_end_datetime, "$$target_table$$", target_table); + SET @update_end_datetime = REPLACE(@update_end_datetime, "$$target_column$$", target_column); PREPARE update_end_datetime_stmt from @update_end_datetime; EXECUTE update_end_datetime_stmt; DEALLOCATE PREPARE update_end_datetime_stmt;