-
Notifications
You must be signed in to change notification settings - Fork 2
/
Example.sol
102 lines (82 loc) · 2.74 KB
/
Example.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
pragma solidity ^0.5.0;
import "../EbakusDB.sol";
contract Example {
event LogUser(uint Id, string Name, string Pass, string Email);
event LogBool(bool _value);
string TableName = "Users";
struct User {
uint64 Id;
string Name;
string Pass;
string Email;
}
constructor() public {
// [{
// "type": "table",
// "name": "Users",
// "inputs": [
// {"name": "Id", "type": "uint64"},
// {"name": "Name", "type": "string"},
// {"name": "Pass", "type": "string"},
// {"name": "Email", "type": "string"}
// ]
// }]
string memory tablesAbi = '[{"type":"table","name":"Users","inputs":[{"name":"Id","type":"uint64"},{"name":"Name","type":"string"},{"name":"Pass","type":"string"},{"name":"Email","type":"string"}]}]';
EbakusDB.createTable(TableName, "Name,Email", tablesAbi);
}
function get() external view returns (uint id, string memory name, string memory pass, string memory email) {
User memory u;
bytes memory out = EbakusDB.get(TableName, "", "Email ASC");
(u.Id, u.Name, u.Pass, u.Email) = abi.decode(out, (uint64, string, string, string));
return (u.Id, u.Name, u.Pass, u.Email);
}
function select() external {
User memory u;
bool found;
bytes memory out;
bytes32 iter = EbakusDB.select(TableName, "", "Name ASC");
(out, found) = EbakusDB.next(iter);
if (found) {
(u.Id, u.Name, u.Pass, u.Email) = abi.decode(out, (uint64, string, string, string));
emit LogUser(u.Id, u.Name, u.Pass, u.Email);
}
(out, found) = EbakusDB.next(iter);
if (found) {
(u.Id, u.Name, u.Pass, u.Email) = abi.decode(out, (uint64, string, string, string));
emit LogUser(u.Id, u.Name, u.Pass, u.Email);
}
(out, found) = EbakusDB.next(iter);
require(!found);
emit LogBool(!found);
}
function insertObjs() external {
User memory u;
bytes memory input;
u = User(1, "Harry", "123", "[email protected]");
input = abi.encode(u.Id, u.Name, u.Pass, u.Email);
bool out = EbakusDB.insertObj(TableName, input);
emit LogBool(out);
u = User(2, "Chris", "456", "[email protected]");
input = abi.encode(u.Id, u.Name, u.Pass, u.Email);
out = EbakusDB.insertObj(TableName, input);
emit LogBool(out);
}
function updateObjs() external {
User memory u;
bytes memory input;
u = User(1, "Harry", "1234", "[email protected]");
input = abi.encode(u.Id, u.Name, u.Pass, u.Email);
bool out = EbakusDB.insertObj(TableName, input);
emit LogBool(out);
u = User(2, "Chris", "4567", "[email protected]");
input = abi.encode(u.Id, u.Name, u.Pass, u.Email);
out = EbakusDB.insertObj(TableName, input);
emit LogBool(out);
}
function deleteObj() external {
// delete user with Id=2
bytes memory input = abi.encode(2);
bool out = EbakusDB.deleteObj(TableName, input);
emit LogBool(out);
}
}