1
- // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity >= 0.6.10 < 0.8.20 ;
3
- pragma experimental ABIEncoderV2;
4
-
5
- import "./Table.sol " ;
6
- import "./Cast.sol " ;
7
-
8
- contract TableTest {
9
- event CreateResult (int256 count );
10
- event InsertResult (int256 count );
11
- event UpdateResult (int256 count );
12
- event RemoveResult (int256 count );
13
-
14
- Cast constant cast = Cast (address (0x100f ));
15
- TableManager constant tm = TableManager (address (0x1002 ));
16
- Table table;
17
- string constant TABLE_NAME = "t_testV320 " ;
18
- constructor () public {
19
- // create table
20
- string [] memory columnNames = new string [](3 );
21
- columnNames[0 ] = "name " ;
22
- columnNames[1 ] = "age " ;
23
- columnNames[2 ] = "status " ;
24
- TableInfo memory tf = TableInfo (KeyOrder.Numerical ,"id " , columnNames);
25
-
26
- tm.createTable (TABLE_NAME, tf);
27
- address t_address = tm.openTable (TABLE_NAME);
28
- require (t_address!= address (0x0 ),"" );
29
- table = Table (t_address);
30
- }
31
-
32
- function select (int64 id ) public view returns (string memory , string memory )
33
- {
34
- Entry memory entry = table.select (cast.s64ToString (id));
35
- string memory name;
36
- string memory age;
37
- if (entry.fields.length == 3 ){
38
- name = entry.fields[0 ];
39
- age = entry.fields[1 ];
40
- }
41
- return (name, age);
42
- }
43
-
44
- function insert (int64 id , string memory name , string memory age ) public returns (int32 ){
45
- Entry memory entry = Entry (cast.s64ToString (id), new string [](3 ));
46
- entry.fields[0 ] = name;
47
- entry.fields[1 ] = age;
48
- entry.fields[2 ] = "init " ;
49
- int32 result = table.insert (entry);
50
- emit InsertResult (result);
51
- return result;
52
- }
53
-
54
- function update (int64 id , string memory name , string memory age ) public returns (int32 ){
55
- UpdateField[] memory updateFields = new UpdateField [](2 );
56
- updateFields[0 ] = UpdateField ("name " , name);
57
- updateFields[1 ] = UpdateField ("age " , age);
58
-
59
- int32 result = table.update (cast.s64ToString (id), updateFields);
60
- emit UpdateResult (result);
61
- return result;
62
- }
63
-
64
- function remove (int64 id ) public returns (int32 ){
65
- int32 result = table.remove (cast.s64ToString (id));
66
- emit RemoveResult (result);
67
- return result;
68
- }
69
-
70
- function select (int64 idLow , int64 idHigh ) public view returns (string [] memory )
71
- {
72
- Limit memory limit = Limit (0 , 500 );
73
- Condition[] memory cond = new Condition [](2 );
74
- cond[0 ] = Condition (ConditionOP.GT, "id " , cast.s64ToString (idLow));
75
- cond[1 ] = Condition (ConditionOP.LE, "id " , cast.s64ToString (idHigh));
76
- Entry[] memory entries = table.select (cond, limit);
77
- string [] memory names = new string [](entries.length );
78
- for (uint i = 0 ; i < names.length ; i++ )
79
- {
80
- names[i] = entries[i].fields[0 ];
81
- }
82
- return names;
83
- }
84
-
85
- function count (int64 idLow , int64 idHigh ) public view returns (uint32 )
86
- {
87
- Condition[] memory cond = new Condition [](2 );
88
- cond[0 ] = Condition (ConditionOP.GT, "id " , cast.s64ToString (idLow));
89
- cond[1 ] = Condition (ConditionOP.LE, "id " , cast.s64ToString (idHigh));
90
- return table.count (cond);
91
- }
92
-
93
- function update (int64 idLow , int64 idHigh ) public returns (int32 )
94
- {
95
- UpdateField[] memory updateFields = new UpdateField [](1 );
96
- updateFields[0 ] = UpdateField ("status " , "updated " );
97
-
98
- Limit memory limit = Limit (0 , 500 );
99
- Condition[] memory cond = new Condition [](2 );
100
- cond[0 ] = Condition (ConditionOP.GT, "id " , cast.s64ToString (idLow));
101
- cond[1 ] = Condition (ConditionOP.LE, "id " , cast.s64ToString (idHigh));
102
- return table.update (cond, limit, updateFields);
103
- }
104
-
105
- function remove (int64 idLow , int64 idHigh ) public returns (int32 )
106
- {
107
- Limit memory limit = Limit (0 , 500 );
108
- Condition[] memory cond = new Condition [](2 );
109
- cond[0 ] = Condition (ConditionOP.GT, "id " , cast.s64ToString (idLow));
110
- cond[1 ] = Condition (ConditionOP.LE, "id " , cast.s64ToString (idHigh));
111
- return table.remove (cond, limit);
112
- }
113
-
114
- function createTable (string memory tableName , uint8 keyOrder , string memory key ,string [] memory fields ) public returns (int256 ){
115
- require (keyOrder == 0 || keyOrder == 1 );
116
- KeyOrder _keyOrder = KeyOrder.Lexicographic;
117
- if (keyOrder == 1 )
118
- {
119
- _keyOrder = KeyOrder.Numerical;
120
- }
121
- TableInfo memory tf = TableInfo (_keyOrder, key, fields);
122
- int32 result = tm.createTable (tableName,tf);
123
- emit CreateResult (result);
124
- return result;
125
- }
126
-
127
- function desc () public view returns (string memory , string [] memory ){
128
- TableInfo memory ti = tm.descWithKeyOrder (TABLE_NAME);
129
- return (ti.keyColumn,ti.valueColumns);
130
- }
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity >= 0.6.10 < 0.8.20 ;
3
+ pragma experimental ABIEncoderV2;
4
+
5
+ import "./Table.sol " ;
6
+
7
+ contract TableTest {
8
+ event CreateResult (int256 count );
9
+ event InsertResult (int256 count );
10
+ event UpdateResult (int256 count );
11
+ event RemoveResult (int256 count );
12
+
13
+ TableManager constant tm = TableManager (address (0x1002 ));
14
+ Table table;
15
+ string constant TABLE_NAME = "t_test " ;
16
+ constructor () public {
17
+ // create table
18
+ string [] memory columnNames = new string [](2 );
19
+ columnNames[0 ] = "name " ;
20
+ columnNames[1 ] = "age " ;
21
+ TableInfo memory tf = TableInfo ("id " , columnNames);
22
+
23
+ tm.createTable (TABLE_NAME, tf);
24
+ address t_address = tm.openTable (TABLE_NAME);
25
+ require (t_address!= address (0x0 ),"" );
26
+ table = Table (t_address);
27
+ }
28
+
29
+ function select (string memory id ) public view returns (string memory ,string memory )
30
+ {
31
+ Entry memory entry = table.select (id);
32
+
33
+ string memory name;
34
+ string memory age;
35
+ if (entry.fields.length == 2 ){
36
+ name = entry.fields[0 ];
37
+ age = entry.fields[1 ];
38
+ }
39
+ return (name,age);
40
+ }
41
+
42
+ function insert (string memory id ,string memory name ,string memory age ) public returns (int32 ){
43
+ string [] memory columns = new string [](2 );
44
+ columns[0 ] = name;
45
+ columns[1 ] = age;
46
+ Entry memory entry = Entry (id, columns);
47
+ int32 result = table.insert (entry);
48
+ emit InsertResult (result);
49
+ return result;
50
+ }
51
+
52
+ function update (string memory id , string memory name , string memory age ) public returns (int32 ){
53
+ UpdateField[] memory updateFields = new UpdateField [](2 );
54
+ updateFields[0 ] = UpdateField ("name " , name);
55
+ updateFields[1 ] = UpdateField ("age " , age);
56
+
57
+ int32 result = table.update (id, updateFields);
58
+ emit UpdateResult (result);
59
+ return result;
60
+ }
61
+
62
+ function remove (string memory id ) public returns (int32 ){
63
+ int32 result = table.remove (id);
64
+ emit RemoveResult (result);
65
+ return result;
66
+ }
67
+
68
+ function createTable (string memory tableName ,string memory key ,string [] memory fields ) public returns (int256 ){
69
+ TableInfo memory tf = TableInfo (key, fields);
70
+ int32 result = tm.createTable (tableName,tf);
71
+ emit CreateResult (result);
72
+ return result;
73
+ }
74
+
75
+ function desc () public view returns (string memory , string [] memory ){
76
+ TableInfo memory ti = tm.desc (TABLE_NAME);
77
+ return (ti.keyColumn,ti.valueColumns);
78
+ }
131
79
}
0 commit comments