From 3fdb17316025cf66a1f97c0b941ac084490c2042 Mon Sep 17 00:00:00 2001 From: Marco Zanon Date: Thu, 29 Oct 2015 16:03:35 +0000 Subject: [PATCH] Renamed a package and the corresponding class names. Also styled the code a little. --- .../MDatabaseConnection.java} | 54 +++++------ .../MDatabaseConnectionFailureException.java | 30 ++++++ .../MDatabaseConnectionGenerator.java} | 24 ++--- .../database/MDatabaseConnectionPool.java | 95 +++++++++++++++++++ .../MDatabaseException.java} | 12 +-- .../MSqlStatementException.java | 4 +- .../MSqlStatementResults.java | 6 +- .../macaco/{sql => database}/MSqlTable.java | 54 +++++------ .../MSqlTransactionException.java | 4 +- .../macaco/logging/MLogDatabaseTable.java | 36 +++---- .../sql/MSqlConnectionFailureException.java | 30 ------ .../macaco/sql/MSqlConnectionPool.java | 95 ------------------- 12 files changed, 222 insertions(+), 222 deletions(-) rename 4.x/src/java/com/marcozanon/macaco/{sql/MSqlConnection.java => database/MDatabaseConnection.java} (72%) create mode 100644 4.x/src/java/com/marcozanon/macaco/database/MDatabaseConnectionFailureException.java rename 4.x/src/java/com/marcozanon/macaco/{sql/MSqlConnectionGenerator.java => database/MDatabaseConnectionGenerator.java} (64%) create mode 100644 4.x/src/java/com/marcozanon/macaco/database/MDatabaseConnectionPool.java rename 4.x/src/java/com/marcozanon/macaco/{sql/MSqlException.java => database/MDatabaseException.java} (51%) rename 4.x/src/java/com/marcozanon/macaco/{sql => database}/MSqlStatementException.java (83%) rename 4.x/src/java/com/marcozanon/macaco/{sql => database}/MSqlStatementResults.java (94%) rename 4.x/src/java/com/marcozanon/macaco/{sql => database}/MSqlTable.java (67%) rename 4.x/src/java/com/marcozanon/macaco/{sql => database}/MSqlTransactionException.java (83%) delete mode 100644 4.x/src/java/com/marcozanon/macaco/sql/MSqlConnectionFailureException.java delete mode 100644 4.x/src/java/com/marcozanon/macaco/sql/MSqlConnectionPool.java diff --git a/4.x/src/java/com/marcozanon/macaco/sql/MSqlConnection.java b/4.x/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java similarity index 72% rename from 4.x/src/java/com/marcozanon/macaco/sql/MSqlConnection.java rename to 4.x/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java index 12ab736..63aa6ee 100644 --- a/4.x/src/java/com/marcozanon/macaco/sql/MSqlConnection.java +++ b/4.x/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java @@ -4,7 +4,7 @@ * Released under MIT license (see LICENSE for details). */ -package com.marcozanon.macaco.sql; +package com.marcozanon.macaco.database; import com.marcozanon.macaco.MObject; import com.marcozanon.macaco.text.MText; @@ -14,7 +14,7 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.LinkedList; -public class MSqlConnection extends MObject { +public class MDatabaseConnection extends MObject { public static enum TransactionStatus { CLOSED, @@ -29,11 +29,11 @@ public class MSqlConnection extends MObject { protected Connection connection = null; - protected MSqlConnection.TransactionStatus transactionStatus = MSqlConnection.TransactionStatus.CLOSED; + protected MDatabaseConnection.TransactionStatus transactionStatus = MDatabaseConnection.TransactionStatus.CLOSED; /* */ - public MSqlConnection(String driver, String url, String username, String password) throws MSqlConnectionFailureException { + public MDatabaseConnection(String driver, String url, String username, String password) throws MDatabaseConnectionFailureException { super(); // if (MText.isBlank(driver)) { @@ -58,45 +58,45 @@ public class MSqlConnection extends MObject { Class.forName(this.getDriver()).newInstance(); } catch (ClassNotFoundException exception) { - throw new MSqlConnectionFailureException("Could not load driver.", exception); + throw new MDatabaseConnectionFailureException("Could not load driver.", exception); } catch (IllegalAccessException exception) { - throw new MSqlConnectionFailureException("Could not load driver.", exception); + throw new MDatabaseConnectionFailureException("Could not load driver.", exception); } catch (InstantiationException exception) { - throw new MSqlConnectionFailureException("Could not load driver.", exception); + throw new MDatabaseConnectionFailureException("Could not load driver.", exception); } // Establish a connection. try { this.connection = DriverManager.getConnection(this.getUrl(), this.getUsername(), this.getPassword()); } catch (SQLException exception) { - throw new MSqlConnectionFailureException("Could not get connection.", exception); + throw new MDatabaseConnectionFailureException("Could not get database connection.", exception); } // Set SQL mode to standard ANSI. try { this.getConnection().createStatement().executeUpdate("SET SQL_MODE='ANSI'"); } catch (SQLException exception) { - throw new MSqlConnectionFailureException("Could not set SQL mode to ANSI.", exception); + throw new MDatabaseConnectionFailureException("Could not set SQL mode to ANSI.", exception); } } - public void close() throws MSqlConnectionFailureException { + public void close() throws MDatabaseConnectionFailureException { try { this.getConnection().close(); } catch (SQLException exception) { - throw new MSqlConnectionFailureException("Could not close connection.", exception); + throw new MDatabaseConnectionFailureException("Could not close database connection.", exception); } } - public boolean isClosed() throws MSqlConnectionFailureException { + public boolean isClosed() throws MDatabaseConnectionFailureException { try { return this.getConnection().isClosed(); } catch (SQLException exception) { - throw new MSqlConnectionFailureException("Could not determine the state.", exception); + throw new MDatabaseConnectionFailureException("Could not determine the state.", exception); } } @@ -140,7 +140,7 @@ public class MSqlConnection extends MObject { /* Transactions. */ - protected void setTransactionStatus(MSqlConnection.TransactionStatus transactionStatus) { + protected void setTransactionStatus(MDatabaseConnection.TransactionStatus transactionStatus) { if (null == transactionStatus) { throw new IllegalArgumentException("Invalid 'transactionStatus': null."); } @@ -148,18 +148,18 @@ public class MSqlConnection extends MObject { this.transactionStatus = transactionStatus; } - public MSqlConnection.TransactionStatus getTransactionStatus() { + public MDatabaseConnection.TransactionStatus getTransactionStatus() { return this.transactionStatus; } public void startTransaction() throws MSqlTransactionException { - if (MSqlConnection.TransactionStatus.CLOSED != this.getTransactionStatus()) { + if (MDatabaseConnection.TransactionStatus.CLOSED != this.getTransactionStatus()) { throw new MSqlTransactionException("Nested transactions not allowed."); } try { this.getConnection().setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); this.getConnection().setAutoCommit(false); - this.setTransactionStatus(MSqlConnection.TransactionStatus.SUCCESSFUL); + this.setTransactionStatus(MDatabaseConnection.TransactionStatus.SUCCESSFUL); } catch (SQLException exception) { throw new MSqlTransactionException("Could not start transaction.", exception); @@ -167,36 +167,36 @@ public class MSqlConnection extends MObject { } public void rollBackTransaction() throws MSqlTransactionException { - if (MSqlConnection.TransactionStatus.CLOSED == this.getTransactionStatus()) { + if (MDatabaseConnection.TransactionStatus.CLOSED == this.getTransactionStatus()) { throw new MSqlTransactionException("Transaction not started."); } try { this.getConnection().rollback(); this.getConnection().setAutoCommit(true); - this.setTransactionStatus(MSqlConnection.TransactionStatus.CLOSED); + this.setTransactionStatus(MDatabaseConnection.TransactionStatus.CLOSED); } catch (SQLException exception) { - this.setTransactionStatus(MSqlConnection.TransactionStatus.FAILED); + this.setTransactionStatus(MDatabaseConnection.TransactionStatus.FAILED); throw new MSqlTransactionException("Could not roll back transaction.", exception); } } - public MSqlConnection.TransactionStatus commitTransaction() throws MSqlTransactionException { + public MDatabaseConnection.TransactionStatus commitTransaction() throws MSqlTransactionException { switch (this.getTransactionStatus()) { case SUCCESSFUL: try { this.getConnection().commit(); this.getConnection().setAutoCommit(true); - this.setTransactionStatus(MSqlConnection.TransactionStatus.CLOSED); - return MSqlConnection.TransactionStatus.SUCCESSFUL; + this.setTransactionStatus(MDatabaseConnection.TransactionStatus.CLOSED); + return MDatabaseConnection.TransactionStatus.SUCCESSFUL; } catch (SQLException exception) { this.rollBackTransaction(); - return MSqlConnection.TransactionStatus.FAILED; + return MDatabaseConnection.TransactionStatus.FAILED; } case FAILED: this.rollBackTransaction(); - return MSqlConnection.TransactionStatus.FAILED; + return MDatabaseConnection.TransactionStatus.FAILED; default: // instead of "case CLOSED:" (necessary to avoid Java compilation errors) throw new MSqlTransactionException("Transaction not started."); } @@ -229,8 +229,8 @@ public class MSqlConnection extends MObject { results = new MSqlStatementResults(preparedStatement); } catch (SQLException exception) { - if (MSqlConnection.TransactionStatus.SUCCESSFUL == this.getTransactionStatus()) { - this.setTransactionStatus(MSqlConnection.TransactionStatus.FAILED); + if (MDatabaseConnection.TransactionStatus.SUCCESSFUL == this.getTransactionStatus()) { + this.setTransactionStatus(MDatabaseConnection.TransactionStatus.FAILED); } throw new MSqlStatementException(String.format("Could not execute prepared statement: %s.", preparedStatement), exception); } diff --git a/4.x/src/java/com/marcozanon/macaco/database/MDatabaseConnectionFailureException.java b/4.x/src/java/com/marcozanon/macaco/database/MDatabaseConnectionFailureException.java new file mode 100644 index 0000000..3092f1d --- /dev/null +++ b/4.x/src/java/com/marcozanon/macaco/database/MDatabaseConnectionFailureException.java @@ -0,0 +1,30 @@ +/** + * Macaco + * Copyright (c) 2009-2015 Marco Zanon . + * Released under MIT license (see LICENSE for details). + */ + +package com.marcozanon.macaco.database; + +@SuppressWarnings("serial") +public class MDatabaseConnectionFailureException extends MDatabaseException { + + /* */ + + public MDatabaseConnectionFailureException() { + super(); + } + + public MDatabaseConnectionFailureException(String message) { + super(message); + } + + public MDatabaseConnectionFailureException(Throwable error) { + super(error); + } + + public MDatabaseConnectionFailureException(String message, Throwable error) { + super(message, error); + } + +} diff --git a/4.x/src/java/com/marcozanon/macaco/sql/MSqlConnectionGenerator.java b/4.x/src/java/com/marcozanon/macaco/database/MDatabaseConnectionGenerator.java similarity index 64% rename from 4.x/src/java/com/marcozanon/macaco/sql/MSqlConnectionGenerator.java rename to 4.x/src/java/com/marcozanon/macaco/database/MDatabaseConnectionGenerator.java index 1a98ddf..76b62e0 100644 --- a/4.x/src/java/com/marcozanon/macaco/sql/MSqlConnectionGenerator.java +++ b/4.x/src/java/com/marcozanon/macaco/database/MDatabaseConnectionGenerator.java @@ -4,12 +4,12 @@ * Released under MIT license (see LICENSE for details). */ -package com.marcozanon.macaco.sql; +package com.marcozanon.macaco.database; import com.marcozanon.macaco.MObject; import com.marcozanon.macaco.text.MText; -public class MSqlConnectionGenerator extends MObject { +public class MDatabaseConnectionGenerator extends MObject { protected String driver = null; protected String url = null; @@ -18,7 +18,7 @@ public class MSqlConnectionGenerator extends MObject { /* */ - public MSqlConnectionGenerator(String driver, String url, String username, String password) { + public MDatabaseConnectionGenerator(String driver, String url, String username, String password) { super(); // if (MText.isBlank(driver)) { @@ -66,25 +66,25 @@ public class MSqlConnectionGenerator extends MObject { /* Generator. */ - public MSqlConnection getNewSqlConnection() throws MSqlConnectionFailureException { - return new MSqlConnection(this.getDriver(), this.getUrl(), this.getUsername(), this.getPassword()); + public MDatabaseConnection getNewDatabaseConnection() throws MDatabaseConnectionFailureException { + return new MDatabaseConnection(this.getDriver(), this.getUrl(), this.getUsername(), this.getPassword()); } - public boolean isGeneratorFor(MSqlConnection sqlConnection) { - if (null == sqlConnection) { - throw new IllegalArgumentException("Invalid 'sqlConnection': null."); + public boolean isGeneratorFor(MDatabaseConnection databaseConnection) { + if (null == databaseConnection) { + throw new IllegalArgumentException("Invalid 'databaseConnection': null."); } // - if (!sqlConnection.getDriver().equals(this.getDriver())) { + if (!databaseConnection.getDriver().equals(this.getDriver())) { return false; } - if (!sqlConnection.getUrl().equals(this.getUrl())) { + if (!databaseConnection.getUrl().equals(this.getUrl())) { return false; } - if (!sqlConnection.getUsername().equals(this.getUsername())) { + if (!databaseConnection.getUsername().equals(this.getUsername())) { return false; } - if (!sqlConnection.getPassword().equals(this.getPassword())) { + if (!databaseConnection.getPassword().equals(this.getPassword())) { return false; } // diff --git a/4.x/src/java/com/marcozanon/macaco/database/MDatabaseConnectionPool.java b/4.x/src/java/com/marcozanon/macaco/database/MDatabaseConnectionPool.java new file mode 100644 index 0000000..1b3b636 --- /dev/null +++ b/4.x/src/java/com/marcozanon/macaco/database/MDatabaseConnectionPool.java @@ -0,0 +1,95 @@ +/** + * Macaco + * Copyright (c) 2009-2015 Marco Zanon . + * Released under MIT license (see LICENSE for details). + */ + +package com.marcozanon.macaco.database; + +import com.marcozanon.macaco.MObject; +import java.util.LinkedList; + +public class MDatabaseConnectionPool extends MObject { + + protected MDatabaseConnectionGenerator databaseConnectionGenerator = null; + + protected LinkedList databaseConnections = new LinkedList(); + protected int minimumSize = 0; + protected int maximumSize = 0; + + /* */ + + public MDatabaseConnectionPool(String driver, String url, String username, String password, int minimumSize, int maximumSize) throws MDatabaseConnectionFailureException { + super(); + // + if (0 > minimumSize) { + throw new IllegalArgumentException(String.format("Invalid 'minimumSize': %s.", minimumSize)); + } + if (1 > maximumSize) { + throw new IllegalArgumentException(String.format("Invalid 'maximumSize': %s.", maximumSize)); + } + else if (minimumSize > maximumSize) { + throw new IllegalArgumentException(String.format("Invalid 'maximumSize': %s < %s ('minimumSize').", maximumSize, minimumSize)); + } + // + this.databaseConnectionGenerator = new MDatabaseConnectionGenerator(driver, url, username, password); + this.minimumSize = minimumSize; + this.maximumSize = maximumSize; + // + this.initialize(); + } + + protected void initialize() throws MDatabaseConnectionFailureException { + for (int c = 0; c < this.getMinimumSize(); c++) { + MDatabaseConnection databaseConnection = this.getDatabaseConnectionGenerator().getNewDatabaseConnection(); + this.pushDatabaseConnection(databaseConnection); + } + } + + /* Database connection generator. */ + + protected MDatabaseConnectionGenerator getDatabaseConnectionGenerator() { + return this.databaseConnectionGenerator; + } + + /* Database connections. */ + + protected LinkedList getDatabaseConnections() { + return this.databaseConnections; + } + + protected int getMinimumSize() { + return this.minimumSize; + } + + protected int getMaximumSize() { + return this.maximumSize; + } + + public synchronized MDatabaseConnection popDatabaseConnection() throws MDatabaseConnectionFailureException { + LinkedList databaseConnections = this.getDatabaseConnections(); + MDatabaseConnection databaseConnection = databaseConnections.removeLast(); + if (this.getMinimumSize() > databaseConnections.size()) { + databaseConnections.add(this.getDatabaseConnectionGenerator().getNewDatabaseConnection()); + } + return databaseConnection; + } + + public synchronized void pushDatabaseConnection(MDatabaseConnection databaseConnection) throws MDatabaseConnectionFailureException { + if (null == databaseConnection) { + throw new IllegalArgumentException("Invalid 'databaseConnection': null."); + } + else if (!this.getDatabaseConnectionGenerator().isGeneratorFor(databaseConnection)) { + throw new IllegalArgumentException("Invalid 'databaseConnection': not compatible with this database connection pool."); + } + else if (databaseConnection.isClosed()) { + throw new IllegalArgumentException("Invalid 'databaseConnection': closed."); + } + // + LinkedList databaseConnections = this.getDatabaseConnections(); + if (this.getMaximumSize() >= databaseConnections.size()) { + databaseConnections.add(databaseConnection); + } + } + +} diff --git a/4.x/src/java/com/marcozanon/macaco/sql/MSqlException.java b/4.x/src/java/com/marcozanon/macaco/database/MDatabaseException.java similarity index 51% rename from 4.x/src/java/com/marcozanon/macaco/sql/MSqlException.java rename to 4.x/src/java/com/marcozanon/macaco/database/MDatabaseException.java index 6309000..a6cce0e 100644 --- a/4.x/src/java/com/marcozanon/macaco/sql/MSqlException.java +++ b/4.x/src/java/com/marcozanon/macaco/database/MDatabaseException.java @@ -4,27 +4,27 @@ * Released under MIT license (see LICENSE for details). */ -package com.marcozanon.macaco.sql; +package com.marcozanon.macaco.database; import com.marcozanon.macaco.MException; -public abstract class MSqlException extends MException { +public abstract class MDatabaseException extends MException { /* */ - public MSqlException() { + public MDatabaseException() { super(); } - public MSqlException(String message) { + public MDatabaseException(String message) { super(message); } - public MSqlException(Throwable error) { + public MDatabaseException(Throwable error) { super(error); } - public MSqlException(String message, Throwable error) { + public MDatabaseException(String message, Throwable error) { super(message, error); } diff --git a/4.x/src/java/com/marcozanon/macaco/sql/MSqlStatementException.java b/4.x/src/java/com/marcozanon/macaco/database/MSqlStatementException.java similarity index 83% rename from 4.x/src/java/com/marcozanon/macaco/sql/MSqlStatementException.java rename to 4.x/src/java/com/marcozanon/macaco/database/MSqlStatementException.java index 9cc7d6d..dced93b 100644 --- a/4.x/src/java/com/marcozanon/macaco/sql/MSqlStatementException.java +++ b/4.x/src/java/com/marcozanon/macaco/database/MSqlStatementException.java @@ -4,10 +4,10 @@ * Released under MIT license (see LICENSE for details). */ -package com.marcozanon.macaco.sql; +package com.marcozanon.macaco.database; @SuppressWarnings("serial") -public class MSqlStatementException extends MSqlException { +public class MSqlStatementException extends MDatabaseException { /* */ diff --git a/4.x/src/java/com/marcozanon/macaco/sql/MSqlStatementResults.java b/4.x/src/java/com/marcozanon/macaco/database/MSqlStatementResults.java similarity index 94% rename from 4.x/src/java/com/marcozanon/macaco/sql/MSqlStatementResults.java rename to 4.x/src/java/com/marcozanon/macaco/database/MSqlStatementResults.java index 7d3210a..b89eff2 100644 --- a/4.x/src/java/com/marcozanon/macaco/sql/MSqlStatementResults.java +++ b/4.x/src/java/com/marcozanon/macaco/database/MSqlStatementResults.java @@ -4,7 +4,7 @@ * Released under MIT license (see LICENSE for details). */ -package com.marcozanon.macaco.sql; +package com.marcozanon.macaco.database; import com.marcozanon.macaco.MObject; import com.marcozanon.macaco.text.MText; @@ -67,7 +67,7 @@ public class MSqlStatementResults extends MObject { } } - public void close() throws MSqlConnectionFailureException { + public void close() throws MDatabaseConnectionFailureException { try { if (null != this.getResultSet()) { this.getResultSet().close(); @@ -75,7 +75,7 @@ public class MSqlStatementResults extends MObject { this.getPreparedStatement().close(); } catch (SQLException exception) { - throw new MSqlConnectionFailureException("Could not close statement and/or result set references.", exception); + throw new MDatabaseConnectionFailureException("Could not close statement and/or result set references.", exception); } } diff --git a/4.x/src/java/com/marcozanon/macaco/sql/MSqlTable.java b/4.x/src/java/com/marcozanon/macaco/database/MSqlTable.java similarity index 67% rename from 4.x/src/java/com/marcozanon/macaco/sql/MSqlTable.java rename to 4.x/src/java/com/marcozanon/macaco/database/MSqlTable.java index 932e776..f48524e 100644 --- a/4.x/src/java/com/marcozanon/macaco/sql/MSqlTable.java +++ b/4.x/src/java/com/marcozanon/macaco/database/MSqlTable.java @@ -4,7 +4,7 @@ * Released under MIT license (see LICENSE for details). */ -package com.marcozanon.macaco.sql; +package com.marcozanon.macaco.database; import com.marcozanon.macaco.MObject; import com.marcozanon.macaco.text.MText; @@ -14,17 +14,17 @@ import java.util.LinkedList; public class MSqlTable extends MObject { - protected MSqlConnection sqlConnection = null; + protected MDatabaseConnection databaseConnection = null; protected String table = null; protected String primaryKey = null; /* */ - public MSqlTable(MSqlConnection sqlConnection, String table, String primaryKey) { + public MSqlTable(MDatabaseConnection databaseConnection, String table, String primaryKey) { super(); // - if (null == sqlConnection) { - throw new IllegalArgumentException("Invalid 'sqlConnection': null."); + if (null == databaseConnection) { + throw new IllegalArgumentException("Invalid 'databaseConnection': null."); } if (MText.isBlank(table)) { throw new IllegalArgumentException("Invalid 'table': null or empty."); @@ -33,15 +33,15 @@ public class MSqlTable extends MObject { throw new IllegalArgumentException("Invalid 'primaryKey': null or empty."); } // - this.sqlConnection = sqlConnection; + this.databaseConnection = databaseConnection; this.table = table; this.primaryKey = primaryKey; } - /* Sql connection. */ + /* Database connection. */ - protected MSqlConnection getSqlConnection() { - return this.sqlConnection; + protected MDatabaseConnection getDatabaseConnection() { + return this.databaseConnection; } /* Table. */ @@ -73,10 +73,10 @@ public class MSqlTable extends MObject { } fields.delete(fields.length() - 2, fields.length()); s.delete(s.length() - 2, s.length()); - return this.getSqlConnection().executePreparedStatement(" INSERT INTO \"" + this.getTable() + "\"" - + " (" + fields + ")" - + " VALUES (" + s + ")", - p); + return this.getDatabaseConnection().executePreparedStatement(" INSERT INTO \"" + this.getTable() + "\"" + + " (" + fields + ")" + + " VALUES (" + s + ")", + p); } protected MSqlStatementResults updateRecord(LinkedHashMap map, Object id) throws MSqlStatementException { @@ -95,10 +95,10 @@ public class MSqlTable extends MObject { } s.delete(s.length() - 2, s.length()); p.add(id); - return this.getSqlConnection().executePreparedStatement(" UPDATE \"" + this.getTable() + "\"" - + " SET " + s - + " WHERE (\"" + this.getTable() + "\".\"" + this.getPrimaryKey() + "\" = ?)", - p); + return this.getDatabaseConnection().executePreparedStatement(" UPDATE \"" + this.getTable() + "\"" + + " SET " + s + + " WHERE (\"" + this.getTable() + "\".\"" + this.getPrimaryKey() + "\" = ?)", + p); } public MSqlStatementResults setRecord(LinkedHashMap map, Object id) throws MSqlStatementException { @@ -117,10 +117,10 @@ public class MSqlTable extends MObject { // LinkedList p = new LinkedList(); p.add(id); - return this.getSqlConnection().executePreparedStatement(" SELECT *" - + " FROM \"" + this.getTable() + "\"" - + " WHERE (\"" + this.getTable() + "\".\"" + this.getPrimaryKey() + "\" = ?)", - p); + return this.getDatabaseConnection().executePreparedStatement(" SELECT *" + + " FROM \"" + this.getTable() + "\"" + + " WHERE (\"" + this.getTable() + "\".\"" + this.getPrimaryKey() + "\" = ?)", + p); } public MSqlStatementResults deleteRecord(Object id) throws MSqlStatementException { @@ -130,9 +130,9 @@ public class MSqlTable extends MObject { // LinkedList p = new LinkedList(); p.add(id); - return this.getSqlConnection().executePreparedStatement(" DELETE FROM \"" + this.getTable() + "\"" - + " WHERE (\"" + this.getTable() + "\".\"" + this.getPrimaryKey() + "\" = ?)", - p); + return this.getDatabaseConnection().executePreparedStatement(" DELETE FROM \"" + this.getTable() + "\"" + + " WHERE (\"" + this.getTable() + "\".\"" + this.getPrimaryKey() + "\" = ?)", + p); } public MSqlStatementResults deleteRecords(LinkedHashSet idSet) throws MSqlStatementException { @@ -146,9 +146,9 @@ public class MSqlTable extends MObject { whereClause.append(" OR (\"" + this.getTable() + "\".\"" + this.getPrimaryKey() + "\" = ?)"); p.add(id); } - return this.getSqlConnection().executePreparedStatement(" DELETE FROM \"" + this.getTable() + "\"" - + " WHERE (" + whereClause + ")", - p); + return this.getDatabaseConnection().executePreparedStatement(" DELETE FROM \"" + this.getTable() + "\"" + + " WHERE (" + whereClause + ")", + p); } } diff --git a/4.x/src/java/com/marcozanon/macaco/sql/MSqlTransactionException.java b/4.x/src/java/com/marcozanon/macaco/database/MSqlTransactionException.java similarity index 83% rename from 4.x/src/java/com/marcozanon/macaco/sql/MSqlTransactionException.java rename to 4.x/src/java/com/marcozanon/macaco/database/MSqlTransactionException.java index 62773e1..b19ae52 100644 --- a/4.x/src/java/com/marcozanon/macaco/sql/MSqlTransactionException.java +++ b/4.x/src/java/com/marcozanon/macaco/database/MSqlTransactionException.java @@ -4,10 +4,10 @@ * Released under MIT license (see LICENSE for details). */ -package com.marcozanon.macaco.sql; +package com.marcozanon.macaco.database; @SuppressWarnings("serial") -public class MSqlTransactionException extends MSqlException { +public class MSqlTransactionException extends MDatabaseException { /* */ diff --git a/4.x/src/java/com/marcozanon/macaco/logging/MLogDatabaseTable.java b/4.x/src/java/com/marcozanon/macaco/logging/MLogDatabaseTable.java index 7e03230..bfdb828 100644 --- a/4.x/src/java/com/marcozanon/macaco/logging/MLogDatabaseTable.java +++ b/4.x/src/java/com/marcozanon/macaco/logging/MLogDatabaseTable.java @@ -6,11 +6,11 @@ package com.marcozanon.macaco.logging; -import com.marcozanon.macaco.sql.MSqlConnection; -import com.marcozanon.macaco.sql.MSqlConnectionFailureException; -import com.marcozanon.macaco.sql.MSqlConnectionPool; -import com.marcozanon.macaco.sql.MSqlStatementException; -import com.marcozanon.macaco.sql.MSqlTable; +import com.marcozanon.macaco.database.MDatabaseConnection; +import com.marcozanon.macaco.database.MDatabaseConnectionFailureException; +import com.marcozanon.macaco.database.MDatabaseConnectionPool; +import com.marcozanon.macaco.database.MSqlStatementException; +import com.marcozanon.macaco.database.MSqlTable; import com.marcozanon.macaco.text.MText; import java.text.SimpleDateFormat; import java.util.Date; @@ -18,21 +18,21 @@ import java.util.LinkedHashMap; public class MLogDatabaseTable extends MLogTarget { - protected MSqlConnectionPool sqlConnectionPool = null; + protected MDatabaseConnectionPool databaseConnectionPool = null; protected String logDatabaseTable = null; protected String logDatabaseTablePrimaryKey = null; protected String logDatabaseField = null; - protected MSqlConnection sqlConnection = null; + protected MDatabaseConnection databaseConnection = null; /* */ - public MLogDatabaseTable(MSqlConnectionPool sqlConnectionPool, String logDatabaseTable, String logDatabaseTablePrimaryKey, String logDatabaseField) throws MLoggingException { + public MLogDatabaseTable(MDatabaseConnectionPool databaseConnectionPool, String logDatabaseTable, String logDatabaseTablePrimaryKey, String logDatabaseField) throws MLoggingException { super(); // - if (null == sqlConnectionPool) { - throw new IllegalArgumentException("Invalid 'sqlConnectionPool': null."); + if (null == databaseConnectionPool) { + throw new IllegalArgumentException("Invalid 'databaseConnectionPool': null."); } if (MText.isEmpty(logDatabaseTable)) { throw new IllegalArgumentException("Invalid 'logDatabaseTable': null or empty."); @@ -44,7 +44,7 @@ public class MLogDatabaseTable extends MLogTarget { throw new IllegalArgumentException("Invalid 'logDatabaseField': null or empty."); } // - this.sqlConnectionPool = sqlConnectionPool; + this.databaseConnectionPool = databaseConnectionPool; this.logDatabaseTable = logDatabaseTable; this.logDatabaseTablePrimaryKey = logDatabaseTablePrimaryKey; this.logDatabaseField = logDatabaseField; @@ -53,10 +53,10 @@ public class MLogDatabaseTable extends MLogTarget { public void close() throws MLoggingException { } - /* Sql connection pool. */ + /* Database connection pool. */ - protected MSqlConnectionPool getSqlConnectionPool() { - return this.sqlConnectionPool; + protected MDatabaseConnectionPool getDatabaseConnectionPool() { + return this.databaseConnectionPool; } /* Database logging parameters. */ @@ -95,15 +95,15 @@ public class MLogDatabaseTable extends MLogTarget { separator.append(" "); } // - MSqlConnection sqlConnection = this.getSqlConnectionPool().popSqlConnection(); + MDatabaseConnection databaseConnection = this.getDatabaseConnectionPool().popDatabaseConnection(); // LinkedHashMap p = new LinkedHashMap(); p.put(this.getLogDatabaseField(), timestamp + separator + message); - (new MSqlTable(sqlConnection, this.getLogDatabaseTable(), this.getLogDatabaseTablePrimaryKey())).setRecord(p, null); + (new MSqlTable(databaseConnection, this.getLogDatabaseTable(), this.getLogDatabaseTablePrimaryKey())).setRecord(p, null); // - this.getSqlConnectionPool().pushSqlConnection(sqlConnection); + this.getDatabaseConnectionPool().pushDatabaseConnection(databaseConnection); } - catch (MSqlConnectionFailureException exception) { + catch (MDatabaseConnectionFailureException exception) { throw new MLoggingException("Could not write to database table.", exception); } catch (MSqlStatementException exception) { diff --git a/4.x/src/java/com/marcozanon/macaco/sql/MSqlConnectionFailureException.java b/4.x/src/java/com/marcozanon/macaco/sql/MSqlConnectionFailureException.java deleted file mode 100644 index fd0636b..0000000 --- a/4.x/src/java/com/marcozanon/macaco/sql/MSqlConnectionFailureException.java +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Macaco - * Copyright (c) 2009-2015 Marco Zanon . - * Released under MIT license (see LICENSE for details). - */ - -package com.marcozanon.macaco.sql; - -@SuppressWarnings("serial") -public class MSqlConnectionFailureException extends MSqlException { - - /* */ - - public MSqlConnectionFailureException() { - super(); - } - - public MSqlConnectionFailureException(String message) { - super(message); - } - - public MSqlConnectionFailureException(Throwable error) { - super(error); - } - - public MSqlConnectionFailureException(String message, Throwable error) { - super(message, error); - } - -} diff --git a/4.x/src/java/com/marcozanon/macaco/sql/MSqlConnectionPool.java b/4.x/src/java/com/marcozanon/macaco/sql/MSqlConnectionPool.java deleted file mode 100644 index 4523bfd..0000000 --- a/4.x/src/java/com/marcozanon/macaco/sql/MSqlConnectionPool.java +++ /dev/null @@ -1,95 +0,0 @@ -/** - * Macaco - * Copyright (c) 2009-2015 Marco Zanon . - * Released under MIT license (see LICENSE for details). - */ - -package com.marcozanon.macaco.sql; - -import com.marcozanon.macaco.MObject; -import java.util.LinkedList; - -public class MSqlConnectionPool extends MObject { - - protected MSqlConnectionGenerator sqlConnectionGenerator = null; - - protected int minimumSize = 0; - protected int maximumSize = 0; - protected LinkedList sqlConnectionPool = new LinkedList(); - - /* */ - - public MSqlConnectionPool(String driver, String url, String username, String password, int minimumSize, int maximumSize) throws MSqlConnectionFailureException { - super(); - // - if (0 > minimumSize) { - throw new IllegalArgumentException(String.format("Invalid 'minimumSize': %s.", minimumSize)); - } - if (1 > maximumSize) { - throw new IllegalArgumentException(String.format("Invalid 'maximumSize': %s.", maximumSize)); - } - else if (minimumSize > maximumSize) { - throw new IllegalArgumentException(String.format("Invalid 'maximumSize': %s < %s ('minimumSize').", maximumSize, minimumSize)); - } - // - this.sqlConnectionGenerator = new MSqlConnectionGenerator(driver, url, username, password); - this.minimumSize = minimumSize; - this.maximumSize = maximumSize; - // - this.initializeSqlConnectionPool(); - } - - /* Sql connection generator. */ - - protected MSqlConnectionGenerator getSqlConnectionGenerator() { - return this.sqlConnectionGenerator; - } - - /* Sql connection pool. */ - - protected int getMinimumSize() { - return this.minimumSize; - } - - protected int getMaximumSize() { - return this.maximumSize; - } - - protected LinkedList getSqlConnectionPool() { - return this.sqlConnectionPool; - } - - protected void initializeSqlConnectionPool() throws MSqlConnectionFailureException { - for (int c = 0; c < this.getMinimumSize(); c++) { - MSqlConnection sqlConnection = this.getSqlConnectionGenerator().getNewSqlConnection(); - this.getSqlConnectionPool().add(sqlConnection); - } - } - - public synchronized MSqlConnection popSqlConnection() throws MSqlConnectionFailureException { - LinkedList sqlConnectionPool = this.getSqlConnectionPool(); - MSqlConnection sqlConnection = sqlConnectionPool.removeLast(); - if (this.getMinimumSize() > sqlConnectionPool.size()) { - sqlConnectionPool.add(this.getSqlConnectionGenerator().getNewSqlConnection()); - } - return sqlConnection; - } - - public synchronized void pushSqlConnection(MSqlConnection sqlConnection) throws MSqlConnectionFailureException { - if (null == sqlConnection) { - throw new IllegalArgumentException("Invalid 'sqlConnection': null."); - } - else if (!this.getSqlConnectionGenerator().isGeneratorFor(sqlConnection)) { - throw new IllegalArgumentException("Invalid 'sqlConnection': not generated by this Sql connection generator."); - } - else if (sqlConnection.isClosed()) { - throw new IllegalArgumentException("Invalid 'sqlConnection': closed."); - } - // - LinkedList sqlConnectionPool = this.getSqlConnectionPool(); - if (this.getMaximumSize() >= sqlConnectionPool.size()) { - sqlConnectionPool.add(sqlConnection); - } - } - -} -- 2.30.2