diff --git a/modules/core/src/de/diedavids/cuba/runtimediagnose/sql/SqlDiagnoseServiceBean.groovy b/modules/core/src/de/diedavids/cuba/runtimediagnose/sql/DatabaseDiagnoseServiceBean.groovy similarity index 70% rename from modules/core/src/de/diedavids/cuba/runtimediagnose/sql/SqlDiagnoseServiceBean.groovy rename to modules/core/src/de/diedavids/cuba/runtimediagnose/sql/DatabaseDiagnoseServiceBean.groovy index 8cc46d2..4da4ba4 100644 --- a/modules/core/src/de/diedavids/cuba/runtimediagnose/sql/SqlDiagnoseServiceBean.groovy +++ b/modules/core/src/de/diedavids/cuba/runtimediagnose/sql/DatabaseDiagnoseServiceBean.groovy @@ -15,8 +15,8 @@ import org.springframework.stereotype.Service import javax.inject.Inject import javax.sql.DataSource -@Service(SqlDiagnoseService.NAME) -class SqlDiagnoseServiceBean implements SqlDiagnoseService { +@Service(DatabaseDiagnoseService.NAME) +class DatabaseDiagnoseServiceBean implements DatabaseDiagnoseService { @Inject Persistence persistence @@ -25,7 +25,7 @@ class SqlDiagnoseServiceBean implements SqlDiagnoseService { SqlSelectResultFactory selectResultFactory @Inject - SqlConsoleParser sqlConsoleParser + DatabaseQueryParser databaseQueryParser @Inject TimeSource timeSource @@ -39,50 +39,40 @@ class SqlDiagnoseServiceBean implements SqlDiagnoseService { @Inject DiagnoseExecutionFactory diagnoseExecutionFactory - @Inject - JpqlConsoleParser jpqlConsoleParser @Override - SqlSelectResult runSqlDiagnose(String queryString, DiagnoseType diagnoseType) { - Statements queryStatements = sqlConsoleParser.analyseSql(queryString) - // analyzeSql are gonna return null in some cases + DatabaseQueryResult runSqlDiagnose(String queryString, DiagnoseType diagnoseType) { + Statements queryStatements = databaseQueryParser.analyseQueryString(queryString, diagnoseType) + if (!statementsAvailable(queryStatements)) { return selectResultFactory.createFromRows([]) } - if (DiagnoseType.JPQL == diagnoseType) { - jpqlConsoleParser.analyseJpql(queryString) - } - def queryStatement = queryStatements.statements[0].toString() DiagnoseExecution diagnoseExecution = createAdHocDiagnose(queryStatement, diagnoseType) - executeAdHocDiagnose(diagnoseType, queryStatement, queryStatements, diagnoseExecution) - } - - private void executeAdHocDiagnose(DiagnoseType diagnoseType, String queryStatement, Statements queryStatements, DiagnoseExecution diagnoseExecution) { - SqlSelectResult sqlSelectResult + DatabaseQueryResult databaseQueryResult try { - sqlSelectResult = getQueryResult(diagnoseType, queryStatement, queryStatements) - diagnoseExecution.handleSuccessfulExecution(sqlSelectResult.entities[0].toString()) + databaseQueryResult = getQueryResult(diagnoseType, queryStatement, queryStatements) + diagnoseExecution.handleSuccessfulExecution(databaseQueryResult.entities[0].toString()) diagnoseExecutionLogService.logDiagnoseExecution(diagnoseExecution) } catch (Exception e) { - sqlSelectResult = selectResultFactory.createFromRows([]) + databaseQueryResult = selectResultFactory.createFromRows([]) diagnoseExecution.handleErrorExecution(e) diagnoseExecutionLogService.logDiagnoseExecution(diagnoseExecution) } - sqlSelectResult + databaseQueryResult } - protected SqlSelectResult getQueryResult(DiagnoseType diagnoseType, String queryStatement, Statements queryStatements) { - SqlSelectResult sqlSelectResult + protected DatabaseQueryResult getQueryResult(DiagnoseType diagnoseType, String queryStatement, Statements queryStatements) { + DatabaseQueryResult sqlSelectResult switch (diagnoseType) { case DiagnoseType.JPQL: sqlSelectResult = executeJpqlStatement(queryStatement, queryStatements) break case DiagnoseType.SQL: def sql = createSqlConnection(persistence.dataSource) - sqlSelectResult = executeStatement(sql, queryStatement) + sqlSelectResult = executeSqlStatement(sql, queryStatement) break default: throw new IllegalArgumentException('DiagnoseType is not supported (' + diagnoseType + ')') @@ -90,20 +80,20 @@ class SqlDiagnoseServiceBean implements SqlDiagnoseService { sqlSelectResult } - protected SqlSelectResult executeJpqlStatement(String queryStatement, Statements queryStatements) { + protected DatabaseQueryResult executeJpqlStatement(String queryStatement, Statements queryStatements) { persistence.callInTransaction { Query q = persistence.entityManager.createQuery(queryStatement) - if (sqlConsoleParser.containsDataManipulation(queryStatements)) { + if (databaseQueryParser.containsDataManipulation(queryStatements)) { q.executeUpdate() - new SqlSelectResult() + new DatabaseQueryResult() } else { selectResultFactory.createFromRows(q.resultList) } } } - protected SqlSelectResult executeStatement(Sql sql, String queryString) { + protected DatabaseQueryResult executeSqlStatement(Sql sql, String queryString) { def rows = sql.rows(queryString) selectResultFactory.createFromRows(rows) } diff --git a/modules/core/src/de/diedavids/cuba/runtimediagnose/sql/SqlConsoleParser.groovy b/modules/core/src/de/diedavids/cuba/runtimediagnose/sql/DatabaseQueryParser.groovy similarity index 75% rename from modules/core/src/de/diedavids/cuba/runtimediagnose/sql/SqlConsoleParser.groovy rename to modules/core/src/de/diedavids/cuba/runtimediagnose/sql/DatabaseQueryParser.groovy index 7e34ed3..cf5092b 100644 --- a/modules/core/src/de/diedavids/cuba/runtimediagnose/sql/SqlConsoleParser.groovy +++ b/modules/core/src/de/diedavids/cuba/runtimediagnose/sql/DatabaseQueryParser.groovy @@ -1,8 +1,13 @@ package de.diedavids.cuba.runtimediagnose.sql +import com.haulmont.cuba.core.global.AppBeans import com.haulmont.cuba.core.global.Messages +import com.haulmont.cuba.core.global.QueryParserAstBased +import com.haulmont.cuba.core.sys.jpql.DomainModel +import com.haulmont.cuba.core.sys.jpql.DomainModelBuilder import de.diedavids.cuba.runtimediagnose.RuntimeDiagnoseConfiguration import de.diedavids.cuba.runtimediagnose.SqlConsoleSecurityException +import de.diedavids.cuba.runtimediagnose.diagnose.DiagnoseType import net.sf.jsqlparser.parser.CCJSqlParserUtil import net.sf.jsqlparser.statement.SetStatement import net.sf.jsqlparser.statement.Statements @@ -24,7 +29,7 @@ import org.springframework.stereotype.Component import javax.inject.Inject @Component -class SqlConsoleParser { +class DatabaseQueryParser { @Inject RuntimeDiagnoseConfiguration configuration @@ -42,9 +47,9 @@ class SqlConsoleParser { Drop, CreateTable, CreateView, Alter, AlterView, CreateIndex ] - Statements analyseSql(String sqlString) { + Statements analyseQueryString(String queryString, DiagnoseType diagnoseType) { - Statements statements = CCJSqlParserUtil.parseStatements(sqlString) + Statements statements = CCJSqlParserUtil.parseStatements(queryString) if (!configuration.sqlAllowDataManipulation && containsDataManipulation(statements)) { throw new SqlConsoleSecurityException(messages.getMessage(getClass(), 'dataManipulationNotAllowed')) @@ -58,9 +63,27 @@ class SqlConsoleParser { throw new SqlConsoleSecurityException(messages.getMessage(getClass(), 'executeOperationNotAllowed')) } + + if (DiagnoseType.JPQL == diagnoseType) { + analyseJpql(queryString) + } + statements } + void analyseJpql(String queryString) { + QueryParserAstBased parser = new QueryParserAstBased(ScriptManagerUtilsHolder.domainModelInstance, queryString) + parser.queryPaths + } + + private static class ScriptManagerUtilsHolder { + private static class ScriptManagerUtilLazyHolder { + public static final DomainModel DOMAIN_MODEL_INSTANCE = AppBeans.get(DomainModelBuilder).produce() + } + static DomainModel getDomainModelInstance() { + ScriptManagerUtilLazyHolder.DOMAIN_MODEL_INSTANCE + } + } boolean containsDataManipulation(Statements statements) { containsIllegalOperation(statements, DATA_MANIPULATION_OPERATIONS) } diff --git a/modules/core/src/de/diedavids/cuba/runtimediagnose/sql/JpqlConsoleParser.groovy b/modules/core/src/de/diedavids/cuba/runtimediagnose/sql/JpqlConsoleParser.groovy deleted file mode 100644 index 037073f..0000000 --- a/modules/core/src/de/diedavids/cuba/runtimediagnose/sql/JpqlConsoleParser.groovy +++ /dev/null @@ -1,25 +0,0 @@ -package de.diedavids.cuba.runtimediagnose.sql - -import com.haulmont.cuba.core.global.AppBeans -import com.haulmont.cuba.core.global.QueryParserAstBased -import com.haulmont.cuba.core.sys.jpql.DomainModel -import com.haulmont.cuba.core.sys.jpql.DomainModelBuilder -import org.springframework.stereotype.Component - -@Component -class JpqlConsoleParser { - - void analyseJpql(String queryString) { - QueryParserAstBased parser = new QueryParserAstBased(ScriptManagerUtilsHolder.domainModelInstance, queryString) - parser.queryPaths - } - - private static class ScriptManagerUtilsHolder { - private static class ScriptManagerUtilLazyHolder { - public static final DomainModel DOMAIN_MODEL_INSTANCE = AppBeans.get(DomainModelBuilder).produce() - } - static DomainModel getDomainModelInstance() { - ScriptManagerUtilLazyHolder.DOMAIN_MODEL_INSTANCE - } - } -} diff --git a/modules/core/test/de/diedavids/cuba/runtimediagnose/sql/SqlDiagnoseServiceBeanSpec.groovy b/modules/core/test/de/diedavids/cuba/runtimediagnose/sql/DatabaseDiagnoseServiceBeanSpec.groovy similarity index 64% rename from modules/core/test/de/diedavids/cuba/runtimediagnose/sql/SqlDiagnoseServiceBeanSpec.groovy rename to modules/core/test/de/diedavids/cuba/runtimediagnose/sql/DatabaseDiagnoseServiceBeanSpec.groovy index e1c5839..260c4f0 100644 --- a/modules/core/test/de/diedavids/cuba/runtimediagnose/sql/SqlDiagnoseServiceBeanSpec.groovy +++ b/modules/core/test/de/diedavids/cuba/runtimediagnose/sql/DatabaseDiagnoseServiceBeanSpec.groovy @@ -18,13 +18,12 @@ import spock.lang.Specification import javax.sql.DataSource -class SqlDiagnoseServiceBeanSpec extends Specification { +class DatabaseDiagnoseServiceBeanSpec extends Specification { - MockableSqlDiagnoseServiceBean sqlConsoleService + MockableDatabaseDiagnoseServiceBean databaseDiagnoseServiceBean - SqlConsoleParser sqlConsoleParser - JpqlConsoleParser jpqlConsoleParser + DatabaseQueryParser databaseQueryParser SqlSelectResultFactory selectResultFactory Transaction transaction Persistence persistence @@ -37,8 +36,7 @@ class SqlDiagnoseServiceBeanSpec extends Specification { Date currentDate def setup() { - sqlConsoleParser = Mock(SqlConsoleParser) - jpqlConsoleParser = Mock(JpqlConsoleParser) + databaseQueryParser = Mock(DatabaseQueryParser) selectResultFactory = Mock(SqlSelectResultFactory) transaction = Mock(Transaction) persistence = Mock(Persistence) @@ -58,9 +56,8 @@ class SqlDiagnoseServiceBeanSpec extends Specification { userSessionSource.getUserSession() >> userSession diagnoseExecutionFactory = Mock(DiagnoseExecutionFactory) - sqlConsoleService = new MockableSqlDiagnoseServiceBean( - sqlConsoleParser: sqlConsoleParser, - jpqlConsoleParser: jpqlConsoleParser, + databaseDiagnoseServiceBean = new MockableDatabaseDiagnoseServiceBean( + databaseQueryParser: databaseQueryParser, selectResultFactory: selectResultFactory, persistence: persistence, sql: sql, @@ -76,27 +73,27 @@ class SqlDiagnoseServiceBeanSpec extends Specification { } - def "executeSql uses SqlConsoleParser to analyse the sql statements"() { + def "runSqlDiagnose uses DatabaseQueryParser to analyse the sql statements"() { given: def sqlString = 'SELECT * FROM SEC_USER;' when: - sqlConsoleService.runSqlDiagnose(sqlString, DiagnoseType.SQL) + databaseDiagnoseServiceBean.runSqlDiagnose(sqlString, DiagnoseType.SQL) then: - 1 * sqlConsoleParser.analyseSql(sqlString) + 1 * databaseQueryParser.analyseQueryString(sqlString, DiagnoseType.SQL) } - def "executeJpql uses SqlConsoleParser to analyse the jpql statements"() { + def "runSqlDiagnose uses DatabaseQueryParser to analyse the jpql statements"() { given: def sqlString = 'select u from sec$User u' when: - sqlConsoleService.runSqlDiagnose(sqlString, DiagnoseType.JPQL) + databaseDiagnoseServiceBean.runSqlDiagnose(sqlString, DiagnoseType.JPQL) then: - 1 * sqlConsoleParser.analyseSql(sqlString) + 1 * databaseQueryParser.analyseQueryString(sqlString, DiagnoseType.JPQL) } - def "executeSql creates a SQL object with the datasource from persistence"() { + def "runSqlDiagnose creates a SQL object with the datasource from persistence"() { given: def sqlString = 'SELECT * FROM SEC_USER;' @@ -106,19 +103,19 @@ class SqlDiagnoseServiceBeanSpec extends Specification { sqlStatement.toString() >> sqlString statements.getStatements() >> [sqlStatement] - sqlConsoleParser.analyseSql(_) >> statements + databaseQueryParser.analyseQueryString(_,_) >> statements and: diagnoseExecutionFactory.createAdHocDiagnoseExecution(_ as String,_ as DiagnoseType) >> new DiagnoseExecution() when: - sqlConsoleService.runSqlDiagnose(sqlString, DiagnoseType.SQL) + databaseDiagnoseServiceBean.runSqlDiagnose(sqlString, DiagnoseType.SQL) then: - sqlConsoleService.actualDataSource == dataSource + databaseDiagnoseServiceBean.actualDataSource == dataSource } - def "executeSql executes the sql script if there is at least one result of the sql parser"() { + def "runSqlDiagnose executes the sql script if there is at least one result of the sql parser"() { given: def sqlString = 'SELECT * FROM SEC_USER;' @@ -129,19 +126,19 @@ class SqlDiagnoseServiceBeanSpec extends Specification { sqlStatement.toString() >> sqlString statements.getStatements() >> [sqlStatement] - sqlConsoleParser.analyseSql(_) >> statements + databaseQueryParser.analyseQueryString(_,_) >> statements and: diagnoseExecutionFactory.createAdHocDiagnoseExecution(_,_) >> new DiagnoseExecution() when: - sqlConsoleService.runSqlDiagnose(sqlString, DiagnoseType.SQL) + databaseDiagnoseServiceBean.runSqlDiagnose(sqlString, DiagnoseType.SQL) then: 1 * sql.rows(sqlString) } - def "executeJpql executes the jpql script if there is at least one result of the jpql parser"() { + def "runSqlDiagnose executes the jpql script if there is at least one result of the jpql parser"() { given: def sqlString = 'select u from sec$User u' @@ -152,19 +149,19 @@ class SqlDiagnoseServiceBeanSpec extends Specification { sqlStatement.toString() >> sqlString statements.getStatements() >> [sqlStatement] - sqlConsoleParser.analyseSql(_ as String) >> statements + databaseQueryParser.analyseQueryString(_ as String, DiagnoseType.JPQL) >> statements and: diagnoseExecutionFactory.createAdHocDiagnoseExecution(_ as String,_ as DiagnoseType) >> new DiagnoseExecution() when: - sqlConsoleService.runSqlDiagnose(sqlString, DiagnoseType.JPQL) + databaseDiagnoseServiceBean.runSqlDiagnose(sqlString, DiagnoseType.JPQL) then: 1 * persistence.callInTransaction(_ as Transaction.Callable) } - def "executeSql executes no sql script if there is no result of the sql parser"() { + def "runSqlDiagnose executes no sql script if there is no result of the sql parser"() { given: def sqlString = 'SELECT * FROM SEC_USER;' @@ -173,16 +170,16 @@ class SqlDiagnoseServiceBeanSpec extends Specification { def statements = Mock(Statements) statements.getStatements() >> [] - sqlConsoleParser.analyseSql(_) >> statements + databaseQueryParser.analyseQueryString(_, DiagnoseType.SQL) >> statements when: - sqlConsoleService.runSqlDiagnose(sqlString, DiagnoseType.SQL) + databaseDiagnoseServiceBean.runSqlDiagnose(sqlString, DiagnoseType.SQL) then: 0 * sql.rows(_) } - def "executeJpql executes no sql script if there is no result of the jpql parser"() { + def "runSqlDiagnose executes no sql script if there is no result of the jpql parser"() { given: def sqlString = 'select u from sec$User u' @@ -191,16 +188,16 @@ class SqlDiagnoseServiceBeanSpec extends Specification { def statements = Mock(Statements) statements.getStatements() >> [] - sqlConsoleParser.analyseSql(_) >> statements + databaseQueryParser.analyseQueryString(_, DiagnoseType.JPQL) >> statements when: - sqlConsoleService.runSqlDiagnose(sqlString, DiagnoseType.JPQL) + databaseDiagnoseServiceBean.runSqlDiagnose(sqlString, DiagnoseType.JPQL) then: 0 * sql.rows(_) } - def "executeSql executes no jpql script if there is no result of the jpql parser"() { + def "runSqlDiagnose executes no jpql script if there is no result of the jpql parser"() { given: def sqlString = 'select u from sec$User u' @@ -209,10 +206,10 @@ class SqlDiagnoseServiceBeanSpec extends Specification { def statements = Mock(Statements) statements.getStatements() >> [] - sqlConsoleParser.analyseSql(_) >> statements + databaseQueryParser.analyseQueryString(_, DiagnoseType.JPQL) >> statements when: - sqlConsoleService.runSqlDiagnose(sqlString, DiagnoseType.JPQL) + databaseDiagnoseServiceBean.runSqlDiagnose(sqlString, DiagnoseType.JPQL) then: 0 * sql.rows(_) @@ -224,20 +221,7 @@ class SqlDiagnoseServiceBeanSpec extends Specification { def diagnoseExecution = new DiagnoseExecution() when: - sqlConsoleService.runSqlDiagnose(diagnoseExecution, DiagnoseType.SQL) - - then: - diagnoseExecution.executionTimestamp == currentDate - diagnoseExecution.executionUser == "admin" - } - - def "runJpqlDiagnose adds metainformation to the diagnoseExecution"() { - - given: - def diagnoseExecution = new DiagnoseExecution() - - when: - sqlConsoleService.runSqlDiagnose(diagnoseExecution, DiagnoseType.JPQL) + databaseDiagnoseServiceBean.runSqlDiagnose(diagnoseExecution, DiagnoseType.SQL) then: diagnoseExecution.executionTimestamp == currentDate @@ -250,19 +234,7 @@ class SqlDiagnoseServiceBeanSpec extends Specification { def diagnoseExecution = new DiagnoseExecution() when: - sqlConsoleService.runSqlDiagnose(diagnoseExecution, DiagnoseType.SQL) - - then: - 1 * diagnoseExecutionLogService.logDiagnoseExecution(diagnoseExecution) - } - - def "runJpqlDiagnose logs the diagnose execution"() { - - given: - def diagnoseExecution = new DiagnoseExecution() - - when: - sqlConsoleService.runSqlDiagnose(diagnoseExecution, DiagnoseType.JPQL) + databaseDiagnoseServiceBean.runSqlDiagnose(diagnoseExecution, DiagnoseType.SQL) then: 1 * diagnoseExecutionLogService.logDiagnoseExecution(diagnoseExecution) @@ -275,14 +247,14 @@ class SqlDiagnoseServiceBeanSpec extends Specification { Statements statements = Mock(Statements) when: - sqlConsoleService.getQueryResult(diagnoseType, _ as String, statements) + databaseDiagnoseServiceBean.getQueryResult(diagnoseType, _ as String, statements) then: thrown(IllegalArgumentException) } } -class MockableSqlDiagnoseServiceBean extends SqlDiagnoseServiceBean { +class MockableDatabaseDiagnoseServiceBean extends DatabaseDiagnoseServiceBean { Sql sql diff --git a/modules/core/test/de/diedavids/cuba/runtimediagnose/sql/SqlConsoleParserSpec.groovy b/modules/core/test/de/diedavids/cuba/runtimediagnose/sql/DatabaseQueryParserSpec.groovy similarity index 56% rename from modules/core/test/de/diedavids/cuba/runtimediagnose/sql/SqlConsoleParserSpec.groovy rename to modules/core/test/de/diedavids/cuba/runtimediagnose/sql/DatabaseQueryParserSpec.groovy index 223b24e..ae90c8d 100644 --- a/modules/core/test/de/diedavids/cuba/runtimediagnose/sql/SqlConsoleParserSpec.groovy +++ b/modules/core/test/de/diedavids/cuba/runtimediagnose/sql/DatabaseQueryParserSpec.groovy @@ -3,16 +3,17 @@ package de.diedavids.cuba.runtimediagnose.sql import com.haulmont.cuba.core.global.Messages import de.diedavids.cuba.runtimediagnose.RuntimeDiagnoseConfiguration import de.diedavids.cuba.runtimediagnose.SqlConsoleSecurityException +import de.diedavids.cuba.runtimediagnose.diagnose.DiagnoseType import net.sf.jsqlparser.statement.SetStatement import net.sf.jsqlparser.statement.Statements import net.sf.jsqlparser.statement.drop.Drop import net.sf.jsqlparser.statement.insert.Insert import spock.lang.Specification -class SqlConsoleParserSpec extends Specification { +class DatabaseQueryParserSpec extends Specification { - SqlConsoleParser sut + DatabaseQueryParser sut RuntimeDiagnoseConfiguration runtimeDiagnoseConfiguration Messages messages @@ -20,22 +21,22 @@ class SqlConsoleParserSpec extends Specification { runtimeDiagnoseConfiguration = Mock(RuntimeDiagnoseConfiguration) messages = Mock(Messages) - sut = new SqlConsoleParser( + sut = new DatabaseQueryParser( configuration: runtimeDiagnoseConfiguration, messages: messages ) } - def "analyseSql throws a security exception if the SQL statement is data manipulation and the config does not allow data manipulation"() { + def "analyseQueryString throws a security exception if the SQL statement is data manipulation and the config does not allow data manipulation"() { given: runtimeDiagnoseConfiguration.getSqlAllowDataManipulation() >> false and: - messages.getMessage(SqlConsoleParser, 'dataManipulationNotAllowed') >> 'nope for data manipulation' + messages.getMessage(DatabaseQueryParser, 'dataManipulationNotAllowed') >> 'nope for data manipulation' when: - sut.analyseSql("INSERT INTO SEC_USER(ID) VALUES ('e54f6d8d-29b1-439e-846c-b6180495c066');") + sut.analyseQueryString("INSERT INTO SEC_USER(ID) VALUES ('e54f6d8d-29b1-439e-846c-b6180495c066');", DiagnoseType.SQL) then: def ex = thrown SqlConsoleSecurityException @@ -44,13 +45,13 @@ class SqlConsoleParserSpec extends Specification { ex.message == 'nope for data manipulation' } - def "analyseSql creates a insert statement if the SQL statement is data manipulation and the config does allow data manipulation"() { + def "analyseQueryString creates a insert statement if the SQL statement is data manipulation and the config does allow data manipulation"() { given: runtimeDiagnoseConfiguration.getSqlAllowDataManipulation() >> true when: - Statements result = sut.analyseSql("INSERT INTO SEC_USER (ID) VALUES ('e54f6d8d-29b1-439e-846c-b6180495c066')") + Statements result = sut.analyseQueryString("INSERT INTO SEC_USER (ID) VALUES ('e54f6d8d-29b1-439e-846c-b6180495c066')", DiagnoseType.SQL) def insertStatement = result.statements[0] then: @@ -63,16 +64,16 @@ class SqlConsoleParserSpec extends Specification { - def "analyseSql throws a security exception if the SQL statement is schema manipulation and the config does not allow schema manipulation"() { + def "analyseQueryString throws a security exception if the SQL statement is schema manipulation and the config does not allow schema manipulation"() { given: runtimeDiagnoseConfiguration.getSqlAllowSchemaManipulation() >> false and: - messages.getMessage(SqlConsoleParser, 'schemaManipulationNotAllowed') >> 'nope for schema manipulation' + messages.getMessage(DatabaseQueryParser, 'schemaManipulationNotAllowed') >> 'nope for schema manipulation' when: - sut.analyseSql("DROP TABLE SEC_USER") + sut.analyseQueryString("DROP TABLE SEC_USER", DiagnoseType.SQL) then: def ex = thrown SqlConsoleSecurityException @@ -81,13 +82,13 @@ class SqlConsoleParserSpec extends Specification { ex.message == 'nope for schema manipulation' } - def "analyseSql creates a drop statement if the SQL statement is schema manipulation and the config does allow schema manipulation"() { + def "analyseQueryString creates a drop statement if the SQL statement is schema manipulation and the config does allow schema manipulation"() { given: runtimeDiagnoseConfiguration.getSqlAllowSchemaManipulation() >> true when: - Statements result = sut.analyseSql("DROP TABLE SEC_USER") + Statements result = sut.analyseQueryString("DROP TABLE SEC_USER", DiagnoseType.SQL) def dropStatement = result.statements[0] then: @@ -100,16 +101,16 @@ class SqlConsoleParserSpec extends Specification { - def "analyseSql throws a security exception if the SQL statement is an execute operation and the config does not allow execution operations"() { + def "analyseQueryString throws a security exception if the SQL statement is an execute operation and the config does not allow execution operations"() { given: runtimeDiagnoseConfiguration.getSqlAllowExecuteOperations() >> false and: - messages.getMessage(SqlConsoleParser, 'executeOperationNotAllowed') >> 'nope for execute operations' + messages.getMessage(DatabaseQueryParser, 'executeOperationNotAllowed') >> 'nope for execute operations' when: - sut.analyseSql("EXECUTE MY_STORED_PROCEDURE") + sut.analyseQueryString("EXECUTE MY_STORED_PROCEDURE", DiagnoseType.SQL) then: def ex = thrown SqlConsoleSecurityException @@ -118,13 +119,13 @@ class SqlConsoleParserSpec extends Specification { ex.message == 'nope for execute operations' } - def "analyseSql creates a execute statement if the SQL statement is an execute operation and the config does allow execute operations"() { + def "analyseQueryString creates a execute statement if the SQL statement is an execute operation and the config does allow execute operations"() { given: runtimeDiagnoseConfiguration.getSqlAllowExecuteOperations() >> true when: - Statements result = sut.analyseSql("SET OPTION = VALUE") + Statements result = sut.analyseQueryString("SET OPTION = VALUE", DiagnoseType.SQL) def dropStatement = result.statements[0] then: diff --git a/modules/global/src/de/diedavids/cuba/runtimediagnose/sql/SqlDiagnoseService.groovy b/modules/global/src/de/diedavids/cuba/runtimediagnose/sql/DatabaseDiagnoseService.groovy similarity index 72% rename from modules/global/src/de/diedavids/cuba/runtimediagnose/sql/SqlDiagnoseService.groovy rename to modules/global/src/de/diedavids/cuba/runtimediagnose/sql/DatabaseDiagnoseService.groovy index 0292ec6..88cc6c4 100644 --- a/modules/global/src/de/diedavids/cuba/runtimediagnose/sql/SqlDiagnoseService.groovy +++ b/modules/global/src/de/diedavids/cuba/runtimediagnose/sql/DatabaseDiagnoseService.groovy @@ -3,9 +3,9 @@ package de.diedavids.cuba.runtimediagnose.sql import de.diedavids.cuba.runtimediagnose.diagnose.DiagnoseExecution import de.diedavids.cuba.runtimediagnose.diagnose.DiagnoseType -interface SqlDiagnoseService { +interface DatabaseDiagnoseService { String NAME = 'ddcrd_SqlConsoleService' - SqlSelectResult runSqlDiagnose(String queryString, DiagnoseType diagnoseType) + DatabaseQueryResult runSqlDiagnose(String queryString, DiagnoseType diagnoseType) DiagnoseExecution runSqlDiagnose(DiagnoseExecution diagnoseExecution, DiagnoseType diagnoseType) } \ No newline at end of file diff --git a/modules/global/src/de/diedavids/cuba/runtimediagnose/sql/SqlSelectResult.groovy b/modules/global/src/de/diedavids/cuba/runtimediagnose/sql/DatabaseQueryResult.groovy similarity index 88% rename from modules/global/src/de/diedavids/cuba/runtimediagnose/sql/SqlSelectResult.groovy rename to modules/global/src/de/diedavids/cuba/runtimediagnose/sql/DatabaseQueryResult.groovy index b6803b8..0815c6f 100644 --- a/modules/global/src/de/diedavids/cuba/runtimediagnose/sql/SqlSelectResult.groovy +++ b/modules/global/src/de/diedavids/cuba/runtimediagnose/sql/DatabaseQueryResult.groovy @@ -2,7 +2,7 @@ package de.diedavids.cuba.runtimediagnose.sql import com.haulmont.cuba.core.entity.KeyValueEntity -class SqlSelectResult implements Serializable { +class DatabaseQueryResult implements Serializable { private static final long serialVersionUID = -8288812447591918153L diff --git a/modules/global/src/de/diedavids/cuba/runtimediagnose/sql/SqlSelectResultFactory.groovy b/modules/global/src/de/diedavids/cuba/runtimediagnose/sql/SqlSelectResultFactory.groovy index a15f0f7..c62de0f 100644 --- a/modules/global/src/de/diedavids/cuba/runtimediagnose/sql/SqlSelectResultFactory.groovy +++ b/modules/global/src/de/diedavids/cuba/runtimediagnose/sql/SqlSelectResultFactory.groovy @@ -4,6 +4,6 @@ interface SqlSelectResultFactory { public static final String NAME = 'ddcrd_SqlSelectResultFactory' - SqlSelectResult createFromRows(List rows) + DatabaseQueryResult createFromRows(List rows) } diff --git a/modules/global/src/de/diedavids/cuba/runtimediagnose/sql/SqlSelectResultFactoryBean.groovy b/modules/global/src/de/diedavids/cuba/runtimediagnose/sql/SqlSelectResultFactoryBean.groovy index afb548a..4fee51a 100644 --- a/modules/global/src/de/diedavids/cuba/runtimediagnose/sql/SqlSelectResultFactoryBean.groovy +++ b/modules/global/src/de/diedavids/cuba/runtimediagnose/sql/SqlSelectResultFactoryBean.groovy @@ -16,8 +16,8 @@ class SqlSelectResultFactoryBean implements SqlSelectResultFactory { DatatypeFormatter datatypeFormatter @Override - SqlSelectResult createFromRows(List rows) { - def result = new SqlSelectResult() + DatabaseQueryResult createFromRows(List rows) { + def result = new DatabaseQueryResult() def queryValue = rows[0] if (queryValue instanceof Entity) { diff --git a/modules/global/test/de/diedavids/cuba/runtimediagnose/sql/SqlSelectResultSpec.groovy b/modules/global/test/de/diedavids/cuba/runtimediagnose/sql/SqlSelectResultSpec.groovy index e8dc78a..dbe45eb 100644 --- a/modules/global/test/de/diedavids/cuba/runtimediagnose/sql/SqlSelectResultSpec.groovy +++ b/modules/global/test/de/diedavids/cuba/runtimediagnose/sql/SqlSelectResultSpec.groovy @@ -6,11 +6,11 @@ import spock.lang.Specification class SqlSelectResultSpec extends Specification { - SqlSelectResult sqlSelectResult + DatabaseQueryResult sqlSelectResult def setup() { - sqlSelectResult = new SqlSelectResult( + sqlSelectResult = new DatabaseQueryResult( ) } diff --git a/modules/web/src/de/diedavids/cuba/runtimediagnose/web-spring.xml b/modules/web/src/de/diedavids/cuba/runtimediagnose/web-spring.xml index 7f2c97a..3e960af 100644 --- a/modules/web/src/de/diedavids/cuba/runtimediagnose/web-spring.xml +++ b/modules/web/src/de/diedavids/cuba/runtimediagnose/web-spring.xml @@ -11,7 +11,7 @@ + value="de.diedavids.cuba.runtimediagnose.sql.DatabaseDiagnoseService"/>