From 014385379c00eb77805f0ac2d826c66683d00ced Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Thu, 14 Jul 2022 14:51:24 +0900 Subject: [PATCH 01/12] =?UTF-8?q?feat:crm=5Fhistory=E3=81=AE=E6=96=B0?= =?UTF-8?q?=E8=A6=8F=E8=BF=BD=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 | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 rds_mysql/stored_procedure/crm_history.sql diff --git a/rds_mysql/stored_procedure/crm_history.sql b/rds_mysql/stored_procedure/crm_history.sql new file mode 100644 index 00000000..cf2ca1d8 --- /dev/null +++ b/rds_mysql/stored_procedure/crm_history.sql @@ -0,0 +1,24 @@ +-- A5M2で実行時にSQL区切り文字を「;」以外にすること + +CREATE PROCEDURE crm_history(target_table VARCHAR(255), target_column VARCHAR(255)) +BEGIN + SET @new_history_save = 'UPDATE @@target_table SET start_datetime = @@target_column, end_datetime = "9999-12-31 00:00:00" WHERE 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); + PREPARE new_history_save_stmt from @new_history_save; + EXECUTE new_history_save_stmt; + + SET @make_history_tmp_create = 'CREATE TEMPORARY TABLE make_history_tmp SELECT id, MIN(@@target_column) AS min_start_datetime, MAX(start_datetime) AS max_start_datetime FROM @@target_table WHERE end_datetime = "9999-12-31 00:00:00" GROUP BY id'; + 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; + + SET @update_end_datetime = 'UPDATE @@target_table tt LEFT JOIN make_history_tmp mht ON tt.id = mht.id AND tt.start_datetime = mht.min_start_datetime SET start_datetime = mht.max_start_datetime - INTERVAL 1 SECOND WHERE mht.id IS 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); + PREPARE update_end_datetime_stmt from @update_end_datetime; + EXECUTE update_end_datetime_stmt; + + DROP TEMPORARY TABLE make_history_tmp; +END \ No newline at end of file From a89762eb0d0231de2e487becdd99443f3912d2e7 Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Fri, 15 Jul 2022 09:35:55 +0900 Subject: [PATCH 02/12] =?UTF-8?q?refactor:=E3=82=B3=E3=83=A1=E3=83=B3?= =?UTF-8?q?=E3=83=88=E3=81=AE=E8=BF=BD=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 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rds_mysql/stored_procedure/crm_history.sql b/rds_mysql/stored_procedure/crm_history.sql index cf2ca1d8..3247da51 100644 --- a/rds_mysql/stored_procedure/crm_history.sql +++ b/rds_mysql/stored_procedure/crm_history.sql @@ -2,23 +2,27 @@ CREATE PROCEDURE crm_history(target_table VARCHAR(255), target_column VARCHAR(255)) BEGIN + -- ①-1 Salesforce側で更新されたデータの適用開始日時と適用終了日時を設定する SET @new_history_save = 'UPDATE @@target_table SET start_datetime = @@target_column, end_datetime = "9999-12-31 00:00:00" WHERE 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); PREPARE new_history_save_stmt from @new_history_save; EXECUTE new_history_save_stmt; + -- ②-1 Salesforce側で更新されたデータを検出用一時テーブルの作成 SET @make_history_tmp_create = 'CREATE TEMPORARY TABLE make_history_tmp SELECT id, MIN(@@target_column) AS min_start_datetime, MAX(start_datetime) AS max_start_datetime FROM @@target_table WHERE end_datetime = "9999-12-31 00:00:00" GROUP BY id'; 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; + -- ②-2 「②-1」で取得した全件に更新処理を行う SET @update_end_datetime = 'UPDATE @@target_table tt LEFT JOIN make_history_tmp mht ON tt.id = mht.id AND tt.start_datetime = mht.min_start_datetime SET start_datetime = mht.max_start_datetime - INTERVAL 1 SECOND WHERE mht.id IS 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); PREPARE update_end_datetime_stmt from @update_end_datetime; EXECUTE update_end_datetime_stmt; + -- ②-3 「②-1」で作成した一時テーブルを削除する DROP TEMPORARY TABLE make_history_tmp; END \ No newline at end of file From e24d299cb8acc449db88523810f575702aa7082e Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Fri, 15 Jul 2022 10:44:06 +0900 Subject: [PATCH 03/12] =?UTF-8?q?fix:=E4=B8=80=E6=99=82=E3=83=86=E3=83=BC?= =?UTF-8?q?=E3=83=96=E3=83=AB=E4=BD=9C=E6=88=90=E6=99=82=E3=81=AE=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E3=83=9F=E3=82=B9=E3=82=92=E4=BF=AE=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rds_mysql/stored_procedure/crm_history.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rds_mysql/stored_procedure/crm_history.sql b/rds_mysql/stored_procedure/crm_history.sql index 3247da51..caeb09c0 100644 --- a/rds_mysql/stored_procedure/crm_history.sql +++ b/rds_mysql/stored_procedure/crm_history.sql @@ -10,7 +10,7 @@ BEGIN EXECUTE new_history_save_stmt; -- ②-1 Salesforce側で更新されたデータを検出用一時テーブルの作成 - SET @make_history_tmp_create = 'CREATE TEMPORARY TABLE make_history_tmp SELECT id, MIN(@@target_column) AS min_start_datetime, MAX(start_datetime) AS max_start_datetime FROM @@target_table WHERE end_datetime = "9999-12-31 00:00:00" GROUP BY id'; + SET @make_history_tmp_create = 'CREATE TEMPORARY TABLE make_history_tmp SELECT id, MIN(@@target_column) AS min_start_datetime, MAX(start_datetime) AS max_start_datetime FROM @@target_table WHERE end_datetime = "9999-12-31 00:00:00" GROUP BY id 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); PREPARE make_history_tmp_create_stmt from @make_history_tmp_create; From d165cf20ba8a84f6152eadd9d442d2a656342b11 Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Fri, 15 Jul 2022 14:23:49 +0900 Subject: [PATCH 04/12] =?UTF-8?q?fix:=20=E2=91=A1-2=E3=81=AE=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E3=83=9F=E3=82=B9=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rds_mysql/stored_procedure/crm_history.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rds_mysql/stored_procedure/crm_history.sql b/rds_mysql/stored_procedure/crm_history.sql index caeb09c0..7c183714 100644 --- a/rds_mysql/stored_procedure/crm_history.sql +++ b/rds_mysql/stored_procedure/crm_history.sql @@ -17,7 +17,7 @@ BEGIN EXECUTE make_history_tmp_create_stmt; -- ②-2 「②-1」で取得した全件に更新処理を行う - SET @update_end_datetime = 'UPDATE @@target_table tt LEFT JOIN make_history_tmp mht ON tt.id = mht.id AND tt.start_datetime = mht.min_start_datetime SET start_datetime = mht.max_start_datetime - INTERVAL 1 SECOND WHERE mht.id IS NULL'; + SET @update_end_datetime = 'UPDATE @@target_table tt LEFT JOIN make_history_tmp mht ON tt.id = mht.id AND tt.start_datetime = mht.min_start_datetime SET start_datetime = mht.max_start_datetime - INTERVAL 1 SECOND 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); PREPARE update_end_datetime_stmt from @update_end_datetime; From 9a62a7eb5869afe60280b135e006835feecc04e0 Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Wed, 20 Jul 2022 09:01:48 +0900 Subject: [PATCH 05/12] =?UTF-8?q?fix:=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC?= =?UTF-8?q?=E6=8C=87=E6=91=98=E4=BF=AE=E6=AD=A3=E3=80=81=E4=BE=8B=E5=A4=96?= =?UTF-8?q?=E5=87=A6=E7=90=86=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rds_mysql/stored_procedure/crm_history.sql | 54 +++++++++++++++++++--- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/rds_mysql/stored_procedure/crm_history.sql b/rds_mysql/stored_procedure/crm_history.sql index 7c183714..f9057a5e 100644 --- a/rds_mysql/stored_procedure/crm_history.sql +++ b/rds_mysql/stored_procedure/crm_history.sql @@ -1,28 +1,70 @@ -- A5M2で実行時にSQL区切り文字を「;」以外にすること - CREATE PROCEDURE crm_history(target_table VARCHAR(255), target_column VARCHAR(255)) BEGIN + -- 例外処理 + -- エラーが発生した場合に一時テーブルの削除を実施 + DECLARE EXIT HANDLER FOR SQLEXCEPTION + BEGIN + GET DIAGNOSTICS CONDITION 1 + @error_state = RETURNED_SQLSTATE, @error_msg = MESSAGE_TEXT; + DROP TEMPORARY TABLE IF EXISTS make_history_tmp; + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = @error_msg, MYSQL_ERRNO = @error_state; + END; + -- ①-1 Salesforce側で更新されたデータの適用開始日時と適用終了日時を設定する - SET @new_history_save = 'UPDATE @@target_table SET start_datetime = @@target_column, end_datetime = "9999-12-31 00:00:00" WHERE start_datetime IS NULL AND end_datetime IS NULL'; + SET @new_history_save = ' + UPDATE @@target_table + SET + start_datetime = @@target_column + , end_datetime = "9999-12-31 00:00:00" + WHERE + 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); PREPARE new_history_save_stmt from @new_history_save; EXECUTE new_history_save_stmt; -- ②-1 Salesforce側で更新されたデータを検出用一時テーブルの作成 - SET @make_history_tmp_create = 'CREATE TEMPORARY TABLE make_history_tmp SELECT id, MIN(@@target_column) AS min_start_datetime, MAX(start_datetime) AS max_start_datetime FROM @@target_table WHERE end_datetime = "9999-12-31 00:00:00" GROUP BY id HAVING count(id) = 2'; + SET @make_history_tmp_create = ' + CREATE TEMPORARY TABLE make_history_tmp + SELECT + id + , MIN(@@target_column) AS min_start_datetime + , MAX(start_datetime) AS max_start_datetime + FROM + @@target_table + WHERE + end_datetime = "9999-12-31 00:00:00" + GROUP BY + id + 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_column", target_column); PREPARE make_history_tmp_create_stmt from @make_history_tmp_create; EXECUTE make_history_tmp_create_stmt; -- ②-2 「②-1」で取得した全件に更新処理を行う - SET @update_end_datetime = 'UPDATE @@target_table tt LEFT JOIN make_history_tmp mht ON tt.id = mht.id AND tt.start_datetime = mht.min_start_datetime SET start_datetime = mht.max_start_datetime - INTERVAL 1 SECOND WHERE mht.id IS NOT NULL'; + SET @update_end_datetime = ' + UPDATE @@target_table tt + LEFT JOIN make_history_tmp mht + ON tt.id = mht.id + AND tt.start_datetime = mht.min_start_datetime + SET + start_datetime = mht.max_start_datetime - INTERVAL 1 SECOND + 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_column", target_column); PREPARE update_end_datetime_stmt from @update_end_datetime; EXECUTE update_end_datetime_stmt; -- ②-3 「②-1」で作成した一時テーブルを削除する DROP TEMPORARY TABLE make_history_tmp; + END \ No newline at end of file From 085d561371a60e9fd8031485489ce6e8004d51f2 Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Wed, 20 Jul 2022 11:26:58 +0900 Subject: [PATCH 06/12] =?UTF-8?q?refactor:=E5=8D=8A=E8=A7=92=E3=82=B9?= =?UTF-8?q?=E3=83=9A=E3=83=BC=E3=82=B9=E3=82=92=E3=82=BF=E3=83=96=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rds_mysql/stored_procedure/crm_history.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rds_mysql/stored_procedure/crm_history.sql b/rds_mysql/stored_procedure/crm_history.sql index f9057a5e..794abe38 100644 --- a/rds_mysql/stored_procedure/crm_history.sql +++ b/rds_mysql/stored_procedure/crm_history.sql @@ -9,15 +9,15 @@ BEGIN @error_state = RETURNED_SQLSTATE, @error_msg = MESSAGE_TEXT; DROP TEMPORARY TABLE IF EXISTS make_history_tmp; SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = @error_msg, MYSQL_ERRNO = @error_state; + SET MESSAGE_TEXT = @error_msg, MYSQL_ERRNO = @error_state; END; -- ①-1 Salesforce側で更新されたデータの適用開始日時と適用終了日時を設定する SET @new_history_save = ' UPDATE @@target_table SET - start_datetime = @@target_column - , end_datetime = "9999-12-31 00:00:00" + start_datetime = @@target_column + , end_datetime = "9999-12-31 00:00:00" WHERE start_datetime IS NULL AND end_datetime IS NULL From 0079d1110c54d1da43b12e69b3028840c20b2ff7 Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Wed, 20 Jul 2022 13:58:59 +0900 Subject: [PATCH 07/12] =?UTF-8?q?refactor:@@=E3=81=8B=E3=82=89=E5=A7=8B?= =?UTF-8?q?=E3=81=BE=E3=82=8B=E6=96=87=E5=AD=97=E3=81=AE=E8=AA=AC=E6=98=8E?= =?UTF-8?q?=E3=82=92=E8=BF=BD=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 | 1 + 1 file changed, 1 insertion(+) diff --git a/rds_mysql/stored_procedure/crm_history.sql b/rds_mysql/stored_procedure/crm_history.sql index 794abe38..0777912f 100644 --- a/rds_mysql/stored_procedure/crm_history.sql +++ b/rds_mysql/stored_procedure/crm_history.sql @@ -1,4 +1,5 @@ -- A5M2で実行時にSQL区切り文字を「;」以外にすること +-- @@から始まる文字は後からREPLACEする文字を示す独自ルール CREATE PROCEDURE crm_history(target_table VARCHAR(255), target_column VARCHAR(255)) BEGIN -- 例外処理 From e96d8be181dd03e3d36550e6a7fb8a1db38a7d30 Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Wed, 20 Jul 2022 15:40:14 +0900 Subject: [PATCH 08/12] =?UTF-8?q?fix:=E4=B8=8D=E8=B6=B3=E9=A0=85=E7=9B=AE?= =?UTF-8?q?=E3=81=A8=E8=A8=AD=E5=AE=9A=E9=A0=85=E7=9B=AE=E3=83=9F=E3=82=B9?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rds_mysql/stored_procedure/crm_history.sql | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/rds_mysql/stored_procedure/crm_history.sql b/rds_mysql/stored_procedure/crm_history.sql index 0777912f..00279493 100644 --- a/rds_mysql/stored_procedure/crm_history.sql +++ b/rds_mysql/stored_procedure/crm_history.sql @@ -18,7 +18,9 @@ BEGIN UPDATE @@target_table SET start_datetime = @@target_column - , end_datetime = "9999-12-31 00:00:00" + , end_datetime = "9999-12-31 00:00:00" + , upd_user = CURRENT_USER() + , upd_date = CURRENT_TIMESTAMP() WHERE start_datetime IS NULL AND end_datetime IS NULL @@ -56,7 +58,9 @@ BEGIN ON tt.id = mht.id AND tt.start_datetime = mht.min_start_datetime SET - start_datetime = mht.max_start_datetime - INTERVAL 1 SECOND + end_datetime = mht.max_start_datetime - INTERVAL 1 SECOND + , upd_user = CURRENT_USER() + , upd_date = CURRENT_TIMESTAMP() WHERE mht.id IS NOT NULL '; From e2609471aed60b30deac3546cb86fd3e38113007 Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Wed, 20 Jul 2022 18:36:34 +0900 Subject: [PATCH 09/12] =?UTF-8?q?refactor:@@=E2=86=92$$=E3=81=AB=E5=A4=89?= =?UTF-8?q?=E6=9B=B4=E3=80=81=E4=B8=80=E6=99=82=E3=83=86=E3=83=BC=E3=83=96?= =?UTF-8?q?=E3=83=AB=E3=81=AE=E3=83=86=E3=83=BC=E3=83=96=E3=83=AB=E5=90=8D?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E3=80=81DEALLOCATE=E8=BF=BD=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 | 45 ++++++++++++++-------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/rds_mysql/stored_procedure/crm_history.sql b/rds_mysql/stored_procedure/crm_history.sql index 00279493..a83cc581 100644 --- a/rds_mysql/stored_procedure/crm_history.sql +++ b/rds_mysql/stored_procedure/crm_history.sql @@ -1,5 +1,5 @@ -- A5M2で実行時にSQL区切り文字を「;」以外にすること --- @@から始まる文字は後からREPLACEする文字を示す独自ルール +-- $$から始まる文字は後からREPLACEする文字を示す独自ルール CREATE PROCEDURE crm_history(target_table VARCHAR(255), target_column VARCHAR(255)) BEGIN -- 例外処理 @@ -8,16 +8,25 @@ BEGIN BEGIN GET DIAGNOSTICS CONDITION 1 @error_state = RETURNED_SQLSTATE, @error_msg = MESSAGE_TEXT; - DROP TEMPORARY TABLE IF EXISTS make_history_tmp; + EXECUTE drop_tmp_table; + DEALLOCATE PREPARE drop_tmp_table; SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = @error_msg, MYSQL_ERRNO = @error_state; END; + -- ②-3で利用する一時テーブル削除のSQLを準備 + -- 例外処理でも利用するため先に記述 + SET @drop_tmp_table = ' + DROP TEMPORARY TABLE IF EXISTS make_history_tmp_$$target_table + '; + SET @drop_tmp_table = REPLACE(@drop_tmp_table, "$$target_table", target_table); + PREPARE new_history_save_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() @@ -25,20 +34,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 ew_history_save_stmt; -- ②-1 Salesforce側で更新されたデータを検出用一時テーブルの作成 SET @make_history_tmp_create = ' - CREATE TEMPORARY TABLE make_history_tmp + CREATE TEMPORARY TABLE make_history_tmp_$$target_table 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 @@ -46,15 +56,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 make_history_tmp mht + UPDATE $$target_table tt + LEFT JOIN make_history_tmp_$$target_table mht ON tt.id = mht.id AND tt.start_datetime = mht.min_start_datetime SET @@ -64,12 +75,14 @@ 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; -- ②-3 「②-1」で作成した一時テーブルを削除する - DROP TEMPORARY TABLE make_history_tmp; + EXECUTE drop_tmp_table; + DEALLOCATE PREPARE drop_tmp_table; END \ No newline at end of file From 611acb6a3722499a874476bc52e29e67636f5e15 Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Thu, 21 Jul 2022 09:31:40 +0900 Subject: [PATCH 10/12] =?UTF-8?q?refactor:=E4=B8=80=E6=99=82=E3=83=86?= =?UTF-8?q?=E3=83=BC=E3=83=96=E3=83=AB=E5=90=8D=E3=81=AE=E5=A4=89=E6=9B=B4?= =?UTF-8?q?=E3=80=81=E7=B4=B0=E3=81=8B=E3=81=84=E3=83=9F=E3=82=B9=E3=81=AE?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rds_mysql/stored_procedure/crm_history.sql | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/rds_mysql/stored_procedure/crm_history.sql b/rds_mysql/stored_procedure/crm_history.sql index a83cc581..f361a850 100644 --- a/rds_mysql/stored_procedure/crm_history.sql +++ b/rds_mysql/stored_procedure/crm_history.sql @@ -8,19 +8,19 @@ BEGIN BEGIN GET DIAGNOSTICS CONDITION 1 @error_state = RETURNED_SQLSTATE, @error_msg = MESSAGE_TEXT; - EXECUTE drop_tmp_table; - DEALLOCATE PREPARE drop_tmp_table; + EXECUTE drop_tmp_table_stmt; + DEALLOCATE PREPARE drop_tmp_table_stmt; SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = @error_msg, MYSQL_ERRNO = @error_state; + SET MYSQL_ERRNO = @error_state, MESSAGE_TEXT = @error_msg; END; -- ②-3で利用する一時テーブル削除のSQLを準備 -- 例外処理でも利用するため先に記述 SET @drop_tmp_table = ' - DROP TEMPORARY TABLE IF EXISTS make_history_tmp_$$target_table + DROP TEMPORARY TABLE IF EXISTS $$target_table_make_history_tmp '; SET @drop_tmp_table = REPLACE(@drop_tmp_table, "$$target_table", target_table); - PREPARE new_history_save_stmt from @drop_tmp_table; + PREPARE drop_tmp_table_stmt from @drop_tmp_table; -- ①-1 Salesforce側で更新されたデータの適用開始日時と適用終了日時を設定する SET @new_history_save = ' @@ -38,11 +38,11 @@ BEGIN 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 ew_history_save_stmt; + DEALLOCATE PREPARE new_history_save_stmt; -- ②-1 Salesforce側で更新されたデータを検出用一時テーブルの作成 SET @make_history_tmp_create = ' - CREATE TEMPORARY TABLE make_history_tmp_$$target_table + CREATE TEMPORARY TABLE $$target_table_make_history_tmp SELECT id , MIN($$target_column) AS min_start_datetime @@ -65,7 +65,7 @@ BEGIN -- ②-2 「②-1」で取得した全件に更新処理を行う SET @update_end_datetime = ' UPDATE $$target_table tt - LEFT JOIN make_history_tmp_$$target_table mht + LEFT JOIN $$target_table_make_history_tmp mht ON tt.id = mht.id AND tt.start_datetime = mht.min_start_datetime SET @@ -82,7 +82,7 @@ BEGIN DEALLOCATE PREPARE update_end_datetime_stmt; -- ②-3 「②-1」で作成した一時テーブルを削除する - EXECUTE drop_tmp_table; - DEALLOCATE PREPARE drop_tmp_table; + EXECUTE drop_tmp_table_stmt; + DEALLOCATE PREPARE drop_tmp_table_stmt; END \ No newline at end of file From c62db70d9a00ceea5c1b233a1ad1f543290d4a1e Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Thu, 21 Jul 2022 10:14:16 +0900 Subject: [PATCH 11/12] =?UTF-8?q?fix:=E3=83=88=E3=83=A9=E3=83=B3=E3=82=B6?= =?UTF-8?q?=E3=82=AF=E3=82=B7=E3=83=A7=E3=83=B3=E5=87=A6=E7=90=86=E3=81=AE?= =?UTF-8?q?=E8=BF=BD=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 | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rds_mysql/stored_procedure/crm_history.sql b/rds_mysql/stored_procedure/crm_history.sql index f361a850..7573bf9b 100644 --- a/rds_mysql/stored_procedure/crm_history.sql +++ b/rds_mysql/stored_procedure/crm_history.sql @@ -10,10 +10,13 @@ BEGIN @error_state = RETURNED_SQLSTATE, @error_msg = MESSAGE_TEXT; EXECUTE drop_tmp_table_stmt; DEALLOCATE PREPARE drop_tmp_table_stmt; + ROLLBACK; SIGNAL SQLSTATE '45000' SET MYSQL_ERRNO = @error_state, MESSAGE_TEXT = @error_msg; END; + START TRANSACTION; + -- ②-3で利用する一時テーブル削除のSQLを準備 -- 例外処理でも利用するため先に記述 SET @drop_tmp_table = ' @@ -85,4 +88,6 @@ BEGIN EXECUTE drop_tmp_table_stmt; DEALLOCATE PREPARE drop_tmp_table_stmt; + COMMIT; + END \ No newline at end of file From 805dfb26de33e64c40b5301abbb442b329b961c8 Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Thu, 21 Jul 2022 13:30:26 +0900 Subject: [PATCH 12/12] =?UTF-8?q?refactor:=E4=B8=A6=E5=88=97=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=81=A7=E3=81=8D=E3=81=AA=E3=81=84=E3=82=B3=E3=83=A1?= =?UTF-8?q?=E3=83=B3=E3=83=88=E8=BF=BD=E5=8A=A0=E3=80=81=E4=BE=8B=E5=A4=96?= =?UTF-8?q?=E3=81=A7=E4=BD=BF=E7=94=A8=E3=81=99=E3=82=8B=E3=83=A6=E3=83=BC?= =?UTF-8?q?=E3=82=B6=E5=A4=89=E6=95=B0=E3=81=AE=E5=88=9D=E6=9C=9F=E5=8C=96?= =?UTF-8?q?=E3=80=81$$=E7=B5=82=E7=AB=AF=E6=96=87=E5=AD=97=E3=81=AE?= =?UTF-8?q?=E8=BF=BD=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;