Package mgui.datasources
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
-
Nested Class Summary
Nested classes/interfaces inherited from interface mgui.interfaces.xml.XMLObject
XMLObject.XMLEncoding, XMLObject.XMLType -
Field Summary
Fields Modifier and Type Field Description protected DataConnectionconnprotected java.sql.Connectionconnectionprotected booleanisConnectedprotected java.util.ArrayList<DataSourceListener>listenersprotected java.util.ArrayList<DataQuery>queriesprotected DataTableSettableSetprotected java.util.ArrayList<DataTable>temp_tablesprotected InterfaceWorkspaceworkspaceFields inherited from class mgui.interfaces.AbstractInterfaceObject
isDestroyed, tree_nodes -
Constructor Summary
Constructors Constructor Description DataSource()DataSource(java.lang.String name)DataSource(DataConnection dc) -
Method Summary
Modifier and Type Method Description booleanaddDataField(DataTable table, DataField field)Attempts to adds a field to the specified table in this data source.booleanaddDataQuery(DataQuery query)Adds an SQL query to this data source.booleanaddDataTable(DataTable thisTable)Attempt to add a table to data source.voidaddListener(DataSourceListener l)booleanaddTempTable(DataTable thisTable)Attempts to add a temporary table to data source Returns true if successful.java.lang.Objectclone()booleanconnect()Attempts to connect to a data source with the given parameters.booleancreate()Create a new data source with the given connection parameters.DataTablecreateTempTable(java.lang.String name)Creates and returns a temporary table, and adds it to this data source's list of temp tables.booleandelete()Deletes an existing data source with the given connection parameters.voiddestroy()Destroy this object (prepare it to be removed from memory)booleandisconnect()Attempts to disconnect from the connected data source.voidexecuteStatement(java.lang.String SQL_statement)Attempts to executeSQL_statementon this data sourcebooleanexecuteUpdate(DataQuery query)Attempts to execute the given update query.java.sql.ResultSetgetColumnsFromMetaData(java.lang.String table_name)Returns a list of columns from this DataSource's metadata, for the specified table.protected InterfaceTreeNodegetConnectedTreeNode()DataConnectiongetConnection()javax.swing.ImageIcongetConnectionIcon()java.util.ArrayList<DataQuery>getDataQueries()Returns a list of the queries associated with this data source.DataQuerygetDataQuery(java.lang.String query)DataSourceDrivergetDataSourceDriver()Returns theDataSourceDriverassociated with this data source.java.lang.StringgetDTD()Returns the Data Type Declaration (DTD) for this object's XML representationstatic javax.swing.IcongetIcon()java.sql.ResultSetgetKeysFromMetaData(java.lang.String table)Returns a set of primary keys for the specified tablejava.lang.StringgetLocalName()Returns the local name associated with this XML object.java.lang.StringgetName()Gets the name for this object.javax.swing.IcongetObjectIcon()Returns theIconassociated with this object.InterfacePopupMenugetPopupMenu()Produces and returns a popup menu for this object.InterfacePopupMenugetPopupMenu(java.util.List<java.lang.Object> selected)Produces and returns a popup menu for this object.DataRecordSetgetRecordSet(DataSourceItem item)Returns aDataRecordSetobject accessing the specified table in this data source.java.sql.ResultSetgetResultSet(java.lang.String SQLStr)Returns a result set from the given SQL statement.java.lang.StringgetShortXML(int tab)Returns a short XML representation of this object.java.lang.StringgetSourceName()Returns the name of the underlying database (as contained in its URL).DataTableSetgetTableSet()Returns the set of tables for this data source.java.sql.ResultSetgetTablesFromMetaData()Returns a list of tables retrieved from this data source's meta data.java.lang.StringgetTreeLabel()Returns the label text to appear in a tree node.InterfaceWorkspacegetWorkspace()java.lang.StringgetXML()Returns this object's XML representation as a single string.java.lang.StringgetXML(int tab)Returns this object's XML representation as a single string.java.lang.StringgetXMLSchema()Returns the XML schema for this object's XML representationvoidhandlePopupEvent(java.awt.event.ActionEvent e)Handles an event on this object's popup menu.voidhandleXMLElementEnd(java.lang.String localName)Handles the end of an XML element.voidhandleXMLElementStart(java.lang.String localName, org.xml.sax.Attributes attributes, XMLObject.XMLType type)Handles the start of an XML element.voidhandleXMLString(java.lang.String s)Handles a string within an XML element.booleanisConnected()protected booleanopenDriver(java.lang.String driver)booleanremoveDataField(DataTable table, DataField field)Attempts to remove a field from the specified table in this data source.voidremoveDataQuery(DataQuery query)Remove an SQL query from this data source.booleanremoveDataTable(DataTable thisTable)Removes the specified table from this data source.voidremoveListener(DataSourceListener l)booleanremoveTempTables()voidsetConnection(DataConnection dc)Sets this data source's connection parameters.booleansetFromDataSource(DataSource ds)Sets this data source from an existing source.voidsetName(java.lang.String name)Sets the name for this object.booleansetTableSet()Refreshes the list of tables from the data source.booleansetTableSet(boolean overwrite_existing)Refreshes the list of tables from the data source.voidsetTableSet(DataTableSet table_set)Sets the tables for this data source from a predefined set.voidsetTreeNode(InterfaceTreeNode treeNode)Sets the children for this node'sInterfaceTreeNode.voidsetWorkspace(InterfaceWorkspace workspace)voidshowPopupMenu(java.awt.event.MouseEvent e)Shows a popup menu at the point of the givenMouseEvent.java.lang.StringtoString()voidwriteXML(int tab, java.io.Writer writer)Writes the XML representation of this object to file.voidwriteXML(int tab, java.io.Writer writer, ProgressUpdater progress_bar)Writes the XML representation of this object to file, asXMLType.Normal.voidwriteXML(int tab, java.io.Writer writer, XMLOutputOptions options, ProgressUpdater progress_bar)Writes the XML representation of this object to file.Methods inherited from class mgui.interfaces.AbstractInterfaceObject
clean, isDestroyed, issueTreeNode, updateTreeNodesMethods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Field Details
-
tableSet
-
conn
-
isConnected
protected boolean isConnected -
connection
protected java.sql.Connection connection -
queries
-
listeners
-
temp_tables
-
workspace
-
-
Constructor Details
-
DataSource
public DataSource() -
DataSource
public DataSource(java.lang.String name) -
DataSource
-
-
Method Details
-
getConnection
-
setWorkspace
-
getWorkspace
-
getDataQueries
Returns a list of the queries associated with this data source.- Returns:
-
getDataQuery
-
isConnected
public boolean isConnected() -
getIcon
public static javax.swing.Icon getIcon() -
getObjectIcon
public javax.swing.Icon getObjectIcon()Description copied from interface:IconObjectReturns theIconassociated with this object.- Specified by:
getObjectIconin interfaceIconObject- Returns:
-
setConnection
Sets this data source's connection parameters.- Parameters:
dc- TheDataConnectionspecifying the connection parameters- See Also:
DataConnection
-
openDriver
protected boolean openDriver(java.lang.String driver) -
destroy
public void destroy()Description copied from interface:InterfaceObjectDestroy this object (prepare it to be removed from memory)- Specified by:
destroyin interfaceInterfaceObject- Overrides:
destroyin classAbstractInterfaceObject
-
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:
trueif successful,falseotherwise.
-
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 theirDataSourceDriverclass.- Returns:
trueif successful,falseotherwise.
-
connect
Attempts to connect to a data source with the given parameters.- Returns:
trueif successful;falseotherwise.- 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:
trueif successful;falseotherwise.
-
addTempTable
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
Returns theDataSourceDriverassociated with this data source.- Returns:
-
addDataTable
Attempt to add a table to data source. Also adds this table to the table set.- Parameters:
DataTable- thisTable- Returns:
trueif successful;falseotherwise.
-
addDataQuery
Adds an SQL query to this data source.- Parameters:
query-- Returns:
falseif a query by this name already exists
-
removeDataQuery
Remove an SQL query from this data source.- Parameters:
query-
-
removeDataTable
Removes the specified table from this data source.- Parameters:
thisTable-- Returns:
-
addDataField
Attempts to adds a field to the specified table in this data source.- Parameters:
thisTable-thisField-- Returns:
trueif successful;falseotherwise.
-
removeDataField
Attempts to remove a field from the specified table in this data source.- Parameters:
thisTable-thisField-- Returns:
trueif successful;falseotherwise.
-
getResultSet
Returns a result set from the given SQL statement.- Parameters:
SQLStr-- Returns:
- the result set
- Throws:
DataSourceException
-
executeUpdate
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.SQLExceptionAttempts to executeSQL_statementon this data source- Parameters:
SQL_statement-- Throws:
java.sql.SQLException
-
getRecordSet
Returns aDataRecordSetobject accessing the specified table in this data source.- Parameters:
table-- Returns:
- Throws:
DataSourceException
-
getTableSet
Returns the set of tables for this data source. If none have been set, attempts to retrieve them from the source. UsesetTableSet()to refresh this list from the source.- Returns:
- the set of tables, or
nullif none can be retrieved - Throws:
DataSourceException
-
setTableSet
Refreshes the list of tables from the data source.- Returns:
trueif successful;falseotherwise.- Throws:
DataSourceException- if this source is not connected or an exception is encountered while connecting
-
setTableSet
Refreshes the list of tables from the data source.- Parameters:
keep_existing- Iftrue, 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:
trueif successful;falseotherwise.- Throws:
DataSourceException- if this source is not connected or an exception is encountered while connecting
-
setTableSet
Sets the tables for this data source from a predefined set.- Parameters:
table_set-
-
createTempTable
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
Description copied from interface:TreeObjectSets the children for this node'sInterfaceTreeNode.- Specified by:
setTreeNodein interfaceTreeObject- Overrides:
setTreeNodein classAbstractInterfaceObject
-
getConnectedTreeNode
-
getConnectionIcon
public javax.swing.ImageIcon getConnectionIcon() -
getTreeLabel
public java.lang.String getTreeLabel()Description copied from interface:TreeObjectReturns the label text to appear in a tree node.- Specified by:
getTreeLabelin interfaceTreeObject- Overrides:
getTreeLabelin classAbstractInterfaceObject- Returns:
-
getName
public java.lang.String getName()Description copied from interface:NamedObjectGets the name for this object.- Specified by:
getNamein interfaceNamedObject- Overrides:
getNamein classAbstractInterfaceObject- Returns:
-
setName
public void setName(java.lang.String name)Description copied from interface:NamedObjectSets the name for this object.- Specified by:
setNamein interfaceNamedObject- Overrides:
setNamein classAbstractInterfaceObject
-
getSourceName
public java.lang.String getSourceName()Returns the name of the underlying database (as contained in its URL).- Returns:
- the source name
-
addListener
-
removeListener
-
toString
public java.lang.String toString()- Overrides:
toStringin classjava.lang.Object
-
clone
public java.lang.Object clone()- Overrides:
clonein classjava.lang.Object
-
getPopupMenu
Description copied from interface:PopupMenuObjectProduces and returns a popup menu for this object.- Specified by:
getPopupMenuin interfacePopupMenuObject- Returns:
-
getPopupMenu
Description copied from interface:PopupMenuObjectProduces and returns a popup menu for this object. Allows an associated list of objects to be passed as an argument.- Specified by:
getPopupMenuin interfacePopupMenuObject- Returns:
-
handlePopupEvent
public void handlePopupEvent(java.awt.event.ActionEvent e)Description copied from interface:PopupMenuObjectHandles an event on this object's popup menu.- Specified by:
handlePopupEventin interfacePopupMenuObject
-
showPopupMenu
public void showPopupMenu(java.awt.event.MouseEvent e)Description copied from interface:PopupMenuObjectShows a popup menu at the point of the givenMouseEvent.- Specified by:
showPopupMenuin interfacePopupMenuObject
-
setFromDataSource
Sets this data source from an existing source. Attempts to disconnect if currently connected.- Parameters:
ds-- Returns:
trueif successful,falseotherwise.
-
getDTD
public java.lang.String getDTD()Description copied from interface:XMLObjectReturns the Data Type Declaration (DTD) for this object's XML representationSee http://en.wikipedia.org/wiki/Document_Type_Definition for a description.
-
getXMLSchema
public java.lang.String getXMLSchema()Description copied from interface:XMLObjectReturns the XML schema for this object's XML representation- Specified by:
getXMLSchemain interfaceXMLObject- Returns:
-
getXML
public java.lang.String getXML()Description copied from interface:XMLObjectReturns 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 theXMLObject.writeXML(int,java.io.Writer,mgui.io.standard.xml.XMLOutputOptions,mgui.interfaces.ProgressUpdater)functions to write larger objects to file. -
getXML
public java.lang.String getXML(int tab)Description copied from interface:XMLObjectReturns 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 theXMLObject.writeXML(int,java.io.Writer,mgui.io.standard.xml.XMLOutputOptions,mgui.interfaces.ProgressUpdater)functions to write larger objects to file. -
handleXMLElementStart
public void handleXMLElementStart(java.lang.String localName, org.xml.sax.Attributes attributes, XMLObject.XMLType type)Description copied from interface:XMLObjectHandles the start of an XML element.- Specified by:
handleXMLElementStartin interfaceXMLObject- Parameters:
localName- Local name of the elementattributes- Set of element attributestype- TheXMLTypeof this element
-
handleXMLElementEnd
public void handleXMLElementEnd(java.lang.String localName)Description copied from interface:XMLObjectHandles the end of an XML element.- Specified by:
handleXMLElementEndin interfaceXMLObject- Parameters:
localName- Local name of the element
-
handleXMLString
public void handleXMLString(java.lang.String s)Description copied from interface:XMLObjectHandles a string within an XML element.- Specified by:
handleXMLStringin interfaceXMLObject- Parameters:
s- String to handle
-
getLocalName
public java.lang.String getLocalName()Description copied from interface:XMLObjectReturns the local name associated with this XML object.- Specified by:
getLocalNamein interfaceXMLObject- Returns:
-
writeXML
public void writeXML(int tab, java.io.Writer writer, XMLOutputOptions options, ProgressUpdater progress_bar) throws java.io.IOExceptionDescription copied from interface:XMLObjectWrites 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 ofXMLFormat.Asciiwill be used. -
writeXML
public void writeXML(int tab, java.io.Writer writer, ProgressUpdater progress_bar) throws java.io.IOExceptionDescription copied from interface:XMLObjectWrites the XML representation of this object to file, asXMLType.Normal. The default format ofXMLFormat.Asciiwill be used. The basic contract for this method is that it should not write a newline character at its start or end. -
writeXML
public void writeXML(int tab, java.io.Writer writer) throws java.io.IOExceptionDescription copied from interface:XMLObjectWrites 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 ofXMLFormat.Asciiwill be used. -
getShortXML
public java.lang.String getShortXML(int tab)Description copied from interface:XMLObjectReturns a short XML representation of this object.- Specified by:
getShortXMLin interfaceXMLObject- Returns:
-