Class HomeViewModel

java.lang.Object
com.cryptoalgo.oursql.model.HomeViewModel

public class HomeViewModel extends Object
Over-engineered database connection, loading and updating class. Serves as a view model for Home
  • Property Summary

    Properties
    Type
    Property
    Description
    javafx.beans.property.ReadOnlyStringProperty
    Get an observable ReadOnlyStringProperty for the current status.
    javafx.beans.property.ReadOnlyStringProperty
    Get an observable ReadOnlyStringProperty for the selected table.
    javafx.beans.property.ReadOnlyObjectProperty<javafx.scene.paint.Color>
    Get an observable ReadOnlyStringProperty for the current status background.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final javafx.collections.ObservableList<String>
    Columns of the current selected table
    final javafx.collections.ObservableList<javafx.collections.ObservableList<Container<?>>>
    Rows of the current selected table
    final javafx.collections.ObservableList<String>
    Columns of the current selected table
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Add a password to the cache manually, for example right after a cluster is created.
    void
    addCluster(@NotNull Cluster c)
    Add a specified cluster, will fire listeners if any are registered
    void
    attemptEdit(@NotNull String col, @NotNull String ctid, @Nullable String nv)
    Attempt to edit a field in a table
    void
    createTable(@NotNull Cluster cluster, @NotNull String table)
    Create a table in a cluster
    void
    Remove a row in the current table given a ctid
    javafx.beans.property.ReadOnlyStringProperty
    Get an observable ReadOnlyStringProperty for the current status.
    void
    Delete a table from a cluster
    void
    Convenience method to mark a job started by calling setStatusJob(String status) as successfully completed
    void
    Mark a job started by calling setStatusJob(String status) as completed.
    int
    indexOf(@NotNull String clusterID)
    Get the index of the cluster with the specified ID.
    void
    insertColumn(@NotNull String name, @NotNull String type)
    Inserts a column into the currently selected table.
    void
    Inserts a new row into the current table with null values for each column
    void
    Load persisted clusters.
    void
    newTableSelection(Cluster cluster, String table)
    Select and load a new table
    void
    removeCluster(@NotNull Integer idx)
    Remove a cluster at the specified index
    void
    removeCluster(@NotNull String clusterID)
    Remove the cluster with the specified ID
    @Nullable javafx.collections.ObservableList<String>
    Request to load the tables of a particular database cluster.
    javafx.util.Pair<javafx.util.Pair<List<String>,javafx.collections.ObservableList<javafx.collections.ObservableList<Container<?>>>>,Integer>
    Run a SQL query and return the resultSet
    javafx.beans.property.ReadOnlyStringProperty
    Get an observable ReadOnlyStringProperty for the selected table.
    void
    Register a new listener to be called when cluster(s) are added
    void
    Register a new listener to be called when cluster(s) are removed
    setStatusJob(String newStatus)
    Create a job to display its status
    javafx.beans.property.ReadOnlyObjectProperty<javafx.scene.paint.Color>
    Get an observable ReadOnlyStringProperty for the current status background.
    boolean
    tablesCached(String forCluster)
    Check if the tables for a particular cluster are present in the table cache.
    void
    Fetch the columns and rows of the currently selected table

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Property Details

    • selectedTable

      public javafx.beans.property.ReadOnlyStringProperty selectedTableProperty
      Get an observable ReadOnlyStringProperty for the selected table.
      Returns:
      Read only observable property for the selected table. Might be null if no table is selected.
    • displayedStatus

      public javafx.beans.property.ReadOnlyStringProperty displayedStatusProperty
      Get an observable ReadOnlyStringProperty for the current status.
      Returns:
      Read only observable property for the current status.
    • statusBg

      public javafx.beans.property.ReadOnlyObjectProperty<javafx.scene.paint.Color> statusBgProperty
      Get an observable ReadOnlyStringProperty for the current status background. Implementations might choose to transition changes in this property.
      Returns:
      Read only observable property for the requested status background.
  • Field Details

    • tableColumns

      public final javafx.collections.ObservableList<String> tableColumns
      Columns of the current selected table
    • columnTypes

      public final javafx.collections.ObservableList<String> columnTypes
      Columns of the current selected table
    • rows

      public final javafx.collections.ObservableList<javafx.collections.ObservableList<Container<?>>> rows
      Rows of the current selected table
  • Constructor Details

    • HomeViewModel

      public HomeViewModel()
  • Method Details

    • selectedTableProperty

      public javafx.beans.property.ReadOnlyStringProperty selectedTableProperty()
      Get an observable ReadOnlyStringProperty for the selected table.
      Returns:
      Read only observable property for the selected table. Might be null if no table is selected.
    • displayedStatusProperty

      public javafx.beans.property.ReadOnlyStringProperty displayedStatusProperty()
      Get an observable ReadOnlyStringProperty for the current status.
      Returns:
      Read only observable property for the current status.
    • statusBgProperty

      public javafx.beans.property.ReadOnlyObjectProperty<javafx.scene.paint.Color> statusBgProperty()
      Get an observable ReadOnlyStringProperty for the current status background. Implementations might choose to transition changes in this property.
      Returns:
      Read only observable property for the requested status background.
    • requestTables

      @Nullable public @Nullable javafx.collections.ObservableList<String> requestTables(Cluster cluster) throws NoSuchElementException, SQLException
      Request to load the tables of a particular database cluster. Call again with the same cluster ID to refresh clusters.
      Parameters:
      cluster - Cluster to fetch tables of
      Returns:
      An ObservableList of tables for this cluster. Implementations can bind to this ObservableList to be notified of modifications to tables in this cluster (currently only occurs when this method is called again to refresh tables).
      Throws:
      NoSuchElementException - If the cluster ID doesn't exist
      SQLException - If the underlying JDBC driver threw an exception during the creation of a connection or during a query.
    • tablesCached

      public boolean tablesCached(String forCluster)
      Check if the tables for a particular cluster are present in the table cache.
      Parameters:
      forCluster - Cluster ID to check for cached tables
      Returns:
      true if tables of the cluster are already present in the cache
    • newTableSelection

      public void newTableSelection(Cluster cluster, String table)
      Select and load a new table
      Parameters:
      cluster - Cluster to load table from
      table - Table name to load
    • updateTable

      public void updateTable()
      Fetch the columns and rows of the currently selected table
    • runQuery

      public javafx.util.Pair<javafx.util.Pair<List<String>,javafx.collections.ObservableList<javafx.collections.ObservableList<Container<?>>>>,Integer> runQuery(String query) throws SQLException
      Run a SQL query and return the resultSet
      Parameters:
      query - SQL query to run
      Returns:
      A pair containing a pair of the column names and types and a list of rows, and the number of results updated if the operation was an update operation or the length of the result set if the statement returned one
      Throws:
      SQLException - If the statement failed to execute
    • attemptEdit

      public void attemptEdit(@NotNull @NotNull String col, @NotNull @NotNull String ctid, @Nullable @Nullable String nv) throws SQLException
      Attempt to edit a field in a table
      Parameters:
      col - Column name of field to edit
      ctid - TID of row to edit
      nv - New value to update field with (could be null)
      Throws:
      SQLException - If editing failed (JDBC driver threw an exception while executing update statement)
    • insertRow

      public void insertRow()
      Inserts a new row into the current table with null values for each column
    • deleteRow

      public void deleteRow(String ctid)
      Remove a row in the current table given a ctid
      Parameters:
      ctid - CTID of row to remove
    • insertColumn

      public void insertColumn(@NotNull @NotNull String name, @NotNull @NotNull String type)
      Inserts a column into the currently selected table.
    • createTable

      public void createTable(@NotNull @NotNull Cluster cluster, @NotNull @NotNull String table)
      Create a table in a cluster
      Parameters:
      cluster - Cluster to create table in
      table - Name of new table to create
    • dropTable

      public void dropTable()
      Delete a table from a cluster
    • loadClusters

      public void loadClusters()
      Load persisted clusters. Listeners registered through setOnNewCluster(BiConsumer callback) will be notified if any new clusters were loaded.
    • setOnNewCluster

      public void setOnNewCluster(BiConsumer<Cluster,Integer> cb)
      Register a new listener to be called when cluster(s) are added
      Parameters:
      cb - Consumer callback to be call when a new cluster is added
    • setOnRemoveCluster

      public void setOnRemoveCluster(Consumer<Integer> cb)
      Register a new listener to be called when cluster(s) are removed
      Parameters:
      cb - Consumer callback to be called when a cluster is removed
    • indexOf

      public int indexOf(@NotNull @NotNull String clusterID)
      Get the index of the cluster with the specified ID.
      Parameters:
      clusterID - ID to get index of
      Returns:
      Index of cluster with specified ID, -1 if not found
    • addCluster

      public void addCluster(@NotNull @NotNull Cluster c)
      Add a specified cluster, will fire listeners if any are registered
      Parameters:
      c - Cluster to add
    • removeCluster

      public void removeCluster(@NotNull @NotNull String clusterID) throws NoSuchElementException
      Remove the cluster with the specified ID
      Parameters:
      clusterID - ID of cluster to remove
      Throws:
      NoSuchElementException - If no cluster with specified ID exists
      See Also:
    • removeCluster

      public void removeCluster(@NotNull @NotNull Integer idx)
      Remove a cluster at the specified index
      Parameters:
      idx - Index to remove cluster at
    • setStatusJob

      public String setStatusJob(String newStatus)
      Create a job to display its status
      Parameters:
      newStatus - Name or description of new job
      Returns:
      ID of job, can be passed to finishStatusJob(String id) or finishStatusJob(String id, String err) upon job completion
    • finishStatusJob

      public void finishStatusJob(String id)
      Convenience method to mark a job started by calling setStatusJob(String status) as successfully completed
      Parameters:
      id - ID of job, returned by setStatusJob(String status)
      See Also:
    • finishStatusJob

      public void finishStatusJob(String id, String err)
      Mark a job started by calling setStatusJob(String status) as completed.
      Parameters:
      id - ID of job, returned by setStatusJob(String status)
      err - If the job failed, the exception string to display (null if no exception)
      See Also:
    • addCachedPassword

      public static void addCachedPassword(String clusterID, String pw)
      Add a password to the cache manually, for example right after a cluster is created.

      Note: No validation is done to ensure the password is valid etc.

      Parameters:
      clusterID - ID of cluster this password is for
      pw - Password to add to cache