From fb4628413ad3ab4b7e4a60e928b13298f9356740 Mon Sep 17 00:00:00 2001 From: guredd Date: Fri, 1 Jun 2018 17:11:44 +0300 Subject: [PATCH] improved interceptors API, embedded logger interceptor was deprecated --- .../org/jetbrains/exposed/sql/SQLLog.kt | 2 - .../org/jetbrains/exposed/sql/Transaction.kt | 18 +++-- .../exposed/sql/statements/Statement.kt | 70 +++++++++---------- .../sql/statements/StatementMonitor.kt | 11 +-- 4 files changed, 46 insertions(+), 55 deletions(-) diff --git a/src/main/kotlin/org/jetbrains/exposed/sql/SQLLog.kt b/src/main/kotlin/org/jetbrains/exposed/sql/SQLLog.kt index 8373e32ea4..629475764a 100644 --- a/src/main/kotlin/org/jetbrains/exposed/sql/SQLLog.kt +++ b/src/main/kotlin/org/jetbrains/exposed/sql/SQLLog.kt @@ -51,8 +51,6 @@ class CompositeSqlLogger : SqlLogger, StatementInterceptor { } } - override fun beforeExecution(transaction: Transaction, context: StatementContext) { } - override fun afterExecution(transaction: Transaction, contexts: List, executedStatement: PreparedStatement) { contexts.forEach { log(it, transaction) diff --git a/src/main/kotlin/org/jetbrains/exposed/sql/Transaction.kt b/src/main/kotlin/org/jetbrains/exposed/sql/Transaction.kt index 97b74d32e4..ca79545dda 100644 --- a/src/main/kotlin/org/jetbrains/exposed/sql/Transaction.kt +++ b/src/main/kotlin/org/jetbrains/exposed/sql/Transaction.kt @@ -4,7 +4,7 @@ import org.jetbrains.exposed.dao.Entity import org.jetbrains.exposed.dao.EntityCache import org.jetbrains.exposed.dao.EntityHook import org.jetbrains.exposed.sql.statements.Statement -import org.jetbrains.exposed.sql.statements.StatementMonitor +import org.jetbrains.exposed.sql.statements.StatementInterceptor import org.jetbrains.exposed.sql.statements.StatementType import org.jetbrains.exposed.sql.transactions.TransactionInterface import org.jetbrains.exposed.sql.vendors.currentDialect @@ -31,8 +31,13 @@ open class UserDataHolder { open class Transaction(private val transactionImpl: TransactionInterface): UserDataHolder(), TransactionInterface by transactionImpl { - val monitor = StatementMonitor() + internal val interceptors = arrayListOf() + fun registerInterceptor(interceptor: StatementInterceptor) = interceptors.add(interceptor) + + fun unregisterInterceptor(interceptor: StatementInterceptor) = interceptors.remove(interceptor) + + @Deprecated("should be connected externally as StatementInterceptor") val logger = CompositeSqlLogger() var statementCount: Int = 0 @@ -51,27 +56,28 @@ open class Transaction(private val transactionImpl: TransactionInterface): UserD init { logger.addLogger(Slf4jSqlDebugLogger) + registerInterceptor(logger) } override fun commit() { val created = flushCache() EntityHook.alertSubscribers() val createdByHooks = flushCache() - monitor.interceptors.forEach { it.beforeCommit(this) } + interceptors.forEach { it.beforeCommit(this) } transactionImpl.commit() userdata.clear() EntityCache.invalidateGlobalCaches(created + createdByHooks) - monitor.interceptors.forEach { it.afterCommit(this) } + interceptors.forEach { it.afterCommit() } } override fun rollback() { - monitor.interceptors.forEach { it.beforeRollback(this) } + interceptors.forEach { it.beforeRollback(this) } transactionImpl.rollback() userdata.clear() entityCache.clearReferrersCache() entityCache.data.clear() entityCache.inserts.clear() - monitor.interceptors.forEach { it.afterRollback(this) } + interceptors.forEach { it.afterRollback() } } fun flushCache(): List> { diff --git a/src/main/kotlin/org/jetbrains/exposed/sql/statements/Statement.kt b/src/main/kotlin/org/jetbrains/exposed/sql/statements/Statement.kt index c3cc3f5933..d968f453f4 100644 --- a/src/main/kotlin/org/jetbrains/exposed/sql/statements/Statement.kt +++ b/src/main/kotlin/org/jetbrains/exposed/sql/statements/Statement.kt @@ -29,48 +29,42 @@ abstract class Statement(val type: StatementType, val targets: List> { - try { - transaction.monitor.register(transaction.logger) - - val arguments = arguments() - val contexts = if (arguments.count() > 0) { - arguments.map { args -> - val context = StatementContext(this, args) - transaction.monitor.interceptors.forEach { it.beforeExecution(transaction, context) } - context - } - } else { - val context = StatementContext(this, emptyList()) - transaction.monitor.interceptors.forEach { it.beforeExecution(transaction, context) } - listOf(context) - } - - val statement = try { - prepared(transaction, prepareSQL(transaction)) - } catch (e: SQLException) { - throw ExposedSQLException(e, contexts) + val arguments = arguments() + val contexts = if (arguments.count() > 0) { + arguments.map { args -> + val context = StatementContext(this, args) + transaction.interceptors.forEach { it.beforeExecution(transaction, context) } + context } - contexts.forEachIndexed { i, context -> - statement.fillParameters(context.args) - // REVIEW - if (contexts.size > 1 || isAlwaysBatch) statement.addBatch() - } - if (!transaction.db.supportsMultipleResultSets) transaction.closeExecutedStatements() + } else { + val context = StatementContext(this, emptyList()) + transaction.interceptors.forEach { it.beforeExecution(transaction, context) } + listOf(context) + } - transaction.currentStatement = statement - val result = try { - statement.executeInternal(transaction) - } catch (e: SQLException) { - throw ExposedSQLException(e, contexts) - } - transaction.currentStatement = null - transaction.executedStatements.add(statement) + val statement = try { + prepared(transaction, prepareSQL(transaction)) + } catch (e: SQLException) { + throw ExposedSQLException(e, contexts) + } + contexts.forEachIndexed { i, context -> + statement.fillParameters(context.args) + // REVIEW + if (contexts.size > 1 || isAlwaysBatch) statement.addBatch() + } + if (!transaction.db.supportsMultipleResultSets) transaction.closeExecutedStatements() - transaction.monitor.interceptors.forEach { it.afterExecution(transaction, contexts, statement) } - return result to contexts - } finally { - transaction.monitor.unregister(transaction.logger) + transaction.currentStatement = statement + val result = try { + statement.executeInternal(transaction) + } catch (e: SQLException) { + throw ExposedSQLException(e, contexts) } + transaction.currentStatement = null + transaction.executedStatements.add(statement) + + transaction.interceptors.forEach { it.afterExecution(transaction, contexts, statement) } + return result to contexts } } diff --git a/src/main/kotlin/org/jetbrains/exposed/sql/statements/StatementMonitor.kt b/src/main/kotlin/org/jetbrains/exposed/sql/statements/StatementMonitor.kt index 8183ee8372..272ff30d52 100644 --- a/src/main/kotlin/org/jetbrains/exposed/sql/statements/StatementMonitor.kt +++ b/src/main/kotlin/org/jetbrains/exposed/sql/statements/StatementMonitor.kt @@ -8,15 +8,8 @@ interface StatementInterceptor { fun afterExecution(transaction: Transaction, contexts: List, executedStatement: PreparedStatement) {} fun beforeCommit(transaction: Transaction) {} - fun afterCommit(transaction: Transaction) {} + fun afterCommit() {} fun beforeRollback(transaction: Transaction) {} - fun afterRollback(transaction: Transaction) {} -} - -class StatementMonitor { - internal val interceptors: MutableList = arrayListOf() - - fun register(interceptor: StatementInterceptor) = interceptors.add(interceptor) - fun unregister(interceptor: StatementInterceptor) = interceptors.remove(interceptor) + fun afterRollback() {} } \ No newline at end of file