+ update sql api
This commit is contained in:
parent
e5f812d94c
commit
592a9d2dbd
@ -1,12 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module external.system.id="GRADLE" type="JAVA_MODULE" version="4">
|
||||
<component name="FacetManager">
|
||||
<facet type="android-gradle" name="Android-Gradle">
|
||||
<configuration>
|
||||
<option name="GRADLE_PROJECT_PATH" value=":" />
|
||||
</configuration>
|
||||
</facet>
|
||||
</component>
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
|
@ -6,7 +6,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = 'me.skymc'
|
||||
version = '5.09'
|
||||
version = '5.1'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
targetCompatibility = 1.8
|
||||
|
@ -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
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
@ -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> 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();
|
||||
}
|
||||
}
|
@ -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<Object> 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();
|
||||
}
|
||||
}
|
@ -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<String> rowName = Lists.newArrayList();
|
||||
private String tableName;
|
||||
private String distinct;
|
||||
private List<Where> where = Lists.newArrayList();
|
||||
private List<Order> 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();
|
||||
}
|
||||
}
|
@ -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> where = Lists.newArrayList();
|
||||
private List<KV<String, Object>> 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<String, Object> 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();
|
||||
}
|
||||
}
|
@ -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> T run(Object def, Class<? extends T> 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());
|
||||
|
@ -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(", ")) + ")";
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user