Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace joins with select #637

Merged
merged 12 commits into from
Aug 28, 2024
3 changes: 1 addition & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[submodule "configs"]
path = configs
url = https://github.com/OpenSRP/opensrp-server-configs
branch = master
url = [email protected]:opensrp/opensrp-server-configs.git
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<artifactId>opensrp-server-core</artifactId>
<packaging>jar</packaging>
<version>2.14.9-SNAPSHOT</version>
<version>2.14.10-SNAPSHOT</version>
<name>opensrp-server-core</name>
<description>OpenSRP Server Core module</description>
<url>https://github.com/OpenSRP/opensrp-server-core</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class StocksRepositoryImpl extends BaseRepositoryImpl<Stock> implements S

@Autowired
private CustomStockMapper stockMapper;

@Autowired
private CustomStockMetadataMapper stockMetadataMapper;

Expand All @@ -49,45 +49,45 @@ public Stock get(String id) {
return null;
}
org.opensrp.domain.postgres.Stock pgStock = stockMetadataMapper.selectByDocumentId(id);

return convert(pgStock);
}

@Transactional
@Override
public void add(Stock entity) {
if (entity == null) {
return;
}

if (retrievePrimaryKey(entity) != null) { //Stock already added
return;
}

if (StringUtils.isBlank(entity.getId()))
entity.setId(UUID.randomUUID().toString());
setRevision(entity);

org.opensrp.domain.postgres.Stock pgStock = convert(entity, null);
if (pgStock == null) {
throw new IllegalStateException();
}

int rowsAffected = stockMapper.insertSelectiveAndSetId(pgStock);

if (rowsAffected < 1 || pgStock.getId() == null) {
throw new IllegalStateException();
}

updateServerVersion(pgStock, entity);

StockMetadata stockMetadata = createMetadata(entity, pgStock.getId());
if (stockMetadata != null) {
stockMetadataMapper.insertSelective(stockMetadata);
}

}

private void updateServerVersion(org.opensrp.domain.postgres.Stock pgStock, Stock entity) {
long serverVersion = stockMapper.selectServerVersionByPrimaryKey(pgStock.getId());
entity.setServerVersion(serverVersion);
Expand All @@ -98,85 +98,85 @@ private void updateServerVersion(org.opensrp.domain.postgres.Stock pgStock, Stoc
throw new IllegalStateException();
}
}

@Transactional
@Override
public void update(Stock entity) {
if (entity == null) {
return;
}

Long id = retrievePrimaryKey(entity);
if (id == null) { // Stock not added
throw new IllegalStateException();
}

setRevision(entity);
org.opensrp.domain.postgres.Stock pgStock = convert(entity, id);
if (pgStock == null) {
throw new IllegalStateException();
}



int rowsAffected = stockMapper.updateByPrimaryKeyAndGenerateServerVersion(pgStock);
if (rowsAffected < 1) {
throw new IllegalStateException();
}
updateServerVersion(pgStock, entity);

StockMetadata stockMetadata = createMetadata(entity, id);
if (stockMetadata == null) {
throw new IllegalStateException();
}

StockMetadataExample stockMetadataExample = new StockMetadataExample();
stockMetadataExample.createCriteria().andStockIdEqualTo(id);
stockMetadata.setId(stockMetadataMapper.selectByExample(stockMetadataExample).get(0).getId());
stockMetadataMapper.updateByPrimaryKey(stockMetadata);

}

@Override
public List<Stock> getAll() {
List<org.opensrp.domain.postgres.Stock> stocks = stockMetadataMapper.selectMany(new StockMetadataExample(), 0,
DEFAULT_FETCH_SIZE);
return convert(stocks);
}

@Override
public void safeRemove(Stock entity) {
if (entity == null) {
return;
}

Long id = retrievePrimaryKey(entity);
if (id == null) {
return;
}

StockMetadataExample stockMetadataExample = new StockMetadataExample();
stockMetadataExample.createCriteria().andStockIdEqualTo(id);
int rowsAffected = stockMetadataMapper.deleteByExample(stockMetadataExample);
if (rowsAffected < 1) {
return;
}

stockMapper.deleteByPrimaryKey(id);

}

@Override
public List<Stock> findAllByProviderid(String providerid) {
StockMetadataExample stockMetadataExample = new StockMetadataExample();
stockMetadataExample.createCriteria().andProviderIdEqualTo(providerid);
return convert(stockMetadataMapper.selectMany(stockMetadataExample, 0, DEFAULT_FETCH_SIZE));
}

/**
* implements the method equivalent in couch repository that return stocks matching stock type
* id
*
*
* @param stockType the stock type
* @param stockTypeId the stock type id
* @return list of stock of a particluar stock type id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper
namespace="org.opensrp.repository.postgres.mapper.custom.CustomEventMetadataMapper">
<mapper namespace="org.opensrp.repository.postgres.mapper.custom.CustomEventMetadataMapper">

<resultMap id="BaseEventResultMap" type="org.opensrp.domain.postgres.Event">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="json" jdbcType="OTHER" property="json"
typeHandler="org.opensrp.repository.postgres.handler.EventTypeHandler" />
typeHandler="org.opensrp.repository.postgres.handler.EventTypeHandler" />
</resultMap>

<sql id="Base_Event_Column_List">
Expand Down Expand Up @@ -34,7 +33,7 @@
<when test="criterion.listValue">
and em.${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem"
open="(" separator=",">
open="(" separator=",">
#{listItem}
</foreach>
</when>
Expand All @@ -49,7 +48,7 @@
<sql id="Rowbounds_Example_Where_Clause">
<where>
<foreach collection="example.oredCriteria" item="criteria"
separator="or">
separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
Expand All @@ -68,7 +67,7 @@
<when test="criterion.listValue">
and em.${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem"
open="(" separator=",">
open="(" separator=",">
#{listItem}
</foreach>
</when>
Expand All @@ -81,14 +80,14 @@
</sql>

<select id="selectByDocumentId" parameterType="java.lang.String"
resultMap="BaseEventResultMap">
resultMap="BaseEventResultMap">
select
<include refid="Base_Event_Column_List" />
from core.event_metadata em
join core.event e on em.event_id = e.id
where em.document_id = #{documentId,jdbcType=VARCHAR} and em.date_deleted is null
from core.event e
where e.id = (select em.event_id from core.event_metadata em
where em.document_id = #{documentId,jdbcType=VARCHAR} and em.date_deleted is null)
</select>

<select id="selectPrimaryKey" parameterType="org.opensrp.domain.postgres.EventMetadataExample"
resultType="java.lang.Long">
select event_id
Expand All @@ -99,45 +98,49 @@
</select>

<select id="selectMany" parameterType="org.opensrp.domain.postgres.EventMetadataExample"
resultMap="BaseEventResultMap">
resultMap="BaseEventResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Event_Column_List" />
from core.event e
where e.id in (
select em.event_id
from core.event_metadata em
join core.event e on em.event_id = e.id
<if test="_parameter != null">
<include refid="Event_Example_Where_Clause" />
</if>
)
<if test="orderByClause != null">
order by em.${orderByClause}
order by e.${orderByClause}
</if>
</select>



<select id="selectNotInOpenMRSByServerVersion" parameterType="map"
resultMap="BaseEventResultMap">
resultMap="BaseEventResultMap">
select
<include refid="Base_Event_Column_List" />
from core.event_metadata em
join core.event e on em.event_id = e.id
where ( em.openmrs_uuid is null or em.openmrs_uuid ='' )
and em.server_version between #{from,jdbcType=BIGINT} and #{to,jdbcType=BIGINT}
and em.date_deleted is null
from core.event e
where e.id in (select em.event_id from core.event_metadata em
where (em.openmrs_uuid is null or em.openmrs_uuid = '')
and em.server_version between #{from,jdbcType=BIGINT} and #{to,jdbcType=BIGINT}
and em.date_deleted is null)
LIMIT #{limit}
</select>

<select id="selectNotInOpenMRSByServerVersionAndType" parameterType="map"
resultMap="BaseEventResultMap">
resultMap="BaseEventResultMap">
select
<include refid="Base_Event_Column_List" />
from core.event_metadata em
join core.event e on em.event_id = e.id
where ( em.openmrs_uuid is null or em.openmrs_uuid ='' )
and em.event_type = #{eventType,jdbcType=VARCHAR}
and em.server_version between #{from,jdbcType=BIGINT} and #{to,jdbcType=BIGINT}
and em.date_deleted is null
from core.event e
where e.id in (select em.event_id from core.event_metadata em
where (em.openmrs_uuid is null or em.openmrs_uuid = '')
and em.event_type = #{eventType,jdbcType=VARCHAR}
and em.server_version between #{from,jdbcType=BIGINT} and #{to,jdbcType=BIGINT}
and em.date_deleted is null)
LIMIT #{limit}
</select>

Expand All @@ -148,17 +151,18 @@
distinct
</if>
<include refid="Base_Event_Column_List" />
from core.event_metadata em
inner join core.event e on em.event_id = e.id
from core.event e
where e.id in (select em.event_id from core.event_metadata em
<if test="_parameter != null">
<include refid="Rowbounds_Example_Where_Clause" />
</if>
</if>)
<if test="example.orderByClause != null">
order by em.${example.orderByClause}
order by ${example.orderByClause}
</if>
LIMIT #{limit} OFFSET #{offset}
</select>


<select id="selectManyIds" resultType="java.lang.String">
select
<if test="example.distinct">
Expand All @@ -170,11 +174,11 @@
<include refid="Rowbounds_Example_Where_Clause" />
</if>
<if test="example.orderByClause != null">
order by em.${example.orderByClause}
order by ${example.orderByClause}
</if>
LIMIT #{limit} OFFSET #{offset}
</select>

<select id="selectManyBaseEntityIds" resultType="java.lang.String">
select
<if test="example.distinct">
Expand All @@ -194,10 +198,10 @@
resultMap="BaseEventResultMap">
select
<include refid="Base_Event_Column_List" />
from core.event_metadata em
join core.event e on em.event_id = e.id
where em.base_entity_id= #{baseEntityId,jdbcType=VARCHAR} and
em.plan_identifier = #{planIdentifier,jdbcType=VARCHAR}
from core.event e
where e.id = (select em.event_id from core.event_metadata em
where em.base_entity_id = #{baseEntityId,jdbcType=VARCHAR} and
em.plan_identifier = #{planIdentifier,jdbcType=VARCHAR})
</select>

</mapper>
Empty file.
Loading
Loading