Skip to content

Commit

Permalink
Refactor and improve documentation in MYSQL project
Browse files Browse the repository at this point in the history
Added comprehensive Javadoc comments in various classes, improving documentation. Implemented QueryHelper in User, Row, and Column classes, streamlining the code. Refactored some parts of the code to enhance readability and maintainability. Made the code more legible by removing redundancy and unnecessary try-catch blocks.
  • Loading branch information
Coho04 committed Dec 13, 2023
1 parent 5b316b4 commit 63bc65b
Show file tree
Hide file tree
Showing 16 changed files with 336 additions and 370 deletions.
23 changes: 16 additions & 7 deletions src/main/java/de/goldendeveloper/mysql/MYSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ public MYSQL(String hostname, String username, String password) {
}

/**
* This method is used to create a new MYSQL object with the specified hostname, username, password, port, and exception handler class.
* Constructs a MYSQL object with the specified hostname, username, password, and port.
*
* @param hostname The hostname of the MySQL server.
* @param username The username to connect to the MySQL server.
* @param password The password to connect to the MySQL server.
* @param port The port of the MySQL server.
* @param exceptionHandlerClass The exception handler class to handle exceptions.
* @param hostname The hostname of the MySQL server.
* @param username The username to connect to the MySQL server.
* @param password The password to connect to the MySQL server.
* @param port The port of the MySQL server.
* @param exceptionHandlerClass The ExceptionHandler class for handling exceptions.
* @param <T> The type of the ExceptionHandler class.
*/
public <T extends ExceptionHandler> MYSQL(String hostname, String username, String password, int port, T exceptionHandlerClass) {
this.hostname = hostname;
Expand Down Expand Up @@ -287,6 +288,13 @@ public void createUser(String username, String password) {
this.createUser(username, password, false);
}

/**
* Retrieves an existing User object with the given username or creates a new one if it doesn't exist.
*
* @param username The username of the user to retrieve or create.
* @param password The password of the user to retrieve or create.
* @return An instance of the User class representing the user with the given username.
*/
public User getOrCreateUser(String username, String password) {
if (!existsUser(username)) {
this.createUser(username, password, false);
Expand Down Expand Up @@ -323,7 +331,8 @@ public User getUser(String name) {
/**
* Sets the ExceptionHandler class for handling exceptions.
*
* @param exceptionHandler The ExceptionHandler instance to be set.
* @param exceptionHandler The instance of the ExceptionHandler class.
* @param <T> The type of the ExceptionHandler class.
*/
public <T extends ExceptionHandler> void setExceptionHandlerClass(T exceptionHandler) {
this.exceptionHandlerClass = exceptionHandler;
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/de/goldendeveloper/mysql/entities/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ public class Column implements QueryHelper {
private final MYSQL mysql;

/**
* @param name - name of the column
* @param table - table of the column
* @param mysql - mysql instance
* Initializes a new instance of the Column class with the given name, table, and MYSQL object.
*
* @param name The name of the column.
* @param table The Table object associated with this column.
* @param mysql The MYSQL object used for database operations.
*/
public Column(String name, Table table, MYSQL mysql) {
this.name = name;
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/de/goldendeveloper/mysql/entities/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public class Database implements QueryHelper {
private final MYSQL mysql;

/**
* Represents a database.
* Creates a new instance of the Database class.
*
* @param name the name of the database
* @param mysql the MYSQL object used to interact with the database
*/
public Database(String name, MYSQL mysql) {
this.name = name;
Expand Down Expand Up @@ -144,10 +147,10 @@ public void createTable(String name, List<String> columns) {
* @param name the name of the table to check
* @return true if the table exists, false otherwise
*/
public boolean existsTable(String tableName) {
public boolean existsTable(String name) {
try (Connection connection = mysql.getConnect()) {
DatabaseMetaData metaData = connection.getMetaData();
ResultSet resultSet = metaData.getTables(null, null, tableName, new String[] {"TABLE"});
ResultSet resultSet = metaData.getTables(null, null, name, new String[] {"TABLE"});
boolean exists = resultSet.next();
resultSet.close();
return exists;
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/de/goldendeveloper/mysql/entities/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ public class Row implements QueryHelper {
private final MYSQL mysql;

/**
* Represents a row in a MySQL database table.
* Represents a row in a database table.
*
* The Row class contains methods to retrieve and modify data in the corresponding row of the table.
*
* @param table The Table object representing the table the row belongs to.
* @param column The Column object representing the column in the row.
* @param mysql The MYSQL object representing the connection to the database.
* @param item The value of the column in the row.
*/
public Row(Table table, Column column, MYSQL mysql, String item) {
this.db = table.getDatabase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import java.util.HashMap;

/**
* Represents a builder class for building rows to be inserted into a database table.
* The RowBuilder class is used to build a row for database insertion.
* It allows users to insert key-value pairs into a HashMap using the with() method, and retrieve the built HashMap using the build() method.
*/
public class RowBuilder {

Expand Down
16 changes: 8 additions & 8 deletions src/main/java/de/goldendeveloper/mysql/entities/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ public class Table implements QueryHelper {
private final MYSQL mysql;

/**
* The Table class represents a table in a database.
* It provides methods to perform various operations on the table,
* such as retrieving the table description, dropping the table,
* getting rows, columns, and checking table and column existence.
* Creates a new Table object with the given name, database, and MYSQL objects.
*
* @param name The name of the table.
* @param database The Database object representing the associated database.
* @param mysql The MYSQL object representing the database connection.
*/
public Table(String name, Database database, MYSQL mysql) {
this.name = name;
Expand Down Expand Up @@ -95,12 +96,11 @@ public List<Row> getRows() {
}

/**
* Retrieves a map of search results based on the provided statement, column, and item.
* Retrieves a map of search results based on the specified column and item.
*
* @param column The Column object representing the column to search in.
* @param column The column to search in.
* @param item The item to search for in the specified column.
* @return A HashMap<String, SearchResult> containing the search results, where the key is the column name
* and the value is a SearchResult object representing the retrieved item.
* @return A map of search results, where the key is the column name and the value is a SearchResult object representing the retrieved item.
*/
public HashMap<String, SearchResult> getMap(Column column, String item) {
String query = "SELECT * FROM `" + this.getName() + "` WHERE " + column.getName() + " = '" + item + "';";
Expand Down
178 changes: 178 additions & 0 deletions src/main/java/de/goldendeveloper/mysql/entities/enums/MysqlTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,208 @@
*/
@SuppressWarnings("unused")
public enum MysqlTypes {

/**
* Enumeration representing the variable "BIT".
*
* This enumeration is used to represent a bit variable.
*
* The value of this enumeration is a string "bit".
*/
BIT("bit"),
/**
* The TINYINT enum represents the tinyint MySQL data type.
*
* <p>
* The TINYINT data type is used to store a small integer value ranging from -128 to 127.
* It occupies 1 byte of storage space.
* </p>
*/
TINYINT("tinyint"),
/**
* The SMALLINT enum represents the smallint MySQL data type.
*
* <p>
* The SMALLINT data type is used to store small integer values ranging from -32768 to 32767.
* It occupies 2 bytes of storage space.
* </p>
*/
SMALLINT("smallint"),
/**
* The INT enum represents the int MySQL data type.
*
* <p>
* The INT data type is used to store integer values ranging from -2147483648 to 2147483647.
* It occupies 4 bytes of storage space.
* </p>
*
* <p>
* The INT data type is represented as 'int' in MySQL.
* </p>
*
* @see MysqlTypes
*/
INT("int"),
/**
* Represents a variable of type BIGINT.
*
* The BIGINT type in SQL is typically used to represent large integer values.
*/
BIGINT("bigint"),
/**
* The DECIMAL enum represents the decimal MySQL data type.
*
* <p>
* The DECIMAL data type is used to store fixed-point decimal numbers.
* It provides exact precision but can store a variable number of digits before and after the decimal point.
* </p>
*
* <p>
* The DECIMAL data type is represented as 'decimal' in MySQL and its storage size depends on the specified precision and scale.
* </p>
*
* @see MysqlTypes
*/
DECIMAL("decimal"),
/**
* Represents a numeric value.
*/
NUMERIC("numeric"),
/**
* Represents a variable of data type float.
*/
FLOAT("float"),
/**
* Enum variable representing the type "real".
*/
REAL("real"),
/**
* Represents a variable of type double.
*/
DOUBLE("double"),
/**
* This is a constant variable representing a date.
*
* The variable is stored as a string with the value "date".
*
* Usage example:
* String myDate = DATE.getValue();
*/
DATE("date"),
/**
* Represents a variable called TIME.
*
* <p>
* The variable stores a time value.
* </p>
*
* @since 1.0
* @see <a href="link_to_related_documentation">Related documentation</a>
*/
TIME("time"),
/**
* Represents a variable indicating the datetime.
*/
DATETIME("datetime"),
/**
* Represents the timestamp of an event or record.
* <p>
* The timestamp is a value that represents the date and time when the event or record occurred.
* It can be used to track the sequence and timing of events or records.
* </p>
* @see Record
*/
TIMESTAMP("timestamp"),
/**
* This enumeration represents the available values for the "year" variable.
*/
YEAR("year"),
/**
* The CHAR variable represents a character type in Java.
* It is used to store single characters and occupies 2 bytes in memory.
* The value of a CHAR variable can be any valid Unicode character.
*
* Usage:
* CHAR("char")
*
* Example:
* char myChar = CHAR.getValue();
*/
CHAR("char"),
/**
* Represents the VARCHAR data type in a database.
*/
VARCHAR("varchar"),
/**
* Represents a variable for storing text.
*/
TEXT("text"),
/**
* Represents a constant for the "nchar" variable.
*/
NCHAR("nchar"),
/**
* Represents a variable of NVARCHAR data type.
*/
NVARCHAR("nvarchar"),
/**
* Represents a binary value.
*
* Usage:
* BINARY binary = BINARY("binary");
*
* This class can be used to store binary values and perform operations on them.
*
*/
BINARY("binary"),
/**
* Represents a VARBINARY type in a database.
*
* <p>
* VARBINARY is a data type that stores binary data (byte arrays) of variable length.
* It is often used to store images, documents, or any other binary data.
* </p>
*
* @since 1.0.0
*/
VARBINARY("varbinary"),
/**
* Represents a BLOB (Binary Large OBject).
*
* <p>
* The BLOB type is used to store large binary data, such as images or documents, in a database.
* </p>
*
* <p>
* This variable represents a BLOB value named "blob".
* </p>
*
* @since 1.0
*/
BLOB("blob"),
/**
* The JSON enum represents the JSON MySQL data type.
*
* <p>
* The JSON data type is used to store JSON (JavaScript Object Notation) data in MySQL.
* It can store JSON objects or arrays and supports various JSON functions and operations.
* </p>
*
* <p>
* The JSON data type is represented as 'json' in MySQL and does not have a fixed storage size.
* </p>
*
* @see MysqlTypes
*/
JSON("json"),
/**
* The BOOLEAN enum represents the boolean MySQL data type.
*
* <p>
* The BOOLEAN data type is used to store boolean values (true or false).
* It is represented as 'bit(1)' in MySQL and occupies 1 byte of storage space.
* </p>
*/
BOOLEAN("boolean");

private final String mysqlTypeName;
Expand Down
Loading

0 comments on commit 63bc65b

Please sign in to comment.