98 lines
3.0 KiB
Solidity
98 lines
3.0 KiB
Solidity
pragma solidity>=0.4.24 <0.6.11;
|
|
pragma experimental ABIEncoderV2;
|
|
|
|
import "./Table.sol";
|
|
|
|
contract TableTest {
|
|
event CreateResult(int256 count);
|
|
event InsertResult(int256 count);
|
|
event UpdateResult(int256 count);
|
|
event RemoveResult(int256 count);
|
|
|
|
TableFactory tableFactory;
|
|
string constant TABLE_NAME = "t_test";
|
|
constructor() public {
|
|
tableFactory = TableFactory(0x1001); //The fixed address is 0x1001 for TableFactory
|
|
// the parameters of createTable are tableName,keyField,"vlaueFiled1,vlaueFiled2,vlaueFiled3,..."
|
|
tableFactory.createTable(TABLE_NAME, "name", "item_id,item_name");
|
|
}
|
|
|
|
//select records
|
|
function select(string memory name)
|
|
public
|
|
view
|
|
returns (string[] memory, int256[] memory, string[] memory)
|
|
{
|
|
Table table = tableFactory.openTable(TABLE_NAME);
|
|
|
|
Condition condition = table.newCondition();
|
|
|
|
Entries entries = table.select(name, condition);
|
|
string[] memory user_name_bytes_list = new string[](
|
|
uint256(entries.size())
|
|
);
|
|
int256[] memory item_id_list = new int256[](uint256(entries.size()));
|
|
string[] memory item_name_bytes_list = new string[](
|
|
uint256(entries.size())
|
|
);
|
|
|
|
for (int256 i = 0; i < entries.size(); ++i) {
|
|
Entry entry = entries.get(i);
|
|
|
|
user_name_bytes_list[uint256(i)] = entry.getString("name");
|
|
item_id_list[uint256(i)] = entry.getInt("item_id");
|
|
item_name_bytes_list[uint256(i)] = entry.getString("item_name");
|
|
}
|
|
|
|
return (user_name_bytes_list, item_id_list, item_name_bytes_list);
|
|
}
|
|
//insert records
|
|
function insert(string memory name, int256 item_id, string memory item_name)
|
|
public
|
|
returns (int256)
|
|
{
|
|
Table table = tableFactory.openTable(TABLE_NAME);
|
|
|
|
Entry entry = table.newEntry();
|
|
entry.set("name", name);
|
|
entry.set("item_id", item_id);
|
|
entry.set("item_name", item_name);
|
|
|
|
int256 count = table.insert(name, entry);
|
|
emit InsertResult(count);
|
|
|
|
return count;
|
|
}
|
|
//update records
|
|
function update(string memory name, int256 item_id, string memory item_name)
|
|
public
|
|
returns (int256)
|
|
{
|
|
Table table = tableFactory.openTable(TABLE_NAME);
|
|
|
|
Entry entry = table.newEntry();
|
|
entry.set("item_name", item_name);
|
|
|
|
Condition condition = table.newCondition();
|
|
condition.EQ("name", name);
|
|
condition.EQ("item_id", item_id);
|
|
|
|
int256 count = table.update(name, entry, condition);
|
|
emit UpdateResult(count);
|
|
|
|
return count;
|
|
}
|
|
//remove records
|
|
function remove(string memory name, int256 item_id) public returns (int256) {
|
|
Table table = tableFactory.openTable(TABLE_NAME);
|
|
|
|
Condition condition = table.newCondition();
|
|
condition.EQ("name", name);
|
|
condition.EQ("item_id", item_id);
|
|
|
|
int256 count = table.remove(name, condition);
|
|
emit RemoveResult(count);
|
|
|
|
return count;
|
|
}
|
|
} |