-
Notifications
You must be signed in to change notification settings - Fork 0
/
backside.cpp
559 lines (499 loc) · 18.8 KB
/
backside.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include "backside.h"
sql::Connection *con = 0;
sql::PreparedStatement *pstmt = 0;
sql::ResultSet *res = 0;
void verbinde_db() {
try {
// INIT:
sql::mysql::MySQL_Driver *driver = 0;
driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("tcp://localhost", "netzplan", "netzplan");
con->setSchema("netzplan");
} catch (sql::SQLException &e) {
std::cerr << "# ERR: " << e.what()
<< " (MySQL error code: " << e.getErrorCode()
<< ", SQLState: " << e.getSQLState() << " )" << std::endl;
}
}
void trenne_db() {
if (con) {
delete con;
}
}
void loesche_geraete(std::string &) { throw std::string("Not Implemented"); }
void loesche_geraet(wohnung wohn, int pos) {
pstmt = con->prepareStatement("DELETE FROM geraet WHERE ort_haus = ? AND "
"ort_apartment = ? AND ort_position = ?;");
pstmt->setInt(1, wohn.haus);
pstmt->setInt(2, wohn.apartment);
pstmt->setInt(3, pos);
pstmt->executeUpdate();
delete pstmt;
pstmt = 0;
}
void loesche_verbindungen(std::string &, connection_type typ) {
throw std::string("Not Implemented.");
}
void loesche_verbindungen(std::string &, unsigned, connection_type typ) {
throw std::string("Not Implemented.");
}
void loesche_verbindungen(wohnung wohn, int pos, unsigned port,
connection_type typ) {
unsigned geraet_id = geraete_id_von_ort(wohn, pos);
pstmt = con->prepareStatement(
"DELETE FROM " +
std::string(typ == Patchung ? "patchung" : "verkabelung") +
" WHERE (von_geraet = ? AND von_port = ?) OR (zu_geraet = ? AND "
"zu_port = ?);");
pstmt->setUInt(1, geraet_id);
pstmt->setUInt(2, port);
pstmt->setUInt(3, geraet_id);
pstmt->setUInt(4, port);
pstmt->executeUpdate();
delete pstmt;
pstmt = 0;
}
bool geraet_existiert(wohnung wohn, int pos) {
bool result = false;
pstmt =
con->prepareStatement("SELECT id FROM geraet WHERE ort_haus = ? AND "
"ort_apartment = ? AND ort_position = ?;");
pstmt->setInt(1, wohn.haus);
pstmt->setInt(2, wohn.apartment);
pstmt->setInt(3, pos);
res = pstmt->executeQuery();
if (res->next()) {
result = true;
}
delete res;
res = 0;
delete pstmt;
pstmt = 0;
return result;
}
bool geraetetyp_existiert(std::string &name) {
bool result = false;
pstmt =
con->prepareStatement("SELECT id FROM geraetetyp WHERE typname = ?;");
pstmt->setString(1, name);
res = pstmt->executeQuery();
if (res->next()) {
result = true;
}
delete res;
res = 0;
delete pstmt;
pstmt = 0;
return result;
}
unsigned geraetetyp_id_von_typname(std::string &typname) {
unsigned typ = 0;
pstmt =
con->prepareStatement("SELECT id FROM geraetetyp WHERE typname = ?;");
pstmt->setString(1, typname);
res = pstmt->executeQuery();
if (res->next()) {
typ = res->getUInt("id");
} else {
throw std::string("Geraete-Typ nicht gefunden (Fehler 110)");
}
delete res;
res = 0;
delete pstmt;
pstmt = 0;
return typ;
}
void erzeuge_geraet(wohnung wohn, std::string &typname, int pos,
std::string &name) {
unsigned typid = geraetetyp_id_von_typname(typname);
pstmt = con->prepareStatement("INSERT INTO geraet (typ, ort_haus, "
"ort_apartment, ort_position, name) VALUES "
"(?, ?, ?, ?, ?);");
pstmt->setUInt(1, typid);
pstmt->setInt(2, wohn.haus);
pstmt->setInt(3, wohn.apartment);
pstmt->setInt(4, pos);
pstmt->setString(5, name);
pstmt->executeUpdate();
delete pstmt;
pstmt = 0;
std::cout << "*";
}
unsigned geraete_id_von_ort(wohnung wohn, int pos) {
unsigned geraet_id = 0;
pstmt =
con->prepareStatement("SELECT id FROM geraet WHERE ort_haus = ? AND "
"ort_apartment = ? AND ort_position = ?;");
pstmt->setInt(1, wohn.haus);
pstmt->setInt(2, wohn.apartment);
pstmt->setInt(3, pos);
res = pstmt->executeQuery();
if (res->next()) {
geraet_id = res->getUInt("id");
} else {
throw std::string("Geraet nicht gefunden (Fehler 145)");
}
delete res;
res = 0;
delete pstmt;
pstmt = 0;
return geraet_id;
}
void erzeuge_verbindung(wohnung wohn1, int pos1, unsigned port1,
wohnung wohn2, int pos2, unsigned port2,
connection_type typ, std::string &medium) {
unsigned geraet_id1 = geraete_id_von_ort(wohn1, pos1);
unsigned geraet_id2 = geraete_id_von_ort(wohn2, pos2);
pstmt = con->prepareStatement(
"INSERT INTO " +
std::string(typ == Patchung ? "patchung" : "verkabelung") +
"(von_geraet, von_port, zu_geraet, zu_port, medium) VALUES "
"(?,?,?,?,?);");
pstmt->setUInt(1, geraet_id1);
pstmt->setUInt(2, port1);
pstmt->setUInt(3, geraet_id2);
pstmt->setUInt(4, port2);
pstmt->setString(5, medium);
pstmt->executeUpdate();
pstmt->setUInt(1, geraet_id2);
pstmt->setUInt(2, port2);
pstmt->setUInt(3, geraet_id1);
pstmt->setUInt(4, port1);
pstmt->setString(5, medium);
pstmt->executeUpdate();
delete pstmt;
pstmt = 0;
std::cout << "-";
}
void zeige_kabelstrippe(unsigned geraet, std::string typ, int port) {
sql::PreparedStatement *pstmt2 = con->prepareStatement(
"SELECT von_port, zu_port, " + typ +
".medium, geraet.name, zu_geraet "
"FROM " + typ + " " +
"JOIN geraet ON geraet.id = zu_geraet "
"WHERE von_geraet = ?" +
(port < 0 ? ";" : " AND von_port = ?;"));
pstmt2->setUInt(1, geraet);
if (port >= 0) {
pstmt2->setUInt(2, port);
}
sql::ResultSet *res2 = pstmt2->executeQuery();
while (res2->next()) {
std::cout << " p" << res2->getUInt("von_port")
<< (res2->getString("medium") == "kupfer" ? " --- "
: " ... ") << "p"
<< res2->getUInt("zu_port") << " " << res2->getString("name");
if (port < 0) {
std::cout << std::endl;
}
std::string typ2 = (typ == "verkabelung") ? "patchung" : "verkabelung";
zeige_kabelstrippe(res2->getUInt("zu_geraet"), typ2,
res2->getUInt("zu_port"));
}
delete res2;
delete pstmt2;
}
void zeige_alle_geraetetypen() {
std::string delim("\n");
zeige_alle_geraetetypen(delim);
std::cout << std::endl;
}
void zeige_alle_geraetetypen(std::string &delim) {
pstmt = con->prepareStatement("SELECT typname, n_ports FROM geraetetyp;");
res = pstmt->executeQuery();
bool first = true;
while (res->next()) {
std::cout << (first ? "" : delim) << res->getString("typname");
first = false;
}
delete res;
res = 0;
delete pstmt;
pstmt = 0;
}
void zeige_geraet(wohnung wohn, int pos) {
pstmt = con->prepareStatement(
"SELECT name, ort_haus, ort_apartment, ort_position, typname, "
"geraet.kommentar, typ AS kommentar "
"FROM geraet "
"JOIN geraetetyp ON geraet.typ = geraetetyp.id "
"WHERE ort_haus = ? AND ort_apartment = ? AND ort_position = ?;");
pstmt->setInt(1, wohn.haus);
pstmt->setInt(2, wohn.apartment);
pstmt->setInt(3, pos);
res = pstmt->executeQuery();
if (res->next()) {
std::cout << "Geraet " << res->getString("name") << ": "
<< res->getString("typname") << " in "
<< res->getInt("ort_haus") << "/"
<< res->getInt("ort_apartment") << " (pos "
<< res->getInt("ort_position") << ")"
<< ", Kommentar: " << res->getString("kommentar")
<< std::endl;
std::string typ = "patchung";
zeige_kabelstrippe(res->getUInt("typ"), typ);
std::cout << std::endl;
typ = "verkabelung";
zeige_kabelstrippe(res->getUInt("typ"), typ);
}
delete res;
res = 0;
delete pstmt;
pstmt = 0;
}
void zeige_geraet(std::string &name, int skip) {
pstmt = con->prepareStatement(
"SELECT name, typname, ort_haus, ort_apartment, ort_position, "
"geraet.kommentar AS kommentar "
"FROM geraet "
"JOIN geraetetyp ON geraet.typ = geraetetyp.id "
"WHERE INSTR(geraet.name, ?)>0 AND (typ > ?)"
"ORDER BY typ DESC, ort_haus, ort_apartment, ort_position;");
pstmt->setString(1, name);
pstmt->setInt(2, skip);
res = pstmt->executeQuery();
while (res->next()) {
std::cout << "Geraet " << res->getString("name") << " ("
<< res->getString("typname") << ") in "
<< res->getInt("ort_haus") << "/"
<< res->getInt("ort_apartment") << " (pos "
<< res->getInt("ort_position") << ")"
<< " \"" << res->getString("kommentar") << "\""
<< std::endl;
}
delete res;
res = 0;
delete pstmt;
pstmt = 0;
}
void zeige_geraete_in_wohnung(wohnung wohn) {
pstmt = con->prepareStatement(
"SELECT name, typname, ort_haus, ort_apartment, ort_position, "
"geraet.kommentar AS kommentar "
"FROM geraet "
"JOIN geraetetyp ON geraet.typ = geraetetyp.id "
"WHERE ort_haus = ? AND ort_apartment = ? "
"ORDER BY ort_position ;");
pstmt->setInt(1, wohn.haus);
pstmt->setInt(2, wohn.apartment);
res = pstmt->executeQuery();
std::cout << "In " << wohn.haus << "/" << wohn.apartment << ":\n";
while (res->next()) {
std::cout << "Geraet " << res->getString("name") << " ("
<< res->getString("typname") << ") an Pos "
<< res->getInt("ort_position")
<< " \"" << res->getString("kommentar") << "\""
<< std::endl;
}
delete res;
res = 0;
delete pstmt;
pstmt = 0;
}
void zeige_geraetetyp(std::string &name) {
pstmt = con->prepareStatement(
"SELECT * FROM geraetetyp WHERE INSTR(typname, ?)>0;");
pstmt->setString(1, name);
res = pstmt->executeQuery();
while (res->next()) {
std::cout << "Typ " << res->getString("typname") << ": "
<< res->getUInt("n_ports") << "-Port "
<< res->getString("hersteller") << " ("
<< res->getString("modell")
<< ") \"" << res->getString("kommentar") << "\""
<< std::endl;
sql::PreparedStatement *pstmt2 =
con->prepareStatement("SELECT * FROM geraet WHERE typ = ?");
pstmt2->setUInt(1, res->getUInt("id"));
sql::ResultSet *res2 = pstmt2->executeQuery();
while (res2->next()) {
std::cout << " Geraet " << res2->getString("name") << " in "
<< res2->getInt("ort_haus") << "/"
<< res2->getInt("ort_apartment") << " (pos "
<< res2->getInt("ort_position") << ")" << std::endl;
}
delete res2;
delete pstmt2;
}
delete res;
res = 0;
delete pstmt;
pstmt = 0;
}
void dump_all() {
pstmt = con->prepareStatement(
"SELECT ort_haus, ort_apartment, ort_position, name, typname "
"FROM geraet "
"JOIN geraetetyp ON geraet.typ = geraetetyp.id "
"ORDER BY ort_haus, ort_apartment, ort_position;");
res = pstmt->executeQuery();
while (res->next()) {
std::string typ = res->getString("typname");
std::cout << res->getInt("ort_haus") << "/"
<< res->getInt("ort_apartment") << "\tgeraet\t" << typ
<< (typ.length() > 7 ? "\t" : "\t\t")
<< res->getInt("ort_position") << "\t"
<< res->getString("name") << " ;" << std::endl;
}
delete res;
res = 0;
delete pstmt;
pstmt = 0;
std::cout << std::endl;
pstmt = con->prepareStatement(
"SELECT g.ort_haus, g.ort_apartment, g.ort_position, g2.ort_haus AS "
"ort_haus2, g2.ort_apartment AS ort_apartment2, g2.ort_position AS "
"ort_position2, von_port, zu_port, medium "
"FROM verkabelung "
"JOIN geraet AS g ON verkabelung.von_geraet = g.id "
"JOIN geraet AS g2 ON verkabelung.zu_geraet = g2.id "
"WHERE g.id < g2.id "
"OR (g.id = g2.id AND von_port < zu_port);");
res = pstmt->executeQuery();
while (res->next()) {
std::cout << res->getInt("ort_haus") << "/"
<< res->getInt("ort_apartment") << "\t"
<< res->getInt("ort_haus2") << "/"
<< res->getInt("ort_apartment2") << "\t"
<< res->getString("medium") << "\t"
<< res->getInt("ort_position") << "\t"
<< res->getUInt("von_port") << "\t"
<< res->getInt("ort_position2") << "\t"
<< res->getUInt("zu_port") << " ;" << std::endl;
}
delete res;
res = 0;
delete pstmt;
pstmt = 0;
std::cout << std::endl;
for (int i = 0; i < 2; i++) {
std::string medium = i ? "glas" : "kupfer";
pstmt = con->prepareStatement(
"SELECT DISTINCT ort_haus, ort_apartment FROM geraet;");
res = pstmt->executeQuery();
while (res->next()) {
wohnung wohn = {res->getInt("ort_haus"),
res->getInt("ort_apartment")};
// std::cerr << wohn.haus << "/" << wohn.apartment <<
// std::endl;
sql::PreparedStatement *pstmt2 = con->prepareStatement(
"SELECT g.ort_haus, g.ort_apartment, g.ort_position, "
"g2.ort_position AS ort_position2, von_port, zu_port "
"FROM patchung "
"JOIN geraet AS g ON patchung.von_geraet = g.id "
"JOIN geraet AS g2 ON patchung.zu_geraet = g2.id "
"WHERE g.ort_haus = ? AND g.ort_apartment = ? "
"AND (g.id < g2.id OR (g.id = g2.id AND von_port < zu_port)) "
"AND patchung.medium = ?;");
pstmt2->setInt(1, wohn.haus);
pstmt2->setInt(2, wohn.apartment);
pstmt2->setString(3, medium);
sql::ResultSet *res2 = pstmt2->executeQuery();
bool first = true;
while (res2->next()) {
if (first) {
std::cout << wohn.haus << "/" << wohn.apartment
<< "\tpatch " << medium;
first = false;
}
std::cout << std::endl << res2->getInt("ort_position") << "\t"
<< res2->getUInt("von_port") << "\t"
<< res2->getInt("ort_position2") << "\t"
<< res2->getUInt("zu_port");
}
if (!first) {
std::cout << " ;\n";
}
delete res2;
delete pstmt2;
}
delete res;
res = 0;
delete pstmt;
pstmt = 0;
}
}
void setup_db() {
/* WARNING:
Before you call this funktion, make sure to have a backup of the database
*/
using namespace std;
string table_geraetetyp_string = "CREATE TABLE geraetetyp ("
"id INT UNSIGNED NOT NULL AUTO_INCREMENT, "
"typname VARCHAR(250), "
"n_ports INT UNSIGNED, "
"hersteller VARCHAR(250), "
"modell VARCHAR(250), "
"kommentar TEXT, "
"PRIMARY KEY (id));";
string table_geraete_string = "CREATE TABLE geraet ("
"id INT UNSIGNED NOT NULL AUTO_INCREMENT, "
"typ INT UNSIGNED, "
"ort_haus INT, "
"ort_apartment INT, "
"ort_position INT, "
"name VARCHAR(250), "
"mac VARCHAR(250), "
"seriennummer VARCHAR(250), "
"kommentar TEXT, "
"PRIMARY KEY (id));";
string table_patchung_string = "CREATE TABLE patchung ("
"von_geraet INT UNSIGNED, "
"von_port INT UNSIGNED, "
"zu_geraet INT UNSIGNED, "
"zu_port INT UNSIGNED, "
"medium VARCHAR(250), "
"kommentar VARCHAR(250));";
string table_verkabelung_string = "CREATE TABLE verkabelung ("
"von_geraet INT UNSIGNED, "
"von_port INT UNSIGNED, "
"zu_geraet INT UNSIGNED, "
"zu_port INT UNSIGNED, "
"medium VARCHAR(255), "
"kommentar VARCHAR(255));";
string create_list[] = {table_geraetetyp_string, table_geraete_string,
table_patchung_string, table_verkabelung_string};
string delete_list[] = {"patchung", "verkabelung", "geraet", "geraetetyp"};
class geraetetyp_record {
public:
string name;
uint n_ports;
string hersteller;
string modell;
};
geraetetyp_record geraetetypen[] = {
{"dose", 2, "", ""},
{"mhunap", 5, "", ""},
{"pp24", 24, "", ""},
{"pp32", 32, "", ""},
{"pp48", 48, "", ""},
{"hp2530", 10, "hp", "2530-8G (J9777A)"},
{"hp2620-24", 28, "hp", "2620-24 (J9623A)"},
{"hp2620-48", 52, "hp", "2620-48 (J9626A)"},
{"hp2626", 28, "hp", "2626 (J4900A)"},
{"3com4200", 26, "3com", "4228G (3C17304)"},
{"3com4500", 50, "3com", "4500 (3CR17562-91)"},
{"3com4800", 24, "3com", "4800G (3CRS48G-24S-91)"}};
string geraete_insert =
"INSERT INTO geraetetyp (typname, n_ports, hersteller, "
"modell) VALUES (?, ?, ?, ?)";
for (string table : delete_list) {
pstmt = con->prepareStatement("DROP TABLE IF EXISTS " + table + ";");
pstmt->execute();
delete pstmt;
}
for (string statement : create_list) {
pstmt = con->prepareStatement(statement);
pstmt->execute();
delete pstmt;
}
pstmt = con->prepareStatement(geraete_insert);
for (geraetetyp_record geraetetyp : geraetetypen) {
pstmt->setString(1, geraetetyp.name);
pstmt->setUInt(2, geraetetyp.n_ports);
pstmt->setString(3, geraetetyp.hersteller);
pstmt->setString(4, geraetetyp.modell);
pstmt->execute();
}
delete pstmt;
}