Skip to content

Commit

Permalink
First Commit 😎
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Mar 1, 2023
0 parents commit 5a49de0
Show file tree
Hide file tree
Showing 31 changed files with 638 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/discord.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions pom.xml
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>
72 changes: 72 additions & 0 deletions src/main/java/net/neyrowz/orm/DataManager.java
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)");
}
}
59 changes: 59 additions & 0 deletions src/main/java/net/neyrowz/orm/mysql/Credentials.java
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;
}
}
Loading

0 comments on commit 5a49de0

Please sign in to comment.