Skip to content

Commit a9aedb8

Browse files
author
thisisnzed
committed
Added first release
1 parent a54d562 commit a9aedb8

File tree

5 files changed

+364
-0
lines changed

5 files changed

+364
-0
lines changed

pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.easysql</groupId>
6+
<artifactId>EasySQL</artifactId>
7+
<version>1.0.0</version>
8+
<properties>
9+
<maven.compiler.source>8</maven.compiler.source>
10+
<maven.compiler.target>8</maven.compiler.target>
11+
</properties>
12+
<dependencies>
13+
<dependency>
14+
<groupId>mysql</groupId>
15+
<artifactId>mysql-connector-java</artifactId>
16+
<version>8.0.26</version>
17+
</dependency>
18+
</dependencies>
19+
</project>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.nz1337.easysql;
2+
3+
import com.nz1337.easysql.manager.Database;
4+
import com.nz1337.easysql.manager.Table;
5+
6+
import java.sql.Connection;
7+
import java.sql.DriverManager;
8+
import java.sql.SQLException;
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
12+
public class EasySQL {
13+
14+
private final ArrayList<Table> tables = new ArrayList<>();
15+
private Connection connection;
16+
private Database database;
17+
private String host = "127.0.0.1";
18+
private String user = "root";
19+
private String password = "password";
20+
private String databaseName = "database";
21+
private int port = 3306;
22+
private boolean encoder = true;
23+
24+
public void connect() {
25+
try {
26+
synchronized (this) {
27+
if (getConnection() != null && !getConnection().isClosed()) {
28+
System.err.println("[EasySQL] An SQL connection is already active!");
29+
return;
30+
}
31+
Class.forName("com.mysql.jdbc.Driver");
32+
String url = "jdbc:mysql://" + this.host + ":" + this.port;
33+
String encoding = this.encoder ? "?useUnicode=true&characterEncoding=utf8" : "";
34+
setConnection(DriverManager.getConnection(url + encoding, this.user, this.password));
35+
this.database = new Database();
36+
this.database.createIfNotExists(this.getConnection(), this.databaseName);
37+
setConnection(DriverManager.getConnection(url + "/" + this.databaseName + encoding, this.user, this.password));
38+
this.tables.forEach(tableCreator -> tableCreator.createIfNotExists(connection));
39+
}
40+
} catch (SQLException | ClassNotFoundException e) {
41+
e.printStackTrace();
42+
}
43+
}
44+
45+
public void close() {
46+
try {
47+
this.getConnection().close();
48+
} catch (SQLException e) {
49+
e.printStackTrace();
50+
}
51+
}
52+
53+
/*public Column getColumns(Table table) {
54+
return this.columns.stream().filter(column -> column.getTable().equals(table.getTable())).findFirst().orElse(null);
55+
}*/
56+
57+
public void delete() {
58+
this.database.deleteIfExists(this.databaseName);
59+
}
60+
61+
public EasySQL createDefaultTables(Table... table) {
62+
this.tables.addAll(Arrays.asList(table));
63+
return this;
64+
}
65+
66+
public EasySQL setHost(String host) {
67+
this.host = host;
68+
return this;
69+
}
70+
71+
public EasySQL setPassword(String password) {
72+
this.password = password;
73+
return this;
74+
}
75+
76+
public EasySQL setUser(String user) {
77+
this.user = user;
78+
return this;
79+
}
80+
81+
public EasySQL setDatabase(String databaseName) {
82+
this.databaseName = databaseName;
83+
return this;
84+
}
85+
86+
public EasySQL setPort(int port) {
87+
this.port = port;
88+
return this;
89+
}
90+
91+
public EasySQL setEncoder(boolean encoder) {
92+
this.encoder = encoder;
93+
return this;
94+
}
95+
96+
private Connection getConnection() {
97+
return connection;
98+
}
99+
100+
private void setConnection(Connection connection) {
101+
this.connection = connection;
102+
}
103+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.nz1337.easysql.manager;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.util.ArrayList;
8+
import java.util.concurrent.atomic.AtomicInteger;
9+
10+
public class Column {
11+
12+
private final Connection connection;
13+
private final Table table;
14+
private final String tableName;
15+
private final String primaryKey;
16+
private final StringBuilder allColumns = new StringBuilder();
17+
18+
public Column(Table table, Connection connection, ArrayList<String> listedColumns) {
19+
this.connection = connection;
20+
this.table = table;
21+
this.primaryKey = table.getPrimaryKey();
22+
this.tableName = this.table.getTable();
23+
listedColumns.forEach(column -> {
24+
allColumns.append("`").append(column).append("`");
25+
if (!listedColumns.get(listedColumns.size() - 1).equals(column)) allColumns.append(",");
26+
});
27+
}
28+
29+
public boolean isExists(String column, Object value) {
30+
try {
31+
PreparedStatement preparedStatement = this.connection.prepareStatement("SELECT * FROM `" + this.tableName + "` WHERE `" + column + "`=?");
32+
preparedStatement.setObject(1, value);
33+
if (preparedStatement.executeQuery().next()) return true;
34+
} catch (SQLException e) {
35+
e.printStackTrace();
36+
}
37+
return false;
38+
}
39+
40+
public void insertDefault(Object... values) {
41+
String primary = this.getPrimaryKey();
42+
try {
43+
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `" + this.tableName + "` WHERE `" + primary + "`=?");
44+
preparedStatement.setObject(1, values[0]);
45+
preparedStatement.executeQuery().next();
46+
if (!isExists(primary, values[0])) {
47+
StringBuilder unknownValue = new StringBuilder();
48+
for (int i = 0; i < countChars(this.allColumns.toString(), ',') + 1; i++) unknownValue.append("?,");
49+
unknownValue.append(")");
50+
PreparedStatement insert = connection.prepareStatement("INSERT INTO `" + this.tableName + "` (" + this.allColumns + ") VALUE (" + unknownValue.toString().replace(",)", ")"));
51+
for (int i = 0; i < values.length; i++) insert.setObject(i + 1, values[i]);
52+
insert.executeUpdate();
53+
}
54+
} catch (SQLException e) {
55+
e.printStackTrace();
56+
}
57+
}
58+
59+
public void editValue(String searchedColumn, Object searchedValue, String editedColumn, Object newValue) {
60+
try {
61+
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `" + this.getTableName() + "` SET `" + editedColumn + "`=? WHERE `" + searchedColumn + "`=?");
62+
preparedStatement.setObject(1, newValue);
63+
preparedStatement.setObject(2, searchedValue);
64+
preparedStatement.executeUpdate();
65+
} catch (SQLException e) {
66+
e.printStackTrace();
67+
}
68+
}
69+
70+
public Object getValue(String column, Object value, String desiredValue) {
71+
try {
72+
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `" + this.tableName + "` WHERE `" + column + "`=?");
73+
preparedStatement.setObject(1, value);
74+
ResultSet results = preparedStatement.executeQuery();
75+
results.next();
76+
return results.getObject(desiredValue);
77+
} catch (SQLException e) {
78+
e.printStackTrace();
79+
}
80+
return null;
81+
}
82+
83+
public void delete(String column, Object value) {
84+
if (!isExists(column, value)) return;
85+
try {
86+
PreparedStatement preparedStatement = this.connection.prepareStatement("DELETE FROM `" + this.tableName + "` WHERE `" + this.tableName + "`.`" + column + "`=?");
87+
preparedStatement.setObject(1, value);
88+
preparedStatement.executeUpdate();
89+
} catch (SQLException e) {
90+
e.printStackTrace();
91+
}
92+
}
93+
94+
public void makeCustomRequest(String request) {
95+
try {
96+
PreparedStatement db = this.connection.prepareStatement(request);
97+
db.execute();
98+
} catch (SQLException e) {
99+
e.printStackTrace();
100+
}
101+
}
102+
103+
public Table getTable() {
104+
return this.table;
105+
}
106+
107+
public String getTableName() {
108+
return tableName;
109+
}
110+
111+
public String getPrimaryKey() {
112+
return this.primaryKey;
113+
}
114+
115+
private int countChars(String s, char c) {
116+
AtomicInteger i = new AtomicInteger();
117+
s.chars().forEach(chars -> {
118+
if (chars == c) i.getAndIncrement();
119+
});
120+
return i.get();
121+
}
122+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.nz1337.easysql.manager;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.SQLException;
6+
7+
public class Database {
8+
9+
private Connection connection;
10+
11+
public void createIfNotExists(Connection connection, String database) {
12+
this.connection = connection;
13+
try {
14+
PreparedStatement db = connection.prepareStatement("CREATE DATABASE IF NOT EXISTS " + database);
15+
db.execute();
16+
} catch (SQLException e) {
17+
e.printStackTrace();
18+
}
19+
}
20+
21+
public void deleteIfExists(String database) {
22+
try {
23+
PreparedStatement db = this.connection.prepareStatement("DROP DATABASE IF EXISTS " + database);
24+
db.execute();
25+
} catch (SQLException e) {
26+
e.printStackTrace();
27+
}
28+
}
29+
30+
public void makeCustomRequest(String request) {
31+
try {
32+
PreparedStatement db = this.connection.prepareStatement(request);
33+
db.execute();
34+
} catch (SQLException e) {
35+
e.printStackTrace();
36+
}
37+
}
38+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.nz1337.easysql.manager;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.SQLException;
6+
import java.util.ArrayList;
7+
8+
public class Table {
9+
10+
private String primaryKey;
11+
private final String tableName;
12+
private final StringBuilder columns = new StringBuilder();
13+
private final ArrayList<String> listedColumns = new ArrayList<>();
14+
private Connection connection;
15+
private Column column;
16+
17+
public Table(String tableName) {
18+
this.tableName = tableName;
19+
}
20+
21+
public void createIfNotExists(Connection connection) {
22+
this.connection = connection;
23+
if (this.primaryKey.equals("")) this.primaryKey = listedColumns.get(0);
24+
try {
25+
connection.prepareStatement("CREATE TABLE IF NOT EXISTS `" + this.tableName + "` (" + this.columns + "PRIMARY KEY (`" + this.primaryKey + "`))").execute();
26+
} catch (SQLException e) {
27+
e.printStackTrace();
28+
}
29+
this.column = new Column(this, this.connection, this.listedColumns);
30+
}
31+
32+
public Table addColumn(String columnName, String type) {
33+
type = convert(type);
34+
this.columns.append("`").append(columnName).append("`").append(" ").append(type).append(", ");
35+
this.listedColumns.add(columnName);
36+
return this;
37+
}
38+
39+
public Table setPrimaryKey(String primaryKey) {
40+
this.primaryKey = primaryKey;
41+
return this;
42+
}
43+
44+
public void delete() {
45+
try {
46+
this.connection.prepareStatement("DROP TABLE " + getTable()).execute();
47+
} catch (SQLException e) {
48+
e.printStackTrace();
49+
}
50+
}
51+
52+
public String getPrimaryKey() {
53+
return primaryKey;
54+
}
55+
56+
public String getTable() {
57+
return this.tableName;
58+
}
59+
60+
public Column getColumns() {
61+
return this.column;
62+
}
63+
64+
public void makeCustomRequest(String request) {
65+
try {
66+
PreparedStatement db = this.connection.prepareStatement(request);
67+
db.execute();
68+
} catch (SQLException e) {
69+
e.printStackTrace();
70+
}
71+
}
72+
73+
private String convert(String type) {
74+
if (type.equalsIgnoreCase("integer") || type.equalsIgnoreCase("int")) type = "INTEGER";
75+
if (type.equalsIgnoreCase("string") || type.equalsIgnoreCase("str")) type = "VARCHAR(255)";
76+
if (type.equalsIgnoreCase("id") || type.equalsIgnoreCase("uuid")) type = "VARCHAR(36)";
77+
if (type.equalsIgnoreCase("bool") || type.equalsIgnoreCase("boolean")) type = "TINYINT(1)";
78+
if (type.equalsIgnoreCase("text") || type.equalsIgnoreCase("texte")) type = "TEXT";
79+
if (type.equalsIgnoreCase("double")) type = "DOUBLE";
80+
return type;
81+
}
82+
}

0 commit comments

Comments
 (0)