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 DataConnection
conn
protected java.sql.Connection
connection
protected boolean
isConnected
protected java.util.ArrayList<DataSourceListener>
listeners
protected java.util.ArrayList<DataQuery>
queries
protected DataTableSet
tableSet
protected java.util.ArrayList<DataTable>
temp_tables
protected InterfaceWorkspace
workspace
Fields 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 boolean
addDataField(DataTable table, DataField field)
Attempts to adds a field to the specified table in this data source.boolean
addDataQuery(DataQuery query)
Adds an SQL query to this data source.boolean
addDataTable(DataTable thisTable)
Attempt to add a table to data source.void
addListener(DataSourceListener l)
boolean
addTempTable(DataTable thisTable)
Attempts to add a temporary table to data source Returns true if successful.java.lang.Object
clone()
boolean
connect()
Attempts to connect to a data source with the given parameters.boolean
create()
Create a new data source with the given connection parameters.DataTable
createTempTable(java.lang.String name)
Creates and returns a temporary table, and adds it to this data source's list of temp tables.boolean
delete()
Deletes an existing data source with the given connection parameters.void
destroy()
Destroy this object (prepare it to be removed from memory)boolean
disconnect()
Attempts to disconnect from the connected data source.void
executeStatement(java.lang.String SQL_statement)
Attempts to executeSQL_statement
on this data sourceboolean
executeUpdate(DataQuery query)
Attempts to execute the given update query.java.sql.ResultSet
getColumnsFromMetaData(java.lang.String table_name)
Returns a list of columns from this DataSource's metadata, for the specified table.protected InterfaceTreeNode
getConnectedTreeNode()
DataConnection
getConnection()
javax.swing.ImageIcon
getConnectionIcon()
java.util.ArrayList<DataQuery>
getDataQueries()
Returns a list of the queries associated with this data source.DataQuery
getDataQuery(java.lang.String query)
DataSourceDriver
getDataSourceDriver()
Returns theDataSourceDriver
associated with this data source.java.lang.String
getDTD()
Returns the Data Type Declaration (DTD) for this object's XML representationstatic javax.swing.Icon
getIcon()
java.sql.ResultSet
getKeysFromMetaData(java.lang.String table)
Returns a set of primary keys for the specified tablejava.lang.String
getLocalName()
Returns the local name associated with this XML object.java.lang.String
getName()
Gets the name for this object.javax.swing.Icon
getObjectIcon()
Returns theIcon
associated with this object.InterfacePopupMenu
getPopupMenu()
Produces and returns a popup menu for this object.InterfacePopupMenu
getPopupMenu(java.util.List<java.lang.Object> selected)
Produces and returns a popup menu for this object.DataRecordSet
getRecordSet(DataSourceItem item)
Returns aDataRecordSet
object accessing the specified table in this data source.java.sql.ResultSet
getResultSet(java.lang.String SQLStr)
Returns a result set from the given SQL statement.java.lang.String
getShortXML(int tab)
Returns a short XML representation of this object.java.lang.String
getSourceName()
Returns the name of the underlying database (as contained in its URL).DataTableSet
getTableSet()
Returns the set of tables for this data source.java.sql.ResultSet
getTablesFromMetaData()
Returns a list of tables retrieved from this data source's meta data.java.lang.String
getTreeLabel()
Returns the label text to appear in a tree node.InterfaceWorkspace
getWorkspace()
java.lang.String
getXML()
Returns this object's XML representation as a single string.java.lang.String
getXML(int tab)
Returns this object's XML representation as a single string.java.lang.String
getXMLSchema()
Returns the XML schema for this object's XML representationvoid
handlePopupEvent(java.awt.event.ActionEvent e)
Handles an event on this object's popup menu.void
handleXMLElementEnd(java.lang.String localName)
Handles the end of an XML element.void
handleXMLElementStart(java.lang.String localName, org.xml.sax.Attributes attributes, XMLObject.XMLType type)
Handles the start of an XML element.void
handleXMLString(java.lang.String s)
Handles a string within an XML element.boolean
isConnected()
protected boolean
openDriver(java.lang.String driver)
boolean
removeDataField(DataTable table, DataField field)
Attempts to remove a field from the specified table in this data source.void
removeDataQuery(DataQuery query)
Remove an SQL query from this data source.boolean
removeDataTable(DataTable thisTable)
Removes the specified table from this data source.void
removeListener(DataSourceListener l)
boolean
removeTempTables()
void
setConnection(DataConnection dc)
Sets this data source's connection parameters.boolean
setFromDataSource(DataSource ds)
Sets this data source from an existing source.void
setName(java.lang.String name)
Sets the name for this object.boolean
setTableSet()
Refreshes the list of tables from the data source.boolean
setTableSet(boolean overwrite_existing)
Refreshes the list of tables from the data source.void
setTableSet(DataTableSet table_set)
Sets the tables for this data source from a predefined set.void
setTreeNode(InterfaceTreeNode treeNode)
Sets the children for this node'sInterfaceTreeNode
.void
setWorkspace(InterfaceWorkspace workspace)
void
showPopupMenu(java.awt.event.MouseEvent e)
Shows a popup menu at the point of the givenMouseEvent
.java.lang.String
toString()
void
writeXML(int tab, java.io.Writer writer)
Writes the XML representation of this object to file.void
writeXML(int tab, java.io.Writer writer, ProgressUpdater progress_bar)
Writes the XML representation of this object to file, asXMLType.Normal
.void
writeXML(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, updateTreeNodes
Methods 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:IconObject
Returns theIcon
associated with this object.- Specified by:
getObjectIcon
in interfaceIconObject
- Returns:
-
setConnection
Sets this data source's connection parameters.- Parameters:
dc
- TheDataConnection
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 interfaceInterfaceObject
- Overrides:
destroy
in 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:
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 theirDataSourceDriver
class.- Returns:
true
if successful,false
otherwise.
-
connect
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
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 theDataSourceDriver
associated 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:
true
if successful;false
otherwise.
-
addDataQuery
Adds an SQL query to this data source.- Parameters:
query
-- Returns:
false
if 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:
true
if successful;false
otherwise.
-
removeDataField
Attempts to remove a field from the specified table in this data source.- Parameters:
thisTable
-thisField
-- Returns:
true
if successful;false
otherwise.
-
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_statement
on this data source- Parameters:
SQL_statement
-- Throws:
java.sql.SQLException
-
getRecordSet
Returns aDataRecordSet
object 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
null
if none can be retrieved - Throws:
DataSourceException
-
setTableSet
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
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:
true
if successful;false
otherwise.- 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:TreeObject
Sets the children for this node'sInterfaceTreeNode
.- Specified by:
setTreeNode
in interfaceTreeObject
- Overrides:
setTreeNode
in classAbstractInterfaceObject
-
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 interfaceTreeObject
- Overrides:
getTreeLabel
in classAbstractInterfaceObject
- Returns:
-
getName
public java.lang.String getName()Description copied from interface:NamedObject
Gets the name for this object.- Specified by:
getName
in interfaceNamedObject
- Overrides:
getName
in classAbstractInterfaceObject
- Returns:
-
setName
public void setName(java.lang.String name)Description copied from interface:NamedObject
Sets the name for this object.- Specified by:
setName
in interfaceNamedObject
- Overrides:
setName
in 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:
toString
in classjava.lang.Object
-
clone
public java.lang.Object clone()- Overrides:
clone
in classjava.lang.Object
-
getPopupMenu
Description copied from interface:PopupMenuObject
Produces and returns a popup menu for this object.- Specified by:
getPopupMenu
in interfacePopupMenuObject
- Returns:
-
getPopupMenu
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 interfacePopupMenuObject
- 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 interfacePopupMenuObject
-
showPopupMenu
public void showPopupMenu(java.awt.event.MouseEvent e)Description copied from interface:PopupMenuObject
Shows a popup menu at the point of the givenMouseEvent
.- Specified by:
showPopupMenu
in interfacePopupMenuObject
-
setFromDataSource
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 representationSee http://en.wikipedia.org/wiki/Document_Type_Definition for a description.
-
getXMLSchema
public java.lang.String getXMLSchema()Description copied from interface:XMLObject
Returns the XML schema for this object's XML representation- Specified by:
getXMLSchema
in interfaceXMLObject
- 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 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: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 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:XMLObject
Handles the start of an XML element.- Specified by:
handleXMLElementStart
in interfaceXMLObject
- Parameters:
localName
- Local name of the elementattributes
- Set of element attributestype
- TheXMLType
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 interfaceXMLObject
- 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 interfaceXMLObject
- 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 interfaceXMLObject
- Returns:
-
writeXML
public void writeXML(int tab, java.io.Writer writer, XMLOutputOptions options, ProgressUpdater progress_bar) throws java.io.IOExceptionDescription 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 ofXMLFormat.Ascii
will be used. -
writeXML
public void writeXML(int tab, java.io.Writer writer, ProgressUpdater progress_bar) throws java.io.IOExceptionDescription copied from interface:XMLObject
Writes the XML representation of this object to file, asXMLType.Normal
. The default format ofXMLFormat.Ascii
will 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: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 ofXMLFormat.Ascii
will be used. -
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 interfaceXMLObject
- Returns:
-