1、抽象Bean对象接口
pragma solidity ^0.5.0;
import "./Table.sol";
pragma experimental ABIEncoderV2;
/**
@title 获取实体对象的字段与值
*/
interface Bean {
function getFields() external pure returns (string[] memory);
function getValues(Entry entry) external view returns (string[] memory);
}
2、编写将Bean装换为Json的library
pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;
import "./Table.sol";
import "./Bean.sol";
/**
@title 将Bean格式化为json
*/
library BeanToJson {
function getJsonString(Bean bean, Entries entries) internal view returns (int, string memory) {
string memory detail;
if (0 == entries.size()) {
return (- 1, detail);
}
else {
// 获取Bean的列名
string[] memory fields = bean.getFields();
// [{"index":"",{"key1":"","key2":""}}]
detail = "[";
// 获取Bean的值
for (uint i = 0; i < uint(entries.size()); i++) {
string[] memory values = bean.getValues(entries.get(int(i)));
for (uint j = 0; j < values.length; j++) {
if (j == 0) {
detail = strConcat4(detail, "{\"index\":\"", values[0], "\",{");
}
detail = strConcat6(detail, "\"", fields[j], "\":\"", values[j], "\"");
if (j == values.length - 1) {
detail = strConcat2(detail, "}}");
} else {
detail = strConcat2(detail, ",");
}
}
if (i != uint(entries.size()) - 1) {
detail = strConcat2(detail, ",");
}
}
detail = strConcat2(detail, "]");
return (0, detail);
}
}
function strConcat6(
string memory str1,
string memory str2,
string memory str3,
string memory str4,
string memory str5,
string memory str6
) public pure returns (string memory) {
string[] memory strings = new string[](6);
strings[0] = str1;
strings[1] = str2;
strings[2] = str3;
strings[3] = str4;
strings[4] = str5;
strings[5] = str6;
return strConcat(strings);
}
function strConcat5(
string memory str1,
string memory str2,
string memory str3,
string memory str4,
string memory str5
) public pure returns (string memory) {
string[] memory strings = new string[](5);
strings[0] = str1;
strings[1] = str2;
strings[2] = str3;
strings[3] = str4;
strings[4] = str5;
return strConcat(strings);
}
function strConcat4(
string memory str1,
string memory str2,
string memory str3,
string memory str4
) public pure returns (string memory) {
string[] memory strings = new string[](4);
strings[0] = str1;
strings[1] = str2;
strings[2] = str3;
strings[3] = str4;
return strConcat(strings);
}
function strConcat3(
string memory str1,
string memory str2,
string memory str3
) public pure returns (string memory) {
string[] memory strings = new string[](3);
strings[0] = str1;
strings[1] = str2;
strings[2] = str3;
return strConcat(strings);
}
function strConcat2(string memory str1, string memory str2) public pure returns (string memory) {
string[] memory strings = new string[](2);
strings[0] = str1;
strings[1] = str2;
return strConcat(strings);
}
function strConcat(string[] memory strings) public pure returns (string memory) {
// 计算字节长度
uint bLength = 0;
for (uint i = 0; i < strings.length; i++) {
bLength += bytes(strings[i]).length;
}
// 实例化字符串
string memory result = new string(bLength);
bytes memory bResult = bytes(result);
// 填充字符串
uint currLength = 0;
for (uint i = 0; i < strings.length; i++) {
// 将当前字符串转换为字节数组
bytes memory bs = bytes(strings[i]);
for (uint j = 0; j < bs.length; j++) {
bResult[currLength] = bs[j];
currLength++;
}
}
return string(bResult);
}
}
3、编写Bean实现合约
pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;
import "./Table.sol";
import "./Bean.sol";
import "./BeanToJson.sol";
/**
@title 企业表
对外提供接口:
1、查询所有数据
2、通过群组、企业ID获取单条记录
3、新增记录
*/
contract Company is Bean {
event AddEvent(int256 ret, string groupId, string companyName);
constructor() public {
// 构造函数中创建企业表
createTable();
}
/*
描述:根据群组查询所有公司记录
参数:
groupId:群组ID
返回值:
参数一:成功返回0,群组不存在返回-1
参数二:第一个参数为0时有效,key->id,value->对象json 的数组
*/
function select(string memory groupId) public view returns (int, string memory) {
// 打开表
Table table = openTable();
// 查询
Entries entries = table.select(groupId, table.newCondition());
// 将查询结果解析为json字符串
return BeanToJson.getJsonString(this, entries);
}
/*
描述:根据群组查询所有公司记录
参数:
groupId:群组ID
id:企业ID
返回值:
参数一:成功返回0,群组不存在返回-1
参数二:第一个参数为0时有效,key->id,value->对象json 的数组
*/
function select(string memory groupId, string memory id) public returns (int, string memory) {
// 打开表
Table table = openTable();
// 构建查询条件
Condition condition = table.newCondition();
condition.EQ("id", id);
// 查询
Entries entries = table.select(groupId, condition);
// 将查询结果解析为json字符串
return BeanToJson.getJsonString(this, entries);
}
/*
描述:新增记录
参数:
groupId:群组ID(主键)
id:企业ID
name:企业名称
busiLicense:营业执照
legalPerson:法人
contacts:联系人
contactsPhone:联系人号码
contactsAddress:联系人地址
other:其它信息
返回值:
成功返回0,失败返回-1(其它错误)
*/
function insert(
string memory groupId,
string memory id,
string memory name,
string memory busiLicense,
string memory legalPerson,
string memory contacts,
string memory contactsPhone,
string memory contactsAddress,
string memory other
) public returns (int) {
// 打开表
Table table = openTable();
// 创建表记录
Entry entry = table.newEntry();
entry.set("group_id", groupId);
entry.set("id", id);
entry.set("name", name);
entry.set("busi_license", busiLicense);
entry.set("legal_person", legalPerson);
entry.set("contacts", contacts);
entry.set("contacts_phone", contactsPhone);
entry.set("contacts_address", contactsAddress);
entry.set("other", other);
// 新增表记录
int retCode;
int count = table.insert(groupId, entry);
if (count == 1) {
retCode = 1;
} else {
retCode = 0;
}
// 记录新增结果
emit AddEvent(retCode, id, name);
// 返回结果
return retCode;
}
function getFields() external pure returns (string[] memory) {
string[] memory fields = new string[](8);
fields[0] = "id";
fields[1] = "name";
fields[2] = "busiLicense";
fields[3] = "legalPerson";
fields[4] = "contacts";
fields[5] = "contactsPhone";
fields[6] = "contactsAddress";
fields[7] = "other";
return fields;
}
function getValues(Entry entry) external view returns (string[] memory) {
string[] memory fields = new string[](8);
fields[0] = entry.getString("id");
fields[1] = entry.getString("name");
fields[2] = entry.getString("busi_license");
fields[3] = entry.getString("legal_person");
fields[4] = entry.getString("contacts");
fields[5] = entry.getString("contacts_phone");
fields[6] = entry.getString("contacts_address");
fields[7] = entry.getString("other");
return fields;
}
function createTable() private {
TableFactory tf = TableFactory(0x1001);
tf.createTable("company", "group_id", "id,name,busi_license,legal_person,contacts,contacts_phone,contacts_address,other");
}
function openTable() private view returns (Table) {
TableFactory tf = TableFactory(0x1001);
return tf.openTable("company");
}
}
4、在fisco-bcos提供的console上调用合约新增与查询数据
image.png









网友评论