committed by
Andrew Shvayka
13 changed files with 353 additions and 104 deletions
@ -0,0 +1,74 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2019 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.dao.sqlts; |
||||
|
|
||||
|
import org.springframework.stereotype.Repository; |
||||
|
|
||||
|
import javax.persistence.EntityManager; |
||||
|
import javax.persistence.PersistenceContext; |
||||
|
|
||||
|
@Repository |
||||
|
public abstract class AbstractInsertRepository { |
||||
|
|
||||
|
protected static final String BOOL_V = "bool_v"; |
||||
|
protected static final String STR_V = "str_v"; |
||||
|
protected static final String LONG_V = "long_v"; |
||||
|
protected static final String DBL_V = "dbl_v"; |
||||
|
|
||||
|
protected static final String TS_KV_LATEST_TABLE = "ts_kv_latest"; |
||||
|
protected static final String TS_KV_TABLE = "ts_kv"; |
||||
|
|
||||
|
protected static final String HSQL_ON_BOOL_VALUE_UPDATE_SET_NULLS = getHsqlNullValues(TS_KV_TABLE, BOOL_V); |
||||
|
protected static final String HSQL_ON_STR_VALUE_UPDATE_SET_NULLS = getHsqlNullValues(TS_KV_TABLE, STR_V); |
||||
|
protected static final String HSQL_ON_LONG_VALUE_UPDATE_SET_NULLS = getHsqlNullValues(TS_KV_TABLE, LONG_V); |
||||
|
protected static final String HSQL_ON_DBL_VALUE_UPDATE_SET_NULLS = getHsqlNullValues(TS_KV_TABLE, DBL_V); |
||||
|
|
||||
|
protected static final String HSQL_LATEST_ON_BOOL_VALUE_UPDATE_SET_NULLS = getHsqlNullValues(TS_KV_LATEST_TABLE, BOOL_V); |
||||
|
protected static final String HSQL_LATEST_ON_STR_VALUE_UPDATE_SET_NULLS = getHsqlNullValues(TS_KV_LATEST_TABLE, STR_V); |
||||
|
protected static final String HSQL_LATEST_ON_LONG_VALUE_UPDATE_SET_NULLS = getHsqlNullValues(TS_KV_LATEST_TABLE, LONG_V); |
||||
|
protected static final String HSQL_LATEST_ON_DBL_VALUE_UPDATE_SET_NULLS = getHsqlNullValues(TS_KV_LATEST_TABLE, DBL_V); |
||||
|
|
||||
|
protected static final String PSQL_ON_BOOL_VALUE_UPDATE_SET_NULLS = "str_v = null, long_v = null, dbl_v = null"; |
||||
|
protected static final String PSQL_ON_STR_VALUE_UPDATE_SET_NULLS = "bool_v = null, long_v = null, dbl_v = null"; |
||||
|
protected static final String PSQL_ON_LONG_VALUE_UPDATE_SET_NULLS = "str_v = null, bool_v = null, dbl_v = null"; |
||||
|
protected static final String PSQL_ON_DBL_VALUE_UPDATE_SET_NULLS = "str_v = null, long_v = null, bool_v = null"; |
||||
|
|
||||
|
@PersistenceContext |
||||
|
protected EntityManager entityManager; |
||||
|
|
||||
|
protected static String getInsertOrUpdateStringHsql(String tableName, String constraint, String value, String nullValues) { |
||||
|
return "MERGE INTO " + tableName + " USING(VALUES :entity_type, :entity_id, :key, :ts, :" + value + ") A (entity_type, entity_id, key, ts, " + value + ") ON " + constraint + " WHEN MATCHED THEN UPDATE SET " + tableName + "." + value + " = A." + value + ", " + tableName + ".ts = A.ts," + nullValues + "WHEN NOT MATCHED THEN INSERT (entity_type, entity_id, key, ts, " + value + ") VALUES (A.entity_type, A.entity_id, A.key, A.ts, A." + value + ")"; |
||||
|
} |
||||
|
|
||||
|
protected static String getInsertOrUpdateStringPsql(String tableName, String constraint, String value, String nullValues) { |
||||
|
return "INSERT INTO " + tableName + " (entity_type, entity_id, key, ts, " + value + ") VALUES (:entity_type, :entity_id, :key, :ts, :" + value + ") ON CONFLICT " + constraint + " DO UPDATE SET " + value + " = :" + value + ", ts = :ts," + nullValues; |
||||
|
} |
||||
|
|
||||
|
private static String getHsqlNullValues(String tableName, String notNullValue) { |
||||
|
switch (notNullValue) { |
||||
|
case BOOL_V: |
||||
|
return " " + tableName + ".str_v = null, " + tableName + ".long_v = null, " + tableName + ".dbl_v = null "; |
||||
|
case STR_V: |
||||
|
return " " + tableName + ".bool_v = null, " + tableName + ".long_v = null, " + tableName + ".dbl_v = null "; |
||||
|
case LONG_V: |
||||
|
return " " + tableName + ".str_v = null, " + tableName + ".bool_v = null, " + tableName + ".dbl_v = null "; |
||||
|
case DBL_V: |
||||
|
return " " + tableName + ".str_v = null, " + tableName + ".long_v = null, " + tableName + ".bool_v = null "; |
||||
|
default: |
||||
|
throw new RuntimeException("Unsupported insert value: [" + notNullValue + "]"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,54 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2019 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.dao.sqlts; |
||||
|
|
||||
|
import org.springframework.data.jpa.repository.Modifying; |
||||
|
import org.springframework.stereotype.Repository; |
||||
|
import org.thingsboard.server.dao.model.sqlts.ts.TsKvLatestEntity; |
||||
|
|
||||
|
@Repository |
||||
|
public abstract class AbstractLatestInsertRepository extends AbstractInsertRepository { |
||||
|
|
||||
|
public abstract void saveOrUpdate(TsKvLatestEntity entity); |
||||
|
|
||||
|
protected void processSaveOrUpdate(TsKvLatestEntity entity, String requestBoolValue, String requestStrValue, String requestLongValue, String requestDblValue) { |
||||
|
if (entity.getBooleanValue() != null) { |
||||
|
saveOrUpdateBoolean(entity, requestBoolValue); |
||||
|
} |
||||
|
if (entity.getStrValue() != null) { |
||||
|
saveOrUpdateString(entity, requestStrValue); |
||||
|
} |
||||
|
if (entity.getLongValue() != null) { |
||||
|
saveOrUpdateLong(entity, requestLongValue); |
||||
|
} |
||||
|
if (entity.getDoubleValue() != null) { |
||||
|
saveOrUpdateDouble(entity, requestDblValue); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Modifying |
||||
|
protected abstract void saveOrUpdateBoolean(TsKvLatestEntity entity, String query); |
||||
|
|
||||
|
@Modifying |
||||
|
protected abstract void saveOrUpdateString(TsKvLatestEntity entity, String query); |
||||
|
|
||||
|
@Modifying |
||||
|
protected abstract void saveOrUpdateLong(TsKvLatestEntity entity, String query); |
||||
|
|
||||
|
@Modifying |
||||
|
protected abstract void saveOrUpdateDouble(TsKvLatestEntity entity, String query); |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,86 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2019 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.dao.sqlts.ts; |
||||
|
|
||||
|
import org.springframework.stereotype.Repository; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
import org.thingsboard.server.dao.model.sqlts.ts.TsKvLatestEntity; |
||||
|
import org.thingsboard.server.dao.sqlts.AbstractLatestInsertRepository; |
||||
|
import org.thingsboard.server.dao.util.HsqlDao; |
||||
|
import org.thingsboard.server.dao.util.SqlTsDao; |
||||
|
|
||||
|
@SqlTsDao |
||||
|
@HsqlDao |
||||
|
@Repository |
||||
|
@Transactional |
||||
|
public class HsqlLatestInsertRepository extends AbstractLatestInsertRepository { |
||||
|
|
||||
|
private static final String TS_KV_LATEST_CONSTRAINT = "(ts_kv_latest.entity_type=A.entity_type AND ts_kv_latest.entity_id=A.entity_id AND ts_kv_latest.key=A.key)"; |
||||
|
|
||||
|
private static final String INSERT_OR_UPDATE_BOOL_STATEMENT = getInsertOrUpdateStringHsql(TS_KV_LATEST_TABLE, TS_KV_LATEST_CONSTRAINT, BOOL_V, HSQL_LATEST_ON_BOOL_VALUE_UPDATE_SET_NULLS); |
||||
|
private static final String INSERT_OR_UPDATE_STR_STATEMENT = getInsertOrUpdateStringHsql(TS_KV_LATEST_TABLE, TS_KV_LATEST_CONSTRAINT, STR_V, HSQL_LATEST_ON_STR_VALUE_UPDATE_SET_NULLS); |
||||
|
private static final String INSERT_OR_UPDATE_LONG_STATEMENT = getInsertOrUpdateStringHsql(TS_KV_LATEST_TABLE, TS_KV_LATEST_CONSTRAINT, LONG_V, HSQL_LATEST_ON_LONG_VALUE_UPDATE_SET_NULLS); |
||||
|
private static final String INSERT_OR_UPDATE_DBL_STATEMENT = getInsertOrUpdateStringHsql(TS_KV_LATEST_TABLE, TS_KV_LATEST_CONSTRAINT, DBL_V, HSQL_LATEST_ON_DBL_VALUE_UPDATE_SET_NULLS); |
||||
|
|
||||
|
@Override |
||||
|
public void saveOrUpdate(TsKvLatestEntity entity) { |
||||
|
processSaveOrUpdate(entity, INSERT_OR_UPDATE_BOOL_STATEMENT, INSERT_OR_UPDATE_STR_STATEMENT, INSERT_OR_UPDATE_LONG_STATEMENT, INSERT_OR_UPDATE_DBL_STATEMENT); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void saveOrUpdateBoolean(TsKvLatestEntity entity, String query) { |
||||
|
entityManager.createNativeQuery(query) |
||||
|
.setParameter("entity_type", entity.getEntityType().name()) |
||||
|
.setParameter("entity_id", entity.getEntityId()) |
||||
|
.setParameter("key", entity.getKey()) |
||||
|
.setParameter("ts", entity.getTs()) |
||||
|
.setParameter("bool_v", entity.getBooleanValue()) |
||||
|
.executeUpdate(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void saveOrUpdateString(TsKvLatestEntity entity, String query) { |
||||
|
entityManager.createNativeQuery(query) |
||||
|
.setParameter("entity_type", entity.getEntityType().name()) |
||||
|
.setParameter("entity_id", entity.getEntityId()) |
||||
|
.setParameter("key", entity.getKey()) |
||||
|
.setParameter("ts", entity.getTs()) |
||||
|
.setParameter("str_v", entity.getStrValue()) |
||||
|
.executeUpdate(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void saveOrUpdateLong(TsKvLatestEntity entity, String query) { |
||||
|
entityManager.createNativeQuery(query) |
||||
|
.setParameter("entity_type", entity.getEntityType().name()) |
||||
|
.setParameter("entity_id", entity.getEntityId()) |
||||
|
.setParameter("key", entity.getKey()) |
||||
|
.setParameter("ts", entity.getTs()) |
||||
|
.setParameter("long_v", entity.getLongValue()) |
||||
|
.executeUpdate(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void saveOrUpdateDouble(TsKvLatestEntity entity, String query) { |
||||
|
entityManager.createNativeQuery(query) |
||||
|
.setParameter("entity_type", entity.getEntityType().name()) |
||||
|
.setParameter("entity_id", entity.getEntityId()) |
||||
|
.setParameter("key", entity.getKey()) |
||||
|
.setParameter("ts", entity.getTs()) |
||||
|
.setParameter("dbl_v", entity.getDoubleValue()) |
||||
|
.executeUpdate(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,86 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2019 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.dao.sqlts.ts; |
||||
|
|
||||
|
import org.springframework.stereotype.Repository; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
import org.thingsboard.server.dao.model.sqlts.ts.TsKvLatestEntity; |
||||
|
import org.thingsboard.server.dao.sqlts.AbstractLatestInsertRepository; |
||||
|
import org.thingsboard.server.dao.util.PsqlDao; |
||||
|
import org.thingsboard.server.dao.util.SqlTsDao; |
||||
|
|
||||
|
@SqlTsDao |
||||
|
@PsqlDao |
||||
|
@Repository |
||||
|
@Transactional |
||||
|
public class PsqlLatestInsertRepository extends AbstractLatestInsertRepository { |
||||
|
|
||||
|
private static final String TS_KV_LATEST_CONSTRAINT = "(entity_type, entity_id, key)"; |
||||
|
|
||||
|
private static final String INSERT_OR_UPDATE_BOOL_STATEMENT = getInsertOrUpdateStringPsql(TS_KV_LATEST_TABLE, TS_KV_LATEST_CONSTRAINT, BOOL_V, PSQL_ON_BOOL_VALUE_UPDATE_SET_NULLS); |
||||
|
private static final String INSERT_OR_UPDATE_STR_STATEMENT = getInsertOrUpdateStringPsql(TS_KV_LATEST_TABLE, TS_KV_LATEST_CONSTRAINT, STR_V, PSQL_ON_STR_VALUE_UPDATE_SET_NULLS); |
||||
|
private static final String INSERT_OR_UPDATE_LONG_STATEMENT = getInsertOrUpdateStringPsql(TS_KV_LATEST_TABLE, TS_KV_LATEST_CONSTRAINT, LONG_V, PSQL_ON_LONG_VALUE_UPDATE_SET_NULLS); |
||||
|
private static final String INSERT_OR_UPDATE_DBL_STATEMENT = getInsertOrUpdateStringPsql(TS_KV_LATEST_TABLE, TS_KV_LATEST_CONSTRAINT, DBL_V, PSQL_ON_DBL_VALUE_UPDATE_SET_NULLS); |
||||
|
|
||||
|
@Override |
||||
|
public void saveOrUpdate(TsKvLatestEntity entity) { |
||||
|
processSaveOrUpdate(entity, INSERT_OR_UPDATE_BOOL_STATEMENT, INSERT_OR_UPDATE_STR_STATEMENT, INSERT_OR_UPDATE_LONG_STATEMENT, INSERT_OR_UPDATE_DBL_STATEMENT); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void saveOrUpdateBoolean(TsKvLatestEntity entity, String query) { |
||||
|
entityManager.createNativeQuery(query) |
||||
|
.setParameter("entity_type", entity.getEntityType().name()) |
||||
|
.setParameter("entity_id", entity.getEntityId()) |
||||
|
.setParameter("key", entity.getKey()) |
||||
|
.setParameter("ts", entity.getTs()) |
||||
|
.setParameter("bool_v", entity.getBooleanValue()) |
||||
|
.executeUpdate(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void saveOrUpdateString(TsKvLatestEntity entity, String query) { |
||||
|
entityManager.createNativeQuery(query) |
||||
|
.setParameter("entity_type", entity.getEntityType().name()) |
||||
|
.setParameter("entity_id", entity.getEntityId()) |
||||
|
.setParameter("key", entity.getKey()) |
||||
|
.setParameter("ts", entity.getTs()) |
||||
|
.setParameter("str_v", entity.getStrValue()) |
||||
|
.executeUpdate(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void saveOrUpdateLong(TsKvLatestEntity entity, String query) { |
||||
|
entityManager.createNativeQuery(query) |
||||
|
.setParameter("entity_type", entity.getEntityType().name()) |
||||
|
.setParameter("entity_id", entity.getEntityId()) |
||||
|
.setParameter("key", entity.getKey()) |
||||
|
.setParameter("ts", entity.getTs()) |
||||
|
.setParameter("long_v", entity.getLongValue()) |
||||
|
.executeUpdate(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void saveOrUpdateDouble(TsKvLatestEntity entity, String query) { |
||||
|
entityManager.createNativeQuery(query) |
||||
|
.setParameter("entity_type", entity.getEntityType().name()) |
||||
|
.setParameter("entity_id", entity.getEntityId()) |
||||
|
.setParameter("key", entity.getKey()) |
||||
|
.setParameter("ts", entity.getTs()) |
||||
|
.setParameter("dbl_v", entity.getDoubleValue()) |
||||
|
.executeUpdate(); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue