Search in sources :

Example 1 with PrecompileMessageException

use of org.fisco.bcos.web3j.precompile.exception.PrecompileMessageException in project web3sdk by FISCO-BCOS.

the class CRUDService method remove.

public int remove(Table table, Condition condition) throws Exception {
    if (table.getKey().length() > PrecompiledCommon.TABLE_KEY_MAX_LENGTH) {
        throw new PrecompileMessageException("The value of the table key exceeds the maximum limit(" + PrecompiledCommon.TABLE_KEY_MAX_LENGTH + ").");
    }
    String conditionStr = ObjectMapperFactory.getObjectMapper().writeValueAsString(condition.getConditions());
    TransactionReceipt receipt = crud.remove(table.getTableName(), table.getKey(), conditionStr, table.getOptional()).send();
    return PrecompiledCommon.handleTransactionReceiptForCRUD(receipt);
}
Also used : TransactionReceipt(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt) PrecompileMessageException(org.fisco.bcos.web3j.precompile.exception.PrecompileMessageException)

Example 2 with PrecompileMessageException

use of org.fisco.bcos.web3j.precompile.exception.PrecompileMessageException in project web3sdk by FISCO-BCOS.

the class CRUDService method insert.

public int insert(Table table, Entry entry) throws Exception {
    if (table.getKey().length() > PrecompiledCommon.TABLE_KEY_MAX_LENGTH) {
        throw new PrecompileMessageException("The value of the table key exceeds the maximum limit(" + PrecompiledCommon.TABLE_KEY_MAX_LENGTH + ").");
    }
    String entryJsonStr = ObjectMapperFactory.getObjectMapper().writeValueAsString(entry.getFields());
    TransactionReceipt receipt = crud.insert(table.getTableName(), table.getKey(), entryJsonStr, table.getOptional()).send();
    return PrecompiledCommon.handleTransactionReceiptForCRUD(receipt);
}
Also used : TransactionReceipt(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt) PrecompileMessageException(org.fisco.bcos.web3j.precompile.exception.PrecompileMessageException)

Example 3 with PrecompileMessageException

use of org.fisco.bcos.web3j.precompile.exception.PrecompileMessageException in project web3sdk by FISCO-BCOS.

the class CRUDService method select.

@SuppressWarnings("unchecked")
public List<Map<String, String>> select(Table table, Condition condition) throws Exception {
    if (table.getKey().length() > PrecompiledCommon.TABLE_KEY_MAX_LENGTH) {
        throw new PrecompileMessageException("The value of the table key exceeds the maximum limit(" + PrecompiledCommon.TABLE_KEY_MAX_LENGTH + ").");
    }
    ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
    String conditionJsonStr = objectMapper.writeValueAsString(condition.getConditions());
    String resultStr = crud.select(table.getTableName(), table.getKey(), conditionJsonStr, table.getOptional()).send();
    List<Map<String, String>> result = (List<Map<String, String>>) objectMapper.readValue(resultStr, objectMapper.getTypeFactory().constructCollectionType(List.class, Map.class));
    return result;
}
Also used : PrecompileMessageException(org.fisco.bcos.web3j.precompile.exception.PrecompileMessageException) List(java.util.List) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with PrecompileMessageException

use of org.fisco.bcos.web3j.precompile.exception.PrecompileMessageException in project web3sdk by FISCO-BCOS.

the class CRUDService method update.

public int update(Table table, Entry entry, Condition condition) throws Exception {
    if (table.getKey().length() > PrecompiledCommon.TABLE_KEY_MAX_LENGTH) {
        throw new PrecompileMessageException("The value of the table key exceeds the maximum limit(" + PrecompiledCommon.TABLE_KEY_MAX_LENGTH + ").");
    }
    String entryJsonStr = ObjectMapperFactory.getObjectMapper().writeValueAsString(entry.getFields());
    String conditionStr = ObjectMapperFactory.getObjectMapper().writeValueAsString(condition.getConditions());
    TransactionReceipt receipt = crud.update(table.getTableName(), table.getKey(), entryJsonStr, conditionStr, table.getOptional()).send();
    return PrecompiledCommon.handleTransactionReceiptForCRUD(receipt);
}
Also used : TransactionReceipt(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt) PrecompileMessageException(org.fisco.bcos.web3j.precompile.exception.PrecompileMessageException)

Example 5 with PrecompileMessageException

use of org.fisco.bcos.web3j.precompile.exception.PrecompileMessageException in project web3sdk by FISCO-BCOS.

the class CRUDService method desc.

public Table desc(String tableName) throws Exception {
    Table table = new Table();
    table.setTableName(PrecompiledCommon.SYS_TABLE);
    /**
     * Compatible with node version 2.2.0
     */
    try {
        String supportedVersion = crud.getTransactionManager().getNodeVersion().getSupportedVersion();
        EnumNodeVersion.Version version = EnumNodeVersion.getClassVersion(supportedVersion);
        logger.debug("desc, table: {}, node version: {}", tableName, version);
        // less than 2.2.0
        if ((version.getMajor() == 2) && (version.getMinor() < 2)) {
            table.setKey(PrecompiledCommon.USER_TABLE_PREFIX + tableName);
        } else {
            table.setKey(PrecompiledCommon.USER_TABLE_PREFIX_2_2_0_VERSION + tableName);
        }
    } catch (Exception e) {
        throw new PrecompileMessageException(" Cannot query node version, maybe disconnect to all nodes.");
    }
    Condition condition = table.getCondition();
    List<Map<String, String>> userTable = select(table, condition);
    Table tableInfo = new Table();
    if ((userTable != null) && (userTable.size() != 0) && (null != userTable.get(0)) && !userTable.get(0).isEmpty()) {
        tableInfo.setTableName(tableName);
        tableInfo.setKey(userTable.get(0).get("key_field"));
        tableInfo.setValueFields(userTable.get(0).get("value_field"));
    } else {
        throw new PrecompileMessageException("The table '" + tableName + "' does not exist.");
    }
    return tableInfo;
}
Also used : PrecompileMessageException(org.fisco.bcos.web3j.precompile.exception.PrecompileMessageException) Map(java.util.Map) EnumNodeVersion(org.fisco.bcos.fisco.EnumNodeVersion) PrecompileMessageException(org.fisco.bcos.web3j.precompile.exception.PrecompileMessageException)

Aggregations

PrecompileMessageException (org.fisco.bcos.web3j.precompile.exception.PrecompileMessageException)5 TransactionReceipt (org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt)3 Map (java.util.Map)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 List (java.util.List)1 EnumNodeVersion (org.fisco.bcos.fisco.EnumNodeVersion)1