From 4e337f119df5ee05515f85a6f26075d3667d7707 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Wed, 30 Sep 2015 16:17:20 +0200 Subject: [PATCH] SONAR-6857 ComponentDao with a generic way to search for components --- ...eportComputeMeasureVariationsStepTest.java | 20 +++--- .../step/ReportPersistSnapshotsStepTest.java | 8 +-- .../step/ValidateProjectStepTest.java | 6 +- ...ViewsComputeMeasureVariationsStepTest.java | 22 +++---- .../step/ViewsPersistSnapshotsStepTest.java | 6 +- .../IssueBulkChangeServiceMediumTest.java | 4 +- .../issue/IssueCommentServiceMediumTest.java | 4 +- .../measure/custom/ws/SearchActionTest.java | 4 +- .../project/ws/BulkDeleteActionTest.java | 4 +- .../server/project/ws/DeleteActionTest.java | 4 +- .../server/project/ws/GhostsActionTest.java | 10 +-- .../project/ws/ProvisionedActionTest.java | 4 +- .../ui/ws/ComponentNavigationActionTest.java | 8 +-- .../org/sonar/db/component/ComponentDao.java | 10 ++- .../sonar/db/component/ComponentMapper.java | 5 +- .../sonar/db/component/ComponentQuery.java | 61 +++++++++++++++++ .../org/sonar/db/dialect/AbstractDialect.java | 40 +++++++++++ .../java/org/sonar/db/dialect/Dialect.java | 6 ++ .../sonar/db/dialect/WildcardPosition.java | 25 +++++++ .../sonar/db/component/ComponentMapper.xml | 37 +++++++++++ .../sonar/db/component/ComponentDaoTest.java | 66 +++++++++++++++---- .../sonar/db/component/ComponentDbTester.java | 54 +++++++++++++++ .../sonar/db}/component/SnapshotTesting.java | 8 +-- .../org/sonar/db/dialect/DialectTest.java | 53 +++++++++++++++ 24 files changed, 397 insertions(+), 72 deletions(-) create mode 100644 sonar-db/src/main/java/org/sonar/db/component/ComponentQuery.java create mode 100644 sonar-db/src/main/java/org/sonar/db/dialect/WildcardPosition.java create mode 100644 sonar-db/src/test/java/org/sonar/db/component/ComponentDbTester.java rename {server/sonar-server/src/test/java/org/sonar/server => sonar-db/src/test/java/org/sonar/db}/component/SnapshotTesting.java (90%) create mode 100644 sonar-db/src/test/java/org/sonar/db/dialect/DialectTest.java diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportComputeMeasureVariationsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportComputeMeasureVariationsStepTest.java index a072dfd9a89..1b74b2c70ca 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportComputeMeasureVariationsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportComputeMeasureVariationsStepTest.java @@ -46,8 +46,8 @@ import org.sonar.server.computation.period.PeriodsHolderRule; import org.sonar.test.DbTests; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.server.component.SnapshotTesting.createForComponent; -import static org.sonar.server.component.SnapshotTesting.createForProject; +import static org.sonar.db.component.SnapshotTesting.createForComponent; +import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject; @Category(DbTests.class) public class ReportComputeMeasureVariationsStepTest { @@ -100,7 +100,7 @@ public class ReportComputeMeasureVariationsStepTest { @Test public void do_nothing_when_no_raw_measure() { - SnapshotDto period1ProjectSnapshot = createForProject(PROJECT_DTO); + SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO); dbClient.snapshotDao().insert(session, period1ProjectSnapshot); dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d)); session.commit(); @@ -128,7 +128,7 @@ public class ReportComputeMeasureVariationsStepTest { @Test public void set_variation() { // Project - SnapshotDto period1ProjectSnapshot = createForProject(PROJECT_DTO); + SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO); dbClient.snapshotDao().insert(session, period1ProjectSnapshot); dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d)); @@ -157,11 +157,11 @@ public class ReportComputeMeasureVariationsStepTest { @Test public void set_variations_on_all_periods() { - SnapshotDto period1ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false); - SnapshotDto period2ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false); - SnapshotDto period3ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false); - SnapshotDto period4ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false); - SnapshotDto period5ProjectSnapshot = createForProject(PROJECT_DTO).setLast(false); + SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false); + SnapshotDto period2ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false); + SnapshotDto period3ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false); + SnapshotDto period4ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false); + SnapshotDto period5ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false); dbClient.snapshotDao().insert(session, period1ProjectSnapshot, period2ProjectSnapshot, period3ProjectSnapshot, period4ProjectSnapshot, period5ProjectSnapshot); dbClient.measureDao().insert(session, @@ -197,7 +197,7 @@ public class ReportComputeMeasureVariationsStepTest { @Test public void set_variation_on_all_numeric_metrics() { - SnapshotDto period1ProjectSnapshot = createForProject(PROJECT_DTO); + SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO); dbClient.snapshotDao().insert(session, period1ProjectSnapshot); dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d), diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportPersistSnapshotsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportPersistSnapshotsStepTest.java index e9d0ced4fd6..84bebad7585 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportPersistSnapshotsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportPersistSnapshotsStepTest.java @@ -35,7 +35,7 @@ import org.sonar.db.component.ComponentDto; import org.sonar.db.component.SnapshotDto; import org.sonar.db.component.SnapshotQuery; import org.sonar.db.component.ComponentTesting; -import org.sonar.server.component.SnapshotTesting; +import org.sonar.db.component.SnapshotTesting; import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolderRule; import org.sonar.server.computation.batch.TreeRootHolderRule; import org.sonar.server.computation.component.Component; @@ -282,7 +282,7 @@ public class ReportPersistSnapshotsStepTest extends BaseStepTest { public void persist_snapshots_with_periods() { ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project"); dbClient.componentDao().insert(dbTester.getSession(), projectDto); - SnapshotDto snapshotDto = SnapshotTesting.createForProject(projectDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime()); + SnapshotDto snapshotDto = SnapshotTesting.newSnapshotForProject(projectDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime()); dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto); dbTester.getSession().commit(); periodsHolder.setPeriods(new Period(1, CoreProperties.TIMEMACHINE_MODE_DATE, "2015-01-01", analysisDate, 123L)); @@ -305,7 +305,7 @@ public class ReportPersistSnapshotsStepTest extends BaseStepTest { ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project"); dbClient.componentDao().insert(dbTester.getSession(), projectDto); - SnapshotDto projectSnapshot = SnapshotTesting.createForProject(projectDto); + SnapshotDto projectSnapshot = SnapshotTesting.newSnapshotForProject(projectDto); dbClient.snapshotDao().insert(dbTester.getSession(), projectSnapshot); ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setKey("MODULE_KEY").setName("Module"); @@ -355,7 +355,7 @@ public class ReportPersistSnapshotsStepTest extends BaseStepTest { public void set_no_period_on_snapshots_when_no_period() { ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project"); dbClient.componentDao().insert(dbTester.getSession(), projectDto); - SnapshotDto snapshotDto = SnapshotTesting.createForProject(projectDto); + SnapshotDto snapshotDto = SnapshotTesting.newSnapshotForProject(projectDto); dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto); dbTester.getSession().commit(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ValidateProjectStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ValidateProjectStepTest.java index 7a32cee3485..bc24a015cae 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ValidateProjectStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ValidateProjectStepTest.java @@ -33,7 +33,7 @@ import org.sonar.db.DbClient; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentTesting; -import org.sonar.server.component.SnapshotTesting; +import org.sonar.db.component.SnapshotTesting; import org.sonar.server.computation.batch.BatchReportReaderRule; import org.sonar.server.computation.batch.TreeRootHolderRule; import org.sonar.server.computation.component.Component; @@ -247,7 +247,7 @@ public class ValidateProjectStepTest { ComponentDto project = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY); dbClient.componentDao().insert(dbTester.getSession(), project); - dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.createForProject(project).setCreatedAt(1420088400000L)); // 2015-01-01 + dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newSnapshotForProject(project).setCreatedAt(1420088400000L)); // 2015-01-01 dbTester.getSession().commit(); treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build()); @@ -274,7 +274,7 @@ public class ValidateProjectStepTest { ComponentDto project = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY); dbClient.componentDao().insert(dbTester.getSession(), project); - dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.createForProject(project).setCreatedAt(1433131200000L)); // 2015-06-01 + dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newSnapshotForProject(project).setCreatedAt(1433131200000L)); // 2015-06-01 dbTester.getSession().commit(); treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsComputeMeasureVariationsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsComputeMeasureVariationsStepTest.java index 90408c55709..f498a21c092 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsComputeMeasureVariationsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsComputeMeasureVariationsStepTest.java @@ -46,9 +46,9 @@ import org.sonar.server.computation.period.PeriodsHolderRule; import org.sonar.test.DbTests; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.server.component.SnapshotTesting.createForComponent; -import static org.sonar.server.component.SnapshotTesting.createForProject; -import static org.sonar.server.component.SnapshotTesting.createForView; +import static org.sonar.db.component.SnapshotTesting.createForComponent; +import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject; +import static org.sonar.db.component.SnapshotTesting.newSnapshotForView; @Category(DbTests.class) public class ViewsComputeMeasureVariationsStepTest { @@ -101,7 +101,7 @@ public class ViewsComputeMeasureVariationsStepTest { @Test public void do_nothing_when_no_raw_measure() { - SnapshotDto period1ViewSnapshot = createForView(VIEW_DTO); + SnapshotDto period1ViewSnapshot = newSnapshotForView(VIEW_DTO); dbClient.snapshotDao().insert(session, period1ViewSnapshot); dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.getId(), period1ViewSnapshot.getId(), 60d)); session.commit(); @@ -129,7 +129,7 @@ public class ViewsComputeMeasureVariationsStepTest { @Test public void set_variation() { // View - SnapshotDto period1ViewSnapshot = createForView(VIEW_DTO); + SnapshotDto period1ViewSnapshot = newSnapshotForView(VIEW_DTO); dbClient.snapshotDao().insert(session, period1ViewSnapshot); dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.getId(), period1ViewSnapshot.getId(), 60d)); @@ -158,11 +158,11 @@ public class ViewsComputeMeasureVariationsStepTest { @Test public void set_variations_on_all_periods() { - SnapshotDto period1ViewSnapshot = createForProject(VIEW_DTO).setLast(false); - SnapshotDto period2ViewSnapshot = createForProject(VIEW_DTO).setLast(false); - SnapshotDto period3ViewSnapshot = createForProject(VIEW_DTO).setLast(false); - SnapshotDto period4ViewSnapshot = createForProject(VIEW_DTO).setLast(false); - SnapshotDto period5ViewSnapshot = createForProject(VIEW_DTO).setLast(false); + SnapshotDto period1ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false); + SnapshotDto period2ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false); + SnapshotDto period3ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false); + SnapshotDto period4ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false); + SnapshotDto period5ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false); dbClient.snapshotDao().insert(session, period1ViewSnapshot, period2ViewSnapshot, period3ViewSnapshot, period4ViewSnapshot, period5ViewSnapshot); dbClient.measureDao().insert(session, @@ -198,7 +198,7 @@ public class ViewsComputeMeasureVariationsStepTest { @Test public void set_variation_on_all_numeric_metrics() { - SnapshotDto period1ViewSnapshot = createForProject(VIEW_DTO); + SnapshotDto period1ViewSnapshot = newSnapshotForProject(VIEW_DTO); dbClient.snapshotDao().insert(session, period1ViewSnapshot); dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.getId(), period1ViewSnapshot.getId(), 60d), diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistSnapshotsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistSnapshotsStepTest.java index 432f5cc8777..fb517d68bf2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistSnapshotsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistSnapshotsStepTest.java @@ -51,7 +51,7 @@ import static org.sonar.db.component.ComponentTesting.newProjectCopy; import static org.sonar.db.component.ComponentTesting.newProjectDto; import static org.sonar.db.component.ComponentTesting.newSubView; import static org.sonar.db.component.ComponentTesting.newView; -import static org.sonar.server.component.SnapshotTesting.createForProject; +import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject; import static org.sonar.server.computation.component.Component.Type.PROJECT_VIEW; import static org.sonar.server.computation.component.Component.Type.SUBVIEW; import static org.sonar.server.computation.component.Component.Type.VIEW; @@ -182,8 +182,8 @@ public class ViewsPersistSnapshotsStepTest extends BaseStepTest { public void persist_snapshots_with_periods() { ComponentDto viewDto = save(newView("ABCD").setKey(valueOf(PROJECT_KEY)).setName("Project")); ComponentDto subViewDto = save(newSubView(viewDto, "CDEF", "key").setKey("2")); - SnapshotDto viewSnapshotDto = save(createForProject(viewDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime())); - SnapshotDto subViewSnapshotDto = save(createForProject(subViewDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime())); + SnapshotDto viewSnapshotDto = save(newSnapshotForProject(viewDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime())); + SnapshotDto subViewSnapshotDto = save(newSnapshotForProject(subViewDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime())); dbTester.getSession().commit(); Component subView = ViewsComponent.builder(SUBVIEW, 2).setUuid("ABCD").build(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceMediumTest.java index c68780b4fc6..0facac5f24b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceMediumTest.java @@ -45,7 +45,7 @@ import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleTesting; import org.sonar.db.user.UserDto; import org.sonar.db.component.ComponentTesting; -import org.sonar.server.component.SnapshotTesting; +import org.sonar.db.component.SnapshotTesting; import org.sonar.server.issue.index.IssueIndexer; import org.sonar.server.permission.PermissionChange; import org.sonar.server.permission.PermissionUpdater; @@ -88,7 +88,7 @@ public class IssueBulkChangeServiceMediumTest { project = ComponentTesting.newProjectDto().setKey("MyProject"); tester.get(ComponentDao.class).insert(session, project); - SnapshotDto projectSnapshot = SnapshotTesting.createForProject(project); + SnapshotDto projectSnapshot = SnapshotTesting.newSnapshotForProject(project); tester.get(SnapshotDao.class).insert(session, projectSnapshot); file = ComponentTesting.newFileDto(project).setKey("MyComponent"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceMediumTest.java index d9f4c802d0c..920f3da5950 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceMediumTest.java @@ -44,7 +44,7 @@ import org.sonar.db.issue.IssueDto; import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleTesting; import org.sonar.db.component.ComponentTesting; -import org.sonar.server.component.SnapshotTesting; +import org.sonar.db.component.SnapshotTesting; import org.sonar.server.issue.index.IssueIndexer; import org.sonar.server.permission.PermissionChange; import org.sonar.server.permission.PermissionUpdater; @@ -85,7 +85,7 @@ public class IssueCommentServiceMediumTest { project = ComponentTesting.newProjectDto(); tester.get(ComponentDao.class).insert(session, project); - SnapshotDto projectSnapshot = SnapshotTesting.createForProject(project); + SnapshotDto projectSnapshot = SnapshotTesting.newSnapshotForProject(project); tester.get(SnapshotDao.class).insert(session, projectSnapshot); file = ComponentTesting.newFileDto(project); diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/SearchActionTest.java index 282de27bafe..71fa47a68b5 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/SearchActionTest.java @@ -45,7 +45,7 @@ import org.sonar.db.measure.custom.CustomMeasureDto; import org.sonar.db.metric.MetricDto; import org.sonar.server.component.ComponentFinder; import org.sonar.db.component.ComponentTesting; -import org.sonar.server.component.SnapshotTesting; +import org.sonar.db.component.SnapshotTesting; import org.sonar.server.db.DbClient; import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.ForbiddenException; @@ -208,7 +208,7 @@ public class SearchActionTest { dbClient.customMeasureDao().insert(dbSession, newCustomMeasure(1, metric) .setCreatedAt(yesterday) .setUpdatedAt(yesterday)); - dbClient.snapshotDao().insert(dbSession, SnapshotTesting.createForProject(defaultProject)); + dbClient.snapshotDao().insert(dbSession, SnapshotTesting.newSnapshotForProject(defaultProject)); dbSession.commit(); String response = newRequest() diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java index 71cf3db4433..4238a605471 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java @@ -52,7 +52,7 @@ import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleTesting; import org.sonar.server.component.ComponentCleanerService; import org.sonar.server.component.ComponentFinder; -import org.sonar.server.component.SnapshotTesting; +import org.sonar.db.component.SnapshotTesting; import org.sonar.server.db.DbClient; import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.ForbiddenException; @@ -226,7 +226,7 @@ public class BulkDeleteActionTest { dbClient.deprecatedRuleDao().insert(dbSession, rule); IssueDto issue = IssueTesting.newDto(rule, project, project).setKee("issue-key-" + suffix); dbClient.componentDao().insert(dbSession, project); - SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, SnapshotTesting.createForProject(project)); + SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, SnapshotTesting.newSnapshotForProject(project)); dbClient.issueDao().insert(dbSession, issue); dbSession.commit(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/DeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/DeleteActionTest.java index 1a590fd3df5..4f4bcef3f87 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/DeleteActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/DeleteActionTest.java @@ -50,7 +50,7 @@ import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleTesting; import org.sonar.server.component.ComponentCleanerService; import org.sonar.server.component.ComponentFinder; -import org.sonar.server.component.SnapshotTesting; +import org.sonar.db.component.SnapshotTesting; import org.sonar.server.db.DbClient; import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.ForbiddenException; @@ -248,7 +248,7 @@ public class DeleteActionTest { dbClient.deprecatedRuleDao().insert(dbSession, rule); IssueDto issue = IssueTesting.newDto(rule, project, project).setKee("issue-key-" + suffix); dbClient.componentDao().insert(dbSession, project); - SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, SnapshotTesting.createForProject(project)); + SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, SnapshotTesting.newSnapshotForProject(project)); dbClient.issueDao().insert(dbSession, issue); dbSession.commit(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/GhostsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/GhostsActionTest.java index 124513278f6..0f91139b583 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/GhostsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/GhostsActionTest.java @@ -35,7 +35,7 @@ import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.SnapshotDto; import org.sonar.db.component.ComponentTesting; -import org.sonar.server.component.SnapshotTesting; +import org.sonar.db.component.SnapshotTesting; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsTester; @@ -142,14 +142,14 @@ public class GhostsActionTest { .setName("HBase") .setCreatedAt(DateUtils.parseDateTime("2015-03-04T23:03:44+0100")); dbClient.componentDao().insert(db.getSession(), hBaseProject); - dbClient.snapshotDao().insert(db.getSession(), SnapshotTesting.createForProject(hBaseProject) + dbClient.snapshotDao().insert(db.getSession(), SnapshotTesting.newSnapshotForProject(hBaseProject) .setStatus(SnapshotDto.STATUS_UNPROCESSED)); ComponentDto roslynProject = ComponentTesting.newProjectDto("c526ef20-131b-4486-9357-063fa64b5079") .setKey("com.microsoft.roslyn:roslyn") .setName("Roslyn") .setCreatedAt(DateUtils.parseDateTime("2013-03-04T23:03:44+0100")); dbClient.componentDao().insert(db.getSession(), roslynProject); - dbClient.snapshotDao().insert(db.getSession(), SnapshotTesting.createForProject(roslynProject) + dbClient.snapshotDao().insert(db.getSession(), SnapshotTesting.newSnapshotForProject(roslynProject) .setStatus(SnapshotDto.STATUS_UNPROCESSED)); db.getSession().commit(); @@ -171,7 +171,7 @@ public class GhostsActionTest { .setName("ghost-name-" + id) .setKey("ghost-key-" + id); dbClient.componentDao().insert(db.getSession(), project); - SnapshotDto snapshot = SnapshotTesting.createForProject(project) + SnapshotDto snapshot = SnapshotTesting.newSnapshotForProject(project) .setStatus(SnapshotDto.STATUS_UNPROCESSED); dbClient.snapshotDao().insert(db.getSession(), snapshot); db.getSession().commit(); @@ -183,7 +183,7 @@ public class GhostsActionTest { .setName("analyzed-name-" + id) .setKey("analyzed-key-" + id); dbClient.componentDao().insert(db.getSession(), project); - SnapshotDto snapshot = SnapshotTesting.createForProject(project); + SnapshotDto snapshot = SnapshotTesting.newSnapshotForProject(project); dbClient.snapshotDao().insert(db.getSession(), snapshot); db.getSession().commit(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/ProvisionedActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/ProvisionedActionTest.java index 18a14f58e2b..c0cc0420755 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/ProvisionedActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/ProvisionedActionTest.java @@ -37,7 +37,7 @@ import org.sonar.db.component.ComponentDao; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.SnapshotDto; import org.sonar.db.component.ComponentTesting; -import org.sonar.server.component.SnapshotTesting; +import org.sonar.db.component.SnapshotTesting; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsTester; @@ -74,7 +74,7 @@ public class ProvisionedActionTest { userSessionRule.setGlobalPermissions(GlobalPermissions.PROVISIONING); ComponentDto analyzedProject = ComponentTesting.newProjectDto("analyzed-uuid-1"); componentDao.insert(db.getSession(), newProvisionedProject("1"), newProvisionedProject("2"), analyzedProject); - SnapshotDto snapshot = SnapshotTesting.createForProject(analyzedProject); + SnapshotDto snapshot = SnapshotTesting.newSnapshotForProject(analyzedProject); dbClient.snapshotDao().insert(db.getSession(), snapshot); db.getSession().commit(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentNavigationActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentNavigationActionTest.java index 1e7bc2c2715..75867abb12e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentNavigationActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentNavigationActionTest.java @@ -50,7 +50,7 @@ import org.sonar.db.dashboard.DashboardDto; import org.sonar.db.property.PropertyDto; import org.sonar.server.component.ComponentFinder; import org.sonar.db.component.ComponentTesting; -import org.sonar.server.component.SnapshotTesting; +import org.sonar.db.component.SnapshotTesting; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.tester.UserSessionRule; @@ -194,7 +194,7 @@ public class ComponentNavigationActionTest { ComponentDto project = ComponentTesting.newProjectDto("abcd") .setKey("polop").setName("Polop").setLanguage("xoo"); dbClient.componentDao().insert(dbTester.getSession(), project); - dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.createForProject(project)); + dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newSnapshotForProject(project)); dbTester.getSession().commit(); userSessionRule.addProjectUuidPermissions(UserRole.USER, "abcd"); @@ -208,7 +208,7 @@ public class ComponentNavigationActionTest { ComponentDto project = ComponentTesting.newProjectDto("abcd") .setKey("polop").setName("Polop").setLanguage("xoo"); dbClient.componentDao().insert(dbTester.getSession(), project); - dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.createForProject(project)); + dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newSnapshotForProject(project)); dbTester.getSession().commit(); userSessionRule @@ -354,7 +354,7 @@ public class ComponentNavigationActionTest { .setPath(directory.path()); dbClient.componentDao().insert(dbTester.getSession(), project, module, directory, file); - SnapshotDto projectSnapshot = SnapshotTesting.createForProject(project); + SnapshotDto projectSnapshot = SnapshotTesting.newSnapshotForProject(project); dbClient.snapshotDao().insert(dbTester.getSession(), projectSnapshot); SnapshotDto moduleSnapshot = SnapshotTesting.createForComponent(module, projectSnapshot); dbClient.snapshotDao().insert(dbTester.getSession(), moduleSnapshot); diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java index 96199463731..4f4a9cc49ca 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java +++ b/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java @@ -66,6 +66,14 @@ public class ComponentDao implements Dao { return componentDto.get(); } + public List selectByQuery(DbSession session, ComponentQuery query, int offset, int limit) { + return mapper(session).selectByQuery(query, new RowBounds(offset, limit)); + } + + public int countByQuery(DbSession session, ComponentQuery query) { + return mapper(session).countByQuery(query); + } + public boolean existsById(Long id, DbSession session) { return mapper(session).countById(id) > 0; } @@ -186,7 +194,7 @@ public class ComponentDao implements Dao { * Does not return component copies */ public List selectComponents(DbSession session, Collection qualifiers, int offset, int limit, @Nullable String query) { - Map parameters = newHashMapWithExpectedSize(2); + Map parameters = newHashMapWithExpectedSize(3); addProjectQualifier(parameters); addPartialQueryParameterIfNotNull(parameters, query); addQualifiers(parameters, qualifiers); diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java index dc2ab91b60b..774c14a378d 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java @@ -57,6 +57,10 @@ public interface ComponentMapper { List selectExistingUuids(@Param("uuids") Collection uuids); + List selectByQuery(ComponentQuery query, RowBounds rowBounds); + + int countByQuery(ComponentQuery query); + /** * Return all project (PRJ/TRK) uuids */ @@ -127,5 +131,4 @@ public interface ComponentMapper { void update(ComponentDto componentDto); void delete(long componentId); - } diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentQuery.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentQuery.java new file mode 100644 index 00000000000..6c4e361ddfc --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/component/ComponentQuery.java @@ -0,0 +1,61 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.db.component; + +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import org.sonar.db.Database; + +import static com.google.common.base.Preconditions.checkArgument; +import static org.sonar.db.dialect.WildcardPosition.AFTER; + +public class ComponentQuery { + private final Database database; + private final String nameOrKeyQuery; + private final String[] qualifiers; + + public ComponentQuery(Database database, @Nullable String nameOrKeyQuery, String... qualifiers) { + checkArgument(qualifiers.length > 0, "At least one qualifier must be provided"); + + this.database = database; + this.nameOrKeyQuery = nameOrKeyQuery; + this.qualifiers = qualifiers; + } + + public String[] getQualifiers() { + return qualifiers; + } + + @CheckForNull + public String getNameOrKeyQuery() { + return nameOrKeyQuery; + } + + @CheckForNull + public String getNameOrKeyQueryToSqlForResourceIndex() { + return database.getDialect().buildLikeValue(nameOrKeyQuery, AFTER).toLowerCase(); + } + + @CheckForNull + public String getNameOrKeyQueryToSqlForProjectKey() { + return database.getDialect().buildLikeValue(nameOrKeyQuery, AFTER); + } +} diff --git a/sonar-db/src/main/java/org/sonar/db/dialect/AbstractDialect.java b/sonar-db/src/main/java/org/sonar/db/dialect/AbstractDialect.java index 236c90acd1b..5aeb289f495 100644 --- a/sonar-db/src/main/java/org/sonar/db/dialect/AbstractDialect.java +++ b/sonar-db/src/main/java/org/sonar/db/dialect/AbstractDialect.java @@ -87,4 +87,44 @@ abstract class AbstractDialect implements Dialect { public int getScrollSingleRowFetchSize() { return 1; } + + @Override + public String buildLikeValue(String value, WildcardPosition wildcardPosition) { + String escapedValue = escapePercentAndUnderscore(value); + String wildcard = "%"; + switch (wildcardPosition) { + case BEFORE: + escapedValue = wildcard + escapedValue; + break; + case AFTER: + escapedValue += wildcard; + break; + case BEFORE_AND_AFTER: + escapedValue = wildcard + escapedValue + wildcard; + break; + default: + throw new UnsupportedOperationException("Unhandled WildcardPosition: " + wildcardPosition); + } + + return appendEscapeBackslachForSomeDb(escapedValue); + } + + /** + * Replace escape percent and underscore by adding a slash just before + */ + private static String escapePercentAndUnderscore(String value) { + return value + .replaceAll("%", "\\\\%") + .replaceAll("_", "\\\\_"); + } + + private String appendEscapeBackslachForSomeDb(String value) { + return isOracleOrMsSql() + ? (value + " ESCAPE '\\'") + : value; + } + + private boolean isOracleOrMsSql() { + return getId().equals(Oracle.ID) || getId().equals(MsSql.ID); + } } diff --git a/sonar-db/src/main/java/org/sonar/db/dialect/Dialect.java b/sonar-db/src/main/java/org/sonar/db/dialect/Dialect.java index 3d0a0f6e730..a6fba169d91 100644 --- a/sonar-db/src/main/java/org/sonar/db/dialect/Dialect.java +++ b/sonar-db/src/main/java/org/sonar/db/dialect/Dialect.java @@ -89,4 +89,10 @@ public interface Dialect { * @return a boolean */ boolean supportsMigration(); + + /** + * Returns an escaped value in parameter, with the desired wildcards. + * Suitable to be used in a like sql query + */ + String buildLikeValue(String value, WildcardPosition wildcardPosition); } diff --git a/sonar-db/src/main/java/org/sonar/db/dialect/WildcardPosition.java b/sonar-db/src/main/java/org/sonar/db/dialect/WildcardPosition.java new file mode 100644 index 00000000000..0b8780b58e7 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/dialect/WildcardPosition.java @@ -0,0 +1,25 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.db.dialect; + +public enum WildcardPosition { + BEFORE, AFTER, BEFORE_AND_AFTER +} diff --git a/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml b/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml index da674300494..8d846a61e39 100644 --- a/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml @@ -250,6 +250,43 @@ ORDER BY UPPER(p.name), p.name + + + + + + from projects p + + AND p.enabled=${_true} + AND p.copy_resource_id is null + AND p.qualifier in + + #{qualifier} + + + AND (exists ( + select 1 + from resource_index ri + where + ri.resource_id=p.id + AND ri.qualifier in + + #{qualifier} + + AND ri.kee like #{nameOrKeyQueryToSqlForResourceIndex}) + OR p.kee like #{nameOrKeyQueryToSqlForProjectKey}) + + + +