Skip to content

Commit

Permalink
[0.3.10] 新增 SQLTable 用于快速创建与该表相关的操作。
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Apr 11, 2022
1 parent 3fbc58a commit 6ba58b5
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easysql-parent</artifactId>
<version>0.3.9</version>
<version>0.3.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
128 changes: 128 additions & 0 deletions api/src/main/java/cc/carm/lib/easysql/api/SQLTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package cc.carm.lib.easysql.api;

import cc.carm.lib.easysql.api.action.PreparedSQLUpdateAction;
import cc.carm.lib.easysql.api.action.PreparedSQLUpdateBatchAction;
import cc.carm.lib.easysql.api.builder.*;
import cc.carm.lib.easysql.api.function.SQLHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.sql.SQLException;

/**
* SQLTable 基于 {@link TableCreateBuilder} 构建表,用于快速创建与该表相关的操作。
* <ul>
* <li>1. 调用 {@link SQLTable#of(String, String[])} 方法创建一个 SQLTable 对象;</li>
* <li>2. 在应用初始化阶段调用 {@link SQLTable#create(SQLManager)} 方法初始化 SQLTable 对象;</li>
* <li>3. 获取已创建的{@link SQLTable} 实例,直接调用对应方法进行关于表的相关操作。</li>
* </ul>
*
* @author CarmJos
* @since 0.3.10
*/
public abstract class SQLTable {

public static @NotNull SQLTable of(@NotNull String tableName, @Nullable SQLHandler<TableCreateBuilder> table) {
return new SQLTable(tableName) {
@Override
public int create(SQLManager sqlManager) throws SQLException {
if (this.manager == null) this.manager = sqlManager;
TableCreateBuilder tableBuilder = sqlManager.createTable(getTableName());
if (table != null) table.accept(tableBuilder);
return tableBuilder.build().execute();
}
};
}

public static @NotNull SQLTable of(@NotNull String tableName, @NotNull String[] columns) {
return of(tableName, columns, null);
}

public static @NotNull SQLTable of(@NotNull String tableName,
@NotNull String[] columns, @Nullable String tableSettings) {
return of(tableName, builder -> {
builder.setColumns(columns);
if (tableSettings != null) builder.setTableSettings(tableSettings);
});
}

private final @NotNull String tableName;

protected SQLManager manager;

/**
* 请调用 {@link SQLTable} 下的静态方法进行对象的初始化。
*
* @param tableName 该表的名称
*/
private SQLTable(@NotNull String tableName) {
this.tableName = tableName;
}

public @NotNull String getTableName() {
return tableName;
}

public abstract int create(SQLManager sqlManager) throws SQLException;

public @NotNull TableQueryBuilder createQuery(@NotNull SQLManager sqlManager) {
return sqlManager.createQuery().inTable(getTableName());
}

public @NotNull TableQueryBuilder createQuery() {
return createQuery(this.manager);
}

public @NotNull DeleteBuilder createDelete() {
return createDelete(this.manager);
}

public @NotNull DeleteBuilder createDelete(@NotNull SQLManager sqlManager) {
return sqlManager.createDelete(getTableName());
}

public @NotNull UpdateBuilder createUpdate() {
return createUpdate(this.manager);
}

public @NotNull UpdateBuilder createUpdate(@NotNull SQLManager sqlManager) {
return sqlManager.createUpdate(getTableName());
}


public @NotNull InsertBuilder<PreparedSQLUpdateAction> createInsert() {
return createInsert(this.manager);
}

public @NotNull InsertBuilder<PreparedSQLUpdateAction> createInsert(@NotNull SQLManager sqlManager) {
return sqlManager.createInsert(getTableName());
}


public @NotNull InsertBuilder<PreparedSQLUpdateBatchAction> createInsertBatch() {
return createInsertBatch(this.manager);
}

public @NotNull InsertBuilder<PreparedSQLUpdateBatchAction> createInsertBatch(@NotNull SQLManager sqlManager) {
return sqlManager.createInsertBatch(getTableName());
}


public @NotNull ReplaceBuilder<PreparedSQLUpdateAction> createReplace() {
return createReplace(this.manager);
}

public @NotNull ReplaceBuilder<PreparedSQLUpdateAction> createReplace(@NotNull SQLManager sqlManager) {
return sqlManager.createReplace(getTableName());
}


public @NotNull ReplaceBuilder<PreparedSQLUpdateBatchAction> createReplaceBatch() {
return createReplaceBatch(this.manager);
}

public @NotNull ReplaceBuilder<PreparedSQLUpdateBatchAction> createReplaceBatch(@NotNull SQLManager sqlManager) {
return sqlManager.createReplaceBatch(getTableName());
}

}
13 changes: 13 additions & 0 deletions api/src/main/java/cc/carm/lib/easysql/api/util/UUIDUtil.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
package cc.carm.lib.easysql.api.util;

import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UUIDUtil {

private static final Pattern COMPILE = Pattern.compile("-", Pattern.LITERAL);

public static UUID random() {
return UUID.randomUUID();
}

public static String toString(UUID uuid, boolean withDash) {
if (withDash) return uuid.toString();
else return COMPILE.matcher(uuid.toString()).replaceAll(Matcher.quoteReplacement(""));
}

public static UUID toUUID(String s) {
if (s.length() == 36) {
return UUID.fromString(s);
Expand Down
2 changes: 1 addition & 1 deletion example/demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.3.9</version>
<version>0.3.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
28 changes: 28 additions & 0 deletions example/demo/src/main/java/EasySQLDemo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.lib.easysql.api.SQLQuery;
import cc.carm.lib.easysql.api.SQLTable;
import cc.carm.lib.easysql.api.enums.ForeignKeyRule;
import cc.carm.lib.easysql.api.enums.IndexType;
import cc.carm.lib.easysql.api.enums.NumberType;
Expand Down Expand Up @@ -43,6 +44,33 @@ public void createTable(SQLManager sqlManager) {
.build().execute(null /* 不处理错误 */);
}

public void useSQLTable(SQLManager sqlManager) {
SQLTable tags = SQLTable.of("servers", table -> {
table.addAutoIncrementColumn("id", true);
table.addColumn("user", "INT UNSIGNED NOT NULL");
table.addColumn("content", "TEXT NOT NULL");
table.addColumn("time", "DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP");

table.addForeignKey(
"user", "fk_user_tags",
"users", "id",
ForeignKeyRule.CASCADE, ForeignKeyRule.CASCADE
);

});

try {
tags.create(sqlManager);
} catch (SQLException e) {
e.printStackTrace();
}

tags.createQuery().addCondition("id", 5).build()
.executeAsync(success -> {
System.out.println("success!");
});
}

public void alertTable(SQLManager sqlManager) {
// 同步更新表
sqlManager.alterTable("users")
Expand Down
2 changes: 1 addition & 1 deletion impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.3.9</version>
<version>0.3.10</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easysql-parent</artifactId>
<packaging>pom</packaging>
<version>0.3.9</version>
<version>0.3.10</version>

<modules>
<module>api</module>
Expand Down
2 changes: 1 addition & 1 deletion with-pool/beecp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.3.9</version>
<version>0.3.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion with-pool/hikaricp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>easysql-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>0.3.9</version>
<version>0.3.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down

0 comments on commit 6ba58b5

Please sign in to comment.