Skip to content

Commit

Permalink
Drop usages of MyBatis#openSession()
Browse files Browse the repository at this point in the history
in favor of DbClient#openSession()
  • Loading branch information
Simon Brandhof committed Feb 26, 2017
1 parent 03acebc commit 76a4e8f
Show file tree
Hide file tree
Showing 58 changed files with 464 additions and 990 deletions.
11 changes: 7 additions & 4 deletions pom.xml
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.sonarsource.parent</groupId> <groupId>org.sonarsource.parent</groupId>
Expand Down Expand Up @@ -1219,7 +1220,8 @@
<goal>compile</goal> <goal>compile</goal>
</goals> </goals>
<configuration> <configuration>
<protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact> <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}
</protocArtifact>
<protoSourceRoot>${project.basedir}/src/main/protobuf</protoSourceRoot> <protoSourceRoot>${project.basedir}/src/main/protobuf</protoSourceRoot>
<outputDirectory>${project.build.directory}/generated-sources/protobuf</outputDirectory> <outputDirectory>${project.build.directory}/generated-sources/protobuf</outputDirectory>
</configuration> </configuration>
Expand Down Expand Up @@ -1275,7 +1277,8 @@
<goal>test-compile</goal> <goal>test-compile</goal>
</goals> </goals>
<configuration> <configuration>
<protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact> <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}
</protocArtifact>
<protoTestSourceRoot>${project.basedir}/src/test/protobuf</protoTestSourceRoot> <protoTestSourceRoot>${project.basedir}/src/test/protobuf</protoTestSourceRoot>
<outputDirectory>${project.build.directory}/generated-test-sources/protobuf</outputDirectory> <outputDirectory>${project.build.directory}/generated-test-sources/protobuf</outputDirectory>
</configuration> </configuration>
Expand Down
Expand Up @@ -123,7 +123,6 @@
import org.sonar.server.plugins.privileged.PrivilegedPluginsBootstraper; import org.sonar.server.plugins.privileged.PrivilegedPluginsBootstraper;
import org.sonar.server.plugins.privileged.PrivilegedPluginsStopper; import org.sonar.server.plugins.privileged.PrivilegedPluginsStopper;
import org.sonar.server.property.InternalPropertiesImpl; import org.sonar.server.property.InternalPropertiesImpl;
import org.sonar.server.qualityprofile.QProfileLookup;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex; import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.rule.CommonRuleDefinitionsImpl; import org.sonar.server.rule.CommonRuleDefinitionsImpl;
Expand Down Expand Up @@ -303,7 +302,6 @@ private static Object[] level4Components() {
XMLProfileSerializer.class, XMLProfileSerializer.class,
AnnotationProfileParser.class, AnnotationProfileParser.class,
Rules.QProfiles.class, Rules.QProfiles.class,
QProfileLookup.class,


// rule // rule
RuleIndexer.class, RuleIndexer.class,
Expand Down
Expand Up @@ -88,7 +88,7 @@ public void real_start() throws IOException {
assertThat(picoContainer.getComponentAdapters()) assertThat(picoContainer.getComponentAdapters())
.hasSize( .hasSize(
CONTAINER_ITSELF CONTAINER_ITSELF
+ 79 // level 4 + 78 // level 4
+ 4 // content of CeConfigurationModule + 4 // content of CeConfigurationModule
+ 3 // content of CeHttpModule + 3 // content of CeHttpModule
+ 5 // content of CeQueueModule + 5 // content of CeQueueModule
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/ */
package org.sonar.db.component; package org.sonar.db.component;


import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
Expand All @@ -33,7 +34,6 @@
import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Qualifiers;
import org.sonar.db.Dao; import org.sonar.db.Dao;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;


import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static org.sonar.core.component.ComponentKeys.checkModuleKey; import static org.sonar.core.component.ComponentKeys.checkModuleKey;
Expand All @@ -45,49 +45,25 @@
* @since 3.2 * @since 3.2
*/ */
public class ComponentKeyUpdaterDao implements Dao { public class ComponentKeyUpdaterDao implements Dao {
private static final Set<String> PROJECT_OR_MODULE_QUALIFIERS = ImmutableSet.of(Qualifiers.PROJECT, Qualifiers.MODULE);

private MyBatis mybatis;

public ComponentKeyUpdaterDao(MyBatis mybatis) {
this.mybatis = mybatis;
}


public void updateKey(String projectUuid, String newKey) { private static final Set<String> PROJECT_OR_MODULE_QUALIFIERS = ImmutableSet.of(Qualifiers.PROJECT, Qualifiers.MODULE);
try (DbSession session = mybatis.openSession(true)) {
ComponentKeyUpdaterMapper mapper = session.getMapper(ComponentKeyUpdaterMapper.class);
if (mapper.countResourceByKey(newKey) > 0) {
throw new IllegalArgumentException("Impossible to update key: a component with key \"" + newKey + "\" already exists.");
}


// must SELECT first everything public void updateKey(DbSession dbSession, String projectUuid, String newKey) {
ResourceDto project = mapper.selectProject(projectUuid); ComponentKeyUpdaterMapper mapper = dbSession.getMapper(ComponentKeyUpdaterMapper.class);
String projectOldKey = project.getKey(); if (mapper.countResourceByKey(newKey) > 0) {
List<ResourceDto> resources = mapper.selectProjectResources(projectUuid); throw new IllegalArgumentException("Impossible to update key: a component with key \"" + newKey + "\" already exists.");
resources.add(project); }


// and then proceed with the batch UPDATE at once // must SELECT first everything
runBatchUpdateForAllResources(resources, projectOldKey, newKey, mapper); ResourceDto project = mapper.selectProject(projectUuid);
String projectOldKey = project.getKey();
List<ResourceDto> resources = mapper.selectProjectResources(projectUuid);
resources.add(project);


session.commit(); // and then proceed with the batch UPDATE at once
} runBatchUpdateForAllResources(resources, projectOldKey, newKey, mapper);
}


public Map<String, String> checkModuleKeysBeforeRenaming(String projectUuid, String stringToReplace, String replacementString) { dbSession.commit();
try (DbSession session = mybatis.openSession(false)) {
ComponentKeyUpdaterMapper mapper = session.getMapper(ComponentKeyUpdaterMapper.class);
Map<String, String> result = Maps.newHashMap();
Set<ResourceDto> modules = collectAllModules(projectUuid, stringToReplace, mapper);
for (ResourceDto module : modules) {
String newKey = computeNewKey(module.getKey(), stringToReplace, replacementString);
if (mapper.countResourceByKey(newKey) > 0) {
result.put(module.getKey(), "#duplicate_key#");
} else {
result.put(module.getKey(), newKey);
}
}
return result;
}
} }


public static void checkIsProjectOrModule(ComponentDto component) { public static void checkIsProjectOrModule(ComponentDto component) {
Expand Down Expand Up @@ -117,7 +93,8 @@ public Map<String, Boolean> checkComponentKeys(DbSession dbSession, List<String>
return newComponentKeys.stream().collect(Collectors.toMap(Function.identity(), key -> mapper(dbSession).countResourceByKey(key) > 0)); return newComponentKeys.stream().collect(Collectors.toMap(Function.identity(), key -> mapper(dbSession).countResourceByKey(key) > 0));
} }


public static String computeNewKey(String key, String stringToReplace, String replacementString) { @VisibleForTesting
static String computeNewKey(String key, String stringToReplace, String replacementString) {
return key.replace(stringToReplace, replacementString); return key.replace(stringToReplace, replacementString);
} }


Expand Down
Expand Up @@ -26,31 +26,22 @@
import org.sonar.core.util.stream.Collectors; import org.sonar.core.util.stream.Collectors;
import org.sonar.db.Dao; import org.sonar.db.Dao;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;


import static java.util.Collections.singletonList; import static java.util.Collections.singletonList;
import static org.sonar.db.DatabaseUtils.executeLargeInputs; import static org.sonar.db.DatabaseUtils.executeLargeInputs;


public class IssueChangeDao implements Dao { public class IssueChangeDao implements Dao {


private final MyBatis mybatis;

public IssueChangeDao(MyBatis mybatis) {
this.mybatis = mybatis;
}

public List<FieldDiffs> selectChangelogByIssue(DbSession session, String issueKey) { public List<FieldDiffs> selectChangelogByIssue(DbSession session, String issueKey) {
return selectByTypeAndIssueKeys(session, singletonList(issueKey), IssueChangeDto.TYPE_FIELD_CHANGE) return selectByTypeAndIssueKeys(session, singletonList(issueKey), IssueChangeDto.TYPE_FIELD_CHANGE)
.stream() .stream()
.map(IssueChangeDto::toFieldDiffs) .map(IssueChangeDto::toFieldDiffs)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }


public List<IssueChangeDto> selectChangelogOfNonClosedIssuesByComponent(String componentUuid) { public List<IssueChangeDto> selectChangelogOfNonClosedIssuesByComponent(DbSession session, String componentUuid) {
try (DbSession session = mybatis.openSession(false)) { IssueChangeMapper mapper = mapper(session);
IssueChangeMapper mapper = mapper(session); return mapper.selectChangelogOfNonClosedIssuesByComponent(componentUuid, IssueChangeDto.TYPE_FIELD_CHANGE);
return mapper.selectChangelogOfNonClosedIssuesByComponent(componentUuid, IssueChangeDto.TYPE_FIELD_CHANGE);
}
} }


public List<IssueChangeDto> selectByTypeAndIssueKeys(DbSession session, Collection<String> issueKeys, String changeType) { public List<IssueChangeDto> selectByTypeAndIssueKeys(DbSession session, Collection<String> issueKeys, String changeType) {
Expand Down
Expand Up @@ -21,33 +21,13 @@


import org.sonar.db.Dao; import org.sonar.db.Dao;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;


public class LoadedTemplateDao implements Dao { public class LoadedTemplateDao implements Dao {


private MyBatis mybatis;

public LoadedTemplateDao(MyBatis mybatis) {
this.mybatis = mybatis;
}

public int countByTypeAndKey(String type, String key) {
try (DbSession session = mybatis.openSession(false)) {
return countByTypeAndKey(type, key, session);
}
}

public int countByTypeAndKey(String type, String key, DbSession session) { public int countByTypeAndKey(String type, String key, DbSession session) {
return session.getMapper(LoadedTemplateMapper.class).countByTypeAndKey(type, key); return session.getMapper(LoadedTemplateMapper.class).countByTypeAndKey(type, key);
} }


public void insert(LoadedTemplateDto loadedTemplateDto) {
try (DbSession session = mybatis.openSession(false)) {
insert(loadedTemplateDto, session);
session.commit();
}
}

public void insert(LoadedTemplateDto loadedTemplateDto, DbSession session) { public void insert(LoadedTemplateDto loadedTemplateDto, DbSession session) {
session.getMapper(LoadedTemplateMapper.class).insert(loadedTemplateDto); session.getMapper(LoadedTemplateMapper.class).insert(loadedTemplateDto);
} }
Expand Down
Expand Up @@ -23,64 +23,25 @@
import java.util.Date; import java.util.Date;
import org.sonar.db.Dao; import org.sonar.db.Dao;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;


/**
* @since 4.3
*/
public class QualityGateConditionDao implements Dao { public class QualityGateConditionDao implements Dao {


private final MyBatis myBatis;

public QualityGateConditionDao(MyBatis myBatis) {
this.myBatis = myBatis;
}

public void insert(QualityGateConditionDto newQualityGate, DbSession session) { public void insert(QualityGateConditionDto newQualityGate, DbSession session) {
mapper(session).insert(newQualityGate.setCreatedAt(new Date())); mapper(session).insert(newQualityGate.setCreatedAt(new Date()));
} }


public Collection<QualityGateConditionDto> selectForQualityGate(long qGateId) { public Collection<QualityGateConditionDto> selectForQualityGate(DbSession session, long qGateId) {
try (DbSession dbSession = myBatis.openSession(false)) {
return selectForQualityGate(qGateId, dbSession);
}
}

public Collection<QualityGateConditionDto> selectForQualityGate(long qGateId, DbSession session) {
return mapper(session).selectForQualityGate(qGateId); return mapper(session).selectForQualityGate(qGateId);
} }


public QualityGateConditionDto selectById(long id) {
try (DbSession session = myBatis.openSession(false)) {
return selectById(id, session);
}
}

public QualityGateConditionDto selectById(long id, DbSession session) { public QualityGateConditionDto selectById(long id, DbSession session) {
return mapper(session).selectById(id); return mapper(session).selectById(id);
} }


public void delete(QualityGateConditionDto qGate) {
DbSession session = myBatis.openSession(false);
try {
delete(qGate, session);
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
}

public void delete(QualityGateConditionDto qGate, DbSession session) { public void delete(QualityGateConditionDto qGate, DbSession session) {
mapper(session).delete(qGate.getId()); mapper(session).delete(qGate.getId());
} }


public void update(QualityGateConditionDto qGate) {
try (DbSession dbSession = myBatis.openSession(false)) {
update(qGate, dbSession);
dbSession.commit();
}
}

public void update(QualityGateConditionDto qGate, DbSession session) { public void update(QualityGateConditionDto qGate, DbSession session) {
mapper(session).update(qGate.setUpdatedAt(new Date())); mapper(session).update(qGate.setUpdatedAt(new Date()));
} }
Expand Down
Expand Up @@ -24,99 +24,33 @@
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import org.sonar.db.Dao; import org.sonar.db.Dao;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;


public class QualityGateDao implements Dao { public class QualityGateDao implements Dao {


private final MyBatis myBatis;

public QualityGateDao(MyBatis myBatis) {
this.myBatis = myBatis;
}

public void insert(QualityGateDto newQualityGate) {
DbSession session = myBatis.openSession(false);
try {
insert(session, newQualityGate);
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
}

public QualityGateDto insert(DbSession session, QualityGateDto newQualityGate) { public QualityGateDto insert(DbSession session, QualityGateDto newQualityGate) {
mapper(session).insert(newQualityGate.setCreatedAt(new Date())); mapper(session).insert(newQualityGate.setCreatedAt(new Date()));


return newQualityGate; return newQualityGate;
} }


public Collection<QualityGateDto> selectAll() {
DbSession session = myBatis.openSession(false);
try {
return selectAll(session);
} finally {
MyBatis.closeQuietly(session);
}
}

public Collection<QualityGateDto> selectAll(DbSession session) { public Collection<QualityGateDto> selectAll(DbSession session) {
return mapper(session).selectAll(); return mapper(session).selectAll();
} }


@CheckForNull
public QualityGateDto selectByName(String name) {
DbSession session = myBatis.openSession(false);
try {
return selectByName(session, name);
} finally {
MyBatis.closeQuietly(session);
}
}

@CheckForNull @CheckForNull
public QualityGateDto selectByName(DbSession session, String name) { public QualityGateDto selectByName(DbSession session, String name) {
return mapper(session).selectByName(name); return mapper(session).selectByName(name);
} }


@CheckForNull
public QualityGateDto selectById(long id) {
DbSession session = myBatis.openSession(false);
try {
return selectById(session, id);
} finally {
MyBatis.closeQuietly(session);
}
}

@CheckForNull @CheckForNull
public QualityGateDto selectById(DbSession session, long id) { public QualityGateDto selectById(DbSession session, long id) {
return mapper(session).selectById(id); return mapper(session).selectById(id);
} }


public void delete(QualityGateDto qGate) {
DbSession session = myBatis.openSession(false);
try {
delete(qGate, session);
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
}

public void delete(QualityGateDto qGate, DbSession session) { public void delete(QualityGateDto qGate, DbSession session) {
mapper(session).delete(qGate.getId()); mapper(session).delete(qGate.getId());
} }


public void update(QualityGateDto qGate) {
DbSession session = myBatis.openSession(false);
try {
update(qGate, session);
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
}

public void update(QualityGateDto qGate, DbSession session) { public void update(QualityGateDto qGate, DbSession session) {
mapper(session).update(qGate.setUpdatedAt(new Date())); mapper(session).update(qGate.setUpdatedAt(new Date()));
} }
Expand Down

0 comments on commit 76a4e8f

Please sign in to comment.