diff --git a/src/main/java/com/hz6826/clockin/init/DatabaseInit.java b/src/main/java/com/hz6826/clockin/init/DatabaseInit.java index 8a07a73..2931839 100644 --- a/src/main/java/com/hz6826/clockin/init/DatabaseInit.java +++ b/src/main/java/com/hz6826/clockin/init/DatabaseInit.java @@ -3,52 +3,192 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; -import java.util.HashMap; +import java.util.ArrayList; import java.util.Scanner; -import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import com.google.gson.JsonObject; +import com.google.gson.JsonArray; +import java.util.Map; +import java.util.Set; import com.hz6826.clockin.ClockIn; -import org.jetbrains.annotations.Nullable; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.DatabaseMetaData; +import java.sql.Statement; + + +/* + * 储存了sql的信息 + * */ +class SQLPair { + public String dataBaseName; + public JsonObject sql_tables; // ??? + + public SQLPair(String dataBaseName, JsonObject sql_tables) { + this.sql_tables = sql_tables; + this.databaseName = databaseName; + } +}; public class DatabaseInit { - private HashMap sql; + private SQLPair sql; + private Connection connection; + private String databaseName; + + /* + * 构造器初始化sql表,并根据数据库类型建立表 + * */ + public static void DatabaseInit(Connection DBconnection) { + DatabaseMetaData metaData = null; + try { + metaData = DBconnection.getMetaData(); + } catch (SQLException e) { + throw new RuntimeException("get database metadata error"); + } - public DatabaseInit(){ - //读取配置文件 - Gson gson = new Gson(); + String dataBaseName = ""; + try { + dataBaseName = metaData.getDatabaseProductName(); + } catch (SQLException e) { + throw new RuntimeException("get database type error"); + } + + + JsonArray sql_list = JsonParser.parseString(loadSource("sql/index.sql")).getAsJsonObject().getAsJsonArray(); + + if (sql_list.getAsJsonArray().isEmpty()) { + throw new RuntimeException("no sql list"); + } + boolean databaseExists = false; + for (JsonElement databaseElm : sql_list) { + if (databaseElm.getAsString().equalsIgnoreCase(dataBaseName)) { + databaseExists = true; + break; + } + } - //获取当前数据库的类型 + if (!databaseExists) { + throw new RuntimeException("no sql list"); + } //查找是否有匹配的数据库文件 + JsonObject sqlJson = JsonParser.parseString(loadSource("sql" + dataBaseName + "/index.json")).getAsJsonObject().getAsJsonObject(); + if (!sqlJson.get("sql_name").getAsString().equalsIgnoreCase(dataBaseName)) { + throw new RuntimeException("sql name not match,may be the sql is bad"); + } + ; + + JsonObject table_sql_list_array = sqlJson.get("tables").getAsJsonObject(); + + this.sql = new sql_list(dataBaseName, table_sql_list_array); + //有则添加 //没有报错 } - public boolean createTable(String name){ + public boolean export_data(String path) { + + return true; + } + + public boolean import_data(String path) { + return true; + } + + public boolean createTable(String name) { + + String sql=this.sql.sql_tables.get(name).getAsString(); + if (sql == null) { + return false; + } + try (Statement statement = this.connection.createStatement()) { + statement.executeUpdate(sql); + + } catch (SQLException e) { + throw new RuntimeException("create table error",e); + } return true; } + + public boolean deleteTable(String name) { + return true; } - public boolean createAll(String name){ + + public boolean createAll(String name) { + try{ + this.connection.setAutoCommit(false); + + }catch (SQLException e){ + throw new RuntimeException("can not create Transaction",e); + } + + JsonObject jsonObject = this.sql.sql_tables; + + // 获取所有键值对 + Set> entries = jsonObject.entrySet(); + + // 遍历并打印所有数据 + + String currentKey=""; + try (Connection conn = ...) { + for (Map.Entry entry : entries) { + currentKey= entry.getKey(); + String value = entry.getValue().getAsString(); // 根据值的类型选择合适的方法 + this.createTable(value); + } + // 提交事务 + this.connection.commit(); + this.connection.setAutoCommit(true); + + } catch (SQLException e) { + // 回滚事务 + ClockIn.LOGGER.error("create failed:{}", currentKey); + if (this.connection != null) { + try { + this.connection.rollback(); + } + catch (SQLException rollback) { + throw new RuntimeException("rollback error",rollback); + } + } + }finally { + if (this.connection != null) { + try { + this.connection.setAutoCommit(true); + this.connection.close(); + } catch (SQLException e) { + ClockIn.LOGGER.error("close connection error",e); + } + } + } return true; } - public boolean deleteAll(){ + + public boolean deleteAll() { return true; } - public boolean checkTable(){ + + public boolean checkTable() { return true; } /** * 从资源里加载文件 + * * @param fileName 资源名称 * @return 资源内容 */ @@ -63,7 +203,7 @@ private static String loadSource(String fileName) { scanner.useDelimiter("\\A"); return scanner.hasNext() ? scanner.next() : ""; } - } catch (IOException e){ + } catch (IOException e) { ClockIn.LOGGER.error("An exception occured when loading source {}! This is technically our fault. Please report it to us!", fileName, e); } return ""; diff --git a/src/main/resources/sql/create-all/mysql/DailyClockInRecord.sql b/src/main/resources/sql/mysql/DailyClockInRecord.sql similarity index 100% rename from src/main/resources/sql/create-all/mysql/DailyClockInRecord.sql rename to src/main/resources/sql/mysql/DailyClockInRecord.sql diff --git a/src/main/resources/sql/create-all/mysql/GlobalShop.sql b/src/main/resources/sql/mysql/GlobalShop.sql similarity index 100% rename from src/main/resources/sql/create-all/mysql/GlobalShop.sql rename to src/main/resources/sql/mysql/GlobalShop.sql diff --git a/src/main/resources/sql/create-all/mysql/Mail.sql b/src/main/resources/sql/mysql/Mail.sql similarity index 100% rename from src/main/resources/sql/create-all/mysql/Mail.sql rename to src/main/resources/sql/mysql/Mail.sql diff --git a/src/main/resources/sql/create-all/mysql/Reward.sql b/src/main/resources/sql/mysql/Reward.sql similarity index 100% rename from src/main/resources/sql/create-all/mysql/Reward.sql rename to src/main/resources/sql/mysql/Reward.sql diff --git a/src/main/resources/sql/create-all/mysql/User.sql b/src/main/resources/sql/mysql/User.sql similarity index 100% rename from src/main/resources/sql/create-all/mysql/User.sql rename to src/main/resources/sql/mysql/User.sql diff --git a/src/main/resources/sql/create-all/mysql/index.json b/src/main/resources/sql/mysql/index.json similarity index 100% rename from src/main/resources/sql/create-all/mysql/index.json rename to src/main/resources/sql/mysql/index.json diff --git a/src/main/resources/sql/create-all/sql.json b/src/main/resources/sql/sql.json similarity index 100% rename from src/main/resources/sql/create-all/sql.json rename to src/main/resources/sql/sql.json diff --git a/src/main/resources/sql/create-all/sqlite/DailyClockInRecord.sql b/src/main/resources/sql/sqlite/DailyClockInRecord.sql similarity index 100% rename from src/main/resources/sql/create-all/sqlite/DailyClockInRecord.sql rename to src/main/resources/sql/sqlite/DailyClockInRecord.sql diff --git a/src/main/resources/sql/create-all/sqlite/GlobalShop.sql b/src/main/resources/sql/sqlite/GlobalShop.sql similarity index 100% rename from src/main/resources/sql/create-all/sqlite/GlobalShop.sql rename to src/main/resources/sql/sqlite/GlobalShop.sql diff --git a/src/main/resources/sql/create-all/sqlite/Mail.sql b/src/main/resources/sql/sqlite/Mail.sql similarity index 100% rename from src/main/resources/sql/create-all/sqlite/Mail.sql rename to src/main/resources/sql/sqlite/Mail.sql diff --git a/src/main/resources/sql/create-all/sqlite/Reward.sql b/src/main/resources/sql/sqlite/Reward.sql similarity index 100% rename from src/main/resources/sql/create-all/sqlite/Reward.sql rename to src/main/resources/sql/sqlite/Reward.sql diff --git a/src/main/resources/sql/create-all/sqlite/User.sql b/src/main/resources/sql/sqlite/User.sql similarity index 100% rename from src/main/resources/sql/create-all/sqlite/User.sql rename to src/main/resources/sql/sqlite/User.sql diff --git a/src/main/resources/sql/create-all/sqlite/index.json b/src/main/resources/sql/sqlite/index.json similarity index 97% rename from src/main/resources/sql/create-all/sqlite/index.json rename to src/main/resources/sql/sqlite/index.json index dcf87dc..4fb7fed 100644 --- a/src/main/resources/sql/create-all/sqlite/index.json +++ b/src/main/resources/sql/sqlite/index.json @@ -5,5 +5,5 @@ "rewards": "Reward.sql", "mails": "Mail.sql", "daily_clock_in_records": "DailyClockInRecord.sql" - }, + } } \ No newline at end of file