This is an automated email from the ASF dual-hosted git repository.

xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new b8ce54d  [ISSUE #3019] Remove trigger in the pg sql script. (#3020)
b8ce54d is described below

commit b8ce54d22ca1d0fb3e6a6b4748a76812b57137eb
Author: Shawn Jim <[email protected]>
AuthorDate: Thu Mar 10 23:25:57 2022 +0800

    [ISSUE #3019] Remove trigger in the pg sql script. (#3020)
---
 .../apache/shenyu/admin/config/MapperConfig.java   | 12 ++++
 .../interceptor/PostgreSqlUpdateInterceptor.java   | 76 ++++++++++++++++++++++
 .../src/main/resources/sql-script/pg/schema.sql    | 22 +------
 .../apache/shenyu/common/utils/ReflectUtils.java   |  1 -
 4 files changed, 90 insertions(+), 21 deletions(-)

diff --git 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/MapperConfig.java 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/MapperConfig.java
index 151ab83..55a950d 100644
--- 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/MapperConfig.java
+++ 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/MapperConfig.java
@@ -19,6 +19,7 @@ package org.apache.shenyu.admin.config;
 
 import 
org.apache.shenyu.admin.mybatis.pg.interceptor.PostgreSQLPrepareInterceptor;
 import 
org.apache.shenyu.admin.mybatis.pg.interceptor.PostgreSQLQueryInterceptor;
+import 
org.apache.shenyu.admin.mybatis.pg.interceptor.PostgreSqlUpdateInterceptor;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
@@ -51,4 +52,15 @@ public class MapperConfig {
     public PostgreSQLPrepareInterceptor postgreSqlPrepareInterceptor() {
         return new PostgreSQLPrepareInterceptor();
     }
+
+    /**
+     * Add the plugin to the MyBatis plugin interceptor chain.
+     *
+     * @return {@linkplain PostgreSqlUpdateInterceptor}
+     */
+    @Bean
+    @ConditionalOnProperty(name = "shenyu.database.dialect", havingValue = 
"postgresql")
+    public PostgreSqlUpdateInterceptor postgreSqlUpdateInterceptor() {
+        return new PostgreSqlUpdateInterceptor();
+    }
 }
diff --git 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mybatis/pg/interceptor/PostgreSqlUpdateInterceptor.java
 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mybatis/pg/interceptor/PostgreSqlUpdateInterceptor.java
new file mode 100644
index 0000000..e76328f
--- /dev/null
+++ 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mybatis/pg/interceptor/PostgreSqlUpdateInterceptor.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.admin.mybatis.pg.interceptor;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.ibatis.executor.Executor;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.plugin.Interceptor;
+import org.apache.ibatis.plugin.Intercepts;
+import org.apache.ibatis.plugin.Invocation;
+import org.apache.ibatis.plugin.Plugin;
+import org.apache.ibatis.plugin.Signature;
+import org.apache.shenyu.common.utils.ReflectUtils;
+
+import java.lang.reflect.Field;
+import java.sql.Timestamp;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import java.util.Properties;
+
+/**
+ * The 'date_updated' field in the postgreSql library is not automatically 
updated.
+ * So this interceptor intercepts the UPDATE statement.
+ * Adds the current time to the 'date_updated' field at run time.
+ */
+@Intercepts({
+    @Signature(type = Executor.class, method = "update", args = 
{MappedStatement.class, Object.class})
+})
+public class PostgreSqlUpdateInterceptor implements Interceptor {
+
+    private static final List<String> AUTOMATIC_DATES = 
ImmutableList.of("dateUpdated");
+
+    @Override
+    public Object intercept(final Invocation invocation) throws Throwable {
+        Object[] args = invocation.getArgs();
+        MappedStatement ms = (MappedStatement) args[0];
+        Object parameter = args[1];
+        Executor executor = (Executor) invocation.getTarget();
+        for (Class<?> superClass = parameter.getClass(); superClass != 
Object.class; superClass = superClass.getSuperclass()) {
+            Arrays.stream(superClass.getDeclaredFields())
+                    .filter(f -> matchParam(parameter, f))
+                    .forEach(f -> ReflectUtils.setFieldValue(parameter, 
f.getName(), new Timestamp(System.currentTimeMillis())));
+        }
+        return executor.update(ms, parameter);
+    }
+
+    private boolean matchParam(final Object parameter, final Field f) {
+        return AUTOMATIC_DATES.contains(f.getName()) && 
Objects.isNull(ReflectUtils.getFieldValue(parameter, f));
+    }
+
+    @Override
+    public Object plugin(final Object target) {
+        return Plugin.wrap(target, this);
+    }
+
+    @Override
+    public void setProperties(final Properties properties) {
+
+    }
+}
diff --git a/shenyu-admin/src/main/resources/sql-script/pg/schema.sql 
b/shenyu-admin/src/main/resources/sql-script/pg/schema.sql
index ec11c15..ea0eff3 100644
--- a/shenyu-admin/src/main/resources/sql-script/pg/schema.sql
+++ b/shenyu-admin/src/main/resources/sql-script/pg/schema.sql
@@ -30,17 +30,8 @@ BEGIN
     PERFORM public.dblink_exec('CREATE DATABASE ' || _db || ' template 
template0;');
   END IF;
 
-       PERFORM public.dblink_connect('init_conn','host=localhost user=' || 
_user || ' password=' || _password || ' dbname=' ||_db);
-       PERFORM public.dblink_exec('init_conn', 'BEGIN');
-    PERFORM public.dblink_exec('init_conn','CREATE OR REPLACE FUNCTION 
update_timestamp() RETURNS TRIGGER AS
-                                          $$
-                                          BEGIN
-                                          NEW.date_updated = 
NOW()::TIMESTAMP(0);
-                                          RETURN NEW;
-                                          END
-                                          $$
-                                          language plpgsql;');
-       PERFORM public.dblink_exec('init_conn', 'COMMIT');
+    PERFORM public.dblink_connect('init_conn','host=localhost user=' || _user 
|| ' password=' || _password || ' dbname=' ||_db);
+
 
 -- ----------------------------------------
 -- create table app_auth if not exist ---
@@ -453,11 +444,6 @@ ELSE
          "type" "pg_catalog"."int2_ops" ASC NULLS LAST
        );');
 
-       PERFORM public.dblink_exec('init_conn',  ' CREATE TRIGGER 
plugin_handle_trigger
-                 BEFORE UPDATE ON plugin_handle
-                 FOR EACH ROW EXECUTE PROCEDURE update_timestamp()'
-    );
-
     ----------------------------
        -- Records of plugin_handle
        -- ----------------------------
@@ -958,10 +944,6 @@ ELSE
     PERFORM public.dblink_exec('init_conn',  'CREATE SEQUENCE 
shenyu_dict_ID_seq;      ');
        PERFORM public.dblink_exec('init_conn',  'ALTER SEQUENCE 
shenyu_dict_ID_seq OWNED BY shenyu_dict.ID;');
 
-       PERFORM public.dblink_exec('init_conn',  ' CREATE TRIGGER 
shenyu_dict_trigger
-                 BEFORE UPDATE ON shenyu_dict
-                 FOR EACH ROW EXECUTE PROCEDURE update_timestamp()');
-
        -- ----------------------------
        -- Records of shenyu_dict
        -- ----------------------------
diff --git 
a/shenyu-common/src/main/java/org/apache/shenyu/common/utils/ReflectUtils.java 
b/shenyu-common/src/main/java/org/apache/shenyu/common/utils/ReflectUtils.java
index 7a16eb0..38a96de 100644
--- 
a/shenyu-common/src/main/java/org/apache/shenyu/common/utils/ReflectUtils.java
+++ 
b/shenyu-common/src/main/java/org/apache/shenyu/common/utils/ReflectUtils.java
@@ -163,7 +163,6 @@ public class ReflectUtils {
             } catch (NoSuchFieldException e) {
                 // Field is not defined in the current class and continues to 
transition up
                 // new add
-                LOG.error("field is not defined in the current class and 
continues to transition up", e);
             }
         }
         return null;

Reply via email to