|
1 | 1 | pragma solidity ^0.4.24;
|
2 | 2 | pragma experimental ABIEncoderV2;
|
3 |
| - |
4 | 3 | import "./Table.sol";
|
5 | 4 |
|
6 | 5 | contract TableTest {
|
7 |
| - event CreateResult(int count); |
8 |
| - event InsertResult(int count); |
9 |
| - event UpdateResult(int count); |
10 |
| - event RemoveResult(int count); |
11 |
| - |
12 |
| - //create table |
13 |
| - function create() public returns(int){ |
14 |
| - TableFactory tf = TableFactory(0x1001); //The fixed address is 0x1001 for TableFactory |
15 |
| - |
16 |
| - int count = tf.createTable("t_test", "name", "item_id,item_name"); |
17 |
| - emit CreateResult(count); |
18 |
| - return count; |
| 6 | + event CreateResult(int256 count); |
| 7 | + event InsertResult(int256 count); |
| 8 | + event UpdateResult(int256 count); |
| 9 | + event RemoveResult(int256 count); |
| 10 | + |
| 11 | + TableFactory tableFactory; |
| 12 | + string constant TABLE_NAME = "t_test"; |
| 13 | + constructor() public { |
| 14 | + tableFactory = TableFactory(0x1001); //The fixed address is 0x1001 for TableFactory |
| 15 | + // the parameters of createTable are tableName,keyField,"vlaueFiled1,vlaueFiled2,vlaueFiled3,..." |
| 16 | + tableFactory.createTable(TABLE_NAME, "name", "item_id,item_name"); |
19 | 17 | }
|
20 | 18 |
|
21 | 19 | //select records
|
22 |
| - function select(string name) public constant returns(string[], int[], string[]){ |
23 |
| - TableFactory tf = TableFactory(0x1001); |
24 |
| - Table table = tf.openTable("t_test"); |
25 |
| - |
| 20 | + function select(string name) |
| 21 | + public |
| 22 | + view |
| 23 | + returns (string[], int256[], string[]) |
| 24 | + { |
| 25 | + Table table = tableFactory.openTable(TABLE_NAME); |
| 26 | + |
26 | 27 | Condition condition = table.newCondition();
|
27 |
| - |
| 28 | + |
28 | 29 | Entries entries = table.select(name, condition);
|
29 |
| - string[] memory user_name_bytes_list = new string[](uint256(entries.size())); |
30 |
| - int[] memory item_id_list = new int[](uint256(entries.size())); |
31 |
| - string[] memory item_name_bytes_list = new string[](uint256(entries.size())); |
32 |
| - |
33 |
| - for(int i=0; i<entries.size(); ++i) { |
| 30 | + string[] memory user_name_bytes_list = new string[]( |
| 31 | + uint256(entries.size()) |
| 32 | + ); |
| 33 | + int256[] memory item_id_list = new int256[](uint256(entries.size())); |
| 34 | + string[] memory item_name_bytes_list = new string[]( |
| 35 | + uint256(entries.size()) |
| 36 | + ); |
| 37 | + |
| 38 | + for (int256 i = 0; i < entries.size(); ++i) { |
34 | 39 | Entry entry = entries.get(i);
|
35 |
| - |
| 40 | + |
36 | 41 | user_name_bytes_list[uint256(i)] = entry.getString("name");
|
37 | 42 | item_id_list[uint256(i)] = entry.getInt("item_id");
|
38 |
| - item_name_bytes_list[uint256(i)] = entry.getString("item_name"); |
| 43 | + item_name_bytes_list[uint256(i)] = entry.getString("item_name"); |
39 | 44 | }
|
40 |
| - |
| 45 | + |
41 | 46 | return (user_name_bytes_list, item_id_list, item_name_bytes_list);
|
42 | 47 | }
|
43 | 48 | //insert records
|
44 |
| - function insert(string name, int item_id, string item_name) public returns(int) { |
45 |
| - TableFactory tf = TableFactory(0x1001); |
46 |
| - Table table = tf.openTable("t_test"); |
47 |
| - |
| 49 | + function insert(string name, int256 item_id, string item_name) |
| 50 | + public |
| 51 | + returns (int256) |
| 52 | + { |
| 53 | + Table table = tableFactory.openTable(TABLE_NAME); |
| 54 | + |
48 | 55 | Entry entry = table.newEntry();
|
49 | 56 | entry.set("name", name);
|
50 | 57 | entry.set("item_id", item_id);
|
51 | 58 | entry.set("item_name", item_name);
|
52 |
| - |
53 |
| - int count = table.insert(name, entry); |
| 59 | + |
| 60 | + int256 count = table.insert(name, entry); |
54 | 61 | emit InsertResult(count);
|
55 |
| - |
| 62 | + |
56 | 63 | return count;
|
57 | 64 | }
|
58 | 65 | //update records
|
59 |
| - function update(string name, int item_id, string item_name) public returns(int) { |
60 |
| - TableFactory tf = TableFactory(0x1001); |
61 |
| - Table table = tf.openTable("t_test"); |
62 |
| - |
| 66 | + function update(string name, int256 item_id, string item_name) |
| 67 | + public |
| 68 | + returns (int256) |
| 69 | + { |
| 70 | + Table table = tableFactory.openTable(TABLE_NAME); |
| 71 | + |
63 | 72 | Entry entry = table.newEntry();
|
64 | 73 | entry.set("item_name", item_name);
|
65 |
| - |
| 74 | + |
66 | 75 | Condition condition = table.newCondition();
|
67 | 76 | condition.EQ("name", name);
|
68 | 77 | condition.EQ("item_id", item_id);
|
69 |
| - |
70 |
| - int count = table.update(name, entry, condition); |
| 78 | + |
| 79 | + int256 count = table.update(name, entry, condition); |
71 | 80 | emit UpdateResult(count);
|
72 |
| - |
| 81 | + |
73 | 82 | return count;
|
74 | 83 | }
|
75 | 84 | //remove records
|
76 |
| - function remove(string name, int item_id) public returns(int){ |
77 |
| - TableFactory tf = TableFactory(0x1001); |
78 |
| - Table table = tf.openTable("t_test"); |
79 |
| - |
| 85 | + function remove(string name, int256 item_id) public returns (int256) { |
| 86 | + Table table = tableFactory.openTable(TABLE_NAME); |
| 87 | + |
80 | 88 | Condition condition = table.newCondition();
|
81 | 89 | condition.EQ("name", name);
|
82 | 90 | condition.EQ("item_id", item_id);
|
83 |
| - |
84 |
| - int count = table.remove(name, condition); |
| 91 | + |
| 92 | + int256 count = table.remove(name, condition); |
85 | 93 | emit RemoveResult(count);
|
86 |
| - |
| 94 | + |
87 | 95 | return count;
|
88 | 96 | }
|
89 | 97 | }
|
0 commit comments