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