2 * Copyright (C) 2011, BMW AG
6 * \file DataBaseHandler.cpp
7 * if (query.exec(command) != true) {
11 * \author Christian Müller (christian.ei.mueller@bmw.de)
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
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.
28 #include "DataBaseHandler.h"
36 DataBaseHandler::DataBaseHandler() {
39 m_path = "/home/blacky";
42 m_path.append(AUDIO_DATABASE);
44 std::ifstream infile(m_path.c_str());
47 remove(m_path.c_str());
48 DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("Knocked down database"));
50 if (!this->open_database()) {
51 DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("Problems with opening the database"));
55 DataBaseHandler::~DataBaseHandler() {
56 this->close_database();
59 bool DataBaseHandler::pQuery(std::string command) {
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()));
68 std::string DataBaseHandler::int2string(int i) {
69 std::stringstream out;
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) {
82 void DataBaseHandler::close_database() {
83 DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("Closed Database"));
84 sqlite3_close(m_database);
87 bool DataBaseHandler::delete_data(std::string table) {
88 std::string command = "DELETE FROM" + table;
89 if (!this->pQuery(command)) return false;
93 bool DataBaseHandler::create_tables() {
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;
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;
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;
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;
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;
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;
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;
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;
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;
125 domain_t DataBaseHandler::insert_into_Domains_table(std::string DomainName, std::string BusName, std::string NodeName, bool EarlyMode) {
127 std::string _EarlyMode = "false";
132 std::string command = "SELECT BusName,ID FROM " + std::string(DOMAIN_TABLE) + " WHERE DomainName='" + DomainName + "'";
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));
138 return sqlite3_column_int(query,1);
140 command = "UPDATE " + std::string(DOMAIN_TABLE) + "SET Busname=" + BusName + " Nodename=" + NodeName + " EarlyMode=" + _EarlyMode + " WHERE DomainName=" + DomainName;
143 command = "INSERT INTO " + std::string(DOMAIN_TABLE) + " (DomainName, BusName, NodeName, EarlyMode) VALUES ('" + DomainName + "','" + BusName + "'','" + NodeName + "','" + _EarlyMode + "')";
147 sqlite3_finalize(query);
149 if (!this->pQuery(command)) {
152 return get_Domain_ID_from_Name(DomainName);
156 sourceClass_t DataBaseHandler::insert_into_Source_Class_table(std::string ClassName, volume_t VolumeOffset, bool IsInterrupt, bool IsMixed) {
158 std::string _IsInterrupt = "false";
159 std::string _IsMixed = "false";
162 _IsInterrupt = "true";
168 std::string command = "SELECT ID FROM " + std::string(SOURCE_CLASS_TABLE) + " WHERE ClassName='" + ClassName + "';";
170 if (SQexecute(command)) {
171 if (sqlite3_step(query)) {
172 return sqlite3_column_int(query,0);
176 command = "INSERT INTO " + std::string(SOURCE_CLASS_TABLE) + " (ClassName, VolumeOffset, IsInterrupt, IsMixed) VALUES ('" + ClassName + "'," + int2string(VolumeOffset) + ",'" + _IsInterrupt + "','" + _IsMixed + "')";
178 if (!this->pQuery(command)) {
181 return sqlite3_last_insert_rowid(m_database);
185 sink_t DataBaseHandler::insert_into_Sink_Class_table(std::string ClassName) {
188 std::string command = "SELECT ID FROM " + std::string(SINK_CLASS_TABLE) + " WHERE ClassName='" + ClassName + "';";
190 if (SQexecute(command)) {
191 if (sqlite3_step(query)) {
192 return sqlite3_column_int(query,0);
196 command = "INSERT INTO " + std::string(SINK_CLASS_TABLE) + " (ClassName) VALUES ('" + ClassName + "')";
198 if (!this->pQuery(command)) {
201 return sqlite3_last_insert_rowid(m_database);
205 source_t DataBaseHandler::insert_into_Source_table(std::string Name, sourceClass_t Class_ID, domain_t Domain_ID, bool IsGateway) {
207 std::string _IsGateway = "false";
213 std::string command = "SELECT ID FROM " + std::string(SOURCE_TABLE) + " WHERE Name='" + Name + "';";
215 if (SQexecute(command)) {
216 if (sqlite3_step(query)) {
217 return sqlite3_column_int(query,0);
221 command = "INSERT INTO " + std::string(SOURCE_TABLE) + " (Name, Class_ID, Domain_ID, IsGateway) VALUES ('" + Name + "'," + int2string(Class_ID) + ",'" + int2string(Domain_ID) + "','" + _IsGateway + "')";
223 if (sqlite3_step(query)) {
226 //emit signal_numberOfSourcesChanged();
227 return sqlite3_last_insert_rowid(m_database);
231 sink_t DataBaseHandler::insert_into_Sink_table(std::string Name, sinkClass_t Class_ID, domain_t Domain_ID, bool IsGateway) {
233 std::string _IsGateway = "false";
239 std::string command = "SELECT ID FROM " + std::string(SINK_TABLE) + " WHERE Name='" + Name + "';";
241 if (SQexecute(command)) {
242 if (sqlite3_step(query)) {
243 return sqlite3_column_int(query,0);
247 command = "INSERT INTO " + std::string(SINK_TABLE) + " (Name, Class_ID, Domain_ID, IsGateway) VALUES ('" + Name + "'," + int2string(Class_ID) + ",'" + int2string(Domain_ID) + "','" + _IsGateway + "')";
249 if (!this->pQuery(command)) {
252 return sqlite3_last_insert_rowid(m_database);
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) {
258 std::string command = "SELECT ID FROM " + std::string(GATEWAY_TABLE) + " WHERE Name='" + Name + "';";
260 if (SQexecute(command)) {
261 if (sqlite3_step(query)) {
262 return sqlite3_column_int(query,0);
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')";
268 if (!this->pQuery(command)) {
271 return sqlite3_last_insert_rowid(m_database);
275 genInt_t DataBaseHandler::reserveInterrupt(sink_t Sink_ID, source_t Source_ID) {
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);
282 if (!this->pQuery(command)) {
285 return sqlite3_last_insert_rowid(m_database);
289 genError_t DataBaseHandler::updateInterrupt(const genInt_t intID, connection_t connID, bool mixed, std::list<source_t> listInterruptedSources) {
291 std::string _mixed = "false";
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;
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);
308 if (!this->pQuery(command)) {
309 return GEN_DATABASE_ERROR;
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) {
317 std::string command = "SELECT Connection_ID, Sink_ID, Source_ID, mixed, listInterruptedSources FROM " + std::string(INTERRUPT_TABLE) + " WHERE ID=" + int2string(intID) + ";";
319 if (SQexecute(command)!=true) {
320 return GEN_DATABASE_ERROR;
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));
335 genError_t DataBaseHandler::removeInterrupt(const genInt_t intID) {
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;
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;
354 domain_t DataBaseHandler::peek_Domain_ID(std::string DomainName) {
357 std::string command = "SELECT ID FROM " + std::string(DOMAIN_TABLE) + " WHERE DomainName='" + DomainName + "';";
359 if (SQexecute(command)) {
360 if (sqlite3_step(query)) {
361 return sqlite3_column_int(query,0);
363 command = "INSERT INTO " + std::string(DOMAIN_TABLE) + " (DomainName) VALUES ('" + DomainName + "')";
367 if (!this->pQuery(command)) {
370 return sqlite3_last_insert_rowid(m_database);
374 domain_t DataBaseHandler::get_Domain_ID_from_Source_ID(source_t Source_ID) {
376 std::string command = "SELECT Domain_ID FROM " + std::string(SOURCE_TABLE) + " WHERE ID=" + int2string(Source_ID) + ";";
378 if (SQexecute(command)) {
382 return sqlite3_column_int(query,0);
386 domain_t DataBaseHandler::get_Domain_ID_from_Sink_ID(sink_t Sink_ID) {
388 std::string command = "SELECT Domain_ID FROM " + std::string(SINK_TABLE) + " WHERE ID=" + int2string(Sink_ID) + ";";
390 if (SQexecute(command)) {
394 return sqlite3_column_int(query,0);
398 source_t DataBaseHandler::get_Source_ID_from_Name(std::string name) {
400 std::string command = "SELECT ID FROM " + std::string(SOURCE_TABLE) + " WHERE Name='" + name + "';";
402 if (SQexecute(command)) {
406 return sqlite3_column_int(query,0);
410 sourceClass_t DataBaseHandler::get_Source_Class_ID_from_Name(std::string name) {
412 std::string command = "SELECT ID FROM " + std::string(SOURCE_CLASS_TABLE) + " WHERE ClassName='" + name + "';";
414 if (SQexecute(command)) {
418 return sqlite3_column_int(query,0);
422 domain_t DataBaseHandler::get_Domain_ID_from_Name(std::string name) {
424 std::string command = "SELECT ID FROM " + std::string(DOMAIN_TABLE) + " WHERE DomainName='" + name + "';";
426 if (SQexecute(command)) {
430 return sqlite3_column_int(query,0);
434 gateway_t DataBaseHandler::get_Gateway_ID_with_Domain_ID(domain_t startDomain_ID, domain_t targetDomain_ID) {
436 std::string command = "SELECT ID FROM " + std::string(GATEWAY_TABLE) + " WHERE DomainSource_ID=" + int2string(startDomain_ID) + " AND DomainSink_ID=" + int2string(targetDomain_ID) + ";";
438 if (SQexecute(command)) {
442 return sqlite3_column_int(query,0);
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) {
448 std::string command = "SELECT Source_ID, Sink_ID, ControlDomain_ID FROM " + std::string(GATEWAY_TABLE) + " WHERE ID=" + int2string(Gateway_ID) + ";";
450 if (SQexecute(command)) {
451 return GEN_DATABASE_ERROR;
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);
464 void DataBaseHandler::get_Domain_ID_Tree(bool onlyfree, RoutingTree* Tree, std::list<RoutingTreeItem*>* allItems) {
466 int RootID = Tree->returnRootDomainID();
467 RoutingTreeItem *parent = Tree->returnRootItem();
468 std::string _onlyfree = "false";
475 std::string command="SELECT ID,DomainSource_ID FROM " + std::string(GATEWAY_TABLE) + " WHERE DomainSink_ID=:id AND IsBlocked=:flag;";
477 sqlite3_prepare_v2(m_database,command.c_str(),NULL,&query,NULL);
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));
484 std::list<RoutingTreeItem*>::iterator it=allItems->begin();
486 RootID = (*it)->returnDomainID();
488 } while (allItems->size() > i);
491 std::string DataBaseHandler::get_Bus_from_Domain_ID(domain_t Domain_ID) {
493 std::string command = "SELECT BusName FROM " + std::string(DOMAIN_TABLE) + " WHERE ID=" + int2string(Domain_ID) + ";";
495 if (SQexecute(command)) {
496 return std::string("");
499 return std::string((const char*)sqlite3_column_text(query,0));
503 domain_t DataBaseHandler::get_Domain_ID_from_Connection_ID(connection_t ID) {
505 std::string command = "SELECT Source_ID FROM " + std::string(CONNECTION_TABLE) + " WHERE ID=" + int2string(ID) + ";";
507 if (SQexecute(command)) {
512 int SourceID = sqlite3_column_int(query,0);
513 command = "SELECT Domain_ID FROM " + std::string(SOURCE_TABLE) + " WHERE ID=" + int2string(SourceID) + ";";
515 if (SQexecute(command)) {
519 return sqlite3_column_int(query,0);
523 void DataBaseHandler::getListofSources(std::list<SourceType>* SourceList) {
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);
536 void DataBaseHandler::getListofSinks(std::list<SinkType>* SinkList) {
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);
549 void DataBaseHandler::getListofConnections(std::list<ConnectionType>* ConnectionList) {
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);
562 bool DataBaseHandler::is_source_Mixed(source_t source) {
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);
572 command = "SELECT isMixed FROM " + std::string(SOURCE_CLASS_TABLE) + " WHERE ID='" + int2string(classID) + "';";
574 if (SQexecute(command)) {
575 if (sqlite3_step(query)) {
576 char* answer=(char*)sqlite3_column_text(query,0);
577 if (strcmp(answer,"true") == 0) {
585 sink_t DataBaseHandler::get_Sink_ID_from_Name(std::string name) {
587 std::string command = "SELECT ID FROM " + std::string(SINK_TABLE) + " WHERE Name='" + name + "';";
589 if (SQexecute(command)) {
593 return sqlite3_column_int(query,0);
597 connection_t DataBaseHandler::getConnectionID(source_t SourceID, sink_t SinkID) {
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);
604 if (SQexecute(command)) {
608 return sqlite3_column_int(query,0);
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)) {
617 return sqlite3_last_insert_rowid(m_database);
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;
630 connection_t DataBaseHandler::reserveMainConnection(source_t source, sink_t sink) {
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)) {
639 return sqlite3_last_insert_rowid(m_database);
643 genError_t DataBaseHandler::updateMainConnection(connection_t connID, genRoute_t route) {
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);
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));
654 if (!this->pQuery(command)) {
655 return GEN_DATABASE_ERROR;
661 genError_t DataBaseHandler::getMainConnectionDatafromID(const connection_t connID, sink_t* return_sinkID, source_t* return_sourceID, genRoute_t** return_route) {
663 std::string command = "SELECT Sink_ID, Source_ID, route FROM " + std::string(MAIN_TABLE) + " WHERE ID=" + int2string(connID) + ";";
665 if (SQexecute(command)) {
666 return GEN_DATABASE_ERROR;
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));
679 connection_t DataBaseHandler::returnMainconnectionIDforSinkSourceID(sink_t sink, source_t source) {
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);
686 if (SQexecute(command)) {
690 return sqlite3_column_int(query,0);
694 std::list<source_t> DataBaseHandler::getSourceIDsForSinkID(sink_t sink) {
695 std::list<source_t> list;
698 std::string command="SELECT Source_ID FROM " + std::string(MAIN_TABLE) + " WHERE Sink_ID=" + int2string(sink);
700 if (SQexecute(command)) {
701 while (sqlite3_step(query)) {
702 list.push_back(sqlite3_column_int(query,0));
708 std::list<ConnectionType> DataBaseHandler::getListAllMainConnections() {
709 std::list<ConnectionType> connectionList;
711 std::string command = "SELECT Sink_ID, Source_ID FROM " + std::string(MAIN_TABLE) + ";";
713 if (SQexecute(command)) {
714 while (sqlite3_step(query)) {
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));
722 return connectionList;
725 genError_t DataBaseHandler::removeMainConnection(connection_t connID) {
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;