- first attempt to remove QT
[profile/ivi/audiomanager.git] / AudioManagerDaemon / DataBaseHandler.cpp
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * AudioManangerDeamon
5  *
6  * \file DataBaseHandler.cpp
7  *      if (query.exec(command) != true) {
8
9         }
10  * \date 20.05.2011
11  * \author Christian Müller (christian.ei.mueller@bmw.de)
12  *
13  * \section License
14  * GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
15  * Copyright (C) 2011, BMW AG – Christian Müller  Christian.ei.mueller@bmw.de
16  *
17  * 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.
18  * 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.
19  * 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>.
20  * 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.
21  * 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.
22  * 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.
23  * 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.
24  *
25  *
26  */
27
28 #include "DataBaseHandler.h"
29 #include <cstdlib>
30 #include <fstream>
31 #include <stdio.h>
32 #include <cstring>
33 #include <sstream>
34 #include <sqlite3.h>
35
36 DataBaseHandler::DataBaseHandler() {
37
38         //knock down database
39         m_path = "/home/blacky";
40
41         m_path.append("/");
42         m_path.append(AUDIO_DATABASE);
43
44         std::ifstream infile(m_path.c_str());
45
46         if (infile) {
47                 remove(m_path.c_str());
48                 DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("Knocked down database"));
49         }
50         if (!this->open_database()) {
51                 DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("Problems with opening the database"));
52         }
53 }
54
55 DataBaseHandler::~DataBaseHandler() {
56         this->close_database();
57 }
58
59 bool DataBaseHandler::pQuery(std::string command) {
60         sqlite3_stmt* query;
61         if (sqlite3_exec(m_database,command.c_str(),NULL,&query,NULL)!= SQLITE_OK) {
62                 DLT_LOG( AudioManager, DLT_LOG_ERROR, DLT_STRING("SQL Query failed:"), DLT_STRING(command.c_str()));
63                 return false;
64         }
65         return true;
66 }
67
68 std::string DataBaseHandler::int2string(int i) {
69         std::stringstream out;
70         out << i;
71         return out.str();
72 }
73
74 bool DataBaseHandler::open_database() {
75         if (sqlite3_open_v2(m_path.c_str(), &m_database,SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL) == SQLITE_OK) {
76                 return true;
77         } else {
78                 return false;
79         }
80 }
81
82 void DataBaseHandler::close_database() {
83         DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("Closed Database"));
84         sqlite3_close(m_database);
85 }
86
87 bool DataBaseHandler::delete_data(std::string table) {
88         std::string command = "DELETE FROM" + table;
89         if (!this->pQuery(command)) return false;
90         return true;
91 }
92
93 bool DataBaseHandler::create_tables() {
94         std::string command;
95         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));";
96         if (!this->pQuery(command)) return false;
97
98         command = "CREATE TABLE " + std::string(SOURCE_CLASS_TABLE) + " (ID INTEGER NOT NULL, ClassName VARCHAR(50), VolumeOffset INTEGER, IsInterrupt BOOL, IsMixed BOOL, PRIMARY KEY(ID));";
99         if (!this->pQuery(command)) return false;
100
101         command = "CREATE TABLE " + std::string(SINK_CLASS_TABLE) + " (ID INTEGER NOT NULL, ClassName VARCHAR(50), PRIMARY KEY(ID));";
102         if (!this->pQuery(command)) return false;
103
104         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));";
105         if (!this->pQuery(command)) return false;
106
107         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));";
108         if (!this->pQuery(command)) return false;
109
110         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));";
111         if (!this->pQuery(command)) return false;
112
113         command = "CREATE TABLE " + std::string(CONNECTION_TABLE) + " (ID INTEGER NOT NULL, Source_ID INTEGER, Sink_ID INTEGER, PRIMARY KEY(ID));";
114         if (!this->pQuery(command)) return false;
115
116         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));";
117         if (!this->pQuery(command)) return false;
118
119         command = "CREATE TABLE " + std::string(MAIN_TABLE) + " (ID INTEGER NOT NULL, Source_ID INTEGER, Sink_ID INTEGER, route INTEGER, PRIMARY KEY(ID));";
120         if (!this->pQuery(command)) return false;
121
122         return true;
123 }
124
125 domain_t DataBaseHandler::insert_into_Domains_table(std::string DomainName, std::string BusName, std::string NodeName, bool EarlyMode) {
126         sqlite3_stmt* query;
127         std::string _EarlyMode = "false";
128         if (EarlyMode) {
129                 _EarlyMode = "true";
130         }
131
132         std::string command = "SELECT BusName,ID FROM " + std::string(DOMAIN_TABLE) + " WHERE DomainName='" + DomainName + "'";
133
134         if (sqlite3_exec(m_database,command.c_str(),NULL,&query,NULL)!= SQLITE_OK) {
135                 if (sqlite3_step(query)==SQLITE_ROW) {
136                         std::string name((const char*) sqlite3_column_text(query,0));
137                         if (!name.empty()) {
138                                 return sqlite3_column_int(query,1);
139                         } else {
140                                 command = "UPDATE " + std::string(DOMAIN_TABLE) + "SET Busname=" + BusName + " Nodename=" + NodeName + " EarlyMode=" + _EarlyMode + " WHERE DomainName=" + DomainName;
141                         }
142                 } else {
143                         command = "INSERT INTO " + std::string(DOMAIN_TABLE) + " (DomainName, BusName, NodeName, EarlyMode) VALUES ('" + DomainName + "','" + BusName + "'','" + NodeName + "','" + _EarlyMode + "')";
144                 }
145         }
146
147         sqlite3_finalize(query);
148
149         if (!this->pQuery(command)) {
150                 return -1;
151         } else {
152                 return get_Domain_ID_from_Name(DomainName);
153         }
154 }
155
156 sourceClass_t DataBaseHandler::insert_into_Source_Class_table(std::string ClassName, volume_t VolumeOffset, bool IsInterrupt, bool IsMixed) {
157         sqlite3_stmt* query;
158         std::string _IsInterrupt = "false";
159         std::string _IsMixed = "false";
160
161         if (IsInterrupt) {
162                 _IsInterrupt = "true";
163         }
164         if (IsMixed) {
165                 _IsMixed = "true";
166         }
167
168         std::string command = "SELECT ID FROM " + std::string(SOURCE_CLASS_TABLE) + " WHERE ClassName='" + ClassName + "';";
169
170         if (SQexecute(command)) {
171                 if (sqlite3_step(query)) {
172                         return sqlite3_column_int(query,0);
173                 }
174         }
175
176         command = "INSERT INTO " + std::string(SOURCE_CLASS_TABLE) + " (ClassName, VolumeOffset, IsInterrupt, IsMixed) VALUES ('" + ClassName + "'," + int2string(VolumeOffset) + ",'" + _IsInterrupt + "','" + _IsMixed + "')";
177
178         if (!this->pQuery(command)) {
179                 return -1;
180         } else {
181                 return sqlite3_last_insert_rowid(m_database);
182         }
183 }
184
185 sink_t DataBaseHandler::insert_into_Sink_Class_table(std::string ClassName) {
186         sqlite3_stmt* query;
187
188         std::string command = "SELECT ID FROM " + std::string(SINK_CLASS_TABLE) + " WHERE ClassName='" + ClassName + "';";
189
190         if (SQexecute(command)) {
191                 if (sqlite3_step(query)) {
192                         return sqlite3_column_int(query,0);
193                 }
194         }
195
196         command = "INSERT INTO " + std::string(SINK_CLASS_TABLE) + " (ClassName) VALUES ('" + ClassName + "')";
197
198         if (!this->pQuery(command)) {
199                 return -1;
200         } else {
201                 return sqlite3_last_insert_rowid(m_database);
202         }
203 }
204
205 source_t DataBaseHandler::insert_into_Source_table(std::string Name, sourceClass_t Class_ID, domain_t Domain_ID, bool IsGateway) {
206         sqlite3_stmt* query;
207         std::string _IsGateway = "false";
208
209         if (IsGateway) {
210                 _IsGateway = "true";
211         }
212
213         std::string command = "SELECT ID FROM " + std::string(SOURCE_TABLE) + " WHERE Name='" + Name + "';";
214
215         if (SQexecute(command)) {
216                 if (sqlite3_step(query)) {
217                         return sqlite3_column_int(query,0);
218                 }
219         }
220
221         command = "INSERT INTO " + std::string(SOURCE_TABLE) + " (Name, Class_ID, Domain_ID, IsGateway) VALUES ('" + Name + "'," + int2string(Class_ID) + ",'" + int2string(Domain_ID) + "','" + _IsGateway + "')";
222
223         if (sqlite3_step(query)) {
224                 return -1;
225         } else {
226                 //emit signal_numberOfSourcesChanged();
227                 return sqlite3_last_insert_rowid(m_database);
228         }
229 }
230
231 sink_t DataBaseHandler::insert_into_Sink_table(std::string Name, sinkClass_t Class_ID, domain_t Domain_ID, bool IsGateway) {
232         sqlite3_stmt* query;
233         std::string _IsGateway = "false";
234
235         if (IsGateway) {
236                 _IsGateway = "true";
237         }
238
239         std::string command = "SELECT ID FROM " + std::string(SINK_TABLE) + " WHERE Name='" + Name + "';";
240
241         if (SQexecute(command)) {
242                 if (sqlite3_step(query)) {
243                         return sqlite3_column_int(query,0);
244                 }
245         }
246
247         command = "INSERT INTO " + std::string(SINK_TABLE) + " (Name, Class_ID, Domain_ID, IsGateway) VALUES ('" + Name + "'," + int2string(Class_ID) + ",'" + int2string(Domain_ID) + "','" + _IsGateway + "')";
248
249         if (!this->pQuery(command)) {
250                 return -1;
251         } else {
252                 return sqlite3_last_insert_rowid(m_database);
253         }
254 }
255
256 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) {
257         sqlite3_stmt* query;
258         std::string command = "SELECT ID FROM " + std::string(GATEWAY_TABLE) + " WHERE Name='" + Name + "';";
259
260         if (SQexecute(command)) {
261                 if (sqlite3_step(query)) {
262                         return sqlite3_column_int(query,0);
263                 }
264         }
265
266         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')";
267
268         if (!this->pQuery(command)) {
269                 return -1;
270         } else {
271                 return sqlite3_last_insert_rowid(m_database);
272         }
273 }
274
275 genInt_t DataBaseHandler::reserveInterrupt(sink_t Sink_ID, source_t Source_ID) {
276         sqlite3_stmt* query;
277         std::string command= "INSERT INTO " + std::string(INTERRUPT_TABLE) + "(Source_ID, Sink_ID) VALUES(:Source_ID, :Sink_ID)";
278         sqlite3_prepare_v2(m_database,command.c_str(),NULL,&query,NULL);
279         sqlite3_bind_int(query,0, Source_ID);
280         sqlite3_bind_int(query,1, Sink_ID);
281
282         if (!this->pQuery(command)) {
283                 return -1;
284         } else {
285                 return sqlite3_last_insert_rowid(m_database);
286         }
287 }
288
289 genError_t DataBaseHandler::updateInterrupt(const genInt_t intID, connection_t connID, bool mixed, std::list<source_t> listInterruptedSources) {
290         sqlite3_stmt* query;
291         std::string _mixed = "false";
292
293         if (mixed) {
294                 _mixed = "true";
295         }
296
297         //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.
298         std::list<source_t>* pointer = new std::list<source_t>;
299         pointer=&listInterruptedSources;
300
301         std::string command="UPDATE " + std::string(INTERRUPT_TABLE) + " SET Connection_ID=:Connection_ID, mixed=:mixed ,listInterruptedSources=:listInterruptedSources WHERE ID=:id";
302         sqlite3_prepare_v2(m_database,command.c_str(),NULL,&query,NULL);
303         sqlite3_bind_int(query,0,connID);
304         sqlite3_bind_text(query,1,_mixed.c_str(),_mixed.size(),NULL);
305         sqlite3_bind_int(query,2,int(pointer));
306         sqlite3_bind_int(query,3,intID);
307
308         if (!this->pQuery(command)) {
309                 return GEN_DATABASE_ERROR;
310         } else {
311                 return GEN_OK;
312         }
313 }
314
315 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) {
316         sqlite3_stmt* query;
317         std::string command = "SELECT Connection_ID, Sink_ID, Source_ID, mixed, listInterruptedSources FROM " + std::string(INTERRUPT_TABLE) + " WHERE ID=" + int2string(intID) + ";";
318
319         if (SQexecute(command)!=true) {
320                 return GEN_DATABASE_ERROR;
321         } else {
322                 if (sqlite3_step(query)) {
323                         *return_connID = sqlite3_column_int(query,0);
324                         *return_Sink_ID = sqlite3_column_int(query,1);
325                         *return_Source_ID = sqlite3_column_int(query,2);
326                         *return_mixed = sqlite3_column_int(query,3);
327                         *return_listInterruptedSources = reinterpret_cast<std::list<source_t>*>(sqlite3_column_int(query,4));
328                         return GEN_OK;
329                 } else {
330                         return GEN_UNKNOWN;
331                 }
332         }
333 }
334
335 genError_t DataBaseHandler::removeInterrupt(const genInt_t intID) {
336         sqlite3_stmt* query;
337         std::string command = "SELECT listInterruptedSources FROM " + std::string(INTERRUPT_TABLE) + " WHERE ID=" + int2string(intID) + ";";
338         if (SQexecute(command) != true) {
339                 return GEN_DATABASE_ERROR;
340         } else {
341                 if (sqlite3_step(query)) {
342                         delete reinterpret_cast<std::list<source_t>*>(sqlite3_column_int(query,0));
343                         command = "DELETE FROM " + std::string(INTERRUPT_TABLE) + " WHERE ID='" + int2string(intID) + "';";
344                         if (!this->pQuery(command)) {
345                                 return GEN_DATABASE_ERROR;
346                         } else {
347                                 return GEN_OK;
348                         }
349                 }
350         }
351         return GEN_UNKNOWN;
352 }
353
354 domain_t DataBaseHandler::peek_Domain_ID(std::string DomainName) {
355         sqlite3_stmt* query;
356
357         std::string command = "SELECT ID FROM " + std::string(DOMAIN_TABLE) + " WHERE DomainName='" + DomainName + "';";
358
359         if (SQexecute(command)) {
360                 if (sqlite3_step(query)) {
361                         return sqlite3_column_int(query,0);
362                 } else {
363                         command = "INSERT INTO " + std::string(DOMAIN_TABLE) + " (DomainName) VALUES ('" + DomainName + "')";
364                 }
365         }
366
367         if (!this->pQuery(command)) {
368                 return -1;
369         } else {
370                 return sqlite3_last_insert_rowid(m_database);
371         }
372 }
373
374 domain_t DataBaseHandler::get_Domain_ID_from_Source_ID(source_t Source_ID) {
375         sqlite3_stmt* query;
376         std::string command = "SELECT Domain_ID FROM " + std::string(SOURCE_TABLE) + " WHERE ID=" + int2string(Source_ID) + ";";
377
378         if (SQexecute(command)) {
379                 return -1;
380         } else {
381                 sqlite3_step(query);
382                 return sqlite3_column_int(query,0);
383         }
384 }
385
386 domain_t DataBaseHandler::get_Domain_ID_from_Sink_ID(sink_t Sink_ID) {
387         sqlite3_stmt* query;
388         std::string command = "SELECT Domain_ID FROM " + std::string(SINK_TABLE) + " WHERE ID=" + int2string(Sink_ID) + ";";
389
390         if (SQexecute(command)) {
391                 return -1;
392         } else {
393                 sqlite3_step(query);
394                 return sqlite3_column_int(query,0);
395         }
396 }
397
398 source_t DataBaseHandler::get_Source_ID_from_Name(std::string name) {
399         sqlite3_stmt* query;
400         std::string command = "SELECT ID FROM " + std::string(SOURCE_TABLE) + " WHERE Name='" + name + "';";
401
402         if (SQexecute(command)) {
403                 return -1;
404         } else {
405                 sqlite3_step(query);
406                 return sqlite3_column_int(query,0);
407         }
408 }
409
410 sourceClass_t DataBaseHandler::get_Source_Class_ID_from_Name(std::string name) {
411         sqlite3_stmt* query;
412         std::string command = "SELECT ID FROM " + std::string(SOURCE_CLASS_TABLE) + " WHERE ClassName='" + name + "';";
413
414         if (SQexecute(command)) {
415                 return -1;
416         } else {
417                 sqlite3_step(query);
418                 return sqlite3_column_int(query,0);
419         }
420 }
421
422 domain_t DataBaseHandler::get_Domain_ID_from_Name(std::string name) {
423         sqlite3_stmt* query;
424         std::string command = "SELECT ID FROM " + std::string(DOMAIN_TABLE) + " WHERE DomainName='" + name + "';";
425
426         if (SQexecute(command)) {
427                 return -1;
428         } else {
429                 sqlite3_step(query);
430                 return sqlite3_column_int(query,0);
431         }
432 }
433
434 gateway_t DataBaseHandler::get_Gateway_ID_with_Domain_ID(domain_t startDomain_ID, domain_t targetDomain_ID) {
435         sqlite3_stmt* query;
436         std::string command = "SELECT ID FROM " + std::string(GATEWAY_TABLE) + " WHERE DomainSource_ID=" + int2string(startDomain_ID) + " AND DomainSink_ID=" + int2string(targetDomain_ID) + ";";
437
438         if (SQexecute(command)) {
439                 return -1;
440         } else {
441                 sqlite3_step(query);
442                 return sqlite3_column_int(query,0);
443         }
444 }
445
446 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) {
447         sqlite3_stmt* query;
448         std::string command = "SELECT Source_ID, Sink_ID, ControlDomain_ID FROM " + std::string(GATEWAY_TABLE) + " WHERE ID=" + int2string(Gateway_ID) + ";";
449
450         if (SQexecute(command)) {
451                 return GEN_DATABASE_ERROR;
452         } else {
453                 if (sqlite3_step(query)) {
454                         *return_Source_ID = sqlite3_column_int(query,0);
455                         *return_Sink_ID = sqlite3_column_int(query,1);
456                         *return_ControlDomain_ID = sqlite3_column_int(query,2);
457                         return GEN_OK;
458                 } else {
459                         return GEN_UNKNOWN;
460                 }
461         }
462 }
463
464 void DataBaseHandler::get_Domain_ID_Tree(bool onlyfree, RoutingTree* Tree, std::list<RoutingTreeItem*>* allItems) {
465         sqlite3_stmt* query;
466         int RootID = Tree->returnRootDomainID();
467         RoutingTreeItem *parent = Tree->returnRootItem();
468         std::string _onlyfree = "false";
469         unsigned int i = 0;
470
471         if (onlyfree) {
472                 _onlyfree = "true";
473         }
474
475         std::string command="SELECT ID,DomainSource_ID FROM " + std::string(GATEWAY_TABLE) + " WHERE DomainSink_ID=:id AND IsBlocked=:flag;";
476
477         sqlite3_prepare_v2(m_database,command.c_str(),NULL,&query,NULL);
478         do {
479                 sqlite3_bind_int(query,0,RootID);
480                 sqlite3_bind_text(query,1,_onlyfree.c_str(),_onlyfree.size(),NULL);
481                 while (sqlite3_step(query)) {
482                         allItems->push_back(Tree->insertItem(sqlite3_column_int(query,1), sqlite3_column_int(query,0), parent));
483                 }
484                 std::list<RoutingTreeItem*>::iterator it=allItems->begin();
485                 std::advance(it,i);
486                 RootID = (*it)->returnDomainID();
487                 i++;
488         } while (allItems->size() > i);
489 }
490
491 std::string DataBaseHandler::get_Bus_from_Domain_ID(domain_t Domain_ID) {
492         sqlite3_stmt* query;
493         std::string command = "SELECT BusName FROM " + std::string(DOMAIN_TABLE) + " WHERE ID=" + int2string(Domain_ID) + ";";
494
495         if (SQexecute(command)) {
496                 return std::string("");
497         } else {
498                 sqlite3_step(query);
499                 return std::string((const char*)sqlite3_column_text(query,0));
500         }
501 }
502
503 domain_t DataBaseHandler::get_Domain_ID_from_Connection_ID(connection_t ID) {
504         sqlite3_stmt* query;
505         std::string command = "SELECT Source_ID FROM " + std::string(CONNECTION_TABLE) + " WHERE ID=" + int2string(ID) + ";";
506
507         if (SQexecute(command)) {
508                 return -1;
509         }
510
511         sqlite3_step(query);
512         int SourceID = sqlite3_column_int(query,0);
513         command = "SELECT Domain_ID FROM " + std::string(SOURCE_TABLE) + " WHERE ID=" + int2string(SourceID) + ";";
514
515         if (SQexecute(command)) {
516                 return -1;
517         } else {
518                 sqlite3_step(query);
519                 return sqlite3_column_int(query,0);
520         }
521 }
522
523 void DataBaseHandler::getListofSources(std::list<SourceType>* SourceList) {
524         sqlite3_stmt* query;
525         SourceType sType;
526         std::string command = "SELECT ID,NAME FROM " + std::string(SOURCE_TABLE) + " WHERE isGateway='false';";
527         if (SQexecute(command)) {
528                 while (sqlite3_step(query)) {
529                         sType.ID = sqlite3_column_int(query,0);
530                         sType.name = std::string((const char*)sqlite3_column_text(query,1));
531                         SourceList->push_back(sType);
532                 }
533         }
534 }
535
536 void DataBaseHandler::getListofSinks(std::list<SinkType>* SinkList) {
537         sqlite3_stmt* query;
538         SinkType sType;
539         std::string command = "SELECT ID,NAME FROM " + std::string(SINK_TABLE) + ";";
540         if (SQexecute(command)) {
541                 while (sqlite3_step(query)) {
542                         sType.ID = sqlite3_column_int(query,0);
543                         sType.name =  std::string((const char*)sqlite3_column_text(query,1));
544                         SinkList->push_back(sType);
545                 }
546         }
547 }
548
549 void DataBaseHandler::getListofConnections(std::list<ConnectionType>* ConnectionList) {
550         sqlite3_stmt* query;
551         ConnectionType cType;
552         std::string command = "SELECT Source_ID,Sink_ID FROM " + std::string(CONNECTION_TABLE) + ";";
553         if (SQexecute(command)) {
554                 while (sqlite3_step(query)) {
555                         cType.Source_ID = sqlite3_column_int(query,0);
556                         cType.Sink_ID =  sqlite3_column_int(query,1);
557                         ConnectionList->push_back(cType);
558                 }
559         }
560 }
561
562 bool DataBaseHandler::is_source_Mixed(source_t source) {
563         sqlite3_stmt* query;
564         int classID = 0;
565
566         std::string command = "SELECT Class_ID FROM " + std::string(SOURCE_TABLE) + " WHERE ID='" + int2string(source) + "';";
567         if (SQexecute(command)) {
568                 if (sqlite3_step(query)) {
569                         classID = sqlite3_column_int(query,0);
570                 }
571         }
572         command = "SELECT isMixed FROM " + std::string(SOURCE_CLASS_TABLE) + " WHERE ID='" + int2string(classID) + "';";
573
574         if (SQexecute(command)) {
575                 if (sqlite3_step(query)) {
576                         char* answer=(char*)sqlite3_column_text(query,0);
577                         if (strcmp(answer,"true") == 0) {
578                                 return true;
579                         }
580                 }
581         }
582         return false;
583 }
584
585 sink_t DataBaseHandler::get_Sink_ID_from_Name(std::string name) {
586         sqlite3_stmt* query;
587         std::string command = "SELECT ID FROM " + std::string(SINK_TABLE) + " WHERE Name='" + name + "';";
588
589         if (SQexecute(command)) {
590                 return -1;
591         } else {
592                 sqlite3_step(query);
593                 return sqlite3_column_int(query,0);
594         }
595 }
596
597 connection_t DataBaseHandler::getConnectionID(source_t SourceID, sink_t SinkID) {
598         sqlite3_stmt* query;
599         std::string command="SELECT ID FROM " + std::string(MAIN_TABLE) + " WHERE Source_ID=:sourceID AND Sink_ID=:sinkID";
600         sqlite3_prepare_v2(m_database,command.c_str(),NULL,&query,NULL);
601         sqlite3_bind_int(query,0,SourceID);
602         sqlite3_bind_int(query,1,SinkID);
603
604         if (SQexecute(command)) {
605                 return -1;
606         } else {
607                 sqlite3_step(query);
608                 return sqlite3_column_int(query,0);
609         }
610 }
611
612 connection_t DataBaseHandler::insertConnection(source_t SourceID, sink_t SinkID) {
613         std::string command = "INSERT INTO " + std::string(CONNECTION_TABLE) + " (Source_ID, Sink_ID) VALUES (" + int2string(SourceID) + "," + int2string(SinkID) + ");";
614         if (!this->pQuery(command)) {
615                 return -1;
616         } else {
617                 return sqlite3_last_insert_rowid(m_database);
618         }
619 }
620
621 genError_t DataBaseHandler::removeConnection(connection_t ConnectionID) {
622         std::string command = "DELETE FROM " + std::string(CONNECTION_TABLE) + " WHERE ID='" + int2string(ConnectionID) + "';";
623         if (!this->pQuery(command)) {
624                 return GEN_DATABASE_ERROR;
625         } else {
626                 return GEN_OK;
627         }
628 }
629
630 connection_t DataBaseHandler::reserveMainConnection(source_t source, sink_t sink) {
631         sqlite3_stmt* query;
632         std::string command = "INSERT INTO " + std::string(MAIN_TABLE) + "(Source_ID, Sink_ID) VALUES(:Source_ID, :Sink_ID)";
633         sqlite3_prepare_v2(m_database,command.c_str(),NULL,&query,NULL);
634         sqlite3_bind_int(query,0,source);
635         sqlite3_bind_int(query,1,sink);
636         if (!this->pQuery(command)) {
637                 return -1;
638         } else {
639                 return sqlite3_last_insert_rowid(m_database);
640         }
641 }
642
643 genError_t DataBaseHandler::updateMainConnection(connection_t connID, genRoute_t route) {
644         sqlite3_stmt* query;
645
646         //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.
647         genRoute_t* routeheap = new genRoute_t(route);
648
649         std::string command = "UPDATE " + std::string(MAIN_TABLE) + " SET route=:route WHERE ID=:connID";
650         sqlite3_prepare_v2(m_database,command.c_str(),NULL,&query,NULL);
651         sqlite3_bind_int(query,0,connID);
652         sqlite3_bind_int(query,1,int(routeheap));
653
654         if (!this->pQuery(command)) {
655                 return GEN_DATABASE_ERROR;
656         } else {
657                 return GEN_OK;
658         }
659 }
660
661 genError_t DataBaseHandler::getMainConnectionDatafromID(const connection_t connID, sink_t* return_sinkID, source_t* return_sourceID, genRoute_t** return_route) {
662         sqlite3_stmt* query;
663         std::string command = "SELECT Sink_ID, Source_ID, route FROM " + std::string(MAIN_TABLE) + " WHERE ID=" + int2string(connID) + ";";
664
665         if (SQexecute(command)) {
666                 return GEN_DATABASE_ERROR;
667         } else {
668                 if (sqlite3_step(query)) {
669                         *return_sinkID = sqlite3_column_int(query,0);
670                         *return_sourceID = sqlite3_column_int(query,1);
671                         *return_route = reinterpret_cast<genRoute_t*>(sqlite3_column_int(query,2));
672                         return GEN_OK;
673                 } else {
674                         return GEN_UNKNOWN;
675                 }
676         }
677 }
678
679 connection_t DataBaseHandler::returnMainconnectionIDforSinkSourceID(sink_t sink, source_t source) {
680         sqlite3_stmt* query;
681         std::string command="SELECT ID FROM " + std::string(MAIN_TABLE) + " WHERE Sink_ID=:sinkID AND Source_ID=:SourceID";
682         sqlite3_prepare_v2(m_database,command.c_str(),NULL,&query,NULL);
683         sqlite3_bind_int(query,0,sink);
684         sqlite3_bind_int(query,1,source);
685
686         if (SQexecute(command)) {
687                 return -1;
688         } else {
689                 sqlite3_step(query);
690                 return sqlite3_column_int(query,0);
691         }
692 }
693
694 std::list<source_t> DataBaseHandler::getSourceIDsForSinkID(sink_t sink) {
695         std::list<source_t> list;
696         sqlite3_stmt* query;
697
698         std::string command="SELECT Source_ID FROM " + std::string(MAIN_TABLE) + " WHERE Sink_ID=" + int2string(sink);
699
700         if (SQexecute(command)) {
701                 while (sqlite3_step(query)) {
702                         list.push_back(sqlite3_column_int(query,0));
703                 }
704         }
705         return list;
706 }
707
708 std::list<ConnectionType> DataBaseHandler::getListAllMainConnections() {
709         std::list<ConnectionType> connectionList;
710         sqlite3_stmt* query;
711         std::string command = "SELECT Sink_ID, Source_ID FROM " + std::string(MAIN_TABLE) + ";";
712
713         if (SQexecute(command)) {
714                 while (sqlite3_step(query)) {
715                         ConnectionType temp;
716                         temp.Sink_ID = sqlite3_column_int(query,0);
717                         temp.Source_ID = sqlite3_column_int(query,1);
718                         connectionList.push_back(temp);
719                         DLT_LOG( AudioManager, DLT_LOG_INFO, DLT_STRING("Added Connection"), DLT_INT(temp.Sink_ID), DLT_INT(temp.Source_ID));
720                 }
721         }
722         return connectionList;
723 }
724
725 genError_t DataBaseHandler::removeMainConnection(connection_t connID) {
726         sqlite3_stmt* query;
727         std::string command = "SELECT route FROM " + std::string(MAIN_TABLE) + " WHERE ID=" + int2string(connID) + ";";
728         if (SQexecute(command)) {
729                 if (sqlite3_step(query)) {
730                         delete reinterpret_cast<genRoute_t*>(sqlite3_column_int(query,0));
731                         command = "DELETE FROM " + std::string(MAIN_TABLE) + " WHERE ID='" + int2string(connID) + "';";
732                         if (SQexecute(command)!=true) {
733                                 return GEN_DATABASE_ERROR;
734                         } else {
735                                 return GEN_OK;
736                         }
737                 }
738         }
739         return GEN_UNKNOWN;
740 }