-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>net.neyrowz.orm</groupId> | ||
<artifactId>MysqlORM</artifactId> | ||
<version>1.0.0-8</version> | ||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
<version>2.10.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>5.1.6</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package net.neyrowz.orm; | ||
|
||
import com.google.gson.Gson; | ||
import net.neyrowz.orm.mysql.Mysql; | ||
import net.neyrowz.orm.mysql.MysqlStatement; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
|
||
public class DataManager<I, T> { | ||
|
||
private final String name; | ||
private final Mysql mysql; | ||
private final HashMap<I, T> datas; | ||
|
||
public DataManager(String name, Mysql mysql) { | ||
this.name = name; | ||
this.mysql = mysql; | ||
datas = new HashMap<>(); | ||
this.setupTable(); | ||
} | ||
|
||
public void insertInDB(I id, T obj) { | ||
MysqlStatement mysqlStatement = new MysqlStatement("INSERT INTO " + name + "(id, data) VALUES (?, ?)", mysql); | ||
mysqlStatement.addValue(getId(id)); | ||
mysqlStatement.addValue(new Gson().toJson(obj)); | ||
this.mysql.executeAsync(mysqlStatement); | ||
} | ||
|
||
public void updateInDB(I id, T obj, boolean async) { | ||
MysqlStatement mysqlStatement = new MysqlStatement("UPDATE " + name + " SET data = ? WHERE id = ?", mysql); | ||
mysqlStatement.addValue(new Gson().toJson(obj)); | ||
mysqlStatement.addValue(getId(id)); | ||
if (async) { | ||
this.mysql.executeAsync(mysqlStatement); | ||
} | ||
else { | ||
this.mysql.execute(mysqlStatement); | ||
} | ||
} | ||
|
||
public T getInDB(I id, Class<T> clazz) throws SQLException { | ||
MysqlStatement mysqlStatement = new MysqlStatement("SELECT * FROM " + name + " WHERE id = ?", mysql); | ||
mysqlStatement.addValue(getId(id)); | ||
ResultSet result = this.mysql.executeGet(mysqlStatement); | ||
if (result == null) { | ||
return null; | ||
} | ||
T t = new Gson().fromJson(result.getString("data"), clazz); | ||
result.getStatement().close(); | ||
return t; | ||
} | ||
|
||
public HashMap<I, T> getDatas() { | ||
return datas; | ||
} | ||
|
||
public String getId(I id) { | ||
return id.toString(); | ||
} | ||
|
||
public void shutdown() { | ||
getDatas().forEach((i, t) -> { | ||
updateInDB(i, t, false); | ||
}); | ||
} | ||
|
||
public void setupTable() { | ||
this.mysql.execute("CREATE TABLE IF NOT EXISTS " + name + "(id TEXT, data TEXT)"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package net.neyrowz.orm.mysql; | ||
|
||
import com.google.gson.Gson; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
public class Credentials { | ||
|
||
private final String host; | ||
private final String database; | ||
private final String username; | ||
private final String password; | ||
|
||
public Credentials fromJson(File file) { | ||
try { | ||
if (!file.exists()) { | ||
file.getParentFile().mkdirs(); | ||
file.createNewFile(); | ||
} | ||
BufferedReader bf = new BufferedReader(new FileReader(file)); | ||
StringBuilder sb = new StringBuilder(); | ||
String line; | ||
while ((line = bf.readLine()) != null) { | ||
sb.append(line); | ||
} | ||
bf.close(); | ||
return new Gson().fromJson(sb.toString(), Credentials.class); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return this; | ||
} | ||
|
||
public Credentials(String host, String database, String username, String password) { | ||
this.host = host; | ||
this.database = database; | ||
this.username = username; | ||
this.password = password; | ||
} | ||
|
||
public String getHost() { | ||
return host; | ||
} | ||
|
||
public String getDatabase() { | ||
return database; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
} |