-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.cc
1840 lines (1469 loc) · 61.1 KB
/
database.cc
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* database.cc ---
*
* Copyright (C) 2010 Alp Eren Köse
*
* Author: Alp Eren Köse <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3, or
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/*!
\file database.cc
\date Thu Jul 15 17:38:56 2010
\brief All Database Communication
Encapsulates all the database related functions
with the exception of pci_ids folder having their own functions to update PCI IDs.
*/
#include <cppconn/driver.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/exception.h>
#include <vector>
#include <map>
// #include <string>
#include "os_info.hpp"
#include "pci_device.hpp"
#include "harixApp.hpp" // needed for wApp
#include <Wt/WLogger>
using std::string;
//! Where MySQL Server is located.
const string host_url("localhost");
//! User to connect to the server.
const string user("root");
//! Password of the user.
const string pass("password");
//! Database to connect to.
const string database("harix_db");
//! Object to retrieve results of queries.
static sql::ResultSet* commonResultSet;
//! Object to establish SQL Server connection.
static sql::Connection* connector;
//! SQL statements are given through this.
static sql::Statement* stmt;
//! Connect to Database with MySQL Connector/C++.
/*!
Used in the beginning of each function to open a connection to database.
*/
inline void connectDatabase()
{
sql::Driver* driver = get_driver_instance();
connector = driver->connect(host_url, user, pass);
connector->setSchema(database);
stmt = connector->createStatement();
}
//! Delete the objects used to connect database.
/*!
Used in the end of each function to delete the #stmt, #commonResultSet and #connector.
*/
inline void disconnectDatabase()
{
delete commonResultSet;
delete stmt;
delete connector;
}
//! Query full class name for provided codes.
/*!
Retrieve a string list of Class, Subclass and Prog-if names
corresponding to provided codes.
\param class_code Class Code of a device.
\param subclass_code Subclass Code of a device.
\param progif_code Programming interface Code of a device.
\return Class, Subclass and Prog-if names that correspond to provided codes.
*/
std::vector<string> queryClassName(string class_code, string subclass_code, string progif_code)
{
std::vector<string> full_class_name; // Hold Class, Subclass and Prog-if name.
try{
connectDatabase();
// Make a call to the stored procedure which will return a line of corresponding names to the codes.
// At least Class-Subclass code pair should exist in database for non-empty return.
commonResultSet = stmt->executeQuery("CALL sp_queryClassName('"+ class_code +"','"+ subclass_code +"'," +
"'" + progif_code + "')");
if( commonResultSet->next() ){
full_class_name.push_back(commonResultSet->getString("className"));
full_class_name.push_back(commonResultSet->getString("subclassName"));
full_class_name.push_back(commonResultSet->getString("progifName"));
}
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
}
disconnectDatabase();
return full_class_name;
}
//! Query full device name for provided codes.
/*!
Retrieve a string list of Vendor, Device and Subsystem names
corresponding to provided codes.
\param vendor_code Vendor Code of a device.
\param device_code Device Code of a device.
\param subvendor_code Subvendor Code of a device.
\param subdevice_code Subdevice Code of a device.
\return Vendor, Device and Subsystem name that correspond to provided codes.
*/
std::vector<string>
queryDeviceName(string vendor_code, string device_code, string subvendor_code, string subdevice_code)
{
std::vector<string> full_device_name; // Hold Vendor, Device and Subsystem name.
try{
connectDatabase();
// Make a call to the stored procedure which will return a line of corresponding names to the codes.
// At least Vendor-Device code pair should exist in database for non-empty return.
commonResultSet = stmt->executeQuery("CALL sp_queryDeviceName('"+ vendor_code +"','"+ device_code +"'," +
"'" + subvendor_code + "','" + subdevice_code + "')");
if( commonResultSet->next() ){
full_device_name.push_back(commonResultSet->getString("vendorName"));
full_device_name.push_back(commonResultSet->getString("deviceName"));
full_device_name.push_back(commonResultSet->getString("subsysName"));
}
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
}
disconnectDatabase();
return full_device_name;
}
//! Query list of OSes with their modules that support the provided device.
/*!
Retrieves list of Unique IDs representing OSes and their modules which
support the provided device.
\param vendor_code Vendor Code of provided device.
\param device_code Device Code of provided device.
\param subvendor_code Subvendor Code of provided device.
\param subdevice_code Subdevice Code of provided device.
\param class_code Class Code of provided device.
\param subclass_code Subclass Code of provided device.
\param progif_code Prog-if Code of provided device.
\return List of (Unique OS ID - Module Name ID) pairs.
*/
std::multimap<string, string>
queryPcimapOsList(string vendor_code, string device_code, string subvendor_code, string subdevice_code,
string class_code, string subclass_code, string progif_code)
{
std::multimap<string,string> ukernel_module_map; // ( unique_kernel_id, module_name_id )
try{
connectDatabase();
// Make a call to the stored procedure which will return a list of OS-Module pairs that support the device.
commonResultSet = stmt->executeQuery("CALL sp_queryPcimap('"+ vendor_code +"','"+ device_code +"'," +
"'" + subvendor_code + "','" + subdevice_code + "'," +
"'" + class_code + subclass_code + "','" + progif_code + "')");
while( commonResultSet->next() ){
ukernel_module_map.insert( std::pair<string,string>(commonResultSet->getString("uKernelID"), commonResultSet->getString("modNameID") ) );
}
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
}
disconnectDatabase();
return ukernel_module_map;
}
//! Query full name of an OS.
/*!
Queries full OS name with it's unique id and puts them into a std::vector container.
\param unique_kernel_id Unique kernel ID of an OS.
\return List of Distribution name, Release name, Kernel version and architecture.
*/
std::vector<string> queryOs( string unique_kernel_id )
{
std::vector<string> full_os_details;
try{
connectDatabase();
commonResultSet = stmt->executeQuery("SELECT osName, releaseName, kernelVersion, machineHardware FROM vw_osList WHERE uKernelID='" + unique_kernel_id +"'");
if( commonResultSet->next() ){
full_os_details.push_back(commonResultSet->getString("osName"));
full_os_details.push_back(commonResultSet->getString("releaseName"));
full_os_details.push_back(commonResultSet->getString("kernelVersion"));
full_os_details.push_back(commonResultSet->getString("machineHardware"));
}
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
}
disconnectDatabase();
return full_os_details;
}
//! Query unique kernel ID of an OS.
/*!
Queries the Unique Kernel ID representing each OS stored in database.
\param os_name Distribution name(e.g. Ubuntu).
\param release Release name of given distribution(e.g. 9.10).
\param kernel Kernel Version of given distribution(e.g. 2.6.32).
\param architecture Architecture of the kernel compiled for(e.g. i686).
\return The Unique Kernel ID of OS.
*/
string queryOsKernelId( string os_name, string release, string kernel, string architecture )
{
string ukernel_id="";
try{
connectDatabase();
commonResultSet = stmt->executeQuery("SELECT uKernelID FROM vw_osList WHERE osName='"+ os_name + "' " +
"AND releaseName='"+ release +"' " +
"AND kernelVersion='"+ kernel +"' " +
"AND machineHardware='"+ architecture +"'");
if( commonResultSet->next() ){
ukernel_id = commonResultSet->getString("uKernelID");
}
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
}
disconnectDatabase();
return ukernel_id;
}
//! Query Name of the Module having the provided ID.
/*!
\param mod_name_id ID of a Module name in database.
\return Name of the Module.
*/
string queryModuleName( string mod_name_id )
{
string module_name; // Hold name of module.
try{
connectDatabase();
// Select name of the module with provided ID.
commonResultSet = stmt->executeQuery("SELECT modName FROM module_names WHERE modNameID='"
+ mod_name_id +"'");
if( commonResultSet->next() ){
module_name = commonResultSet->getString("modName");
}
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
}
disconnectDatabase();
return module_name;
}
//! Record the given OS in database.
/*!
Updates database tables `OSes' and `os_releases' if they don't
have the corresponding entries for current OS, and lastly writes an entry
into `kernels' table for the new OS.
\param osInfo Object holding information of the OS.
\return The Unique Kernel ID of added OS.
*/
string recordOsInfo (OsInfo osInfo)
{
string osId_="", releaseId_="", uKernelId_="";
try{
connectDatabase();
/*
* Check OS Name entry in DB
* @{
*/
commonResultSet = stmt->executeQuery("SELECT osID FROM OSes WHERE osName='"+ osInfo.getDistro() +"'");
if( commonResultSet->first() ){ // We alread have a distribution with that name.
osId_ = commonResultSet->getString("osID"); // Return the osID from `OSes` table.
}
else{ // Distribution with given OS name doesn't exist.
// Insert Distribution into OSes.
stmt->execute("INSERT INTO OSes(osName) VALUES ('"+ osInfo.getDistro() +"')");
commonResultSet = stmt->executeQuery("SELECT LAST_INSERT_ID()");
if( commonResultSet->next() ){
osId_ = commonResultSet->getString(1); // Return the currently inserted osID from `OSes` table.
}
}
assert( osId_ != "" );
/*
* @}
*/
/*
* Check Release entry in DB
* @{
*/
commonResultSet = stmt->executeQuery("SELECT releaseID FROM os_releases WHERE osID_FK="+ osId_ +
" AND releaseName='"+ osInfo.getRelease() +"'");
if( commonResultSet->first() ){ // We alread have a distribution with given OS and Release names.
releaseId_ = commonResultSet->getString("releaseID"); // Return the releaseID from `os_releases` table.
}
else{ // Distribution with given Release name doesn't exist.
// Insert Dist.Release into os_releases.
stmt->execute("INSERT INTO os_releases(osID_FK,releaseName) VALUES("+ osId_ +",'"+ osInfo.getRelease() +"')");
commonResultSet = stmt->executeQuery("SELECT LAST_INSERT_ID()");
if( commonResultSet->next() ){
// Return the currently inserted releaseID from `os_releases` table.
releaseId_ = commonResultSet->getString(1);
}
}
assert( releaseId_ != "" );
/*
* @}
*/
/*
* Insert uKernel entry in DB
* @{
*/
// Insert Kernel Version into kernels and retrieve uKernelID
stmt->execute("INSERT INTO kernels( releaseID_FK, kernelVersion, machineHardware ) VALUES ("
+ releaseId_ +",'"+ osInfo.getKernel() +"','"+ osInfo.getArch() +"')");
commonResultSet = stmt->executeQuery("SELECT LAST_INSERT_ID()");
if( commonResultSet->next() ){
uKernelId_ = commonResultSet->getString(1);
}
/*
* @}
*/
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
}
disconnectDatabase();
assert( uKernelId_ != "" );
return uKernelId_;
}
//! Check if Module with given name exists.
/*!
Query the database `module_names' table with given name, and
insert it if it doesn't exist. Returns modNameID from `module_names' table.
\param module_name Name of the Kernel module(driver).
\return The ID representing the given module name.
*/
string checkModuleNameId( string module_name )
{
string mod_name_id;
try{
connectDatabase();
commonResultSet = stmt->executeQuery("SELECT modNameID FROM module_names WHERE modName='"
+ module_name +"'");
if( commonResultSet->first() ){ // If the given module name is already in database.
mod_name_id = commonResultSet->getString("modNameID"); // Get it's ID.
}
else { // If module name is not found, insert it..
stmt->execute("INSERT INTO module_names(modName) VALUES ('"+ module_name +"')");
commonResultSet = stmt->executeQuery("SELECT LAST_INSERT_ID()");
if( commonResultSet->next() ){
mod_name_id = commonResultSet->getString(1); // And get it's ID.
}
}
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
}
disconnectDatabase();
assert( mod_name_id != "" );
return mod_name_id;
}
//! Check if the Kernel has the provided Module in database.
/*!
Query the database `modules' table with "Unique Kernel ID" - "Module Name ID" pair,
and insert if it doesn't exist.
\param uKernelId Unique Kernel ID of an OS.
\param modNameId ID representing a module name.
\return The unique ID representing the given Module of the given OS.
*/
string checkKernelModule (string uKernelId, string modNameId)
{
string uniqueModuleId_="";
try{
connectDatabase();
commonResultSet = stmt->executeQuery("SELECT uModID FROM modules WHERE uKernelID_FK="+ uKernelId +
" AND modNameID_FK="+ modNameId);
if( commonResultSet->first() ){ // We found the Module for the OS.
uniqueModuleId_ = commonResultSet->getString("uModID"); // So get it's ID.
}
else { // Module is not found for the OS, so insert a new record.
stmt->execute("INSERT INTO modules(uKernelID_FK, modNameID_FK) VALUES ("+ uKernelId +","+ modNameId +")");
commonResultSet = stmt->executeQuery("SELECT LAST_INSERT_ID()");
if( commonResultSet->next() ){
uniqueModuleId_ = commonResultSet->getString(1); // Get it's ID, which is the last inserted one.
}
}
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
}
disconnectDatabase();
assert( uniqueModuleId_ != "" );
return uniqueModuleId_;
}
//! Update pcimap-list entry of a given Module of OS.
/*!
First check if the entry already exists in database,
if not add an entry with Unique Module ID(uModID from `modules' table) and
the given device.
\param currentPciDevice Object holding the device details.
\param uniqueModuleId Unique ID representing a Module of an OS.
\return Success status
- 0 -> (SUCCESS)
- 1 -> (FAIL)
*/
int insertPcimap ( const PciDevice* const currentPciDevice, std::string uniqueModuleId )
{
try{
connectDatabase();
// Select from the database `pcimap' table for pcimap-list entry of a specific Module of an OS.
commonResultSet = stmt->executeQuery("SELECT * FROM pcimap WHERE uModID_FK="+ uniqueModuleId +
" AND vendor='"+ currentPciDevice->getVendor() +"'"+
" AND device='"+ currentPciDevice->getDevice() +"'"+
" AND subvendor='"+ currentPciDevice->getSubvendor() +"'"+
" AND subdevice='"+ currentPciDevice->getSubdevice() +"'"+
" AND class='"+ currentPciDevice->getClass() + currentPciDevice->getSubclass() + currentPciDevice->getProgif() +"'"+
" AND classMask='"+ currentPciDevice->getClassMask() +"'");
if( !commonResultSet->first() ){ // The provided entry does not exist in pcimap-list so insert it.
std::string insertPciEntry =
"INSERT INTO pcimap VALUES ("+ uniqueModuleId +",'"+ currentPciDevice->getVendor() +"',"
+"'"+ currentPciDevice->getDevice() +"','"+ currentPciDevice->getSubvendor() +"',"
+"'"+ currentPciDevice->getSubdevice() +"',"
+"'"+ currentPciDevice->getClass() + currentPciDevice->getSubclass() + currentPciDevice->getProgif() +"',"
+"'"+ currentPciDevice->getClassMask() + "')";
stmt->execute(insertPciEntry);
// @TODO: check success of insertion with Statement -stmt- ; and put return
}
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
disconnectDatabase();
return 1;
}
disconnectDatabase();
return 0;
}
//! Query ID of the mainboard with provided name.
/*!
Retrieves ID of the mainboard, returns an empty string if it
does not exist in database.
\param board_name Name of a mainboard.
\return The ID representing the provided mainboard in database.
*/
string queryBoardModelId( string board_name )
{
string board_id = ""; // Hold the ID of the mainboard.
try{
connectDatabase();
// Select ID of the mainboard with given name.
commonResultSet = stmt->executeQuery("SELECT boardID FROM board_models WHERE boardName='"
+ board_name +"'");
if( commonResultSet->next() ){
board_id = commonResultSet->getString("boardID");
}
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
}
disconnectDatabase();
return board_id;
}
//! Delete Mainboard with given ID.
/*!
Delete the mainboard from database together with the devices
belonging to it in `dev_board' table.
\param board_id ID of a mainboard.
\return Success status
- 0 -> (SUCCESS)
- 1 -> (FAIL)
*/
int deleteBoardModel( std::string board_id )
{
try{
connectDatabase();
// Delete board from `board_models' table, which will cascade into `dev_board' to delete it's devices.
stmt->execute("DELETE FROM board_models WHERE boardID="+ board_id);
commonResultSet = stmt->executeQuery("SELECT LAST_INSERT_ID()");
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
return 1;
}
disconnectDatabase();
return 0;
}
//! Store the provided mainboard.
/*!
Stores the mainboard with given name in database and
return it's ID.
\param board_name Name of the mainboard to be saved.
\return The ID of currently stored mainboard.
*/
string insertBoardModel( string board_name )
{
string board_id; // Hold the ID of the mainboard.
try{
connectDatabase();
// Insert the given mainboard in database.
stmt->execute("INSERT INTO board_models(boardName) VALUES ('"+ board_name +"')");
// Retrieve the ID given at last insertion which is the currently inserted mainboard ID.
commonResultSet = stmt->executeQuery("SELECT LAST_INSERT_ID()");
if( commonResultSet->next() ){
board_id = commonResultSet->getString(1);
}
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
}
disconnectDatabase();
return board_id;
}
//! Store devices of given mainboard.
/*!
Insert the provided devices belonging to the given mainboard
to database.
\param board_id ID of the mainboard the devices intented to be added to.
\param device_id_list Unique ID list of devices to be added.
*/
void insertBoardDevices( string board_id, std::vector<string>& device_id_list )
{
try{
connectDatabase();
// Iterate through the list of devices and insert them in `dev_board' database table with given mainboard.
std::vector<string>::iterator device_iter;
for( device_iter = device_id_list.begin(); device_iter != device_id_list.end(); ++device_iter ){
stmt->execute("INSERT INTO dev_board VALUES ("+ board_id +","+ *device_iter +")");
commonResultSet = stmt->executeQuery("SELECT LAST_INSERT_ID()");
}
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
}
disconnectDatabase();
}
//! Query PCI IDs Vendor name for provided code.
/*!
Retrieve vendor name corresponding to provided code.
\param vendor_code Vendor Code of a device.
\return Vendor name that correspond to provided code.
*/
string queryPciIdsVendorName( string vendor_code )
{
string vendor_name=""; // Hold Vendor name.
try{
connectDatabase();
// Make a call to the stored procedure which will return an entry of corresponding name to the code.
commonResultSet = stmt->executeQuery("SELECT vendorName FROM pci_vendors WHERE vendorCode='"
+ vendor_code +"'");
if( commonResultSet->next() ){
vendor_name = commonResultSet->getString("vendorName");
}
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
}
disconnectDatabase();
return vendor_name;
}
//! Insert a PCI IDs Vendor entry.
/*!
Insert a PCI Vendor to database, does not check for anything.
\param vendor_code Vendor Code.
\param vendor_name Vendor Name.
\warning <b>THIS FUNCTION IS NOT USED IN THE APPLICATION!</b>
*/
void insertPciIdsVendor( string vendor_code, string vendor_name )
{
try{
connectDatabase();
stmt->execute("INSERT INTO pci_vendors VALUES ('"+ vendor_code +"','"+ vendor_name +"')");
commonResultSet = stmt->executeQuery("SELECT LAST_INSERT_ID()");
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
}
disconnectDatabase();
}
//! Query ID of a PCI IDs Device entry in database.
/*!
Retrieve ID of a PCI IDs Device entry(pci.ids device line) stored in database.
\param vendor_code Vendor Code of Device entry.
\param device_code Device Code.
\return ID representing the PCI IDs Device entry.
\warning <b>THIS FUNCTION IS NOT USED IN THE APPLICATION!</b>
*/
string queryPciIdsDeviceId( string vendor_code, string device_code )
{
string device_id="";
try{
connectDatabase();
commonResultSet = stmt->executeQuery("SELECT deviceID FROM pci_devices WHERE vendorCode_FK='"
+ vendor_code +"' AND deviceCode='"+ device_code +"'");
if( commonResultSet->next() ){
device_id = commonResultSet->getString("deviceID");
}
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
wApp->log("debug") << "# ERR: SQLException in " << __FILE__;
wApp->log("debug") << "(" << __FUNCTION__ << ") on line " << __LINE__;
/* Use what() (derived from std::runtime_error) to fetch the error message */
wApp->log("debug") << "# ERR: " << e.what();
wApp->log("debug") << " (MySQL error code: " << e.getErrorCode();
wApp->log("debug") << ", SQLState: " << e.getSQLState() << " )";
}
disconnectDatabase();
return device_id;
}
//! Insert a PCI IDs Device entry in database.
/*!
Add a PCI IDs Device entry(pci.ids device line) to database..
\param vendor_code Vendor Code of the Device entry.
\param device_code Device Code.
\param device_name Device Name.
\return ID representing the PCI IDs Device entry.
\warning <b>THIS FUNCTION IS NOT USED IN THE APPLICATION!</b>
*/
string insertPciIdsDevice( string vendor_code, string device_code, string device_name )
{
string device_id;