Class Table

java.lang.Object
io.github.coho04.mysql.entities.Table
All Implemented Interfaces:
QueryHelper

public class Table extends Object implements 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.
  • Constructor Details

    • Table

      public Table(String name, Database database, MYSQL mysql)
      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

      public List<String> describe()
      Returns the description of the table.
      Returns:
      a ResultSet containing the description of the table
    • getName

      public String 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

      public Row getRow(Column column, Object item)
      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

      public List<Row> getRows()
      Retrieves the list of rows from the table.
      Returns:
      A List of Row objects representing the rows in the table.
    • getMap

      public HashMap<String,SearchResult> getMap(Column column, String item)
      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

      public List<Column> getColumns()
      Retrieves the list of columns for the table.
      Returns:
      A List of Column objects representing the columns in the table.
    • getColumn

      public Column getColumn(String name)
      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

      public boolean existsColumn(String name)
      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

      public boolean existsRow(Column column, String item)
      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

      public void setUniqueColumn(String name)
      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

      public void addColumn(String name)
      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

      public boolean hasColumn(String name)
      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

      public void insert(HashMap<String,String> rowBuilder)
      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

      public Database getDatabase()
      Retrieves the database associated with the table.
      Returns:
      The Database object representing the database associated with the table.
    • getRowById

      public Row getRowById(int id)
      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

      public Row getLastestRow()
      Retrieves the latest row from the table.
      Returns:
      A Row object representing the latest row.
    • getOldestRow

      public 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

      public Row getRandomRow()
      Retrieves a random row from the table.
      Returns:
      A Row object representing a randomly selected row from the table.