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