From 592a9d2dbd4799e589d99160d20b90b231a60e76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Sat, 26 Oct 2019 15:44:56 +0800 Subject: [PATCH] + update sql api --- TabooLib R2.iml | 7 - build.gradle | 2 +- .../taboolib/module/db/sql/SQLTable.java | 124 +++++++++++++----- .../taboolib/module/db/sql/query/Order.java | 24 ++++ .../module/db/sql/query/QueryDelete.java | 54 ++++++++ .../module/db/sql/query/QueryInsert.java | 53 ++++++++ .../module/db/sql/query/QuerySelect.java | 101 ++++++++++++++ .../module/db/sql/query/QueryUpdate.java | 69 ++++++++++ .../module/db/sql/query/RunnableQuery.java | 12 +- .../taboolib/module/db/sql/query/Where.java | 99 ++++++++++++++ 10 files changed, 505 insertions(+), 40 deletions(-) create mode 100644 src/main/scala/io/izzel/taboolib/module/db/sql/query/Order.java create mode 100644 src/main/scala/io/izzel/taboolib/module/db/sql/query/QueryDelete.java create mode 100644 src/main/scala/io/izzel/taboolib/module/db/sql/query/QueryInsert.java create mode 100644 src/main/scala/io/izzel/taboolib/module/db/sql/query/QuerySelect.java create mode 100644 src/main/scala/io/izzel/taboolib/module/db/sql/query/QueryUpdate.java create mode 100644 src/main/scala/io/izzel/taboolib/module/db/sql/query/Where.java diff --git a/TabooLib R2.iml b/TabooLib R2.iml index 305d1b0..c709ad4 100644 --- a/TabooLib R2.iml +++ b/TabooLib R2.iml @@ -1,12 +1,5 @@ - - - - - - diff --git a/build.gradle b/build.gradle index b3d7bda..99838c6 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group = 'me.skymc' -version = '5.09' +version = '5.1' sourceCompatibility = 1.8 targetCompatibility = 1.8 diff --git a/src/main/scala/io/izzel/taboolib/module/db/sql/SQLTable.java b/src/main/scala/io/izzel/taboolib/module/db/sql/SQLTable.java index 870f489..0bcbd9d 100644 --- a/src/main/scala/io/izzel/taboolib/module/db/sql/SQLTable.java +++ b/src/main/scala/io/izzel/taboolib/module/db/sql/SQLTable.java @@ -1,11 +1,13 @@ package io.izzel.taboolib.module.db.sql; +import com.google.common.base.Enums; import io.izzel.taboolib.module.db.IColumn; -import io.izzel.taboolib.module.db.sql.query.RunnableQuery; -import io.izzel.taboolib.module.db.sql.query.RunnableUpdate; +import io.izzel.taboolib.module.db.sql.query.*; import io.izzel.taboolib.util.ArrayUtil; import io.izzel.taboolib.util.Strings; +import javax.sql.DataSource; + /** * @Author sky * @Since 2018-05-14 19:07 @@ -34,58 +36,118 @@ public class SQLTable { return this; } - @Deprecated - public SQLTable addColumn(SQLColumn sqlColumn) { - columns = columns == null ? new SQLColumn[] {sqlColumn} : ArrayUtil.arrayAppend(columns, sqlColumn); + /** + * 5.1 update + */ + public SQLTable column(String... column) { + for (String c : column) { + if (c.equalsIgnoreCase("$primary_key_id") || c.equalsIgnoreCase("$id")) { + column(SQLColumn.PRIMARY_KEY_ID); + } else { + String[] v = c.split(":"); + if (v.length == 2) { + column(new SQLColumn(Enums.getIfPresent(SQLColumnType.class, v[0].toUpperCase()).or(SQLColumnType.TEXT), v[1])); + } else { + column(new SQLColumn(SQLColumnType.TEXT, "error_" + c)); + } + } + } return this; } - public String createQuery() { - StringBuilder builder = new StringBuilder(); - java.util.Arrays.stream(columns).forEach(sqlColumn -> builder.append(sqlColumn.convertToCommand()).append(", ")); - return Strings.replaceWithOrder("create table if not exists `{0}` ({1})", tableName, builder.substring(0, builder.length() - 2)); + public void create(DataSource dataSource) { + executeUpdate(createQuery()).dataSource(dataSource).run(); } - public String deleteQuery() { - return Strings.replaceWithOrder("drop table if exists `{0}`", tableName); + public QuerySelect select() { + return new QuerySelect().row("*").table(tableName); } - public String cleanQuery() { - return Strings.replaceWithOrder("delete from `{0}`", tableName); + public QuerySelect select(String... row) { + return new QuerySelect().row(row).table(tableName); } - public String truncateQuery() { - return Strings.replaceWithOrder("truncate table `{0}`", tableName); + public QuerySelect select(Where... where) { + return new QuerySelect().where(where).table(tableName); } + public QueryUpdate update() { + return new QueryUpdate().table(tableName); + } + + public QueryUpdate update(Where... where) { + return new QueryUpdate().where(where).table(tableName); + } + + public QueryInsert insert() { + return new QueryInsert().table(tableName); + } + + public QueryInsert insert(Object... value) { + return new QueryInsert().table(tableName).value(value); + } + + public QueryDelete delete() { + return new QueryDelete().table(tableName); + } + + public QueryDelete delete(Where... where) { + return new QueryDelete().table(tableName).where(where); + } + + /** + * 4.x version + */ public RunnableQuery executeQuery(String query) { return new RunnableQuery(query); } - public RunnableQuery executeSelect() { - return executeQuery("select * from " + tableName); - } - - public RunnableQuery executeSelect(String queryWhere) { - return executeQuery("select * from " + tableName + " where " + queryWhere); - } - - public RunnableUpdate executeInsert(String queryValues) { - return executeUpdate("insert into " + tableName + " values(" + queryValues + ")"); - } - - public RunnableUpdate executeUpdateIfDuplicate(String queryValues, String queryUpdate) { - return executeUpdate("insert into " + tableName + " values(" + queryValues + ") on duplicate key update " + queryUpdate); - } - public RunnableUpdate executeUpdate(String query) { return new RunnableUpdate(query); } + @Deprecated + public String deleteQuery() { + return Strings.replaceWithOrder("drop table if exists `{0}`", tableName); + } + + @Deprecated + public String cleanQuery() { + return Strings.replaceWithOrder("delete from `{0}`", tableName); + } + + @Deprecated + public String truncateQuery() { + return Strings.replaceWithOrder("truncate table `{0}`", tableName); + } + + @Deprecated + public RunnableQuery executeSelect() { + return executeQuery("select * from " + tableName); + } + + @Deprecated + public RunnableQuery executeSelect(String queryWhere) { + return executeQuery("select * from " + tableName + " where " + queryWhere); + } + + @Deprecated + public RunnableUpdate executeInsert(String queryValues) { + return executeUpdate("insert into " + tableName + " values(" + queryValues + ")"); + } + + @Deprecated public RunnableUpdate executeUpdate(String update, String where) { return executeUpdate("update " + tableName + " set " + update + " where " + where); } + @Deprecated + public String createQuery() { + StringBuilder builder = new StringBuilder(); + java.util.Arrays.stream(columns).forEach(sqlColumn -> builder.append(sqlColumn.convertToCommand()).append(", ")); + return Strings.replaceWithOrder("create table if not exists `{0}` ({1})", tableName, builder.substring(0, builder.length() - 2)); + } + // ********************************* // // Getter and Setter diff --git a/src/main/scala/io/izzel/taboolib/module/db/sql/query/Order.java b/src/main/scala/io/izzel/taboolib/module/db/sql/query/Order.java new file mode 100644 index 0000000..b9db5e7 --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/module/db/sql/query/Order.java @@ -0,0 +1,24 @@ +package io.izzel.taboolib.module.db.sql.query; + +/** + * @Author sky + * @Since 2019-10-26 14:02 + */ +public class Order { + + private String row; + private boolean desc; + + public Order(String row) { + this.row = row; + } + + public Order(String row, boolean desc) { + this.row = row; + this.desc = desc; + } + + public String toQuery() { + return row + " " + (desc ? "desc" : "asc"); + } +} diff --git a/src/main/scala/io/izzel/taboolib/module/db/sql/query/QueryDelete.java b/src/main/scala/io/izzel/taboolib/module/db/sql/query/QueryDelete.java new file mode 100644 index 0000000..5ed81f5 --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/module/db/sql/query/QueryDelete.java @@ -0,0 +1,54 @@ +package io.izzel.taboolib.module.db.sql.query; + +import com.google.common.collect.Lists; + +import javax.sql.DataSource; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +/** + * @Author sky + * @Since 2019-10-26 13:34 + */ +public class QueryDelete { + + private String tableName; + private List where = Lists.newArrayList(); + + public QueryDelete table(String tableName) { + this.tableName = tableName; + return this; + } + + public QueryDelete where(Where where) { + this.where.add(where); + return this; + } + + public QueryDelete where(Where... where) { + Collections.addAll(this.where, where); + return this; + } + + public RunnableUpdate to(DataSource dataSource) { + return new RunnableUpdate(toQuery()).dataSource(dataSource).statement(s -> { + int index = 1; + for (Where w : where) { + index = w.toStatement(s, index); + } + }); + } + + public String toQuery() { + StringBuilder builder = new StringBuilder(); + builder.append("delete from ").append(tableName); + builder.append(" "); + if (!where.isEmpty()) { + builder.append("where "); + builder.append(where.stream().map(Where::toQuery).collect(Collectors.joining(" and "))); + builder.append(" "); + } + return builder.toString().trim(); + } +} diff --git a/src/main/scala/io/izzel/taboolib/module/db/sql/query/QueryInsert.java b/src/main/scala/io/izzel/taboolib/module/db/sql/query/QueryInsert.java new file mode 100644 index 0000000..5bdc06d --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/module/db/sql/query/QueryInsert.java @@ -0,0 +1,53 @@ +package io.izzel.taboolib.module.db.sql.query; + +import com.google.common.collect.Lists; + +import javax.sql.DataSource; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +/** + * @Author sky + * @Since 2019-10-26 13:34 + */ +public class QueryInsert { + + private String tableName; + private List value = Lists.newArrayList(); + + public QueryInsert table(String tableName) { + this.tableName = tableName; + return this; + } + + public QueryInsert value(Object... value) { + Collections.addAll(this.value, value); + return this; + } + + public void run(DataSource dataSource) { + to(dataSource).run(); + } + + public RunnableUpdate to(DataSource dataSource) { + return new RunnableUpdate(toQuery()).dataSource(dataSource).statement(s -> { + int index = 1; + for (Object v : value) { + s.setObject(index++, v); + } + }); + } + + public String toQuery() { + StringBuilder builder = new StringBuilder(); + builder.append("insert into ").append(tableName); + builder.append(" "); + builder.append("values ("); + if (!value.isEmpty()) { + builder.append(value.stream().map(i -> "?").collect(Collectors.joining(", "))); + builder.append(" "); + } + return builder.append(")").toString().trim(); + } +} diff --git a/src/main/scala/io/izzel/taboolib/module/db/sql/query/QuerySelect.java b/src/main/scala/io/izzel/taboolib/module/db/sql/query/QuerySelect.java new file mode 100644 index 0000000..a6d6802 --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/module/db/sql/query/QuerySelect.java @@ -0,0 +1,101 @@ +package io.izzel.taboolib.module.db.sql.query; + +import com.google.common.collect.Lists; +import io.izzel.taboolib.util.ArrayUtil; + +import javax.sql.DataSource; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +/** + * @Author sky + * @Since 2019-10-26 13:34 + */ +public class QuerySelect { + + private List rowName = Lists.newArrayList(); + private String tableName; + private String distinct; + private List where = Lists.newArrayList(); + private List order = Lists.newArrayList(); + private int limit = -1; + + public QuerySelect row(String... row) { + this.rowName = ArrayUtil.asList(row); + return this; + } + + public QuerySelect table(String tableName) { + this.tableName = tableName; + return this; + } + + public QuerySelect distinct(String row) { + this.distinct = row; + return this; + } + + public QuerySelect where(Where where) { + this.where.add(where); + return this; + } + + public QuerySelect where(Where... where) { + Collections.addAll(this.where, where); + return this; + } + + public QuerySelect order(Order order) { + this.order.add(order); + return this; + } + + public QuerySelect limit(int limit) { + this.limit = limit; + return this; + } + + public boolean find(DataSource dataSource) { + return to(dataSource).find(); + } + + public RunnableQuery to(DataSource dataSource) { + return new RunnableQuery(toQuery()).dataSource(dataSource).statement(s -> { + int index = 1; + for (Where w : where) { + index = w.toStatement(s, index); + } + }); + } + + public String toQuery() { + StringBuilder builder = new StringBuilder(); + builder.append("select"); + builder.append(" "); + if (!rowName.isEmpty()) { + builder.append(String.join(", ", rowName)); + } else if (distinct != null) { + builder.append("distinct ").append(distinct); + } else { + builder.append("*"); + } + builder.append(" "); + builder.append("from ").append(tableName); + builder.append(" "); + if (!where.isEmpty()) { + builder.append("where "); + builder.append(where.stream().map(Where::toQuery).collect(Collectors.joining(" and "))); + builder.append(" "); + } + if (!order.isEmpty()) { + builder.append("order by "); + builder.append(order.stream().map(Order::toQuery).collect(Collectors.joining(", "))); + builder.append(" "); + } + if (limit > -1) { + builder.append("limit ").append(limit); + } + return builder.toString().trim(); + } +} diff --git a/src/main/scala/io/izzel/taboolib/module/db/sql/query/QueryUpdate.java b/src/main/scala/io/izzel/taboolib/module/db/sql/query/QueryUpdate.java new file mode 100644 index 0000000..c615c46 --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/module/db/sql/query/QueryUpdate.java @@ -0,0 +1,69 @@ +package io.izzel.taboolib.module.db.sql.query; + +import com.google.common.collect.Lists; +import io.izzel.taboolib.util.KV; + +import javax.sql.DataSource; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +/** + * @Author sky + * @Since 2019-10-26 13:34 + */ +public class QueryUpdate { + + private String tableName; + private List where = Lists.newArrayList(); + private List> set = Lists.newArrayList(); + + public QueryUpdate table(String tableName) { + this.tableName = tableName; + return this; + } + + public QueryUpdate set(String row, Object value) { + this.set.add(new KV<>(row, value)); + return this; + } + + public QueryUpdate where(Where where) { + this.where.add(where); + return this; + } + + public QueryUpdate where(Where... where) { + Collections.addAll(this.where, where); + return this; + } + + public RunnableUpdate to(DataSource dataSource) { + return new RunnableUpdate(toQuery()).dataSource(dataSource).statement(s -> { + int index = 1; + for (KV pair : set) { + s.setObject(index++, pair.getValue()); + } + for (Where w : where) { + index = w.toStatement(s, index); + } + }); + } + + public String toQuery() { + StringBuilder builder = new StringBuilder(); + builder.append("update ").append(tableName); + builder.append(" "); + if (!set.isEmpty()) { + builder.append("set "); + builder.append(set.stream().map(s -> s.getKey() + " = ?").collect(Collectors.joining(", "))); + builder.append(" "); + } + if (!where.isEmpty()) { + builder.append("where "); + builder.append(where.stream().map(Where::toQuery).collect(Collectors.joining(" and "))); + builder.append(" "); + } + return builder.toString().trim(); + } +} diff --git a/src/main/scala/io/izzel/taboolib/module/db/sql/query/RunnableQuery.java b/src/main/scala/io/izzel/taboolib/module/db/sql/query/RunnableQuery.java index 7d4fecc..81c696b 100644 --- a/src/main/scala/io/izzel/taboolib/module/db/sql/query/RunnableQuery.java +++ b/src/main/scala/io/izzel/taboolib/module/db/sql/query/RunnableQuery.java @@ -1,7 +1,7 @@ package io.izzel.taboolib.module.db.sql.query; -import io.izzel.taboolib.module.locale.logger.TLogger; import io.izzel.taboolib.module.db.sql.SQLExecutor; +import io.izzel.taboolib.module.locale.logger.TLogger; import javax.sql.DataSource; import java.sql.Connection; @@ -68,6 +68,11 @@ public class RunnableQuery { return object == null ? def == null ? null : (T) def : (T) object; } + public T run(Object def, Class translate) { + Object object = run(def); + return object == null ? def == null ? null : (T) def : (T) object; + } + public Object run() { return run(null); } @@ -108,6 +113,11 @@ public class RunnableQuery { return def; } + public boolean find() { + this.resultNext = r -> true; + return run(false, false); + } + private void printException(Exception e) { TLogger.getGlobalLogger().error("An exception occurred in the database. (" + query + ")"); TLogger.getGlobalLogger().error("Reason: " + e.toString()); diff --git a/src/main/scala/io/izzel/taboolib/module/db/sql/query/Where.java b/src/main/scala/io/izzel/taboolib/module/db/sql/query/Where.java new file mode 100644 index 0000000..41a657a --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/module/db/sql/query/Where.java @@ -0,0 +1,99 @@ +package io.izzel.taboolib.module.db.sql.query; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.stream.Collectors; + +public class Where { + + private String row; + private String symbol; + private Object value; + private Object between; + private Object[] in; + + Where(String row, String symbol, Object value) { + this.row = row; + this.symbol = symbol; + this.value = value; + } + + public Where(String row, String symbol, Object value, Object between) { + this.row = row; + this.symbol = symbol; + this.value = value; + this.between = between; + } + + public Where(String row, Object[] in) { + this.row = row; + this.in = in; + } + + public static Where is(String row, Object value) { + return new Where(row, "=", value); + } + + public static Where isNot(String row, Object value) { + return new Where(row, "<>", value); + } + + public static Where more(String row, Object value) { + return new Where(row, ">", value); + } + + public static Where less(String row, Object value) { + return new Where(row, "<", value); + } + + public static Where moreEqual(String row, Object value) { + return new Where(row, ">=", value); + } + + public static Where lessEqual(String row, Object value) { + return new Where(row, "<=", value); + } + + public static Where like(String row, Object value) { + return new Where(row, "like", value); + } + + public static Where between(String row, Object value1, Object value2) { + return new Where(row, "between", value1, value2); + } + + public static Where betweenNot(String row, Object value1, Object value2) { + return new Where(row, "not between", value1, value2); + } + + public static Where in(String row, Object... value3) { + return new Where(row, value3); + } + + public int toStatement(PreparedStatement statement, int index) throws SQLException { + if (between == null) { + statement.setObject(index, value); + return index + 1; + } else if (in == null) { + statement.setObject(index, value); + statement.setObject(index + 1, between); + return index + 2; + } else { + for (int i = 0; i < in.length; i++) { + statement.setObject(index + i, in[i]); + } + return index + in.length; + } + } + + public String toQuery() { + if (between == null) { + return row + " " + symbol + " ?"; + } else if (in == null) { + return row + " " + symbol + " ? and ?"; + } else { + return row + " in (" + Arrays.stream(in).map(i -> "?").collect(Collectors.joining(", ")) + ")"; + } + } +} \ No newline at end of file