1- contract TableFactory {
2- function openTable (string memory ) public view returns (Table); //open table
3- function createTable (string , string , string ) public returns (int256 ); //create table
4- }
1+ // SPDX-License-Identifier: Apache-2.0
2+ // 该接口文件定义了FISCO BCOS v3.1.0及以前版本的接口,使用时需要将该文件放在合约目录下
3+ // 若要使用FISCO BCOS v3.2.0及以后版本的接口,请使用TableV320.sol,旧合约仍然能在新节点中使用
4+ pragma solidity >= 0.6.10 < 0.8.20 ;
5+ pragma experimental ABIEncoderV2;
56
6- //select condition
7- contract Condition {
8- function EQ (string , int256 ) public ;
9- function EQ (string , string ) public ;
7+ // KeyOrder指定Key的排序规则,字典序和数字序,如果指定为数字序,key只能为数字
8+ // enum KeyOrder {Lexicographic, Numerical}
9+ struct TableInfo {
10+ string keyColumn;
11+ string [] valueColumns;
12+ }
1013
11- function NE (string , int256 ) public ;
12- function NE (string , string ) public ;
14+ // 记录,用于select和insert
15+ struct Entry {
16+ string key;
17+ string [] fields; // 考虑2.0的Entry接口,临时Precompiled的问题,考虑加工具类接口
18+ }
1319
14- function GT (string , int256 ) public ;
15- function GE (string , int256 ) public ;
20+ // 更新字段,用于update
21+ struct UpdateField {
22+ string columnName;
23+ // 考虑工具类
24+ string value;
25+ }
1626
17- function LT (string , int256 ) public ;
18- function LE (string , int256 ) public ;
27+ // 筛选条件,大于、大于等于、小于、小于等于
28+ enum ConditionOP {GT, GE, LT, LE}
29+ struct Condition {
30+ ConditionOP op;
31+ // string field;
32+ string value;
33+ }
1934
20- function limit (int256 ) public ;
21- function limit (int256 , int256 ) public ;
22- }
35+ // 数量限制
36+ struct Limit {
37+ uint32 offset;
38+ // count limit max is 500
39+ uint32 count;
40+ }
2341
24- //one record
25- contract Entry {
26- function getInt (string ) public view returns (int256 );
27- function getUInt (string ) public view returns (int256 );
28- function getAddress (string ) public view returns (address );
29- function getBytes64 (string ) public view returns (bytes1 [64 ]);
30- function getBytes32 (string ) public view returns (bytes32 );
31- function getString (string ) public view returns (string );
32-
33- function set (string , int256 ) public ;
34- function set (string , uint256 ) public ;
35- function set (string , string ) public ;
36- function set (string , address ) public ;
37- }
42+ // 表管理合约,是静态Precompiled,有固定的合约地址
43+ abstract contract TableManager {
44+ // 创建表,传入TableInfo
45+ function createTable (string memory path , TableInfo memory tableInfo ) public virtual returns (int32 );
3846
39- //record sets
40- contract Entries {
41- function get (int256 ) public view returns (Entry);
42- function size () public view returns (int256 );
43- }
47+ // 创建KV表,传入key和value字段名
48+ function createKVTable (string memory tableName , string memory keyField , string memory valueField ) public virtual returns (int32 );
4449
45- //Table main contract
46- contract Table {
47- function select (string , Condition) public view returns (Entries);
48- function insert (string , Entry) public returns (int256 );
49- function update (string , Entry, Condition) public returns (int256 );
50- function remove (string , Condition) public returns (int256 );
50+ // 只提供给Solidity合约调用时使用
51+ function openTable (string memory path ) public view virtual returns (address );
5152
52- function newEntry () public view returns (Entry);
53- function newCondition () public view returns (Condition);
54- }
53+ // 变更表字段
54+ // 只能新增字段,不能删除字段,新增的字段默认值为空,不能与原有字段重复
55+ function appendColumns ( string memory path , string [] memory newColumns ) public virtual returns ( int32 );
5556
56- contract KVTableFactory {
57- function openTable (string ) public view returns (KVTable);
58- function createTable (string , string , string ) public returns (int256 );
57+ // 获取表信息
58+ function desc (string memory tableName ) public view virtual returns (TableInfo memory );
5959}
6060
61- //KVTable per permiary key has only one Entry
62- contract KVTable {
63- function get (string ) public view returns (bool , Entry);
64- function set (string , Entry) public returns (int256 );
65- function newEntry () public view returns (Entry);
61+ // 表合约,是动态Precompiled,TableManager创建时指定地址
62+ abstract contract Table {
63+ // 按key查询entry
64+ function select (string memory key ) public virtual view returns (Entry memory );
65+
66+ // 按条件批量查询entry,condition为空则查询所有记录
67+ function select (Condition[] memory conditions , Limit memory limit ) public virtual view returns (Entry[] memory );
68+
69+ // 按照条件查询count数据
70+ function count (Condition[] memory conditions ) public virtual view returns (uint32 );
71+
72+ // 插入数据
73+ function insert (Entry memory entry ) public virtual returns (int32 );
74+
75+ // 按key更新entry
76+ function update (string memory key , UpdateField[] memory updateFields ) public virtual returns (int32 );
77+
78+ // 按条件批量更新entry,condition为空则更新所有记录
79+ function update (Condition[] memory conditions , Limit memory limit , UpdateField[] memory updateFields ) public virtual returns (int32 );
80+
81+ // 按key删除entry
82+ function remove (string memory key ) public virtual returns (int32 );
83+ // 按条件批量删除entry,condition为空则删除所有记录
84+ function remove (Condition[] memory conditions , Limit memory limit ) public virtual returns (int32 );
6685}
86+
87+ abstract contract KVTable {
88+ function get (string memory key ) public view virtual returns (bool , string memory );
89+
90+ function set (string memory key , string memory value ) public virtual returns (int32 );
91+ }
0 commit comments