Class DatabaseController
- java.lang.Object
-
- org.teichert.databaseexplorer.controllers.DatabaseController
-
public final class DatabaseController extends Object
The Database-Controller-Class provides all necessary methods to work with the database.
This Class is mostly used via the Database-ModelDatabaseModel
.
-
-
Field Summary
Fields Modifier and Type Field Description static int
EMPTY_CONNECTION_POOL_ID
Default ConnectionPoolId for System Executions (Which is not a user execution and belongs to no Tab)
This ConnectionPoolId will not be chaced within the HashMapmapOfDatabasePool
,
so the connection will created every time again.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
abort(int connectionPoolId)
Aborts the current database-execution represented by the connectionPoolId.void
abort(Connection conn)
Aborts the current database-execution represented by theConnection
conn.void
closeConnection(ExecutorService es, int connectionPoolId, Map<Object,Object> mapOfData)
Close (if possible) the cached Database-Connection for the given connectionPoolId.
The mode will determine if the connection is a (JDBC) or (Hibernate) Connection.void
copyRows(DatabaseModel model, SQLTable sourceTable, SQLTable targetTable)
Iterates through the source-table and copy row by row into the target-table on the other database!Connection
createConnection(int connectionPoolId, String id, String grouping, String customName, String sshConnId, String sshForwardingId, String db, String ip, String port, String user, String pwd, String parameter, boolean isLazyLoaded, DatabaseModel model)
Returns a Database-Connection.
If no connection exists for the given connectionPoolId, a new one will be created
otherwise it will return a cached one.Connection
createConnection(int connectionPoolId, SQLDatabase database)
Returns a Database-Connection.
If no connection exists for the given connectionPoolId, a new one will be created
otherwise it will return a cached one.String
createConnectionURL(String db, String ip, String port, String parameter, DatabaseModel model)
Returns the connectionURL of the given parameters to theDatabaseModel
specific syntax.Map<String,Object>
createMapFromTableRow(SQLRow row)
Creates a HashMap based on the internal structure of the SaveItem table.
Key = SQLTableColumn-name
value = value of the row to the column
The table names are (always in uppercase):
ID, DBNAME, IP, PORT, USER, USERPWD, DBPARAMETER, CUSTOMNAME, GROUPNAME, SSHCONNID, SSHFORWARDINGID, MODEL, LAZYLOADEDConnection
createNewConnectionForSubTransaction(int connectionPoolId, String db, String ip, String port, String user, String pwd, String parameter, DatabaseModel model)
Creates always a new Database-Connection, because SubTransaction will be handled within a new connection, to split it from the original one.
(All Database-ConnectionsConnection
have it's own execution-context)Connection
createNewConnectionForSubTransaction(int connectionPoolId, SQLDatabase database)
Creates always a new Database-Connection, because SubTransaction will be handled within a new connection, to split it from the original one.
(All Database-ConnectionsConnection
have it's own execution-context)void
createNewSQLIndexes(SQLTable table)
Creates a new index for the given table.void
createNewSQLTable(SQLTable table)
Creates a new table in the database with the values from the given SQL table.KeyValue<String,SQLTable>
createPreparedTempSQLTable(int connectionPoolId, SQLDatabase database, String sql, Object... values)
Creates a temporary table with a prepared statement.KeyValue<String,SQLTable>
createTempSQLTable(int connectionPoolId, SQLTextEditor<?> editor, SQLDatabase database, String sql, Map<Object,Object> mapOfData, boolean fillContent)
Creates a temporary table when executing a "SELECT"-SQL script.KeyValue<String,SQLTable>
createTempSQLTableForSQL(int connectionPoolId, SQLDatabase database, String sql, boolean fillContent)
Executes the sql and returns aSQLTable
.void
deleteSQLTableValue(int connectionPoolId, SQLTableValue[] oldValues)
Deletes the given rows.void
doCommit(int connectionPoolId, Map<Object,Object> mapOfData)
Do a commit (if possible) to the cached Database-Connection for the given connectionPoolId.void
doRollback(int connectionPoolId, Map<Object,Object> mapOfData)
Do a rollback (if possible) to the cached Database-Connection for the given connectionPoolId.Exception
executePreparedSQL(int connectionPoolId, SQLDatabase database, String sql, Object[] values)
Executes an SQL-Statement via aPreparedStatement
without returning aResultSet
.Exception
executeSQL(int connectionPoolId, SQLDatabase database, String sql, SQLTextEditor<?> editor, IDoingAfterThread d)
Executes an SQL-Statement without returning aResultSet
.
For example: (UPDATE, DELETE, INSERT INTO) etc. commands should be executed!String
exportSQl(String sql, SQLDatabase db)
Returns an SQL-script for export for the given input-sqlvoid
fillConstraints(SQLDatabase database)
Adds allSQLForeignInfo
into the columns of all tables within the givenSQLDatabase
.void
fillConstraints(SQLTable table)
Adds allSQLForeignInfo
into the columns of all tables within the givenSQLDatabase
.List<SQLProcedure>
getAllStoredProcedure(SQLDatabase database)
Loads all StoredProcedures of the given database and return them into aLinkedList
.int
getColumnCount(Connection conn, SQLTable table, boolean closeAfterLoading)
Returns the number of columns of the given (not yet filled) tabletableName
.String
getCreateIndexString(SQLIndex index, boolean withSchema)
Returns the string for creating the given index.String
getCreateSQLIndexesString(SQLTable table, boolean withSchema)
Returns the string for creating the indexes of the given table.
It only get index-create-statement for non-primary-indexes!String
getCreationString(SQLTable table, boolean withSchema)
Creates the Create-Table SQL-Script from the givenSQLTable
static DatabaseController
getInstance()
Map<Integer,Connection>
getMapOfConnectionPool()
Map<Integer,SQLDatabase>
getMapOfDatabasePool()
Map<Connection,Statement>
getMapOfRunningStatements()
List<IDatabaseControllerExtensionPluginInterface>
getPlugins()
List<SQLDatabase>
getServerDatabases(SQLServer server)
Returns a list of all databases within the given server.boolean
hasConnection(int connectionPoolId, Map<Object,Object> mapOfData)
Checks if a cached Database-Connection exists for the given connectionPoolId.void
insertIntoSQLTableValue(int connectionPoolId, String columnName, SQLTableValue newValue)
Inserts a new row for the given table.SQLDatabase
loadDatabase(SQLServer parent, String id, String grouping, String customName, String sshConnId, String sshForwardingId, String user, String pwd, String ip, String port, String databaseName, String para, boolean isLazyLoadedIntern, boolean isLazyLoaded, Class<? extends DatabaseModel> databaseClass, Map<String,Object> map)
Loads an SQL database with all SQLTables, SQLTableColumns and SQLRows with associated SQLValues.SQLDatabase
loadDatabaseTables(SQLDatabase db, Map<String,Object> map)
void
loadSQLColumns(Connection conn, SQLDatabase database, SQLTable table)
Loads the SQLColumns for the given SQLTable and the given SQLDatabase.List<SQLRow>
loadSQLRows(Connection conn, SQLTable table, int limit)
Loads all rows (max: limit) of a table and returns them with values!void
loadTableInfos(int connectionPoolId, SQLTable table)
Loads all table-informations but takes lazyloading into account.void
loadTableInfosWithoutLazyloading(int connectionPoolId, SQLTable table)
Loads all table-informations and ignore lazyloading.void
reconnect(ExecutorService es, int connectionPoolId, Map<Object,Object> mapOfData)
Do a "reconnect" (close and create a new Connection if possible) to the cached Database-Connection for the given connectionPoolId and also rollback not committet executions.void
reconnect(ExecutorService es, int connectionPoolId, SQLDatabase database, Map<Object,Object> mapOfData)
Do a "reconnect" (close and create a new Connection if possible) to the cached Database-Connection for the given connectionPoolId.void
reloadTable(int connectionPoolId, SQLDatabase database, Connection conn, ResultSet rs, SQLTable table, int limit, boolean fillTable, boolean closeAfterLoading)
KeyValue<String,SQLDatabase>
testConnection(String id, String grouping, String customName, String sshConnId, String sshForwardingId, String db, String ip, String port, String user, String pwd, String parameter, DatabaseModel model, Map<String,Object> map)
Tests the Connection.
Timeout is for default 5 seconds.void
updateSQLTableValue(int connectionPoolId, SQLTableColumn column, SQLTableValue[] oldValues, SQLTableValue newValue, boolean isFunctionValue)
Update the givenSQLTableValue
.
-
-
-
Field Detail
-
EMPTY_CONNECTION_POOL_ID
public static final int EMPTY_CONNECTION_POOL_ID
Default ConnectionPoolId for System Executions (Which is not a user execution and belongs to no Tab)
This ConnectionPoolId will not be chaced within the HashMapmapOfDatabasePool
,
so the connection will created every time again.- See Also:
- Constant Field Values
-
-
Method Detail
-
getMapOfDatabasePool
public Map<Integer,SQLDatabase> getMapOfDatabasePool()
-
getMapOfConnectionPool
public Map<Integer,Connection> getMapOfConnectionPool()
-
getMapOfRunningStatements
public Map<Connection,Statement> getMapOfRunningStatements()
-
getPlugins
public List<IDatabaseControllerExtensionPluginInterface> getPlugins()
-
getInstance
public static DatabaseController getInstance()
-
loadDatabase
public SQLDatabase loadDatabase(SQLServer parent, String id, String grouping, String customName, String sshConnId, String sshForwardingId, String user, String pwd, String ip, String port, String databaseName, String para, boolean isLazyLoadedIntern, boolean isLazyLoaded, Class<? extends DatabaseModel> databaseClass, Map<String,Object> map) throws InstantiationException, IllegalAccessException, SQLException
Loads an SQL database with all SQLTables, SQLTableColumns and SQLRows with associated SQLValues.- Parameters:
parent
- = ParentSQLServeruser
- = Username for the databasepwd
- = Password for the database belongs to the usernameip
- = Ip-address from the databasedatabaseName
- = database namepara
- = parameter for the databaseisLazyLoadedIntern
- = if the database only be loaded by lazyloading (only tables first)databaseClass
- = DatabaseModel-Class (for example. MySQLModel or H2Model ...)map
- = Global-configurations fromLoader
-class- Returns:
- Throws:
IllegalAccessException
InstantiationException
SQLException
-
loadDatabaseTables
public SQLDatabase loadDatabaseTables(SQLDatabase db, Map<String,Object> map) throws InstantiationException, IllegalAccessException, SQLException
-
createNewSQLTable
public void createNewSQLTable(SQLTable table) throws SQLException
Creates a new table in the database with the values from the given SQL table.- Parameters:
table
-- Throws:
SQLException
-
getCreateSQLIndexesString
public String getCreateSQLIndexesString(SQLTable table, boolean withSchema) throws SQLException
Returns the string for creating the indexes of the given table.
It only get index-create-statement for non-primary-indexes!- Parameters:
table
-- Throws:
SQLException
-
getCreateIndexString
public String getCreateIndexString(SQLIndex index, boolean withSchema)
Returns the string for creating the given index.- Parameters:
index
-- Returns:
-
getCreationString
public String getCreationString(SQLTable table, boolean withSchema)
Creates the Create-Table SQL-Script from the givenSQLTable
- Parameters:
table
-withSchema
-- Returns:
-
createTempSQLTable
public KeyValue<String,SQLTable> createTempSQLTable(int connectionPoolId, SQLTextEditor<?> editor, SQLDatabase database, String sql, Map<Object,Object> mapOfData, boolean fillContent)
Creates a temporary table when executing a "SELECT"-SQL script.- Parameters:
connectionPoolId
-editor
-database
-sql
-fillContent
-mapOfData
-- Returns:
-
createPreparedTempSQLTable
public KeyValue<String,SQLTable> createPreparedTempSQLTable(int connectionPoolId, SQLDatabase database, String sql, Object... values)
Creates a temporary table with a prepared statement.- Parameters:
database
-sql
-values
-- Returns:
-
createTempSQLTableForSQL
public KeyValue<String,SQLTable> createTempSQLTableForSQL(int connectionPoolId, SQLDatabase database, String sql, boolean fillContent)
Executes the sql and returns aSQLTable
.- Parameters:
database
-sql
-fillContent
-- Returns:
-
createNewSQLIndexes
public void createNewSQLIndexes(SQLTable table) throws SQLException
Creates a new index for the given table.- Parameters:
table
-- Throws:
SQLException
-
insertIntoSQLTableValue
public void insertIntoSQLTableValue(int connectionPoolId, String columnName, SQLTableValue newValue) throws SQLException
Inserts a new row for the given table.- Parameters:
newValue
-- Throws:
SQLException
-
copyRows
public void copyRows(DatabaseModel model, SQLTable sourceTable, SQLTable targetTable) throws Exception
Iterates through the source-table and copy row by row into the target-table on the other database!- Parameters:
model
-sourceTable
-targetTable
-- Throws:
Exception
-
exportSQl
public String exportSQl(String sql, SQLDatabase db) throws Exception
Returns an SQL-script for export for the given input-sql- Parameters:
sql
- = input sql, which will be exporteddb
- = database on which the export sql will be executed- Returns:
- Throws:
Exception
-
updateSQLTableValue
public void updateSQLTableValue(int connectionPoolId, SQLTableColumn column, SQLTableValue[] oldValues, SQLTableValue newValue, boolean isFunctionValue) throws SQLException
Update the givenSQLTableValue
.- Throws:
SQLException
-
deleteSQLTableValue
public void deleteSQLTableValue(int connectionPoolId, SQLTableValue[] oldValues) throws SQLException
Deletes the given rows.- Parameters:
connectionPoolId
-oldValues
-- Throws:
SQLException
-
reloadTable
public void reloadTable(int connectionPoolId, SQLDatabase database, Connection conn, ResultSet rs, SQLTable table, int limit, boolean fillTable, boolean closeAfterLoading) throws Exception
Fills the givenSQLTable
with all information and if fillTable is true alsoSQLRow
s with the given amount restricted by limit.- Parameters:
database
- = databaseconn
- = MAY ALSO BE ZERO, THEN A NEW CONNECTION WILL BE CREATED!rs
- = MAY ALSO BE ZERO, THEN A NEW RESULTSET WILL BE CREATED! (IN COMBINATION WITHconn
!!!table
- = table to be filled.limit
- = row limit (SQL statement)closeAfterLoading
- = Should the new or passed connection be closed after the execution? (Recommended for a new connection, since it is no longer used!)- Throws:
SQLException
Exception
-
executeSQL
public Exception executeSQL(int connectionPoolId, SQLDatabase database, String sql, SQLTextEditor<?> editor, IDoingAfterThread d)
Executes an SQL-Statement without returning aResultSet
.
For example: (UPDATE, DELETE, INSERT INTO) etc. commands should be executed!- Parameters:
connectionPoolId
-database
- =SQLDatabase
sql
- = SQL-Statementeditor
-d
-- Returns:
-
executePreparedSQL
public Exception executePreparedSQL(int connectionPoolId, SQLDatabase database, String sql, Object[] values)
Executes an SQL-Statement via aPreparedStatement
without returning aResultSet
. So with this, for example: (UPDATE, DELETE, INSERT INTO) etc. commands should be executed!- Parameters:
connectionPoolId
-database
-sql
-values
-- Returns:
-
testConnection
public KeyValue<String,SQLDatabase> testConnection(String id, String grouping, String customName, String sshConnId, String sshForwardingId, String db, String ip, String port, String user, String pwd, String parameter, DatabaseModel model, Map<String,Object> map)
Tests the Connection.
Timeout is for default 5 seconds.- Parameters:
id
- = database identifiergrouping
- = database grouping (for css style only)customName
- = database customName (for gui only)sshConnId
- = the identifier for a SSH-Connection (can be null)sshForwardingId
- = the identifier for a SSH-Port-Forwarding (can be null)db
- = database nameip
- = IP-address of the databaseport
- = Port of the databaseuser
- = Username of the databasepwd
- = Password of the user of the databaseparameter
- = Parameter of the database.- Returns:
- returns an array of type
Object
from a size at least 2.
Index 0 is a boolean; This means if the connection is correctly done or not.
Index 1 is the SQLDatabase-Object; this is the object created by the test.
if the test gone wrong, the database-object isnull
!
-
createConnectionURL
public String createConnectionURL(String db, String ip, String port, String parameter, DatabaseModel model)
Returns the connectionURL of the given parameters to theDatabaseModel
specific syntax.- Parameters:
db
-ip
-port
-parameter
-- Returns:
-
createConnection
public Connection createConnection(int connectionPoolId, SQLDatabase database) throws SQLException
Returns a Database-Connection.
If no connection exists for the given connectionPoolId, a new one will be created
otherwise it will return a cached one.- Parameters:
connectionPoolId
-database
-- Returns:
- Throws:
SQLException
-
createConnection
public Connection createConnection(int connectionPoolId, String id, String grouping, String customName, String sshConnId, String sshForwardingId, String db, String ip, String port, String user, String pwd, String parameter, boolean isLazyLoaded, DatabaseModel model) throws SQLException
Returns a Database-Connection.
If no connection exists for the given connectionPoolId, a new one will be created
otherwise it will return a cached one.- Parameters:
connectionPoolId
-id
-grouping
-customName
-sshConnId
-sshForwardingId
-db
-ip
-port
-user
-pwd
-parameter
-isLazyLoaded
-- Returns:
- Throws:
SQLException
-
createNewConnectionForSubTransaction
public Connection createNewConnectionForSubTransaction(int connectionPoolId, SQLDatabase database) throws SQLException
Creates always a new Database-Connection, because SubTransaction will be handled within a new connection, to split it from the original one.
(All Database-ConnectionsConnection
have it's own execution-context)- Parameters:
connectionPoolId
-database
-- Returns:
- Throws:
SQLException
-
createNewConnectionForSubTransaction
public Connection createNewConnectionForSubTransaction(int connectionPoolId, String db, String ip, String port, String user, String pwd, String parameter, DatabaseModel model) throws SQLException
Creates always a new Database-Connection, because SubTransaction will be handled within a new connection, to split it from the original one.
(All Database-ConnectionsConnection
have it's own execution-context)- Parameters:
connectionPoolId
-db
-ip
-port
-user
-pwd
-parameter
-- Returns:
- Throws:
SQLException
-
doRollback
public void doRollback(int connectionPoolId, Map<Object,Object> mapOfData) throws SQLException
Do a rollback (if possible) to the cached Database-Connection for the given connectionPoolId.- Parameters:
connectionPoolId
-mapOfData
-- Throws:
SQLException
-
doCommit
public void doCommit(int connectionPoolId, Map<Object,Object> mapOfData) throws SQLException
Do a commit (if possible) to the cached Database-Connection for the given connectionPoolId.- Parameters:
connectionPoolId
-mapOfData
-- Throws:
SQLException
-
hasConnection
public boolean hasConnection(int connectionPoolId, Map<Object,Object> mapOfData)
Checks if a cached Database-Connection exists for the given connectionPoolId.- Parameters:
connectionPoolId
-mapOfData
-- Returns:
-
reconnect
public void reconnect(ExecutorService es, int connectionPoolId, Map<Object,Object> mapOfData) throws Exception
Do a "reconnect" (close and create a new Connection if possible) to the cached Database-Connection for the given connectionPoolId and also rollback not committet executions.- Parameters:
es
-connectionPoolId
-mapOfData
-- Throws:
Exception
-
reconnect
public void reconnect(ExecutorService es, int connectionPoolId, SQLDatabase database, Map<Object,Object> mapOfData) throws Exception
Do a "reconnect" (close and create a new Connection if possible) to the cached Database-Connection for the given connectionPoolId.- Parameters:
es
-connectionPoolId
-database
-mapOfData
-- Throws:
Exception
-
abort
public void abort(int connectionPoolId) throws SQLException
Aborts the current database-execution represented by the connectionPoolId.- Parameters:
connectionPoolId
-- Throws:
SQLException
-
abort
public void abort(Connection conn) throws SQLException
Aborts the current database-execution represented by theConnection
conn.- Parameters:
conn
-- Throws:
SQLException
-
closeConnection
public void closeConnection(ExecutorService es, int connectionPoolId, Map<Object,Object> mapOfData)
Close (if possible) the cached Database-Connection for the given connectionPoolId.
The mode will determine if the connection is a (JDBC) or (Hibernate) Connection.- Parameters:
connectionPoolId
-mapOfData
-
-
loadTableInfosWithoutLazyloading
public void loadTableInfosWithoutLazyloading(int connectionPoolId, SQLTable table) throws SQLException
Loads all table-informations and ignore lazyloading.- Parameters:
connectionPoolId
-table
-- Throws:
SQLException
-
loadTableInfos
public void loadTableInfos(int connectionPoolId, SQLTable table) throws SQLException
Loads all table-informations but takes lazyloading into account.- Parameters:
connectionPoolId
-table
-- Throws:
SQLException
-
fillConstraints
public void fillConstraints(SQLDatabase database)
Adds allSQLForeignInfo
into the columns of all tables within the givenSQLDatabase
.- Parameters:
database
-
-
fillConstraints
public void fillConstraints(SQLTable table)
Adds allSQLForeignInfo
into the columns of all tables within the givenSQLDatabase
.- Parameters:
table
-
-
getAllStoredProcedure
public List<SQLProcedure> getAllStoredProcedure(SQLDatabase database) throws SQLException
Loads all StoredProcedures of the given database and return them into aLinkedList
.- Parameters:
database
-- Returns:
- Throws:
SQLException
-
getColumnCount
public int getColumnCount(Connection conn, SQLTable table, boolean closeAfterLoading) throws SQLException
Returns the number of columns of the given (not yet filled) tabletableName
.- Parameters:
conn
-table
-closeAfterLoading
-- Returns:
- Throws:
SQLException
-
loadSQLColumns
public void loadSQLColumns(Connection conn, SQLDatabase database, SQLTable table) throws SQLException
Loads the SQLColumns for the given SQLTable and the given SQLDatabase.
The SQL database is still included if the table does not yet have a parent!- Parameters:
conn
-database
-table
-- Throws:
SQLException
-
loadSQLRows
public List<SQLRow> loadSQLRows(Connection conn, SQLTable table, int limit) throws Exception
Loads all rows (max: limit) of a table and returns them with values!- Parameters:
conn
-table
-limit
-- Returns:
- Throws:
SQLException
Exception
-
getServerDatabases
public List<SQLDatabase> getServerDatabases(SQLServer server) throws SQLException
Returns a list of all databases within the given server.- Parameters:
server
-- Returns:
- Throws:
SQLException
-
createMapFromTableRow
public Map<String,Object> createMapFromTableRow(SQLRow row)
Creates a HashMap based on the internal structure of the SaveItem table.
Key = SQLTableColumn-name
value = value of the row to the column
The table names are (always in uppercase):
ID, DBNAME, IP, PORT, USER, USERPWD, DBPARAMETER, CUSTOMNAME, GROUPNAME, SSHCONNID, SSHFORWARDINGID, MODEL, LAZYLOADED- Parameters:
row
-- Returns:
-
-