From 001d0f5e1c4a3a9ecd2fd88559d29d6b4498c459 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 28 Sep 2023 22:09:17 +0800 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20=E6=8A=BD=E8=B1=A1=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=94=B6=E9=9B=86=E5=99=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/audit/AbstractMessageCollector.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/AbstractMessageCollector.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/AbstractMessageCollector.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/AbstractMessageCollector.java new file mode 100644 index 00000000..3267c7d7 --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/AbstractMessageCollector.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed 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 com.mybatisflex.core.audit; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * 抽象消息收集器。 + * + * @author 王帅 + * @since 2023-09-28 + */ +public abstract class AbstractMessageCollector implements MessageCollector { + + private final MessageReporter messageSender; + private final List messages = Collections.synchronizedList(new ArrayList<>()); + private final ReentrantReadWriteLock rrwLock = new ReentrantReadWriteLock(); + + protected AbstractMessageCollector(MessageReporter messageSender) { + this.messageSender = messageSender; + } + + @Override + public void collect(AuditMessage message) { + try { + rrwLock.readLock().lock(); + messages.add(message); + } finally { + rrwLock.readLock().unlock(); + } + } + + protected void doSendMessages() { + if (messages.isEmpty()) { + return; + } + List sendMessages; + try { + rrwLock.writeLock().lock(); + sendMessages = new ArrayList<>(messages); + messages.clear(); + } finally { + rrwLock.writeLock().unlock(); + } + messageSender.sendMessages(sendMessages); + } + + public void release() { + doSendMessages(); + } + + protected List getMessages() { + return messages; + } + + protected MessageReporter getMessageSender() { + return messageSender; + } + +} From 5bcf62d56c161d9750c8619b007c2e0b26da4c54 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 28 Sep 2023 22:09:30 +0800 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20=E7=BB=A7=E6=89=BF=E6=8A=BD?= =?UTF-8?q?=E8=B1=A1=E6=B6=88=E6=81=AF=E6=94=B6=E9=9B=86=E5=99=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/audit/ScheduledMessageCollector.java | 44 ++----------------- 1 file changed, 4 insertions(+), 40 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/ScheduledMessageCollector.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/ScheduledMessageCollector.java index 81d8eaba..0e1fb50b 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/ScheduledMessageCollector.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/ScheduledMessageCollector.java @@ -15,24 +15,16 @@ */ package com.mybatisflex.core.audit; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import java.util.concurrent.locks.ReentrantReadWriteLock; /** * 默认的审计消息收集器,其收集消息后,定时通过消息发送器{@link MessageReporter}把消息发送过去 */ -public class ScheduledMessageCollector implements MessageCollector, Runnable { +public class ScheduledMessageCollector extends AbstractMessageCollector { private final ScheduledExecutorService scheduler; - private final MessageReporter messageSender; - - private final List messages = Collections.synchronizedList(new ArrayList<>()); - private final ReentrantReadWriteLock rrwLock = new ReentrantReadWriteLock(); public ScheduledMessageCollector() { this(10, new ConsoleMessageReporter()); @@ -40,45 +32,17 @@ public class ScheduledMessageCollector implements MessageCollector, Runnable { public ScheduledMessageCollector(long period, MessageReporter messageSender) { - this.messageSender = messageSender; + super(messageSender); this.scheduler = Executors.newSingleThreadScheduledExecutor(runnable -> { Thread thread = new Thread(runnable, "ScheduledMessageCollector"); thread.setDaemon(true); return thread; }); - this.scheduler.scheduleAtFixedRate(this, period, period, TimeUnit.SECONDS); - } - - - @Override - public void collect(AuditMessage message) { - try { - rrwLock.readLock().lock(); - messages.add(message); - } finally { - rrwLock.readLock().unlock(); - } - } - - - @Override - public void run() { - if (messages.isEmpty()) { - return; - } - List sendMessages; - try { - rrwLock.writeLock().lock(); - sendMessages = new ArrayList<>(messages); - messages.clear(); - } finally { - rrwLock.writeLock().unlock(); - } - messageSender.sendMessages(sendMessages); + this.scheduler.scheduleAtFixedRate(this::doSendMessages, period, period, TimeUnit.SECONDS); } public void release() { - run(); //clear the messages + doSendMessages(); //clear the messages scheduler.shutdown(); } From d03367e8b676639a4c216b9356130ccc645e6389 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 28 Sep 2023 22:12:41 +0800 Subject: [PATCH 3/6] =?UTF-8?q?feat:=20=E8=AE=A1=E6=95=B0=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=94=B6=E9=9B=86=E5=99=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/audit/CountableMessageCollector.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/CountableMessageCollector.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/CountableMessageCollector.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/CountableMessageCollector.java new file mode 100644 index 00000000..404a62e3 --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/audit/CountableMessageCollector.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed 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 com.mybatisflex.core.audit; + +/** + * 计数消息收集器,当消息达到指定数量时发送消息。 + * + * @author 王帅 + * @since 2023-09-28 + */ +public class CountableMessageCollector extends AbstractMessageCollector { + + private final int count; + + public CountableMessageCollector() { + this(1000, new ConsoleMessageReporter()); + } + + public CountableMessageCollector(int count, MessageReporter messageSender) { + super(messageSender); + this.count = count; + } + + @Override + public void collect(AuditMessage message) { + super.collect(message); + if (getMessages().size() >= count) { + new Thread(this::doSendMessages).start(); + } + } + +} From aeabd8fc50ffc023788ad1efd6d2e65cdc1a920b Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sun, 1 Oct 2023 19:57:04 +0800 Subject: [PATCH 4/6] =?UTF-8?q?build:=20=E5=9C=A8=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=B8=AA=20SQL=20?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BE=9D=E8=B5=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mybatis-flex-core/pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mybatis-flex-core/pom.xml b/mybatis-flex-core/pom.xml index 00993f90..26c569e3 100644 --- a/mybatis-flex-core/pom.xml +++ b/mybatis-flex-core/pom.xml @@ -91,6 +91,12 @@ test + + com.github.vertical-blank + sql-formatter + 2.0.4 + test + From 8880033464c0e392595f8bfd29b030433e4a0517 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sun, 1 Oct 2023 19:58:21 +0800 Subject: [PATCH 5/6] =?UTF-8?q?feat:=20Lambda=20=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20allColumns=20=E5=92=8C=20defaultColumns=20?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/core/query/QueryMethods.java | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java index 4a8e760c..5dedca0d 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java @@ -15,10 +15,15 @@ */ package com.mybatisflex.core.query; +import com.mybatisflex.core.table.TableInfo; +import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.util.ArrayUtil; import com.mybatisflex.core.util.LambdaGetter; import com.mybatisflex.core.util.LambdaUtil; +import java.util.ArrayList; +import java.util.List; + import static com.mybatisflex.core.constant.FuncName.*; /** @@ -2439,10 +2444,35 @@ public class QueryMethods { } /** - * 构建所有列 + * 构建所有列。 */ - public static QueryColumn allColumns(){ - return column("*"); + public static QueryColumn allColumns() { + return column("*"); + } + + /** + * 构建所有列。 + */ + public static Iterable allColumns(Class... classes) { + List queryColumns = new ArrayList<>(classes.length); + for (Class aClass : classes) { + TableInfo tableInfo = TableInfoFactory.ofEntityClass(aClass); + QueryTable queryTable = new QueryTable(tableInfo.getSchema(), tableInfo.getTableName()); + queryColumns.add(new QueryColumn(queryTable, "*")); + } + return queryColumns; + } + + /** + * 构建默认列。 + */ + public static Iterable defaultColumns(Class... classes) { + List queryColumns = new ArrayList<>(); + for (Class aClass : classes) { + TableInfo tableInfo = TableInfoFactory.ofEntityClass(aClass); + queryColumns.addAll(tableInfo.getDefaultQueryColumn()); + } + return queryColumns; } // === IF 函数 === @@ -2584,7 +2614,7 @@ public class QueryMethods { * 分组值拼接 */ public static QueryColumn groupConcat(QueryColumn columnX) { - return new FunctionQueryColumn(GROUP_CONCAT,columnX); + return new FunctionQueryColumn(GROUP_CONCAT, columnX); } } From 3b90348adae1b71b7768b03cf8582656e8d3459f Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sun, 1 Oct 2023 19:58:41 +0800 Subject: [PATCH 6/6] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=20Lambda=20?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=20SQL=20=E5=88=97=E6=9E=84=E5=BB=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/coretest/LambdaSqlTest.java | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LambdaSqlTest.java diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LambdaSqlTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LambdaSqlTest.java new file mode 100644 index 00000000..5cad6b71 --- /dev/null +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LambdaSqlTest.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed 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 com.mybatisflex.coretest; + +import com.github.vertical_blank.sqlformatter.SqlFormatter; +import com.mybatisflex.core.query.QueryWrapper; +import org.junit.Test; + +import static com.mybatisflex.core.query.QueryMethods.allColumns; +import static com.mybatisflex.core.query.QueryMethods.defaultColumns; + +/** + * Lambda 构建 SQL 测试。 + * + * @author 王帅 + * @since 2023-10-01 + */ +public class LambdaSqlTest { + + public static void printSQL(QueryWrapper queryWrapper) { + System.out.println(SqlFormatter.format(queryWrapper.toSQL())); + } + + @Test + public void test01() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select() + .from(Account.class) + .join(Article.class).on(wrapper -> wrapper.where(Article::getAccountId).eq(Account::getId)) + .where(Account::getAge).ge(18); + + printSQL(queryWrapper); + } + + @Test + public void test02() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select(allColumns(Account.class, Article.class)) + .from(Account.class) + .join(Article.class).on(wrapper -> wrapper.where(Article::getAccountId).eq(Account::getId)) + .where(Account::getAge).ge(18); + + printSQL(queryWrapper); + } + + @Test + public void test03() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select(allColumns(Account.class, Article.class)) + .from(Account.class).as("ac") + .join(Article.class).as("ar").on(wrapper -> wrapper.where(Article::getAccountId).eq(Account::getId)) + .where(Account::getAge).ge(18); + + printSQL(queryWrapper); + } + + @Test + public void test04() { + @SuppressWarnings("unchecked") + QueryWrapper queryWrapper = QueryWrapper.create() + .select(defaultColumns(Account.class)) + .select(Article::getTitle, Article::getContent) + .from(Account.class).as("ac") + .join(Article.class).as("ar").on(wrapper -> wrapper.where(Article::getAccountId).eq(Account::getId)) + .where(Account::getAge).ge(18); + + printSQL(queryWrapper); + } + + @Test + public void test05() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select(defaultColumns(Account.class)) + .select(allColumns(Article.class)) + .from(Account.class).as("ac") + .join(Article.class).as("ar").on(wrapper -> wrapper.where(Article::getAccountId).eq(Account::getId)) + .where(Account::getAge).ge(18); + + printSQL(queryWrapper); + } + +}