@ -22,6 +22,9 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param ;
import org.thingsboard.server.common.data.DeviceTransportType ;
import org.thingsboard.server.common.data.DeviceIdInfo ;
import org.thingsboard.server.common.data.id.CustomerId ;
import org.thingsboard.server.common.data.id.DeviceProfileId ;
import org.thingsboard.server.common.data.id.TenantId ;
import org.thingsboard.server.dao.ExportableEntityRepository ;
import org.thingsboard.server.dao.model.sql.DeviceEntity ;
import org.thingsboard.server.dao.model.sql.DeviceInfoEntity ;
@ -29,16 +32,9 @@ import org.thingsboard.server.dao.model.sql.DeviceInfoEntity;
import java.util.List ;
import java.util.UUID ;
/ * *
* Created by Valerii Sosliuk on 5 / 6 / 2017 .
* /
public interface DeviceRepository extends JpaRepository < DeviceEntity , UUID > , ExportableEntityRepository < DeviceEntity > {
@Query ( "SELECT new org.thingsboard.server.dao.model.sql.DeviceInfoEntity(d, c.title, c.additionalInfo, p.name) " +
"FROM DeviceEntity d " +
"LEFT JOIN CustomerEntity c on c.id = d.customerId " +
"LEFT JOIN DeviceProfileEntity p on p.id = d.deviceProfileId " +
"WHERE d.id = :deviceId" )
@Query ( "SELECT d FROM DeviceInfoEntity d WHERE d.id = :deviceId" )
DeviceInfoEntity findDeviceInfoById ( @Param ( "deviceId" ) UUID deviceId ) ;
@Query ( "SELECT d FROM DeviceEntity d WHERE d.tenantId = :tenantId " +
@ -57,10 +53,7 @@ public interface DeviceRepository extends JpaRepository<DeviceEntity, UUID>, Exp
@Param ( "searchText" ) String searchText ,
Pageable pageable ) ;
@Query ( "SELECT new org.thingsboard.server.dao.model.sql.DeviceInfoEntity(d, c.title, c.additionalInfo, p.name) " +
"FROM DeviceEntity d " +
"LEFT JOIN CustomerEntity c on c.id = d.customerId " +
"LEFT JOIN DeviceProfileEntity p on p.id = d.deviceProfileId " +
@Query ( "SELECT d FROM DeviceInfoEntity d " +
"WHERE d.tenantId = :tenantId " +
"AND d.customerId = :customerId " +
"AND LOWER(d.searchText) LIKE LOWER(CONCAT('%', :searchText, '%'))" )
@ -79,19 +72,6 @@ public interface DeviceRepository extends JpaRepository<DeviceEntity, UUID>, Exp
@Param ( "textSearch" ) String textSearch ,
Pageable pageable ) ;
@Query ( "SELECT new org.thingsboard.server.dao.model.sql.DeviceInfoEntity(d, c.title, c.additionalInfo, p.name) " +
"FROM DeviceEntity d " +
"LEFT JOIN CustomerEntity c on c.id = d.customerId " +
"LEFT JOIN DeviceProfileEntity p on p.id = d.deviceProfileId " +
"WHERE d.tenantId = :tenantId " +
"AND (LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(d.label) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(p.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(c.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')))" )
Page < DeviceInfoEntity > findDeviceInfosByTenantId ( @Param ( "tenantId" ) UUID tenantId ,
@Param ( "textSearch" ) String textSearch ,
Pageable pageable ) ;
@Query ( "SELECT d FROM DeviceEntity d WHERE d.tenantId = :tenantId " +
"AND d.type = :type " +
"AND LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%'))" )
@ -105,9 +85,9 @@ public interface DeviceRepository extends JpaRepository<DeviceEntity, UUID>, Exp
"AND d.firmwareId = null " +
"AND LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%'))" )
Page < DeviceEntity > findByTenantIdAndTypeAndFirmwareIdIsNull ( @Param ( "tenantId" ) UUID tenantId ,
@Param ( "deviceProfileId" ) UUID deviceProfileId ,
@Param ( "textSearch" ) String textSearch ,
Pageable pageable ) ;
@Param ( "deviceProfileId" ) UUID deviceProfileId ,
@Param ( "textSearch" ) String textSearch ,
Pageable pageable ) ;
@Query ( "SELECT d FROM DeviceEntity d WHERE d.tenantId = :tenantId " +
"AND d.deviceProfileId = :deviceProfileId " +
@ -130,34 +110,6 @@ public interface DeviceRepository extends JpaRepository<DeviceEntity, UUID>, Exp
Long countByTenantIdAndDeviceProfileIdAndSoftwareIdIsNull ( @Param ( "tenantId" ) UUID tenantId ,
@Param ( "deviceProfileId" ) UUID deviceProfileId ) ;
@Query ( "SELECT new org.thingsboard.server.dao.model.sql.DeviceInfoEntity(d, c.title, c.additionalInfo, p.name) " +
"FROM DeviceEntity d " +
"LEFT JOIN CustomerEntity c on c.id = d.customerId " +
"LEFT JOIN DeviceProfileEntity p on p.id = d.deviceProfileId " +
"WHERE d.tenantId = :tenantId " +
"AND d.type = :type " +
"AND (LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(d.label) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(c.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')))" )
Page < DeviceInfoEntity > findDeviceInfosByTenantIdAndType ( @Param ( "tenantId" ) UUID tenantId ,
@Param ( "type" ) String type ,
@Param ( "textSearch" ) String textSearch ,
Pageable pageable ) ;
@Query ( "SELECT new org.thingsboard.server.dao.model.sql.DeviceInfoEntity(d, c.title, c.additionalInfo, p.name) " +
"FROM DeviceEntity d " +
"LEFT JOIN CustomerEntity c on c.id = d.customerId " +
"LEFT JOIN DeviceProfileEntity p on p.id = d.deviceProfileId " +
"WHERE d.tenantId = :tenantId " +
"AND d.deviceProfileId = :deviceProfileId " +
"AND (LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(d.label) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(c.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')))" )
Page < DeviceInfoEntity > findDeviceInfosByTenantIdAndDeviceProfileId ( @Param ( "tenantId" ) UUID tenantId ,
@Param ( "deviceProfileId" ) UUID deviceProfileId ,
@Param ( "textSearch" ) String textSearch ,
Pageable pageable ) ;
@Query ( "SELECT d FROM DeviceEntity d WHERE d.tenantId = :tenantId " +
"AND d.customerId = :customerId " +
"AND d.type = :type " +
@ -168,33 +120,25 @@ public interface DeviceRepository extends JpaRepository<DeviceEntity, UUID>, Exp
@Param ( "textSearch" ) String textSearch ,
Pageable pageable ) ;
@Query ( "SELECT new org.thingsboard.server.dao.model.sql.DeviceInfoEntity(d, c.title, c.additionalInfo, p.name) " +
"FROM DeviceEntity d " +
"LEFT JOIN CustomerEntity c on c.id = d.customerId " +
"LEFT JOIN DeviceProfileEntity p on p.id = d.deviceProfileId " +
@Query ( "SELECT d FROM DeviceInfoEntity d " +
"WHERE d.tenantId = :tenantId " +
"AND d.customerId = :customerId " +
"AND d.type = :type " +
"AND LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%'))" )
Page < DeviceInfoEntity > findDeviceInfosByTenantIdAndCustomerIdAndType ( @Param ( "tenantId" ) UUID tenantId ,
@Param ( "customerId" ) UUID customerId ,
@Param ( "type" ) String type ,
@Param ( "textSearch" ) String textSearch ,
Pageable pageable ) ;
@Query ( "SELECT new org.thingsboard.server.dao.model.sql.DeviceInfoEntity(d, c.title, c.additionalInfo, p.name) " +
"FROM DeviceEntity d " +
"LEFT JOIN CustomerEntity c on c.id = d.customerId " +
"LEFT JOIN DeviceProfileEntity p on p.id = d.deviceProfileId " +
"WHERE d.tenantId = :tenantId " +
"AND d.customerId = :customerId " +
"AND d.deviceProfileId = :deviceProfileId " +
"AND LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%'))" )
Page < DeviceInfoEntity > findDeviceInfosByTenantIdAndCustomerIdAndDeviceProfileId ( @Param ( "tenantId" ) UUID tenantId ,
@Param ( "customerId" ) UUID customerId ,
@Param ( "deviceProfileId" ) UUID deviceProfileId ,
@Param ( "textSearch" ) String textSearch ,
Pageable pageable ) ;
"AND (:customerId IS NULL OR d.customerId = uuid(:customerId)) " +
"AND ((:deviceType) IS NULL OR d.type = :deviceType) " +
"AND (:deviceProfileId IS NULL OR d.deviceProfileId = uuid(:deviceProfileId)) " +
"AND ((:filterByActive) IS FALSE OR (((:persistToTelemetry) IS TRUE AND d.tsActive = :deviceActive) OR ((:persistToTelemetry) IS FALSE AND d.attributeActive = :deviceActive))) " +
"AND (LOWER(d.searchText) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(d.label) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(d.type) LIKE LOWER(CONCAT('%', :textSearch, '%')) " +
"OR LOWER(d.customerTitle) LIKE LOWER(CONCAT('%', :textSearch, '%')))" )
Page < DeviceInfoEntity > findDeviceInfosByFilter ( @Param ( "tenantId" ) UUID tenantId ,
@Param ( "customerId" ) String customerId ,
@Param ( "deviceType" ) String type ,
@Param ( "deviceProfileId" ) String deviceProfileId ,
@Param ( "filterByActive" ) boolean filterByActive ,
@Param ( "persistToTelemetry" ) boolean persistToTelemetry ,
@Param ( "deviceActive" ) boolean active ,
@Param ( "textSearch" ) String textSearch ,
Pageable pageable ) ;
@Query ( "SELECT DISTINCT d.type FROM DeviceEntity d WHERE d.tenantId = :tenantId" )
List < String > findTenantDeviceTypes ( @Param ( "tenantId" ) UUID tenantId ) ;
@ -237,10 +181,10 @@ public interface DeviceRepository extends JpaRepository<DeviceEntity, UUID>, Exp
* < p >
* There is two way to count devices .
* OPTIMAL : count ( * )
* - returns _row_count_ and use index - only scan ( super fast ) .
* - returns _row_count_ and use index - only scan ( super fast ) .
* SLOW : count ( id )
* - returns _NON_NULL_id_count and performs table scan to verify isNull for each id in filtered rows .
* * /
* - returns _NON_NULL_id_count and performs table scan to verify isNull for each id in filtered rows .
* /
@Query ( "SELECT count(*) FROM DeviceEntity d WHERE d.tenantId = :tenantId" )
Long countByTenantId ( @Param ( "tenantId" ) UUID tenantId ) ;