Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
slowcheet4h committed Oct 17, 2022
2 parents bd60470 + 02c9119 commit 7b801d3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 46 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ After that you can add:
<dependency>
<groupId>com.github.unitedmeows</groupId>
<artifactId>yystal</artifactId>
<version>1.1.5</version>
<version>1.1.6</version>
</dependency>
```
to your pom.xml.
83 changes: 38 additions & 45 deletions pisi/unitedmeows/yystal/sql/YDatabaseClient.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package pisi.unitedmeows.yystal.sql;

import pisi.unitedmeows.yystal.utils.IDisposable;

import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YDatabaseClient implements IDisposable {
import pisi.unitedmeows.yystal.utils.IDisposable;

public class YDatabaseClient implements IDisposable {
private boolean connected;
private Connection connection;
private final java.lang.Object actionLock = new java.lang.Object();
private HashMap<String, List<String>> tableColumnsCache;
private HashMap<String, List<String>> tableColumnsCache;

public YDatabaseClient(String username, String password, String database, String host, int port) {
try {
Expand All @@ -22,10 +25,11 @@ public YDatabaseClient(String username, String password, String database, String
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?characterEncoding=latin1&useConfigs=maxPerformance", username, password);
connected = true;
}
tableColumnsCache = new HashMap<>();
tableColumnsCache = new HashMap<>();
}
catch (Exception ex) {
connected = false;
ex.printStackTrace();
}
}

Expand Down Expand Up @@ -55,37 +59,26 @@ public boolean execute(YSQLCommand sql) {



public List<String> dbColumnsNoCache(String table) {
synchronized (actionLock) {
List<Map<String, Object>> select = select(
new YSQLCommand(
"SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=^").putString(table),
"TABLE_CATALOG",
"TABLE_SCHEMA",
"TABLE_NAME",
"COLUMN_NAME"
);

List<String> columns = new ArrayList<>();

for (int i = 0; i < select.size(); i++) {
columns.add((String) select.get(i).get("COLUMN_NAME"));
}

return columns;
}
}

public List<String> dbColumns(String table) {
List<String> columns = tableColumnsCache.getOrDefault(table, null);

if (columns == null) {
tableColumnsCache.put(table, dbColumnsNoCache(table));
return columns;
}
public List<String> dbColumnsNoCache(String table) {
synchronized (actionLock) {
List<Map<String, Object>> select = select(new YSQLCommand("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=^").putString(table), "TABLE_CATALOG", "TABLE_SCHEMA", "TABLE_NAME",
"COLUMN_NAME");
List<String> columns = new ArrayList<>();
for (int i = 0; i < select.size(); i++) {
columns.add((String) select.get(i).get("COLUMN_NAME"));
}
return columns;
}
}

return tableColumnsCache.get(table);
}
public List<String> dbColumns(String table) {
List<String> columns = tableColumnsCache.getOrDefault(table, null);
if (columns == null) {
tableColumnsCache.put(table, dbColumnsNoCache(table));
return columns;
}
return tableColumnsCache.get(table);
}


public List<List<Object>> select(YSQLCommand sql) {
Expand Down Expand Up @@ -149,15 +142,15 @@ public Connection connection() {
return connection;
}

@Override
public void close() {
try {
connection.close();
} catch (Exception ex) {}
connected = false;
tableColumnsCache.clear();
}

@Override
public void close() {
try {
connection.close();
}
catch (Exception ex) {}
connected = false;
tableColumnsCache.clear();
}
/* public boolean insertMulti(String tableName, List<List<Object>> dataList, String... columns) {
String sql = "INSERT INTO " + tableName + "(" + String.join(",", columns) + " VALUES ";
Expand Down

0 comments on commit 7b801d3

Please sign in to comment.