Skip to content

Commit

Permalink
updated to CUBA 6.10; resolved no result set bug in #23 (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariodavid authored Oct 30, 2018
1 parent 5f48c56 commit 9def522
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 101 deletions.
8 changes: 5 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

buildscript {
ext.cubaVersion = '6.9.3'
ext.cubaVersion = '6.10.2'
repositories {

mavenLocal()
Expand Down Expand Up @@ -55,7 +55,7 @@ apply(plugin: 'cuba')
cuba {
artifact {
group = 'de.diedavids.cuba.runtimediagnose'
version = "1.1.0"
version = "1.2.0"
isSnapshot = false
}
tomcat {
Expand Down Expand Up @@ -215,7 +215,9 @@ configure([globalModule, coreModule, guiModule, webModule]) {
}

configure(globalModule) {
task enhance(type: CubaEnhancing)
entitiesEnhancing {
main { enabled = true }
}

jar {
manifest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class DbDiagnoseServiceBean implements DbDiagnoseService {
@Inject
DbQueryParser dbQueryParser

@Inject
DbSqlExecutor dbSqlExecutor

@Inject
TimeSource timeSource

Expand All @@ -57,7 +60,8 @@ class DbDiagnoseServiceBean implements DbDiagnoseService {
DbQueryResult dbQueryResult
try {
dbQueryResult = getQueryResult(diagnoseType, queryStatement, queryStatements)
diagnoseExecution.handleSuccessfulExecution(dbQueryResult.entities[0].toString())

diagnoseExecution.handleSuccessfulExecution(getResultMessage(dbQueryResult))
diagnoseExecutionLogService.logDiagnoseExecution(diagnoseExecution)
} catch (Exception e) {
dbQueryResult = selectResultFactory.createFromRows([])
Expand All @@ -68,20 +72,30 @@ class DbDiagnoseServiceBean implements DbDiagnoseService {
dbQueryResult
}

protected String getResultMessage(DbQueryResult dbQueryResult) {
String resultMessage = ''
if (dbQueryResult.empty) {
resultMessage = 'Execution successful'
} else {
resultMessage = dbQueryResult.entities[0].toString()
}
resultMessage
}

protected DbQueryResult getQueryResult(DiagnoseType diagnoseType, String queryStatement, Statements queryStatements) {
DbQueryResult sqlSelectResult
DbQueryResult dbQueryResult
switch (diagnoseType) {
case DiagnoseType.JPQL:
sqlSelectResult = executeJpqlStatement(queryStatement, queryStatements)
dbQueryResult = executeJpqlStatement(queryStatement, queryStatements)
break
case DiagnoseType.SQL:
def sql = createSqlConnection(persistence.dataSource)
sqlSelectResult = executeSqlStatement(sql, queryStatement)
dbQueryResult = executeSqlStatement(sql, queryStatements)
break
default:
throw new IllegalArgumentException('DiagnoseType is not supported (' + diagnoseType + ')')
}
sqlSelectResult
dbQueryResult
}

protected DbQueryResult executeJpqlStatement(String queryStatement, Statements queryStatements) {
Expand All @@ -97,9 +111,8 @@ class DbDiagnoseServiceBean implements DbDiagnoseService {
}
}

protected DbQueryResult executeSqlStatement(Sql sql, String queryString) {
def rows = sql.rows(queryString)
selectResultFactory.createFromRows(rows)
protected DbQueryResult executeSqlStatement(Sql sql, Statements queryStatements) {
dbSqlExecutor.executeStatement(sql, queryStatements.statements[0])
}

private DiagnoseExecution createAdHocDiagnose(String sqlStatement, DiagnoseType diagnoseType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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.Statement
import net.sf.jsqlparser.statement.Statements
import net.sf.jsqlparser.statement.alter.Alter
import net.sf.jsqlparser.statement.create.index.CreateIndex
Expand All @@ -22,6 +23,7 @@ import net.sf.jsqlparser.statement.execute.Execute
import net.sf.jsqlparser.statement.insert.Insert
import net.sf.jsqlparser.statement.merge.Merge
import net.sf.jsqlparser.statement.replace.Replace
import net.sf.jsqlparser.statement.select.Select
import net.sf.jsqlparser.statement.truncate.Truncate
import net.sf.jsqlparser.statement.update.Update
import org.springframework.stereotype.Component
Expand All @@ -38,6 +40,9 @@ class DbQueryParser {
@Inject
Messages messages

protected static final SELECT_OPERATIONS = [
Select
]
protected static final EXECUTE_OPERATIONS = [
Execute, SetStatement
]
Expand Down Expand Up @@ -86,26 +91,52 @@ class DbQueryParser {
ScriptManagerUtilLazyHolder.DOMAIN_MODEL_INSTANCE
}
}

boolean containsDataManipulation(Statements statements) {
containsIllegalOperation(statements, DATA_MANIPULATION_OPERATIONS)
}
boolean isDataManipulation(Statement statement) {
containsIllegalOperation(statement, DATA_MANIPULATION_OPERATIONS)
}

boolean containsSelect(Statements statements) {
containsIllegalOperation(statements, SELECT_OPERATIONS)
}

boolean isSelect(Statement statement) {
containsIllegalOperation(statement, SELECT_OPERATIONS)
}

boolean containsSchemaManipulation(Statements statements) {
containsIllegalOperation(statements, SCHEMA_MANIPULATION_OPERATIONS)
}
boolean isSchemaManipulation(Statement statement) {
containsIllegalOperation(statement, SCHEMA_MANIPULATION_OPERATIONS)
}

boolean containsExecuteOperations(Statements statements) {
containsIllegalOperation(statements, EXECUTE_OPERATIONS)
}

protected boolean containsIllegalOperation(Statements statements, dataManipulationOperations) {
boolean isExecuteOperations(Statement statement) {
containsIllegalOperation(statement, EXECUTE_OPERATIONS)
}

protected boolean containsIllegalOperation(Statements statements, List<Class> dataManipulationOperations) {

def statementsResults = statements.statements.collect { Statement statement ->
containsIllegalOperation(statement, dataManipulationOperations)
}

statementsResults.any { it }
}

protected boolean containsIllegalOperation(Statement statement, List<Class> dataManipulationOperations) {

def containsIllegalOperation = false
statements.statements.each { statement ->
dataManipulationOperations.each { operationClass ->
if (operationClass.isAssignableFrom(statement.class)) {
containsIllegalOperation = true
}
dataManipulationOperations.each { operationClass ->
if (operationClass.isAssignableFrom(statement.class)) {
containsIllegalOperation = true
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package de.diedavids.cuba.runtimediagnose.db

import groovy.sql.Sql
import net.sf.jsqlparser.statement.Statement
import org.springframework.stereotype.Component

import javax.inject.Inject

@Component
class DbSqlExecutor {

@Inject
DbQueryParser dbQueryParser

@Inject
SqlSelectResultFactory selectResultFactory


DbQueryResult executeStatement(Sql sql, Statement sqlStatement) {

def rows = []

def queryString = sqlStatement.toString()

if (dbQueryParser.isSelect(sqlStatement)) {
rows = sql.rows(queryString)
}
else if (dbQueryParser.isDataManipulation(sqlStatement)) {
sql.executeUpdate(queryString)
}
else {
rows = sql.execute(queryString)
}
selectResultFactory.createFromRows(rows)
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package de.diedavids.cuba.runtimediagnose

import com.haulmont.cuba.core.global.BeanLocator
import com.haulmont.cuba.core.global.Messages
import com.haulmont.cuba.core.global.Metadata
import com.haulmont.cuba.core.global.UserSessionSource
import com.haulmont.cuba.core.global.UuidSource
import com.haulmont.cuba.core.sys.AppContext
import org.springframework.context.ApplicationContext
import spock.lang.Shared
import spock.lang.Specification


class SpecificationWithApplicationContext extends Specification {

@Shared
Expand All @@ -19,9 +22,17 @@ class SpecificationWithApplicationContext extends Specification {
UserSessionSource sessionSource
@Shared
UuidSource uuidSource
@Shared
BeanLocator beanLocator
@Shared
Metadata metadata

def setup() {
applicationContext = Mock()
beanLocator = Mock()

applicationContext.getBean(BeanLocator.NAME,BeanLocator) >> beanLocator

initBeans()

AppContext.Internals.applicationContext = applicationContext
Expand All @@ -32,31 +43,39 @@ class SpecificationWithApplicationContext extends Specification {
}

private void initBeans() {
addDefaultBeans(beans)
mockGetBean(beans)
def allBeans = getBeans()
addDefaultBeans(allBeans)
mockGetBean(allBeans)
}

private void mockGetBean(Map<Class, Object> beans) {
for (Map.Entry<Class, Object> bean : beans) {
Class clazz = bean.key
Object instance = bean.value

applicationContext.getBean(clazz) >> instance
beanLocator.get(clazz) >> instance
String name = clazz.NAME
if (name) {
applicationContext.getBean(name) >> instance
applicationContext.getBean(name, clazz) >> instance
beanLocator.get(name) >> instance
beanLocator.get(name, clazz) >> instance
}
}
}

protected void addDefaultBeans(Map<Class, Object> beansFromSpec) {

messages = Mock()
sessionSource = Mock()
uuidSource = Mock()
metadata = Mock()
uuidSource.createUuid() >> UUID.randomUUID()

beansFromSpec.putAll([(Messages): messages, (UserSessionSource): sessionSource, (UuidSource): uuidSource])
beansFromSpec.putAll([
(Messages) : messages,
(UserSessionSource): sessionSource,
(UuidSource) : uuidSource,
(Metadata) : metadata,
])
}

Map<Class, Object> getBeans() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class DbDiagnoseServiceBeanSpec extends Specification {
MockableDbDiagnoseServiceBean dbDiagnoseServiceBean

DbQueryParser dbQueryParser
DbSqlExecutor dbSqlExecutor
SqlSelectResultFactory selectResultFactory
Transaction transaction
Persistence persistence
Expand All @@ -42,6 +43,10 @@ class DbDiagnoseServiceBeanSpec extends Specification {
def setup() {
dbQueryParser = Mock(DbQueryParser)
selectResultFactory = Mock(SqlSelectResultFactory)
dbSqlExecutor = Mock(DbSqlExecutor)

dbSqlExecutor.executeStatement(_,_) >> new DbQueryResult()

transaction = Mock(Transaction)
persistence = Mock(Persistence)
sql = Mock(Sql) {
Expand All @@ -62,6 +67,7 @@ class DbDiagnoseServiceBeanSpec extends Specification {
diagnoseExecutionFactory = Mock(DiagnoseExecutionFactory)
dbDiagnoseServiceBean = new MockableDbDiagnoseServiceBean(
dbQueryParser: dbQueryParser,
dbSqlExecutor: dbSqlExecutor,
selectResultFactory: selectResultFactory,
persistence: persistence,
sql: sql,
Expand Down Expand Up @@ -139,7 +145,7 @@ class DbDiagnoseServiceBeanSpec extends Specification {
dbDiagnoseServiceBean.runSqlDiagnose(sqlString, DiagnoseType.SQL)

then:
1 * sql.rows(sqlString)
1 * dbSqlExecutor.executeStatement(_, sqlStatement) >> new DbQueryResult()
}

def "runSqlDiagnose executes the jpql script if there is at least one result of the jpql parser"() {
Expand Down Expand Up @@ -170,7 +176,7 @@ class DbDiagnoseServiceBeanSpec extends Specification {
given:
def sqlString = 'SELECT * FROM SEC_USER;'

and: "the db parser returns one db statement"
and: "the db parser returns no db statement"
def statements = Mock(Statements)
statements.getStatements() >> []

Expand All @@ -180,7 +186,7 @@ class DbDiagnoseServiceBeanSpec extends Specification {
dbDiagnoseServiceBean.runSqlDiagnose(sqlString, DiagnoseType.SQL)

then:
0 * sql.rows(_)
0 * dbSqlExecutor.executeStatement(_,_)
}

def "runSqlDiagnose executes no sql script if there is no result of the jpql parser"() {
Expand All @@ -198,7 +204,7 @@ class DbDiagnoseServiceBeanSpec extends Specification {
dbDiagnoseServiceBean.runSqlDiagnose(sqlString, DiagnoseType.JPQL)

then:
0 * sql.rows(_)
0 * dbSqlExecutor.executeStatement(_,_)
}

def "runSqlDiagnose executes no jpql script if there is no result of the jpql parser"() {
Expand All @@ -216,7 +222,7 @@ class DbDiagnoseServiceBeanSpec extends Specification {
dbDiagnoseServiceBean.runSqlDiagnose(sqlString, DiagnoseType.JPQL)

then:
0 * sql.rows(_)
0 * dbSqlExecutor.executeStatement(_,_)
}

def "runSqlDiagnose adds metainformation to the diagnoseExecution"() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.diedavids.cuba.runtimediagnose.diagnose

import com.haulmont.cuba.core.global.BuildInfo
import com.haulmont.cuba.core.global.BuildInfo.Content
import com.haulmont.cuba.core.global.Messages
import com.haulmont.cuba.core.global.Metadata
import com.haulmont.cuba.core.global.UuidSource
Expand All @@ -13,7 +14,7 @@ class DiagnoseFileValidationServiceBeanSpec extends SpecificationWithApplication
DiagnoseFileValidationService sut
Metadata metadata
BuildInfo buildInfo
BuildInfo.Content buildInfoContent
Content buildInfoContent

@Override
Map<Class, Object> getBeans() {
Expand All @@ -31,7 +32,7 @@ class DiagnoseFileValidationServiceBeanSpec extends SpecificationWithApplication
buildInfo: buildInfo
)

buildInfoContent = Mock(BuildInfo.Content)
buildInfoContent = Mock(Content)
buildInfo.getContent() >> buildInfoContent
}

Expand Down
Loading

0 comments on commit 9def522

Please sign in to comment.