mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2024-11-27 04:25:14 +01:00
Started working on the database stuff :D
This commit is contained in:
parent
988a3a549c
commit
8c5d0f9c01
41
com/Acrobot/Breeze/Database/Database.java
Normal file
41
com/Acrobot/Breeze/Database/Database.java
Normal file
@ -0,0 +1,41 @@
|
||||
package com.Acrobot.Breeze.Database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* Database class, which can be used to connect to JDBC
|
||||
*
|
||||
* @author Acrobot
|
||||
*/
|
||||
public class Database {
|
||||
private String uri;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public Database(String uri, String username, String password) {
|
||||
this.uri = uri;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the table with given name, even if it doesn't exist in the database
|
||||
*
|
||||
* @param name Table's name
|
||||
* @return Table
|
||||
*/
|
||||
public Table getTable(String name) {
|
||||
return new Table(this, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Connection to the database
|
||||
* @throws SQLException exception
|
||||
*/
|
||||
public Connection getConnection() throws SQLException {
|
||||
return DriverManager.getConnection(uri, username, password);
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package com.Acrobot.Breeze.Database;
|
||||
|
||||
/**
|
||||
* @author Acrobot
|
||||
*/
|
||||
public interface DatabaseDriver {
|
||||
|
||||
}
|
95
com/Acrobot/Breeze/Database/Row.java
Normal file
95
com/Acrobot/Breeze/Database/Row.java
Normal file
@ -0,0 +1,95 @@
|
||||
package com.Acrobot.Breeze.Database;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* A class representing a Row in SQL query
|
||||
*
|
||||
* @author Acrobot
|
||||
*/
|
||||
public class Row {
|
||||
private List<String> keys = new ArrayList<String>();
|
||||
private Map<String, String> values = new HashMap<String, String>();
|
||||
|
||||
/**
|
||||
* Puts a name/value pair inside the row
|
||||
*
|
||||
* @param name Item's name
|
||||
* @param value Item's value
|
||||
*/
|
||||
public void put(String name, String value) {
|
||||
if (!values.containsKey(name)) {
|
||||
keys.add(name);
|
||||
}
|
||||
|
||||
values.put(name, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value from name
|
||||
*
|
||||
* @param name Value's name
|
||||
* @return Value
|
||||
*/
|
||||
public String get(String name) {
|
||||
return values.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value from index
|
||||
*
|
||||
* @param index Value's index
|
||||
* @return Value
|
||||
*/
|
||||
public String get(int index) {
|
||||
String key = keys.get(index);
|
||||
return values.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns value's key
|
||||
*
|
||||
* @param index Value's index
|
||||
* @return Value's key
|
||||
*/
|
||||
public String getKey(int index) {
|
||||
return keys.get(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all values
|
||||
*
|
||||
* @return All values in this row
|
||||
*/
|
||||
public Collection<String> getValues() {
|
||||
return values.values();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string of all values separated by a comma
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String stringOfValues() {
|
||||
StringBuilder builder = new StringBuilder(30);
|
||||
|
||||
for (int i = 0; i < values.size(); i++) {
|
||||
builder.append('\'').append(get(i)).append('\'');
|
||||
|
||||
if (i != (values.size() - 1)) {
|
||||
builder.append(',');
|
||||
}
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of values inside the row
|
||||
*
|
||||
* @return number of values
|
||||
*/
|
||||
public int getSize() {
|
||||
return values.size();
|
||||
}
|
||||
}
|
39
com/Acrobot/Breeze/Database/RowSet.java
Normal file
39
com/Acrobot/Breeze/Database/RowSet.java
Normal file
@ -0,0 +1,39 @@
|
||||
package com.Acrobot.Breeze.Database;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Acrobot
|
||||
*/
|
||||
public class RowSet {
|
||||
private List<Row> rowList = new ArrayList<Row>();
|
||||
|
||||
/**
|
||||
* Adds a row to the set
|
||||
*
|
||||
* @param row Row to add
|
||||
*/
|
||||
public void add(Row row) {
|
||||
rowList.add(row);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a row from its index
|
||||
*
|
||||
* @param index Row's index
|
||||
* @return Row
|
||||
*/
|
||||
public Row get(int index) {
|
||||
return rowList.get(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of rows inside this RowSet
|
||||
*
|
||||
* @return row number
|
||||
*/
|
||||
public int size() {
|
||||
return rowList.size();
|
||||
}
|
||||
}
|
150
com/Acrobot/Breeze/Database/Table.java
Normal file
150
com/Acrobot/Breeze/Database/Table.java
Normal file
@ -0,0 +1,150 @@
|
||||
package com.Acrobot.Breeze.Database;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
/**
|
||||
* Represents a table in database
|
||||
*
|
||||
* @author Acrobot
|
||||
*/
|
||||
public class Table {
|
||||
private Database database;
|
||||
private String name;
|
||||
|
||||
private final String SELECT_ALL = "SELECT * FROM " + name;
|
||||
private final String SELECT_STATEMENT = "SELECT * FROM " + name + " WHERE 's'";
|
||||
|
||||
private final String INSERT_VALUES = "INSERT INTO " + name + " VALUES ('s')";
|
||||
private final String UPDATE = "UPDATE " + name + " SET 's' WHERE 's'";
|
||||
|
||||
private final String CREATE = "CREATE TABLE IF NOT EXISTS " + name + " ('s')";
|
||||
|
||||
|
||||
public Table(Database database, String name) {
|
||||
this.database = database;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private RowSet select(String criteria) throws SQLException {
|
||||
Connection connection = database.getConnection();
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
|
||||
String query = criteria == null || criteria.isEmpty() ? SELECT_ALL : SELECT_STATEMENT;
|
||||
query = String.format(query, criteria);
|
||||
|
||||
ResultSet results = statement.executeQuery(query);
|
||||
ResultSetMetaData metaData = results.getMetaData();
|
||||
|
||||
RowSet rowSet = new RowSet();
|
||||
int columnCount = metaData.getColumnCount();
|
||||
|
||||
while (results.next()) {
|
||||
Row row = new Row();
|
||||
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
String name = metaData.getColumnName(i);
|
||||
String value = results.getString(i);
|
||||
row.put(name, value);
|
||||
}
|
||||
|
||||
rowSet.add(row);
|
||||
}
|
||||
|
||||
connection.close();
|
||||
|
||||
return rowSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the first row from the given statement
|
||||
*
|
||||
* @param criteria Criteria for the statement, "SELECT * FROM table_name WHERE ......."
|
||||
* @return First row of the result set
|
||||
* @throws SQLException exception
|
||||
*/
|
||||
public Row getRow(String criteria) throws SQLException {
|
||||
RowSet rs = select(criteria);
|
||||
return rs.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all rows from the given statement
|
||||
*
|
||||
* @param criteria Criteria for the statement, "SELECT * FROM table_name WHERE ......."
|
||||
* @return Result set
|
||||
* @throws SQLException exception
|
||||
*/
|
||||
public RowSet getRows(String criteria) throws SQLException {
|
||||
return select(criteria);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the rows of this table
|
||||
*
|
||||
* @return All rows of this table
|
||||
* @throws SQLException exception
|
||||
*/
|
||||
public RowSet getRows() throws SQLException {
|
||||
return getRows(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a row into the table
|
||||
*
|
||||
* @param row Row to insert
|
||||
* @throws SQLException exception
|
||||
*/
|
||||
public void insertRow(Row row) throws SQLException {
|
||||
insertRow(row, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a row into the table
|
||||
*
|
||||
* @param row Row to insert
|
||||
* @param condition If the conditions are present, the row is updated when the conditions are met
|
||||
* @throws SQLException exception
|
||||
*/
|
||||
public void insertRow(Row row, String condition) throws SQLException {
|
||||
String statement = "";
|
||||
|
||||
if (condition == null || condition.isEmpty()) {
|
||||
statement = String.format(INSERT_VALUES, row.stringOfValues());
|
||||
} else {
|
||||
StringBuilder builder = new StringBuilder(30);
|
||||
|
||||
for (int i = 0; i < row.getSize(); ++i) {
|
||||
String key = row.getKey(i);
|
||||
String value = row.get(i);
|
||||
|
||||
builder.append(key).append("= '").append(value).append('\'');
|
||||
|
||||
if (i != row.getSize() - 1) {
|
||||
builder.append(", ");
|
||||
}
|
||||
}
|
||||
|
||||
statement = String.format(UPDATE, builder.toString(), condition);
|
||||
}
|
||||
|
||||
Connection connection = database.getConnection();
|
||||
Statement stm = connection.createStatement();
|
||||
|
||||
stm.executeUpdate(statement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a table with given fields. If the table already exists, nothing happens
|
||||
*
|
||||
* @param fields Fields of the table
|
||||
*/
|
||||
public void create(String fields) throws SQLException {
|
||||
String statement = String.format(CREATE, fields);
|
||||
|
||||
Connection connection = database.getConnection();
|
||||
Statement stm = connection.createStatement();
|
||||
|
||||
stm.executeUpdate(statement);
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@ name: ChestShop
|
||||
|
||||
main: com.Acrobot.ChestShop.ChestShop
|
||||
|
||||
version: 3.50t0022
|
||||
version: 3.50t0023
|
||||
|
||||
#for CButD
|
||||
dev-url: http://dev.bukkit.org/server-mods/chestshop/
|
||||
|
Loading…
Reference in New Issue
Block a user