Class DataSource

java.lang.Object
mgui.interfaces.AbstractInterfaceObject
mgui.datasources.DataSource
All Implemented Interfaces:
java.lang.Cloneable, InterfaceObject, PopupMenuObject, NamedObject, TreeObject, CleanableObject, XMLObject, IconObject

public class DataSource
extends AbstractInterfaceObject
implements java.lang.Cloneable, IconObject, PopupMenuObject, XMLObject
Acts as a port into the JDBC interface. Specific data source objects should extend this class's basic implementations. In general, communicates to database drivers (e.g. LDBC?) to retrieve, modify, and store data using SQL statements.

Can also:

  • Build a DataTableSet from meta data
  • Create/delete databases
Since:
1.0
Version:
1.0
Author:
Andrew Reid
  • Field Details

    • tableSet

      protected DataTableSet tableSet
    • conn

      protected DataConnection conn
    • isConnected

      protected boolean isConnected
    • connection

      protected java.sql.Connection connection
    • queries

      protected java.util.ArrayList<DataQuery> queries
    • listeners

      protected java.util.ArrayList<DataSourceListener> listeners
    • temp_tables

      protected java.util.ArrayList<DataTable> temp_tables
    • workspace

      protected InterfaceWorkspace workspace
  • Constructor Details

    • DataSource

      public DataSource()
    • DataSource

      public DataSource​(java.lang.String name)
    • DataSource

      public DataSource​(DataConnection dc)
  • Method Details

    • getConnection

      public DataConnection getConnection()
    • setWorkspace

      public void setWorkspace​(InterfaceWorkspace workspace)
    • getWorkspace

      public InterfaceWorkspace getWorkspace()
    • getDataQueries

      public java.util.ArrayList<DataQuery> getDataQueries()
      Returns a list of the queries associated with this data source.
      Returns:
    • getDataQuery

      public DataQuery getDataQuery​(java.lang.String query)
    • isConnected

      public boolean isConnected()
    • getIcon

      public static javax.swing.Icon getIcon()
    • getObjectIcon

      public javax.swing.Icon getObjectIcon()
      Description copied from interface: IconObject
      Returns the Icon associated with this object.
      Specified by:
      getObjectIcon in interface IconObject
      Returns:
    • setConnection

      public void setConnection​(DataConnection dc)
      Sets this data source's connection parameters.
      Parameters:
      dc - The DataConnection specifying the connection parameters
      See Also:
      DataConnection
    • openDriver

      protected boolean openDriver​(java.lang.String driver)
    • destroy

      public void destroy()
      Description copied from interface: InterfaceObject
      Destroy this object (prepare it to be removed from memory)
      Specified by:
      destroy in interface InterfaceObject
      Overrides:
      destroy in class AbstractInterfaceObject
    • create

      public boolean create()
      Create a new data source with the given connection parameters. This method will only work if the specified driver/engine supports the creation of new databases.
      Returns:
      true if successful, false otherwise.
    • delete

      public boolean delete()
      Deletes an existing data source with the given connection parameters. This method will only work if the specified driver/engine supports the deletion of databases. TODO: transfer driver-specific code to their DataSourceDriver class.
      Returns:
      true if successful, false otherwise.
    • connect

      public boolean connect() throws DataSourceException
      Attempts to connect to a data source with the given parameters.
      Returns:
      true if successful; false otherwise.
      Throws:
      DataSourceException
    • disconnect

      public boolean disconnect()
      Attempts to disconnect from the connected data source. If successful this will also remove all temporary tables from the data source.
      Returns:
      true if successful; false otherwise.
    • addTempTable

      public boolean addTempTable​(DataTable thisTable)
      Attempts to add a temporary table to data source Returns true if successful. Temp tables can be cleared using removeTempTables() or disconnect()
      Parameters:
      thisTable -
      Returns:
    • removeTempTables

      public boolean removeTempTables()
    • getDataSourceDriver

      public DataSourceDriver getDataSourceDriver()
      Returns the DataSourceDriver associated with this data source.
      Returns:
    • addDataTable

      public boolean addDataTable​(DataTable thisTable)
      Attempt to add a table to data source. Also adds this table to the table set.
      Parameters:
      DataTable - thisTable
      Returns:
      true if successful; false otherwise.
    • addDataQuery

      public boolean addDataQuery​(DataQuery query)
      Adds an SQL query to this data source.
      Parameters:
      query -
      Returns:
      false if a query by this name already exists
    • removeDataQuery

      public void removeDataQuery​(DataQuery query)
      Remove an SQL query from this data source.
      Parameters:
      query -
    • removeDataTable

      public boolean removeDataTable​(DataTable thisTable)
      Removes the specified table from this data source.
      Parameters:
      thisTable -
      Returns:
    • addDataField

      public boolean addDataField​(DataTable table, DataField field)
      Attempts to adds a field to the specified table in this data source.
      Parameters:
      thisTable -
      thisField -
      Returns:
      true if successful; false otherwise.
    • removeDataField

      public boolean removeDataField​(DataTable table, DataField field)
      Attempts to remove a field from the specified table in this data source.
      Parameters:
      thisTable -
      thisField -
      Returns:
      true if successful; false otherwise.
    • getResultSet

      public java.sql.ResultSet getResultSet​(java.lang.String SQLStr) throws DataSourceException
      Returns a result set from the given SQL statement.
      Parameters:
      SQLStr -
      Returns:
      the result set
      Throws:
      DataSourceException
    • executeUpdate

      public boolean executeUpdate​(DataQuery query) throws DataSourceException
      Attempts to execute the given update query. This command should be used, e.g., for create table, insert, or delete queries.
      Parameters:
      query -
      Returns:
      Success of execution
      Throws:
      DataSourceException - If something goes wrong...
    • executeStatement

      public void executeStatement​(java.lang.String SQL_statement) throws java.sql.SQLException
      Attempts to execute SQL_statement on this data source
      Parameters:
      SQL_statement -
      Throws:
      java.sql.SQLException
    • getRecordSet

      public DataRecordSet getRecordSet​(DataSourceItem item) throws DataSourceException
      Returns a DataRecordSet object accessing the specified table in this data source.
      Parameters:
      table -
      Returns:
      Throws:
      DataSourceException
    • getTableSet

      public DataTableSet getTableSet() throws DataSourceException
      Returns the set of tables for this data source. If none have been set, attempts to retrieve them from the source. Use setTableSet() to refresh this list from the source.
      Returns:
      the set of tables, or null if none can be retrieved
      Throws:
      DataSourceException
    • setTableSet

      public boolean setTableSet() throws DataSourceException
      Refreshes the list of tables from the data source.
      Returns:
      true if successful; false otherwise.
      Throws:
      DataSourceException - if this source is not connected or an exception is encountered while connecting
    • setTableSet

      public boolean setTableSet​(boolean overwrite_existing) throws DataSourceException
      Refreshes the list of tables from the data source.
      Parameters:
      keep_existing - If true, only tables which do not already exist in the table set will be added; otherwise, these tables will be overwritten with the metadata versions.
      Returns:
      true if successful; false otherwise.
      Throws:
      DataSourceException - if this source is not connected or an exception is encountered while connecting
    • setTableSet

      public void setTableSet​(DataTableSet table_set)
      Sets the tables for this data source from a predefined set.
      Parameters:
      table_set -
    • createTempTable

      public DataTable createTempTable​(java.lang.String name) throws DataSourceException
      Creates and returns a temporary table, and adds it to this data source's list of temp tables. Temporary tables will be removed from the data source whenever disconnect() or clearTempTables() is called.
      Parameters:
      name -
      Returns:
      Throws:
      DataSourceException
    • getTablesFromMetaData

      public java.sql.ResultSet getTablesFromMetaData()
      Returns a list of tables retrieved from this data source's meta data.
      Returns:
      ResultSet as described in DatabaseMetaData.getTables()
      See Also:
      DatabaseMetaData
    • getColumnsFromMetaData

      public java.sql.ResultSet getColumnsFromMetaData​(java.lang.String table_name)
      Returns a list of columns from this DataSource's metadata, for the specified table.
      Parameters:
      table_name -
      Returns:
    • getKeysFromMetaData

      public java.sql.ResultSet getKeysFromMetaData​(java.lang.String table)
      Returns a set of primary keys for the specified table
      Returns:
      ResultSet as described in DatabaseMetaData.getPrimaryKeys()
      See Also:
      DatabaseMetaData
    • setTreeNode

      public void setTreeNode​(InterfaceTreeNode treeNode)
      Description copied from interface: TreeObject
      Sets the children for this node's InterfaceTreeNode.
      Specified by:
      setTreeNode in interface TreeObject
      Overrides:
      setTreeNode in class AbstractInterfaceObject
    • getConnectedTreeNode

      protected InterfaceTreeNode getConnectedTreeNode()
    • getConnectionIcon

      public javax.swing.ImageIcon getConnectionIcon()
    • getTreeLabel

      public java.lang.String getTreeLabel()
      Description copied from interface: TreeObject
      Returns the label text to appear in a tree node.
      Specified by:
      getTreeLabel in interface TreeObject
      Overrides:
      getTreeLabel in class AbstractInterfaceObject
      Returns:
    • getName

      public java.lang.String getName()
      Description copied from interface: NamedObject
      Gets the name for this object.
      Specified by:
      getName in interface NamedObject
      Overrides:
      getName in class AbstractInterfaceObject
      Returns:
    • setName

      public void setName​(java.lang.String name)
      Description copied from interface: NamedObject
      Sets the name for this object.
      Specified by:
      setName in interface NamedObject
      Overrides:
      setName in class AbstractInterfaceObject
    • getSourceName

      public java.lang.String getSourceName()
      Returns the name of the underlying database (as contained in its URL).
      Returns:
      the source name
    • addListener

      public void addListener​(DataSourceListener l)
    • removeListener

      public void removeListener​(DataSourceListener l)
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object
    • clone

      public java.lang.Object clone()
      Overrides:
      clone in class java.lang.Object
    • getPopupMenu

      public InterfacePopupMenu getPopupMenu()
      Description copied from interface: PopupMenuObject
      Produces and returns a popup menu for this object.
      Specified by:
      getPopupMenu in interface PopupMenuObject
      Returns:
    • getPopupMenu

      public InterfacePopupMenu getPopupMenu​(java.util.List<java.lang.Object> selected)
      Description copied from interface: PopupMenuObject
      Produces and returns a popup menu for this object. Allows an associated list of objects to be passed as an argument.
      Specified by:
      getPopupMenu in interface PopupMenuObject
      Returns:
    • handlePopupEvent

      public void handlePopupEvent​(java.awt.event.ActionEvent e)
      Description copied from interface: PopupMenuObject
      Handles an event on this object's popup menu.
      Specified by:
      handlePopupEvent in interface PopupMenuObject
    • showPopupMenu

      public void showPopupMenu​(java.awt.event.MouseEvent e)
      Description copied from interface: PopupMenuObject
      Shows a popup menu at the point of the given MouseEvent.
      Specified by:
      showPopupMenu in interface PopupMenuObject
    • setFromDataSource

      public boolean setFromDataSource​(DataSource ds)
      Sets this data source from an existing source. Attempts to disconnect if currently connected.
      Parameters:
      ds -
      Returns:
      true if successful, false otherwise.
    • getDTD

      public java.lang.String getDTD()
      Description copied from interface: XMLObject
      Returns the Data Type Declaration (DTD) for this object's XML representation

      See http://en.wikipedia.org/wiki/Document_Type_Definition for a description.

      Specified by:
      getDTD in interface XMLObject
      Returns:
    • getXMLSchema

      public java.lang.String getXMLSchema()
      Description copied from interface: XMLObject
      Returns the XML schema for this object's XML representation

      See http://en.wikipedia.org/wiki/XML_schema.

      Specified by:
      getXMLSchema in interface XMLObject
      Returns:
    • getXML

      public java.lang.String getXML()
      Description copied from interface: XMLObject
      Returns this object's XML representation as a single string. NOTE: this is not feasible for larger objects and containers, thus may not be implemented for these objects. Use the XMLObject.writeXML(int,java.io.Writer,mgui.io.standard.xml.XMLOutputOptions,mgui.interfaces.ProgressUpdater) functions to write larger objects to file.
      Specified by:
      getXML in interface XMLObject
      Returns:
    • getXML

      public java.lang.String getXML​(int tab)
      Description copied from interface: XMLObject
      Returns this object's XML representation as a single string. NOTE: this is not feasible for larger objects and containers, thus may not be implemented for these objects. Use the XMLObject.writeXML(int,java.io.Writer,mgui.io.standard.xml.XMLOutputOptions,mgui.interfaces.ProgressUpdater) functions to write larger objects to file.
      Specified by:
      getXML in interface XMLObject
      Parameters:
      tab - The number of tabs to place before the opening XML wrapper
      Returns:
    • handleXMLElementStart

      public void handleXMLElementStart​(java.lang.String localName, org.xml.sax.Attributes attributes, XMLObject.XMLType type)
      Description copied from interface: XMLObject
      Handles the start of an XML element.
      Specified by:
      handleXMLElementStart in interface XMLObject
      Parameters:
      localName - Local name of the element
      attributes - Set of element attributes
      type - The XMLType of this element
    • handleXMLElementEnd

      public void handleXMLElementEnd​(java.lang.String localName)
      Description copied from interface: XMLObject
      Handles the end of an XML element.
      Specified by:
      handleXMLElementEnd in interface XMLObject
      Parameters:
      localName - Local name of the element
    • handleXMLString

      public void handleXMLString​(java.lang.String s)
      Description copied from interface: XMLObject
      Handles a string within an XML element.
      Specified by:
      handleXMLString in interface XMLObject
      Parameters:
      s - String to handle
    • getLocalName

      public java.lang.String getLocalName()
      Description copied from interface: XMLObject
      Returns the local name associated with this XML object.
      Specified by:
      getLocalName in interface XMLObject
      Returns:
    • writeXML

      public void writeXML​(int tab, java.io.Writer writer, XMLOutputOptions options, ProgressUpdater progress_bar) throws java.io.IOException
      Description copied from interface: XMLObject
      Writes the XML representation of this object to file. The basic contract for this method is that it should not write a newline character at its start or end. The default format of XMLFormat.Ascii will be used.
      Specified by:
      writeXML in interface XMLObject
      Parameters:
      tab - The number of tabs to place before the XML text
      writer - The writer
      options - XMLOutputOptions defining the write parameters
      progress_bar - Optional progress updater (may be null)
      Throws:
      java.io.IOException
    • writeXML

      public void writeXML​(int tab, java.io.Writer writer, ProgressUpdater progress_bar) throws java.io.IOException
      Description copied from interface: XMLObject
      Writes the XML representation of this object to file, as XMLType.Normal. The default format of XMLFormat.Ascii will be used. The basic contract for this method is that it should not write a newline character at its start or end.
      Specified by:
      writeXML in interface XMLObject
      Parameters:
      tab - The number of tabs to place before the XML text
      writer - The writer
      progress_bar - Optional progress updater (may be null)
      Throws:
      java.io.IOException
    • writeXML

      public void writeXML​(int tab, java.io.Writer writer) throws java.io.IOException
      Description copied from interface: XMLObject
      Writes the XML representation of this object to file. The basic contract for this method is that it should not write a newline character at its start or end. The default format of XMLFormat.Ascii will be used.
      Specified by:
      writeXML in interface XMLObject
      Parameters:
      tab - The number of tabs to place before the XML text
      writer - The writer
      Throws:
      java.io.IOException
    • getShortXML

      public java.lang.String getShortXML​(int tab)
      Description copied from interface: XMLObject
      Returns a short XML representation of this object.
      Specified by:
      getShortXML in interface XMLObject
      Returns: