summaryrefslogtreecommitdiff
path: root/AudioManagerDaemon/DataBaseHandler.cpp
blob: 9da96f244c0bc0a2627f8188839bb1a53be3c451 (plain)
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
/**
 * Copyright (C) 2011, BMW AG
 *
 * AudioManangerDeamon
 *
 * \file DataBaseHandler.cpp
 *	if (query.exec(command) != true) {

	}
 * \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"
#include <cstdlib>
#include <fstream>
#include <stdio.h>
#include <cstring>
#include <sstream>
#include <sqlite3.h>

DataBaseHandler::DataBaseHandler() {

	//knock down database
	m_path = "/tmp";

	m_path.append("/");
	m_path.append(AUDIO_DATABASE);
	DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("Database path:"), DLT_STRING(m_path.c_str()));

	std::ifstream infile(m_path.c_str());

	if (infile) {
		remove(m_path.c_str());
		DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("Knocked down database"));
	}
	if (!this->open_database()) {
		DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("Problems with opening the database"));
	}
}

DataBaseHandler::~DataBaseHandler() {
	this->close_database();
}

bool DataBaseHandler::pQuery(std::string command) {
	sqlite3_stmt* query;
	if (sqlite3_exec(m_database,command.c_str(),NULL,&query,NULL)!= SQLITE_OK) {
		DLT_LOG( AudioManager, DLT_LOG_ERROR, DLT_STRING("SQL Query failed:"), DLT_STRING(command.c_str()));
		return false;
	}
	return true;
}

std::string DataBaseHandler::int2string(int i) {
	std::stringstream out;
	out << i;
	return out.str();
}

bool DataBaseHandler::open_database() {
	if (sqlite3_open_v2(m_path.c_str(), &m_database,SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL) == SQLITE_OK) {
		return true;
	} else {
		return false;
	}
}

void DataBaseHandler::close_database() {
	DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("Closed Database"));
	sqlite3_close(m_database);
}

bool DataBaseHandler::delete_data(std::string table) {
	std::string command = "DELETE FROM" + table;
	if (!this->pQuery(command)) return false;
	return true;
}

bool DataBaseHandler::create_tables() {
	std::string command;
	command="CREATE TABLE " + std::string(DOMAIN_TABLE) + " (ID INTEGER NOT NULL, DomainName VARCHAR(50), BusName VARCHAR(50), NodeName VARCHAR(50), EarlyMode BOOL, PRIMARY KEY(ID));";
	if (!this->pQuery(command)) return false;

	command = "CREATE TABLE " + std::string(SOURCE_CLASS_TABLE) + " (ID INTEGER NOT NULL, ClassName VARCHAR(50), VolumeOffset INTEGER, IsInterrupt BOOL, IsMixed BOOL, PRIMARY KEY(ID));";
	if (!this->pQuery(command)) return false;

	command = "CREATE TABLE " + std::string(SINK_CLASS_TABLE) + " (ID INTEGER NOT NULL, ClassName VARCHAR(50), PRIMARY KEY(ID));";
	if (!this->pQuery(command)) return false;

	command = "CREATE TABLE " + std::string(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 (!this->pQuery(command)) return false;

	command = "CREATE TABLE " + std::string(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 (!this->pQuery(command)) return false;

	command = "CREATE TABLE " + std::string(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 (!this->pQuery(command)) return false;

	command = "CREATE TABLE " + std::string(CONNECTION_TABLE) + " (ID INTEGER NOT NULL, Source_ID INTEGER, Sink_ID INTEGER, PRIMARY KEY(ID));";
	if (!this->pQuery(command)) return false;

	command = "CREATE TABLE " + std::string(INTERRUPT_TABLE) + " (ID INTEGER NOT NULL, Source_ID INTEGER, Sink_ID INTEGER, Connection_ID INTEGER, mixed BOOL, listInterruptedSources INTEGER, PRIMARY KEY(ID));";
	if (!this->pQuery(command)) return false;

	command = "CREATE TABLE " + std::string(MAIN_TABLE) + " (ID INTEGER NOT NULL, Source_ID INTEGER, Sink_ID INTEGER, route INTEGER, PRIMARY KEY(ID));";
	if (!this->pQuery(command)) return false;

	return true;
}

domain_t DataBaseHandler::insert_into_Domains_table(std::string DomainName, std::string BusName, std::string NodeName, bool EarlyMode) {
	sqlite3_stmt* query;
	std::string _EarlyMode = "false";
	if (EarlyMode) {
		_EarlyMode = "true";
	}

	std::string command = "SELECT BusName,ID FROM " + std::string(DOMAIN_TABLE) + " WHERE DomainName='" + DomainName + "'";

	if (SQexecute(command)) {
		if (sqlite3_step(query)==SQLITE_ROW) {
			std::string name((const char*) sqlite3_column_text(query,0));
			if (!name.empty()) {
				return sqlite3_column_int(query,1);
			} else {
				command = "UPDATE " + std::string(DOMAIN_TABLE) + "SET Busname=" + BusName + " Nodename=" + NodeName + " EarlyMode=" + _EarlyMode + " WHERE DomainName=" + DomainName;
			}
		} else {
			command = "INSERT INTO " + std::string(DOMAIN_TABLE) + " (DomainName, BusName, NodeName, EarlyMode) VALUES ('" + DomainName + "','" + BusName + "','" + NodeName + "','" + _EarlyMode + "')";
		}
	}

	sqlite3_finalize(query);

	if (!this->pQuery(command)) {
		return -1;
	} else {
		return get_Domain_ID_from_Name(DomainName);
	}
}

sourceClass_t DataBaseHandler::insert_into_Source_Class_table(std::string ClassName, volume_t VolumeOffset, bool IsInterrupt, bool IsMixed) {
	sqlite3_stmt* query;
	std::string _IsInterrupt = "false";
	std::string _IsMixed = "false";

	if (IsInterrupt) {
		_IsInterrupt = "true";
	}
	if (IsMixed) {
		_IsMixed = "true";
	}

	std::string command = "SELECT ID FROM " + std::string(SOURCE_CLASS_TABLE) + " WHERE ClassName='" + ClassName + "';";

	if (SQexecute(command)) {
		if (sqlite3_step(query)==SQLITE_ROW) {
			return sqlite3_column_int(query,0);
		}
	}

	command = "INSERT INTO " + std::string(SOURCE_CLASS_TABLE) + " (ClassName, VolumeOffset, IsInterrupt, IsMixed) VALUES ('" + ClassName + "'," + int2string(VolumeOffset) + ",'" + _IsInterrupt + "','" + _IsMixed + "')";

	if (!this->pQuery(command)) {
		return -1;
	} else {
		return sqlite3_last_insert_rowid(m_database);
	}
}

sink_t DataBaseHandler::insert_into_Sink_Class_table(std::string ClassName) {
	sqlite3_stmt* query;

	std::string command = "SELECT ID FROM " + std::string(SINK_CLASS_TABLE) + " WHERE ClassName='" + ClassName + "';";

	if (SQexecute(command)) {
		if (sqlite3_step(query)) {
			return sqlite3_column_int(query,0);
		}
	}

	command = "INSERT INTO " + std::string(SINK_CLASS_TABLE) + " (ClassName) VALUES ('" + ClassName + "')";

	if (!this->pQuery(command)) {
		return -1;
	} else {
		return sqlite3_last_insert_rowid(m_database);
	}
}

source_t DataBaseHandler::insert_into_Source_table(std::string Name, sourceClass_t Class_ID, domain_t Domain_ID, bool IsGateway) {
	sqlite3_stmt* query;
	std::string _IsGateway = "false";

	if (IsGateway) {
		_IsGateway = "true";
	}

	std::string command = "SELECT ID FROM " + std::string(SOURCE_TABLE) + " WHERE Name='" + Name + "';";

	if (SQexecute(command)) {
		if (sqlite3_step(query)==SQLITE_ROW) {
			return sqlite3_column_int(query,0);
		}
	}

	command = "INSERT INTO " + std::string(SOURCE_TABLE) + " (Name, Class_ID, Domain_ID, IsGateway) VALUES ('" + Name + "'," + int2string(Class_ID) + ",'" + int2string(Domain_ID) + "','" + _IsGateway + "')";

	if (this->pQuery(command)!=true) {
		return -1;
	} else {
		//emit signal_numberOfSourcesChanged();
		return sqlite3_last_insert_rowid(m_database);
	}
}

sink_t DataBaseHandler::insert_into_Sink_table(std::string Name, sinkClass_t Class_ID, domain_t Domain_ID, bool IsGateway) {
	sqlite3_stmt* query;
	std::string _IsGateway = "false";

	if (IsGateway) {
		_IsGateway = "true";
	}

	std::string command = "SELECT ID FROM " + std::string(SINK_TABLE) + " WHERE Name='" + Name + "';";

	if (SQexecute(command)) {
		if (sqlite3_step(query)==SQLITE_ROW) {
			return sqlite3_column_int(query,0);
		}
	}

	command = "INSERT INTO " + std::string(SINK_TABLE) + " (Name, Class_ID, Domain_ID, IsGateway) VALUES ('" + Name + "'," + int2string(Class_ID) + ",'" + int2string(Domain_ID) + "','" + _IsGateway + "')";

	if (!this->pQuery(command)) {
		return -1;
	} else {
		return sqlite3_last_insert_rowid(m_database);
	}
}

gateway_t DataBaseHandler::insert_into_Gatway_table(std::string Name, sink_t Sink_ID, source_t Source_ID, domain_t DomainSource_ID, domain_t DomainSink_ID, domain_t ControlDomain_ID) {
	sqlite3_stmt* query;
	std::string command = "SELECT ID FROM " + std::string(GATEWAY_TABLE) + " WHERE Name='" + Name + "';";

	if (SQexecute(command)) {
		if (sqlite3_step(query)==SQLITE_ROW) {
			return sqlite3_column_int(query,0);
		}
	}

	command = "INSERT INTO " + std::string(GATEWAY_TABLE) + " (Name, Sink_ID, Source_ID, DomainSource_ID, DomainSink_ID, ControlDomain_ID, IsBlocked) VALUES ('" + Name + "'," + int2string(Sink_ID) + "," + int2string(Source_ID) + "," + int2string(DomainSource_ID) + "," + int2string(DomainSink_ID) + "," + int2string(ControlDomain_ID) + ",'false')";

	if (!this->pQuery(command)) {
		return -1;
	} else {
		return sqlite3_last_insert_rowid(m_database);
	}
}

genInt_t DataBaseHandler::reserveInterrupt(sink_t Sink_ID, source_t Source_ID) {
	sqlite3_stmt* query;
	std::string command= "INSERT INTO " + std::string(INTERRUPT_TABLE) + "(Source_ID, Sink_ID) VALUES(:Source_ID, :Sink_ID)";
	sqlite3_prepare_v2(m_database,command.c_str(),NULL,&query,NULL);
	sqlite3_bind_int(query,0, Source_ID);
	sqlite3_bind_int(query,1, Sink_ID);

	if (!this->pQuery(command)) {
		return -1;
	} else {
		return sqlite3_last_insert_rowid(m_database);
	}
}

genError_t DataBaseHandler::updateInterrupt(const genInt_t intID, connection_t connID, bool mixed, std::list<source_t> listInterruptedSources) {
	sqlite3_stmt* query;
	std::string _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.
	std::list<source_t>* pointer = new std::list<source_t>;
	pointer=&listInterruptedSources;

	std::string command="UPDATE " + std::string(INTERRUPT_TABLE) + " SET Connection_ID=:Connection_ID, mixed=:mixed ,listInterruptedSources=:listInterruptedSources WHERE ID=:id";
	sqlite3_prepare_v2(m_database,command.c_str(),NULL,&query,NULL);
	sqlite3_bind_int(query,0,connID);
	sqlite3_bind_text(query,1,_mixed.c_str(),_mixed.size(),NULL);
	sqlite3_bind_int(query,2,int(pointer));
	sqlite3_bind_int(query,3,intID);

	if (!this->pQuery(command)) {
		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, std::list<source_t>** return_listInterruptedSources) {
	sqlite3_stmt* query;
	std::string command = "SELECT Connection_ID, Sink_ID, Source_ID, mixed, listInterruptedSources FROM " + std::string(INTERRUPT_TABLE) + " WHERE ID=" + int2string(intID) + ";";

	if (SQexecute(command)!=true) {
		return GEN_DATABASE_ERROR;
	} else {
		if (sqlite3_step(query)==SQLITE_ROW) {
			*return_connID = sqlite3_column_int(query,0);
			*return_Sink_ID = sqlite3_column_int(query,1);
			*return_Source_ID = sqlite3_column_int(query,2);
			*return_mixed = sqlite3_column_int(query,3);
			*return_listInterruptedSources = reinterpret_cast<std::list<source_t>*>(sqlite3_column_int(query,4));
			return GEN_OK;
		} else {
			return GEN_UNKNOWN;
		}
	}
}

genError_t DataBaseHandler::removeInterrupt(const genInt_t intID) {
	sqlite3_stmt* query;
	std::string command = "SELECT listInterruptedSources FROM " + std::string(INTERRUPT_TABLE) + " WHERE ID=" + int2string(intID) + ";";
	if (SQexecute(command) != true) {
		return GEN_DATABASE_ERROR;
	} else {
		if (sqlite3_step(query)==SQLITE_ROW) {
			delete reinterpret_cast<std::list<source_t>*>(sqlite3_column_int(query,0));
			command = "DELETE FROM " + std::string(INTERRUPT_TABLE) + " WHERE ID='" + int2string(intID) + "';";
			if (!this->pQuery(command)) {
				return GEN_DATABASE_ERROR;
			} else {
				return GEN_OK;
			}
		}
	}
	return GEN_UNKNOWN;
}

domain_t DataBaseHandler::peek_Domain_ID(std::string DomainName) {
	sqlite3_stmt* query;

	std::string command = "SELECT ID FROM " + std::string(DOMAIN_TABLE) + " WHERE DomainName='" + DomainName + "';";

	if (SQexecute(command)) {
		if (sqlite3_step(query)==SQLITE_ROW) {
			return sqlite3_column_int(query,0);
		} else {
			command = "INSERT INTO " + std::string(DOMAIN_TABLE) + " (DomainName) VALUES ('" + DomainName + "')";
		}
	}

	if (!this->pQuery(command)) {
		return -1;
	} else {
		return sqlite3_last_insert_rowid(m_database);
	}
}

domain_t DataBaseHandler::get_Domain_ID_from_Source_ID(source_t Source_ID) {
	sqlite3_stmt* query;
	std::string command = "SELECT Domain_ID FROM " + std::string(SOURCE_TABLE) + " WHERE ID=" + int2string(Source_ID) + ";";

	if (SQexecute(command)) {
		return -1;
	} else {
		sqlite3_step(query);
		return sqlite3_column_int(query,0);
	}
}

domain_t DataBaseHandler::get_Domain_ID_from_Sink_ID(sink_t Sink_ID) {
	sqlite3_stmt* query;
	std::string command = "SELECT Domain_ID FROM " + std::string(SINK_TABLE) + " WHERE ID=" + int2string(Sink_ID) + ";";

	if (SQexecute(command)) {
		return -1;
	} else {
		sqlite3_step(query);
		return sqlite3_column_int(query,0);
	}
}

source_t DataBaseHandler::get_Source_ID_from_Name(std::string name) {
	sqlite3_stmt* query;
	std::string command = "SELECT ID FROM " + std::string(SOURCE_TABLE) + " WHERE Name='" + name + "';";

	if (SQexecute(command)) {
		return -1;
	} else {
		sqlite3_step(query);
		return sqlite3_column_int(query,0);
	}
}

sourceClass_t DataBaseHandler::get_Source_Class_ID_from_Name(std::string name) {
	sqlite3_stmt* query;
	std::string command = "SELECT ID FROM " + std::string(SOURCE_CLASS_TABLE) + " WHERE ClassName='" + name + "';";
	int p=0;
	if (SQexecute(command)!=true) {
		return -1;
	} else {
		p=sqlite3_step(query);
		if (p==SQLITE_ROW) {
			return sqlite3_column_int(query,0);
		} else {
			p=p+1;
		}
	}
}

domain_t DataBaseHandler::get_Domain_ID_from_Name(std::string name) {
	sqlite3_stmt* query;
	std::string command = "SELECT ID FROM " + std::string(DOMAIN_TABLE) + " WHERE DomainName='" + name + "';";

	if (SQexecute(command)!=true) {
		return -1;
	} else {
		sqlite3_step(query);
		return sqlite3_column_int(query,0);
	}
}

gateway_t DataBaseHandler::get_Gateway_ID_with_Domain_ID(domain_t startDomain_ID, domain_t targetDomain_ID) {
	sqlite3_stmt* query;
	std::string command = "SELECT ID FROM " + std::string(GATEWAY_TABLE) + " WHERE DomainSource_ID=" + int2string(startDomain_ID) + " AND DomainSink_ID=" + int2string(targetDomain_ID) + ";";

	if (SQexecute(command)) {
		return -1;
	} else {
		sqlite3_step(query);
		return sqlite3_column_int(query,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) {
	sqlite3_stmt* query;
	std::string command = "SELECT Source_ID, Sink_ID, ControlDomain_ID FROM " + std::string(GATEWAY_TABLE) + " WHERE ID=" + int2string(Gateway_ID) + ";";

	if (SQexecute(command)) {
		return GEN_DATABASE_ERROR;
	} else {
		if (sqlite3_step(query)) {
			*return_Source_ID = sqlite3_column_int(query,0);
			*return_Sink_ID = sqlite3_column_int(query,1);
			*return_ControlDomain_ID = sqlite3_column_int(query,2);
			return GEN_OK;
		} else {
			return GEN_UNKNOWN;
		}
	}
}

void DataBaseHandler::get_Domain_ID_Tree(bool onlyfree, RoutingTree* Tree, std::list<RoutingTreeItem*>* allItems) {
	sqlite3_stmt* query;
	int RootID = Tree->returnRootDomainID();
	RoutingTreeItem *parent = Tree->returnRootItem();
	std::string _onlyfree = "false";
	unsigned int i = 0;

	if (onlyfree) {
		_onlyfree = "true";
	}

	std::string command="SELECT ID,DomainSource_ID FROM " + std::string(GATEWAY_TABLE) + " WHERE DomainSink_ID=:id AND IsBlocked=:flag;";

	sqlite3_prepare_v2(m_database,command.c_str(),NULL,&query,NULL);
	do {
		sqlite3_bind_int(query,0,RootID);
		sqlite3_bind_text(query,1,_onlyfree.c_str(),_onlyfree.size(),NULL);
		while (sqlite3_step(query)) {
			allItems->push_back(Tree->insertItem(sqlite3_column_int(query,1), sqlite3_column_int(query,0), parent));
		}
		std::list<RoutingTreeItem*>::iterator it=allItems->begin();
		std::advance(it,i);
		RootID = (*it)->returnDomainID();
		i++;
	} while (allItems->size() > i);
}

std::string DataBaseHandler::get_Bus_from_Domain_ID(domain_t Domain_ID) {
	sqlite3_stmt* query;
	std::string command = "SELECT BusName FROM " + std::string(DOMAIN_TABLE) + " WHERE ID=" + int2string(Domain_ID) + ";";

	if (SQexecute(command)) {
		return std::string("");
	} else {
		sqlite3_step(query);
		return std::string((const char*)sqlite3_column_text(query,0));
	}
}

domain_t DataBaseHandler::get_Domain_ID_from_Connection_ID(connection_t ID) {
	sqlite3_stmt* query;
	std::string command = "SELECT Source_ID FROM " + std::string(CONNECTION_TABLE) + " WHERE ID=" + int2string(ID) + ";";

	if (SQexecute(command)) {
		return -1;
	}

	sqlite3_step(query);
	int SourceID = sqlite3_column_int(query,0);
	command = "SELECT Domain_ID FROM " + std::string(SOURCE_TABLE) + " WHERE ID=" + int2string(SourceID) + ";";

	if (SQexecute(command)) {
		return -1;
	} else {
		sqlite3_step(query);
		return sqlite3_column_int(query,0);
	}
}

void DataBaseHandler::getListofSources(std::list<SourceType>* SourceList) {
	sqlite3_stmt* query;
	SourceType sType;
	std::string command = "SELECT ID,NAME FROM " + std::string(SOURCE_TABLE) + " WHERE isGateway='false';";
	if (SQexecute(command)) {
		while (sqlite3_step(query)) {
			sType.ID = sqlite3_column_int(query,0);
			sType.name = std::string((const char*)sqlite3_column_text(query,1));
			SourceList->push_back(sType);
		}
	}
}

void DataBaseHandler::getListofSinks(std::list<SinkType>* SinkList) {
	sqlite3_stmt* query;
	SinkType sType;
	std::string command = "SELECT ID,NAME FROM " + std::string(SINK_TABLE) + ";";
	if (SQexecute(command)) {
		while (sqlite3_step(query)) {
			sType.ID = sqlite3_column_int(query,0);
			sType.name =  std::string((const char*)sqlite3_column_text(query,1));
			SinkList->push_back(sType);
		}
	}
}

void DataBaseHandler::getListofConnections(std::list<ConnectionType>* ConnectionList) {
	sqlite3_stmt* query;
	ConnectionType cType;
	std::string command = "SELECT Source_ID,Sink_ID FROM " + std::string(CONNECTION_TABLE) + ";";
	if (SQexecute(command)) {
		while (sqlite3_step(query)) {
			cType.Source_ID = sqlite3_column_int(query,0);
			cType.Sink_ID =  sqlite3_column_int(query,1);
			ConnectionList->push_back(cType);
		}
	}
}

bool DataBaseHandler::is_source_Mixed(source_t source) {
	sqlite3_stmt* query;
	int classID = 0;

	std::string command = "SELECT Class_ID FROM " + std::string(SOURCE_TABLE) + " WHERE ID='" + int2string(source) + "';";
	if (SQexecute(command)) {
		if (sqlite3_step(query)) {
			classID = sqlite3_column_int(query,0);
		}
	}
	command = "SELECT isMixed FROM " + std::string(SOURCE_CLASS_TABLE) + " WHERE ID='" + int2string(classID) + "';";

	if (SQexecute(command)) {
		if (sqlite3_step(query)) {
			char* answer=(char*)sqlite3_column_text(query,0);
			if (strcmp(answer,"true") == 0) {
				return true;
			}
		}
	}
	return false;
}

sink_t DataBaseHandler::get_Sink_ID_from_Name(std::string name) {
	sqlite3_stmt* query;
	std::string command = "SELECT ID FROM " + std::string(SINK_TABLE) + " WHERE Name='" + name + "';";

	if (SQexecute(command)) {
		return -1;
	} else {
		sqlite3_step(query);
		return sqlite3_column_int(query,0);
	}
}

connection_t DataBaseHandler::getConnectionID(source_t SourceID, sink_t SinkID) {
	sqlite3_stmt* query;
	std::string command="SELECT ID FROM " + std::string(MAIN_TABLE) + " WHERE Source_ID=:sourceID AND Sink_ID=:sinkID";
	sqlite3_prepare_v2(m_database,command.c_str(),NULL,&query,NULL);
	sqlite3_bind_int(query,0,SourceID);
	sqlite3_bind_int(query,1,SinkID);

	if (SQexecute(command)) {
		return -1;
	} else {
		sqlite3_step(query);
		return sqlite3_column_int(query,0);
	}
}

connection_t DataBaseHandler::insertConnection(source_t SourceID, sink_t SinkID) {
	std::string command = "INSERT INTO " + std::string(CONNECTION_TABLE) + " (Source_ID, Sink_ID) VALUES (" + int2string(SourceID) + "," + int2string(SinkID) + ");";
	if (!this->pQuery(command)) {
		return -1;
	} else {
		return sqlite3_last_insert_rowid(m_database);
	}
}

genError_t DataBaseHandler::removeConnection(connection_t ConnectionID) {
	std::string command = "DELETE FROM " + std::string(CONNECTION_TABLE) + " WHERE ID='" + int2string(ConnectionID) + "';";
	if (!this->pQuery(command)) {
		return GEN_DATABASE_ERROR;
	} else {
		return GEN_OK;
	}
}

connection_t DataBaseHandler::reserveMainConnection(source_t source, sink_t sink) {
	sqlite3_stmt* query;
	std::string command = "INSERT INTO " + std::string(MAIN_TABLE) + "(Source_ID, Sink_ID) VALUES(:Source_ID, :Sink_ID)";
	sqlite3_prepare_v2(m_database,command.c_str(),NULL,&query,NULL);
	sqlite3_bind_int(query,0,source);
	sqlite3_bind_int(query,1,sink);
	if (!this->pQuery(command)) {
		return -1;
	} else {
		return sqlite3_last_insert_rowid(m_database);
	}
}

genError_t DataBaseHandler::updateMainConnection(connection_t connID, genRoute_t route) {
	sqlite3_stmt* 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);

	std::string command = "UPDATE " + std::string(MAIN_TABLE) + " SET route=:route WHERE ID=:connID";
	sqlite3_prepare_v2(m_database,command.c_str(),NULL,&query,NULL);
	sqlite3_bind_int(query,0,connID);
	sqlite3_bind_int(query,1,int(routeheap));

	if (!this->pQuery(command)) {
		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) {
	sqlite3_stmt* query;
	std::string command = "SELECT Sink_ID, Source_ID, route FROM " + std::string(MAIN_TABLE) + " WHERE ID=" + int2string(connID) + ";";

	if (SQexecute(command)) {
		return GEN_DATABASE_ERROR;
	} else {
		if (sqlite3_step(query)) {
			*return_sinkID = sqlite3_column_int(query,0);
			*return_sourceID = sqlite3_column_int(query,1);
			*return_route = reinterpret_cast<genRoute_t*>(sqlite3_column_int(query,2));
			return GEN_OK;
		} else {
			return GEN_UNKNOWN;
		}
	}
}

connection_t DataBaseHandler::returnMainconnectionIDforSinkSourceID(sink_t sink, source_t source) {
	sqlite3_stmt* query;
	std::string command="SELECT ID FROM " + std::string(MAIN_TABLE) + " WHERE Sink_ID=:sinkID AND Source_ID=:SourceID";
	sqlite3_prepare_v2(m_database,command.c_str(),NULL,&query,NULL);
	sqlite3_bind_int(query,0,sink);
	sqlite3_bind_int(query,1,source);

	if (SQexecute(command)) {
		return -1;
	} else {
		sqlite3_step(query);
		return sqlite3_column_int(query,0);
	}
}

std::list<source_t> DataBaseHandler::getSourceIDsForSinkID(sink_t sink) {
	std::list<source_t> list;
	sqlite3_stmt* query;

	std::string command="SELECT Source_ID FROM " + std::string(MAIN_TABLE) + " WHERE Sink_ID=" + int2string(sink);

	if (SQexecute(command)) {
		while (sqlite3_step(query)) {
			list.push_back(sqlite3_column_int(query,0));
		}
	}
	return list;
}

std::list<ConnectionType> DataBaseHandler::getListAllMainConnections() {
	std::list<ConnectionType> connectionList;
	sqlite3_stmt* query;
	std::string command = "SELECT Sink_ID, Source_ID FROM " + std::string(MAIN_TABLE) + ";";

	if (SQexecute(command)) {
		while (sqlite3_step(query)) {
			ConnectionType temp;
			temp.Sink_ID = sqlite3_column_int(query,0);
			temp.Source_ID = sqlite3_column_int(query,1);
			connectionList.push_back(temp);
			DLT_LOG( AudioManager, DLT_LOG_INFO, DLT_STRING("Added Connection"), DLT_INT(temp.Sink_ID), DLT_INT(temp.Source_ID));
		}
	}
	return connectionList;
}

genError_t DataBaseHandler::removeMainConnection(connection_t connID) {
	sqlite3_stmt* query;
	std::string command = "SELECT route FROM " + std::string(MAIN_TABLE) + " WHERE ID=" + int2string(connID) + ";";
	if (SQexecute(command)) {
		if (sqlite3_step(query)) {
			delete reinterpret_cast<genRoute_t*>(sqlite3_column_int(query,0));
			command = "DELETE FROM " + std::string(MAIN_TABLE) + " WHERE ID='" + int2string(connID) + "';";
			if (SQexecute(command)!=true) {
				return GEN_DATABASE_ERROR;
			} else {
				return GEN_OK;
			}
		}
	}
	return GEN_UNKNOWN;
}