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
|
/**
* Copyright (C) 2011, BMW AG
*
* AudioManangerDeamon
*
* \file DataBaseHandler.cpp
*
* \date 20.05.2011
* \author Christian Müller (christian.ei.mueller@bmw.de)
*
* \section License
* GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
* Copyright (C) 2011, BMW AG – Christian Müller Christian.ei.mueller@bmw.de
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
* 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 Lesser General Public License, version 2.1, for more details.
* You should have received a copy of the GNU Lesser General Public License, version 2.1, along with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
* Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may also be applicable to programs even in cases in which the program is not a library in the technical sense.
* Linking AudioManager statically or dynamically with other modules is making a combined work based on AudioManager. You may license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to license your linked modules under the GNU Lesser General Public License, version 2.1, you may use the program under the following exception.
* As a special exception, the copyright holders of AudioManager give you permission to combine AudioManager with software programs or libraries that are released under any license unless such a combination is not permitted by the license of such a software program or library. You may copy and distribute such a system following the terms of the GNU Lesser General Public License, version 2.1, including this special exception, for AudioManager and the licenses of the other code concerned.
* Note that people who make modified versions of AudioManager are not obligated to grant this special exception for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, version 2.1, gives permission to release a modified version without this exception; this exception also makes it possible to release a modified version which carries forward this exception.
*
*
*/
#include "DataBaseHandler.h"
DataBaseHandler::DataBaseHandler() {
//knock down database
QString path = (QDir::home().path());
path.append(QDir::separator()).append(AUDIO_DATABASE);
path = QDir::toNativeSeparators(path);
QFile db_file(path);
if (db_file.exists()) {
db_file.remove();
}
if (!this->open_database()) {
DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("Problems with opening the database"));
}
}
DataBaseHandler::~DataBaseHandler() {
this->close_database();
}
bool DataBaseHandler::open_database() {
m_database = QSqlDatabase::addDatabase("QSQLITE");
QString path = (QDir::home().path());
path.append(QDir::separator()).append(AUDIO_DATABASE);
path = QDir::toNativeSeparators(path);
m_database.setDatabaseName(path);
return m_database.open();
}
void DataBaseHandler::close_database() {
DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("Closed Database"));
m_database.close();
}
bool DataBaseHandler::delete_data(QString table) {
QSqlQuery query;
QString comand = "DELETE FROM " + table;
return query.exec(comand);
}
bool DataBaseHandler::create_tables() {
QSqlQuery query;
QString
command =
"CREATE TABLE " + QString(DOMAIN_TABLE)
+ " (ID INTEGER NOT NULL, DomainName VARCHAR(50), BusName VARCHAR(50), NodeName VARCHAR(50), EarlyMode BOOL, PRIMARY KEY(ID));";
if (query.exec(command) != true) {
DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(DOMAIN_TABLE));
return false;
}
command
= "CREATE TABLE " + QString(SOURCE_CLASS_TABLE)
+ " (ID INTEGER NOT NULL, ClassName VARCHAR(50), VolumeOffset INTEGER, IsInterrupt BOOL, IsMixed BOOL, PRIMARY KEY(ID));";
if (query.exec(command) != true) {
DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(SOURCE_CLASS_TABLE));
return false;
}
command = "CREATE TABLE " + QString(SINK_CLASS_TABLE)
+ " (ID INTEGER NOT NULL, ClassName VARCHAR(50), PRIMARY KEY(ID));";
if (query.exec(command) != true) {
DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(SINK_CLASS_TABLE));
return false;
}
command
= "CREATE TABLE " + QString(SOURCE_TABLE)
+ " (ID INTEGER NOT NULL, Name VARCHAR(50), Class_ID INTEGER, Domain_ID INTEGER, IsGateway BOOL, PRIMARY KEY(ID), FOREIGN KEY (Domain_ID) REFERENCES "
+ DOMAIN_TABLE + "(ID), FOREIGN KEY (Class_ID) REFERENCES "
+ SOURCE_CLASS_TABLE + "(ID));";
if (query.exec(command) != true) {
DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(SOURCE_TABLE));
return false;
}
command
= "CREATE TABLE " + QString(SINK_TABLE)
+ " (ID INTEGER NOT NULL, Name VARCHAR(50), Class_ID INTEGER, Domain_ID INTEGER, IsGateway BOOL, PRIMARY KEY(ID), FOREIGN KEY (DOMAIN_ID) REFERENCES "
+ DOMAIN_TABLE + "(ID), FOREIGN KEY (Class_ID) REFERENCES "
+ SOURCE_CLASS_TABLE + "(ID));";
if (query.exec(command) != true) {
DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(SOURCE_CLASS_TABLE));
return false;
}
command
= "CREATE TABLE " + QString(GATEWAY_TABLE)
+ " (ID INTEGER NOT NULL, Name VARCHAR(50), Sink_ID INTEGER, Source_ID INTEGER, DomainSource_ID INTEGER, DomainSink_ID INTEGER, ControlDomain_ID Integer, IsBlocked BOOL, PRIMARY KEY(ID), FOREIGN KEY (Sink_ID) REFERENCES "
+ SINK_TABLE + "(ID), FOREIGN KEY (Source_ID) REFERENCES "
+ SOURCE_TABLE
+ "(ID),FOREIGN KEY (DomainSource_ID) REFERENCES "
+ DOMAIN_TABLE
+ "(ID),FOREIGN KEY (DomainSink_ID) REFERENCES "
+ DOMAIN_TABLE + "(ID));";
if (query.exec(command) != true) {
DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(GATEWAY_TABLE));
return false;
}
command
= "CREATE TABLE " + QString(CONNECTION_TABLE)
+ " (ID INTEGER NOT NULL, Source_ID INTEGER, Sink_ID INTEGER, PRIMARY KEY(ID));";
if (query.exec(command) != true) {
DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(CONNECTION_TABLE));
return false;
}
command
= "CREATE TABLE " + QString(INTERRUPT_TABLE)
+ " (ID INTEGER NOT NULL, Source_ID INTEGER, Sink_ID INTEGER, Connection_ID INTEGER, mixed BOOL, listInterrruptedSources INTEGER, PRIMARY KEY(ID));";
if (query.exec(command) != true) {
DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(INTERRUPT_TABLE));
return false;
}
command
= "CREATE TABLE " + QString(MAIN_TABLE)
+ " (ID INTEGER NOT NULL, Source_ID INTEGER, Sink_ID INTEGER, route INTEGER, PRIMARY KEY(ID));";
if (query.exec(command) != true) {
DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("Databasehandler: Could not create table"), DLT_STRING(MAIN_TABLE));
return false;
}
return true;
}
domain_t DataBaseHandler::insert_into_Domains_table(QString DomainName,
QString BusName, QString NodeName, bool EarlyMode) {
QSqlQuery query;
QString _EarlyMode = "false";
if (EarlyMode) {
_EarlyMode = "true";
}
QString command = "SELECT BusName,ID FROM " + QString(DOMAIN_TABLE)
+ " WHERE DomainName=\"" + DomainName + "\";";
if (query.exec(command) == true) {
if (query.next()) {
if (!query.value(0).toString().isEmpty()) {
return query.value(1).toInt();
} else {
command = "UPDATE " + QString(DOMAIN_TABLE) + "SET Busname="
+ BusName + " Nodename=" + NodeName + " EarlyMode="
+ _EarlyMode + " WHERE DomainName=" + DomainName;
}
} else {
command = "INSERT INTO " + QString(DOMAIN_TABLE)
+ " (DomainName, BusName, NodeName, EarlyMode) VALUES (\""
+ DomainName + "\",\"" + BusName + "\",\"" + NodeName
+ "\",\"" + _EarlyMode + "\")";
}
}
if (query.exec(command) != true) {
return -1;
} else {
return get_Domain_ID_from_Name(DomainName);
}
}
sourceClass_t DataBaseHandler::insert_into_Source_Class_table(
QString ClassName, volume_t VolumeOffset, bool IsInterrupt,
bool IsMixed) {
QSqlQuery query;
QString _IsInterrupt = "false";
QString _IsMixed = "false";
if (IsInterrupt) {
_IsInterrupt = "true";
}
if (IsMixed) {
_IsMixed = "true";
}
QString command = "SELECT ID FROM " + QString(SOURCE_CLASS_TABLE)
+ " WHERE ClassName=\"" + ClassName + "\";";
if (query.exec(command) == true) {
if (query.next()) {
return query.value(0).toInt();
}
}
command = "INSERT INTO " + QString(SOURCE_CLASS_TABLE)
+ " (ClassName, VolumeOffset, IsInterrupt, IsMixed) VALUES (\""
+ ClassName + "\"," + QString::number(VolumeOffset) + ",\""
+ _IsInterrupt + "\",\"" + _IsMixed + "\")";
if (query.exec(command) != true) {
return -1;
} else {
return query.lastInsertId().toInt();
}
}
sink_t DataBaseHandler::insert_into_Sink_Class_table(QString ClassName) {
QSqlQuery query;
QString command = "SELECT ID FROM " + QString(SINK_CLASS_TABLE)
+ " WHERE ClassName=\"" + ClassName + "\";";
if (query.exec(command) == true) {
if (query.next()) {
return query.value(0).toInt();
}
}
command = "INSERT INTO " + QString(SINK_CLASS_TABLE)
+ " (ClassName) VALUES (\"" + ClassName + "\")";
if (query.exec(command) != true) {
return -1;
} else {
return query.lastInsertId().toInt();
}
}
source_t DataBaseHandler::insert_into_Source_table(QString Name,
sourceClass_t Class_ID, domain_t Domain_ID, bool IsGateway) {
QSqlQuery query;
QString _IsGateway = "false";
if (IsGateway) {
_IsGateway = "true";
}
QString command = "SELECT ID FROM " + QString(SOURCE_TABLE)
+ " WHERE Name=\"" + Name + "\";";
if (query.exec(command) == true) {
if (query.next()) {
return query.value(0).toInt();
}
}
command = "INSERT INTO " + QString(SOURCE_TABLE)
+ " (Name, Class_ID, Domain_ID, IsGateway) VALUES (\"" + Name
+ "\"," + QString::number(Class_ID) + ",\"" + QString::number(
Domain_ID) + "\",\"" + _IsGateway + "\")";
if (query.exec(command) != true) {
return -1;
} else {
emit signal_numberOfSourcesChanged();
return query.lastInsertId().toInt();
}
}
sink_t DataBaseHandler::insert_into_Sink_table(QString Name,
sinkClass_t Class_ID, domain_t Domain_ID, bool IsGateway) {
QSqlQuery query;
QString _IsGateway = "false";
if (IsGateway) {
_IsGateway = "true";
}
QString command = "SELECT ID FROM " + QString(SINK_TABLE)
+ " WHERE Name=\"" + Name + "\";";
if (query.exec(command) == true) {
if (query.next()) {
return query.value(0).toInt();
}
}
command = "INSERT INTO " + QString(SINK_TABLE)
+ " (Name, Class_ID, Domain_ID, IsGateway) VALUES (\"" + Name
+ "\"," + QString::number(Class_ID) + ",\"" + QString::number(
Domain_ID) + "\",\"" + _IsGateway + "\")";
if (query.exec(command) != true) {
return -1;
} else {
emit signal_numberOfSinksChanged();
return query.lastInsertId().toInt();
}
}
gateway_t DataBaseHandler::insert_into_Gatway_table(QString Name,
sink_t Sink_ID, source_t Source_ID, domain_t DomainSource_ID,
domain_t DomainSink_ID, domain_t ControlDomain_ID) {
QSqlQuery query;
QString command = "SELECT ID FROM " + QString(GATEWAY_TABLE)
+ " WHERE Name=\"" + Name + "\";";
if (query.exec(command) == true) {
if (query.next()) {
return query.value(0).toInt();
}
}
command
= "INSERT INTO " + QString(GATEWAY_TABLE)
+ " (Name, Sink_ID, Source_ID, DomainSource_ID, DomainSink_ID, ControlDomain_ID, IsBlocked) VALUES (\""
+ Name + "\"," + QString::number(Sink_ID) + ","
+ QString::number(Source_ID) + "," + QString::number(
DomainSource_ID) + "," + QString::number(DomainSink_ID)
+ "," + QString::number(ControlDomain_ID) + ",\"false\")";
if (query.exec(command) != true) {
return -1;
} else {
return query.lastInsertId().toInt();
}
}
genInt_t DataBaseHandler::reserveInterrupt(sink_t Sink_ID, source_t Source_ID) {
QSqlQuery query;
query.prepare(
"INSERT INTO " + QString(INTERRUPT_TABLE) + "(Source_ID, Sink_ID)"
" VALUES(:Source_ID, :Sink_ID)");
query.bindValue(":Source_ID", Source_ID);
query.bindValue(":Sink_ID", Sink_ID);
if (query.exec() != true) {
return -1;
} else {
return query.lastInsertId().toInt();
}
}
genError_t DataBaseHandler::updateInterrupt(const genInt_t intID,
connection_t connID, bool mixed,
QList<source_t> listInterrruptedSources) {
QSqlQuery query;
QString _mixed = "false";
if (mixed) {
_mixed = "true";
}
//This information is not handy to be stored directly in the database. So we put it on the heap and store the pointer to it.
QList<source_t>* pointer = new QList<source_t> (listInterrruptedSources);
query.prepare(
"UPDATE " + QString(INTERRUPT_TABLE)
+ " SET Connection_ID=:Connection_ID, mixed=:mixed ,listInterrruptedSources=:listInterrruptedSources WHERE ID=:id");
query.bindValue(":Connection_ID", connID);
query.bindValue(":mixed", _mixed);
query.bindValue(":listInterrruptedSources", int(pointer));
query.bindValue(":id", intID);
if (query.exec() != true) {
return GEN_DATABASE_ERROR;
} else {
return GEN_OK;
}
}
genError_t DataBaseHandler::getInterruptDatafromID(const genInt_t intID,
connection_t* return_connID, sink_t* return_Sink_ID,
source_t* return_Source_ID, bool* return_mixed,
QList<source_t>** return_listInterrruptedSources) {
QSqlQuery query;
QString
command =
"SELECT Connection_ID, Sink_ID, Source_ID, mixed, listInterrruptedSources FROM "
+ QString(INTERRUPT_TABLE) + " WHERE ID="
+ QString::number(intID) + ";";
if (query.exec(command) != true) {
return GEN_DATABASE_ERROR;
} else {
if (query.next()) {
*return_connID = query.value(0).toInt();
*return_Sink_ID = query.value(1).toInt();
*return_Source_ID = query.value(2).toInt();
*return_mixed = query.value(3).toBool();
*return_listInterrruptedSources
= reinterpret_cast<QList<source_t>*> (query.value(4).toInt());
return GEN_OK;
} else {
return GEN_UNKNOWN;
}
}
}
genError_t DataBaseHandler::removeInterrupt(const genInt_t intID) {
QSqlQuery query;
QString command = "SELECT listInterrruptedSources FROM " + QString(
INTERRUPT_TABLE) + " WHERE ID=" + QString::number(intID) + ";";
if (query.exec(command) != true) {
return GEN_DATABASE_ERROR;
} else {
if (query.next()) {
delete reinterpret_cast<QList<source_t>*> (query.value(0).toInt());
command = "DELETE FROM " + QString(INTERRUPT_TABLE)
+ " WHERE ID=\"" + QString::number(intID) + "\";";
if (query.exec(command) != true) {
return GEN_DATABASE_ERROR;
} else {
return GEN_OK;
}
}
}
return GEN_UNKNOWN;
}
domain_t DataBaseHandler::peek_Domain_ID(QString DomainName) {
QSqlQuery query;
QString command = "SELECT ID FROM " + QString(DOMAIN_TABLE)
+ " WHERE DomainName=\"" + DomainName + "\";";
if (query.next()) {
return query.value(0).toInt();
} else {
command = "INSERT INTO " + QString(DOMAIN_TABLE)
+ " (DomainName) VALUES (\"" + DomainName + "\")";
}
if (query.exec(command) != true) {
return -1;
} else {
return query.lastInsertId().toInt();
}
}
domain_t DataBaseHandler::get_Domain_ID_from_Source_ID(source_t Source_ID) {
QSqlQuery query;
QString command = "SELECT Domain_ID FROM " + QString(SOURCE_TABLE)
+ " WHERE ID=" + QString::number(Source_ID) + ";";
if (query.exec(command) != true) {
return -1;
} else {
query.next();
return query.value(0).toInt();
}
}
domain_t DataBaseHandler::get_Domain_ID_from_Sink_ID(sink_t Sink_ID) {
QSqlQuery query;
QString command = "SELECT Domain_ID FROM " + QString(SINK_TABLE)
+ " WHERE ID=" + QString::number(Sink_ID) + ";";
if (query.exec(command) != true) {
return -1;
} else {
query.next();
return query.value(0).toInt();
}
}
source_t DataBaseHandler::get_Source_ID_from_Name(QString name) {
QSqlQuery query;
QString command = "SELECT ID FROM " + QString(SOURCE_TABLE)
+ " WHERE Name=\"" + name + "\";";
if (query.exec(command) != true) {
return -1;
} else {
if (query.next()) {
return query.value(0).toInt();
} else {
return 0;
}
}
}
sourceClass_t DataBaseHandler::get_Source_Class_ID_from_Name(QString name) {
QSqlQuery query;
QString command = "SELECT ID FROM " + QString(SOURCE_CLASS_TABLE)
+ " WHERE ClassName=\"" + name + "\";";
if (query.exec(command) != true) {
return -1;
} else {
if (query.next()) {
return query.value(0).toInt();
} else {
return 0;
}
}
}
domain_t DataBaseHandler::get_Domain_ID_from_Name(QString name) {
QSqlQuery query;
QString command = "SELECT ID FROM " + QString(DOMAIN_TABLE)
+ " WHERE DomainName=\"" + name + "\";";
if (query.exec(command) != true) {
return -1;
} else {
if (query.next()) {
return query.value(0).toInt();
} else {
return 0;
}
}
}
gateway_t DataBaseHandler::get_Gateway_ID_with_Domain_ID(
domain_t startDomain_ID, domain_t targetDomain_ID) {
QSqlQuery query;
QString command = "SELECT ID FROM " + QString(GATEWAY_TABLE)
+ " WHERE DomainSource_ID=" + QString::number(startDomain_ID)
+ " AND DomainSink_ID=" + QString::number(targetDomain_ID) + ";";
if (query.exec(command) != true) {
return -1;
} else {
if (query.next()) {
return query.value(0).toInt();
} else {
return 0;
}
}
}
genError_t DataBaseHandler::get_Gateway_Source_Sink_Domain_ID_from_ID(
gateway_t Gateway_ID, source_t* return_Source_ID,
sink_t* return_Sink_ID, domain_t* return_ControlDomain_ID) {
QSqlQuery query;
QString command = "SELECT Source_ID, Sink_ID, ControlDomain_ID FROM "
+ QString(GATEWAY_TABLE) + " WHERE ID=" + QString::number(
Gateway_ID) + ";";
if (query.exec(command) != true) {
return GEN_DATABASE_ERROR;
} else {
if (query.next()) {
*return_Source_ID = query.value(0).toInt();
*return_Sink_ID = query.value(1).toInt();
*return_ControlDomain_ID = query.value(2).toInt();
return GEN_OK;
} else {
return GEN_UNKNOWN;
}
}
}
void DataBaseHandler::get_Domain_ID_Tree(bool onlyfree, RoutingTree* Tree,
QList<RoutingTreeItem*>* allItems) {
QSqlQuery query;
int RootID = Tree->returnRootDomainID();
RoutingTreeItem *parent = Tree->returnRootItem();
QString _onlyfree = "false";
int i = 0;
if (onlyfree) {
_onlyfree = "true";
}
query.prepare(
"SELECT ID,DomainSource_ID FROM " + QString(GATEWAY_TABLE)
+ " WHERE DomainSink_ID=:id AND IsBlocked=:flag;");
do {
query.bindValue(":id", RootID);
query.bindValue(":flag", _onlyfree);
query.exec();
while (query.next()) {
allItems->append(
Tree->insertItem(query.value(1).toInt(),
query.value(0).toInt(), parent));
}
parent = allItems->value(i);
RootID = parent->returnDomainID();
i++;
} while (allItems->length() > i);
}
QString DataBaseHandler::get_Bus_from_Domain_ID(domain_t Domain_ID) {
QSqlQuery query;
QString command = "SELECT BusName FROM " + QString(DOMAIN_TABLE)
+ " WHERE ID=" + QString::number(Domain_ID) + ";";
if (query.exec(command) != true) {
return NULL;
} else {
query.next();
return query.value(0).toString();
}
}
domain_t DataBaseHandler::get_Domain_ID_from_Connection_ID(connection_t ID) {
QSqlQuery query;
QString command = "SELECT Source_ID FROM " + QString(CONNECTION_TABLE)
+ " WHERE ID=" + QString::number(ID) + ";";
if (query.exec(command) != true) {
return -1;
}
query.next();
int SourceID = query.value(0).toInt();
command = "SELECT Domain_ID FROM " + QString(SOURCE_TABLE) + " WHERE ID="
+ QString::number(SourceID) + ";";
if (query.exec(command) != true) {
return -1;
} else {
query.next();
return query.value(0).toInt();
}
}
void DataBaseHandler::getListofSources(QList<SourceType>* SourceList) {
QSqlQuery query;
SourceType sType;
QString command = "SELECT ID,NAME FROM " + QString(SOURCE_TABLE)
+ " WHERE isGateway=\"false\";";
if (query.exec(command) != true) {
} else {
while (query.next()) {
sType.ID = query.value(0).toInt();
sType.name = query.value(1).toString();
SourceList->append(sType);
}
}
}
void DataBaseHandler::getListofSinks(QList<SinkType>* SinkList) {
QSqlQuery query;
SinkType sType;
QString command = "SELECT ID,NAME FROM " + QString(SINK_TABLE) + ";";
if (query.exec(command) != true) {
} else {
while (query.next()) {
sType.ID = query.value(0).toInt();
sType.name = query.value(1).toString();
SinkList->append(sType);
}
}
}
void DataBaseHandler::getListofConnections(
QList<ConnectionType>* ConnectionList) {
QSqlQuery query;
ConnectionType sType;
QString command = "SELECT Source_ID,Sink_ID FROM " + QString(
CONNECTION_TABLE) + ";";
if (query.exec(command) != true) {
} else {
while (query.next()) {
sType.Source_ID = query.value(0).toInt();
sType.Sink_ID = query.value(1).toInt();
ConnectionList->append(sType);
}
}
}
bool DataBaseHandler::is_source_Mixed(source_t source) {
QSqlQuery query;
int classID = 0;
QString command = "SELECT Class_ID FROM " + QString(SOURCE_TABLE)
+ " WHERE ID=\"" + QString::number(source) + "\";";
if (query.exec(command) == true) {
if (query.next()) {
classID = query.value(0).toInt();
}
}
command = "SELECT isMixed FROM " + QString(SOURCE_CLASS_TABLE)
+ " WHERE ID=\"" + QString::number(classID) + "\";";
if (query.exec(command) == true) {
if (query.next()) {
if (query.value(0).toString().compare("true") == 0) {
return true;
}
}
}
return false;
}
sink_t DataBaseHandler::get_Sink_ID_from_Name(QString name) {
QSqlQuery query;
QString command = "SELECT ID FROM " + QString(SINK_TABLE)
+ " WHERE Name=\"" + name + "\";";
if (query.exec(command) != true) {
return -1;
} else {
if (query.next()) {
return query.value(0).toInt();
} else {
return 0;
}
}
}
connection_t DataBaseHandler::getConnectionID(source_t SourceID, sink_t SinkID) {
QSqlQuery query;
query.prepare(
"SELECT ID FROM " + QString(MAIN_TABLE)
+ " WHERE Source_ID=:sourceID AND Sink_ID=:sinkID");
query.bindValue(":sourceID", SourceID);
query.bindValue(":sinkID", SinkID);
if (query.exec() != true) {
return -1;
} else {
if (query.next()) {
return query.value(0).toInt();
} else {
return -1;
}
}
}
connection_t DataBaseHandler::insertConnection(source_t SourceID, sink_t SinkID) {
QSqlQuery query;
QString command = "INSERT INTO " + QString(CONNECTION_TABLE)
+ " (Source_ID, Sink_ID) VALUES (" + QString::number(SourceID)
+ "," + QString::number(SinkID) + ");";
if (query.exec(command) != true) {
return -1;
} else {
return query.lastInsertId().toInt();
}
}
genError_t DataBaseHandler::removeConnection(connection_t ConnectionID) {
QSqlQuery query;
QString command = "DELETE FROM " + QString(CONNECTION_TABLE)
+ " WHERE ID=\"" + QString::number(ConnectionID) + "\";";
if (query.exec(command) != true) {
return GEN_DATABASE_ERROR;
} else {
return GEN_OK;
}
}
connection_t DataBaseHandler::reserveMainConnection(source_t source,
sink_t sink) {
QSqlQuery query;
query.prepare("INSERT INTO " + QString(MAIN_TABLE) + "(Source_ID, Sink_ID)"
" VALUES(:Source_ID, :Sink_ID)");
query.bindValue(":Source_ID", source);
query.bindValue(":Sink_ID", sink);
if (query.exec() != true) {
return -1;
} else {
return query.lastInsertId().toInt();
}
}
genError_t DataBaseHandler::updateMainConnection(connection_t connID,
genRoute_t route) {
QSqlQuery query;
//This information is not handy to be stored directly in the database. So we put it on the heap and store the pointer to it.
genRoute_t* routeheap = new genRoute_t(route);
query.prepare(
"UPDATE " + QString(MAIN_TABLE)
+ " SET route=:route WHERE ID=:connID");
query.bindValue(":connID", connID);
query.bindValue(":route", int(routeheap));
if (query.exec() != true) {
return GEN_DATABASE_ERROR;
} else {
return GEN_OK;
}
}
genError_t DataBaseHandler::getMainConnectionDatafromID(
const connection_t connID, sink_t* return_sinkID,
source_t* return_sourceID, genRoute_t** return_route) {
QSqlQuery query;
QString command = "SELECT Sink_ID, Source_ID, route FROM " + QString(
MAIN_TABLE) + " WHERE ID=" + QString::number(connID) + ";";
if (query.exec(command) != true) {
return GEN_DATABASE_ERROR;
} else {
if (query.next()) {
*return_sinkID = query.value(0).toInt();
*return_sourceID = query.value(1).toInt();
*return_route
= reinterpret_cast<genRoute_t*> (query.value(2).toInt());
return GEN_OK;
} else {
return GEN_UNKNOWN;
}
}
}
connection_t DataBaseHandler::returnMainconnectionIDforSinkSourceID(
sink_t sink, source_t source) {
QSqlQuery query;
query.prepare(
"SELECT ID FROM " + QString(MAIN_TABLE)
+ " WHERE Sink_ID=:sinkID AND Source_ID=:SourceID");
query.bindValue(":SinkID", sink);
query.bindValue(":SourceID", source);
if (query.exec() != true) {
return -1;
} else {
if (query.next()) {
return query.value(0).toInt();
}
}
return -1;
}
QList<source_t> DataBaseHandler::getSourceIDsForSinkID(sink_t sink) {
QList<source_t> list;
QSqlQuery query;
query.prepare(
"SELECT Source_ID FROM " + QString(MAIN_TABLE)
+ " WHERE Sink_ID=:sinkID");
query.bindValue(":sinkID", sink);
if (query.exec() == true) {
DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("query good"));
while (query.next()) {
int p = query.value(0).toInt();
DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("SourceID"), DLT_INT(p));
list.append(query.value(0).toInt());
}
}
return list;
}
QList<ConnectionType> DataBaseHandler::getListAllMainConnections() {
QList<ConnectionType> connectionList;
QSqlQuery query;
QString command = "SELECT Sink_ID, Source_ID, route FROM " + QString(
MAIN_TABLE) + ";";
if (query.exec(command) != true) {
} else {
if (query.next()) {
ConnectionType temp;
temp.Sink_ID = query.value(0).toInt();
temp.Source_ID = query.value(1).toInt();
connectionList.append(temp);
}
}
return connectionList;
}
genError_t DataBaseHandler::removeMainConnection(connection_t connID) {
QSqlQuery query;
QString command = "SELECT route FROM " + QString(MAIN_TABLE) + " WHERE ID="
+ QString::number(connID) + ";";
if (query.exec(command) != true) {
return GEN_DATABASE_ERROR;
} else {
if (query.next()) {
delete reinterpret_cast<genRoute_t*> (query.value(0).toInt());
command = "DELETE FROM " + QString(MAIN_TABLE) + " WHERE ID=\""
+ QString::number(connID) + "\";";
if (query.exec(command) != true) {
return GEN_DATABASE_ERROR;
} else {
return GEN_OK;
}
}
}
return GEN_UNKNOWN;
}
|