Package io.github.coho04.mysql.entities
Class Table
java.lang.Object
io.github.coho04.mysql.entities.Table
- All Implemented Interfaces:
QueryHelper
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.
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.github.coho04.mysql.interfaces.QueryHelper
QueryHelper.ResultSetHandler<T>
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a column to the table with the given name.int
Retrieves the number of columns in the table.int
Retrieves the number of rows in the database table.describe()
Returns the description of the table.void
drop()
Drops the table from the database.void
dropRow
(int id) Drops a row from the database table based on the provided ID.boolean
existsColumn
(String name) Checks if a column exists in the table.boolean
Checks if a row exists in the database table based on the provided column and item.Retrieves a Column object with the given name from the table.Retrieves the list of columns for the table.Retrieves the database associated with the table.Retrieves the latest row from the table.Retrieves a map of search results based on the specified column and item.getName()
Retrieves the name of the table.Retrieves the oldest row from the table.Retrieves a random row from the table.Retrieves a row from the table based on a specific column and item.getRowById
(int id) Retrieves a row from the table based on the provided ID.getRows()
Retrieves the list of rows from the table.boolean
Checks if a column exists in the table.boolean
Checks if the table has any columns.void
Inserts a row into the table with the provided data.boolean
isEmpty()
Checks if the table is empty.void
setUniqueColumn
(String name) Sets a unique constraint on the specified column in the table.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.github.coho04.mysql.interfaces.QueryHelper
executeQuery, executeQuery, executeUpdate, executeUpdate
-
Constructor Details
-
Table
Creates a new Table object with the given name, database, and MYSQL objects.- Parameters:
name
- The name of the table.database
- The Database object representing the associated database.mysql
- The MYSQL object representing the database connection.
-
-
Method Details
-
describe
Returns the description of the table.- Returns:
- a ResultSet containing the description of the table
-
getName
Retrieves the name of the table.- Returns:
- The name of the table.
-
drop
public void drop()Drops the table from the database. This method executes a SQL query to drop the table with the given name. -
getRow
Retrieves a row from the table based on a specific column and item.- Parameters:
column
- The Column object representing the column to search in.item
- The item to search for in the specified column.- Returns:
- A Row object representing the retrieved row.
-
getRows
Retrieves the list of rows from the table.- Returns:
- A List of Row objects representing the rows in the table.
-
getMap
Retrieves a map of search results based on the specified column and item.- Parameters:
column
- The column to search in.item
- The item to search for in the specified column.- Returns:
- A map of search results, where the key is the column name and the value is a SearchResult object representing the retrieved item.
-
countRows
public int countRows()Retrieves the number of rows in the database table.- Returns:
- The number of rows in the table.
-
countColumn
public int countColumn()Retrieves the number of columns in the table.- Returns:
- The number of columns in the table.
-
isEmpty
public boolean isEmpty()Checks if the table is empty.- Returns:
- true if the table is empty, false otherwise.
-
dropRow
public void dropRow(int id) Drops a row from the database table based on the provided ID.- Parameters:
id
- The ID of the row to be dropped.
-
getColumns
Retrieves the list of columns for the table.- Returns:
- A List of Column objects representing the columns in the table.
-
getColumn
Retrieves a Column object with the given name from the table.- Parameters:
name
- The name of the column to retrieve.- Returns:
- A Column object representing the retrieved column, or null if the column does not exist.
-
existsColumn
Checks if a column exists in the table.- Parameters:
name
- The name of the column to check.- Returns:
true
if the column exists,false
otherwise.
-
existsRow
Checks if a row exists in the database table based on the provided column and item.- Parameters:
column
- The Column object representing the column to search in.item
- The item to search for in the specified column.- Returns:
- true if the row exists in the table, false otherwise.
-
setUniqueColumn
Sets a unique constraint on the specified column in the table. This method executes an SQL query to add a unique constraint to the table.- Parameters:
name
- The name of the column to set as unique.
-
addColumn
Adds a column to the table with the given name.- Parameters:
name
- the name of the column to add
-
hasColumns
public boolean hasColumns()Checks if the table has any columns.- Returns:
true
if the table has any columns,false
otherwise.
-
hasColumn
Checks if a column exists in the table.- Parameters:
name
- The name of the column to check.- Returns:
true
if the column exists,false
otherwise.
-
insert
Inserts a row into the table with the provided data.- Parameters:
rowBuilder
- a HashMap containing the column names as keys and the corresponding values as values. The column names and values must be of type String.
-
getDatabase
Retrieves the database associated with the table.- Returns:
- The Database object representing the database associated with the table.
-
getRowById
Retrieves a row from the table based on the provided ID.- Parameters:
id
- The ID of the row to retrieve.- Returns:
- A Row object representing the retrieved row.
-
getLastestRow
Retrieves the latest row from the table.- Returns:
- A Row object representing the latest row.
-
getOldestRow
Retrieves the oldest row from the table.- Returns:
- A Row object representing the oldest row in the table. Returns null if an error occurs.
-
getRandomRow
Retrieves a random row from the table.- Returns:
- A Row object representing a randomly selected row from the table.
-