org.fisco.bcos.web3j.precompile.crud.Entry Java Examples

The following examples show how to use org.fisco.bcos.web3j.precompile.crud.Entry. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PrecompiledWithSignService.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
/**
 * CRUD: update table through webase-sign
 */
public int update(int groupId, String signUserId, Table table,
				  Entry entry, Condition condition) throws Exception {
	checkTableKeyLength(table);
	// trans
	String entryJsonStr =
			ObjectMapperFactory.getObjectMapper().writeValueAsString(entry.getFields());
	String conditionStr =
			ObjectMapperFactory.getObjectMapper().writeValueAsString(condition.getConditions());
	List<Object> funcParams = new ArrayList<>();
	funcParams.add(table.getTableName());
	funcParams.add(table.getKey());
	funcParams.add(entryJsonStr);
	funcParams.add(conditionStr);
	funcParams.add(table.getOptional());
	TransactionReceipt receipt = (TransactionReceipt) transService.transHandleWithSignForPrecompile(groupId,
			signUserId, PrecompiledTypes.CRUD, FUNC_UPDATE, funcParams);
	return PrecompiledCommon.handleTransactionReceiptForCRUD(receipt);
}
 
Example #2
Source File: CRUDParseUtils.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
public static void checkUserTableParam(Entry entry, Table descTable)
        throws FrontException {
    Map<String, String> fieldsMap = entry.getFields();
    Set<String> keys = fieldsMap.keySet();
    for (String key : keys) {
        if (key.equals(descTable.getKey())) {
            if (fieldsMap.get(key).length() > PrecompiledUtils.USER_TABLE_KEY_VALUE_MAX_LENGTH) {
                throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR,
                        "The table primary key value length is greater than "
                                + PrecompiledUtils.USER_TABLE_KEY_VALUE_MAX_LENGTH
                                + ".");
            }
        } else {
            if (fieldsMap.get(key).length() > PrecompiledUtils.USER_TABLE_FIELD_VALUE_MAX_LENGTH) {
                throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR,
                        "The table field '" + key + "' value length is greater than 16M - 1.");
            }
        }
    }
}
 
Example #3
Source File: PrecompiledWithSignService.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
/**
 * CRUD: insert table through webase-sign
 */
public int insert(int groupId, String signUserId, Table table, Entry entry) throws Exception {
	checkTableKeyLength(table);
	// trans
	String entryJsonStr =
			ObjectMapperFactory.getObjectMapper().writeValueAsString(entry.getFields());
	List<Object> funcParams = new ArrayList<>();
	funcParams.add(table.getTableName());
	funcParams.add(table.getKey());
	funcParams.add(entryJsonStr);
	funcParams.add(table.getOptional());
	TransactionReceipt receipt = (TransactionReceipt) transService.transHandleWithSignForPrecompile(groupId,
			signUserId, PrecompiledTypes.CRUD, FUNC_INSERT, funcParams);
	return PrecompiledCommon.handleTransactionReceiptForCRUD(receipt);
}
 
Example #4
Source File: CRUDParseUtils.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
public static void parseUpdate(String sql, Table table, Entry entry, Condition condition)
        throws JSQLParserException, FrontException {
    Statement statement = CCJSqlParserUtil.parse(sql);
    Update update = (Update) statement;

    // parse table name
    List<net.sf.jsqlparser.schema.Table> tables = update.getTables();
    String tableName = tables.get(0).getName();
    table.setTableName(tableName);

    // parse cloumns
    List<Column> columns = update.getColumns();
    List<Expression> expressions = update.getExpressions();
    int size = expressions.size();
    String[] values = new String[size];
    for (int i = 0; i < size; i++) {
        values[i] = expressions.get(i).toString();
    }
    for (int i = 0; i < columns.size(); i++) {
        entry.put(trimQuotes(columns.get(i).toString()), trimQuotes(values[i]));
    }

    // parse where clause
    Expression where = update.getWhere();
    if (where != null) {
        BinaryExpression expr2 = (BinaryExpression) (where);
        handleExpression(condition, expr2);
    }
    Limit limit = update.getLimit();
    parseLimit(condition, limit);
}
 
Example #5
Source File: CRUDAddress.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
public boolean addAddress(Long version, String address) throws BrokerException {
    log.info("associated groupId: {}", this.groupId);

    // check exist manually to avoid duplicate record
    Map<Long, String> topicControlAddresses = listAddress();
    if (topicControlAddresses.containsKey(version)) {
        log.info("already exist in CRUD, {} {}", version, address);
        return false;
    }

    try {
        this.table.setKey(ContractAddress);
        Entry record = this.table.getEntry();
        record.put(TableValue, address);
        record.put(TableVersion, String.valueOf(version));
        // notice: record's key can be duplicate in CRUD
        int result = this.crud.insert(this.table, record);
        if (result == 1) {
            log.info("add contract address into CRUD success");
            return true;
        }

        log.error("add contract address into CRUD failed, {}", result);
        return false;
    } catch (Exception e) {
        log.error("add contract address into CRUD failed", e);
        return false;
    }
}
 
Example #6
Source File: PrecompiledService.java    From WeBASE-Front with Apache License 2.0 4 votes vote down vote up
/**
 * insert 校验tableName等操作放在controller
 */
public int insert(int groupId, String signUserId, Table table, Entry entry) throws Exception {
    int res = precompiledWithSignService.insert(groupId, signUserId, table, entry);
    return res;
}
 
Example #7
Source File: PrecompiledService.java    From WeBASE-Front with Apache License 2.0 4 votes vote down vote up
/**
 * update
 */
public int update(int groupId, String signUserId, Table table, Entry entry, Condition condition)
        throws Exception {
    int res = precompiledWithSignService.update(groupId, signUserId, table, entry, condition);
    return res;
}
 
Example #8
Source File: CRUDParseUtils.java    From WeBASE-Front with Apache License 2.0 4 votes vote down vote up
public static boolean parseInsert(String sql, Table table, Entry entry)
        throws JSQLParserException, FrontException {
    Statement statement = CCJSqlParserUtil.parse(sql);
    Insert insert = (Insert) statement;

    if (insert.getSelect() != null) {
        throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR, "The insert select clause is not supported.");
    }
    // parse table name
    String tableName = insert.getTable().getName();
    table.setTableName(tableName);

    // parse columns
    List<Column> columns = insert.getColumns();
    ItemsList itemsList = insert.getItemsList();
    String items = itemsList.toString();
    String[] rawItem = items.substring(1, items.length() - 1).split(",");
    String[] itemArr = new String[rawItem.length];
    for (int i = 0; i < rawItem.length; i++) {
        itemArr[i] = rawItem[i].trim();
    }
    if (columns != null) {
        if (columns.size() != itemArr.length) {
            throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR, "Column count doesn't match value count.");
        }
        List<String> columnNames = new ArrayList<>();
        for (Column column : columns) {
            String columnName = trimQuotes(column.toString());
            if (columnNames.contains(columnName)) {
                throw new FrontException(PrecompiledUtils.CRUD_SQL_ERROR,
                        "Please provide the field '" + columnName + "' only once.");
            } else {
                columnNames.add(columnName);
            }
        }
        for (int i = 0; i < columnNames.size(); i++) {
            entry.put(columnNames.get(i), trimQuotes(itemArr[i]));
        }
        return false;
    } else {
        for (int i = 0; i < itemArr.length; i++) {
            entry.put(i + "", trimQuotes(itemArr[i]));
        }
        return true;
    }
}
 
Example #9
Source File: CRUDServiceTest.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
 public void curdTest() throws Exception {
   
 	String tableName = "t_test" + new Random().nextInt(100000);
 	String key = "name";
 	String valueFields  = "item_id, item_name";
 	Table table = new Table(tableName, key, valueFields);

 	// create table
   int resultCreate = crudService.createTable(table);
   assertEquals(resultCreate, 0);
 	
   // insert records
   int insertResult = 0;
   int num = 5;
   for(int i = 1; i <= num; i++)
   {
    Entry insertEntry = table.getEntry();
    insertEntry.put("item_id", "1");
   	insertEntry.put("item_name", "apple"+i);
   	table.setKey("fruit");
   	insertResult += crudService.insert(table, insertEntry);
   }
   assertEquals(insertResult, num);
	
   // select records
	Condition condition1 = table.getCondition();
	condition1.EQ("item_id", "1");
	condition1.Limit(1);
	
	List<Map<String, String>> resultSelect1 = crudService.select(table, condition1);
	assertEquals(resultSelect1.get(0).get("name"), "fruit");
	assertEquals(resultSelect1.get(0).get("item_id"), "1");
	assertEquals(resultSelect1.get(0).get("item_name"), "apple1");
 	
  // update records
	Entry updateEntry = table.getEntry();
  	updateEntry.put("item_id", "1");
  	updateEntry.put("item_name", "orange");
  	Condition updateCondition = table.getCondition();
  	updateCondition.EQ("item_id", "1");
  	int updateResult = crudService.update(table, updateEntry, updateCondition);
  	assertEquals(updateResult, num);
	
  // select records
	Condition condition2 = table.getCondition();
	condition2.EQ("item_id", "1");
	condition2.Limit(1);
	List<Map<String, String>> resultSelect2 = crudService.select(table, condition2);
	assertEquals(resultSelect2.get(0).get("name"), "fruit");
	assertEquals(resultSelect2.get(0).get("item_id"), "1");
	assertEquals(resultSelect2.get(0).get("item_name"), "orange");
	
  // remove records
	Condition removeCondition = table.getCondition();
	removeCondition.EQ("item_id", "1");
	int removeResult = crudService.remove(table, removeCondition);
	assertEquals(removeResult, num);
 }