Thomas Göhring: Fix for: Bug 69 - Endless loop in CAmDatabaseHandler::getRoutingTree...
[profile/ivi/genivi/genivi-audio-manager.git] / AudioManagerDaemon / src / CAmDatabaseHandler.cpp
1 /**
2  * Copyright (C) 2012, BMW AG
3  *
4  * This file is part of GENIVI Project AudioManager.
5  *
6  * Contributions are licensed to the GENIVI Alliance under one or more
7  * Contribution License Agreements.
8  *
9  * \copyright
10  * This Source Code Form is subject to the terms of the
11  * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed with
12  * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  *
15  * \author Christian Mueller, christian.ei.mueller@bmw.de BMW 2011,2012
16  *
17  * \file CAmDatabaseHandler.cpp
18  * For further information see http://www.genivi.org/.
19  *
20  */
21
22 #include "CAmDatabaseHandler.h"
23 #include <cassert>
24 #include <stdexcept>
25 #include <vector>
26 #include <fstream>
27 #include <sstream>
28 #include <string>
29 #include "CAmDatabaseObserver.h"
30 #include "CAmRouter.h"
31 #include "shared/CAmDltWrapper.h"
32
33 namespace am
34 {
35
36 /**
37  * Macro to handle SQLITE errors on prepare
38  */
39 #define MY_SQLITE_PREPARE_V2(db,zSql,nByte,ppStmt,pzTail)                                                               \
40         if ((eCode = sqlite3_prepare_v2(db, zSql, nByte, ppStmt, pzTail)))                                              \
41         {                                                                                                               \
42             logError("CAmDatabaseHandler::my_sqlite_prepare_v2 on Command",zSql,"failed with errorCode:", eCode);       \
43             return (E_DATABASE_ERROR);                                                                                  \
44         }
45
46 #define MY_SQLITE_PREPARE_V2_BOOL(db,zSql,nByte,ppStmt,pzTail)                                                          \
47         if ((eCode = sqlite3_prepare_v2(db, zSql, nByte, ppStmt, pzTail)))                                              \
48         {                                                                                                               \
49             logError("CAmDatabaseHandler::my_sqlite_prepare_v2_bool on Command",zSql,"failed with errorCode:", eCode);  \
50             return (false);                                                                                             \
51         }
52
53 /**
54  * Macro to handle SQLITE errors bind text
55  */
56 #define MY_SQLITE_BIND_TEXT(query,index,text,size,static_)                                                              \
57         if ((eCode = sqlite3_bind_text(query, index, text, size, static_)))                                             \
58         {                                                                                                               \
59             logError("CAmDatabaseHandler::sqlite3_bind_text failed with errorCode:", eCode);                            \
60             return (E_DATABASE_ERROR);                                                                                  \
61         }
62
63 /**
64  * Macro to handle SQLITE errors on bind int
65  */
66 #define MY_SQLITE_BIND_INT(query, index, data)                                                                          \
67         if((eCode = sqlite3_bind_int(query, index, data)))                                                              \
68         {                                                                                                               \
69             logError("CAmDatabaseHandler::sqlite3_bind_int failed with errorCode:", eCode);                             \
70             return (E_DATABASE_ERROR);                                                                                  \
71         }
72
73 /**
74  * Macro to handle SQLITE errors on reset
75  */
76 #define MY_SQLITE_RESET(query)                                                                                          \
77         if((eCode = sqlite3_reset(query)))                                                                              \
78         {                                                                                                               \
79             logError("CAmDatabaseHandler::sqlite3_reset failed with errorCode:", eCode);                                \
80             return (E_DATABASE_ERROR);                                                                                  \
81         }
82
83 /**
84  * Macro to handle SQLITE finalize
85  */
86 #define MY_SQLITE_FINALIZE(query)                                                                                       \
87         if((eCode = sqlite3_finalize(query)))                                                                           \
88         {                                                                                                               \
89             logError("CAmDatabaseHandler::sqlite3_finalize failed with errorCode:", eCode);                             \
90             return (E_DATABASE_ERROR);                                                                                  \
91         }
92
93 #define MY_SQLITE_FINALIZE_BOOL(query)                                                                                  \
94         if((eCode = sqlite3_finalize(query)))                                                                           \
95         {                                                                                                               \
96             logError("CAmDatabaseHandler::sqlite3_finalize failed with errorCode:", eCode);                             \
97             return (true);                                                                                              \
98         }
99
100 #define DOMAIN_TABLE "Domains"  //!< domain table
101 #define SOURCE_CLASS_TABLE "SourceClasses" //!< source class table
102 #define SINK_CLASS_TABLE "SinkClasses" //!< sink class table
103 #define SOURCE_TABLE "Sources" //!< source table
104 #define SINK_TABLE "Sinks" //!< sink table
105 #define GATEWAY_TABLE "Gateways" //!< gateway table
106 #define CROSSFADER_TABLE "Crossfaders" //!< crossfader table
107 #define CONNECTION_TABLE "Connections" //!< connection table
108 #define MAINCONNECTION_TABLE "MainConnections" //!< main connection table
109 #define SYSTEM_TABLE "SystemProperties" //!< system properties table
110 /**
111  * table that holds table informations
112  */
113 const std::string databaseTables[] =
114 { " Domains (domainID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50), busname VARCHAR(50), nodename VARCHAR(50), early BOOL, complete BOOL, state INTEGER, reserved BOOL);", //
115         " SourceClasses (sourceClassID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50));", //
116         " SinkClasses (sinkClassID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50));", //
117         " Sources (sourceID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, domainID INTEGER, name VARCHAR(50), sourceClassID INTEGER, sourceState INTEGER, volume INTEGER, visible BOOL, availability INTEGER, availabilityReason INTEGER, interruptState INTEGER, reserved BOOL);", //
118         " Sinks (sinkID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50), domainID INTEGER, sinkClassID INTEGER, volume INTEGER, visible BOOL, availability INTEGER, availabilityReason INTEGER, muteState INTEGER, mainVolume INTEGER, reserved BOOL);", //
119         " Gateways (gatewayID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50), sinkID INTEGER, sourceID INTEGER, domainSinkID INTEGER, domainSourceID INTEGER, controlDomainID INTEGER);", //
120         " Crossfaders (crossfaderID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50), sinkID_A INTEGER, sinkID_B INTEGER, sourceID INTEGER, hotSink INTEGER);", //
121         " Connections (connectionID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, sourceID INTEGER, sinkID INTEGER, delay INTEGER, connectionFormat INTEGER, reserved BOOL);", //
122         " MainConnections (mainConnectionID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, sourceID INTEGER, sinkID INTEGER, connectionState INTEGER, delay INTEGER);", //
123         " SystemProperties (type INTEGER PRIMARY KEY, value INTEGER);" };
124
125 /**
126  * template to converts T to std::string
127  * @param x T
128  * @return string
129  */
130 template<typename T>
131 inline std::string i2s(T const& x)
132 {
133     std::ostringstream o;
134     o << x;
135     return (o.str());
136 }
137
138 CAmDatabaseHandler::CAmDatabaseHandler(std::string databasePath) :
139         mpDatabase(NULL), //
140         mPath(databasePath), //
141         mpDatabaseObserver(NULL), //
142         mFirstStaticSink(true), //
143         mFirstStaticSource(true), //
144         mFirstStaticGateway(true), //
145         mFirstStaticSinkClass(true), //
146         mFirstStaticSourceClass(true), //
147         mFirstStaticCrossfader(true), //
148         mListConnectionFormat()
149 {
150
151     std::ifstream infile(mPath.c_str());
152
153     if (infile)
154     {
155         if(remove(mPath.c_str())==0)
156         {
157                 logError("DatabaseHandler::DatabaseHandler Knocked down database failed !");
158         }
159         logInfo("DatabaseHandler::DatabaseHandler Knocked down database");
160     }
161
162     bool dbOpen = openDatabase();
163     if (!dbOpen)
164     {
165         logInfo("DatabaseHandler::DatabaseHandler problems opening the database!");
166     }
167
168     createTables();
169 }
170
171 CAmDatabaseHandler::~CAmDatabaseHandler()
172 {
173     logInfo("Closed Database");
174     sqlite3_close(mpDatabase);
175 }
176
177 am_Error_e CAmDatabaseHandler::enterDomainDB(const am_Domain_s & domainData, am_domainID_t & domainID)
178 {
179     assert(domainData.domainID==0);
180     assert(!domainData.name.empty());
181     assert(!domainData.busname.empty());
182     assert(domainData.state>=DS_UNKNOWN && domainData.state<=DS_MAX);
183
184     //first check for a reserved domain
185     sqlite3_stmt* query = NULL;
186     int eCode = 0;
187     domainID=0;
188     std::string command = "SELECT domainID FROM " + std::string(DOMAIN_TABLE) + " WHERE name=?";
189     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
190     MY_SQLITE_BIND_TEXT(query, 1, domainData.name.c_str(), domainData.name.size(), SQLITE_STATIC)
191     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
192     {
193         domainID = sqlite3_column_int(query, 0);
194         command = "UPDATE " + std::string(DOMAIN_TABLE) + " SET name=?, busname=?, nodename=?, early=?, complete=?, state=?, reserved=? WHERE domainID=" + i2s(sqlite3_column_int(query, 0));
195     }
196     else if (eCode == SQLITE_DONE)
197     {
198
199         command = "INSERT INTO " + std::string(DOMAIN_TABLE) + " (name, busname, nodename, early, complete, state, reserved) VALUES (?,?,?,?,?,?,?)";
200     }
201     else
202     {
203         logError("DatabaseHandler::enterDomainDB SQLITE Step error code:", eCode);
204         return (E_DATABASE_ERROR);
205     }
206
207     MY_SQLITE_FINALIZE(query)
208
209     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
210     MY_SQLITE_BIND_TEXT(query, 1, domainData.name.c_str(), domainData.name.size(), SQLITE_STATIC)
211     MY_SQLITE_BIND_TEXT(query, 2, domainData.busname.c_str(), domainData.busname.size(), SQLITE_STATIC)
212     MY_SQLITE_BIND_TEXT(query, 3, domainData.nodename.c_str(), domainData.nodename.size(), SQLITE_STATIC)
213     MY_SQLITE_BIND_INT(query, 4, domainData.early)
214     MY_SQLITE_BIND_INT(query, 5, domainData.complete)
215     MY_SQLITE_BIND_INT(query, 6, domainData.state)
216     MY_SQLITE_BIND_INT(query, 7, 0)
217
218     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
219     {
220         logError("DatabaseHandler::enterDomainDB SQLITE Step error code:", eCode);
221         MY_SQLITE_FINALIZE(query)
222         return (E_DATABASE_ERROR);
223     }
224     MY_SQLITE_FINALIZE(query)
225
226     if (domainID==0)
227         domainID = sqlite3_last_insert_rowid(mpDatabase);
228     logInfo("DatabaseHandler::enterDomainDB entered new domain with name=", domainData.name, "busname=", domainData.busname, "nodename=", domainData.nodename, "assigned ID:", domainID);
229
230     am_Domain_s domain = domainData;
231     domain.domainID = domainID;
232     if (mpDatabaseObserver)
233         mpDatabaseObserver->newDomain(domain);
234
235     return (E_OK);
236 }
237
238 am_Error_e CAmDatabaseHandler::enterMainConnectionDB(const am_MainConnection_s & mainConnectionData, am_mainConnectionID_t & connectionID)
239 {
240     assert(mainConnectionData.mainConnectionID==0);
241     assert(mainConnectionData.connectionState>=CS_UNKNOWN && mainConnectionData.connectionState<=CS_MAX);
242     assert(mainConnectionData.sinkID!=0);
243     assert(mainConnectionData.sourceID!=0);
244
245     sqlite3_stmt* query = NULL;
246     int eCode = 0;
247     int16_t delay = 0;
248     std::string command = "INSERT INTO " + std::string(MAINCONNECTION_TABLE) + "(sourceID, sinkID, connectionState, delay) VALUES (?,?,?,-1)";
249     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
250     MY_SQLITE_BIND_INT(query, 1, mainConnectionData.sourceID)
251     MY_SQLITE_BIND_INT(query, 2, mainConnectionData.sinkID)
252     MY_SQLITE_BIND_INT(query, 3, mainConnectionData.connectionState)
253
254     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
255     {
256         logError("DatabaseHandler::enterMainConnectionDB SQLITE Step error code:", eCode);
257         MY_SQLITE_FINALIZE(query)
258         return (E_DATABASE_ERROR);
259     }
260
261     MY_SQLITE_FINALIZE(query)
262
263     connectionID = sqlite3_last_insert_rowid(mpDatabase);
264
265     //now check the connectionTable for all connections in the route. IF connectionID exist
266     command = "SELECT delay FROM " + std::string(CONNECTION_TABLE) + (" WHERE connectionID=?");
267     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
268     std::vector<am_connectionID_t>::const_iterator elementIterator = mainConnectionData.listConnectionID.begin();
269     for (; elementIterator < mainConnectionData.listConnectionID.end(); ++elementIterator)
270     {
271         MY_SQLITE_BIND_INT(query, 1, *elementIterator)
272
273         if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
274         {
275             int16_t temp_delay = sqlite3_column_int(query, 1);
276             if (temp_delay != -1 && delay != -1)
277                 delay += temp_delay;
278             else
279                 delay = -1;
280         }
281         else
282         {
283             logError("DatabaseHandler::enterMainConnectionDB did not find route for MainConnection: ", eCode);
284             MY_SQLITE_FINALIZE(query)
285             return (E_DATABASE_ERROR);
286         }
287         MY_SQLITE_RESET(query)
288     }
289     MY_SQLITE_FINALIZE(query)
290
291     //now we create a table with references to the connections;
292     command = "CREATE TABLE MainConnectionRoute" + i2s(connectionID) + std::string("(connectionID INTEGER)");
293     if (!this->sqQuery(command))
294         return (E_DATABASE_ERROR);
295
296     command = "INSERT INTO MainConnectionRoute" + i2s(connectionID) + "(connectionID) VALUES (?)";
297     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
298     std::vector<am_connectionID_t>::const_iterator listConnectionIterator(mainConnectionData.listConnectionID.begin());
299     for (; listConnectionIterator < mainConnectionData.listConnectionID.end(); ++listConnectionIterator)
300     {
301         MY_SQLITE_BIND_INT(query, 1, *listConnectionIterator)
302         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
303         {
304             logError("DatabaseHandler::enterMainConnectionDB SQLITE Step error code:", eCode);
305             MY_SQLITE_FINALIZE(query)
306             return (E_DATABASE_ERROR);
307         }
308         MY_SQLITE_RESET(query)
309     }
310
311     MY_SQLITE_FINALIZE(query)
312     logInfo("DatabaseHandler::enterMainConnectionDB entered new mainConnection with sourceID", mainConnectionData.sourceID, "sinkID:", mainConnectionData.sinkID, "delay:", delay, "assigned ID:", connectionID);
313
314     if (mpDatabaseObserver)
315     {
316         am_MainConnectionType_s mainConnection;
317         mainConnection.mainConnectionID = connectionID;
318         mainConnection.connectionState = mainConnectionData.connectionState;
319         mainConnection.delay = delay;
320         mainConnection.sinkID = mainConnectionData.sinkID;
321         mainConnection.sourceID = mainConnectionData.sourceID;
322         mpDatabaseObserver->newMainConnection(mainConnection);
323         mpDatabaseObserver->mainConnectionStateChanged(connectionID, mainConnectionData.connectionState);
324     }
325
326     //finally, we update the delay value for the maintable
327     if (delay == 0)
328         delay = -1;
329     return (changeDelayMainConnection(delay, connectionID));
330 }
331
332 am_Error_e CAmDatabaseHandler::enterSinkDB(const am_Sink_s & sinkData, am_sinkID_t & sinkID)
333 {
334     assert(sinkData.sinkID<DYNAMIC_ID_BOUNDARY);
335     assert(sinkData.domainID!=0);
336     assert(!sinkData.name.empty());
337     assert(sinkData.sinkClassID!=0);
338     //todo: need to check if class exists?
339     assert(!sinkData.listConnectionFormats.empty());
340     assert(sinkData.muteState>=MS_UNKNOWN && sinkData.muteState<=MS_MAX);
341
342     sqlite3_stmt *query = NULL;
343     int eCode = 0;
344     std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE name=? AND reserved=1";
345
346     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
347     MY_SQLITE_BIND_TEXT(query, 1, sinkData.name.c_str(), sinkData.name.size(), SQLITE_STATIC)
348
349     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
350     {
351         command = "UPDATE " + std::string(SINK_TABLE) + " SET name=?, domainID=?, sinkClassID=?, volume=?, visible=?, availability=?, availabilityReason=?, muteState=?, mainVolume=?, reserved=? WHERE sinkID=" + i2s(sqlite3_column_int(query, 0));
352     }
353     else if (eCode == SQLITE_DONE)
354     {
355         //if sinkID is zero and the first Static Sink was already entered, the ID is created
356         if (sinkData.sinkID == 0 && !mFirstStaticSink && !existSinkName(sinkData.name))
357         {
358             command = "INSERT INTO " + std::string(SINK_TABLE) + "(name, domainID, sinkClassID, volume, visible, availability, availabilityReason, muteState, mainVolume, reserved) VALUES (?,?,?,?,?,?,?,?,?,?)";
359         }
360         else
361         {
362             //check if the ID already exists
363             if (existSinkNameOrID(sinkData.sinkID, sinkData.name))
364             {
365                 MY_SQLITE_FINALIZE(query)
366                 return (E_ALREADY_EXISTS);
367             }
368             command = "INSERT INTO " + std::string(SINK_TABLE) + "(name, domainID, sinkClassID, volume, visible, availability, availabilityReason, muteState, mainVolume, reserved, sinkID) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
369         }
370     }
371     else
372     {
373         logError("DatabaseHandler::enterSinkDB SQLITE Step error code:", eCode);
374         MY_SQLITE_FINALIZE(query)
375         return (E_DATABASE_ERROR);
376     }
377
378     MY_SQLITE_FINALIZE(query)
379
380     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
381     MY_SQLITE_BIND_TEXT(query, 1, sinkData.name.c_str(), sinkData.name.size(), SQLITE_STATIC)
382     MY_SQLITE_BIND_INT(query, 2, sinkData.domainID)
383     MY_SQLITE_BIND_INT(query, 3, sinkData.sinkClassID)
384     MY_SQLITE_BIND_INT(query, 4, sinkData.volume)
385     MY_SQLITE_BIND_INT(query, 5, sinkData.visible)
386     MY_SQLITE_BIND_INT(query, 6, sinkData.available.availability)
387     MY_SQLITE_BIND_INT(query, 7, sinkData.available.availabilityReason)
388     MY_SQLITE_BIND_INT(query, 8, sinkData.muteState)
389     MY_SQLITE_BIND_INT(query, 9, sinkData.mainVolume)
390     MY_SQLITE_BIND_INT(query, 10, 0)
391
392     //if the ID is not created, we add it to the query
393     if (sinkData.sinkID != 0)
394     {
395         MY_SQLITE_BIND_INT(query, 11, sinkData.sinkID)
396     }
397
398     //if the first static sink is entered, we need to set it onto the boundary
399     else if (mFirstStaticSink)
400     {
401         MY_SQLITE_BIND_INT(query, 11, DYNAMIC_ID_BOUNDARY)
402         mFirstStaticSink = false;
403     }
404
405     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
406     {
407         logError("DatabaseHandler::enterSinkDB SQLITE Step error code:", eCode);
408         MY_SQLITE_FINALIZE(query)
409         return (E_DATABASE_ERROR);
410     }
411
412     MY_SQLITE_FINALIZE(query)
413
414     //now read back the sinkID
415     command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE name=?";
416     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
417     MY_SQLITE_BIND_TEXT(query, 1, sinkData.name.c_str(), sinkData.name.size(), SQLITE_STATIC)
418     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
419     {
420         sinkID = sqlite3_column_int(query, 0);
421     }
422     else
423     {
424         sinkID = 0;
425         logError("DatabaseHandler::existSink database error!:", eCode);
426         MY_SQLITE_FINALIZE(query)
427         return (E_DATABASE_ERROR);
428     }
429     MY_SQLITE_FINALIZE(query)
430
431     //now we need to create the additional tables:
432     command = "CREATE TABLE SinkConnectionFormat" + i2s(sinkID) + std::string("(soundFormat INTEGER)");
433     if (!this->sqQuery(command))
434         return (E_DATABASE_ERROR);
435     command = "CREATE TABLE SinkSoundProperty" + i2s(sinkID) + std::string("(soundPropertyType INTEGER, value INTEGER)");
436     if (!this->sqQuery(command))
437         return (E_DATABASE_ERROR);
438     command = "CREATE TABLE SinkNotificationConfiguration" + i2s(sinkID) + std::string("(type INTEGER, status INTEGER, parameter INTEGER)");
439     if (!this->sqQuery(command))
440         return (E_DATABASE_ERROR);
441
442     //fill ConnectionFormats
443     command = "INSERT INTO SinkConnectionFormat" + i2s(sinkID) + std::string("(soundFormat) VALUES (?)");
444     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
445     std::vector<am_ConnectionFormat_e>::const_iterator connectionFormatIterator = sinkData.listConnectionFormats.begin();
446     for (; connectionFormatIterator < sinkData.listConnectionFormats.end(); ++connectionFormatIterator)
447     {
448         MY_SQLITE_BIND_INT(query, 1, *connectionFormatIterator)
449         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
450         {
451             logError("DatabaseHandler::enterSinkDB SQLITE Step error code:", eCode);
452             MY_SQLITE_FINALIZE(query)
453             return (E_DATABASE_ERROR);
454         }
455         MY_SQLITE_RESET(query)
456     }
457
458     //Fill SinkSoundProperties
459     command = "INSERT INTO SinkSoundProperty" + i2s(sinkID) + std::string("(soundPropertyType,value) VALUES (?,?)");
460     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
461     std::vector<am_SoundProperty_s>::const_iterator SoundPropertyIterator = sinkData.listSoundProperties.begin();
462     for (; SoundPropertyIterator < sinkData.listSoundProperties.end(); ++SoundPropertyIterator)
463     {
464         MY_SQLITE_BIND_INT(query, 1, SoundPropertyIterator->type)
465         MY_SQLITE_BIND_INT(query, 2, SoundPropertyIterator->value)
466         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
467         {
468             logError("DatabaseHandler::enterSinkDB SQLITE Step error code:", eCode);
469             MY_SQLITE_FINALIZE(query)
470             return (E_DATABASE_ERROR);
471         }
472         MY_SQLITE_RESET(query)
473     }
474
475     //Fill NotificationConfigurations
476     command = "INSERT INTO SinkNotificationConfiguration" + i2s(sinkID) + std::string("(type,status,parameter) VALUES (?,?,?)");
477     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
478     std::vector<am_NotificationConfiguration_s>::const_iterator NotificationConfigurationIterator(sinkData.listNotificationConfigurations.begin());
479     for (; NotificationConfigurationIterator < sinkData.listNotificationConfigurations.end(); ++NotificationConfigurationIterator)
480     {
481         MY_SQLITE_BIND_INT(query, 1, NotificationConfigurationIterator->type)
482         MY_SQLITE_BIND_INT(query, 2, NotificationConfigurationIterator->status)
483         MY_SQLITE_BIND_INT(query, 3, NotificationConfigurationIterator->parameter)
484         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
485         {
486             logError("DatabaseHandler::enterSinkDB SQLITE Step error code:", eCode);
487             MY_SQLITE_FINALIZE(query)
488             return (E_DATABASE_ERROR);
489         }
490         MY_SQLITE_RESET(query)
491     }
492
493     if (sinkData.visible == true)
494     {
495         command = "CREATE TABLE SinkMainSoundProperty" + i2s(sinkID) + std::string("(soundPropertyType INTEGER, value INTEGER)");
496         if (!this->sqQuery(command))
497             return (E_DATABASE_ERROR);
498
499         //Fill MainSinkSoundProperties
500         command = "INSERT INTO SinkMainSoundProperty" + i2s(sinkID) + std::string("(soundPropertyType,value) VALUES (?,?)");
501         MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
502         std::vector<am_MainSoundProperty_s>::const_iterator mainSoundPropertyIterator = sinkData.listMainSoundProperties.begin();
503         for (; mainSoundPropertyIterator < sinkData.listMainSoundProperties.end(); ++mainSoundPropertyIterator)
504         {
505             MY_SQLITE_BIND_INT(query, 1, mainSoundPropertyIterator->type)
506             MY_SQLITE_BIND_INT(query, 2, mainSoundPropertyIterator->value)
507             if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
508             {
509                 logError("DatabaseHandler::enterSinkDB SQLITE Step error code:", eCode);
510                 MY_SQLITE_FINALIZE(query)
511                 return (E_DATABASE_ERROR);
512             }
513             MY_SQLITE_RESET(query)
514         }
515         MY_SQLITE_FINALIZE(query)
516
517         //now we got MainNotificationConfigurations as well
518         command = "CREATE TABLE SinkMainNotificationConfiguration" + i2s(sinkID) + std::string("(type INTEGER, status INTEGER, parameter INTEGER)");
519         if (!this->sqQuery(command))
520             return (E_DATABASE_ERROR);
521
522         command = "INSERT INTO SinkMainNotificationConfiguration" + i2s(sinkID) + std::string("(type,status,parameter) VALUES (?,?,?)");
523         MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
524         std::vector<am_NotificationConfiguration_s>::const_iterator mainNotificationConfigurationIterator(sinkData.listMainNotificationConfigurations.begin());
525         for (; mainNotificationConfigurationIterator < sinkData.listMainNotificationConfigurations.end(); ++mainNotificationConfigurationIterator)
526         {
527             MY_SQLITE_BIND_INT(query, 1, mainNotificationConfigurationIterator->type)
528             MY_SQLITE_BIND_INT(query, 2, mainNotificationConfigurationIterator->status)
529             MY_SQLITE_BIND_INT(query, 3, mainNotificationConfigurationIterator->parameter)
530             if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
531             {
532                 logError("DatabaseHandler::enterSinkDB SQLITE Step error code:", eCode);
533                 MY_SQLITE_FINALIZE(query)
534                 return (E_DATABASE_ERROR);
535             }
536             MY_SQLITE_RESET(query)
537         }
538         MY_SQLITE_FINALIZE(query)
539     }
540
541     logInfo("DatabaseHandler::enterSinkDB entered new sink with name", sinkData.name, "domainID:", sinkData.domainID, "classID:", sinkData.sinkClassID, "volume:", sinkData.volume, "assigned ID:", sinkID);
542     am_Sink_s sink = sinkData;
543     sink.sinkID = sinkID;
544     if (mpDatabaseObserver != NULL)
545         mpDatabaseObserver->newSink(sink);
546     return (E_OK);
547 }
548
549 am_Error_e CAmDatabaseHandler::enterCrossfaderDB(const am_Crossfader_s & crossfaderData, am_crossfaderID_t & crossfaderID)
550 {
551     assert(crossfaderData.crossfaderID<DYNAMIC_ID_BOUNDARY);
552     assert(crossfaderData.hotSink>=HS_UNKNOWN && crossfaderData.hotSink<=HS_MAX);
553     assert(!crossfaderData.name.empty());
554     assert(existSink(crossfaderData.sinkID_A));
555     assert(existSink(crossfaderData.sinkID_B));
556     assert(existSource(crossfaderData.sourceID));
557
558     sqlite3_stmt* query = NULL;
559     int eCode = 0;
560     std::string command;
561
562     //if gatewayData is zero and the first Static Sink was already entered, the ID is created
563     if (crossfaderData.crossfaderID == 0 && !mFirstStaticCrossfader)
564     {
565         command = "INSERT INTO " + std::string(CROSSFADER_TABLE) + "(name, sinkID_A, sinkID_B, sourceID, hotSink) VALUES (?,?,?,?,?)";
566     }
567     else
568     {
569         //check if the ID already exists
570         if (existcrossFader(crossfaderData.crossfaderID))
571             return (E_ALREADY_EXISTS);
572         command = "INSERT INTO " + std::string(CROSSFADER_TABLE) + "(name, sinkID_A, sinkID_B, sourceID, hotSink, crossfaderID) VALUES (?,?,?,?,?,?)";
573     }
574
575     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
576
577     MY_SQLITE_BIND_TEXT(query, 1, crossfaderData.name.c_str(), crossfaderData.name.size(), SQLITE_STATIC)
578     MY_SQLITE_BIND_INT(query, 2, crossfaderData.sinkID_A)
579     MY_SQLITE_BIND_INT(query, 3, crossfaderData.sinkID_B)
580     MY_SQLITE_BIND_INT(query, 4, crossfaderData.sourceID)
581     MY_SQLITE_BIND_INT(query, 5, crossfaderData.hotSink)
582
583     //if the ID is not created, we add it to the query
584     if (crossfaderData.crossfaderID != 0)
585     {
586         MY_SQLITE_BIND_INT(query, 6, crossfaderData.crossfaderID)
587     }
588
589     //if the first static sink is entered, we need to set it onto the boundary
590     else if (mFirstStaticCrossfader)
591     {
592         MY_SQLITE_BIND_INT(query, 6, DYNAMIC_ID_BOUNDARY)
593         mFirstStaticCrossfader = false;
594     }
595
596     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
597     {
598         logError("DatabaseHandler::enterCrossfaderDB SQLITE Step error code:", eCode);
599         MY_SQLITE_FINALIZE(query)
600         return (E_DATABASE_ERROR);
601     }
602
603     MY_SQLITE_FINALIZE(query)
604
605     //now read back the crossfaderID
606     command = "SELECT crossfaderID FROM " + std::string(CROSSFADER_TABLE) + " WHERE name=?";
607     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
608     MY_SQLITE_BIND_TEXT(query, 1, crossfaderData.name.c_str(), crossfaderData.name.size(), SQLITE_STATIC)
609     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
610     {
611         crossfaderID = sqlite3_column_int(query, 0);
612     }
613     else
614     {
615         crossfaderID = 0;
616         logError("DatabaseHandler::enterCrossfaderDB database error!:", eCode);
617         MY_SQLITE_FINALIZE(query)
618         return (E_DATABASE_ERROR);
619     }
620     MY_SQLITE_FINALIZE(query)
621
622     logInfo("DatabaseHandler::enterCrossfaderDB entered new crossfader with name=", crossfaderData.name, "sinkA= ", crossfaderData.sinkID_A, "sinkB=", crossfaderData.sinkID_B, "source=", crossfaderData.sourceID, "assigned ID:", crossfaderID);
623
624     am_Crossfader_s crossfader(crossfaderData);
625     crossfader.crossfaderID = crossfaderID;
626     if (mpDatabaseObserver)
627         mpDatabaseObserver->newCrossfader(crossfader);
628     return (E_OK);
629 }
630
631 am_Error_e CAmDatabaseHandler::enterGatewayDB(const am_Gateway_s & gatewayData, am_gatewayID_t & gatewayID)
632 {
633     assert(gatewayData.gatewayID<DYNAMIC_ID_BOUNDARY);
634     assert(gatewayData.sinkID!=0);
635     assert(gatewayData.sourceID!=0);
636     assert(gatewayData.controlDomainID!=0);
637     assert(gatewayData.domainSinkID!=0);
638     assert(gatewayData.domainSourceID!=0);
639     assert(!gatewayData.name.empty());
640     assert(!gatewayData.convertionMatrix.empty());
641     assert(!gatewayData.listSinkFormats.empty());
642     assert(!gatewayData.listSourceFormats.empty());
643
644     //might be that the sinks and sources are not there during registration time
645     //assert(existSink(gatewayData.sinkID));
646     //assert(existSource(gatewayData.sourceID));
647
648     sqlite3_stmt* query = NULL;
649     int eCode = 0;
650     std::string command;
651
652     //if gatewayData is zero and the first Static Sink was already entered, the ID is created
653     if (gatewayData.gatewayID == 0 && !mFirstStaticGateway)
654     {
655         command = "INSERT INTO " + std::string(GATEWAY_TABLE) + "(name, sinkID, sourceID, domainSinkID, domainSourceID, controlDomainID) VALUES (?,?,?,?,?,?)";
656     }
657     else
658     {
659         //check if the ID already exists
660         if (existGateway(gatewayData.gatewayID))
661             return (E_ALREADY_EXISTS);
662         command = "INSERT INTO " + std::string(GATEWAY_TABLE) + "(name, sinkID, sourceID, domainSinkID, domainSourceID, controlDomainID, gatewayID) VALUES (?,?,?,?,?,?,?)";
663     }
664
665     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
666     MY_SQLITE_BIND_TEXT(query, 1, gatewayData.name.c_str(), gatewayData.name.size(), SQLITE_STATIC)
667     MY_SQLITE_BIND_INT(query, 2, gatewayData.sinkID)
668     MY_SQLITE_BIND_INT(query, 3, gatewayData.sourceID)
669     MY_SQLITE_BIND_INT(query, 4, gatewayData.domainSinkID)
670     MY_SQLITE_BIND_INT(query, 5, gatewayData.domainSourceID)
671     MY_SQLITE_BIND_INT(query, 6, gatewayData.controlDomainID)
672
673     //if the ID is not created, we add it to the query
674     if (gatewayData.gatewayID != 0)
675     {
676         MY_SQLITE_BIND_INT(query, 7, gatewayData.gatewayID)
677     }
678
679     //if the first static sink is entered, we need to set it onto the boundary
680     else if (mFirstStaticGateway)
681     {
682         MY_SQLITE_BIND_INT(query, 7, DYNAMIC_ID_BOUNDARY)
683         mFirstStaticGateway = false;
684     }
685
686     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
687     {
688         logError("DatabaseHandler::enterGatewayDB SQLITE Step error code:", eCode);
689         MY_SQLITE_FINALIZE(query)
690         return (E_DATABASE_ERROR);
691     }
692
693     MY_SQLITE_FINALIZE(query)
694
695     gatewayID = sqlite3_last_insert_rowid(mpDatabase);
696
697     //now the convertion matrix todo: change the map implementation sometimes to blob in sqlite
698     mListConnectionFormat.insert(std::make_pair(gatewayID, gatewayData.convertionMatrix));
699
700     command = "CREATE TABLE GatewaySourceFormat" + i2s(gatewayID) + std::string("(soundFormat INTEGER)");
701     if (!this->sqQuery(command))
702         return (E_DATABASE_ERROR);
703     command = "CREATE TABLE GatewaySinkFormat" + i2s(gatewayID) + std::string("(soundFormat INTEGER)");
704     if (!this->sqQuery(command))
705         return (E_DATABASE_ERROR);
706
707     //fill ConnectionFormats
708     command = "INSERT INTO GatewaySourceFormat" + i2s(gatewayID) + std::string("(soundFormat) VALUES (?)");
709     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
710     std::vector<am_ConnectionFormat_e>::const_iterator connectionFormatIterator = gatewayData.listSourceFormats.begin();
711     for (; connectionFormatIterator < gatewayData.listSourceFormats.end(); ++connectionFormatIterator)
712     {
713         MY_SQLITE_BIND_INT(query, 1, *connectionFormatIterator)
714         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
715         {
716             logError("DatabaseHandler::enterGatewayDB SQLITE Step error code:", eCode);
717             MY_SQLITE_FINALIZE(query)
718             return (E_DATABASE_ERROR);
719         }
720         MY_SQLITE_RESET(query)
721     }
722     MY_SQLITE_FINALIZE(query)
723
724     command = "INSERT INTO GatewaySinkFormat" + i2s(gatewayID) + std::string("(soundFormat) VALUES (?)");
725     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
726     connectionFormatIterator = gatewayData.listSinkFormats.begin();
727     for (; connectionFormatIterator < gatewayData.listSinkFormats.end(); ++connectionFormatIterator)
728     {
729         MY_SQLITE_BIND_INT(query, 1, *connectionFormatIterator)
730         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
731         {
732             logError("DatabaseHandler::enterGatewayDB SQLITE Step error code:", eCode);
733             MY_SQLITE_FINALIZE(query)
734             return (E_DATABASE_ERROR);
735         }
736         MY_SQLITE_RESET(query)
737     }
738     MY_SQLITE_FINALIZE(query)
739
740     logInfo("DatabaseHandler::enterGatewayDB entered new gateway with name", gatewayData.name, "sourceID:", gatewayData.sourceID, "sinkID:", gatewayData.sinkID, "assigned ID:", gatewayID);
741     am_Gateway_s gateway = gatewayData;
742     gateway.gatewayID = gatewayID;
743     if (mpDatabaseObserver)
744         mpDatabaseObserver->newGateway(gateway);
745     return (E_OK);
746 }
747
748 am_Error_e CAmDatabaseHandler::enterSourceDB(const am_Source_s & sourceData, am_sourceID_t & sourceID)
749 {
750     assert(sourceData.sourceID<DYNAMIC_ID_BOUNDARY);
751     assert(sourceData.domainID!=0);
752     assert(!sourceData.name.empty());
753     assert(sourceData.sourceClassID!=0);
754     // \todo: need to check if class exists?
755     assert(!sourceData.listConnectionFormats.empty());
756     assert(sourceData.sourceState>=SS_UNKNNOWN && sourceData.sourceState<=SS_MAX);
757
758     sqlite3_stmt* query = NULL;
759     ;
760     int eCode = 0;
761     std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE name=? AND reserved=1";
762
763     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
764     MY_SQLITE_BIND_TEXT(query, 1, sourceData.name.c_str(), sourceData.name.size(), SQLITE_STATIC)
765
766     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
767     {
768         command = "UPDATE " + std::string(SOURCE_TABLE) + " SET name=?, domainID=?, sourceClassID=?, sourceState=?, volume=?, visible=?, availability=?, availabilityReason=?, interruptState=?, reserved=? WHERE sourceID=" + i2s(sqlite3_column_int(query, 0));
769     }
770     else if (eCode == SQLITE_DONE)
771     {
772         //if sinkID is zero and the first Static Sink was already entered, the ID is created
773         if (sourceData.sourceID == 0 && !mFirstStaticSource && !existSourceName(sourceData.name))
774         {
775             command = "INSERT INTO " + std::string(SOURCE_TABLE) + "(name, domainID, sourceClassID, sourceState, volume, visible, availability, availabilityReason, interruptState, reserved) VALUES (?,?,?,?,?,?,?,?,?,?)";
776         }
777         else
778         {
779             //check if the ID already exists
780             if (existSourceNameOrID(sourceData.sourceID, sourceData.name))
781             {
782                 MY_SQLITE_FINALIZE(query)
783                 return (E_ALREADY_EXISTS);
784             }
785             command = "INSERT INTO " + std::string(SOURCE_TABLE) + "(name, domainID, sourceClassID, sourceState, volume, visible, availability, availabilityReason, interruptState, reserved, sourceID) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
786         }
787     }
788     else
789     {
790         logError("DatabaseHandler::enterSourceDB SQLITE Step error code:", eCode);
791         MY_SQLITE_FINALIZE(query)
792         return (E_DATABASE_ERROR);
793     }
794
795     MY_SQLITE_FINALIZE(query)
796
797     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
798     MY_SQLITE_BIND_TEXT(query, 1, sourceData.name.c_str(), sourceData.name.size(), SQLITE_STATIC)
799     MY_SQLITE_BIND_INT(query, 2, sourceData.domainID)
800     MY_SQLITE_BIND_INT(query, 3, sourceData.sourceClassID)
801     MY_SQLITE_BIND_INT(query, 4, sourceData.sourceState)
802     MY_SQLITE_BIND_INT(query, 5, sourceData.volume)
803     MY_SQLITE_BIND_INT(query, 6, sourceData.visible)
804     MY_SQLITE_BIND_INT(query, 7, sourceData.available.availability)
805     MY_SQLITE_BIND_INT(query, 8, sourceData.available.availabilityReason)
806     MY_SQLITE_BIND_INT(query, 9, sourceData.interruptState)
807     MY_SQLITE_BIND_INT(query, 10, 0)
808
809     //if the ID is not created, we add it to the query
810     if (sourceData.sourceID != 0)
811     {
812         MY_SQLITE_BIND_INT(query, 11, sourceData.sourceID)
813     }
814
815     //if the first static sink is entered, we need to set it onto the boundary
816     else if (mFirstStaticSource)
817     {
818         MY_SQLITE_BIND_INT(query, 11, DYNAMIC_ID_BOUNDARY)
819         mFirstStaticSource = false;
820     }
821
822     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
823     {
824         logError("DatabaseHandler::enterSourceDB SQLITE Step error code:", eCode);
825         MY_SQLITE_FINALIZE(query)
826         return (E_DATABASE_ERROR);
827     }
828
829     MY_SQLITE_FINALIZE(query)
830
831     //now read back the sinkID
832     command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE name=?";
833     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
834     MY_SQLITE_BIND_TEXT(query, 1, sourceData.name.c_str(), sourceData.name.size(), SQLITE_STATIC)
835     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
836     {
837         sourceID = sqlite3_column_int(query, 0);
838     }
839     else
840     {
841         sourceID = 0;
842         logError("DatabaseHandler::existSink database error!:", eCode);
843         MY_SQLITE_FINALIZE(query)
844         return (E_DATABASE_ERROR);
845     }
846     MY_SQLITE_FINALIZE(query)
847
848     //now we need to create the additional tables:
849     command = "CREATE TABLE SourceConnectionFormat" + i2s(sourceID) + std::string("(soundFormat INTEGER)");
850     if (!this->sqQuery(command))
851         return (E_DATABASE_ERROR);
852     command = "CREATE TABLE SourceSoundProperty" + i2s(sourceID) + std::string("(soundPropertyType INTEGER, value INTEGER)");
853     if (!this->sqQuery(command))
854         return (E_DATABASE_ERROR);
855     command = "CREATE TABLE SourceNotificationConfiguration" + i2s(sourceID) + std::string("(type INTEGER, status INTEGER, parameter INTEGER)");
856         if (!this->sqQuery(command))
857             return (E_DATABASE_ERROR);
858
859     //fill ConnectionFormats
860     command = "INSERT INTO SourceConnectionFormat" + i2s(sourceID) + std::string("(soundFormat) VALUES (?)");
861     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
862     std::vector<am_ConnectionFormat_e>::const_iterator connectionFormatIterator = sourceData.listConnectionFormats.begin();
863     for (; connectionFormatIterator != sourceData.listConnectionFormats.end(); ++connectionFormatIterator)
864     {
865         MY_SQLITE_BIND_INT(query, 1, *connectionFormatIterator)
866         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
867         {
868             logError("DatabaseHandler::enterSourceDB SQLITE Step error code:", eCode);
869             MY_SQLITE_FINALIZE(query)
870             return (E_DATABASE_ERROR);
871         }
872         MY_SQLITE_RESET(query)
873     }
874     MY_SQLITE_FINALIZE(query)
875
876     //Fill SinkSoundProperties
877     command = "INSERT INTO SourceSoundProperty" + i2s(sourceID) + std::string("(soundPropertyType,value) VALUES (?,?)");
878     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
879     std::vector<am_SoundProperty_s>::const_iterator SoundPropertyIterator = sourceData.listSoundProperties.begin();
880     for (; SoundPropertyIterator != sourceData.listSoundProperties.end(); ++SoundPropertyIterator)
881     {
882         MY_SQLITE_BIND_INT(query, 1, SoundPropertyIterator->type)
883         MY_SQLITE_BIND_INT(query, 2, SoundPropertyIterator->value)
884         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
885         {
886             logError("DatabaseHandler::enterSinkDB SQLITE Step error code:", eCode);
887             MY_SQLITE_FINALIZE(query)
888             return (E_DATABASE_ERROR);
889         }
890         MY_SQLITE_RESET(query)
891     }
892
893     //Fill NotificationConfigurations
894     command = "INSERT INTO SourceNotificationConfiguration" + i2s(sourceID) + std::string("(type,status,parameter) VALUES (?,?,?)");
895     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
896     std::vector<am_NotificationConfiguration_s>::const_iterator NotificationConfigurationIterator(sourceData.listNotificationConfigurations.begin());
897     for (; NotificationConfigurationIterator < sourceData.listNotificationConfigurations.end(); ++NotificationConfigurationIterator)
898     {
899         MY_SQLITE_BIND_INT(query, 1, NotificationConfigurationIterator->type)
900         MY_SQLITE_BIND_INT(query, 2, NotificationConfigurationIterator->status)
901         MY_SQLITE_BIND_INT(query, 3, NotificationConfigurationIterator->parameter)
902         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
903         {
904             logError("DatabaseHandler::enterSinkDB SQLITE Step error code:", eCode);
905             MY_SQLITE_FINALIZE(query)
906             return (E_DATABASE_ERROR);
907         }
908         MY_SQLITE_RESET(query)
909     }
910
911     if (sourceData.visible == true)
912     {
913         command = "CREATE TABLE SourceMainSoundProperty" + i2s(sourceID) + std::string("(soundPropertyType INTEGER, value INTEGER)");
914         if (!this->sqQuery(command))
915             return (E_DATABASE_ERROR);
916
917         //Fill MainSinkSoundProperties
918         command = "INSERT INTO SourceMainSoundProperty" + i2s(sourceID) + std::string("(soundPropertyType,value) VALUES (?,?)");
919         MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
920         std::vector<am_MainSoundProperty_s>::const_iterator mainSoundPropertyIterator = sourceData.listMainSoundProperties.begin();
921         for (; mainSoundPropertyIterator != sourceData.listMainSoundProperties.end(); ++mainSoundPropertyIterator)
922         {
923             MY_SQLITE_BIND_INT(query, 1, mainSoundPropertyIterator->type)
924             MY_SQLITE_BIND_INT(query, 2, mainSoundPropertyIterator->value)
925             if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
926             {
927                 logError("DatabaseHandler::enterSourceDB SQLITE Step error code:", eCode);
928                 MY_SQLITE_FINALIZE(query)
929                 return (E_DATABASE_ERROR);
930             }
931             MY_SQLITE_RESET(query)
932         }
933         MY_SQLITE_FINALIZE(query)
934
935         //now we got MainNotificationConfigurations as well
936         command = "CREATE TABLE SourceMainNotificationConfiguration" + i2s(sourceID) + std::string("(type INTEGER, status INTEGER, parameter INTEGER)");
937         if (!this->sqQuery(command))
938             return (E_DATABASE_ERROR);
939
940         command = "INSERT INTO SourceMainNotificationConfiguration" + i2s(sourceID) + std::string("(type,status,parameter) VALUES (?,?,?)");
941         MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
942         std::vector<am_NotificationConfiguration_s>::const_iterator mainNotificationConfigurationIterator(sourceData.listMainNotificationConfigurations.begin());
943         for (; mainNotificationConfigurationIterator != sourceData.listMainNotificationConfigurations.end(); ++mainNotificationConfigurationIterator)
944         {
945             MY_SQLITE_BIND_INT(query, 1, mainNotificationConfigurationIterator->type)
946             MY_SQLITE_BIND_INT(query, 2, mainNotificationConfigurationIterator->status)
947             MY_SQLITE_BIND_INT(query, 3, mainNotificationConfigurationIterator->parameter)
948             if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
949             {
950                 logError("DatabaseHandler::enterSinkDB SQLITE Step error code:", eCode);
951                 MY_SQLITE_FINALIZE(query)
952                 return (E_DATABASE_ERROR);
953             }
954             MY_SQLITE_RESET(query)
955         }
956         MY_SQLITE_FINALIZE(query)
957     }
958
959     logInfo("DatabaseHandler::enterSourceDB entered new source with name", sourceData.name, "domainID:", sourceData.domainID, "classID:", sourceData.sourceClassID, "visible:", sourceData.visible, "assigned ID:", sourceID);
960
961     am_Source_s source = sourceData;
962     source.sourceID = sourceID;
963     if (mpDatabaseObserver)
964         mpDatabaseObserver->newSource(source);
965     return (E_OK);
966 }
967
968 am_Error_e CAmDatabaseHandler::changeMainConnectionRouteDB(const am_mainConnectionID_t mainconnectionID, const std::vector<am_connectionID_t>& listConnectionID)
969 {
970     assert(mainconnectionID!=0);
971     if (!existMainConnection(mainconnectionID))
972     {
973         return (E_NON_EXISTENT);
974     }
975     sqlite3_stmt* query = NULL;
976     int eCode = 0;
977     std::string command;
978
979     int16_t delay = 0;
980     command = "SELECT delay FROM " + std::string(CONNECTION_TABLE) + (" WHERE connectionID=?");
981     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
982     std::vector<am_connectionID_t>::const_iterator elementIterator = listConnectionID.begin();
983     for (; elementIterator < listConnectionID.end(); ++elementIterator)
984     {
985         MY_SQLITE_BIND_INT(query, 1, *elementIterator)
986
987         if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
988         {
989             int16_t temp_delay = sqlite3_column_int(query, 1);
990             if (temp_delay != -1 && delay != -1)
991                 delay += temp_delay;
992             else
993                 delay = -1;
994         }
995         else
996         {
997             logError("DatabaseHandler::changeMainConnectionRouteDB did not find route for MainConnection: ", eCode);
998             MY_SQLITE_FINALIZE(query)
999             return (E_DATABASE_ERROR);
1000         }
1001         MY_SQLITE_RESET(query)
1002     }
1003
1004     MY_SQLITE_FINALIZE(query)
1005
1006     //now we delete the data in the table
1007     command = "DELETE from MainConnectionRoute" + i2s(mainconnectionID);
1008     if (!this->sqQuery(command))
1009         return (E_DATABASE_ERROR);
1010
1011     command = "INSERT INTO MainConnectionRoute" + i2s(mainconnectionID) + "(connectionID) VALUES (?)";
1012     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1013     std::vector<am_connectionID_t>::const_iterator listConnectionIterator(listConnectionID.begin());
1014     for (; listConnectionIterator != listConnectionID.end(); ++listConnectionIterator)
1015     {
1016         MY_SQLITE_BIND_INT(query, 1, *listConnectionIterator)
1017         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1018         {
1019             logError("DatabaseHandler::changeMainConnectionRouteDB SQLITE Step error code:", eCode);
1020             MY_SQLITE_FINALIZE(query)
1021             return (E_DATABASE_ERROR);
1022         }
1023         MY_SQLITE_RESET(query)
1024     }
1025
1026     if (changeDelayMainConnection(delay,mainconnectionID)!=E_OK)
1027         logError("DatabaseHandler::changeMainConnectionRouteDB error while changing mainConnectionDelay to ", delay);
1028
1029     MY_SQLITE_FINALIZE(query)
1030     logInfo("DatabaseHandler::changeMainConnectionRouteDB entered new route:", mainconnectionID);
1031     return (E_OK);
1032 }
1033
1034 am_Error_e CAmDatabaseHandler::changeMainConnectionStateDB(const am_mainConnectionID_t mainconnectionID, const am_ConnectionState_e connectionState)
1035 {
1036     assert(mainconnectionID!=0);
1037     assert(connectionState>=CS_UNKNOWN && connectionState<=CS_MAX);
1038
1039     sqlite3_stmt* query = NULL;
1040     int eCode = 0;
1041     std::string command;
1042
1043     if (!existMainConnection(mainconnectionID))
1044     {
1045         return (E_NON_EXISTENT);
1046     }
1047     command = "UPDATE " + std::string(MAINCONNECTION_TABLE) + " SET connectionState=? WHERE mainConnectionID=" + i2s(mainconnectionID);
1048     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1049     MY_SQLITE_BIND_INT(query, 1, connectionState)
1050     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1051     {
1052         logError("DatabaseHandler::changeMainConnectionStateDB SQLITE Step error code:", eCode);
1053         MY_SQLITE_FINALIZE(query)
1054         return (E_DATABASE_ERROR);
1055     }
1056     MY_SQLITE_FINALIZE(query)
1057     logInfo("DatabaseHandler::changeMainConnectionStateDB changed mainConnectionState of MainConnection:", mainconnectionID, "to:", connectionState);
1058
1059     if (mpDatabaseObserver)
1060         mpDatabaseObserver->mainConnectionStateChanged(mainconnectionID, connectionState);
1061     return (E_OK);
1062 }
1063
1064 am_Error_e CAmDatabaseHandler::changeSinkMainVolumeDB(const am_mainVolume_t mainVolume, const am_sinkID_t sinkID)
1065 {
1066     assert(sinkID!=0);
1067
1068     sqlite3_stmt* query = NULL;
1069     int eCode = 0;
1070     std::string command;
1071
1072     if (!existSink(sinkID))
1073     {
1074         return (E_NON_EXISTENT);
1075     }
1076     command = "UPDATE " + std::string(SINK_TABLE) + " SET mainVolume=? WHERE sinkID=" + i2s(sinkID);
1077     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1078     MY_SQLITE_BIND_INT(query, 1, mainVolume)
1079     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1080     {
1081         logError("DatabaseHandler::changeSinkMainVolumeDB SQLITE Step error code:", eCode);
1082         MY_SQLITE_FINALIZE(query)
1083         return (E_DATABASE_ERROR);
1084     }
1085
1086     MY_SQLITE_FINALIZE(query)
1087     logInfo("DatabaseHandler::changeSinkMainVolumeDB changed mainVolume of sink:", sinkID, "to:", mainVolume);
1088
1089     if (mpDatabaseObserver)
1090         mpDatabaseObserver->volumeChanged(sinkID, mainVolume);
1091
1092     return (E_OK);
1093 }
1094
1095 am_Error_e CAmDatabaseHandler::changeSinkAvailabilityDB(const am_Availability_s & availability, const am_sinkID_t sinkID)
1096 {
1097     assert(sinkID!=0);
1098     assert(availability.availability>=A_UNKNOWN && availability.availability<=A_MAX);
1099     assert(availability.availabilityReason>=AR_UNKNOWN && availability.availabilityReason<=AR_MAX);
1100
1101     sqlite3_stmt* query = NULL;
1102     int eCode = 0;
1103     std::string command;
1104
1105     if (!existSink(sinkID))
1106     {
1107         return (E_NON_EXISTENT);
1108     }
1109     command = "UPDATE " + std::string(SINK_TABLE) + " SET availability=?, availabilityReason=? WHERE sinkID=" + i2s(sinkID);
1110     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1111     MY_SQLITE_BIND_INT(query, 1, availability.availability)
1112     MY_SQLITE_BIND_INT(query, 2, availability.availabilityReason)
1113     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1114     {
1115         logError("DatabaseHandler::changeSinkAvailabilityDB SQLITE Step error code:", eCode);
1116         MY_SQLITE_FINALIZE(query)
1117         return (E_DATABASE_ERROR);
1118     }
1119     assert(sinkID!=0);
1120     MY_SQLITE_FINALIZE(query)
1121     logInfo("DatabaseHandler::changeSinkAvailabilityDB changed sinkAvailability of sink:", sinkID, "to:", availability.availability, "Reason:", availability.availabilityReason);
1122
1123     if (mpDatabaseObserver && sourceVisible(sinkID))
1124         mpDatabaseObserver->sinkAvailabilityChanged(sinkID, availability);
1125     return (E_OK);
1126 }
1127
1128 am_Error_e CAmDatabaseHandler::changDomainStateDB(const am_DomainState_e domainState, const am_domainID_t domainID)
1129 {
1130     assert(domainID!=0);
1131     assert(domainState>=DS_UNKNOWN && domainState<=DS_MAX);
1132
1133     sqlite3_stmt* query = NULL;
1134     int eCode = 0;
1135     std::string command;
1136
1137     if (!existDomain(domainID))
1138     {
1139         return (E_NON_EXISTENT);
1140     }
1141     command = "UPDATE " + std::string(DOMAIN_TABLE) + " SET state=? WHERE domainID=" + i2s(domainID);
1142     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1143     MY_SQLITE_BIND_INT(query, 1, domainState)
1144     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1145     {
1146         logError("DatabaseHandler::changDomainStateDB SQLITE Step error code:", eCode);
1147         MY_SQLITE_FINALIZE(query)
1148         return (E_DATABASE_ERROR);
1149     }
1150
1151     MY_SQLITE_FINALIZE(query)
1152
1153     logInfo("DatabaseHandler::changDomainStateDB changed domainState of domain:", domainID, "to:", domainState);
1154     return (E_OK);
1155 }
1156
1157 am_Error_e CAmDatabaseHandler::changeSinkMuteStateDB(const am_MuteState_e muteState, const am_sinkID_t sinkID)
1158 {
1159     assert(sinkID!=0);
1160     assert(muteState>=MS_UNKNOWN && muteState<=MS_MAX);
1161
1162     sqlite3_stmt* query = NULL;
1163     int eCode = 0;
1164     std::string command;
1165
1166     if (!existSink(sinkID))
1167     {
1168         return (E_NON_EXISTENT);
1169     }
1170     command = "UPDATE " + std::string(SINK_TABLE) + " SET muteState=? WHERE sinkID=" + i2s(sinkID);
1171     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1172     MY_SQLITE_BIND_INT(query, 1, muteState)
1173     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1174     {
1175         logError("DatabaseHandler::changeSinkMuteStateDB SQLITE Step error code:", eCode);
1176         MY_SQLITE_FINALIZE(query)
1177         return (E_DATABASE_ERROR);
1178     }
1179     assert(sinkID!=0);
1180     MY_SQLITE_FINALIZE(query)
1181     logInfo("DatabaseHandler::changeSinkMuteStateDB changed sinkMuteState of sink:", sinkID, "to:", muteState);
1182
1183     if (mpDatabaseObserver)
1184         mpDatabaseObserver->sinkMuteStateChanged(sinkID, muteState);
1185
1186     return (E_OK);
1187 }
1188
1189 am_Error_e CAmDatabaseHandler::changeMainSinkSoundPropertyDB(const am_MainSoundProperty_s & soundProperty, const am_sinkID_t sinkID)
1190 {
1191     assert(soundProperty.type>=MSP_UNKNOWN && soundProperty.type<=MSP_MAX);
1192     assert(sinkID!=0);
1193
1194     sqlite3_stmt* query = NULL;
1195     int eCode = 0;
1196     std::string command;
1197
1198     if (!existSink(sinkID))
1199     {
1200         return (E_NON_EXISTENT);
1201     }
1202     command = "UPDATE SinkMainSoundProperty" + i2s(sinkID) + " SET value=? WHERE soundPropertyType=" + i2s(soundProperty.type);
1203     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1204     MY_SQLITE_BIND_INT(query, 1, soundProperty.value)
1205     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1206     {
1207         logError("DatabaseHandler::changeMainSinkSoundPropertyDB SQLITE Step error code:", eCode);
1208         MY_SQLITE_FINALIZE(query)
1209         return (E_DATABASE_ERROR);
1210     }
1211     assert(sinkID!=0);
1212     MY_SQLITE_FINALIZE(query)
1213     logInfo("DatabaseHandler::changeMainSinkSoundPropertyDB changed MainSinkSoundProperty of sink:", sinkID, "type:", soundProperty.type, "to:", soundProperty.value);
1214     if (mpDatabaseObserver)
1215         mpDatabaseObserver->mainSinkSoundPropertyChanged(sinkID, soundProperty);
1216     return (E_OK);
1217 }
1218
1219 am_Error_e CAmDatabaseHandler::changeMainSourceSoundPropertyDB(const am_MainSoundProperty_s & soundProperty, const am_sourceID_t sourceID)
1220 {
1221     assert(soundProperty.type>=MSP_UNKNOWN && soundProperty.type<=MSP_MAX);
1222     assert(sourceID!=0);
1223
1224     sqlite3_stmt* query = NULL;
1225     int eCode = 0;
1226     std::string command;
1227
1228     if (!existSource(sourceID))
1229     {
1230         return (E_NON_EXISTENT);
1231     }
1232     command = "UPDATE SourceMainSoundProperty" + i2s(sourceID) + " SET value=? WHERE soundPropertyType=" + i2s(soundProperty.type);
1233     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1234     MY_SQLITE_BIND_INT(query, 1, soundProperty.value)
1235     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1236     {
1237         logError("DatabaseHandler::changeMainSourceSoundPropertyDB SQLITE Step error code:", eCode);
1238         MY_SQLITE_FINALIZE(query)
1239         return (E_DATABASE_ERROR);
1240     }
1241     MY_SQLITE_FINALIZE(query)
1242
1243     logInfo("DatabaseHandler::changeMainSourceSoundPropertyDB changed MainSinkSoundProperty of source:", sourceID, "type:", soundProperty.type, "to:", soundProperty.value);
1244
1245     if (mpDatabaseObserver)
1246         mpDatabaseObserver->mainSourceSoundPropertyChanged(sourceID, soundProperty);
1247     return (E_OK);
1248 }
1249
1250 am_Error_e CAmDatabaseHandler::changeSourceAvailabilityDB(const am_Availability_s & availability, const am_sourceID_t sourceID)
1251 {
1252     assert(sourceID!=0);
1253     assert(availability.availability>=A_UNKNOWN && availability.availability<=A_MAX);
1254     assert(availability.availabilityReason>=AR_UNKNOWN && availability.availabilityReason<=AR_MAX);
1255
1256     sqlite3_stmt* query = NULL;
1257     int eCode = 0;
1258     std::string command;
1259
1260     if (!existSource(sourceID))
1261     {
1262         return (E_NON_EXISTENT);
1263     }
1264     command = "UPDATE " + std::string(SOURCE_TABLE) + " SET availability=?, availabilityReason=? WHERE sourceID=" + i2s(sourceID);
1265     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1266     MY_SQLITE_BIND_INT(query, 1, availability.availability)
1267     MY_SQLITE_BIND_INT(query, 2, availability.availabilityReason)
1268     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1269     {
1270         logError("DatabaseHandler::changeSourceAvailabilityDB SQLITE Step error code:", eCode);
1271         MY_SQLITE_FINALIZE(query)
1272         return (E_DATABASE_ERROR);
1273     }
1274
1275     MY_SQLITE_FINALIZE(query)
1276
1277     logInfo("DatabaseHandler::changeSourceAvailabilityDB changed changeSourceAvailabilityDB of source:", sourceID, "to:", availability.availability, "Reason:", availability.availabilityReason);
1278
1279     if (mpDatabaseObserver && sourceVisible(sourceID))
1280         mpDatabaseObserver->sourceAvailabilityChanged(sourceID, availability);
1281     return (E_OK);
1282 }
1283
1284 am_Error_e CAmDatabaseHandler::changeSystemPropertyDB(const am_SystemProperty_s & property)
1285 {
1286     assert(property.type>=SYP_UNKNOWN && property.type<=SYP_MAX);
1287     sqlite3_stmt* query = NULL;
1288     int eCode = 0;
1289     std::string command = "UPDATE " + std::string(SYSTEM_TABLE) + " set value=? WHERE type=?";
1290
1291     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1292     MY_SQLITE_BIND_INT(query, 1, property.value)
1293     MY_SQLITE_BIND_INT(query, 2, property.type)
1294
1295     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1296     {
1297         logError("DatabaseHandler::changeSystemPropertyDB SQLITE Step error code:", eCode);
1298         MY_SQLITE_FINALIZE(query)
1299         return (E_DATABASE_ERROR);
1300     }
1301
1302     MY_SQLITE_FINALIZE(query)
1303
1304     logInfo("DatabaseHandler::changeSystemPropertyDB changed system property");
1305
1306     if (mpDatabaseObserver)
1307         mpDatabaseObserver->systemPropertyChanged(property);
1308
1309     return (E_OK);
1310 }
1311
1312 am_Error_e CAmDatabaseHandler::removeMainConnectionDB(const am_mainConnectionID_t mainConnectionID)
1313 {
1314     assert(mainConnectionID!=0);
1315
1316     if (!existMainConnection(mainConnectionID))
1317     {
1318         return (E_NON_EXISTENT);
1319     }
1320     std::string command = "DELETE from " + std::string(MAINCONNECTION_TABLE) + " WHERE mainConnectionID=" + i2s(mainConnectionID);
1321     std::string command1 = "DROP table MainConnectionRoute" + i2s(mainConnectionID);
1322     if (!sqQuery(command))
1323         return (E_DATABASE_ERROR);
1324     if (!sqQuery(command1))
1325         return (E_DATABASE_ERROR);
1326     logInfo("DatabaseHandler::removeMainConnectionDB removed:", mainConnectionID);
1327     if (mpDatabaseObserver)
1328     {
1329         mpDatabaseObserver->mainConnectionStateChanged(mainConnectionID, CS_DISCONNECTED);
1330         mpDatabaseObserver->removedMainConnection(mainConnectionID);
1331     }
1332     return (E_OK);
1333 }
1334
1335 am_Error_e CAmDatabaseHandler::removeSinkDB(const am_sinkID_t sinkID)
1336 {
1337     assert(sinkID!=0);
1338
1339     if (!existSink(sinkID))
1340     {
1341         return (E_NON_EXISTENT);
1342     }
1343
1344     bool visible = sinkVisible(sinkID);
1345
1346     std::string command = "DELETE from " + std::string(SINK_TABLE) + " WHERE sinkID=" + i2s(sinkID);
1347     std::string command1 = "DROP table SinkConnectionFormat" + i2s(sinkID);
1348     std::string command2 = "DROP table SinkSoundProperty" + i2s(sinkID);
1349     std::string command3 = "DROP table SinkMainSoundProperty" + i2s(sinkID);
1350     std::string command4 = "DROP table SinkNotificationConfiguration" + i2s(sinkID);
1351     std::string command5 = "DROP table SinkMainNotificationConfiguration" + i2s(sinkID);
1352     if (!sqQuery(command))
1353         return (E_DATABASE_ERROR);
1354     if (!sqQuery(command1))
1355         return (E_DATABASE_ERROR);
1356     if (!sqQuery(command2))
1357         return (E_DATABASE_ERROR);
1358     if (!sqQuery(command4))
1359         return (E_DATABASE_ERROR);
1360     if (visible) //only drop table if it ever existed
1361     {
1362         if (!sqQuery(command3))
1363             return (E_DATABASE_ERROR);
1364         if (!sqQuery(command5))
1365             return (E_DATABASE_ERROR);
1366     }
1367     logInfo("DatabaseHandler::removeSinkDB removed:", sinkID);
1368
1369     if (mpDatabaseObserver != NULL)
1370         mpDatabaseObserver->removedSink(sinkID, visible);
1371
1372     return (E_OK);
1373 }
1374
1375 am_Error_e CAmDatabaseHandler::removeSourceDB(const am_sourceID_t sourceID)
1376 {
1377     assert(sourceID!=0);
1378
1379     if (!existSource(sourceID))
1380     {
1381         return (E_NON_EXISTENT);
1382     }
1383
1384     bool visible = sourceVisible(sourceID);
1385
1386     std::string command = "DELETE from " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + i2s(sourceID);
1387     std::string command1 = "DROP table SourceConnectionFormat" + i2s(sourceID);
1388     std::string command2 = "DROP table SourceMainSoundProperty" + i2s(sourceID);
1389     std::string command3 = "DROP table SourceSoundProperty" + i2s(sourceID);
1390     std::string command4 = "DROP table SourceNotificationConfiguration" + i2s(sourceID);
1391     std::string command5 = "DROP table SourceMainNotificationConfiguration" + i2s(sourceID);
1392     if (!sqQuery(command))
1393         return (E_DATABASE_ERROR);
1394     if (!sqQuery(command1))
1395         return (E_DATABASE_ERROR);
1396     if (!sqQuery(command3))
1397         return (E_DATABASE_ERROR);
1398     if (!sqQuery(command4))
1399         return (E_DATABASE_ERROR);
1400
1401     if(visible)
1402     {
1403         if (!sqQuery(command2))
1404             return (E_DATABASE_ERROR);
1405         if (!sqQuery(command5))
1406             return (E_DATABASE_ERROR);
1407     }
1408     logInfo("DatabaseHandler::removeSourceDB removed:", sourceID);
1409     if (mpDatabaseObserver)
1410         mpDatabaseObserver->removedSource(sourceID, visible);
1411     return (E_OK);
1412 }
1413
1414 am_Error_e CAmDatabaseHandler::removeGatewayDB(const am_gatewayID_t gatewayID)
1415 {
1416     assert(gatewayID!=0);
1417
1418     if (!existGateway(gatewayID))
1419     {
1420         return (E_NON_EXISTENT);
1421     }
1422     std::string command = "DELETE from " + std::string(GATEWAY_TABLE) + " WHERE gatewayID=" + i2s(gatewayID);
1423     if (!sqQuery(command))
1424         return (E_DATABASE_ERROR);
1425     logInfo("DatabaseHandler::removeGatewayDB removed:", gatewayID);
1426     if (mpDatabaseObserver)
1427         mpDatabaseObserver->removeGateway(gatewayID);
1428     return (E_OK);
1429 }
1430
1431 am_Error_e CAmDatabaseHandler::removeCrossfaderDB(const am_crossfaderID_t crossfaderID)
1432 {
1433     assert(crossfaderID!=0);
1434
1435     if (!existcrossFader(crossfaderID))
1436     {
1437         return (E_NON_EXISTENT);
1438     }
1439     std::string command = "DELETE from " + std::string(CROSSFADER_TABLE) + " WHERE crossfaderID=" + i2s(crossfaderID);
1440     if (!sqQuery(command))
1441         return (E_DATABASE_ERROR);
1442     logInfo("DatabaseHandler::removeCrossfaderDB removed:", crossfaderID);
1443     if (mpDatabaseObserver)
1444         mpDatabaseObserver->removeCrossfader(crossfaderID);
1445     return (E_OK);
1446 }
1447
1448 am_Error_e CAmDatabaseHandler::removeDomainDB(const am_domainID_t domainID)
1449 {
1450     assert(domainID!=0);
1451
1452     if (!existDomain(domainID))
1453     {
1454         return (E_NON_EXISTENT);
1455     }
1456     std::string command = "DELETE from " + std::string(DOMAIN_TABLE) + " WHERE domainID=" + i2s(domainID);
1457     if (!sqQuery(command))
1458         return (E_DATABASE_ERROR);
1459     logInfo("DatabaseHandler::removeDomainDB removed:", domainID);
1460     if (mpDatabaseObserver)
1461         mpDatabaseObserver->removeDomain(domainID);
1462     return (E_OK);
1463 }
1464
1465 am_Error_e CAmDatabaseHandler::removeSinkClassDB(const am_sinkClass_t sinkClassID)
1466 {
1467     assert(sinkClassID!=0);
1468
1469     if (!existSinkClass(sinkClassID))
1470     {
1471         return (E_NON_EXISTENT);
1472     }
1473     std::string command = "DELETE from " + std::string(SINK_CLASS_TABLE) + " WHERE sinkClassID=" + i2s(sinkClassID);
1474     std::string command1 = "DROP table SinkClassProperties" + i2s(sinkClassID);
1475     if (!sqQuery(command))
1476         return (E_DATABASE_ERROR);
1477     if (!sqQuery(command1))
1478         return (E_DATABASE_ERROR);
1479
1480     logInfo("DatabaseHandler::removeSinkClassDB removed:", sinkClassID);
1481     if (mpDatabaseObserver)
1482         mpDatabaseObserver->numberOfSinkClassesChanged();
1483
1484     return (E_OK);
1485 }
1486
1487 am_Error_e CAmDatabaseHandler::removeSourceClassDB(const am_sourceClass_t sourceClassID)
1488 {
1489     assert(sourceClassID!=0);
1490
1491     if (!existSourceClass(sourceClassID))
1492     {
1493         return (E_NON_EXISTENT);
1494     }
1495     std::string command = "DELETE from " + std::string(SOURCE_CLASS_TABLE) + " WHERE sourceClassID=" + i2s(sourceClassID);
1496     std::string command1 = "DROP table SourceClassProperties" + i2s(sourceClassID);
1497     if (!sqQuery(command))
1498         return (E_DATABASE_ERROR);
1499     if (!sqQuery(command1))
1500         return (E_DATABASE_ERROR);
1501     logInfo("DatabaseHandler::removeSourceClassDB removed:", sourceClassID);
1502     if (mpDatabaseObserver)
1503         mpDatabaseObserver->numberOfSourceClassesChanged();
1504     return (E_OK);
1505 }
1506
1507 am_Error_e CAmDatabaseHandler::removeConnection(const am_connectionID_t connectionID)
1508 {
1509     assert(connectionID!=0);
1510
1511     std::string command = "DELETE from " + std::string(CONNECTION_TABLE) + " WHERE connectionID=" + i2s(connectionID);
1512     if (!sqQuery(command))
1513         return (E_DATABASE_ERROR);
1514     logInfo("DatabaseHandler::removeConnection removed:", connectionID);
1515     return (E_OK);
1516 }
1517
1518 am_Error_e CAmDatabaseHandler::getSourceClassInfoDB(const am_sourceID_t sourceID, am_SourceClass_s & classInfo) const
1519 {
1520     assert(sourceID!=0);
1521
1522     if (!existSource(sourceID))
1523     {
1524         return (E_NON_EXISTENT);
1525     }
1526     sqlite3_stmt* query = NULL;
1527     int eCode = 0;
1528     am_ClassProperty_s propertyTemp;
1529     std::string command = "SELECT sourceClassID FROM " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + (i2s(sourceID));
1530     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1531
1532     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1533     {
1534         classInfo.sourceClassID = sqlite3_column_int(query, 0);
1535     }
1536
1537     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1538     {
1539         logError("DatabaseHandler::getSourceClassInfoDB SQLITE error code:", eCode);
1540         MY_SQLITE_FINALIZE(query)
1541         return (E_DATABASE_ERROR);
1542     }
1543
1544     MY_SQLITE_FINALIZE(query)
1545
1546     command = "SELECT name FROM " + std::string(SOURCE_CLASS_TABLE) + " WHERE sourceClassID=" + (i2s(classInfo.sourceClassID));
1547     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1548
1549     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1550     {
1551         classInfo.name = std::string((const char*) sqlite3_column_text(query, 0));
1552     }
1553
1554     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1555     {
1556         logError("DatabaseHandler::getSourceClassInfoDB SQLITE error code:", eCode);
1557         MY_SQLITE_FINALIZE(query)
1558         return (E_DATABASE_ERROR);
1559     }
1560
1561     MY_SQLITE_FINALIZE(query)
1562
1563     //read out Properties
1564     command = "SELECT classProperty, value FROM SourceClassProperties" + i2s(classInfo.sourceClassID);
1565     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1566     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1567     {
1568         propertyTemp.classProperty = (am_ClassProperty_e) sqlite3_column_int(query, 0);
1569         propertyTemp.value = sqlite3_column_int(query, 1);
1570         classInfo.listClassProperties.push_back(propertyTemp);
1571     }
1572
1573     if (eCode != SQLITE_DONE)
1574     {
1575         logError("DatabaseHandler::getSourceClassInfoDB SQLITE error code:", eCode);
1576         MY_SQLITE_FINALIZE(query)
1577         return (E_DATABASE_ERROR);
1578     }
1579
1580     MY_SQLITE_FINALIZE(query)
1581     return (E_OK);
1582 }
1583
1584 am_Error_e CAmDatabaseHandler::getSinkInfoDB(const am_sinkID_t sinkID, am_Sink_s & sinkData) const
1585 {
1586
1587     assert(sinkID!=0);
1588
1589     if (!existSink(sinkID))
1590     {
1591         return (E_NON_EXISTENT);
1592     }
1593
1594     sqlite3_stmt* query = NULL, *qConnectionFormat = NULL, *qSoundProperty = NULL, *qMAinSoundProperty = NULL, *qMainNotification = NULL, *qNotification = NULL;
1595     int eCode = 0;
1596     am_ConnectionFormat_e tempConnectionFormat;
1597     am_SoundProperty_s tempSoundProperty;
1598     am_MainSoundProperty_s tempMainSoundProperty;
1599     am_NotificationConfiguration_s tempNotificationConfiguration,tempMainNotification;
1600     std::string command = "SELECT name, domainID, sinkClassID, volume, visible, availability, availabilityReason, muteState, mainVolume, sinkID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 and sinkID=" + i2s(sinkID);
1601     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1602
1603     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1604     {
1605         sinkData.name = std::string((const char*) sqlite3_column_text(query, 0));
1606         sinkData.domainID = sqlite3_column_int(query, 1);
1607         sinkData.sinkClassID = sqlite3_column_int(query, 2);
1608         sinkData.volume = sqlite3_column_int(query, 3);
1609         sinkData.visible = sqlite3_column_int(query, 4);
1610         sinkData.available.availability = (am_Availability_e) sqlite3_column_int(query, 5);
1611         sinkData.available.availabilityReason = (am_AvailabilityReason_e) sqlite3_column_int(query, 6);
1612         sinkData.muteState = (am_MuteState_e) sqlite3_column_int(query, 7);
1613         sinkData.mainVolume = sqlite3_column_int(query, 8);
1614         sinkData.sinkID = sqlite3_column_int(query, 9);
1615
1616         //read out the connectionFormats
1617         std::string commandConnectionFormat = "SELECT soundFormat FROM SinkConnectionFormat" + i2s(sinkID);
1618         MY_SQLITE_PREPARE_V2(mpDatabase, commandConnectionFormat.c_str(), -1, &qConnectionFormat, NULL)
1619         while ((eCode = sqlite3_step(qConnectionFormat)) == SQLITE_ROW)
1620         {
1621             tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qConnectionFormat, 0);
1622             sinkData.listConnectionFormats.push_back(tempConnectionFormat);
1623         }
1624
1625         MY_SQLITE_FINALIZE(qConnectionFormat)
1626
1627         //read out sound properties
1628         std::string commandSoundProperty = "SELECT soundPropertyType, value FROM SinkSoundProperty" + i2s(sinkID);
1629         MY_SQLITE_PREPARE_V2(mpDatabase, commandSoundProperty.c_str(), -1, &qSoundProperty, NULL)
1630         while ((eCode = sqlite3_step(qSoundProperty)) == SQLITE_ROW)
1631         {
1632             tempSoundProperty.type = (am_SoundPropertyType_e) sqlite3_column_int(qSoundProperty, 0);
1633             tempSoundProperty.value = sqlite3_column_int(qSoundProperty, 1);
1634             sinkData.listSoundProperties.push_back(tempSoundProperty);
1635         }
1636
1637         MY_SQLITE_FINALIZE(qSoundProperty)
1638
1639         std::string notificationCommand = "SELECT type, status, parameter FROM SinkNotificationConfiguration" + i2s(sinkID);
1640         MY_SQLITE_PREPARE_V2(mpDatabase, notificationCommand.c_str(), -1, &qNotification, NULL)
1641
1642         while ((eCode = sqlite3_step(qNotification)) == SQLITE_ROW)
1643         {
1644             tempNotificationConfiguration.type =  static_cast<am_NotificationType_e>(sqlite3_column_int(qNotification, 0));
1645             tempNotificationConfiguration.status = static_cast<am_NotificationStatus_e>(sqlite3_column_int(qNotification, 1));
1646             tempNotificationConfiguration.parameter= static_cast<int16_t>(sqlite3_column_int(qNotification, 2));
1647             sinkData.listNotificationConfigurations.push_back(tempNotificationConfiguration);
1648         }
1649         MY_SQLITE_FINALIZE(qNotification)
1650
1651         if (sinkData.visible)
1652         {
1653
1654             //read out MainSoundProperties
1655             std::string commandMainSoundProperty = "SELECT soundPropertyType, value FROM SinkMainSoundProperty" + i2s(sinkID);
1656             MY_SQLITE_PREPARE_V2(mpDatabase, commandMainSoundProperty.c_str(), -1, &qMAinSoundProperty, NULL)
1657             while ((eCode = sqlite3_step(qMAinSoundProperty)) == SQLITE_ROW)
1658             {
1659                 tempMainSoundProperty.type = (am_MainSoundPropertyType_e) sqlite3_column_int(qMAinSoundProperty, 0);
1660                 tempMainSoundProperty.value = sqlite3_column_int(qMAinSoundProperty, 1);
1661                 sinkData.listMainSoundProperties.push_back(tempMainSoundProperty);
1662             }
1663
1664             MY_SQLITE_FINALIZE(qMAinSoundProperty)
1665
1666             std::string mainNotificationCommand = "SELECT type, status, parameter FROM SinkMainNotificationConfiguration" + i2s(sinkID);
1667             MY_SQLITE_PREPARE_V2(mpDatabase, mainNotificationCommand.c_str(), -1, &qMainNotification, NULL)
1668
1669             while ((eCode = sqlite3_step(qMainNotification)) == SQLITE_ROW)
1670             {
1671                 tempMainNotification.type =  static_cast<am_NotificationType_e>(sqlite3_column_int(qMainNotification, 0));
1672                 tempMainNotification.status = static_cast<am_NotificationStatus_e>(sqlite3_column_int(qMainNotification, 1));
1673                 tempMainNotification.parameter= static_cast<int16_t>(sqlite3_column_int(qMainNotification, 2));
1674                 sinkData.listMainNotificationConfigurations.push_back(tempMainNotification);
1675             }
1676             MY_SQLITE_FINALIZE(qMainNotification)
1677         }
1678     }
1679
1680     else if (eCode != SQLITE_DONE)
1681     {
1682         logError("DatabaseHandler::getSinkInfoDB SQLITE error code:", eCode);
1683         MY_SQLITE_FINALIZE(query)
1684         return (E_DATABASE_ERROR);
1685     }
1686
1687     MY_SQLITE_FINALIZE(query)
1688
1689     return (E_OK);
1690 }
1691
1692 am_Error_e CAmDatabaseHandler::getSourceInfoDB(const am_sourceID_t sourceID, am_Source_s & sourceData) const
1693 {
1694     assert(sourceID!=0);
1695
1696     if (!existSource(sourceID))
1697     {
1698         return (E_NON_EXISTENT);
1699     }
1700
1701     sqlite3_stmt* query = NULL, *qConnectionFormat = NULL, *qSoundProperty = NULL, *qMAinSoundProperty = NULL, *qMainNotification = NULL, *qNotification = NULL;
1702     int eCode = 0;
1703     am_ConnectionFormat_e tempConnectionFormat;
1704     am_SoundProperty_s tempSoundProperty;
1705     am_MainSoundProperty_s tempMainSoundProperty;
1706     am_NotificationConfiguration_s tempNotificationConfiguration,tempMainNotification;
1707     std::string command = "SELECT name, domainID, sourceClassID, sourceState, volume, visible, availability, availabilityReason, interruptState, sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE reserved=0 AND sourceID=" + i2s(sourceID);
1708     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1709
1710     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1711     {
1712         sourceData.name = std::string((const char*) sqlite3_column_text(query, 0));
1713         sourceData.domainID = sqlite3_column_int(query, 1);
1714         sourceData.sourceClassID = sqlite3_column_int(query, 2);
1715         sourceData.sourceState = (am_SourceState_e) sqlite3_column_int(query, 3);
1716         sourceData.volume = sqlite3_column_int(query, 4);
1717         sourceData.visible = sqlite3_column_int(query, 5);
1718         sourceData.available.availability = (am_Availability_e) sqlite3_column_int(query, 6);
1719         sourceData.available.availabilityReason = (am_AvailabilityReason_e) sqlite3_column_int(query, 7);
1720         sourceData.interruptState = (am_InterruptState_e) sqlite3_column_int(query, 8);
1721         sourceData.sourceID = sqlite3_column_int(query, 9);
1722
1723         //read out the connectionFormats
1724         std::string commandConnectionFormat = "SELECT soundFormat FROM SourceConnectionFormat" + i2s(sourceID);
1725         MY_SQLITE_PREPARE_V2(mpDatabase, commandConnectionFormat.c_str(), -1, &qConnectionFormat, NULL)
1726         while ((eCode = sqlite3_step(qConnectionFormat)) == SQLITE_ROW)
1727         {
1728             tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qConnectionFormat, 0);
1729             sourceData.listConnectionFormats.push_back(tempConnectionFormat);
1730         }
1731
1732         MY_SQLITE_FINALIZE(qConnectionFormat)
1733
1734         //read out sound properties
1735         std::string commandSoundProperty = "SELECT soundPropertyType, value FROM SourceSoundProperty" + i2s(sourceID);
1736         MY_SQLITE_PREPARE_V2(mpDatabase, commandSoundProperty.c_str(), -1, &qSoundProperty, NULL);
1737         while ((eCode = sqlite3_step(qSoundProperty)) == SQLITE_ROW)
1738         {
1739             tempSoundProperty.type = (am_SoundPropertyType_e) sqlite3_column_int(qSoundProperty, 0);
1740             tempSoundProperty.value = sqlite3_column_int(qSoundProperty, 1);
1741             sourceData.listSoundProperties.push_back(tempSoundProperty);
1742         }
1743
1744         MY_SQLITE_FINALIZE(qSoundProperty)
1745
1746         std::string notificationCommand = "SELECT type, status, parameter FROM SourceNotificationConfiguration" + i2s(sourceID);
1747         MY_SQLITE_PREPARE_V2(mpDatabase, notificationCommand.c_str(), -1, &qNotification, NULL)
1748
1749         while ((eCode = sqlite3_step(qNotification)) == SQLITE_ROW)
1750         {
1751             tempNotificationConfiguration.type =  static_cast<am_NotificationType_e>(sqlite3_column_int(qNotification, 0));
1752             tempNotificationConfiguration.status = static_cast<am_NotificationStatus_e>(sqlite3_column_int(qNotification, 1));
1753             tempNotificationConfiguration.parameter= static_cast<int16_t>(sqlite3_column_int(qNotification, 2));
1754             sourceData.listNotificationConfigurations.push_back(tempNotificationConfiguration);
1755         }
1756         MY_SQLITE_FINALIZE(qNotification)
1757
1758         if (sourceData.visible)
1759         {
1760
1761             //read out MainSoundProperties
1762             std::string commandMainSoundProperty = "SELECT soundPropertyType, value FROM SourceMainSoundProperty" + i2s(sourceID);
1763             MY_SQLITE_PREPARE_V2(mpDatabase, commandMainSoundProperty.c_str(), -1, &qMAinSoundProperty, NULL)
1764             while ((eCode = sqlite3_step(qMAinSoundProperty)) == SQLITE_ROW)
1765             {
1766                 tempMainSoundProperty.type = (am_MainSoundPropertyType_e) sqlite3_column_int(qMAinSoundProperty, 0);
1767                 tempMainSoundProperty.value = sqlite3_column_int(qMAinSoundProperty, 1);
1768                 sourceData.listMainSoundProperties.push_back(tempMainSoundProperty);
1769             }
1770
1771             MY_SQLITE_FINALIZE(qMAinSoundProperty)
1772
1773             std::string mainNotificationCommand = "SELECT type, status, parameter FROM SourceMainNotificationConfiguration" + i2s(sourceID);
1774             MY_SQLITE_PREPARE_V2(mpDatabase, mainNotificationCommand.c_str(), -1, &qMainNotification, NULL)
1775
1776             while ((eCode = sqlite3_step(qMainNotification)) == SQLITE_ROW)
1777             {
1778                 tempMainNotification.type =  static_cast<am_NotificationType_e>(sqlite3_column_int(qMainNotification, 0));
1779                 tempMainNotification.status = static_cast<am_NotificationStatus_e>(sqlite3_column_int(qMainNotification, 1));
1780                 tempMainNotification.parameter= static_cast<int16_t>(sqlite3_column_int(qMainNotification, 2));
1781                 sourceData.listMainNotificationConfigurations.push_back(tempMainNotification);
1782             }
1783             MY_SQLITE_FINALIZE(qMainNotification)
1784
1785         }
1786     }
1787     else if (eCode != SQLITE_DONE)
1788     {
1789         logError("DatabaseHandler::getSourceInfoDB SQLITE error code:", eCode);
1790         MY_SQLITE_FINALIZE(query)
1791         return (E_DATABASE_ERROR);
1792     }
1793
1794     MY_SQLITE_FINALIZE(query)
1795
1796     return (E_OK);
1797 }
1798
1799 am_Error_e am::CAmDatabaseHandler::getMainConnectionInfoDB(const am_mainConnectionID_t mainConnectionID, am_MainConnection_s & mainConnectionData) const
1800 {
1801     assert(mainConnectionID!=0);
1802     if (!existMainConnection(mainConnectionID))
1803     {
1804         return (E_NON_EXISTENT);
1805     }
1806     sqlite3_stmt *query = NULL, *query1 = NULL;
1807     int eCode = 0;
1808     am_MainConnection_s temp;
1809     std::string command = "SELECT mainConnectionID, sourceID, sinkID, connectionState, delay FROM " + std::string(MAINCONNECTION_TABLE) + " WHERE mainConnectionID=" + i2s(mainConnectionID);
1810     std::string command1 = "SELECT connectionID FROM MainConnectionRoute";
1811     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1812
1813     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1814     {
1815         mainConnectionData.mainConnectionID = sqlite3_column_int(query, 0);
1816         mainConnectionData.sourceID = sqlite3_column_int(query, 1);
1817         mainConnectionData.sinkID = sqlite3_column_int(query, 2);
1818         mainConnectionData.connectionState = (am_ConnectionState_e) sqlite3_column_int(query, 3);
1819         mainConnectionData.delay = sqlite3_column_int(query, 4);
1820         std::string statement = command1 + i2s(mainConnectionID);
1821         MY_SQLITE_PREPARE_V2(mpDatabase, statement.c_str(), -1, &query1, NULL)
1822         while ((eCode = sqlite3_step(query1)) == SQLITE_ROW)
1823         {
1824             mainConnectionData.listConnectionID.push_back(sqlite3_column_int(query1, 0));
1825         }
1826         MY_SQLITE_FINALIZE(query1)
1827     }
1828
1829     if (eCode != SQLITE_DONE)
1830     {
1831         logError("DatabaseHandler::getMainConnectionInfoDB SQLITE error code:", eCode);
1832         MY_SQLITE_FINALIZE(query)
1833         return (E_DATABASE_ERROR);
1834     }
1835
1836     MY_SQLITE_FINALIZE(query)
1837
1838     return (E_OK);
1839 }
1840
1841 am_Error_e CAmDatabaseHandler::changeSinkClassInfoDB(const am_SinkClass_s& sinkClass)
1842 {
1843     assert(sinkClass.sinkClassID!=0);
1844     assert(!sinkClass.listClassProperties.empty());
1845
1846     sqlite3_stmt* query = NULL;
1847     int eCode = 0;
1848
1849     //check if the ID already exists
1850     if (!existSinkClass(sinkClass.sinkClassID))
1851         return (E_NON_EXISTENT);
1852
1853     //fill ConnectionFormats
1854     std::string command = "UPDATE SinkClassProperties" + i2s(sinkClass.sinkClassID) + " set value=? WHERE classProperty=?;";
1855     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1856     std::vector<am_ClassProperty_s>::const_iterator Iterator = sinkClass.listClassProperties.begin();
1857     for (; Iterator < sinkClass.listClassProperties.end(); ++Iterator)
1858     {
1859         MY_SQLITE_BIND_INT(query, 1, Iterator->value)
1860         MY_SQLITE_BIND_INT(query, 2, Iterator->classProperty)
1861         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1862         {
1863             logError("DatabaseHandler::setSinkClassInfoDB SQLITE Step error code:", eCode);
1864             MY_SQLITE_FINALIZE(query)
1865             return (E_DATABASE_ERROR);    if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1866             {
1867                 logError("DatabaseHandler::changeMainSourceNotificationConfigurationDB SQLITE Step error code:", eCode);
1868                 MY_SQLITE_FINALIZE(query)
1869                 return (E_DATABASE_ERROR);
1870             }
1871             MY_SQLITE_FINALIZE(query)
1872         }
1873         MY_SQLITE_RESET(query)
1874     }
1875
1876     MY_SQLITE_FINALIZE(query)
1877
1878     logInfo("DatabaseHandler::setSinkClassInfoDB set setSinkClassInfo");
1879     return (E_OK);
1880 }
1881
1882 am_Error_e CAmDatabaseHandler::changeSourceClassInfoDB(const am_SourceClass_s& sourceClass)
1883 {
1884     assert(sourceClass.sourceClassID!=0);
1885     assert(!sourceClass.listClassProperties.empty());
1886
1887     sqlite3_stmt* query = NULL;
1888     int eCode = 0;
1889
1890     //check if the ID already exists
1891     if (!existSourceClass(sourceClass.sourceClassID))
1892         return (E_NON_EXISTENT);
1893
1894     //fill ConnectionFormats
1895     std::string command = "UPDATE SourceClassProperties" + i2s(sourceClass.sourceClassID) + " set value=? WHERE classProperty=?;";
1896     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1897     std::vector<am_ClassProperty_s>::const_iterator Iterator = sourceClass.listClassProperties.begin();
1898     for (; Iterator < sourceClass.listClassProperties.end(); ++Iterator)
1899     {
1900         MY_SQLITE_BIND_INT(query, 1, Iterator->value)
1901         MY_SQLITE_BIND_INT(query, 2, Iterator->classProperty)
1902         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1903         {
1904             logError("DatabaseHandler::setSinkClassInfoDB SQLITE Step error code:", eCode);
1905             MY_SQLITE_FINALIZE(query)
1906             return (E_DATABASE_ERROR);
1907         }
1908         MY_SQLITE_RESET(query)
1909     }
1910
1911     MY_SQLITE_FINALIZE(query)
1912
1913     logInfo("DatabaseHandler::setSinkClassInfoDB set setSinkClassInfo");
1914     return (E_OK);
1915 }
1916
1917 am_Error_e CAmDatabaseHandler::getSinkClassInfoDB(const am_sinkID_t sinkID, am_SinkClass_s & sinkClass) const
1918 {
1919     assert(sinkID!=0);
1920
1921     if (!existSink(sinkID))
1922     {
1923         return (E_NON_EXISTENT);
1924     }
1925     sqlite3_stmt* query = NULL;
1926     int eCode = 0;
1927     am_ClassProperty_s propertyTemp;
1928     std::string command = "SELECT sinkClassID FROM " + std::string(SINK_TABLE) + " WHERE sinkID=" + (i2s(sinkID));
1929     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1930
1931     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1932     {
1933         sinkClass.sinkClassID = sqlite3_column_int(query, 0);
1934     }
1935
1936     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1937     {
1938         logError("DatabaseHandler::getSinkClassInfoDB SQLITE error code:", eCode);
1939         MY_SQLITE_FINALIZE(query)
1940         return (E_DATABASE_ERROR);
1941     }
1942
1943     MY_SQLITE_FINALIZE(query)
1944
1945     command = "SELECT name FROM " + std::string(SINK_CLASS_TABLE) + " WHERE sinkClassID=" + (i2s(sinkClass.sinkClassID));
1946     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1947
1948     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1949     {
1950         sinkClass.name = std::string((const char*) sqlite3_column_text(query, 0));
1951     }
1952
1953     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1954     {
1955         logError("DatabaseHandler::getSinkClassInfoDB SQLITE error code:", eCode);
1956         MY_SQLITE_FINALIZE(query)
1957         return (E_DATABASE_ERROR);
1958     }
1959
1960     MY_SQLITE_FINALIZE(query)
1961
1962     //read out Properties
1963     command = "SELECT classProperty, value FROM SinkClassProperties" + i2s(sinkClass.sinkClassID);
1964     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1965     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1966     {
1967         propertyTemp.classProperty = (am_ClassProperty_e) sqlite3_column_int(query, 0);
1968         propertyTemp.value = sqlite3_column_int(query, 1);
1969         sinkClass.listClassProperties.push_back(propertyTemp);
1970     }
1971
1972     if (eCode != SQLITE_DONE)
1973     {
1974         logError("DatabaseHandler::getSinkClassInfoDB SQLITE error code:", eCode);
1975         MY_SQLITE_FINALIZE(query)
1976         return (E_DATABASE_ERROR);
1977     }
1978
1979     MY_SQLITE_FINALIZE(query)
1980     return (E_OK);
1981 }
1982
1983 am_Error_e CAmDatabaseHandler::getGatewayInfoDB(const am_gatewayID_t gatewayID, am_Gateway_s & gatewayData) const
1984 {
1985     assert(gatewayID!=0);
1986     if (!existGateway(gatewayID))
1987     {
1988         return (E_NON_EXISTENT);
1989     }
1990     sqlite3_stmt* query = NULL, *qSinkConnectionFormat = NULL, *qSourceConnectionFormat = NULL;
1991     int eCode = 0;
1992     am_ConnectionFormat_e tempConnectionFormat;
1993     std::string command = "SELECT name, sinkID, sourceID, domainSinkID, domainSourceID, controlDomainID, gatewayID FROM " + std::string(GATEWAY_TABLE) + " WHERE gatewayID=" + i2s(gatewayID);
1994     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
1995
1996     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1997     {
1998         gatewayData.name = std::string((const char*) sqlite3_column_text(query, 0));
1999         gatewayData.sinkID = sqlite3_column_int(query, 1);
2000         gatewayData.sourceID = sqlite3_column_int(query, 2);
2001         gatewayData.domainSinkID = sqlite3_column_int(query, 3);
2002         gatewayData.domainSourceID = sqlite3_column_int(query, 4);
2003         gatewayData.controlDomainID = sqlite3_column_int(query, 5);
2004         gatewayData.gatewayID = sqlite3_column_int(query, 6);
2005
2006         //convertionMatrix:
2007         ListConnectionFormat::const_iterator iter = mListConnectionFormat.begin();
2008         iter = mListConnectionFormat.find(gatewayData.gatewayID);
2009         if (iter == mListConnectionFormat.end())
2010         {
2011             logError("DatabaseHandler::getGatewayInfoDB database error with convertionFormat");
2012             MY_SQLITE_FINALIZE(query)
2013             return (E_DATABASE_ERROR);
2014         }
2015         gatewayData.convertionMatrix = iter->second;
2016
2017         //read out the connectionFormats
2018         std::string commandConnectionFormat = "SELECT soundFormat FROM GatewaySourceFormat" + i2s(gatewayData.gatewayID);
2019         MY_SQLITE_PREPARE_V2(mpDatabase, commandConnectionFormat.c_str(), -1, &qSourceConnectionFormat, NULL)
2020         while ((eCode = sqlite3_step(qSourceConnectionFormat)) == SQLITE_ROW)
2021         {
2022             tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qSourceConnectionFormat, 0);
2023             gatewayData.listSourceFormats.push_back(tempConnectionFormat);
2024         }
2025
2026         MY_SQLITE_FINALIZE(qSourceConnectionFormat)
2027
2028         //read out sound properties
2029         commandConnectionFormat = "SELECT soundFormat FROM GatewaySinkFormat" + i2s(gatewayData.gatewayID);
2030         MY_SQLITE_PREPARE_V2(mpDatabase, commandConnectionFormat.c_str(), -1, &qSinkConnectionFormat, NULL)
2031         while ((eCode = sqlite3_step(qSinkConnectionFormat)) == SQLITE_ROW)
2032         {
2033             tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qSinkConnectionFormat, 0);
2034             gatewayData.listSinkFormats.push_back(tempConnectionFormat);
2035         }
2036
2037         MY_SQLITE_FINALIZE(qSinkConnectionFormat)
2038
2039     }
2040
2041     if (eCode != SQLITE_DONE)
2042     {
2043         logError("DatabaseHandler::getGatewayInfoDB SQLITE error code:", eCode);
2044         MY_SQLITE_FINALIZE(query)
2045         return (E_DATABASE_ERROR);
2046     }
2047
2048     MY_SQLITE_FINALIZE(query)
2049
2050     return (E_OK);
2051
2052 }
2053
2054 am_Error_e CAmDatabaseHandler::getCrossfaderInfoDB(const am_crossfaderID_t crossfaderID, am_Crossfader_s & crossfaderData) const
2055 {
2056     assert(crossfaderID!=0);
2057     if (!existcrossFader(crossfaderID))
2058     {
2059         return (E_NON_EXISTENT);
2060     }
2061     sqlite3_stmt* query = NULL;
2062     int eCode = 0;
2063     std::string command = "SELECT name, sinkID_A, sinkID_B, sourceID, hotSink,crossfaderID FROM " + std::string(CROSSFADER_TABLE) + " WHERE crossfaderID=" + i2s(crossfaderID);
2064     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2065
2066     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2067     {
2068         crossfaderData.name = std::string((const char*) sqlite3_column_text(query, 0));
2069         crossfaderData.sinkID_A = sqlite3_column_int(query, 1);
2070         crossfaderData.sinkID_B = sqlite3_column_int(query, 2);
2071         crossfaderData.sourceID = sqlite3_column_int(query, 3);
2072         crossfaderData.hotSink = static_cast<am_HotSink_e>(sqlite3_column_int(query, 4));
2073         crossfaderData.crossfaderID = sqlite3_column_int(query, 5);
2074     }
2075
2076     if (eCode != SQLITE_DONE)
2077     {
2078         logError("DatabaseHandler::getCrossfaderInfoDB SQLITE error code:", eCode);
2079         MY_SQLITE_FINALIZE(query)
2080         return (E_DATABASE_ERROR);
2081     }
2082
2083     MY_SQLITE_FINALIZE(query)
2084
2085     return (E_OK);
2086 }
2087
2088 am_Error_e CAmDatabaseHandler::getListSinksOfDomain(const am_domainID_t domainID, std::vector<am_sinkID_t> & listSinkID) const
2089 {
2090     assert(domainID!=0);
2091     listSinkID.clear();
2092     if (!existDomain(domainID))
2093     {
2094         return (E_NON_EXISTENT);
2095     }
2096     sqlite3_stmt* query = NULL;
2097     int eCode = 0;
2098     am_sinkID_t temp;
2099     std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 AND domainID=" + (i2s(domainID));
2100     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2101
2102     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2103     {
2104         temp = sqlite3_column_int(query, 0);
2105         listSinkID.push_back(temp);
2106     }
2107
2108     if (eCode != SQLITE_DONE)
2109     {
2110         logError("DatabaseHandler::getListSinksOfDomain SQLITE error code:", eCode);
2111         MY_SQLITE_FINALIZE(query)
2112         return (E_DATABASE_ERROR);
2113     }
2114
2115     MY_SQLITE_FINALIZE(query)
2116     return (E_OK);
2117 }
2118
2119 am_Error_e CAmDatabaseHandler::getListSourcesOfDomain(const am_domainID_t domainID, std::vector<am_sourceID_t> & listSourceID) const
2120 {
2121     assert(domainID!=0);
2122     listSourceID.clear();
2123     if (!existDomain(domainID))
2124     {
2125         return (E_NON_EXISTENT);
2126     }
2127     sqlite3_stmt* query = NULL;
2128     int eCode = 0;
2129     am_sourceID_t temp;
2130     std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE reserved=0 AND domainID=" + i2s(domainID);
2131
2132     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2133
2134     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2135     {
2136         temp = sqlite3_column_int(query, 0);
2137         listSourceID.push_back(temp);
2138     }
2139
2140     if (eCode != SQLITE_DONE)
2141     {
2142         logError("DatabaseHandler::getListSourcesOfDomain SQLITE error code:", eCode);
2143         MY_SQLITE_FINALIZE(query)
2144         return (E_DATABASE_ERROR);
2145     }
2146
2147     MY_SQLITE_FINALIZE(query)
2148
2149     return (E_OK);
2150 }
2151
2152 am_Error_e CAmDatabaseHandler::getListCrossfadersOfDomain(const am_domainID_t domainID, std::vector<am_crossfaderID_t> & listCrossfader) const
2153 {
2154     assert(domainID!=0);
2155     listCrossfader.clear();
2156     if (!existDomain(domainID))
2157     {
2158         return (E_NON_EXISTENT);
2159     }
2160     sqlite3_stmt* query = NULL;
2161     int eCode = 0;
2162     am_crossfaderID_t temp;
2163
2164     std::string command = "SELECT c.crossfaderID FROM " + std::string(CROSSFADER_TABLE) + " c," + std::string(SOURCE_TABLE) + " s WHERE c.sourceID=s.sourceID AND s.domainID=" + i2s(domainID);
2165     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2166
2167     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2168     {
2169         temp = sqlite3_column_int(query, 0);
2170         listCrossfader.push_back(temp);
2171     }
2172
2173     if (eCode != SQLITE_DONE)
2174     {
2175         logError("DatabaseHandler::getListCrossfadersOfDomain SQLITE error code:", eCode);
2176         MY_SQLITE_FINALIZE(query)
2177         return (E_DATABASE_ERROR);
2178     }
2179
2180     MY_SQLITE_FINALIZE(query)
2181
2182     return (E_OK);
2183
2184 }
2185
2186 am_Error_e CAmDatabaseHandler::getListGatewaysOfDomain(const am_domainID_t domainID, std::vector<am_gatewayID_t> & listGatewaysID) const
2187 {
2188     assert(domainID!=0);
2189     listGatewaysID.clear();
2190     if (!existDomain(domainID))
2191     {
2192         return (E_NON_EXISTENT);
2193     }
2194     sqlite3_stmt* query = NULL;
2195     int eCode = 0;
2196     am_gatewayID_t temp;
2197
2198     std::string command = "SELECT gatewayID FROM " + std::string(GATEWAY_TABLE) + " WHERE controlDomainID=" + i2s(domainID);
2199     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2200
2201     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2202     {
2203         temp = sqlite3_column_int(query, 0);
2204         listGatewaysID.push_back(temp);
2205     }
2206
2207     if (eCode != SQLITE_DONE)
2208     {
2209         logError("DatabaseHandler::getListGatewaysOfDomain SQLITE error code:", eCode);
2210         MY_SQLITE_FINALIZE(query)
2211         return (E_DATABASE_ERROR);
2212     }
2213
2214     MY_SQLITE_FINALIZE(query)
2215
2216     return (E_OK);
2217 }
2218
2219 am_Error_e CAmDatabaseHandler::getListMainConnections(std::vector<am_MainConnection_s> & listMainConnections) const
2220 {
2221     listMainConnections.clear();
2222     sqlite3_stmt *query = NULL, *query1 = NULL;
2223     int eCode = 0;
2224     am_MainConnection_s temp;
2225     std::string command = "SELECT mainConnectionID, sourceID, sinkID, connectionState, delay FROM " + std::string(MAINCONNECTION_TABLE);
2226     std::string command1 = "SELECT connectionID FROM MainConnectionRoute";
2227     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2228
2229     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2230     {
2231         temp.mainConnectionID = sqlite3_column_int(query, 0);
2232         temp.sourceID = sqlite3_column_int(query, 1);
2233         temp.sinkID = sqlite3_column_int(query, 2);
2234         temp.connectionState = (am_ConnectionState_e) sqlite3_column_int(query, 3);
2235         temp.delay = sqlite3_column_int(query, 4);
2236         std::string statement = command1 + i2s(temp.mainConnectionID);
2237         MY_SQLITE_PREPARE_V2(mpDatabase, statement.c_str(), -1, &query1, NULL)
2238         while ((eCode = sqlite3_step(query1)) == SQLITE_ROW)
2239         {
2240             temp.listConnectionID.push_back(sqlite3_column_int(query1, 0));
2241         }
2242         listMainConnections.push_back(temp);
2243         MY_SQLITE_FINALIZE(query1)
2244     }
2245
2246     if (eCode != SQLITE_DONE)
2247     {
2248         logError("DatabaseHandler::getListMainConnections SQLITE error code:", eCode);
2249         MY_SQLITE_FINALIZE(query)
2250         return (E_DATABASE_ERROR);
2251     }
2252
2253     MY_SQLITE_FINALIZE(query)
2254
2255     return (E_OK);
2256 }
2257
2258 am_Error_e CAmDatabaseHandler::getListDomains(std::vector<am_Domain_s> & listDomains) const
2259 {
2260     listDomains.clear();
2261     sqlite3_stmt* query = NULL;
2262     int eCode = 0;
2263     am_Domain_s temp;
2264     std::string command = "SELECT domainID, name, busname, nodename, early, complete, state FROM " + std::string(DOMAIN_TABLE) + " WHERE reserved=0";
2265     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2266
2267     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2268     {
2269         temp.domainID = sqlite3_column_int(query, 0);
2270         temp.name = std::string((const char*) sqlite3_column_text(query, 1));
2271         temp.busname = std::string((const char*) sqlite3_column_text(query, 2));
2272         temp.nodename = std::string((const char*) sqlite3_column_text(query, 3));
2273         temp.early = sqlite3_column_int(query, 4);
2274         temp.complete = sqlite3_column_int(query, 5);
2275         temp.state = (am_DomainState_e) sqlite3_column_int(query, 6);
2276         listDomains.push_back(temp);
2277     }
2278
2279     if (eCode != SQLITE_DONE)
2280     {
2281         logError("DatabaseHandler::getListDomains SQLITE error code:", eCode);
2282         MY_SQLITE_FINALIZE(query)
2283         return (E_DATABASE_ERROR);
2284     }
2285
2286     MY_SQLITE_FINALIZE(query)
2287
2288     return (E_OK);
2289 }
2290
2291 am_Error_e CAmDatabaseHandler::getListConnections(std::vector<am_Connection_s> & listConnections) const
2292 {
2293     listConnections.clear();
2294     sqlite3_stmt* query = NULL;
2295     int eCode = 0;
2296     am_Connection_s temp;
2297     std::string command = "SELECT connectionID, sourceID, sinkID, delay, connectionFormat FROM " + std::string(CONNECTION_TABLE) + " WHERE reserved=0";
2298     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2299
2300     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2301     {
2302         temp.connectionID = sqlite3_column_int(query, 0);
2303         temp.sourceID = sqlite3_column_int(query, 1);
2304         temp.sinkID = sqlite3_column_int(query, 2);
2305         temp.delay = sqlite3_column_int(query, 3);
2306         temp.connectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(query, 4);
2307         listConnections.push_back(temp);
2308     }
2309
2310     if (eCode != SQLITE_DONE)
2311     {
2312         logError("DatabaseHandler::getListConnections SQLITE error code:", eCode);
2313         MY_SQLITE_FINALIZE(query)
2314         return (E_DATABASE_ERROR);
2315     }
2316
2317     MY_SQLITE_FINALIZE(query)
2318
2319     return (E_OK);
2320 }
2321
2322 am_Error_e CAmDatabaseHandler::getListSinks(std::vector<am_Sink_s> & listSinks) const
2323 {
2324     listSinks.clear();
2325     sqlite3_stmt* query = NULL, *qConnectionFormat = NULL, *qSoundProperty = NULL, *qNotificationConfiguration= NULL, *qMAinSoundProperty = NULL, *qMainNotificationConfiguration= NULL;
2326     int eCode = 0;
2327     am_Sink_s temp;
2328     am_ConnectionFormat_e tempConnectionFormat;
2329     am_SoundProperty_s tempSoundProperty;
2330     am_MainSoundProperty_s tempMainSoundProperty;
2331     am_NotificationConfiguration_s tempNotificationConfiguration;
2332     am_NotificationConfiguration_s tempMainNotificationConfiguration;
2333     std::string command = "SELECT name, domainID, sinkClassID, volume, visible, availability, availabilityReason, muteState, mainVolume, sinkID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0";
2334     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2335
2336     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2337     {
2338         temp.name = std::string((const char*) sqlite3_column_text(query, 0));
2339         temp.domainID = sqlite3_column_int(query, 1);
2340         temp.sinkClassID = sqlite3_column_int(query, 2);
2341         temp.volume = sqlite3_column_int(query, 3);
2342         temp.visible = sqlite3_column_int(query, 4);
2343         temp.available.availability = (am_Availability_e) sqlite3_column_int(query, 5);
2344         temp.available.availabilityReason = (am_AvailabilityReason_e) sqlite3_column_int(query, 6);
2345         temp.muteState = (am_MuteState_e) sqlite3_column_int(query, 7);
2346         temp.mainVolume = sqlite3_column_int(query, 8);
2347         temp.sinkID = sqlite3_column_int(query, 9);
2348
2349         //read out the connectionFormats
2350         std::string commandConnectionFormat = "SELECT soundFormat FROM SinkConnectionFormat" + i2s(temp.sinkID);
2351         MY_SQLITE_PREPARE_V2(mpDatabase, commandConnectionFormat.c_str(), -1, &qConnectionFormat, NULL)
2352         while ((eCode = sqlite3_step(qConnectionFormat)) == SQLITE_ROW)
2353         {
2354             tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qConnectionFormat, 0);
2355             temp.listConnectionFormats.push_back(tempConnectionFormat);
2356         }
2357
2358         MY_SQLITE_FINALIZE(qConnectionFormat)
2359
2360         //read out sound properties
2361         std::string commandSoundProperty = "SELECT soundPropertyType, value FROM SinkSoundProperty" + i2s(temp.sinkID);
2362         MY_SQLITE_PREPARE_V2(mpDatabase, commandSoundProperty.c_str(), -1, &qSoundProperty, NULL)
2363         while ((eCode = sqlite3_step(qSoundProperty)) == SQLITE_ROW)
2364         {
2365             tempSoundProperty.type = (am_SoundPropertyType_e) sqlite3_column_int(qSoundProperty, 0);
2366             tempSoundProperty.value = sqlite3_column_int(qSoundProperty, 1);
2367             temp.listSoundProperties.push_back(tempSoundProperty);
2368         }
2369
2370         MY_SQLITE_FINALIZE(qSoundProperty)
2371
2372         //read out notifications
2373         std::string commandNotificationConfiguration = "SELECT type, status, parameter FROM SinkNotificationConfiguration" + i2s(temp.sinkID);
2374         MY_SQLITE_PREPARE_V2(mpDatabase, commandNotificationConfiguration.c_str(), -1, &qNotificationConfiguration, NULL)
2375         while ((eCode = sqlite3_step(qNotificationConfiguration)) == SQLITE_ROW)
2376         {
2377             tempNotificationConfiguration.type = static_cast<am_NotificationType_e> (sqlite3_column_int(qNotificationConfiguration, 0));
2378             tempNotificationConfiguration.status = static_cast<am_NotificationStatus_e> (sqlite3_column_int(qNotificationConfiguration, 1));
2379             tempNotificationConfiguration.parameter = static_cast<int16_t> (sqlite3_column_int(qNotificationConfiguration, 2));
2380             temp.listNotificationConfigurations.push_back(tempNotificationConfiguration);
2381         }
2382
2383         MY_SQLITE_FINALIZE(qNotificationConfiguration)
2384
2385         //read out MainSoundProperties if sink is visible
2386         if(temp.visible)
2387         {
2388             std::string commandMainSoundProperty = "SELECT soundPropertyType, value FROM SinkMainSoundProperty" + i2s(temp.sinkID);
2389             MY_SQLITE_PREPARE_V2(mpDatabase, commandMainSoundProperty.c_str(), -1, &qMAinSoundProperty, NULL)
2390             while ((eCode = sqlite3_step(qMAinSoundProperty)) == SQLITE_ROW)
2391             {
2392                 tempMainSoundProperty.type = (am_MainSoundPropertyType_e) sqlite3_column_int(qMAinSoundProperty, 0);
2393                 tempMainSoundProperty.value = sqlite3_column_int(qMAinSoundProperty, 1);
2394                 temp.listMainSoundProperties.push_back(tempMainSoundProperty);
2395             }
2396
2397             MY_SQLITE_FINALIZE(qMAinSoundProperty)
2398
2399             //and mainNotificationConfigurations
2400             std::string commandMainNotificationConfiguration = "SELECT type, status, parameter FROM SinkMainNotificationConfiguration" + i2s(temp.sinkID);
2401             MY_SQLITE_PREPARE_V2(mpDatabase, commandMainNotificationConfiguration.c_str(), -1, &qMainNotificationConfiguration, NULL)
2402             while ((eCode = sqlite3_step(qMainNotificationConfiguration)) == SQLITE_ROW)
2403             {
2404                 tempMainNotificationConfiguration.type = static_cast <am_NotificationType_e> (sqlite3_column_int(qMainNotificationConfiguration, 0));
2405                 tempMainNotificationConfiguration.status = static_cast <am_NotificationStatus_e> (sqlite3_column_int(qMainNotificationConfiguration, 1));
2406                 tempMainNotificationConfiguration.parameter = static_cast <uint16_t>(sqlite3_column_int(qMainNotificationConfiguration, 2));
2407                 temp.listMainNotificationConfigurations.push_back(tempMainNotificationConfiguration);
2408             }
2409
2410             MY_SQLITE_FINALIZE(qMainNotificationConfiguration)
2411         }
2412
2413         listSinks.push_back(temp);
2414         temp.listConnectionFormats.clear();
2415         temp.listMainSoundProperties.clear();
2416         temp.listSoundProperties.clear();
2417     }
2418
2419     if (eCode != SQLITE_DONE)
2420     {
2421         logError("DatabaseHandler::getListSinks SQLITE error code:", eCode);
2422         MY_SQLITE_FINALIZE(query)
2423         return (E_DATABASE_ERROR);
2424     }
2425
2426     MY_SQLITE_FINALIZE(query)
2427
2428     return (E_OK);
2429 }
2430
2431 am_Error_e CAmDatabaseHandler::getListSources(std::vector<am_Source_s> & listSources) const
2432 {
2433     listSources.clear();
2434     sqlite3_stmt* query = NULL, *qConnectionFormat = NULL, *qSoundProperty = NULL, *qMAinSoundProperty = NULL, *qNotification(NULL), *qMainNotification(NULL);
2435     int eCode = 0;
2436     am_Source_s temp;
2437     am_ConnectionFormat_e tempConnectionFormat;
2438     am_SoundProperty_s tempSoundProperty;
2439     am_MainSoundProperty_s tempMainSoundProperty;
2440     am_NotificationConfiguration_s tempNotificationConfiguration;
2441     std::string command = "SELECT name, domainID, sourceClassID, sourceState, volume, visible, availability, availabilityReason, interruptState, sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE reserved=0";
2442     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2443
2444     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2445     {
2446         temp.name = std::string((const char*) sqlite3_column_text(query, 0));
2447         temp.domainID = sqlite3_column_int(query, 1);
2448         temp.sourceClassID = sqlite3_column_int(query, 2);
2449         temp.sourceState = (am_SourceState_e) sqlite3_column_int(query, 3);
2450         temp.volume = sqlite3_column_int(query, 4);
2451         temp.visible = sqlite3_column_int(query, 5);
2452         temp.available.availability = (am_Availability_e) sqlite3_column_int(query, 6);
2453         temp.available.availabilityReason = (am_AvailabilityReason_e) sqlite3_column_int(query, 7);
2454         temp.interruptState = (am_InterruptState_e) sqlite3_column_int(query, 8);
2455         temp.sourceID = sqlite3_column_int(query, 9);
2456
2457         //read out the connectionFormats
2458         std::string commandConnectionFormat = "SELECT soundFormat FROM SourceConnectionFormat" + i2s(temp.sourceID);
2459         MY_SQLITE_PREPARE_V2(mpDatabase, commandConnectionFormat.c_str(), -1, &qConnectionFormat, NULL)
2460         while ((eCode = sqlite3_step(qConnectionFormat)) == SQLITE_ROW)
2461         {
2462             tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qConnectionFormat, 0);
2463             temp.listConnectionFormats.push_back(tempConnectionFormat);
2464         }
2465
2466         MY_SQLITE_FINALIZE(qConnectionFormat)
2467
2468         //read out sound properties
2469         std::string commandSoundProperty = "SELECT soundPropertyType, value FROM SourceSoundProperty" + i2s(temp.sourceID);
2470         MY_SQLITE_PREPARE_V2(mpDatabase, commandSoundProperty.c_str(), -1, &qSoundProperty, NULL)
2471         while ((eCode = sqlite3_step(qSoundProperty)) == SQLITE_ROW)
2472         {
2473             tempSoundProperty.type = (am_SoundPropertyType_e) sqlite3_column_int(qSoundProperty, 0);
2474             tempSoundProperty.value = sqlite3_column_int(qSoundProperty, 1);
2475             temp.listSoundProperties.push_back(tempSoundProperty);
2476         }
2477
2478         MY_SQLITE_FINALIZE(qSoundProperty)
2479
2480         std::string notificationCommand = "SELECT type, status, parameter FROM SourceNotificationConfiguration" + i2s(temp.sourceID);
2481         MY_SQLITE_PREPARE_V2(mpDatabase, notificationCommand.c_str(), -1, &qNotification, NULL)
2482
2483         while ((eCode = sqlite3_step(qNotification)) == SQLITE_ROW)
2484         {
2485             tempNotificationConfiguration.type =  static_cast<am_NotificationType_e>(sqlite3_column_int(qNotification, 0));
2486             tempNotificationConfiguration.status = static_cast<am_NotificationStatus_e>(sqlite3_column_int(qNotification, 1));
2487             tempNotificationConfiguration.parameter= static_cast<int16_t>(sqlite3_column_int(qNotification, 2));
2488             temp.listNotificationConfigurations.push_back(tempNotificationConfiguration);
2489         }
2490         MY_SQLITE_FINALIZE(qNotification)
2491
2492         //read out MainSoundProperties if source is visible
2493         if(temp.visible)
2494         {
2495             std::string commandMainSoundProperty = "SELECT soundPropertyType, value FROM SourceMainSoundProperty" + i2s(temp.sourceID);
2496             MY_SQLITE_PREPARE_V2(mpDatabase, commandMainSoundProperty.c_str(), -1, &qMAinSoundProperty, NULL)
2497             while ((eCode = sqlite3_step(qMAinSoundProperty)) == SQLITE_ROW)
2498             {
2499                 tempMainSoundProperty.type = (am_MainSoundPropertyType_e) sqlite3_column_int(qMAinSoundProperty, 0);
2500                 tempMainSoundProperty.value = sqlite3_column_int(qMAinSoundProperty, 1);
2501                 temp.listMainSoundProperties.push_back(tempMainSoundProperty);
2502             }
2503
2504             MY_SQLITE_FINALIZE(qMAinSoundProperty)
2505
2506             std::string mainNotificationCommand = "SELECT type, status, parameter FROM SourceMainNotificationConfiguration" + i2s(temp.sourceID);
2507             MY_SQLITE_PREPARE_V2(mpDatabase, mainNotificationCommand.c_str(), -1, &qMainNotification, NULL)
2508
2509             while ((eCode = sqlite3_step(qMainNotification)) == SQLITE_ROW)
2510             {
2511                 tempNotificationConfiguration.type =  static_cast<am_NotificationType_e>(sqlite3_column_int(qMainNotification, 0));
2512                 tempNotificationConfiguration.status = static_cast<am_NotificationStatus_e>(sqlite3_column_int(qMainNotification, 1));
2513                 tempNotificationConfiguration.parameter= static_cast<int16_t>(sqlite3_column_int(qMainNotification, 2));
2514                 temp.listMainNotificationConfigurations.push_back(tempNotificationConfiguration);
2515             }
2516             MY_SQLITE_FINALIZE(qMainNotification)
2517         }
2518
2519
2520         listSources.push_back(temp);
2521         temp.listConnectionFormats.clear();
2522         temp.listMainSoundProperties.clear();
2523         temp.listSoundProperties.clear();
2524     }
2525
2526     if (eCode != SQLITE_DONE)
2527     {
2528         logError("DatabaseHandler::getListSources SQLITE error code:", eCode);
2529         MY_SQLITE_FINALIZE(query)
2530         return (E_DATABASE_ERROR);
2531     }
2532
2533     MY_SQLITE_FINALIZE(query)
2534
2535     return (E_OK);
2536 }
2537
2538 am_Error_e CAmDatabaseHandler::getListSourceClasses(std::vector<am_SourceClass_s> & listSourceClasses) const
2539 {
2540     listSourceClasses.clear();
2541
2542     sqlite3_stmt* query = NULL, *subQuery = NULL;
2543     int eCode = 0, eCode1;
2544     am_SourceClass_s classTemp;
2545     am_ClassProperty_s propertyTemp;
2546
2547     std::string command = "SELECT sourceClassID, name FROM " + std::string(SOURCE_CLASS_TABLE);
2548     std::string command2;
2549     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2550
2551     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2552     {
2553         classTemp.sourceClassID = sqlite3_column_int(query, 0);
2554         classTemp.name = std::string((const char*) sqlite3_column_text(query, 1));
2555
2556         //read out Properties
2557         command2 = "SELECT classProperty, value FROM SourceClassProperties" + i2s(classTemp.sourceClassID);
2558         MY_SQLITE_PREPARE_V2(mpDatabase, command2.c_str(), -1, &subQuery, NULL)
2559
2560         while ((eCode1 = sqlite3_step(subQuery)) == SQLITE_ROW)
2561         {
2562             propertyTemp.classProperty = (am_ClassProperty_e) sqlite3_column_int(subQuery, 0);
2563             propertyTemp.value = sqlite3_column_int(subQuery, 1);
2564             classTemp.listClassProperties.push_back(propertyTemp);
2565         }
2566
2567         if (eCode1 != SQLITE_DONE)
2568         {
2569             logError("DatabaseHandler::getListSourceClasses SQLITE error code:", eCode1);
2570             MY_SQLITE_FINALIZE(query)
2571             MY_SQLITE_FINALIZE(subQuery)
2572             return (E_DATABASE_ERROR);
2573         }
2574
2575         MY_SQLITE_FINALIZE(subQuery)
2576
2577         listSourceClasses.push_back(classTemp);
2578     }
2579
2580     if (eCode != SQLITE_DONE)
2581     {
2582         logError("DatabaseHandler::getListSourceClasses SQLITE error code:", eCode);
2583         MY_SQLITE_FINALIZE(subQuery)
2584         return (E_DATABASE_ERROR);
2585     }
2586
2587     MY_SQLITE_FINALIZE(query)
2588
2589     return (E_OK);
2590 }
2591
2592 am_Error_e CAmDatabaseHandler::getListCrossfaders(std::vector<am_Crossfader_s> & listCrossfaders) const
2593 {
2594     listCrossfaders.clear();
2595     sqlite3_stmt* query = NULL;
2596     int eCode = 0;
2597     am_Crossfader_s tempData;
2598     std::string command = "SELECT name, sinkID_A, sinkID_B, sourceID, hotSink,crossfaderID FROM " + std::string(CROSSFADER_TABLE);
2599     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2600
2601     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2602     {
2603         tempData.name = std::string((const char*) sqlite3_column_text(query, 0));
2604         tempData.sinkID_A = sqlite3_column_int(query, 1);
2605         tempData.sinkID_B = sqlite3_column_int(query, 2);
2606         tempData.sourceID = sqlite3_column_int(query, 3);
2607         tempData.hotSink = static_cast<am_HotSink_e>(sqlite3_column_int(query, 4));
2608         tempData.crossfaderID = sqlite3_column_int(query, 5);
2609         listCrossfaders.push_back(tempData);
2610     }
2611
2612     if (eCode != SQLITE_DONE)
2613     {
2614         logError("DatabaseHandler::getListCrossfaders SQLITE error code:", eCode);
2615         MY_SQLITE_FINALIZE(query)
2616         return (E_DATABASE_ERROR);
2617     }
2618
2619     MY_SQLITE_FINALIZE(query)
2620
2621     return (E_OK);
2622 }
2623
2624 am_Error_e CAmDatabaseHandler::getListGateways(std::vector<am_Gateway_s> & listGateways) const
2625 {
2626     listGateways.clear();
2627     sqlite3_stmt* query = NULL, *qSinkConnectionFormat = NULL, *qSourceConnectionFormat = NULL;
2628     int eCode = 0;
2629     am_Gateway_s temp;
2630     am_ConnectionFormat_e tempConnectionFormat;
2631
2632     std::string command = "SELECT name, sinkID, sourceID, domainSinkID, domainSourceID, controlDomainID, gatewayID FROM " + std::string(GATEWAY_TABLE);
2633     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2634
2635     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2636     {
2637         temp.name = std::string((const char*) sqlite3_column_text(query, 0));
2638         temp.sinkID = sqlite3_column_int(query, 1);
2639         temp.sourceID = sqlite3_column_int(query, 2);
2640         temp.domainSinkID = sqlite3_column_int(query, 3);
2641         temp.domainSourceID = sqlite3_column_int(query, 4);
2642         temp.controlDomainID = sqlite3_column_int(query, 5);
2643         temp.gatewayID = sqlite3_column_int(query, 6);
2644
2645         //convertionMatrix:
2646         ListConnectionFormat::const_iterator iter = mListConnectionFormat.begin();
2647         iter = mListConnectionFormat.find(temp.gatewayID);
2648         if (iter == mListConnectionFormat.end())
2649         {
2650             logError("DatabaseHandler::getListGateways database error with convertionFormat");
2651
2652             return (E_DATABASE_ERROR);
2653         }
2654         temp.convertionMatrix = iter->second;
2655
2656         //read out the connectionFormats
2657         std::string commandConnectionFormat = "SELECT soundFormat FROM GatewaySourceFormat" + i2s(temp.gatewayID);
2658         MY_SQLITE_PREPARE_V2(mpDatabase, commandConnectionFormat.c_str(), -1, &qSourceConnectionFormat, NULL)
2659         while ((eCode = sqlite3_step(qSourceConnectionFormat)) == SQLITE_ROW)
2660         {
2661             tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qSourceConnectionFormat, 0);
2662             temp.listSourceFormats.push_back(tempConnectionFormat);
2663         }
2664
2665         MY_SQLITE_FINALIZE(qSourceConnectionFormat)
2666
2667         //read out sound properties
2668         commandConnectionFormat = "SELECT soundFormat FROM GatewaySinkFormat" + i2s(temp.gatewayID);
2669         MY_SQLITE_PREPARE_V2(mpDatabase, commandConnectionFormat.c_str(), -1, &qSinkConnectionFormat, NULL)
2670         while ((eCode = sqlite3_step(qSinkConnectionFormat)) == SQLITE_ROW)
2671         {
2672             tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qSinkConnectionFormat, 0);
2673             temp.listSinkFormats.push_back(tempConnectionFormat);
2674         }
2675
2676         MY_SQLITE_FINALIZE(qSinkConnectionFormat)
2677
2678         listGateways.push_back(temp);
2679         temp.listSinkFormats.clear();
2680         temp.listSourceFormats.clear();
2681     }
2682
2683     if (eCode != SQLITE_DONE)
2684     {
2685         logError("DatabaseHandler::getListGateways SQLITE error code:", eCode);
2686         MY_SQLITE_FINALIZE(query)
2687         return (E_DATABASE_ERROR);
2688     }
2689
2690     MY_SQLITE_FINALIZE(query)
2691
2692     return (E_OK);
2693 }
2694
2695 am_Error_e CAmDatabaseHandler::getListSinkClasses(std::vector<am_SinkClass_s> & listSinkClasses) const
2696 {
2697     listSinkClasses.clear();
2698
2699     sqlite3_stmt* query = NULL, *subQuery = NULL;
2700     int eCode = 0;
2701     am_SinkClass_s classTemp;
2702     am_ClassProperty_s propertyTemp;
2703
2704     std::string command = "SELECT sinkClassID, name FROM " + std::string(SINK_CLASS_TABLE);
2705     std::string command2;
2706     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2707
2708     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2709     {
2710         classTemp.sinkClassID = sqlite3_column_int(query, 0);
2711         classTemp.name = std::string((const char*) sqlite3_column_text(query, 1));
2712
2713         //read out Properties
2714         command2 = "SELECT classProperty, value FROM SinkClassProperties" + i2s(classTemp.sinkClassID);
2715         MY_SQLITE_PREPARE_V2(mpDatabase, command2.c_str(), -1, &subQuery, NULL)
2716
2717         while ((eCode = sqlite3_step(subQuery)) == SQLITE_ROW)
2718         {
2719             propertyTemp.classProperty = (am_ClassProperty_e) sqlite3_column_int(subQuery, 0);
2720             propertyTemp.value = sqlite3_column_int(subQuery, 1);
2721             classTemp.listClassProperties.push_back(propertyTemp);
2722         }
2723
2724         if (eCode != SQLITE_DONE)
2725         {
2726             logError("DatabaseHandler::getListSourceClasses SQLITE error code:", eCode);
2727
2728             return (E_DATABASE_ERROR);
2729         }
2730
2731         MY_SQLITE_FINALIZE(subQuery)
2732
2733         listSinkClasses.push_back(classTemp);
2734     }
2735
2736     if (eCode != SQLITE_DONE)
2737     {
2738         logError("DatabaseHandler::getListSourceClasses SQLITE error code:", eCode);
2739         MY_SQLITE_FINALIZE(query)
2740         return (E_DATABASE_ERROR);
2741     }
2742
2743     MY_SQLITE_FINALIZE(query)
2744
2745     return (E_OK);
2746 }
2747
2748 am_Error_e CAmDatabaseHandler::getListVisibleMainConnections(std::vector<am_MainConnectionType_s> & listConnections) const
2749 {
2750     listConnections.clear();
2751     sqlite3_stmt *query = NULL;
2752     int eCode = 0;
2753     am_MainConnectionType_s temp;
2754
2755     std::string command = "SELECT mainConnectionID, sourceID, sinkID, connectionState, delay FROM " + std::string(MAINCONNECTION_TABLE);
2756     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2757
2758     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2759     {
2760         temp.mainConnectionID = sqlite3_column_int(query, 0);
2761         temp.sourceID = sqlite3_column_int(query, 1);
2762         temp.sinkID = sqlite3_column_int(query, 2);
2763         temp.connectionState = (am_ConnectionState_e) sqlite3_column_int(query, 3);
2764         temp.delay = sqlite3_column_int(query, 4);
2765         listConnections.push_back(temp);
2766     }
2767
2768     if (eCode != SQLITE_DONE)
2769     {
2770         logError("DatabaseHandler::getListVisibleMainConnections SQLITE error code:", eCode);
2771         MY_SQLITE_FINALIZE(query)
2772         return (E_DATABASE_ERROR);
2773     }
2774
2775     MY_SQLITE_FINALIZE(query)
2776
2777     return (E_OK);
2778 }
2779
2780 am_Error_e CAmDatabaseHandler::getListMainSinks(std::vector<am_SinkType_s> & listMainSinks) const
2781 {
2782     listMainSinks.clear();
2783     sqlite3_stmt* query = NULL;
2784     int eCode = 0;
2785     am_SinkType_s temp;
2786
2787     std::string command = "SELECT name, sinkID, availability, availabilityReason, muteState, mainVolume, sinkClassID FROM " + std::string(SINK_TABLE) + " WHERE visible=1 AND reserved=0";
2788     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2789
2790     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2791     {
2792         temp.name = std::string((const char*) sqlite3_column_text(query, 0));
2793         temp.sinkID = sqlite3_column_int(query, 1);
2794         temp.availability.availability = (am_Availability_e) sqlite3_column_int(query, 2);
2795         temp.availability.availabilityReason = (am_AvailabilityReason_e) sqlite3_column_int(query, 3);
2796         temp.muteState = (am_MuteState_e) sqlite3_column_int(query, 4);
2797         temp.volume = sqlite3_column_int(query, 5);
2798         temp.sinkClassID = sqlite3_column_int(query, 6);
2799         listMainSinks.push_back(temp);
2800     }
2801
2802     if (eCode != SQLITE_DONE)
2803     {
2804         logError("DatabaseHandler::getListSinks SQLITE error code:", eCode);
2805         MY_SQLITE_FINALIZE(query)
2806         return (E_DATABASE_ERROR);
2807     }
2808
2809     MY_SQLITE_FINALIZE(query)
2810
2811     return (E_OK);
2812 }
2813
2814 am_Error_e CAmDatabaseHandler::getListMainSources(std::vector<am_SourceType_s> & listMainSources) const
2815 {
2816     listMainSources.clear();
2817     sqlite3_stmt* query = NULL;
2818     int eCode = 0;
2819     am_SourceType_s temp;
2820     std::string command = "SELECT name, sourceClassID, availability, availabilityReason, sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE visible=1";
2821     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2822
2823     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2824     {
2825         temp.name = std::string((const char*) sqlite3_column_text(query, 0));
2826         temp.sourceClassID = sqlite3_column_int(query, 1);
2827         temp.availability.availability = (am_Availability_e) sqlite3_column_int(query, 2);
2828         temp.availability.availabilityReason = (am_AvailabilityReason_e) sqlite3_column_int(query, 3);
2829         temp.sourceID = sqlite3_column_int(query, 4);
2830
2831         listMainSources.push_back(temp);
2832     }
2833
2834     if (eCode != SQLITE_DONE)
2835     {
2836         logError("DatabaseHandler::getListSources SQLITE error code:", eCode);
2837         MY_SQLITE_FINALIZE(query)
2838         return (E_DATABASE_ERROR);
2839     }
2840
2841     MY_SQLITE_FINALIZE(query)
2842
2843     return (E_OK);
2844 }
2845
2846 am_Error_e CAmDatabaseHandler::getListMainSinkSoundProperties(const am_sinkID_t sinkID, std::vector<am_MainSoundProperty_s> & listSoundProperties) const
2847 {
2848     assert(sinkID!=0);
2849     if (!existSink(sinkID))
2850         return (E_DATABASE_ERROR); // todo: here we could change to non existen, but not shown in sequences
2851     listSoundProperties.clear();
2852
2853     sqlite3_stmt* query = NULL;
2854     int eCode = 0;
2855     am_MainSoundProperty_s temp;
2856     std::string command = "SELECT soundPropertyType, value FROM SinkMainSoundProperty" + i2s(sinkID);
2857     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2858
2859     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2860     {
2861         temp.type = (am_MainSoundPropertyType_e) sqlite3_column_int(query, 0);
2862         temp.value = sqlite3_column_int(query, 1);
2863         listSoundProperties.push_back(temp);
2864     }
2865
2866     if (eCode != SQLITE_DONE)
2867     {
2868         logError("DatabaseHandler::getListMainSinkSoundProperties SQLITE error code:", eCode);
2869         MY_SQLITE_FINALIZE(query)
2870         return (E_DATABASE_ERROR);
2871     }
2872
2873     MY_SQLITE_FINALIZE(query)
2874
2875     return (E_OK);
2876 }
2877
2878 am_Error_e CAmDatabaseHandler::getListMainSourceSoundProperties(const am_sourceID_t sourceID, std::vector<am_MainSoundProperty_s> & listSourceProperties) const
2879 {
2880     assert(sourceID!=0);
2881     if (!existSource(sourceID))
2882         return (E_DATABASE_ERROR); // todo: here we could change to non existen, but not shown in sequences
2883     listSourceProperties.clear();
2884
2885     sqlite3_stmt* query = NULL;
2886     int eCode = 0;
2887     am_MainSoundProperty_s temp;
2888     std::string command = "SELECT soundPropertyType, value FROM SourceMainSoundProperty" + i2s(sourceID);
2889     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2890
2891     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2892     {
2893         temp.type = (am_MainSoundPropertyType_e) sqlite3_column_int(query, 0);
2894         temp.value = sqlite3_column_int(query, 1);
2895         listSourceProperties.push_back(temp);
2896     }
2897
2898     if (eCode != SQLITE_DONE)
2899     {
2900         logError("DatabaseHandler::getListMainSinkSoundProperties SQLITE error code:", eCode);
2901         MY_SQLITE_FINALIZE(query)
2902         return (E_DATABASE_ERROR);
2903     }
2904
2905     MY_SQLITE_FINALIZE(query)
2906
2907     return (E_OK);
2908 }
2909
2910 am_Error_e CAmDatabaseHandler::getListSystemProperties(std::vector<am_SystemProperty_s> & listSystemProperties) const
2911 {
2912     listSystemProperties.clear();
2913
2914     sqlite3_stmt* query = NULL;
2915     int eCode = 0;
2916     am_SystemProperty_s temp;
2917     std::string command = "SELECT type, value FROM " + std::string(SYSTEM_TABLE);
2918     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
2919
2920     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2921     {
2922         temp.type = (am_SystemPropertyType_e) sqlite3_column_int(query, 0);
2923         temp.value = sqlite3_column_int(query, 1);
2924         listSystemProperties.push_back(temp);
2925     }
2926
2927     if (eCode != SQLITE_DONE)
2928     {
2929         logError("DatabaseHandler::getListSystemProperties SQLITE error code:", eCode);
2930         MY_SQLITE_FINALIZE(query)
2931         return (E_DATABASE_ERROR);
2932     }
2933
2934     MY_SQLITE_FINALIZE(query)
2935
2936     return (E_OK);
2937 }
2938
2939 am_Error_e am::CAmDatabaseHandler::getListSinkConnectionFormats(const am_sinkID_t sinkID, std::vector<am_ConnectionFormat_e> & listConnectionFormats) const
2940 {
2941     listConnectionFormats.clear();
2942     sqlite3_stmt *qConnectionFormat = NULL;
2943     int eCode = 0;
2944     am_ConnectionFormat_e tempConnectionFormat;
2945     std::string commandConnectionFormat = "SELECT soundFormat FROM SinkConnectionFormat" + i2s(sinkID);
2946     MY_SQLITE_PREPARE_V2(mpDatabase, commandConnectionFormat.c_str(), -1, &qConnectionFormat, NULL)
2947     while ((eCode = sqlite3_step(qConnectionFormat)) == SQLITE_ROW)
2948     {
2949         tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qConnectionFormat, 0);
2950         listConnectionFormats.push_back(tempConnectionFormat);
2951     }
2952
2953     MY_SQLITE_FINALIZE(qConnectionFormat)
2954
2955     return (E_OK);
2956 }
2957
2958 am_Error_e am::CAmDatabaseHandler::getListSourceConnectionFormats(const am_sourceID_t sourceID, std::vector<am_ConnectionFormat_e> & listConnectionFormats) const
2959 {
2960     listConnectionFormats.clear();
2961     sqlite3_stmt* qConnectionFormat = NULL;
2962     int eCode = 0;
2963     am_ConnectionFormat_e tempConnectionFormat;
2964
2965     //read out the connectionFormats
2966     std::string commandConnectionFormat = "SELECT soundFormat FROM SourceConnectionFormat" + i2s(sourceID);
2967     MY_SQLITE_PREPARE_V2(mpDatabase, commandConnectionFormat.c_str(), -1, &qConnectionFormat, NULL)
2968     while ((eCode = sqlite3_step(qConnectionFormat)) == SQLITE_ROW)
2969     {
2970         tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qConnectionFormat, 0);
2971         listConnectionFormats.push_back(tempConnectionFormat);
2972     }
2973
2974     MY_SQLITE_FINALIZE(qConnectionFormat)
2975
2976     return (E_OK);
2977 }
2978
2979 am_Error_e am::CAmDatabaseHandler::getListGatewayConnectionFormats(const am_gatewayID_t gatewayID, std::vector<bool> & listConnectionFormat) const
2980 {
2981     ListConnectionFormat::const_iterator iter = mListConnectionFormat.begin();
2982     iter = mListConnectionFormat.find(gatewayID);
2983     if (iter == mListConnectionFormat.end())
2984     {
2985         logError("DatabaseHandler::getListGatewayConnectionFormats database error with convertionFormat");
2986
2987         return (E_DATABASE_ERROR);
2988     }
2989     listConnectionFormat = iter->second;
2990
2991     return (E_OK);
2992 }
2993
2994 am_Error_e CAmDatabaseHandler::getTimingInformation(const am_mainConnectionID_t mainConnectionID, am_timeSync_t & delay) const
2995 {
2996     assert(mainConnectionID!=0);
2997     delay = -1;
2998     sqlite3_stmt *query = NULL;
2999     int eCode = 0;
3000
3001     std::string command = "SELECT delay FROM " + std::string(MAINCONNECTION_TABLE) + " WHERE mainConnectionID=" + i2s(mainConnectionID);
3002     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3003
3004     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3005     {
3006         delay = sqlite3_column_int(query, 0);
3007     }
3008
3009     if (eCode != SQLITE_DONE)
3010     {
3011         logError("DatabaseHandler::getTimingInformation SQLITE error code:", eCode);
3012         MY_SQLITE_FINALIZE(query)
3013         return (E_DATABASE_ERROR);
3014     }
3015
3016     MY_SQLITE_FINALIZE(query)
3017
3018     if (delay == -1)
3019         return (E_NOT_POSSIBLE);
3020
3021     return (E_OK);
3022 }
3023
3024 bool CAmDatabaseHandler::sqQuery(const std::string& query)
3025 {
3026     sqlite3_stmt* statement;
3027     int eCode = 0;
3028     if ((eCode = sqlite3_exec(mpDatabase, query.c_str(), NULL, &statement, NULL)) != SQLITE_OK)
3029     {
3030         logError("DatabaseHandler::sqQuery SQL Query failed:", query.c_str(), "error code:", eCode);
3031         return (false);
3032     }
3033     return (true);
3034 }
3035
3036 bool CAmDatabaseHandler::openDatabase()
3037 {
3038     if (sqlite3_open_v2(mPath.c_str(), &mpDatabase, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL) == SQLITE_OK)
3039     {
3040         logInfo("DatabaseHandler::openDatabase opened database");
3041         return (true);
3042     }
3043     logError("DatabaseHandler::openDatabase failed to open database");
3044     return (false);
3045 }
3046
3047 am_Error_e CAmDatabaseHandler::changeDelayMainConnection(const am_timeSync_t & delay, const am_mainConnectionID_t & connectionID)
3048 {
3049     assert(connectionID!=0);
3050
3051     sqlite3_stmt* query = NULL;
3052     int eCode = 0;
3053     std::string command = "SELECT mainConnectionID FROM " + std::string(MAINCONNECTION_TABLE) + " WHERE delay=? AND mainConnectionID=?";
3054     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3055     MY_SQLITE_BIND_INT(query, 1, delay)
3056     MY_SQLITE_BIND_INT(query, 2, connectionID)
3057     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3058     {
3059         MY_SQLITE_FINALIZE(query)
3060         return (E_OK);
3061     }
3062     command = "UPDATE " + std::string(MAINCONNECTION_TABLE) + " SET delay=? WHERE mainConnectionID=?;";
3063     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3064     MY_SQLITE_BIND_INT(query, 1, delay)
3065     MY_SQLITE_BIND_INT(query, 2, connectionID)
3066
3067     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3068     {
3069         logError("DatabaseHandler::changeDelayMainConnection SQLITE Step error code:", eCode);
3070
3071         MY_SQLITE_FINALIZE(query)
3072         return (E_DATABASE_ERROR);
3073     }
3074
3075     if (mpDatabaseObserver)
3076         mpDatabaseObserver->timingInformationChanged(connectionID, delay);
3077
3078     MY_SQLITE_FINALIZE(query)
3079     return (E_OK);
3080 }
3081
3082 am_Error_e CAmDatabaseHandler::enterConnectionDB(const am_Connection_s& connection, am_connectionID_t& connectionID)
3083 {
3084     assert(connection.connectionID==0);
3085     assert(connection.sinkID!=0);
3086     assert(connection.sourceID!=0);
3087     //connection format is not checked, because it's project specific
3088
3089     sqlite3_stmt* query = NULL;
3090     int eCode = 0;
3091     std::string command = "INSERT INTO " + std::string(CONNECTION_TABLE) + "(sinkID, sourceID, delay, connectionFormat, reserved) VALUES (?,?,?,?,?)";
3092
3093     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3094     MY_SQLITE_BIND_INT(query, 1, connection.sinkID)
3095     MY_SQLITE_BIND_INT(query, 2, connection.sourceID)
3096     MY_SQLITE_BIND_INT(query, 3, connection.delay)
3097     MY_SQLITE_BIND_INT(query, 4, connection.connectionFormat)
3098     MY_SQLITE_BIND_INT(query, 5, true)
3099
3100     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3101     {
3102         logError("DatabaseHandler::enterConnectionDB SQLITE Step error code:", eCode);
3103         MY_SQLITE_FINALIZE(query)
3104         return (E_DATABASE_ERROR);
3105     }
3106
3107     MY_SQLITE_FINALIZE(query)
3108
3109     connectionID = sqlite3_last_insert_rowid(mpDatabase);
3110
3111     logInfo("DatabaseHandler::enterConnectionDB entered new connection sourceID=", connection.sourceID, "sinkID=", connection.sinkID, "sourceID=", connection.sourceID, "connectionFormat=", connection.connectionFormat, "assigned ID=", connectionID);
3112     return (E_OK);
3113 }
3114
3115 am_Error_e CAmDatabaseHandler::enterSinkClassDB(const am_SinkClass_s & sinkClass, am_sinkClass_t & sinkClassID)
3116 {
3117     assert(sinkClass.sinkClassID<DYNAMIC_ID_BOUNDARY);
3118     assert(!sinkClass.name.empty());
3119
3120     sqlite3_stmt* query = NULL;
3121     int eCode = 0;
3122     std::string command;
3123
3124     //if sinkID is zero and the first Static Sink was already entered, the ID is created
3125     if (sinkClass.sinkClassID == 0 && !mFirstStaticSinkClass)
3126     {
3127         command = "INSERT INTO " + std::string(SINK_CLASS_TABLE) + "(name) VALUES (?)";
3128     }
3129     else
3130     {
3131         //check if the ID already exists
3132         if (existSinkClass(sinkClass.sinkClassID))
3133             return (E_ALREADY_EXISTS);
3134         command = "INSERT INTO " + std::string(SINK_CLASS_TABLE) + "(name, sinkClassID) VALUES (?,?)";
3135     }
3136
3137     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3138     MY_SQLITE_BIND_TEXT(query, 1, sinkClass.name.c_str(), sinkClass.name.size(), SQLITE_STATIC)
3139
3140     //if the ID is not created, we add it to the query
3141     if (sinkClass.sinkClassID != 0)
3142     {
3143         MY_SQLITE_BIND_INT(query, 2, sinkClass.sinkClassID)
3144     }
3145
3146     //if the first static sink is entered, we need to set it onto the boundary
3147     else if (mFirstStaticSinkClass)
3148     {
3149         MY_SQLITE_BIND_INT(query, 2, DYNAMIC_ID_BOUNDARY)
3150         mFirstStaticSinkClass = false;
3151     }
3152
3153     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3154     {
3155         logError("DatabaseHandler::enterSinkClassDB SQLITE Step error code:", eCode);
3156         MY_SQLITE_FINALIZE(query)
3157         return (E_DATABASE_ERROR);
3158     }
3159
3160     MY_SQLITE_FINALIZE(query)
3161
3162     sinkClassID = sqlite3_last_insert_rowid(mpDatabase); //todo:change last_insert implementations for mulithread usage...
3163
3164     //now we need to create the additional tables:
3165     command = "CREATE TABLE SinkClassProperties" + i2s(sinkClassID) + std::string("(classProperty INTEGER, value INTEGER)");
3166     if (!this->sqQuery(command))
3167         return (E_DATABASE_ERROR);
3168
3169     //fill ConnectionFormats
3170     command = "INSERT INTO SinkClassProperties" + i2s(sinkClassID) + std::string("(classProperty,value) VALUES (?,?)");
3171     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3172     std::vector<am_ClassProperty_s>::const_iterator Iterator = sinkClass.listClassProperties.begin();
3173     for (; Iterator < sinkClass.listClassProperties.end(); ++Iterator)
3174     {
3175         MY_SQLITE_BIND_INT(query, 1, Iterator->classProperty)
3176         MY_SQLITE_BIND_INT(query, 2, Iterator->value)
3177         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3178         {
3179             logError("DatabaseHandler::enterSinkClassDB SQLITE Step error code:", eCode);
3180             MY_SQLITE_FINALIZE(query)
3181             return (E_DATABASE_ERROR);
3182         }
3183         MY_SQLITE_RESET(query)
3184     }
3185
3186     MY_SQLITE_FINALIZE(query)
3187
3188     logInfo("DatabaseHandler::enterSinkClassDB entered new sinkClass");
3189     if (mpDatabaseObserver)
3190         mpDatabaseObserver->numberOfSinkClassesChanged();
3191     return (E_OK);
3192 }
3193
3194 am_Error_e CAmDatabaseHandler::enterSourceClassDB(am_sourceClass_t & sourceClassID, const am_SourceClass_s & sourceClass)
3195 {
3196     assert(sourceClass.sourceClassID<DYNAMIC_ID_BOUNDARY);
3197     assert(!sourceClass.name.empty());
3198
3199     sqlite3_stmt* query = NULL;
3200     int eCode = 0;
3201     std::string command;
3202
3203     //if sinkID is zero and the first Static Sink was already entered, the ID is created
3204     if (sourceClass.sourceClassID == 0 && !mFirstStaticSourceClass)
3205     {
3206         command = "INSERT INTO " + std::string(SOURCE_CLASS_TABLE) + "(name) VALUES (?)";
3207     }
3208     else
3209     {
3210         //check if the ID already exists
3211         if (existSourceClass(sourceClass.sourceClassID))
3212             return (E_ALREADY_EXISTS);
3213         command = "INSERT INTO " + std::string(SOURCE_CLASS_TABLE) + "(name, sourceClassID) VALUES (?,?)";
3214     }
3215
3216     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3217     MY_SQLITE_BIND_TEXT(query, 1, sourceClass.name.c_str(), sourceClass.name.size(), SQLITE_STATIC)
3218
3219     //if the ID is not created, we add it to the query
3220     if (sourceClass.sourceClassID != 0)
3221     {
3222         MY_SQLITE_BIND_INT(query, 2, sourceClass.sourceClassID)
3223     }
3224
3225     //if the first static sink is entered, we need to set it onto the boundary
3226     else if (mFirstStaticSourceClass)
3227     {
3228         MY_SQLITE_BIND_INT(query, 2, DYNAMIC_ID_BOUNDARY)
3229         mFirstStaticSourceClass = false;
3230     }
3231
3232     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3233     {
3234         logError("DatabaseHandler::enterSourceClassDB SQLITE Step error code:", eCode);
3235         MY_SQLITE_FINALIZE(query)
3236         return (E_DATABASE_ERROR);
3237     }
3238
3239     MY_SQLITE_FINALIZE(query)
3240
3241     sourceClassID = sqlite3_last_insert_rowid(mpDatabase); //todo:change last_insert implementations for mulithread usage...
3242
3243     //now we need to create the additional tables:
3244     command = "CREATE TABLE SourceClassProperties" + i2s(sourceClassID) + std::string("(classProperty INTEGER, value INTEGER)");
3245     if (!this->sqQuery(command))
3246         return (E_DATABASE_ERROR);
3247
3248     //fill ConnectionFormats
3249     command = "INSERT INTO SourceClassProperties" + i2s(sourceClassID) + std::string("(classProperty,value) VALUES (?,?)");
3250     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3251     std::vector<am_ClassProperty_s>::const_iterator Iterator = sourceClass.listClassProperties.begin();
3252     for (; Iterator < sourceClass.listClassProperties.end(); ++Iterator)
3253     {
3254         MY_SQLITE_BIND_INT(query, 1, Iterator->classProperty)
3255         MY_SQLITE_BIND_INT(query, 2, Iterator->value)
3256         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3257         {
3258             logError("DatabaseHandler::enterSourceClassDB SQLITE Step error code:", eCode);
3259             MY_SQLITE_FINALIZE(query)
3260             return (E_DATABASE_ERROR);
3261         }
3262         MY_SQLITE_RESET(query)
3263     }
3264
3265     MY_SQLITE_FINALIZE(query)
3266
3267     logInfo("DatabaseHandler::enterSourceClassDB entered new sourceClass");
3268
3269     if (mpDatabaseObserver)
3270         mpDatabaseObserver->numberOfSourceClassesChanged();
3271     return (E_OK);
3272 }
3273
3274 am_Error_e CAmDatabaseHandler::enterSystemProperties(const std::vector<am_SystemProperty_s> & listSystemProperties)
3275 {
3276     assert(!listSystemProperties.empty());
3277     sqlite3_stmt* query = NULL;
3278     int eCode = 0;
3279     std::vector<am_SystemProperty_s>::const_iterator listIterator = listSystemProperties.begin();
3280     std::string command = "DELETE  FROM " + std::string(SYSTEM_TABLE);
3281     if (!this->sqQuery(command))
3282         return (E_DATABASE_ERROR);
3283
3284     command = "INSERT INTO " + std::string(SYSTEM_TABLE) + " (type, value) VALUES (?,?)";
3285
3286     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3287     for (; listIterator < listSystemProperties.end(); ++listIterator)
3288     {
3289         MY_SQLITE_BIND_INT(query, 1, listIterator->type)
3290         MY_SQLITE_BIND_INT(query, 2, listIterator->value)
3291
3292         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3293         {
3294             logError("DatabaseHandler::enterSystemProperties SQLITE Step error code:", eCode);
3295             MY_SQLITE_FINALIZE(query)
3296             return (E_DATABASE_ERROR);
3297         }
3298
3299         MY_SQLITE_RESET(query)
3300     }
3301
3302     MY_SQLITE_FINALIZE(query)
3303
3304     logInfo("DatabaseHandler::enterSystemProperties entered system properties");
3305     return (E_OK);
3306 }
3307
3308 /**
3309  * checks for a certain mainConnection
3310  * @param mainConnectionID to be checked for
3311  * @return true if it exists
3312  */
3313 bool CAmDatabaseHandler::existMainConnection(const am_mainConnectionID_t mainConnectionID) const
3314 {
3315     sqlite3_stmt* query = NULL;
3316     std::string command = "SELECT mainConnectionID FROM " + std::string(MAINCONNECTION_TABLE) + " WHERE mainConnectionID=" + i2s(mainConnectionID);
3317     int eCode = 0;
3318     bool returnVal = true;
3319     MY_SQLITE_PREPARE_V2_BOOL(mpDatabase, command.c_str(), -1, &query, NULL)
3320     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3321         returnVal = false;
3322     else if (eCode != SQLITE_ROW)
3323     {
3324         returnVal = false;
3325         logError("DatabaseHandler::existMainConnection database error!:", eCode);
3326     }
3327
3328     MY_SQLITE_FINALIZE_BOOL(query)
3329     return (returnVal);
3330 }
3331
3332 /**
3333  * checks for a certain Source
3334  * @param sourceID to be checked for
3335  * @return true if it exists
3336  */
3337 bool CAmDatabaseHandler::existSource(const am_sourceID_t sourceID) const
3338 {
3339     sqlite3_stmt* query = NULL;
3340     std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE reserved=0 AND sourceID=" + i2s(sourceID);
3341     int eCode = 0;
3342     bool returnVal = true;
3343     MY_SQLITE_PREPARE_V2_BOOL(mpDatabase, command.c_str(), -1, &query, NULL)
3344     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3345         returnVal = false;
3346     else if (eCode != SQLITE_ROW)
3347     {
3348         returnVal = false;
3349         logError("DatabaseHandler::existSource database error!:", eCode);
3350     }
3351     MY_SQLITE_FINALIZE_BOOL(query)
3352     return (returnVal);
3353 }
3354
3355 /**
3356  * checks if a source name or ID exists
3357  * @param sourceID the sourceID
3358  * @param name the name
3359  * @return true if it exits
3360  */
3361 bool CAmDatabaseHandler::existSourceNameOrID(const am_sourceID_t sourceID, const std::string & name) const
3362 {
3363     sqlite3_stmt* query = NULL;
3364     std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE reserved=0 AND (name=? OR sourceID=?)";
3365     int eCode = 0;
3366     bool returnVal = true;
3367     MY_SQLITE_PREPARE_V2_BOOL(mpDatabase, command.c_str(), -1, &query, NULL)
3368
3369     if ((eCode = sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC)))
3370     {
3371         logError("CAmDatabaseHandler::sqlite3_bind_text failed with errorCode:", eCode);
3372         return (false);
3373     }
3374
3375     if ((eCode = sqlite3_bind_int(query, 2, sourceID)))
3376     {
3377         logError("CAmDatabaseHandler::sqlite3_bind_int failed with errorCode:", eCode);
3378         return (false);
3379     }
3380
3381     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3382         returnVal = false;
3383     else if (eCode != SQLITE_ROW)
3384     {
3385         returnVal = false;
3386         logError("DatabaseHandler::existSource database error!:", eCode);
3387     }
3388     MY_SQLITE_FINALIZE_BOOL(query)
3389     return (returnVal);
3390 }
3391
3392 /**
3393  * checks if a name exits
3394  * @param name the name
3395  * @return true if it exits
3396  */
3397 bool CAmDatabaseHandler::existSourceName(const std::string & name) const
3398 {
3399     sqlite3_stmt* query = NULL;
3400     std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE reserved=0 AND name=?";
3401     int eCode = 0;
3402     bool returnVal = true;
3403     MY_SQLITE_PREPARE_V2_BOOL(mpDatabase, command.c_str(), -1, &query, NULL)
3404
3405     if ((eCode = sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC)))
3406     {
3407         logError("CAmDatabaseHandler::sqlite3_bind_text failed with errorCode:", eCode);
3408         return (false);
3409     }
3410
3411     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3412         returnVal = false;
3413     else if (eCode != SQLITE_ROW)
3414     {
3415         returnVal = false;
3416         logError("DatabaseHandler::existSource database error!:", eCode);
3417     }
3418
3419     MY_SQLITE_FINALIZE_BOOL(query)
3420     return (returnVal);
3421 }
3422
3423 /**
3424  * checks for a certain Sink
3425  * @param sinkID to be checked for
3426  * @return true if it exists
3427  */
3428 bool CAmDatabaseHandler::existSink(const am_sinkID_t sinkID) const
3429 {
3430     sqlite3_stmt* query = NULL;
3431     std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 AND sinkID=" + i2s(sinkID);
3432     int eCode = 0;
3433     bool returnVal = true;
3434     MY_SQLITE_PREPARE_V2_BOOL(mpDatabase, command.c_str(), -1, &query, NULL)
3435
3436     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3437         returnVal = false;
3438     else if (eCode != SQLITE_ROW)
3439     {
3440         returnVal = false;
3441         logError("DatabaseHandler::existSink database error!:", eCode);
3442     }
3443     MY_SQLITE_FINALIZE_BOOL(query)
3444     return (returnVal);
3445 }
3446
3447 /**
3448  * checks if a sink with the ID or the name exists
3449  * @param sinkID the ID
3450  * @param name the name
3451  * @return true if it exists.
3452  */
3453 bool CAmDatabaseHandler::existSinkNameOrID(const am_sinkID_t sinkID, const std::string & name) const
3454 {
3455     sqlite3_stmt* query = NULL;
3456     std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 AND (name=? OR sinkID=?)";
3457     int eCode = 0;
3458     bool returnVal = true;
3459     MY_SQLITE_PREPARE_V2_BOOL(mpDatabase, command.c_str(), -1, &query, NULL)
3460
3461     if ((eCode = sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC)))
3462     {
3463         logError("CAmDatabaseHandler::sqlite3_bind_text failed with errorCode:", eCode);
3464         return (false);
3465     }
3466
3467     if ((eCode = sqlite3_bind_int(query, 2, sinkID)))
3468     {
3469         logError("CAmDatabaseHandler::sqlite3_bind_int failed with errorCode:", eCode);
3470         return (false);
3471     }
3472
3473     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3474         returnVal = false;
3475     else if (eCode != SQLITE_ROW)
3476     {
3477         returnVal = false;
3478         logError("DatabaseHandler::existSink database error!:", eCode);
3479     }
3480
3481     MY_SQLITE_FINALIZE_BOOL(query)
3482     return (returnVal);
3483 }
3484
3485 /**
3486  * checks if a sink with the name exists
3487  * @param name the name
3488  * @return true if it exists
3489  */
3490 bool CAmDatabaseHandler::existSinkName(const std::string & name) const
3491 {
3492     sqlite3_stmt* query = NULL;
3493     std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 AND name=?";
3494     int eCode = 0;
3495     bool returnVal = true;
3496     MY_SQLITE_PREPARE_V2_BOOL(mpDatabase, command.c_str(), -1, &query, NULL)
3497
3498     if ((eCode = sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC)))
3499     {
3500         logError("CAmDatabaseHandler::sqlite3_bind_text failed with errorCode:", eCode);
3501         return (false);
3502     }
3503
3504     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3505         returnVal = false;
3506     else if (eCode != SQLITE_ROW)
3507     {
3508         returnVal = false;
3509         logError("DatabaseHandler::existSink database error!:", eCode);
3510     }
3511
3512     MY_SQLITE_FINALIZE_BOOL(query)
3513     return (returnVal);
3514 }
3515
3516 /**
3517  * checks for a certain domain
3518  * @param domainID to be checked for
3519  * @return true if it exists
3520  */
3521 bool CAmDatabaseHandler::existDomain(const am_domainID_t domainID) const
3522 {
3523     sqlite3_stmt* query = NULL;
3524     std::string command = "SELECT domainID FROM " + std::string(DOMAIN_TABLE) + " WHERE reserved=0 AND domainID=" + i2s(domainID);
3525     int eCode = 0;
3526     bool returnVal = true;
3527     MY_SQLITE_PREPARE_V2_BOOL(mpDatabase, command.c_str(), -1, &query, NULL)
3528
3529     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3530         returnVal = false;
3531     else if (eCode != SQLITE_ROW)
3532     {
3533         returnVal = false;
3534         logError("DatabaseHandler::existDomain database error!:", eCode);
3535     }
3536
3537     MY_SQLITE_FINALIZE_BOOL(query)
3538     return (returnVal);
3539 }
3540
3541 /**
3542  * checks for certain gateway
3543  * @param gatewayID to be checked for
3544  * @return true if it exists
3545  */
3546 bool CAmDatabaseHandler::existGateway(const am_gatewayID_t gatewayID) const
3547 {
3548     sqlite3_stmt* query = NULL;
3549     std::string command = "SELECT gatewayID FROM " + std::string(GATEWAY_TABLE) + " WHERE gatewayID=" + i2s(gatewayID);
3550     int eCode = 0;
3551     bool returnVal = true;
3552     MY_SQLITE_PREPARE_V2_BOOL(mpDatabase, command.c_str(), -1, &query, NULL)
3553
3554     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3555         returnVal = false;
3556     else if (eCode != SQLITE_ROW)
3557     {
3558         returnVal = false;
3559         logError("DatabaseHandler::existGateway database error!:", eCode);
3560     }
3561
3562     MY_SQLITE_FINALIZE_BOOL(query)
3563     return (returnVal);
3564 }
3565
3566 am_Error_e CAmDatabaseHandler::getDomainOfSource(const am_sourceID_t sourceID, am_domainID_t & domainID) const
3567 {
3568     assert(sourceID!=0);
3569
3570     sqlite3_stmt* query = NULL;
3571     std::string command = "SELECT domainID FROM " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + i2s(sourceID);
3572     int eCode = 0;
3573     am_Error_e returnVal = E_DATABASE_ERROR;
3574     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3575     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3576     {
3577         domainID = sqlite3_column_int(query, 0);
3578         returnVal = E_OK;
3579     }
3580     else
3581     {
3582         logError("DatabaseHandler::getDomainOfSource database error!:", eCode);
3583     }
3584
3585     MY_SQLITE_FINALIZE(query)
3586     return (returnVal);
3587 }
3588
3589 am_Error_e am::CAmDatabaseHandler::getDomainOfSink(const am_sinkID_t sinkID, am_domainID_t & domainID) const
3590 {
3591     assert(sinkID!=0);
3592
3593     sqlite3_stmt* query = NULL;
3594     std::string command = "SELECT domainID FROM " + std::string(SINK_TABLE) + " WHERE sinkID=" + i2s(sinkID);
3595     int eCode = 0;
3596     am_Error_e returnVal = E_DATABASE_ERROR;
3597     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3598
3599     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3600     {
3601         domainID = sqlite3_column_int(query, 0);
3602         returnVal = E_OK;
3603     }
3604     else
3605     {
3606         logError("DatabaseHandler::getDomainOfSink database error!:", eCode);
3607     }
3608
3609     MY_SQLITE_FINALIZE(query)
3610     return (returnVal);
3611 }
3612
3613 /**
3614  * checks for certain SinkClass
3615  * @param sinkClassID
3616  * @return true if it exists
3617  */
3618 bool CAmDatabaseHandler::existSinkClass(const am_sinkClass_t sinkClassID) const
3619 {
3620     sqlite3_stmt* query = NULL;
3621     std::string command = "SELECT sinkClassID FROM " + std::string(SINK_CLASS_TABLE) + " WHERE sinkClassID=" + i2s(sinkClassID);
3622     int eCode = 0;
3623     bool returnVal = true;
3624     MY_SQLITE_PREPARE_V2_BOOL(mpDatabase, command.c_str(), -1, &query, NULL)
3625     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3626         returnVal = false;
3627     else if (eCode != SQLITE_ROW)
3628     {
3629         returnVal = false;
3630         logError("DatabaseHandler::existSinkClass database error!:", eCode);
3631     }
3632
3633     MY_SQLITE_FINALIZE_BOOL(query)
3634     return (returnVal);
3635 }
3636
3637 /**
3638  * checks for certain sourceClass
3639  * @param sourceClassID
3640  * @return true if it exists
3641  */
3642 bool CAmDatabaseHandler::existSourceClass(const am_sourceClass_t sourceClassID) const
3643 {
3644     sqlite3_stmt* query = NULL;
3645     std::string command = "SELECT sourceClassID FROM " + std::string(SOURCE_CLASS_TABLE) + " WHERE sourceClassID=" + i2s(sourceClassID);
3646     int eCode = 0;
3647     bool returnVal = true;
3648     MY_SQLITE_PREPARE_V2_BOOL(mpDatabase, command.c_str(), -1, &query, NULL)
3649
3650     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3651         returnVal = false;
3652     else if (eCode != SQLITE_ROW)
3653     {
3654         returnVal = false;
3655         logError("DatabaseHandler::existSinkClass database error!:", eCode);
3656     }
3657
3658     MY_SQLITE_FINALIZE_BOOL(query)
3659     return (returnVal);
3660 }
3661
3662 am_Error_e CAmDatabaseHandler::changeConnectionTimingInformation(const am_connectionID_t connectionID, const am_timeSync_t delay)
3663 {
3664     assert(connectionID!=0);
3665
3666     sqlite3_stmt *query = NULL, *queryMainConnectionSubIDs = NULL;
3667     int eCode = 0, eCode1 = 0;
3668     std::string command = "UPDATE " + std::string(CONNECTION_TABLE) + " set delay=? WHERE connectionID=?";
3669
3670     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3671     MY_SQLITE_BIND_INT(query, 1, delay)
3672     MY_SQLITE_BIND_INT(query, 2, connectionID)
3673
3674     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3675     {
3676         logError("DatabaseHandler::changeConnectionTimingInformation SQLITE Step error code:", eCode);
3677         MY_SQLITE_FINALIZE(query)
3678         return (E_DATABASE_ERROR);
3679     }
3680
3681     MY_SQLITE_FINALIZE(query)
3682
3683     //now we need to find all mainConnections that use the changed connection and update their timing
3684
3685     int tempMainConnectionID;
3686     //first get all route tables for all mainconnections
3687     command = "SELECT name FROM sqlite_master WHERE type ='table' and name LIKE 'MainConnectionRoute%'";
3688     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3689
3690     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3691     {
3692         //now check if the connection ID is in this table
3693         std::string tablename = std::string((const char*) sqlite3_column_text(query, 0));
3694         std::string command2 = "SELECT connectionID FROM " + tablename + " WHERE connectionID=" + i2s(connectionID);
3695         MY_SQLITE_PREPARE_V2(mpDatabase, command2.c_str(), -1, &queryMainConnectionSubIDs, NULL)
3696         if ((eCode1 = sqlite3_step(queryMainConnectionSubIDs)) == SQLITE_ROW)
3697         {
3698             //if the connection ID is in, recalculate the mainconnection delay
3699             std::stringstream(tablename.substr(tablename.find_first_not_of("MainConnectionRoute"))) >> tempMainConnectionID;
3700             changeDelayMainConnection(calculateMainConnectionDelay(tempMainConnectionID), tempMainConnectionID);
3701         }
3702         else if (eCode1 != SQLITE_DONE)
3703         {
3704             logError("DatabaseHandler::changeConnectionTimingInformation SQLITE error code:", eCode1);
3705
3706             return (E_DATABASE_ERROR);
3707         }
3708         MY_SQLITE_FINALIZE(queryMainConnectionSubIDs)
3709     }
3710
3711     if (eCode != SQLITE_DONE)
3712     {
3713         logError("DatabaseHandler::changeConnectionTimingInformation SQLITE error code:", eCode);
3714         MY_SQLITE_FINALIZE(query)
3715         return (E_DATABASE_ERROR);
3716     }
3717
3718     MY_SQLITE_FINALIZE(query)
3719
3720     return (E_OK);
3721 }
3722
3723 am_Error_e CAmDatabaseHandler::changeConnectionFinal(const am_connectionID_t connectionID)
3724 {
3725     assert(connectionID!=0);
3726
3727     sqlite3_stmt *query = NULL;
3728     int eCode = 0;
3729     std::string command = "UPDATE " + std::string(CONNECTION_TABLE) + " set reserved=0 WHERE connectionID=?";
3730
3731     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3732     MY_SQLITE_BIND_INT(query, 1, connectionID)
3733
3734     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3735     {
3736         logError("DatabaseHandler::changeConnectionFinal SQLITE Step error code:", eCode);
3737         MY_SQLITE_FINALIZE(query)
3738         return (E_DATABASE_ERROR);
3739     }
3740
3741     MY_SQLITE_FINALIZE(query)
3742     return (E_OK);
3743 }
3744
3745 am_timeSync_t CAmDatabaseHandler::calculateMainConnectionDelay(const am_mainConnectionID_t mainConnectionID) const
3746 {
3747     assert(mainConnectionID!=0);
3748     sqlite3_stmt* query = NULL;
3749     std::string command = "SELECT sum(Connections.delay),min(Connections.delay) FROM " + std::string(CONNECTION_TABLE) + ",MainConnectionRoute" + i2s(mainConnectionID) + " WHERE MainConnectionRoute" + i2s(mainConnectionID) + ".connectionID = Connections.connectionID";
3750     int eCode = 0;
3751     am_timeSync_t delay = 0;
3752     am_timeSync_t min = 0;
3753     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3754     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3755     {
3756         delay = sqlite3_column_int(query, 0);
3757         min = sqlite3_column_int(query, 1);
3758     }
3759     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3760     {
3761         logError("DatabaseHandler::calculateMainConnectionDelay SQLITE Step error code:", eCode);
3762         MY_SQLITE_FINALIZE(query)
3763         return (E_DATABASE_ERROR);
3764     }
3765
3766     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3767     {
3768         logError("DatabaseHandler::calculateMainConnectionDelay SQLITE Finalize error code:", eCode);
3769         return (E_DATABASE_ERROR);
3770     }
3771     if (min < 0)
3772         delay = -1;
3773     return (delay);
3774
3775 }
3776
3777 /**
3778  * registers the Observer at the Database
3779  * @param iObserver pointer to the observer
3780  */
3781 void CAmDatabaseHandler::registerObserver(CAmDatabaseObserver *iObserver)
3782 {
3783     assert(iObserver!=NULL);
3784     mpDatabaseObserver = iObserver;
3785 }
3786
3787 /**
3788  * gives information about the visibility of a source
3789  * @param sourceID the sourceID
3790  * @return true if source is visible
3791  */
3792 bool CAmDatabaseHandler::sourceVisible(const am_sourceID_t sourceID) const
3793 {
3794     assert(sourceID!=0);
3795     sqlite3_stmt* query = NULL;
3796     std::string command = "SELECT visible FROM " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + i2s(sourceID);
3797     int eCode = 0;
3798     bool returnVal = false;
3799     MY_SQLITE_PREPARE_V2_BOOL(mpDatabase, command.c_str(), -1, &query, NULL)
3800
3801     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3802     {
3803         returnVal = (bool) sqlite3_column_int(query, 0);
3804     }
3805     else if (eCode != SQLITE_DONE)
3806     {
3807         returnVal = false;
3808         logError("DatabaseHandler::sourceVisible database error!:", eCode);
3809     }
3810
3811     MY_SQLITE_FINALIZE_BOOL(query)
3812     return (returnVal);
3813 }
3814
3815 /**
3816  * gives information about the visibility of a sink
3817  * @param sinkID the sinkID
3818  * @return true if source is visible
3819  */
3820 bool CAmDatabaseHandler::sinkVisible(const am_sinkID_t sinkID) const
3821 {
3822     sqlite3_stmt* query = NULL;
3823     std::string command = "SELECT visible FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 AND sinkID=" + i2s(sinkID);
3824     int eCode = 0;
3825     bool returnVal = false;
3826     MY_SQLITE_PREPARE_V2_BOOL(mpDatabase, command.c_str(), -1, &query, NULL)
3827     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3828     {
3829         returnVal = sqlite3_column_int(query, 0);
3830     }
3831     else if (eCode != SQLITE_DONE)
3832     {
3833         returnVal = false;
3834         logError("DatabaseHandler::sinkVisible database error!:", eCode);
3835     }
3836
3837     MY_SQLITE_FINALIZE_BOOL(query)
3838     return (returnVal);
3839 }
3840
3841 /**
3842  * checks if a connection already exists.
3843  * Only takes sink, source and format information for search!
3844  * @param connection the connection to be checked
3845  * @return true if connections exists
3846  */
3847 bool CAmDatabaseHandler::existConnection(const am_Connection_s connection)
3848 {
3849     sqlite3_stmt* query = NULL;
3850     std::string command = "SELECT connectionID FROM " + std::string(CONNECTION_TABLE) + " WHERE sinkID=? AND sourceID=? AND connectionFormat=? AND reserved=0";
3851     int eCode = 0;
3852     bool returnVal = true;
3853     MY_SQLITE_PREPARE_V2_BOOL(mpDatabase, command.c_str(), -1, &query, NULL)
3854
3855     if ((eCode = sqlite3_bind_int(query, 1, connection.sinkID)))
3856     {
3857         logError("CAmDatabaseHandler::sqlite3_bind_int failed with errorCode:", eCode);
3858         return (false);
3859     }
3860
3861     if ((eCode = sqlite3_bind_int(query, 2, connection.sourceID)))
3862     {
3863         logError("CAmDatabaseHandler::sqlite3_bind_int failed with errorCode:", eCode);
3864         return (false);
3865     }
3866
3867     if ((eCode = sqlite3_bind_int(query, 3, connection.connectionFormat)))
3868     {
3869         logError("CAmDatabaseHandler::sqlite3_bind_int failed with errorCode:", eCode);
3870         return (false);
3871     }
3872
3873     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3874         returnVal = false;
3875     else if (eCode != SQLITE_ROW)
3876     {
3877         returnVal = false;
3878         logError("DatabaseHandler::existMainConnection database error!:", eCode);
3879     }
3880
3881     MY_SQLITE_FINALIZE_BOOL(query)
3882     return (returnVal);
3883 }
3884
3885 /**
3886  * checks if a connection with the given ID exists
3887  * @param connectionID
3888  * @return true if connection exits
3889  */
3890 bool CAmDatabaseHandler::existConnectionID(const am_connectionID_t connectionID)
3891 {
3892     sqlite3_stmt* query = NULL;
3893     std::string command = "SELECT connectionID FROM " + std::string(CONNECTION_TABLE) + " WHERE connectionID=? AND reserved=0";
3894     int eCode = 0;
3895     bool returnVal = true;
3896     MY_SQLITE_PREPARE_V2_BOOL(mpDatabase, command.c_str(), -1, &query, NULL)
3897
3898     if ((eCode = sqlite3_bind_int(query, 1, connectionID)))
3899     {
3900         logError("CAmDatabaseHandler::sqlite3_bind_int failed with errorCode:", eCode);
3901         return (false);
3902     }
3903
3904     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3905         returnVal = false;
3906     else if (eCode != SQLITE_ROW)
3907     {
3908         returnVal = false;
3909         logError("DatabaseHandler::existMainConnection database error!:", eCode);
3910     }
3911
3912     MY_SQLITE_FINALIZE_BOOL(query)
3913     return (returnVal);
3914 }
3915
3916 /**
3917  * checks if a CrossFader exists
3918  * @param crossfaderID the ID of the crossfader to be checked
3919  * @return true if exists
3920  */
3921 bool CAmDatabaseHandler::existcrossFader(const am_crossfaderID_t crossfaderID) const
3922 {
3923     sqlite3_stmt* query = NULL;
3924     std::string command = "SELECT crossfaderID FROM " + std::string(CROSSFADER_TABLE) + " WHERE crossfaderID=?";
3925     int eCode = 0;
3926     bool returnVal = true;
3927     MY_SQLITE_PREPARE_V2_BOOL(mpDatabase, command.c_str(), -1, &query, NULL)
3928
3929     if ((eCode = sqlite3_bind_int(query, 1, crossfaderID)))
3930     {
3931         logError("CAmDatabaseHandler::sqlite3_bind_int failed with errorCode:", eCode);
3932         return (false);
3933     }
3934
3935     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3936         returnVal = false;
3937     else if (eCode != SQLITE_ROW)
3938     {
3939         returnVal = false;
3940         logError("DatabaseHandler::existMainConnection database error!:", eCode);
3941     }
3942
3943     MY_SQLITE_FINALIZE_BOOL(query)
3944     return (returnVal);
3945 }
3946
3947 am_Error_e CAmDatabaseHandler::getSoureState(const am_sourceID_t sourceID, am_SourceState_e & sourceState) const
3948 {
3949     assert(sourceID!=0);
3950     sqlite3_stmt* query = NULL;
3951     sourceState = SS_UNKNNOWN;
3952     std::string command = "SELECT sourceState FROM " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + i2s(sourceID);
3953     int eCode = 0;
3954     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3955     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3956     {
3957         sourceState = (am_SourceState_e) sqlite3_column_int(query, 0);
3958     }
3959     else if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3960     {
3961         logError("DatabaseHandler::getSoureState database error!:", eCode);
3962     }
3963     MY_SQLITE_FINALIZE(query)
3964     return (E_OK);
3965 }
3966
3967 am_Error_e CAmDatabaseHandler::changeSourceState(const am_sourceID_t sourceID, const am_SourceState_e sourceState)
3968 {
3969     assert(sourceID!=0);
3970     assert(sourceState>=SS_UNKNNOWN && sourceState<=SS_MAX);
3971     sqlite3_stmt* query = NULL;
3972     std::string command = "UPDATE " + std::string(SOURCE_TABLE) + " SET sourceState=? WHERE sourceID=" + i2s(sourceID);
3973     int eCode = 0;
3974     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3975     MY_SQLITE_BIND_INT(query, 1, sourceState)
3976     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3977     {
3978         logError("DatabaseHandler::changeSourceState SQLITE Step error code:", eCode);
3979         MY_SQLITE_FINALIZE(query)
3980         return (E_DATABASE_ERROR);
3981     }
3982
3983     MY_SQLITE_FINALIZE(query)
3984     return (E_OK);
3985 }
3986
3987 am_Error_e CAmDatabaseHandler::getSinkVolume(const am_sinkID_t sinkID, am_volume_t & volume) const
3988 {
3989     assert(sinkID!=0);
3990     sqlite3_stmt* query = NULL;
3991     volume = -1;
3992     std::string command = "SELECT volume FROM " + std::string(SINK_TABLE) + " WHERE sinkID=" + i2s(sinkID);
3993     int eCode = 0;
3994     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
3995     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3996     {
3997         volume = sqlite3_column_int(query, 0);
3998     }
3999     else if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
4000     {
4001         logError("DatabaseHandler::getSinkVolume database error!:", eCode);
4002     }
4003     MY_SQLITE_FINALIZE(query)
4004     return (E_OK);
4005 }
4006
4007 am_Error_e CAmDatabaseHandler::getSourceVolume(const am_sourceID_t sourceID, am_volume_t & volume) const
4008 {
4009     assert(sourceID!=0);
4010     sqlite3_stmt* query = NULL;
4011     volume = -1;
4012     std::string command = "SELECT volume FROM " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + i2s(sourceID);
4013     int eCode = 0;
4014     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4015     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
4016     {
4017         volume = sqlite3_column_int(query, 0);
4018     }
4019     else if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
4020     {
4021         logError("DatabaseHandler::getSourceVolume database error!:", eCode);
4022     }
4023     MY_SQLITE_FINALIZE(query)
4024     return (E_OK);
4025 }
4026
4027 am_Error_e CAmDatabaseHandler::getSinkSoundPropertyValue(const am_sinkID_t sinkID, const am_SoundPropertyType_e propertyType, int16_t & value) const
4028 {
4029     assert(sinkID!=0);
4030     if (!existSink(sinkID))
4031         return (E_DATABASE_ERROR); // todo: here we could change to non existent, but not shown in sequences
4032
4033     sqlite3_stmt* query = NULL;
4034     int eCode = 0;
4035     std::string command = "SELECT value FROM SinkSoundProperty" + i2s(sinkID) + " WHERE soundPropertyType=" + i2s(propertyType);
4036     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4037
4038     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
4039     {
4040         value = sqlite3_column_int(query, 0);
4041     }
4042     else
4043     {
4044         logError("DatabaseHandler::getDomainState database error!:", eCode);
4045         MY_SQLITE_FINALIZE(query)
4046         return (E_DATABASE_ERROR);
4047     }
4048
4049     MY_SQLITE_FINALIZE(query)
4050
4051     return (E_OK);
4052 }
4053
4054 am_Error_e CAmDatabaseHandler::getSourceSoundPropertyValue(const am_sourceID_t sourceID, const am_SoundPropertyType_e propertyType, int16_t & value) const
4055 {
4056     assert(sourceID!=0);
4057     if (!existSource(sourceID))
4058         return (E_DATABASE_ERROR); // todo: here we could change to non existent, but not shown in sequences
4059
4060     sqlite3_stmt* query = NULL;
4061     int eCode = 0;
4062     std::string command = "SELECT value FROM SourceSoundProperty" + i2s(sourceID) + " WHERE soundPropertyType=" + i2s(propertyType);
4063     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4064
4065     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
4066     {
4067         value = sqlite3_column_int(query, 0);
4068     }
4069
4070     if (eCode != SQLITE_DONE)
4071     {
4072         logError("DatabaseHandler::getSinkSoundPropertyValue SQLITE error code:", eCode);
4073         MY_SQLITE_FINALIZE(query)
4074         return (E_DATABASE_ERROR);
4075     }
4076
4077     MY_SQLITE_FINALIZE(query)
4078
4079     return (E_OK);
4080 }
4081
4082 am_Error_e CAmDatabaseHandler::getDomainState(const am_domainID_t domainID, am_DomainState_e& state) const
4083 {
4084     assert(domainID!=0);
4085     sqlite3_stmt* query = NULL;
4086     state = DS_UNKNOWN;
4087     std::string command = "SELECT domainState FROM " + std::string(DOMAIN_TABLE) + " WHERE domainID=" + i2s(domainID);
4088     int eCode = 0;
4089     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4090     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
4091     {
4092         state = (am_DomainState_e) sqlite3_column_int(query, 0);
4093     }
4094     else if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
4095     {
4096         logError("DatabaseHandler::getDomainState database error!:", eCode);
4097     }
4098     MY_SQLITE_FINALIZE(query)
4099     return (E_OK);
4100
4101 }
4102
4103 am_Error_e CAmDatabaseHandler::peekDomain(const std::string & name, am_domainID_t & domainID)
4104 {
4105     domainID=0;
4106     sqlite3_stmt* query = NULL, *queryInsert = NULL;
4107     std::string command = "SELECT domainID FROM " + std::string(DOMAIN_TABLE) + " WHERE name=?";
4108     int eCode = 0, eCode1 = 0;
4109     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4110     MY_SQLITE_BIND_TEXT(query, 1, name.c_str(), name.size(), SQLITE_STATIC)
4111     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
4112     {
4113         domainID = sqlite3_column_int(query, 0);
4114     }
4115     else if (eCode != SQLITE_DONE)
4116     {
4117         logError("DatabaseHandler::peekDomain database error!:", eCode);
4118         MY_SQLITE_FINALIZE(query)
4119         return (E_DATABASE_ERROR);
4120     }
4121     else
4122     {
4123         command = "INSERT INTO " + std::string(DOMAIN_TABLE) + " (name,reserved) VALUES (?,?)";
4124         MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &queryInsert, NULL)
4125         MY_SQLITE_BIND_TEXT(queryInsert, 1, name.c_str(), name.size(), SQLITE_STATIC)
4126         MY_SQLITE_BIND_INT(queryInsert, 2, 1)
4127         //reservation flag
4128         if ((eCode1 = sqlite3_step(queryInsert)) != SQLITE_DONE)
4129         {
4130             logError("DatabaseHandler::peekDomain SQLITE Step error code:", eCode1);
4131             MY_SQLITE_FINALIZE(queryInsert)
4132             MY_SQLITE_FINALIZE(query)
4133             return (E_DATABASE_ERROR);
4134         }
4135
4136         MY_SQLITE_FINALIZE(queryInsert)
4137
4138         domainID = sqlite3_last_insert_rowid(mpDatabase);
4139     }
4140     MY_SQLITE_FINALIZE(query)
4141
4142     return (E_OK);
4143 }
4144
4145 am_Error_e CAmDatabaseHandler::peekSink(const std::string & name, am_sinkID_t & sinkID)
4146 {
4147     sqlite3_stmt* query = NULL, *queryInsert = NULL;
4148     std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE name=?";
4149     int eCode = 0, eCode1 = 0;
4150     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4151     MY_SQLITE_BIND_TEXT(query, 1, name.c_str(), name.size(), SQLITE_STATIC)
4152     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
4153     {
4154         sinkID = sqlite3_column_int(query, 0);
4155     }
4156     else if (eCode != SQLITE_DONE)
4157     {
4158         logError("DatabaseHandler::peekSink database error!:", eCode);
4159         MY_SQLITE_FINALIZE(query)
4160         return (E_DATABASE_ERROR);
4161     }
4162     else
4163     {
4164         if (mFirstStaticSink)
4165         {
4166             command = "INSERT INTO " + std::string(SINK_TABLE) + " (name,reserved,sinkID) VALUES (?,?," + i2s(DYNAMIC_ID_BOUNDARY) + ")";
4167             mFirstStaticSink = false;
4168         }
4169         else
4170         {
4171             command = "INSERT INTO " + std::string(SINK_TABLE) + " (name,reserved) VALUES (?,?)";
4172         }
4173         MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &queryInsert, NULL)
4174         MY_SQLITE_BIND_TEXT(queryInsert, 1, name.c_str(), name.size(), SQLITE_STATIC)
4175         MY_SQLITE_BIND_INT(queryInsert, 2, 1)
4176         //reservation flag
4177         if ((eCode1 = sqlite3_step(queryInsert)) != SQLITE_DONE)
4178         {
4179             logError("DatabaseHandler::peekSink SQLITE Step error code:", eCode1);
4180             MY_SQLITE_FINALIZE(queryInsert)
4181             MY_SQLITE_FINALIZE(query)
4182             return (E_DATABASE_ERROR);
4183         }
4184
4185         MY_SQLITE_FINALIZE(queryInsert)
4186
4187         sinkID = sqlite3_last_insert_rowid(mpDatabase);
4188     }
4189     MY_SQLITE_FINALIZE(query)
4190     return (E_OK);
4191 }
4192
4193 am_Error_e CAmDatabaseHandler::peekSource(const std::string & name, am_sourceID_t & sourceID)
4194 {
4195     sqlite3_stmt* query = NULL, *queryInsert = NULL;
4196     std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE name=?";
4197     int eCode = 0, eCode1 = 0;
4198     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4199     MY_SQLITE_BIND_TEXT(query, 1, name.c_str(), name.size(), SQLITE_STATIC)
4200     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
4201     {
4202         sourceID = sqlite3_column_int(query, 0);
4203     }
4204     else if (eCode != SQLITE_DONE)
4205     {
4206         logError("DatabaseHandler::peekSink database error!:", eCode);
4207         MY_SQLITE_FINALIZE(query)
4208         return (E_DATABASE_ERROR);
4209     }
4210     else
4211     {
4212         if (mFirstStaticSource)
4213         {
4214             command = "INSERT INTO " + std::string(SOURCE_TABLE) + " (name,reserved,sourceID) VALUES (?,?," + i2s(DYNAMIC_ID_BOUNDARY) + ")";
4215             mFirstStaticSource = false;
4216         }
4217         else
4218         {
4219             command = "INSERT INTO " + std::string(SOURCE_TABLE) + " (name,reserved) VALUES (?,?)";
4220         }
4221         MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &queryInsert, NULL)
4222         MY_SQLITE_BIND_TEXT(queryInsert, 1, name.c_str(), name.size(), SQLITE_STATIC)
4223         MY_SQLITE_BIND_INT(queryInsert, 2, 1)
4224         //reservation flag
4225         if ((eCode1 = sqlite3_step(queryInsert)) != SQLITE_DONE)
4226         {
4227             logError("DatabaseHandler::peekSink SQLITE Step error code:", eCode1);
4228             MY_SQLITE_FINALIZE(queryInsert)
4229             MY_SQLITE_FINALIZE(query)
4230             return (E_DATABASE_ERROR);
4231         }
4232
4233         MY_SQLITE_FINALIZE(queryInsert)
4234         sourceID = sqlite3_last_insert_rowid(mpDatabase);
4235     }
4236
4237     MY_SQLITE_FINALIZE(query)
4238     return (E_OK);
4239 }
4240
4241 am_Error_e CAmDatabaseHandler::changeSinkVolume(const am_sinkID_t sinkID, const am_volume_t volume)
4242 {
4243     assert(sinkID!=0);
4244
4245     sqlite3_stmt* query = NULL;
4246     int eCode = 0;
4247     std::string command;
4248
4249     if (!existSink(sinkID))
4250     {
4251         return (E_NON_EXISTENT);
4252     }
4253     command = "UPDATE " + std::string(SINK_TABLE) + " SET volume=? WHERE sinkID=" + i2s(sinkID);
4254     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4255     MY_SQLITE_BIND_INT(query, 1, volume)
4256     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4257     {
4258         logError("DatabaseHandler::changeSinkVolume SQLITE Step error code:", eCode);
4259         MY_SQLITE_FINALIZE(query)
4260         return (E_DATABASE_ERROR);
4261     }
4262
4263     MY_SQLITE_FINALIZE(query)
4264     logInfo("DatabaseHandler::changeSinkVolume changed volume of sink:", sinkID, "to:", volume);
4265     return (E_OK);
4266 }
4267
4268 am_Error_e CAmDatabaseHandler::changeSourceVolume(const am_sourceID_t sourceID, const am_volume_t volume)
4269 {
4270     assert(sourceID!=0);
4271
4272     sqlite3_stmt* query = NULL;
4273     int eCode = 0;
4274     std::string command;
4275
4276     if (!existSource(sourceID))
4277     {
4278         return (E_NON_EXISTENT);
4279     }
4280     command = "UPDATE " + std::string(SOURCE_TABLE) + " SET volume=? WHERE sourceID=" + i2s(sourceID);
4281     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4282     MY_SQLITE_BIND_INT(query, 1, volume)
4283     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4284     {
4285         logError("DatabaseHandler::changeSourceVolume SQLITE Step error code:", eCode);
4286         MY_SQLITE_FINALIZE(query)
4287         return (E_DATABASE_ERROR);
4288     }
4289
4290     MY_SQLITE_FINALIZE(query)
4291     logInfo("DatabaseHandler::changeSourceVolume changed volume of source=:", sourceID, "to:", volume);
4292
4293     return (E_OK);
4294 }
4295
4296 am_Error_e CAmDatabaseHandler::changeSourceSoundPropertyDB(const am_SoundProperty_s & soundProperty, const am_sourceID_t sourceID)
4297 {
4298     assert(soundProperty.type>=SP_UNKNOWN && soundProperty.type<=SP_MAX);
4299     assert(sourceID!=0);
4300
4301     sqlite3_stmt* query = NULL;
4302     int eCode = 0;
4303     std::string command;
4304
4305     if (!existSource(sourceID))
4306     {
4307         return (E_NON_EXISTENT);
4308     }
4309     command = "UPDATE SourceSoundProperty" + i2s(sourceID) + " SET value=? WHERE soundPropertyType=" + i2s(soundProperty.type);
4310     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4311     MY_SQLITE_BIND_INT(query, 1, soundProperty.value)
4312     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4313     {
4314         logError("DatabaseHandler::changeSourceSoundPropertyDB SQLITE Step error code:", eCode);
4315         MY_SQLITE_FINALIZE(query)
4316         return (E_DATABASE_ERROR);
4317     }
4318
4319     MY_SQLITE_FINALIZE(query)
4320     logInfo("DatabaseHandler::changeSourceSoundPropertyDB changed SourceSoundProperty of source:", sourceID, "type:", soundProperty.type, "to:", soundProperty.value);
4321     return (E_OK);
4322 }
4323
4324 am_Error_e CAmDatabaseHandler::changeSinkSoundPropertyDB(const am_SoundProperty_s & soundProperty, const am_sinkID_t sinkID)
4325 {
4326     assert(soundProperty.type>=SP_UNKNOWN && soundProperty.type<=SP_MAX);
4327     assert(sinkID!=0);
4328
4329     sqlite3_stmt* query = NULL;
4330     int eCode = 0;
4331     std::string command;
4332
4333     if (!existSink(sinkID))
4334     {
4335         return (E_NON_EXISTENT);
4336     }
4337     command = "UPDATE SinkSoundProperty" + i2s(sinkID) + " SET value=? WHERE soundPropertyType=" + i2s(soundProperty.type);
4338     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4339     MY_SQLITE_BIND_INT(query, 1, soundProperty.value)
4340     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4341     {
4342         logError("DatabaseHandler::changeSinkSoundPropertyDB SQLITE Step error code:", eCode);
4343         MY_SQLITE_FINALIZE(query)
4344         return (E_DATABASE_ERROR);
4345     }
4346     assert(sinkID!=0);
4347
4348     MY_SQLITE_FINALIZE(query)
4349     logInfo("DatabaseHandler::changeSinkSoundPropertyDB changed SinkSoundProperty of sink:", sinkID, "type:", soundProperty.type, "to:", soundProperty.value);
4350     return (E_OK);
4351 }
4352
4353 am_Error_e CAmDatabaseHandler::changeCrossFaderHotSink(const am_crossfaderID_t crossfaderID, const am_HotSink_e hotsink)
4354 {
4355     assert(crossfaderID!=0);
4356     assert(hotsink>=HS_UNKNOWN && hotsink>=HS_MAX);
4357
4358     sqlite3_stmt* query = NULL;
4359     int eCode = 0;
4360     std::string command;
4361
4362     if (!existcrossFader(crossfaderID))
4363     {
4364         return (E_NON_EXISTENT);
4365     }
4366     command = "UPDATE " + std::string(CROSSFADER_TABLE) + " SET hotsink=? WHERE crossfaderID=" + i2s(crossfaderID);
4367     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4368     MY_SQLITE_BIND_INT(query, 1, hotsink)
4369     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4370     {
4371         logError("DatabaseHandler::changeCrossFaderHotSink SQLITE Step error code:", eCode);
4372         MY_SQLITE_FINALIZE(query)
4373         return (E_DATABASE_ERROR);
4374     }
4375     MY_SQLITE_FINALIZE(query)
4376     logInfo("DatabaseHandler::changeCrossFaderHotSink changed hotsink of crossfader=", crossfaderID, "to:", hotsink);
4377     return (E_OK);
4378 }
4379
4380 am_Error_e CAmDatabaseHandler::getRoutingTree(bool onlyfree, CAmRoutingTree& tree, std::vector<CAmRoutingTreeItem*>& flatTree)
4381 {
4382     sqlite3_stmt* query = NULL;
4383     int eCode = 0;
4384     size_t i = 0;
4385     std::string command;
4386     am_domainID_t rootID = tree.returnRootDomainID();
4387     CAmRoutingTreeItem *parent = tree.returnRootItem();
4388
4389     if (onlyfree)
4390     {
4391         command = "SELECT g.domainSourceID,g.gatewayID FROM " + std::string(GATEWAY_TABLE) + " g WHERE domainSinkID=? AND NOT EXISTS (SELECT  NULL FROM " + std::string(CONNECTION_TABLE) + " c WHERE c.sinkID = g.sinkID OR c.sourceID = g.sourceID )";
4392     }
4393     else
4394     {
4395         command = "SELECT domainSourceID,gatewayID FROM " + std::string(GATEWAY_TABLE) + " WHERE domainSinkID=?";
4396     }
4397
4398     do
4399     {
4400         if (i != 0)
4401         {
4402             parent = flatTree.at(i - 1);
4403             rootID = parent->returnDomainID();
4404         }
4405         MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4406         MY_SQLITE_BIND_INT(query, 1, rootID)
4407
4408         while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
4409         {
4410             // additional check to avoid cyclic routes
4411             const am_domainID_t domainSourceID = sqlite3_column_int(query, 0);
4412             bool sourceDomainAlreadyHandledAsSink = false;
4413             for (std::vector<CAmRoutingTreeItem*>::const_iterator iFT = flatTree.begin(); iFT != flatTree.end(); ++iFT)
4414             {
4415                 if (domainSourceID == (*iFT)->returnParent()->returnDomainID()) sourceDomainAlreadyHandledAsSink = true;
4416             }
4417
4418             if (!sourceDomainAlreadyHandledAsSink)
4419             {
4420                 // logInfo("DatabaseHandler::getRoutingTree ", rootID, ", ", domainSourceID, ", ", sqlite3_column_int(query, 1));
4421                 flatTree.push_back(tree.insertItem(domainSourceID, sqlite3_column_int(query, 1), parent));
4422             }
4423         }
4424
4425         if (eCode != SQLITE_DONE)
4426         {
4427             logError("DatabaseHandler::getRoutingTree SQLITE error code:", eCode);
4428             MY_SQLITE_FINALIZE(query)
4429             return (E_DATABASE_ERROR);
4430         }
4431
4432         MY_SQLITE_FINALIZE(query)
4433         i++;
4434     } while (flatTree.size() > (i - 1));
4435
4436     return (E_OK);
4437 }
4438
4439 am_Error_e am::CAmDatabaseHandler::peekSinkClassID(const std::string & name, am_sinkClass_t & sinkClassID)
4440 {
4441     if (name.empty())
4442         return (E_NON_EXISTENT);
4443
4444     am_Error_e returnVal = E_NON_EXISTENT;
4445     sqlite3_stmt* query = NULL;
4446     int eCode = 0;
4447     std::string command = "SELECT sinkClassID FROM " + std::string(SINK_CLASS_TABLE) + " WHERE name=?";
4448     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4449     MY_SQLITE_BIND_TEXT(query, 1, name.c_str(), name.size(), SQLITE_STATIC)
4450
4451     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
4452     {
4453         sinkClassID = sqlite3_column_int(query, 0);
4454         returnVal = E_OK;
4455     }
4456     else if (eCode != SQLITE_DONE)
4457     {
4458         sinkClassID = 0;
4459         logError("DatabaseHandler::peekSinkClassID SQLITE error code:", eCode);
4460         MY_SQLITE_FINALIZE(query)
4461         returnVal = E_DATABASE_ERROR;
4462     }
4463
4464     MY_SQLITE_FINALIZE(query)
4465     return (returnVal);
4466 }
4467
4468 am_Error_e am::CAmDatabaseHandler::peekSourceClassID(const std::string & name, am_sourceClass_t & sourceClassID)
4469 {
4470     if (name.empty())
4471         return (E_NON_EXISTENT);
4472
4473     am_Error_e returnVal = E_NON_EXISTENT;
4474     sqlite3_stmt* query = NULL;
4475     int eCode = 0;
4476     std::string command = "SELECT sourceClassID FROM " + std::string(SOURCE_CLASS_TABLE) + " WHERE name=?";
4477     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4478     MY_SQLITE_BIND_TEXT(query, 1, name.c_str(), name.size(), SQLITE_STATIC)
4479
4480     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
4481     {
4482         sourceClassID = sqlite3_column_int(query, 0);
4483         returnVal = E_OK;
4484     }
4485     else if (eCode != SQLITE_DONE)
4486     {
4487         sourceClassID = 0;
4488         logError("DatabaseHandler::peekSourceClassID SQLITE error code:", eCode);
4489         MY_SQLITE_FINALIZE(query)
4490         returnVal = E_DATABASE_ERROR;
4491     }
4492
4493     MY_SQLITE_FINALIZE(query)
4494     return (returnVal);
4495 }
4496
4497
4498 am_Error_e CAmDatabaseHandler::changeSourceDB(const am_sourceID_t sourceID, const am_sourceClass_t sourceClassID, const std::vector<am_SoundProperty_s>& listSoundProperties, const std::vector<am_ConnectionFormat_e>& listConnectionFormats, const std::vector<am_MainSoundProperty_s>& listMainSoundProperties)
4499 {
4500     assert(sourceID!=0);
4501
4502     sqlite3_stmt* query = NULL;
4503     int eCode = 0;
4504     std::string command;
4505     am_sourceClass_t sourceClassOut(sourceClassID);
4506     std::vector<am_MainSoundProperty_s> listMainSoundPropertiesOut(listMainSoundProperties);
4507
4508     if (!existSource(sourceID))
4509     {
4510         return (E_NON_EXISTENT);
4511     }
4512
4513     //check if sinkClass needs to be changed
4514     if (sourceID!=0)
4515     {
4516         command = "UPDATE"+ std::string(SINK_TABLE)+ " SET sourceClassID=? WHERE sourceID=" + i2s(sourceID);
4517         MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4518         MY_SQLITE_BIND_INT(query, 1, sourceClassID)
4519         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4520         {
4521             logError("DatabaseHandler::changeSink SQLITE Step error code:", eCode);
4522             MY_SQLITE_FINALIZE(query)
4523             return (E_DATABASE_ERROR);
4524         }
4525         MY_SQLITE_FINALIZE(query);
4526     }
4527
4528     else //we need to read out the active one
4529     {
4530         command = "SELECT sourceClassID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 and sourceID=" + i2s(sourceID);
4531         MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4532
4533         while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
4534         {
4535             sourceClassOut = sqlite3_column_int(query, 0);
4536         }
4537         MY_SQLITE_FINALIZE(query)
4538     }
4539
4540     //check if soundProperties need to be updated
4541     if (!listSoundProperties.empty())
4542     {
4543         //first we drop the table
4544         command = "DELETE from SourceSoundProperty" + i2s(sourceID);
4545         if (!sqQuery(command))
4546             return (E_DATABASE_ERROR);
4547
4548         //then we'll have a new one
4549         //Fill SinkSoundProperties
4550         command = "INSERT INTO SourceSoundProperty" + i2s(sourceID) + std::string("(soundPropertyType,value) VALUES (?,?)");
4551         MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4552         std::vector<am_SoundProperty_s>::const_iterator SoundPropertyIterator = listSoundProperties.begin();
4553         for (; SoundPropertyIterator < listSoundProperties.end(); ++SoundPropertyIterator)
4554         {
4555             MY_SQLITE_BIND_INT(query, 1, SoundPropertyIterator->type)
4556             MY_SQLITE_BIND_INT(query, 2, SoundPropertyIterator->value)
4557             if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4558             {
4559                 logError("DatabaseHandler::changeSink SQLITE Step error code:", eCode);
4560                 MY_SQLITE_FINALIZE(query)
4561                 return (E_DATABASE_ERROR);
4562             }
4563             MY_SQLITE_RESET(query)
4564         }
4565         MY_SQLITE_FINALIZE(query)
4566     }
4567
4568     //check if we have to update the list of connectionformats
4569     if (!listConnectionFormats.empty())
4570     {
4571         //first clear the table
4572         command = "DELETE from SourceConnectionFormat" + i2s(sourceID);
4573          if (!sqQuery(command))
4574              return (E_DATABASE_ERROR);
4575
4576         //fill ConnectionFormats
4577        command = "INSERT INTO SourceConnectionFormat" + i2s(sourceID) + std::string("(soundFormat) VALUES (?)");
4578        MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4579        std::vector<am_ConnectionFormat_e>::const_iterator connectionFormatIterator = listConnectionFormats.begin();
4580        for (; connectionFormatIterator < listConnectionFormats.end(); ++connectionFormatIterator)
4581        {
4582            MY_SQLITE_BIND_INT(query, 1, *connectionFormatIterator)
4583            if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4584            {
4585                logError("DatabaseHandler::changeSink SQLITE Step error code:", eCode);
4586                MY_SQLITE_FINALIZE(query)
4587                return (E_DATABASE_ERROR);
4588            }
4589            MY_SQLITE_RESET(query)
4590        }
4591        MY_SQLITE_FINALIZE(query)
4592     }
4593
4594     //then we need to check if we need to update the listMainSoundProperties
4595     if (!listMainSoundProperties.empty() && sourceVisible(sourceID))
4596     {
4597         command = "DELETE from SourceMainSoundProperty" + i2s(sourceID);
4598          if (!sqQuery(command))
4599              return (E_DATABASE_ERROR);
4600
4601          //Fill MainSinkSoundProperties
4602          command = "INSERT INTO SourceMainSoundProperty" + i2s(sourceID) + std::string("(soundPropertyType,value) VALUES (?,?)");
4603          MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4604          std::vector<am_MainSoundProperty_s>::const_iterator mainSoundPropertyIterator = listMainSoundProperties.begin();
4605          for (; mainSoundPropertyIterator < listMainSoundProperties.end(); ++mainSoundPropertyIterator)
4606          {
4607              MY_SQLITE_BIND_INT(query, 1, mainSoundPropertyIterator->type)
4608              MY_SQLITE_BIND_INT(query, 2, mainSoundPropertyIterator->value)
4609              if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4610              {
4611                  logError("DatabaseHandler::changeSink SQLITE Step error code:", eCode);
4612                  MY_SQLITE_FINALIZE(query)
4613                  return (E_DATABASE_ERROR);
4614              }
4615              MY_SQLITE_RESET(query)
4616          }
4617          MY_SQLITE_FINALIZE(query)
4618     }
4619     else //read out the properties
4620     {
4621         getListMainSourceSoundProperties(sourceID,listMainSoundPropertiesOut);
4622     }
4623
4624     logInfo("DatabaseHandler::changeSource changed changeSink of source:", sourceID);
4625
4626     if (mpDatabaseObserver != NULL)
4627     {
4628         mpDatabaseObserver->sourceUpdated(sourceID,sourceClassOut,listMainSoundPropertiesOut,sourceVisible(sourceID));
4629     }
4630
4631     return (E_OK);
4632
4633 }
4634
4635 am_Error_e CAmDatabaseHandler::changeSinkDB(const am_sinkID_t sinkID, const am_sinkClass_t sinkClassID, const std::vector<am_SoundProperty_s>& listSoundProperties, const std::vector<am_ConnectionFormat_e>& listConnectionFormats, const std::vector<am_MainSoundProperty_s>& listMainSoundProperties)
4636 {
4637     assert(sinkID!=0);
4638
4639     sqlite3_stmt* query = NULL;
4640     int eCode = 0;
4641     std::string command;
4642     am_sinkClass_t sinkClassOut(sinkClassID);
4643     std::vector<am_MainSoundProperty_s> listMainSoundPropertiesOut(listMainSoundProperties);
4644
4645     if (!existSink(sinkID))
4646     {
4647         return (E_NON_EXISTENT);
4648     }
4649
4650     //check if sinkClass needs to be changed
4651     if (sinkClassID!=0)
4652     {
4653         command = "UPDATE"+ std::string(SINK_TABLE)+ " SET sinkClassID=? WHERE sinkID=" + i2s(sinkID);
4654         MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4655         MY_SQLITE_BIND_INT(query, 1, sinkClassID)
4656         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4657         {
4658             logError("DatabaseHandler::changeSink SQLITE Step error code:", eCode);
4659             MY_SQLITE_FINALIZE(query)
4660             return (E_DATABASE_ERROR);
4661         }
4662         MY_SQLITE_FINALIZE(query);
4663     }
4664
4665     else //we need to read out the active one
4666     {
4667         command = "SELECT sinkClassID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 and sinkID=" + i2s(sinkID);
4668         MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4669
4670         while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
4671         {
4672             sinkClassOut = sqlite3_column_int(query, 0);
4673         }
4674         MY_SQLITE_FINALIZE(query)
4675     }
4676
4677     //check if soundProperties need to be updated
4678     if (!listSoundProperties.empty())
4679     {
4680         //first we drop the table
4681         command = "DELETE from SinkSoundProperty" + i2s(sinkID);
4682         if (!sqQuery(command))
4683             return (E_DATABASE_ERROR);
4684
4685         //then we'll have a new one
4686         //Fill SinkSoundProperties
4687         command = "INSERT INTO SinkSoundProperty" + i2s(sinkID) + std::string("(soundPropertyType,value) VALUES (?,?)");
4688         MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4689         std::vector<am_SoundProperty_s>::const_iterator SoundPropertyIterator = listSoundProperties.begin();
4690         for (; SoundPropertyIterator < listSoundProperties.end(); ++SoundPropertyIterator)
4691         {
4692             MY_SQLITE_BIND_INT(query, 1, SoundPropertyIterator->type)
4693             MY_SQLITE_BIND_INT(query, 2, SoundPropertyIterator->value)
4694             if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4695             {
4696                 logError("DatabaseHandler::changeSink SQLITE Step error code:", eCode);
4697                 MY_SQLITE_FINALIZE(query)
4698                 return (E_DATABASE_ERROR);
4699             }
4700             MY_SQLITE_RESET(query)
4701         }
4702         MY_SQLITE_FINALIZE(query)
4703     }
4704
4705     //check if we have to update the list of connectionformats
4706     if (!listConnectionFormats.empty())
4707     {
4708         //first clear the table
4709         command = "DELETE from SinkConnectionFormat" + i2s(sinkID);
4710          if (!sqQuery(command))
4711              return (E_DATABASE_ERROR);
4712
4713         //fill ConnectionFormats
4714        command = "INSERT INTO SinkConnectionFormat" + i2s(sinkID) + std::string("(soundFormat) VALUES (?)");
4715        MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4716        std::vector<am_ConnectionFormat_e>::const_iterator connectionFormatIterator = listConnectionFormats.begin();
4717        for (; connectionFormatIterator < listConnectionFormats.end(); ++connectionFormatIterator)
4718        {
4719            MY_SQLITE_BIND_INT(query, 1, *connectionFormatIterator)
4720            if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4721            {
4722                logError("DatabaseHandler::changeSink SQLITE Step error code:", eCode);
4723                MY_SQLITE_FINALIZE(query)
4724                return (E_DATABASE_ERROR);
4725            }
4726            MY_SQLITE_RESET(query)
4727        }
4728        MY_SQLITE_FINALIZE(query)
4729     }
4730
4731     //then we need to check if we need to update the listMainSoundProperties
4732     if (!listMainSoundProperties.empty() && sinkVisible(sinkID))
4733     {
4734         command = "DELETE from SinkMainSoundProperty" + i2s(sinkID);
4735          if (!sqQuery(command))
4736              return (E_DATABASE_ERROR);
4737
4738          //Fill MainSinkSoundProperties
4739          command = "INSERT INTO SinkMainSoundProperty" + i2s(sinkID) + std::string("(soundPropertyType,value) VALUES (?,?)");
4740          MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4741          std::vector<am_MainSoundProperty_s>::const_iterator mainSoundPropertyIterator = listMainSoundProperties.begin();
4742          for (; mainSoundPropertyIterator < listMainSoundProperties.end(); ++mainSoundPropertyIterator)
4743          {
4744              MY_SQLITE_BIND_INT(query, 1, mainSoundPropertyIterator->type)
4745              MY_SQLITE_BIND_INT(query, 2, mainSoundPropertyIterator->value)
4746              if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4747              {
4748                  logError("DatabaseHandler::changeSink SQLITE Step error code:", eCode);
4749                  MY_SQLITE_FINALIZE(query)
4750                  return (E_DATABASE_ERROR);
4751              }
4752              MY_SQLITE_RESET(query)
4753          }
4754          MY_SQLITE_FINALIZE(query)
4755     }
4756     else //read out the properties
4757     {
4758         getListMainSinkSoundProperties(sinkID,listMainSoundPropertiesOut);
4759     }
4760
4761     logInfo("DatabaseHandler::changeSink changed changeSink of sink:", sinkID);
4762
4763     if (mpDatabaseObserver != NULL)
4764     {
4765         mpDatabaseObserver->sinkUpdated(sinkID,sinkClassOut,listMainSoundPropertiesOut,sinkVisible(sinkID));
4766     }
4767
4768     return (E_OK);
4769 }
4770
4771 am_Error_e CAmDatabaseHandler::getListMainSinkNotificationConfigurations(const am_sinkID_t sinkID, std::vector<am_NotificationConfiguration_s>& listMainNotificationConfigurations)
4772 {
4773     assert(sinkID!=0);
4774     if (!existSink(sinkID))
4775         return (E_DATABASE_ERROR); // todo: here we could change to non existen, but not shown in sequences
4776     listMainNotificationConfigurations.clear();
4777
4778     sqlite3_stmt* query = NULL;
4779     int eCode = 0;
4780     am_NotificationConfiguration_s temp;
4781     std::string command = "SELECT type, status, parameter FROM SinkMainNotificationConfiguration" + i2s(sinkID);
4782     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4783
4784     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
4785     {
4786         temp.type =  static_cast<am_NotificationType_e>(sqlite3_column_int(query, 0));
4787         temp.status = static_cast<am_NotificationStatus_e>(sqlite3_column_int(query, 1));
4788         temp.parameter= static_cast<int16_t>(sqlite3_column_int(query, 2));
4789         listMainNotificationConfigurations.push_back(temp);
4790     }
4791
4792     if (eCode != SQLITE_DONE)
4793     {
4794         logError("DatabaseHandler::getListSinkMainNotificationConfigurations SQLITE error code:", eCode);
4795         MY_SQLITE_FINALIZE(query)
4796         return (E_DATABASE_ERROR);
4797     }
4798
4799     MY_SQLITE_FINALIZE(query)
4800
4801     return (E_OK);
4802
4803 }
4804
4805 am_Error_e CAmDatabaseHandler::getListMainSourceNotificationConfigurations(const am_sourceID_t sourceID, std::vector<am_NotificationConfiguration_s>& listMainNotificationConfigurations)
4806 {
4807     assert(sourceID!=0);
4808     if (!existSource(sourceID))
4809         return (E_DATABASE_ERROR); // todo: here we could change to non existen, but not shown in sequences
4810     listMainNotificationConfigurations.clear();
4811
4812     sqlite3_stmt* query = NULL;
4813     int eCode = 0;
4814     am_NotificationConfiguration_s temp;
4815     std::string command = "SELECT type, status, parameter FROM SourceMainNotificationConfiguration" + i2s(sourceID);
4816     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4817
4818     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
4819     {
4820         temp.type =  static_cast<am_NotificationType_e>(sqlite3_column_int(query, 0));
4821         temp.status = static_cast<am_NotificationStatus_e>(sqlite3_column_int(query, 1));
4822         temp.parameter= static_cast<int16_t>(sqlite3_column_int(query, 2));
4823         listMainNotificationConfigurations.push_back(temp);
4824     }
4825
4826     if (eCode != SQLITE_DONE)
4827     {
4828         logError("DatabaseHandler::getListSourceMainNotificationConfigurations SQLITE error code:", eCode);
4829         MY_SQLITE_FINALIZE(query)
4830         return (E_DATABASE_ERROR);
4831     }
4832
4833     MY_SQLITE_FINALIZE(query)
4834
4835     return (E_OK);
4836 }
4837
4838 am_Error_e CAmDatabaseHandler::changeMainSinkNotificationConfigurationDB(const am_sinkID_t sinkID, const am_NotificationConfiguration_s mainNotificationConfiguration)
4839 {
4840     assert(sinkID!=0);
4841
4842     sqlite3_stmt* query = NULL;
4843     int eCode = 0;
4844     std::string command;
4845
4846     if (!existSink(sinkID))
4847     {
4848         return (E_NON_EXISTENT);
4849     }
4850     command = "UPDATE SinkMainNotificationConfiguration" + i2s(sinkID) + " SET status=?, parameter=? WHERE type=" + i2s(mainNotificationConfiguration.type);
4851     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4852     MY_SQLITE_BIND_INT(query, 1, mainNotificationConfiguration.status)
4853     MY_SQLITE_BIND_INT(query, 2, mainNotificationConfiguration.parameter)
4854     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4855     {
4856         logError("DatabaseHandler::changeMainSinkNotificationConfigurationDB SQLITE Step error code:", eCode);
4857         MY_SQLITE_FINALIZE(query)
4858         return (E_DATABASE_ERROR);
4859     }
4860     MY_SQLITE_FINALIZE(query)
4861
4862     logInfo("DatabaseHandler::changeMainSinkNotificationConfigurationDB changed MainNotificationConfiguration of source:", sinkID, "type:", mainNotificationConfiguration.type, "to status=", mainNotificationConfiguration.status, "and parameter=",mainNotificationConfiguration.parameter);
4863
4864     if (mpDatabaseObserver)
4865         mpDatabaseObserver->sinkMainNotificationConfigurationChanged(sinkID, mainNotificationConfiguration);
4866     return (E_OK);
4867 }
4868
4869 am_Error_e CAmDatabaseHandler::changeMainSourceNotificationConfigurationDB(const am_sourceID_t sourceID, const am_NotificationConfiguration_s mainNotificationConfiguration)
4870 {
4871     assert(sourceID!=0);
4872
4873     sqlite3_stmt* query = NULL;
4874     int eCode = 0;
4875     std::string command;
4876
4877     if (!existSource(sourceID))
4878     {
4879         return (E_NON_EXISTENT);
4880     }
4881     command = "UPDATE SourceMainNotificationConfiguration" + i2s(sourceID) + " SET status=?, parameter=? WHERE type=" + i2s(mainNotificationConfiguration.type);
4882     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4883     MY_SQLITE_BIND_INT(query, 1, mainNotificationConfiguration.status)
4884     MY_SQLITE_BIND_INT(query, 2, mainNotificationConfiguration.parameter)
4885     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4886     {
4887         logError("DatabaseHandler::changeMainSourceNotificationConfigurationDB SQLITE Step error code:", eCode);
4888         MY_SQLITE_FINALIZE(query)
4889         return (E_DATABASE_ERROR);
4890     }
4891     MY_SQLITE_FINALIZE(query)
4892
4893     logInfo("DatabaseHandler::changeMainSourceNotificationConfigurationDB changed MainNotificationConfiguration of source:", sourceID, "type:", mainNotificationConfiguration.type, "to status=", mainNotificationConfiguration.status, "and parameter=",mainNotificationConfiguration.parameter);
4894
4895     if (mpDatabaseObserver)
4896         mpDatabaseObserver->sourceMainNotificationConfigurationChanged(sourceID, mainNotificationConfiguration);
4897     return (E_OK);
4898 }
4899
4900 am_Error_e CAmDatabaseHandler::changeGatewayDB(const am_gatewayID_t gatewayID, const std::vector<am_ConnectionFormat_e>& listSourceConnectionFormats, const std::vector<am_ConnectionFormat_e>& listSinkConnectionFormats, const std::vector<bool>& convertionMatrix)
4901 {
4902     assert(gatewayID!=0);
4903
4904    sqlite3_stmt* query = NULL;
4905    int eCode = 0;
4906    std::string command;
4907
4908    if (!existGateway(gatewayID))
4909    {
4910        return (E_NON_EXISTENT);
4911    }
4912
4913    if (!listSourceConnectionFormats.empty())
4914    {
4915        //clear Database
4916        command = "DELETE from GatewaySourceFormat" + i2s(gatewayID);
4917        if (!sqQuery(command))
4918            return (E_DATABASE_ERROR);
4919
4920        //fill ConnectionFormats
4921        command = "INSERT INTO GatewaySourceFormat" + i2s(gatewayID) + std::string("(soundFormat) VALUES (?)");
4922        MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4923        std::vector<am_ConnectionFormat_e>::const_iterator connectionFormatIterator = listSourceConnectionFormats.begin();
4924        for (; connectionFormatIterator < listSourceConnectionFormats.end(); ++connectionFormatIterator)
4925        {
4926            MY_SQLITE_BIND_INT(query, 1, *connectionFormatIterator)
4927            if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4928            {
4929                logError("DatabaseHandler::enterGatewayDB SQLITE Step error code:", eCode);
4930                MY_SQLITE_FINALIZE(query)
4931                return (E_DATABASE_ERROR);
4932            }
4933            MY_SQLITE_RESET(query)
4934        }
4935        MY_SQLITE_FINALIZE(query)
4936    }
4937
4938    if (!listSinkConnectionFormats.empty())
4939    {
4940        //clear Database
4941        command = "DELETE from GatewaySinkFormat" + i2s(gatewayID);
4942        if (!sqQuery(command))
4943            return (E_DATABASE_ERROR);
4944
4945        command = "INSERT INTO GatewaySinkFormat" + i2s(gatewayID) + std::string("(soundFormat) VALUES (?)");
4946        MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4947        std::vector<am_ConnectionFormat_e>::const_iterator connectionFormatIterator = listSinkConnectionFormats.begin();
4948        for (; connectionFormatIterator < listSinkConnectionFormats.end(); ++connectionFormatIterator)
4949        {
4950          MY_SQLITE_BIND_INT(query, 1, *connectionFormatIterator)
4951          if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4952          {
4953              logError("DatabaseHandler::enterGatewayDB SQLITE Step error code:", eCode);
4954              MY_SQLITE_FINALIZE(query)
4955              return (E_DATABASE_ERROR);
4956          }
4957          MY_SQLITE_RESET(query)
4958        }
4959        MY_SQLITE_FINALIZE(query)
4960    }
4961
4962    if (!convertionMatrix.empty())
4963    {
4964        mListConnectionFormat.clear();
4965        mListConnectionFormat.insert(std::make_pair(gatewayID, convertionMatrix));
4966    }
4967
4968    logInfo("DatabaseHandler::changeGatewayDB changed Gateway with ID", gatewayID);
4969
4970    //todo: check if observer needs to be adopted.
4971    return (E_OK);
4972 }
4973
4974 am_Error_e CAmDatabaseHandler::changeSinkNotificationConfigurationDB(const am_sinkID_t sinkID, const am_NotificationConfiguration_s notificationConfiguration)
4975 {
4976     assert(sinkID!=0);
4977
4978     sqlite3_stmt* query = NULL;
4979     int eCode = 0;
4980     std::string command;
4981
4982     if (!existSink(sinkID))
4983     {
4984         return (E_NON_EXISTENT);
4985     }
4986     command = "UPDATE SinkNotificationConfiguration" + i2s(sinkID) + " SET status=?, parameter=? WHERE type=" + i2s(notificationConfiguration.type);
4987     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
4988     MY_SQLITE_BIND_INT(query, 1, notificationConfiguration.status)
4989     MY_SQLITE_BIND_INT(query, 2, notificationConfiguration.parameter)
4990     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
4991     {
4992         logError("DatabaseHandler::changeMainSinkNotificationConfigurationDB SQLITE Step error code:", eCode);
4993         MY_SQLITE_FINALIZE(query)
4994         return (E_DATABASE_ERROR);
4995     }
4996     MY_SQLITE_FINALIZE(query)
4997
4998     logInfo("DatabaseHandler::changeMainSinkNotificationConfigurationDB changed MainNotificationConfiguration of source:", sinkID, "type:", notificationConfiguration.type, "to status=", notificationConfiguration.status, "and parameter=",notificationConfiguration.parameter);
4999
5000     //todo:: inform obsever here...
5001     return (E_OK);
5002 }
5003
5004 am_Error_e CAmDatabaseHandler::changeSourceNotificationConfigurationDB(const am_sourceID_t sourceID, const am_NotificationConfiguration_s notificationConfiguration)
5005 {
5006     assert(sourceID!=0);
5007
5008     sqlite3_stmt* query = NULL;
5009     int eCode = 0;
5010     std::string command;
5011
5012     if (!existSource(sourceID))
5013     {
5014         return (E_NON_EXISTENT);
5015     }
5016     command = "UPDATE SourceNotificationConfiguration" + i2s(sourceID) + " SET status=?, parameter=? WHERE type=" + i2s(notificationConfiguration.type);
5017     MY_SQLITE_PREPARE_V2(mpDatabase, command.c_str(), -1, &query, NULL)
5018     MY_SQLITE_BIND_INT(query, 1, notificationConfiguration.status)
5019     MY_SQLITE_BIND_INT(query, 2, notificationConfiguration.parameter)
5020     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
5021     {
5022         logError("DatabaseHandler::changeMainSourceNotificationConfigurationDB SQLITE Step error code:", eCode);
5023         MY_SQLITE_FINALIZE(query)
5024         return (E_DATABASE_ERROR);
5025     }
5026     MY_SQLITE_FINALIZE(query)
5027
5028     logInfo("DatabaseHandler::changeSourceNotificationConfigurationDB changed MainNotificationConfiguration of source:", sourceID, "type:", notificationConfiguration.type, "to status=", notificationConfiguration.status, "and parameter=",notificationConfiguration.parameter);
5029
5030     //todo:: implement observer function
5031     return (E_OK);
5032 }
5033
5034 void CAmDatabaseHandler::createTables()
5035 {
5036     for (uint16_t i = 0; i < sizeof(databaseTables) / sizeof(databaseTables[0]); i++)
5037     {
5038         if (!sqQuery("CREATE TABLE " + databaseTables[i]))
5039             throw std::runtime_error("CAmDatabaseHandler Could not create tables!");
5040     }
5041 }
5042 }