初步实现mysql适配

This commit is contained in:
zhangyuheng 2024-06-20 00:49:49 +08:00
parent faadf05224
commit 45553fc280
3 changed files with 18 additions and 20 deletions

View File

@ -28,11 +28,7 @@ public class DatabaseManager {
connectionUrl = "jdbc:sqlite:" + plugin.getDataFolder() + "/" + name + ".db";
conn = DriverManager.getConnection(connectionUrl);
// 启用外键约束
try (ResultSet res = query("PRAGMA foreign_keys = ON;")) {
if (res == null) {
handleDatabaseError("启用外键约束失败", new Exception("ResultSet is null"), "PRAGMA foreign_keys = ON;");
}
}
query("PRAGMA foreign_keys = ON;");
} else if (type.equals(DatabaseType.MYSQL)) {
XLogger.info("正在连接到 MySQL 数据库");
Class.forName("com.mysql.cj.jdbc.Driver");

View File

@ -9,7 +9,7 @@ public enum FieldType {
DOUBLE(Double.class, "DOUBLE", "REAL", "DOUBLE PRECISION"),
FLOAT(Float.class, "FLOAT", "REAL", "REAL"),
BOOLEAN(Boolean.class, "TINYINT(1)", "INTEGER", "BOOLEAN"),
DATETIME(Timestamp.class, "DATETIME", "TEXT", "TIMESTAMP"),
DATETIME(Timestamp.class, "DATETIME", "TIMESTAMP", "TIMESTAMP"),
UUID(java.util.UUID.class, "VARCHAR(36)", "TEXT", "UUID"),
;

View File

@ -1,9 +1,6 @@
package cn.lunadeer.minecraftpluginutils.databse.syntax;
import cn.lunadeer.minecraftpluginutils.databse.DatabaseManager;
import cn.lunadeer.minecraftpluginutils.databse.DatabaseType;
import cn.lunadeer.minecraftpluginutils.databse.SqlSyntax;
import cn.lunadeer.minecraftpluginutils.databse.TableColumn;
import cn.lunadeer.minecraftpluginutils.databse.*;
import java.sql.ResultSet;
import java.util.ArrayList;
@ -85,22 +82,27 @@ public class CreateTable implements SqlSyntax {
case PGSQL -> {
sql.append(tableName).append(" (");
for (TableColumn tableColumn : tableColumns) {
sql.append(tableColumn.name).append(" ").append(tableColumn.type.getType(type));
if (tableColumn.isPrimary) {
sql.append(" PRIMARY KEY");
}
// Handle auto-increment
if (tableColumn.isAutoIncrement) {
sql.append(tableColumn.name).append(" ");
sql.append(" SERIAL");
}
if (tableColumn.isNotNull) {
sql.append(" NOT NULL");
} else {
sql.append(tableColumn.name).append(" ").append(tableColumn.type.getType(type));
// Handle other attributes for non-auto-increment columns
if (tableColumn.isPrimary) {
sql.append(" PRIMARY KEY");
}
if (tableColumn.isNotNull) {
sql.append(" NOT NULL");
}
if (tableColumn.defaultValue != null) {
sql.append(" DEFAULT ").append(tableColumn.defaultValue);
}
}
if (tableColumn.isUnique) {
sql.append(" UNIQUE");
}
if (!tableColumn.isAutoIncrement && tableColumn.defaultValue != null) {
sql.append(" DEFAULT ").append(tableColumn.defaultValue);
}
sql.append(", ");
}
for (List<TableColumn> unique : uniqueFields) {