-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.cpp
208 lines (175 loc) · 5.38 KB
/
state.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
EXEC SQL WHENEVER SQLERROR SQLPRINT;
void create_state() {
cout << "Введите название АЕ:" << endl;
string tmpname;
cin >> tmpname;
const char* name = tmpname.c_str();
cout << "Введите название страны:" << endl;
string tmpcountry_name;
cin >> tmpcountry_name;
const char* country_name = tmpcountry_name.c_str();
cout << "Введите ФИО главы:" << endl;
string tmpleader_name;
cin >> tmpleader_name;
const char* leader_name = tmpleader_name.c_str();
cout << "Введите площадь:" << endl;
int square; cin >> square;
cout << "Введите количество населения:" << endl;
int population_density; cin >> population_density;
cout << country_name << leader_name << square << population_density;
EXEC SQL BEGIN DECLARE SECTION;
const char* n = name;
const char* ln = leader_name;
const char* s = country_name;
int sq = square;
int pd = population_density;
EXEC SQL END DECLARE SECTION;
EXEC SQL INSERT INTO state(name, country_name, leader_name, square, population_density ) VALUES(:n, :s, :ln, :sq, :pd);
EXEC SQL COMMIT;
return;
}
void show_state() {
cout << "Введите название АЕ:\n";
string tmpname;
cin >> tmpname;
const char* name = tmpname.c_str();
EXEC SQL BEGIN DECLARE SECTION;
const char* n = name;
char ln [256];
char s [256] ;
int sq;
int pd;
EXEC SQL END DECLARE SECTION;
EXEC SQL DECLARE cursor_state_find CURSOR FOR
SELECT country_name, leader_name, square, population_density FROM state WHERE name = :n ORDER BY name;
EXEC SQL OPEN cursor_state_find;
int count = 0;
while (true) {
EXEC SQL FETCH cursor_state_find INTO :s, :ln, :sq, :pd;
if (sqlca.sqlcode == ECPG_NOT_FOUND || strncmp(sqlca.sqlstate,"00",2))
{
if (count == 0) cout << "Не найдено\n";
break;
}
count ++ ;
cout << endl;
cout << "Название АЕ: " << n << endl;
cout << "Название страны: " << s << endl;
cout << "ФИО лидера: " << ln << endl;
cout << "Площадь: " << sq << endl;
cout << "Количество населения: " << pd << endl;
cout << endl;
}
EXEC SQL CLOSE cursor_state_find;
return;
}
void update_state() {
cout << "Введите название АЕ:" << endl;
string tmpname;
cin >> tmpname;
const char* name = tmpname.c_str();
cout << "Введите название страны:" << endl;
string tmpcountry_name;
cin >> tmpcountry_name;
const char* country_name = tmpcountry_name.c_str();
cout << "Введите ФИО главы:" << endl;
string tmpleader_name;
cin >> tmpleader_name;
const char* leader_name = tmpleader_name.c_str();
cout << "Введите площадь:" << endl;
int square; cin >> square;
cout << "Введите количество населения:" << endl;
int population_density; cin >> population_density;
EXEC SQL BEGIN DECLARE SECTION;
const char* n = name;
const char* ln = leader_name;
const char* s = country_name;
int sq = square;
int pd = population_density;
EXEC SQL END DECLARE SECTION;
EXEC SQL UPDATE state SET country_name = :s, leader_name = :ln, square = :sq, population_density = :pd WHERE name = :n AND country_name = :s;
EXEC SQL COMMIT;
return;
}
void delete_state() {
cout << "Введите название АЕ:" << endl;
string tmpname;
cin >> tmpname;
const char* name = tmpname.c_str();
cout << "Введите название страны:" << endl;
string tmpcountry_name;
cin >> tmpcountry_name;
const char* country_name = tmpcountry_name.c_str();
EXEC SQL BEGIN DECLARE SECTION;
const char* n = name;
const char* s = country_name;
EXEC SQL END DECLARE SECTION;
EXEC SQL DELETE FROM state WHERE name = :n AND country_name = :s;
EXEC SQL COMMIT;
return;
}
void printTable_state() {
EXEC SQL BEGIN DECLARE SECTION;
char n [256];
char ln [256];
char s [256] ;
int sq;
int pd;
EXEC SQL END DECLARE SECTION;
EXEC SQL DECLARE cursor_state CURSOR FOR
SELECT name, country_name, leader_name, square, population_density FROM state ORDER BY name;
EXEC SQL OPEN cursor_state;
while (true) {
EXEC SQL FETCH cursor_state INTO :n, :s, :ln, :sq, :pd;
if (sqlca.sqlcode == ECPG_NOT_FOUND) {
break;
}
cout << endl;
cout << "Название АЕ: " << n << endl;
cout << "Название страны: " << s << endl;
cout << "ФИО лидера: " << ln << endl;
cout << "Площадь: " << sq << endl;
cout << "Количество населения: " << pd << endl;
cout << endl;
}
EXEC SQL CLOSE cursor_state;
return;
}
void menu_state(){
while (true) {
cout << "Меню управления Административными единицами (АЕ):" << endl;
cout << "1. Создать АЕ" << endl;
cout << "2. Найти АЕ по названию" << endl;
cout << "3. Модифицировать информацию об АЕ" << endl;
cout << "4. Удалить АЕ" << endl;
cout << "5. Показать все АЕ" << endl;
cout << "0. Выйти" << endl;
int number;
cin >> number;
switch (number) {
case 1: {
create_state();
break;
}
case 2: {
show_state();
break;
}
case 3: {
update_state();
break;
}
case 4: {
delete_state();
break;
}
case 5: {
printTable_state();
break;
}
case 0: {
return;
}
}
}
}