Android Room 2.1.0-alpha03 #DatabaseView Missing syntax check and view name - android-room

I'm using roomVersion = '2.1.0-alpha03' on the AndroidX branch
// Room (androidx)
implementation "androidx.room:room-runtime:$rootProject.roomVersion"
implementation "androidx.room:room-rxjava2:$rootProject.roomVersion"
implementation "androidx.room:room-guava:$rootProject.roomVersion"
testImplementation "androidx.room:room-testing:$rootProject.roomVersion"
annotationProcessor "androidx.room:room-compiler:$rootProject.roomVersion"
Here is my #DatabaseView query:
#DatabaseView("SELECT * FROM DataPointJoin " +
"INNER JOIN Device ON Device.device_uid = DataPointJoin.device_uid " +
"INNER JOIN Measurement ON Measurement.measurement_uid = DataPointJoin.measurement_uid " +
"INNER JOIN Location ON Location.location_uid = DataPointJoin.location_uid " +
"INNER JOIN User ON User.user_uid = DataPointJoin.user_uid " +
"INNER JOIN SyncRecord ON SyncRecord.sync_uid = DataPointJoin.uid ")
And here is an example of how I'm using it:
#Query("SELECT * FROM DataPoint WHERE DataPoint.uid = :uid")
Flowable<Optional<DataPoint>> get(String uid);
When using the #DatabaseView() to encapsulate a join into a View, and after clean and rebuild:
My #DatabaseView query isn't checked or highlighted like in #Query(), just a plain long string.
Compiles, but IDE can't resolve the View symbol.

Related

Spring Data with interface projection with Subquery

i'm working on a project with spring data jpa and Oracle data base.
i have a problem with spring data jpa in interface projection.
when i try the query in HQL-Console it returns the correct result,but when i use it in a request,it returns this line:
[org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap#2f163567]
//Repository
#Query(value = "select m.codUseridPstme as codUseridPstme from OCMCharts o" +
" join o.ocmUnitsByChartsId u join u.ocmPostsByUnitsId p " +
"join p.ocmPostmemsByPostsId m where lower(trim(o.codCodeChrt)) =lower(trim(:chartCode))" +
" and p.flgIsmanagerPost=true " +
"and p.staPoststatePost='ACTIVE' and m.staMemberstatusPstme='ACTIVE' " +
"and u.codUnitcodeUnts in (select u.codUnitcodeUnts from OCMCharts o " +
"join o.ocmUnitsByChartsId u "+
"join u.ocmPostsByUnitsId p " +
"join p.ocmPostmemsByPostsId m " +
"where lower(trim(o.codCodeChrt)) =lower(trim(:chartCode)) and u.staStatusUnts='ACTIVE' and m.codUseridPstme=:userName)")
List<OCMUserNameDTO> getUnitManagerListByChartCodeAndUnitCodesIn(#Param("chartCode") String chartCode, #Param("userName") String userName);
//DTO
public interface OCMUserNameDTO {
String getCodUseridPstme();
}
//Console Query
select
ocmpostmem3_.COD_USERID_PSTME as col_0_0_
from
OCM.OCM_CHARTS ocmcharts0_
inner join
OCM.OCM_UNITS ocmunitsby1_
on ocmcharts0_.CHARTS_ID=ocmunitsby1_.CHRT_CHARTS_ID
inner join
OCM.OCM_POSTS ocmpostsby2_
on ocmunitsby1_.UNITS_ID=ocmpostsby2_.UNTS_UNITS_ID
inner join
OCM.OCM_POSTMEM ocmpostmem3_
on ocmpostsby2_.POSTS_ID=ocmpostmem3_.POST_POSTS_ID
where
lower(trim(ocmcharts0_.COD_CODE_CHRT))=lower(trim(?))
and ocmpostsby2_.FLG_ISMANAGER_POST=1
and ocmpostsby2_.STA_POSTSTATE_POST='ACTIVE'
and ocmpostmem3_.STA_MEMBERSTATUS_PSTME='ACTIVE'
and (
ocmunitsby1_.COD_UNITCODE_UNTS in (
select
ocmunitsby5_.COD_UNITCODE_UNTS
from
OCM.OCM_CHARTS ocmcharts4_
inner join
OCM.OCM_UNITS ocmunitsby5_
on ocmcharts4_.CHARTS_ID=ocmunitsby5_.CHRT_CHARTS_ID
inner join
OCM.OCM_POSTS ocmpostsby6_
on ocmunitsby5_.UNITS_ID=ocmpostsby6_.UNTS_UNITS_ID
inner join
OCM.OCM_POSTMEM ocmpostmem7_
on ocmpostsby6_.POSTS_ID=ocmpostmem7_.POST_POSTS_ID
where
lower(trim(ocmcharts4_.COD_CODE_CHRT))=lower(trim(?))
and ocmunitsby5_.STA_STATUS_UNTS='ACTIVE'
and ocmpostmem7_.COD_USERID_PSTME=?
)
)
thanks for answers.
i wrote a .toString() method and was wrong.it was made a conflict with return type of interface projection.
List<OCMUserNameDTO> users = ocmPostmemService.getManagerUsernameOfPersonByChartCodeAndUserName(chartCode, userName);
return new ResponseEntity<>(users.toString(), HttpStatus.OK);
//DTO
public interface OCMUserNameDTO {
String getCodUseridPstme();
}
thanks!

How to add a dynamic where in #Query?

I need to add my where conditions based in my filter object. If some value in the filter object is null, isn't necessary add the where condition of this parameter.
How to add a dynamic where?
Exemple:
FilterObject{
name: "Teste";
age: null;
}
#Query(value="SELECT id FROM user WHERE name = FilterObject.name", nativeQuery = true)
public List<User> getUsers(???)
Other example:
FilterObject{
name: "Teste";
age: 12;
}
#Query(value="SELECT id FROM user WHERE name = FilterObject.name AND age = FilterObject.age", nativeQuery = true)
public List<User> getUsers(???)
This is a highlevel example. Isnt the correct code. But... Is the necessary to understand my question
If a parameter of my filter is null this parameter isnt used in my where
Im Using spring boot with Spring Data JPA and my Query stay in my repository a Repository.
My Original request:
#Query(value = " SELECT "
+ " solicitacao.nm_orgao AS nmOrgao, "
+ " solicitacao.ds_ano_exercicio AS exercicio, "
+ " solicitacao.nm_organismo AS nmOrganismo, "
+ " dadosBancarios.valor AS valor, "
+ " dadosBancarios.valor * dadosBancarios.vl_taxa_cambio AS valorReais, "
+ " dadosFinanceiros.vl_dotacao_disponivel AS vlDotacaoDisponivel, "
+ " solicitacao.id_solicitacao AS codigoPagamento "
+ " solicitacao.tp_solicitacao_status AS idFase, "
+ " organismo.id_orgao AS idOrgao, "
+ " solicitacao.id_organismo AS idOrganismo, "
+ " pagamentos.id_status_pagamento AS statusPagamento "
+ " FROM "
+ " tbs_solicitacao solicitacao "
+ " JOIN tbs_dados_bancarios dadosBancarios ON solicitacao.id_solicitacao = dadosBancarios.id_solicitacao "
+ " JOIN tbs_dados_financeiros dadosFinanceiros ON dadosFinanceiros.id_solicitacao = solicitacao.id_solicitacao "
+ " JOIN tbs_organismo organismo ON organismo.id_organismo = solicitacao.id_organismo "
+ " JOIN tbs_pagamentos pagamentos ON dadosFinanceiros.id_dados_financeiros = pagamentos.id_dados_financeiros ",nativeQuery = true)
List<Object[]> getRelatoriosProgramacao();
You can use Spring Data JPA specifications:
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#specifications
You simply have to implement the method toPredicate:
Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder);
In this method you get root, query and the CriteriaBuilder to create a Criteria API where predicate.
Event methods like findAll take a Specification.

#Query not recognizing parameters if they are inside single quote in Spring Framework

I have a big problem with Spring Data in #Query.
I have the following query :
SELECT created_at::DATE "date",
count(*)
FROM
absence
WHERE
created_at::DATE between '2018-05-27' AND '2018-05-31'
GROUP BY created_at::DATE;
this query works in postgres without any problem now in spring to use it :
#Query("SELECT created_at\\:\\:DATE \"date\",count(*) FROM absence " +
"WHERE created_at\\:\\:DATE between '?1' AND '?2' " +
"GROUP created_at\\:\\:DATE", nativeQuery = true)
fun getAbsenceCount(beginDate: String,endDate: String): List<AbsenceCount>
This query doesn't work at all, the problem is that spring can't recognize the parather beginDate (?1) & endDate (?2).
I tried a lot of solution from stackoverflow like solution and solution 2 but I can't get rid of this problem.
I don't know if it's intented or it's a bug in spring.
Try to simply use the classic SQL cast function:
#Query(value = "" +
"select " +
" cast(a.created_at as date) as createdAt, " +
" count(*) as quantity " +
"from " +
" absence a " +
"where " +
" cast(a.created_at as date) between ?1 and ?2 " +
"group " +
" cast(a.created_at as date)" +
"", nativeQuery = true)
fun getAbsenceCount(beginDate: String, endDate: String): List<AbsenceCount>
where AbsenceCount is a projection like this (java):
public interface AbsenceCount {
LocalDate getCreatedAt();
Long getQuantity();
}

Alias Column In Resultset

I have a query in backend spring project, where I create a field alias named agent_id but I don´t have that column in Model. What is the best approach for showing this as a field in table view in front end???.
Here´s the query:
Query query = em.createNativeQuery("SELECT" +
" ips.*, " +
" s.mls_id AS imported," +
" p.INTEGRATOR_SALES_ASSOCIATED_ID AS agent_id " +
"FROM test1.ilist_property_summary ips " +
" LEFT JOIN test1.statistics s ON s.mls_id = ips.INTEGRATOR_PROPERTY_ID " +
" LEFT JOIN test1.person p ON p.INTEGRATOR_SALES_ASSOCIATED_ID = ips.INTEGRATOR_SALES_ASSOCIATED_ID " +
"WHERE ips.AGENCY_ID = :agencyId AND (s.statistics_type = 1 OR s.statistics_type IS NULL) AND ips.ORIG_LISTING_DATE BETWEEN :startDate AND :endDate");
query.setParameter("agencyId", agencyId)
.setParameter("startDate", DateUtil.asDate(startDate), TemporalType.DATE)
.setParameter("endDate", DateUtil.asDate(endDate), TemporalType.DATE);
return (List<PropertyVO>) query.getResultList().stream().map(o -> processProperty((Object[]) o)).collect(Collectors.<PropertyVO>toList());
I already have a field that shows agent_id, but not fetched with same field of table Person. I need to retrieve that field agent id alias fetched from left join.
I followed another approach:
Query query = em.createNativeQuery("SELECT" +
" ips.id, ips.agency_id, ips.integrator_property_id, ips.orig_listing_date, ips.contract_type," +
" ips.transaction_type, ips.current_listing_price, ips.current_listing_currency, ips.apartment_number, ips.commercial_residential," +
" ips.commission_percent, ips.commission_value, ips.property_type, ips.street_name, ips.street_number," +
" s.mls_id AS imported," +
" p.INTEGRATOR_SALES_ASSOCIATED_ID AS INTEGRATOR_SALES_ASSOCIATED_ID" +
" FROM test1.ilist_property_summary ips " +
" LEFT JOIN test1.statistics s ON s.mls_id = ips.INTEGRATOR_PROPERTY_ID " +
" LEFT JOIN test1.person p ON p.INTEGRATOR_SALES_ASSOCIATED_ID = ips.INTEGRATOR_SALES_ASSOCIATED_ID " +
"WHERE ips.AGENCY_ID = :agencyId AND (s.statistics_type = 1 OR s.statistics_type IS NULL) AND ips.ORIG_LISTING_DATE BETWEEN :startDate AND :endDate ");
Here field
INTEGRATOR_SALES_ASSOCIATED_ID
is taken from table p, and not from ips. I just selected specific fields so this one could be taken from the other table.

Sub query with lambda expression

I need lambda expression for the below query.
select tb_device.lat,tb_device.lng,tb_device.speed,tb_device.trackedOn, IL.DeviceIcon, IL.speedLimit, " +
"IL.deviceId, IL.deviceName, tb_device.Location, tb_device.IsExist " +
"from( select CONVERT (DATE, max( trackedOn)) as trackedDate , inventoryLogId as InventoryId from tb_devicelog " +
"group by inventoryLogId) as tb " +
"inner join tb_devicelog as tb_device on tb.InventoryId=tb_device.inventoryLogId and " +
"tb.trackedDate= CONVERT (DATE, tb_device.trackedOn) " +
"inner join tb_inventorylog IL on tb_device.inventorylogid=IL.id " +
"inner join tb_Logins ln on IL.assignedToCustId = ln.customers_id where ln.userName = 'cadmin' and IL.id in(1,3,2 )"
You could try Linqer which is a SQL to LINQ converter:
http://www.sqltolinq.com/

Resources