This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathread_only_query_tests.cpp
156 lines (147 loc) · 3.69 KB
/
read_only_query_tests.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <eosio/eosio.hpp>
#include <eosio/table.hpp>
class [[eosio::contract]] read_only_query_tests : public eosio::contract {
public:
struct my_struct {
uint32_t id;
std::string name;
uint32_t gender;
uint32_t age;
bool operator==(const my_struct& b) const {
return id == b.id &&
name == b.name &&
gender == b.gender &&
age == b.age;
}
};
struct [[eosio::table]] my_table_m : eosio::kv::table<my_struct, "roqm"_n> {
KV_NAMED_INDEX("id"_n, id)
my_table_m(eosio::name contract_name) {
init(contract_name, id);
}
};
struct [[eosio::table]] my_table_f : eosio::kv::table<my_struct, "roqf"_n> {
KV_NAMED_INDEX("id"_n, id)
my_table_f(eosio::name contract_name) {
init(contract_name, id);
}
};
using contract::contract;
my_struct s1{
.id = 1,
.name = "Bob Smith",
.gender = 1,
.age = 25
};
my_struct s2{
.id = 2,
.name = "Alice Smith",
.gender = 0,
.age = 20
};
my_struct s3{
.id = 3,
.name = "John Smith",
.gender = 1,
.age = 42
};
my_struct s4{
.id = 4,
.name = "Jack Smith",
.gender = 1,
.age = 27
};
my_struct s5{
.id = 5,
.name = "Youko Niihara",
.gender = 0,
.age = 26
};
my_struct s6{
.id = 6,
.name = "Rose Lee",
.gender = 0,
.age = 18,
};
my_struct s7{
.id = 7,
.name = "Youko Kawakami",
.gender = 0,
.age = 25,
};
my_struct s8{
.id = 8,
.name = "Yuu Yamada",
.gender = 0,
.age = 24,
};
[[eosio::action]]
void setup() {
my_table_m tm{get_self()};
my_table_f tf{get_self()};
tm.put(s1, get_self());
tf.put(s2, get_self());
tm.put(s3, get_self());
tm.put(s4, get_self());
tf.put(s5, get_self());
tf.put(s6, get_self());
tf.put(s7, get_self());
tf.put(s8, get_self());
}
[[eosio::action, eosio::read_only]]
std::vector<my_struct> get() {
my_table_m tm{get_self()};
my_table_f tf{get_self()};
std::vector<my_struct> ret;
auto itm = tm.id.begin();
auto itm_e = tm.id.end();
eosio::cout << "Males: \n";
while(itm != itm_e){
auto row = itm.value();
eosio::cout << "id=" << row.id << ", name=" << row.name << ",gender=" << row.gender << ", age=" << row.age << "\n";
my_struct s;
s.id = row.id;
s.name = row.name;
s.gender = row.gender;
s.age = row.age;
ret.push_back(s);
++itm;
}
eosio::cout << "Females: \n";
auto itf = tf.id.begin();
auto itf_e = tf.id.end();
while(itf != itf_e){
auto row = itf.value();
eosio::cout << "id=" << row.id << ", name=" << row.name << ",gender=" << row.gender << ", age=" << row.age << "\n";
my_struct s;
s.id = row.id;
s.name = row.name;
s.gender = row.gender;
s.age = row.age;
ret.push_back(s);
++itf;
}
return ret;
}
[[eosio::action]]
// usage: cleos -v push action eosio put '{"id":10,"name":"GULU","gender":1,"age":128}' -p eosio@active
void put(uint32_t id, std::string name, uint32_t gender, uint32_t age ) {
my_table_m tm{get_self()};
my_table_f tf{get_self()};
if(gender == 0){
tf.put({
.id = id,
.name = name,
.gender = gender,
.age = age
}, get_self());
} else {
tm.put({
.id = id,
.name = name,
.gender = gender,
.age = age
}, get_self());
}
}
};