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