From 45553fc280aec06b7a8b145cce5eee2b9ce0ddf7 Mon Sep 17 00:00:00 2001 From: zhangyuheng Date: Thu, 20 Jun 2024 00:49:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=9E=E7=8E=B0mysql?= =?UTF-8?q?=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../databse/DatabaseManager.java | 6 +--- .../databse/FieldType.java | 2 +- .../databse/syntax/CreateTable.java | 30 ++++++++++--------- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/main/java/cn/lunadeer/minecraftpluginutils/databse/DatabaseManager.java b/src/main/java/cn/lunadeer/minecraftpluginutils/databse/DatabaseManager.java index 9ae4ef0..11fc1ed 100644 --- a/src/main/java/cn/lunadeer/minecraftpluginutils/databse/DatabaseManager.java +++ b/src/main/java/cn/lunadeer/minecraftpluginutils/databse/DatabaseManager.java @@ -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"); diff --git a/src/main/java/cn/lunadeer/minecraftpluginutils/databse/FieldType.java b/src/main/java/cn/lunadeer/minecraftpluginutils/databse/FieldType.java index cd24c52..9d7a175 100644 --- a/src/main/java/cn/lunadeer/minecraftpluginutils/databse/FieldType.java +++ b/src/main/java/cn/lunadeer/minecraftpluginutils/databse/FieldType.java @@ -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"), ; diff --git a/src/main/java/cn/lunadeer/minecraftpluginutils/databse/syntax/CreateTable.java b/src/main/java/cn/lunadeer/minecraftpluginutils/databse/syntax/CreateTable.java index b9b6d92..34e9e0a 100644 --- a/src/main/java/cn/lunadeer/minecraftpluginutils/databse/syntax/CreateTable.java +++ b/src/main/java/cn/lunadeer/minecraftpluginutils/databse/syntax/CreateTable.java @@ -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 unique : uniqueFields) {