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