Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 153 additions & 13 deletions src/main/java/com/hz6826/clockin/init/DatabaseInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String,String> 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");
}
Comment on lines +49 to +54
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DatabaseMetaData metaData = null;
try {
metaData = DBconnection.getMetaData();
} catch (SQLException e) {
throw new RuntimeException("get database metadata error");
}
DatabaseMetaData metaData;
try {
metaData = DBconnection.getMetaData();
} catch (SQLException e) {
throw new RuntimeException("get database metadata error");
}

???


public DatabaseInit(){
//读取配置文件
Gson gson = new Gson();
String dataBaseName = "";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
String dataBaseName = "";

?????

try {
dataBaseName = metaData.getDatabaseProductName();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从配置文件中获取

} catch (SQLException e) {
throw new RuntimeException("get database type error");
}
Comment on lines +59 to +61
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

统一拿一个try包就行,最好用try with resource



JsonArray sql_list = JsonParser.parseString(loadSource("sql/index.sql")).getAsJsonObject().getAsJsonArray();

if (sql_list.getAsJsonArray().isEmpty()) {
throw new RuntimeException("no sql list");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要滥用RuntimeException,更好的做法是将捕获的SQLException用LOGGER打印出来,没有必要不要崩溃

}

boolean databaseExists = false;

for (JsonElement databaseElm : sql_list) {
if (databaseElm.getAsString().equalsIgnoreCase(dataBaseName)) {
databaseExists = true;
break;
}
}

//获取当前数据库的类型
if (!databaseExists) {
throw new RuntimeException("no sql list");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

说过了。。。

}

//查找是否有匹配的数据库文件

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");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

。。。

}
;

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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

驼峰命名法能不能不要和蛇形混用?


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<Map.Entry<String, com.google.gson.JsonElement>> entries = jsonObject.entrySet();

// 遍历并打印所有数据

String currentKey="";
try (Connection conn = ...) {
for (Map.Entry<String, com.google.gson.JsonElement> 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 资源内容
*/
Expand All @@ -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 "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"rewards": "Reward.sql",
"mails": "Mail.sql",
"daily_clock_in_records": "DailyClockInRecord.sql"
},
}
}