2 * Copyright (C) 2011, BMW AG
4 * GeniviAudioMananger AudioManagerDaemon
6 * \file Databasehandler.cpp
8 * \date 20-Oct-2011 3:42:04 PM
9 * \author Christian Mueller (christian.ei.mueller@bmw.de)
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
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.
25 #include "DatabaseHandler.h"
26 #include "DatabaseObserver.h"
32 #include "DLTWrapper.h"
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"
50 const std::string databaseTables[] =
52 " Domains (domainID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50), busname VARCHAR(50), nodename VARCHAR(50), early BOOL, complete BOOL, state INTEGER, reserved BOOL);", " SourceClasses (sourceClassID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50));", " SinkClasses (sinkClassID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50));",
53 " Sources (sourceID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, domainID INTEGER, name VARCHAR(50), sourceClassID INTEGER, sourceState INTEGER, volume INTEGER, visible BOOL, availability INTEGER, availabilityReason INTEGER, interruptState INTEGER, reserved BOOL);", //
54 " Sinks (sinkID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50), domainID INTEGER, sinkClassID INTEGER, volume INTEGER, visible BOOL, availability INTEGER, availabilityReason INTEGER, muteState INTEGER, mainVolume INTEGER, reserved BOOL);", " Gateways (gatewayID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50), sinkID INTEGER, sourceID INTEGER, domainSinkID INTEGER, domainSourceID INTEGER, controlDomainID INTEGER, inUse BOOL);", " Crossfaders (crossfaderID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50), sinkID_A INTEGER, sinkID_B INTEGER, sourceID INTEGER, hotSink INTEGER);",
55 " Connections (connectionID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, sourceID INTEGER, sinkID INTEGER, delay INTEGER, connectionFormat INTEGER, reserved BOOL);", //
56 " MainConnections (mainConnectionID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, sourceID INTEGER, sinkID INTEGER, connectionState INTEGER, delay INTEGER);", " SystemProperties (type INTEGER PRIMARY KEY, value INTEGER);" };
59 * template to converts T to std::string
60 * @param i the value to be converted
64 inline std::string i2s(T const& x)
71 DatabaseHandler::DatabaseHandler(std::string databasePath) :
73 mPath(databasePath), //
74 mDatabaseObserver(NULL), //
75 mFirstStaticSink(true), //
76 mFirstStaticSource(true), //
77 mFirstStaticGateway(true), //
78 mFirstStaticSinkClass(true), //
79 mFirstStaticSourceClass(true), //
80 mListConnectionFormat()
84 *\todo: this erases the database. just for testing!
86 std::ifstream infile(mPath.c_str());
90 remove(mPath.c_str());
91 logInfo("DatabaseHandler::DatabaseHandler Knocked down database");
94 bool dbOpen = openDatabase();
97 logInfo("DatabaseHandler::DatabaseHandler problems opening the database!");
103 DatabaseHandler::~DatabaseHandler()
105 logInfo("Closed Database");
106 sqlite3_close(mDatabase);
109 am_Error_e DatabaseHandler::enterDomainDB(const am_Domain_s & domainData, am_domainID_t & domainID)
111 assert(domainData.domainID==0);
112 assert(!domainData.name.empty());
113 assert(!domainData.busname.empty());
114 assert(domainData.state>=DS_CONTROLLED && domainData.state<=DS_INDEPENDENT_RUNDOWN);
116 //first check for a reserved domain
117 sqlite3_stmt* query = NULL, *queryFinal;
119 std::string command = "SELECT domainID FROM " + std::string(DOMAIN_TABLE) + " WHERE name=?";
120 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
121 sqlite3_bind_text(query, 1, domainData.name.c_str(), domainData.name.size(), SQLITE_STATIC);
122 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
124 command = "UPDATE " + std::string(DOMAIN_TABLE) + " SET name=?, busname=?, nodename=?, early=?, complete=?, state=?, reserved=? WHERE domainID=" + i2s(sqlite3_column_int(query, 0));
126 else if (eCode == SQLITE_DONE)
129 command = "INSERT INTO " + std::string(DOMAIN_TABLE) + " (name, busname, nodename, early, complete, state, reserved) VALUES (?,?,?,?,?,?,?)";
133 logError("DatabaseHandler::enterDomainDB SQLITE Step error code:", eCode);
134 return E_DATABASE_ERROR;
137 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
139 logError("DatabaseHandler::enterDomainDB SQLITE Finalize error code:", eCode);
140 return E_DATABASE_ERROR;
143 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &queryFinal, NULL);
144 sqlite3_bind_text(queryFinal, 1, domainData.name.c_str(), domainData.name.size(), SQLITE_STATIC);
145 sqlite3_bind_text(queryFinal, 2, domainData.busname.c_str(), domainData.busname.size(), SQLITE_STATIC);
146 sqlite3_bind_text(queryFinal, 3, domainData.nodename.c_str(), domainData.nodename.size(), SQLITE_STATIC);
147 sqlite3_bind_int(queryFinal, 4, domainData.early);
148 sqlite3_bind_int(queryFinal, 5, domainData.complete);
149 sqlite3_bind_int(queryFinal, 6, domainData.state);
150 sqlite3_bind_int(queryFinal, 7, 0);
152 if ((eCode = sqlite3_step(queryFinal)) != SQLITE_DONE)
154 logError("DatabaseHandler::enterDomainDB SQLITE Step error code:", eCode);
155 return E_DATABASE_ERROR;
158 if ((eCode = sqlite3_finalize(queryFinal)) != SQLITE_OK)
160 logError("DatabaseHandler::enterDomainDB SQLITE Finalize error code:", eCode);
161 return E_DATABASE_ERROR;
164 domainID = sqlite3_last_insert_rowid(mDatabase);
165 logInfo("DatabaseHandler::enterDomainDB entered new domain with name=", domainData.name, "busname=", domainData.busname, "nodename=", domainData.nodename, "assigned ID:", domainID);
167 am_Domain_s domain = domainData;
168 domain.domainID = domainID;
169 if (mDatabaseObserver)
170 mDatabaseObserver->newDomain(domain);
175 am_Error_e DatabaseHandler::enterMainConnectionDB(const am_MainConnection_s & mainConnectionData, am_mainConnectionID_t & connectionID)
177 assert(mainConnectionData.connectionID==0);
178 assert(mainConnectionData.connectionState>=CS_CONNECTING && mainConnectionData.connectionState<=CS_SUSPENDED);
179 assert(mainConnectionData.route.sinkID!=0);
180 assert(mainConnectionData.route.sourceID!=0);
182 sqlite3_stmt* query = NULL;
184 std::string command = "INSERT INTO " + std::string(MAINCONNECTION_TABLE) + "(sourceID, sinkID, connectionState, delay) VALUES (?,?,?,-1)";
185 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
186 sqlite3_bind_int(query, 1, mainConnectionData.route.sourceID);
187 sqlite3_bind_int(query, 2, mainConnectionData.route.sinkID);
188 sqlite3_bind_int(query, 3, mainConnectionData.connectionState);
190 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
192 logError("DatabaseHandler::enterMainConnectionDB SQLITE Step error code:", eCode);
193 return E_DATABASE_ERROR;
196 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
198 logError("DatabaseHandler::enterMainConnectionDB SQLITE Finalize error code:", eCode);
199 return E_DATABASE_ERROR;
202 connectionID = sqlite3_last_insert_rowid(mDatabase);
204 //now check the connectionTable for all connections in the route. IF a particular route is not found, we return with error
205 std::vector<uint16_t> listOfConnections;
207 command = "SELECT connectionID, delay FROM " + std::string(CONNECTION_TABLE) + (" WHERE sourceID=? AND sinkID=? AND connectionFormat=?");
208 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
209 std::vector<am_RoutingElement_s>::const_iterator elementIterator = mainConnectionData.route.route.begin();
210 for (; elementIterator < mainConnectionData.route.route.end(); ++elementIterator)
212 sqlite3_bind_int(query, 1, elementIterator->sourceID);
213 sqlite3_bind_int(query, 2, elementIterator->sinkID);
214 sqlite3_bind_int(query, 3, elementIterator->connectionFormat);
216 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
218 listOfConnections.push_back(sqlite3_column_int(query, 0));
219 int16_t temp_delay = sqlite3_column_int(query, 1);
220 if (temp_delay != -1 && delay != -1)
227 logError("DatabaseHandler::enterMainConnectionDB did not find route for MainConnection:", eCode);
229 return E_DATABASE_ERROR;
231 sqlite3_reset(query);
234 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
236 logError("DatabaseHandler::enterMainConnectionDB SQLITE Finalize error code:", eCode);
237 return E_DATABASE_ERROR;
240 //now we create a table with references to the connections;
241 command = "CREATE TABLE MainConnectionRoute" + i2s(connectionID) + std::string("(connectionID INTEGER)");
242 assert(this->sqQuery(command));
244 command = "INSERT INTO MainConnectionRoute" + i2s(connectionID) + "(connectionID) VALUES (?)";
245 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
246 std::vector<uint16_t>::iterator listConnectionIterator = listOfConnections.begin();
247 for (; listConnectionIterator < listOfConnections.end(); ++listConnectionIterator)
249 sqlite3_bind_int(query, 1, *listConnectionIterator);
250 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
252 logError("DatabaseHandler::enterMainConnectionDB SQLITE Step error code:", eCode);
253 return E_DATABASE_ERROR;
255 sqlite3_reset(query);
258 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
260 logError("DatabaseHandler::enterMainConnectionDB SQLITE Finalize error code:", eCode);
261 return E_DATABASE_ERROR;
264 logInfo("DatabaseHandler::enterMainConnectionDB entered new mainConnection with sourceID", mainConnectionData.route.sourceID, "sinkID:", mainConnectionData.route.sinkID, "delay:", delay, "assigned ID:", connectionID);
266 if (mDatabaseObserver)
268 mDatabaseObserver->numberOfMainConnectionsChanged();
269 mDatabaseObserver->mainConnectionStateChanged(connectionID, mainConnectionData.connectionState);
272 //finally, we update the delay value for the maintable
275 return changeDelayMainConnection(delay, connectionID);
278 am_Error_e DatabaseHandler::enterSinkDB(const am_Sink_s & sinkData, am_sinkID_t & sinkID)
280 assert(sinkData.sinkID<DYNAMIC_ID_BOUNDARY);
281 assert(sinkData.domainID!=0);
282 assert(!sinkData.name.empty());
283 assert(sinkData.sinkClassID!=0);
284 // \todo: need to check if class exists?
285 assert(!sinkData.listConnectionFormats.empty());
286 assert(sinkData.muteState>=MS_MUTED && sinkData.muteState<=MS_UNMUTED);
288 sqlite3_stmt *query = NULL, *queryFinal = NULL;
290 std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE name=? AND reserved=1";
292 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
293 sqlite3_bind_text(query, 1, sinkData.name.c_str(), sinkData.name.size(), SQLITE_STATIC);
295 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
297 command = "UPDATE " + std::string(SINK_TABLE) + " SET name=?, domainID=?, sinkClassID=?, volume=?, visible=?, availability=?, availabilityReason=?, muteState=?, mainVolume=?, reserved=? WHERE sinkID=" + i2s(sqlite3_column_int(query, 0));
299 else if (eCode == SQLITE_DONE)
301 //if sinkID is zero and the first Static Sink was already entered, the ID is created
302 if (sinkData.sinkID == 0 && !mFirstStaticSink && !existSinkName(sinkData.name))
304 command = "INSERT INTO " + std::string(SINK_TABLE) + "(name, domainID, sinkClassID, volume, visible, availability, availabilityReason, muteState, mainVolume, reserved) VALUES (?,?,?,?,?,?,?,?,?,?)";
308 //check if the ID already exists
309 if (existSinkNameOrID(sinkData.sinkID, sinkData.name))
311 sqlite3_finalize(query);
312 return E_ALREADY_EXISTS;
314 command = "INSERT INTO " + std::string(SINK_TABLE) + "(name, domainID, sinkClassID, volume, visible, availability, availabilityReason, muteState, mainVolume, reserved, sinkID) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
319 logError("DatabaseHandler::enterSinkDB SQLITE Step error code:", eCode);
320 sqlite3_finalize(query);
321 return E_DATABASE_ERROR;
324 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
326 logError("DatabaseHandler::enterSinkDB SQLITE Finalize error code:", eCode);
327 return E_DATABASE_ERROR;
330 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &queryFinal, NULL);
331 sqlite3_bind_text(queryFinal, 1, sinkData.name.c_str(), sinkData.name.size(), SQLITE_STATIC);
332 sqlite3_bind_int(queryFinal, 2, sinkData.domainID);
333 sqlite3_bind_int(queryFinal, 3, sinkData.sinkClassID);
334 sqlite3_bind_int(queryFinal, 4, sinkData.volume);
335 sqlite3_bind_int(queryFinal, 5, sinkData.visible);
336 sqlite3_bind_int(queryFinal, 6, sinkData.available.availability);
337 sqlite3_bind_int(queryFinal, 7, sinkData.available.availabilityReason);
338 sqlite3_bind_int(queryFinal, 8, sinkData.muteState);
339 sqlite3_bind_int(queryFinal, 9, sinkData.mainVolume);
340 sqlite3_bind_int(queryFinal, 10, 0);
342 //if the ID is not created, we add it to the query
343 if (sinkData.sinkID != 0)
345 sqlite3_bind_int(queryFinal, 11, sinkData.sinkID);
348 //if the first static sink is entered, we need to set it onto the boundary
349 else if (mFirstStaticSink)
351 sqlite3_bind_int(queryFinal, 11, DYNAMIC_ID_BOUNDARY);
352 mFirstStaticSink = false;
355 if ((eCode = sqlite3_step(queryFinal)) != SQLITE_DONE)
357 logError("DatabaseHandler::enterSinkDB SQLITE Step error code:", eCode);
358 sqlite3_finalize(queryFinal);
359 return E_DATABASE_ERROR;
362 if ((eCode = sqlite3_finalize(queryFinal)) != SQLITE_OK)
364 logError("DatabaseHandler::enterSinkDB SQLITE Finalize error code:", eCode);
365 return E_DATABASE_ERROR;
368 //now read back the sinkID
369 command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE name=?";
370 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
371 sqlite3_bind_text(query, 1, sinkData.name.c_str(), sinkData.name.size(), SQLITE_STATIC);
372 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
374 sinkID = sqlite3_column_int(query, 0);
379 logError("DatabaseHandler::existSink database error!:", eCode);
380 sqlite3_finalize(query);
381 return E_DATABASE_ERROR;
383 sqlite3_finalize(query);
385 //now we need to create the additional tables:
386 command = "CREATE TABLE SinkConnectionFormat" + i2s(sinkID) + std::string("(soundFormat INTEGER)");
387 assert(this->sqQuery(command));
388 command = "CREATE TABLE SinkMainSoundProperty" + i2s(sinkID) + std::string("(soundPropertyType INTEGER, value INTEGER)");
389 assert(this->sqQuery(command));
390 command = "CREATE TABLE SinkSoundProperty" + i2s(sinkID) + std::string("(soundPropertyType INTEGER, value INTEGER)");
391 assert(this->sqQuery(command));
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)
399 sqlite3_bind_int(query, 1, *connectionFormatIterator);
400 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
402 logError("DatabaseHandler::enterSinkDB SQLITE Step error code:", eCode);
403 sqlite3_finalize(query);
404 return E_DATABASE_ERROR;
406 sqlite3_reset(query);
409 //Fill MainSinkSoundProperties
410 command = "INSERT INTO SinkMainSoundProperty" + i2s(sinkID) + std::string("(soundPropertyType,value) VALUES (?,?)");
411 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
412 std::vector<am_MainSoundProperty_s>::const_iterator mainSoundPropertyIterator = sinkData.listMainSoundProperties.begin();
413 for (; mainSoundPropertyIterator < sinkData.listMainSoundProperties.end(); ++mainSoundPropertyIterator)
415 sqlite3_bind_int(query, 1, mainSoundPropertyIterator->type);
416 sqlite3_bind_int(query, 2, mainSoundPropertyIterator->value);
417 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
419 logError("DatabaseHandler::enterSinkDB SQLITE Step error code:", eCode);
420 sqlite3_finalize(query);
421 return E_DATABASE_ERROR;
423 sqlite3_reset(query);
426 //Fill SinkSoundProperties
427 command = "INSERT INTO SinkSoundProperty" + i2s(sinkID) + std::string("(soundPropertyType,value) VALUES (?,?)");
428 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
429 std::vector<am_SoundProperty_s>::const_iterator SoundPropertyIterator = sinkData.listSoundProperties.begin();
430 for (; SoundPropertyIterator < sinkData.listSoundProperties.end(); ++SoundPropertyIterator)
432 sqlite3_bind_int(query, 1, SoundPropertyIterator->type);
433 sqlite3_bind_int(query, 2, SoundPropertyIterator->value);
434 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
436 logError("DatabaseHandler::enterSinkDB SQLITE Step error code:", eCode);
437 sqlite3_finalize(query);
438 return E_DATABASE_ERROR;
440 sqlite3_reset(query);
443 logInfo("DatabaseHandler::enterSinkDB entered new sink with name", sinkData.name, "domainID:", sinkData.domainID, "classID:", sinkData.sinkClassID, "volume:", sinkData.volume, "assigned ID:", sinkID);
444 am_Sink_s sink = sinkData;
445 sink.sinkID = sinkID;
446 if (mDatabaseObserver != NULL)
447 mDatabaseObserver->newSink(sink);
451 am_Error_e DatabaseHandler::enterCrossfaderDB(const am_Crossfader_s & crossfaderData, am_crossfaderID_t & crossfaderID)
453 //todo: implement crossfader
454 (void) crossfaderData;
459 am_Error_e DatabaseHandler::enterGatewayDB(const am_Gateway_s & gatewayData, am_gatewayID_t & gatewayID)
461 assert(gatewayData.gatewayID<DYNAMIC_ID_BOUNDARY);
462 assert(gatewayData.sinkID!=0);
463 assert(gatewayData.sourceID!=0);
464 assert(gatewayData.controlDomainID!=0);
465 assert(gatewayData.domainSinkID!=0);
466 assert(gatewayData.domainSourceID!=0);
467 assert(!gatewayData.name.empty());
468 assert(!gatewayData.convertionMatrix.empty());
469 assert(!gatewayData.listSinkFormats.empty());
470 assert(!gatewayData.listSourceFormats.empty());
472 sqlite3_stmt* query = NULL;
476 //if sinkID is zero and the first Static Sink was already entered, the ID is created
477 if (gatewayData.gatewayID == 0 && !mFirstStaticGateway)
479 command = "INSERT INTO " + std::string(GATEWAY_TABLE) + "(name, sinkID, sourceID, domainSinkID, domainSourceID, controlDomainID, inUse) VALUES (?,?,?,?,?,?,0)";
483 //check if the ID already exists
484 if (existGateway(gatewayData.gatewayID))
485 return E_ALREADY_EXISTS;
486 command = "INSERT INTO " + std::string(GATEWAY_TABLE) + "(name, sinkID, sourceID, domainSinkID, domainSourceID, controlDomainID, gatewayID, inUse) VALUES (?,?,?,?,?,?,?,0)";
489 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
490 sqlite3_bind_text(query, 1, gatewayData.name.c_str(), gatewayData.name.size(), SQLITE_STATIC);
491 sqlite3_bind_int(query, 2, gatewayData.sinkID);
492 sqlite3_bind_int(query, 3, gatewayData.sourceID);
493 sqlite3_bind_int(query, 4, gatewayData.domainSinkID);
494 sqlite3_bind_int(query, 5, gatewayData.domainSourceID);
495 sqlite3_bind_int(query, 6, gatewayData.controlDomainID);
497 //if the ID is not created, we add it to the query
498 if (gatewayData.gatewayID != 0)
500 sqlite3_bind_int(query, 7, gatewayData.gatewayID);
503 //if the first static sink is entered, we need to set it onto the boundary
504 else if (mFirstStaticGateway)
506 sqlite3_bind_int(query, 7, DYNAMIC_ID_BOUNDARY);
507 mFirstStaticGateway = false;
510 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
512 logError("DatabaseHandler::enterGatewayDB SQLITE Step error code:", eCode);
513 return E_DATABASE_ERROR;
516 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
518 logError("DatabaseHandler::enterGatewayDB SQLITE Finalize error code:", eCode);
519 return E_DATABASE_ERROR;
522 gatewayID = sqlite3_last_insert_rowid(mDatabase);
524 //now the convertion matrix todo: change the map implementation sometimes to blob in sqlite
525 mListConnectionFormat.insert(std::make_pair(gatewayID, gatewayData.convertionMatrix));
527 command = "CREATE TABLE GatewaySourceFormat" + i2s(gatewayID) + std::string("(soundFormat INTEGER)");
528 assert(this->sqQuery(command));
529 command = "CREATE TABLE GatewaySinkFormat" + i2s(gatewayID) + std::string("(soundFormat INTEGER)");
530 assert(this->sqQuery(command));
532 //fill ConnectionFormats
533 command = "INSERT INTO GatewaySourceFormat" + i2s(gatewayID) + std::string("(soundFormat) VALUES (?)");
534 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
535 std::vector<am_ConnectionFormat_e>::const_iterator connectionFormatIterator = gatewayData.listSourceFormats.begin();
536 for (; connectionFormatIterator < gatewayData.listSourceFormats.end(); ++connectionFormatIterator)
538 sqlite3_bind_int(query, 1, *connectionFormatIterator);
539 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
541 logError("DatabaseHandler::enterGatewayDB SQLITE Step error code:", eCode);
542 return E_DATABASE_ERROR;
544 sqlite3_reset(query);
547 command = "INSERT INTO GatewaySinkFormat" + i2s(gatewayID) + std::string("(soundFormat) VALUES (?)");
548 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
549 connectionFormatIterator = gatewayData.listSinkFormats.begin();
550 for (; connectionFormatIterator < gatewayData.listSinkFormats.end(); ++connectionFormatIterator)
552 sqlite3_bind_int(query, 1, *connectionFormatIterator);
553 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
555 logError("DatabaseHandler::enterGatewayDB SQLITE Step error code:", eCode);
556 return E_DATABASE_ERROR;
558 sqlite3_reset(query);
561 logInfo("DatabaseHandler::enterGatewayDB entered new gateway with name", gatewayData.name, "sourceID:", gatewayData.sourceID, "sinkID:", gatewayData.sinkID, "assigned ID:", gatewayID);
562 am_Gateway_s gateway = gatewayData;
563 gateway.gatewayID = gatewayID;
564 if (mDatabaseObserver)
565 mDatabaseObserver->newGateway(gateway);
569 am_Error_e DatabaseHandler::enterSourceDB(const am_Source_s & sourceData, am_sourceID_t & sourceID)
571 assert(sourceData.sourceID<DYNAMIC_ID_BOUNDARY);
572 assert(sourceData.domainID!=0);
573 assert(!sourceData.name.empty());
574 assert(sourceData.sourceClassID!=0);
575 // \todo: need to check if class exists?
576 assert(!sourceData.listConnectionFormats.empty());
577 assert(sourceData.sourceState>=SS_ON && sourceData.sourceState<=SS_PAUSED);
579 sqlite3_stmt* query = NULL, *queryFinal = NULL;
582 std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE name=? AND reserved=1";
584 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
585 sqlite3_bind_text(query, 1, sourceData.name.c_str(), sourceData.name.size(), SQLITE_STATIC);
587 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
589 command = "UPDATE " + std::string(SOURCE_TABLE) + " SET name=?, domainID=?, sourceClassID=?, sourceState=?, volume=?, visible=?, availability=?, availabilityReason=?, interruptState=?, reserved=? WHERE sourceID=" + i2s(sqlite3_column_int(query, 0));
591 else if (eCode == SQLITE_DONE)
593 //if sinkID is zero and the first Static Sink was already entered, the ID is created
594 if (sourceData.sourceID == 0 && !mFirstStaticSource && !existSourceName(sourceData.name))
596 command = "INSERT INTO " + std::string(SOURCE_TABLE) + "(name, domainID, sourceClassID, sourceState, volume, visible, availability, availabilityReason, interruptState, reserved) VALUES (?,?,?,?,?,?,?,?,?,?)";
600 //check if the ID already exists
601 if (existSourceNameOrID(sourceData.sourceID, sourceData.name))
603 sqlite3_finalize(query);
604 return E_ALREADY_EXISTS;
606 command = "INSERT INTO " + std::string(SOURCE_TABLE) + "(name, domainID, sourceClassID, sourceState, volume, visible, availability, availabilityReason, interruptState, reserved, sourceID) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
611 logError("DatabaseHandler::enterSourceDB SQLITE Step error code:", eCode);
612 sqlite3_finalize(query);
613 return E_DATABASE_ERROR;
616 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
618 logError("DatabaseHandler::enterSourceDB SQLITE Finalize error code:", eCode);
619 return E_DATABASE_ERROR;
621 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &queryFinal, NULL);
622 sqlite3_bind_text(queryFinal, 1, sourceData.name.c_str(), sourceData.name.size(), SQLITE_STATIC);
623 sqlite3_bind_int(queryFinal, 2, sourceData.domainID);
624 sqlite3_bind_int(queryFinal, 3, sourceData.sourceClassID);
625 sqlite3_bind_int(queryFinal, 4, sourceData.sourceState);
626 sqlite3_bind_int(queryFinal, 5, sourceData.volume);
627 sqlite3_bind_int(queryFinal, 6, sourceData.visible);
628 sqlite3_bind_int(queryFinal, 7, sourceData.available.availability);
629 sqlite3_bind_int(queryFinal, 8, sourceData.available.availabilityReason);
630 sqlite3_bind_int(queryFinal, 9, sourceData.interruptState);
631 sqlite3_bind_int(queryFinal, 10, 0);
633 //if the ID is not created, we add it to the query
634 if (sourceData.sourceID != 0)
636 sqlite3_bind_int(queryFinal, 11, sourceData.sourceID);
639 //if the first static sink is entered, we need to set it onto the boundary
640 else if (mFirstStaticSource)
642 sqlite3_bind_int(queryFinal, 11, DYNAMIC_ID_BOUNDARY);
643 mFirstStaticSource = false;
646 if ((eCode = sqlite3_step(queryFinal)) != SQLITE_DONE)
648 logError("DatabaseHandler::enterSourceDB SQLITE Step error code:", eCode);
649 sqlite3_finalize(queryFinal);
650 return E_DATABASE_ERROR;
653 if ((eCode = sqlite3_finalize(queryFinal)) != SQLITE_OK)
655 logError("DatabaseHandler::enterSourceDB SQLITE Finalize error code:", eCode);
656 sqlite3_finalize(queryFinal);
657 return E_DATABASE_ERROR;
660 //now read back the sinkID
661 command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE name=?";
662 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
663 sqlite3_bind_text(query, 1, sourceData.name.c_str(), sourceData.name.size(), SQLITE_STATIC);
664 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
666 sourceID = sqlite3_column_int(query, 0);
671 logError("DatabaseHandler::existSink database error!:", eCode);
672 sqlite3_finalize(query);
673 return E_DATABASE_ERROR;
675 sqlite3_finalize(query);
677 //now we need to create the additional tables:
678 command = "CREATE TABLE SourceConnectionFormat" + i2s(sourceID) + std::string("(soundFormat INTEGER)");
679 assert(this->sqQuery(command));
680 command = "CREATE TABLE SourceMainSoundProperty" + i2s(sourceID) + std::string("(soundPropertyType INTEGER, value INTEGER)");
681 assert(this->sqQuery(command));
682 command = "CREATE TABLE SourceSoundProperty" + i2s(sourceID) + std::string("(soundPropertyType INTEGER, value INTEGER)");
683 assert(this->sqQuery(command));
685 //fill ConnectionFormats
686 command = "INSERT INTO SourceConnectionFormat" + i2s(sourceID) + std::string("(soundFormat) VALUES (?)");
687 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
688 std::vector<am_ConnectionFormat_e>::const_iterator connectionFormatIterator = sourceData.listConnectionFormats.begin();
689 for (; connectionFormatIterator < sourceData.listConnectionFormats.end(); ++connectionFormatIterator)
691 sqlite3_bind_int(query, 1, *connectionFormatIterator);
692 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
694 logError("DatabaseHandler::enterSourceDB SQLITE Step error code:", eCode);
695 sqlite3_finalize(query);
696 return E_DATABASE_ERROR;
698 sqlite3_reset(query);
701 //Fill MainSinkSoundProperties
702 command = "INSERT INTO SourceMainSoundProperty" + i2s(sourceID) + std::string("(soundPropertyType,value) VALUES (?,?)");
703 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
704 std::vector<am_MainSoundProperty_s>::const_iterator mainSoundPropertyIterator = sourceData.listMainSoundProperties.begin();
705 for (; mainSoundPropertyIterator < sourceData.listMainSoundProperties.end(); ++mainSoundPropertyIterator)
707 sqlite3_bind_int(query, 1, mainSoundPropertyIterator->type);
708 sqlite3_bind_int(query, 2, mainSoundPropertyIterator->value);
709 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
711 logError("DatabaseHandler::enterSourceDB SQLITE Step error code:", eCode);
712 sqlite3_finalize(query);
713 return E_DATABASE_ERROR;
715 sqlite3_reset(query);
718 //Fill SinkSoundProperties
719 command = "INSERT INTO SourceSoundProperty" + i2s(sourceID) + std::string("(soundPropertyType,value) VALUES (?,?)");
720 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
721 std::vector<am_SoundProperty_s>::const_iterator SoundPropertyIterator = sourceData.listSoundProperties.begin();
722 for (; SoundPropertyIterator < sourceData.listSoundProperties.end(); ++SoundPropertyIterator)
724 sqlite3_bind_int(query, 1, SoundPropertyIterator->type);
725 sqlite3_bind_int(query, 2, SoundPropertyIterator->value);
726 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
728 logError("DatabaseHandler::enterSinkDB SQLITE Step error code:", eCode);
729 sqlite3_finalize(query);
730 return E_DATABASE_ERROR;
732 sqlite3_reset(query);
735 logInfo("DatabaseHandler::enterSinkDB entered new source with name", sourceData.name, "domainID:", sourceData.domainID, "classID:", sourceData.sourceClassID, "visible:", sourceData.visible, "assigned ID:", sourceID);
737 am_Source_s source = sourceData;
738 source.sourceID = sourceID;
739 if (mDatabaseObserver)
740 mDatabaseObserver->newSource(source);
744 am_Error_e DatabaseHandler::changeMainConnectionRouteDB(const am_mainConnectionID_t mainconnectionID, const am_Route_s & route)
746 assert(mainconnectionID!=0);
747 if (!existMainConnection(mainconnectionID))
749 return E_NON_EXISTENT;
751 sqlite3_stmt* query = NULL;
755 std::vector<uint16_t> listOfConnections;
757 command = "SELECT connectionID, delay FROM " + std::string(CONNECTION_TABLE) + (" WHERE sourceID=? AND sinkID=? AND connectionFormat=?");
758 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
759 std::vector<am_RoutingElement_s>::const_iterator elementIterator = route.route.begin();
760 for (; elementIterator < route.route.end(); ++elementIterator)
762 sqlite3_bind_int(query, 1, elementIterator->sourceID);
763 sqlite3_bind_int(query, 2, elementIterator->sinkID);
764 sqlite3_bind_int(query, 3, elementIterator->connectionFormat);
766 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
768 listOfConnections.push_back(sqlite3_column_int(query, 0));
769 int16_t temp_delay = sqlite3_column_int(query, 1);
770 if (temp_delay != -1 && delay != -1)
777 logError("DatabaseHandler::changeMainConnectionRouteDB did not find route for MainConnection:", eCode);
779 return E_DATABASE_ERROR;
781 sqlite3_reset(query);
784 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
786 logError("DatabaseHandler::changeMainConnectionRouteDB SQLITE Finalize error code:", eCode);
788 return E_DATABASE_ERROR;
791 //now we delete the data in the table
792 command = "DELETE from MainConnectionRoute" + i2s(mainconnectionID);
793 assert(this->sqQuery(command));
795 command = "INSERT INTO MainConnectionRoute" + i2s(mainconnectionID) + "(connectionID) VALUES (?)";
796 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
797 std::vector<uint16_t>::iterator listConnectionIterator = listOfConnections.begin();
798 for (; listConnectionIterator < listOfConnections.end(); ++listConnectionIterator)
800 sqlite3_bind_int(query, 1, *listConnectionIterator);
801 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
803 logError("DatabaseHandler::changeMainConnectionRouteDB SQLITE Step error code:", eCode);
805 return E_DATABASE_ERROR;
807 sqlite3_reset(query);
810 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
812 logError("DatabaseHandler::changeMainConnectionRouteDB SQLITE Finalize error code:", eCode);
814 return E_DATABASE_ERROR;
816 logInfo("DatabaseHandler::changeMainConnectionRouteDB entered new route:", mainconnectionID);
820 am_Error_e DatabaseHandler::changeMainConnectionStateDB(const am_mainConnectionID_t mainconnectionID, const am_ConnectionState_e connectionState)
822 assert(mainconnectionID!=0);
824 sqlite3_stmt* query = NULL;
828 if (!existMainConnection(mainconnectionID))
830 return E_NON_EXISTENT;
832 command = "UPDATE " + std::string(MAINCONNECTION_TABLE) + " SET connectionState=? WHERE mainConnectionID=" + i2s(mainconnectionID);
833 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
834 sqlite3_bind_int(query, 1, connectionState);
835 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
837 logError("DatabaseHandler::changeMainConnectionStateDB SQLITE Step error code:", eCode);
838 return E_DATABASE_ERROR;
840 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
842 logError("DatabaseHandler::changeMainConnectionStateDB SQLITE Finalize error code:", eCode);
843 return E_DATABASE_ERROR;
845 logInfo("DatabaseHandler::changeMainConnectionStateDB changed mainConnectionState of MainConnection:", mainconnectionID, "to:", connectionState);
847 if (mDatabaseObserver)
848 mDatabaseObserver->mainConnectionStateChanged(mainconnectionID, connectionState);
852 am_Error_e DatabaseHandler::changeSinkMainVolumeDB(const am_mainVolume_t mainVolume, const am_sinkID_t sinkID)
856 sqlite3_stmt* query = NULL;
860 if (!existSink(sinkID))
862 return E_NON_EXISTENT;
864 command = "UPDATE " + std::string(SINK_TABLE) + " SET mainVolume=? WHERE sinkID=" + i2s(sinkID);
865 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
866 sqlite3_bind_int(query, 1, mainVolume);
867 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
869 logError("DatabaseHandler::changeSinkMainVolumeDB SQLITE Step error code:", eCode);
870 return E_DATABASE_ERROR;
872 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
874 logError("DatabaseHandler::changeSinkMainVolumeDB SQLITE Finalize error code:", eCode);
875 return E_DATABASE_ERROR;
878 logInfo("DatabaseHandler::changeSinkMainVolumeDB changed mainVolume of sink:", sinkID, "to:", mainVolume);
880 if (mDatabaseObserver)
881 mDatabaseObserver->volumeChanged(sinkID, mainVolume);
886 am_Error_e DatabaseHandler::changeSinkAvailabilityDB(const am_Availability_s & availability, const am_sinkID_t sinkID)
890 sqlite3_stmt* query = NULL;
894 if (!existSink(sinkID))
896 return E_NON_EXISTENT;
898 command = "UPDATE " + std::string(SINK_TABLE) + " SET availability=?, availabilityReason=? WHERE sinkID=" + i2s(sinkID);
899 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
900 sqlite3_bind_int(query, 1, availability.availability);
901 sqlite3_bind_int(query, 2, availability.availabilityReason);
902 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
904 logError("DatabaseHandler::changeSinkAvailabilityDB SQLITE Step error code:", eCode);
905 return E_DATABASE_ERROR;
908 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
910 logError("DatabaseHandler::changeSinkAvailabilityDB SQLITE Finalize error code:", eCode);
911 return E_DATABASE_ERROR;
914 logInfo("DatabaseHandler::changeSinkAvailabilityDB changed sinkAvailability of sink:", sinkID, "to:", availability.availability, "Reason:", availability.availabilityReason);
916 if (mDatabaseObserver && sourceVisible(sinkID))
917 mDatabaseObserver->sinkAvailabilityChanged(sinkID, availability);
921 am_Error_e DatabaseHandler::changDomainStateDB(const am_DomainState_e domainState, const am_domainID_t domainID)
925 sqlite3_stmt* query = NULL;
929 if (!existDomain(domainID))
931 return E_NON_EXISTENT;
933 command = "UPDATE " + std::string(DOMAIN_TABLE) + " SET state=? WHERE domainID=" + i2s(domainID);
934 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
935 sqlite3_bind_int(query, 1, domainState);
936 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
938 logError("DatabaseHandler::changDomainStateDB SQLITE Step error code:", eCode);
940 return E_DATABASE_ERROR;
943 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
945 logError("DatabaseHandler::changDomainStateDB SQLITE Finalize error code:", eCode);
946 return E_DATABASE_ERROR;
949 logInfo("DatabaseHandler::changDomainStateDB changed domainState of domain:", domainID, "to:", domainState);
953 am_Error_e DatabaseHandler::changeSinkMuteStateDB(const am_MuteState_e muteState, const am_sinkID_t sinkID)
957 sqlite3_stmt* query = NULL;
961 if (!existSink(sinkID))
963 return E_NON_EXISTENT;
965 command = "UPDATE " + std::string(SINK_TABLE) + " SET muteState=? WHERE sinkID=" + i2s(sinkID);
966 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
967 sqlite3_bind_int(query, 1, muteState);
968 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
970 logError("DatabaseHandler::changeSinkMuteStateDB SQLITE Step error code:", eCode);
971 return E_DATABASE_ERROR;
974 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
976 logError("DatabaseHandler::changeSinkMuteStateDB SQLITE Finalize error code:", eCode);
977 return E_DATABASE_ERROR;
980 logInfo("DatabaseHandler::changeSinkMuteStateDB changed sinkMuteState of sink:", sinkID, "to:", muteState);
982 if (mDatabaseObserver)
983 mDatabaseObserver->sinkMuteStateChanged(sinkID, muteState);
988 am_Error_e DatabaseHandler::changeMainSinkSoundPropertyDB(const am_MainSoundProperty_s & soundProperty, const am_sinkID_t sinkID)
990 //todo: add checks if soundproperty exists!
993 sqlite3_stmt* query = NULL;
997 if (!existSink(sinkID))
999 return E_NON_EXISTENT;
1001 command = "UPDATE SinkMainSoundProperty" + i2s(sinkID) + " SET value=? WHERE soundPropertyType=" + i2s(soundProperty.type);
1002 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1003 sqlite3_bind_int(query, 1, soundProperty.value);
1004 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1006 logError("DatabaseHandler::changeMainSinkSoundPropertyDB SQLITE Step error code:", eCode);
1007 return E_DATABASE_ERROR;
1010 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1012 logError("DatabaseHandler::changeMainSinkSoundPropertyDB SQLITE Finalize error code:", eCode);
1013 return E_DATABASE_ERROR;
1016 logInfo("DatabaseHandler::changeMainSinkSoundPropertyDB changed MainSinkSoundProperty of sink:", sinkID, "type:", soundProperty.type, "to:", soundProperty.value);
1017 if (mDatabaseObserver)
1018 mDatabaseObserver->mainSinkSoundPropertyChanged(sinkID, soundProperty);
1022 am_Error_e DatabaseHandler::changeMainSourceSoundPropertyDB(const am_MainSoundProperty_s & soundProperty, const am_sourceID_t sourceID)
1024 //todo: add checks if soundproperty exists!
1025 assert(sourceID!=0);
1027 sqlite3_stmt* query = NULL;
1029 std::string command;
1031 if (!existSource(sourceID))
1033 return E_NON_EXISTENT;
1035 command = "UPDATE SourceMainSoundProperty" + i2s(sourceID) + " SET value=? WHERE soundPropertyType=" + i2s(soundProperty.type);
1036 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1037 sqlite3_bind_int(query, 1, soundProperty.value);
1038 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1040 logError("DatabaseHandler::changeMainSourceSoundPropertyDB SQLITE Step error code:", eCode);
1041 return E_DATABASE_ERROR;
1044 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1046 logError("DatabaseHandler::changeMainSourceSoundPropertyDB SQLITE Finalize error code:", eCode);
1047 return E_DATABASE_ERROR;
1050 logInfo("DatabaseHandler::changeMainSourceSoundPropertyDB changed MainSinkSoundProperty of source:", sourceID, "type:", soundProperty.type, "to:", soundProperty.value);
1052 if (mDatabaseObserver)
1053 mDatabaseObserver->mainSourceSoundPropertyChanged(sourceID, soundProperty);
1057 am_Error_e DatabaseHandler::changeSourceAvailabilityDB(const am_Availability_s & availability, const am_sourceID_t sourceID)
1059 assert(sourceID!=0);
1061 sqlite3_stmt* query = NULL;
1063 std::string command;
1065 if (!existSource(sourceID))
1067 return E_NON_EXISTENT;
1069 command = "UPDATE " + std::string(SOURCE_TABLE) + " SET availability=?, availabilityReason=? WHERE sourceID=" + i2s(sourceID);
1070 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1071 sqlite3_bind_int(query, 1, availability.availability);
1072 sqlite3_bind_int(query, 2, availability.availabilityReason);
1073 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1075 logError("DatabaseHandler::changeSourceAvailabilityDB SQLITE Step error code:", eCode);
1076 return E_DATABASE_ERROR;
1079 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1081 logError("DatabaseHandler::changeSourceAvailabilityDB SQLITE Finalize error code:", eCode);
1082 return E_DATABASE_ERROR;
1085 logInfo("DatabaseHandler::changeSourceAvailabilityDB changed changeSourceAvailabilityDB of source:", sourceID, "to:", availability.availability, "Reason:", availability.availabilityReason);
1087 if (mDatabaseObserver && sourceVisible(sourceID))
1088 mDatabaseObserver->sourceAvailabilityChanged(sourceID, availability);
1092 am_Error_e DatabaseHandler::changeSystemPropertyDB(const am_SystemProperty_s & property)
1094 sqlite3_stmt* query = NULL;
1096 std::string command = "UPDATE " + std::string(SYSTEM_TABLE) + " set value=? WHERE type=?";
1098 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1099 sqlite3_bind_int(query, 1, property.value);
1100 sqlite3_bind_int(query, 2, property.type);
1102 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1104 logError("DatabaseHandler::changeSystemPropertyDB SQLITE Step error code:", eCode);
1105 return E_DATABASE_ERROR;
1108 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1110 logError("DatabaseHandler::changeSystemPropertyDB SQLITE Finalize error code:", eCode);
1111 return E_DATABASE_ERROR;
1114 logInfo("DatabaseHandler::changeSystemPropertyDB changed system property");
1116 if (mDatabaseObserver)
1117 mDatabaseObserver->systemPropertyChanged(property);
1122 am_Error_e DatabaseHandler::removeMainConnectionDB(const am_mainConnectionID_t mainConnectionID)
1124 assert(mainConnectionID!=0);
1126 if (!existMainConnection(mainConnectionID))
1128 return E_NON_EXISTENT;
1130 std::string command = "DELETE from " + std::string(MAINCONNECTION_TABLE) + " WHERE mainConnectionID=" + i2s(mainConnectionID);
1131 std::string command1 = "DROP table MainConnectionRoute" + i2s(mainConnectionID);
1132 if (!sqQuery(command))
1133 return E_DATABASE_ERROR;
1134 if (!sqQuery(command1))
1135 return E_DATABASE_ERROR;
1136 logInfo("DatabaseHandler::removeMainConnectionDB removed:", mainConnectionID);
1137 if (mDatabaseObserver)
1139 mDatabaseObserver->mainConnectionStateChanged(mainConnectionID, CS_DISCONNECTED);
1140 mDatabaseObserver->numberOfMainConnectionsChanged();
1145 am_Error_e DatabaseHandler::removeSinkDB(const am_sinkID_t sinkID)
1149 if (!existSink(sinkID))
1151 return E_NON_EXISTENT;
1153 std::string command = "DELETE from " + std::string(SINK_TABLE) + " WHERE sinkID=" + i2s(sinkID);
1154 std::string command1 = "DROP table SinkConnectionFormat" + i2s(sinkID);
1155 std::string command2 = "DROP table SinkMainSoundProperty" + i2s(sinkID);
1156 std::string command3 = "DROP table SinkSoundProperty" + i2s(sinkID);
1157 if (!sqQuery(command))
1158 return E_DATABASE_ERROR;
1159 if (!sqQuery(command1))
1160 return E_DATABASE_ERROR;
1161 if (!sqQuery(command2))
1162 return E_DATABASE_ERROR;
1163 if (!sqQuery(command3))
1164 return E_DATABASE_ERROR;
1165 logInfo("DatabaseHandler::removeSinkDB removed:", sinkID);
1167 if (mDatabaseObserver != NULL)
1168 mDatabaseObserver->removedSink(sinkID);
1173 am_Error_e DatabaseHandler::removeSourceDB(const am_sourceID_t sourceID)
1175 assert(sourceID!=0);
1177 if (!existSource(sourceID))
1179 return E_NON_EXISTENT;
1181 std::string command = "DELETE from " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + i2s(sourceID);
1182 std::string command1 = "DROP table SourceConnectionFormat" + i2s(sourceID);
1183 std::string command2 = "DROP table SourceMainSoundProperty" + i2s(sourceID);
1184 std::string command3 = "DROP table SourceSoundProperty" + i2s(sourceID);
1185 if (!sqQuery(command))
1186 return E_DATABASE_ERROR;
1187 if (!sqQuery(command1))
1188 return E_DATABASE_ERROR;
1189 if (!sqQuery(command2))
1190 return E_DATABASE_ERROR;
1191 if (!sqQuery(command3))
1192 return E_DATABASE_ERROR;
1193 logInfo("DatabaseHandler::removeSourceDB removed:", sourceID);
1194 if (mDatabaseObserver)
1195 mDatabaseObserver->removedSource(sourceID);
1199 am_Error_e DatabaseHandler::removeGatewayDB(const am_gatewayID_t gatewayID)
1201 assert(gatewayID!=0);
1203 if (!existGateway(gatewayID))
1205 return E_NON_EXISTENT;
1207 std::string command = "DELETE from " + std::string(GATEWAY_TABLE) + " WHERE gatewayID=" + i2s(gatewayID);
1208 if (!sqQuery(command))
1209 return E_DATABASE_ERROR;
1210 logInfo("DatabaseHandler::removeGatewayDB removed:", gatewayID);
1211 if (mDatabaseObserver)
1212 mDatabaseObserver->removeGateway(gatewayID);
1216 am_Error_e DatabaseHandler::removeCrossfaderDB(const am_crossfaderID_t crossfaderID)
1218 //todo: implement crossdfader
1219 (void) crossfaderID;
1223 am_Error_e DatabaseHandler::removeDomainDB(const am_domainID_t domainID)
1225 assert(domainID!=0);
1227 if (!existDomain(domainID))
1229 return E_NON_EXISTENT;
1231 std::string command = "DELETE from " + std::string(DOMAIN_TABLE) + " WHERE domainID=" + i2s(domainID);
1232 if (!sqQuery(command))
1233 return E_DATABASE_ERROR;
1234 logInfo("DatabaseHandler::removeDomainDB removed:", domainID);
1235 if (mDatabaseObserver)
1236 mDatabaseObserver->removeDomain(domainID);
1240 am_Error_e DatabaseHandler::removeSinkClassDB(const am_sinkClass_t sinkClassID)
1242 assert(sinkClassID!=0);
1244 if (!existSinkClass(sinkClassID))
1246 return E_NON_EXISTENT;
1248 std::string command = "DELETE from " + std::string(SINK_CLASS_TABLE) + " WHERE sinkClassID=" + i2s(sinkClassID);
1249 std::string command1 = "DROP table SinkClassProperties" + i2s(sinkClassID);
1250 if (!sqQuery(command))
1251 return E_DATABASE_ERROR;
1252 if (!sqQuery(command1))
1253 return E_DATABASE_ERROR;
1255 logInfo("DatabaseHandler::removeSinkClassDB removed:", sinkClassID);
1256 if (mDatabaseObserver)
1257 mDatabaseObserver->numberOfSinkClassesChanged();
1262 am_Error_e DatabaseHandler::removeSourceClassDB(const am_sourceClass_t sourceClassID)
1264 assert(sourceClassID!=0);
1266 if (!existSourceClass(sourceClassID))
1268 return E_NON_EXISTENT;
1270 std::string command = "DELETE from " + std::string(SOURCE_CLASS_TABLE) + " WHERE sourceClassID=" + i2s(sourceClassID);
1271 std::string command1 = "DROP table SourceClassProperties" + i2s(sourceClassID);
1272 if (!sqQuery(command))
1273 return E_DATABASE_ERROR;
1274 if (!sqQuery(command1))
1275 return E_DATABASE_ERROR;
1276 logInfo("DatabaseHandler::removeSourceClassDB removed:", sourceClassID);
1277 if (mDatabaseObserver)
1278 mDatabaseObserver->numberOfSourceClassesChanged();
1282 am_Error_e DatabaseHandler::removeConnection(const am_connectionID_t connectionID)
1284 assert(connectionID!=0);
1286 std::string command = "DELETE from " + std::string(CONNECTION_TABLE) + " WHERE connectionID=" + i2s(connectionID);
1287 std::string command1 = "DROP table SourceClassProperties" + i2s(connectionID);
1288 if (!sqQuery(command))
1289 return E_DATABASE_ERROR;
1290 if (!sqQuery(command1))
1291 return E_DATABASE_ERROR;
1292 logInfo("DatabaseHandler::removeConnection removed:", connectionID);
1296 am_Error_e DatabaseHandler::getSourceClassInfoDB(const am_sourceID_t sourceID, am_SourceClass_s & classInfo) const
1298 assert(sourceID!=0);
1300 if (!existSource(sourceID))
1302 return E_NON_EXISTENT;
1304 sqlite3_stmt* query = NULL;
1306 am_ClassProperty_s propertyTemp;
1307 std::string command = "SELECT sourceClassID FROM " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + (i2s(sourceID));
1308 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1310 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1312 classInfo.sourceClassID = sqlite3_column_int(query, 0);
1315 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1317 logError("DatabaseHandler::getSourceClassInfoDB SQLITE error code:", eCode);
1318 return E_DATABASE_ERROR;
1321 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1323 logError("DatabaseHandler::getSourceClassInfoDB SQLITE Finalize error code:", eCode);
1324 return E_DATABASE_ERROR;
1327 command = "SELECT name FROM " + std::string(SOURCE_CLASS_TABLE) + " WHERE sourceClassID=" + (i2s(classInfo.sourceClassID));
1328 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1330 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1332 classInfo.name = std::string((const char*) sqlite3_column_text(query, 0));
1335 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1337 logError("DatabaseHandler::getSourceClassInfoDB SQLITE error code:", eCode);
1338 return E_DATABASE_ERROR;
1341 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1343 logError("DatabaseHandler::getSourceClassInfoDB SQLITE Finalize error code:", eCode);
1344 return E_DATABASE_ERROR;
1347 //read out Properties
1348 command = "SELECT classProperty, value FROM SourceClassProperties" + i2s(classInfo.sourceClassID);
1349 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1350 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1352 propertyTemp.classProperty = (am_ClassProperty_e) sqlite3_column_int(query, 0);
1353 propertyTemp.value = sqlite3_column_int(query, 1);
1354 classInfo.listClassProperties.push_back(propertyTemp);
1357 if (eCode != SQLITE_DONE)
1359 logError("DatabaseHandler::getSourceClassInfoDB SQLITE error code:", eCode);
1360 return E_DATABASE_ERROR;
1363 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1365 logError("DatabaseHandler::getSourceClassInfoDB SQLITE Finalize error code:", eCode);
1366 return E_DATABASE_ERROR;
1371 am_Error_e DatabaseHandler::changeSinkClassInfoDB(const am_SinkClass_s& sinkClass)
1373 assert(sinkClass.sinkClassID!=0);
1374 assert(!sinkClass.listClassProperties.empty());
1376 sqlite3_stmt* query = NULL;
1379 //check if the ID already exists
1380 if (!existSinkClass(sinkClass.sinkClassID))
1381 return E_NON_EXISTENT;
1383 //fill ConnectionFormats
1384 std::string command = "UPDATE SinkClassProperties" + i2s(sinkClass.sinkClassID) + " set value=? WHERE classProperty=?;";
1385 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1386 std::vector<am_ClassProperty_s>::const_iterator Iterator = sinkClass.listClassProperties.begin();
1387 for (; Iterator < sinkClass.listClassProperties.end(); ++Iterator)
1389 sqlite3_bind_int(query, 1, Iterator->value);
1390 sqlite3_bind_int(query, 2, Iterator->classProperty);
1391 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1393 logError("DatabaseHandler::setSinkClassInfoDB SQLITE Step error code:", eCode);
1394 return E_DATABASE_ERROR;
1396 sqlite3_reset(query);
1399 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1401 logError("DatabaseHandler::setSinkClassInfoDB SQLITE Finalize error code:", eCode);
1402 return E_DATABASE_ERROR;
1405 logInfo("DatabaseHandler::setSinkClassInfoDB set setSinkClassInfo");
1409 am_Error_e DatabaseHandler::changeSourceClassInfoDB(const am_SourceClass_s& sourceClass)
1411 assert(sourceClass.sourceClassID!=0);
1412 assert(!sourceClass.listClassProperties.empty());
1414 sqlite3_stmt* query = NULL;
1417 //check if the ID already exists
1418 if (!existSourceClass(sourceClass.sourceClassID))
1419 return E_NON_EXISTENT;
1421 //fill ConnectionFormats
1422 std::string command = "UPDATE SourceClassProperties" + i2s(sourceClass.sourceClassID) + " set value=? WHERE classProperty=?;";
1423 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1424 std::vector<am_ClassProperty_s>::const_iterator Iterator = sourceClass.listClassProperties.begin();
1425 for (; Iterator < sourceClass.listClassProperties.end(); ++Iterator)
1427 sqlite3_bind_int(query, 1, Iterator->value);
1428 sqlite3_bind_int(query, 2, Iterator->classProperty);
1429 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1431 logError("DatabaseHandler::setSinkClassInfoDB SQLITE Step error code:", eCode);
1432 return E_DATABASE_ERROR;
1434 sqlite3_reset(query);
1437 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1439 logError("DatabaseHandler::setSinkClassInfoDB SQLITE Finalize error code:", eCode);
1440 return E_DATABASE_ERROR;
1443 logInfo("DatabaseHandler::setSinkClassInfoDB set setSinkClassInfo");
1447 am_Error_e DatabaseHandler::getSinkClassInfoDB(const am_sinkID_t sinkID, am_SinkClass_s & sinkClass) const
1451 if (!existSink(sinkID))
1453 return E_NON_EXISTENT;
1455 sqlite3_stmt* query = NULL;
1457 am_ClassProperty_s propertyTemp;
1458 std::string command = "SELECT sinkClassID FROM " + std::string(SINK_TABLE) + " WHERE sinkID=" + (i2s(sinkID));
1459 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1461 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1463 sinkClass.sinkClassID = sqlite3_column_int(query, 0);
1466 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1468 logError("DatabaseHandler::getSinkClassInfoDB SQLITE error code:", eCode);
1469 return E_DATABASE_ERROR;
1472 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1474 logError("DatabaseHandler::getSinkClassInfoDB SQLITE Finalize error code:", eCode);
1475 return E_DATABASE_ERROR;
1478 command = "SELECT name FROM " + std::string(SINK_CLASS_TABLE) + " WHERE sinkClassID=" + (i2s(sinkClass.sinkClassID));
1479 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1481 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1483 sinkClass.name = std::string((const char*) sqlite3_column_text(query, 0));
1486 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1488 logError("DatabaseHandler::getSinkClassInfoDB SQLITE error code:", eCode);
1489 return E_DATABASE_ERROR;
1492 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1494 logError("DatabaseHandler::getSinkClassInfoDB SQLITE Finalize error code:", eCode);
1495 return E_DATABASE_ERROR;
1498 //read out Properties
1499 command = "SELECT classProperty, value FROM SinkClassProperties" + i2s(sinkClass.sinkClassID);
1500 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1501 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1503 propertyTemp.classProperty = (am_ClassProperty_e) sqlite3_column_int(query, 0);
1504 propertyTemp.value = sqlite3_column_int(query, 1);
1505 sinkClass.listClassProperties.push_back(propertyTemp);
1508 if (eCode != SQLITE_DONE)
1510 logError("DatabaseHandler::getSinkClassInfoDB SQLITE error code:", eCode);
1511 return E_DATABASE_ERROR;
1514 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1516 logError("DatabaseHandler::getSinkClassInfoDB SQLITE Finalize error code:", eCode);
1517 return E_DATABASE_ERROR;
1522 am_Error_e DatabaseHandler::getGatewayInfoDB(const am_gatewayID_t gatewayID, am_Gateway_s & gatewayData) const
1524 assert(gatewayID!=0);
1525 if (!existGateway(gatewayID))
1527 return E_NON_EXISTENT;
1529 sqlite3_stmt* query = NULL, *qSinkConnectionFormat = NULL, *qSourceConnectionFormat = NULL;
1531 am_ConnectionFormat_e tempConnectionFormat;
1532 std::string command = "SELECT name, sinkID, sourceID, domainSinkID, domainSourceID, controlDomainID, gatewayID FROM " + std::string(GATEWAY_TABLE) + " WHERE gatewayID=" + i2s(gatewayID);
1533 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1535 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1537 gatewayData.name = std::string((const char*) sqlite3_column_text(query, 0));
1538 gatewayData.sinkID = sqlite3_column_int(query, 1);
1539 gatewayData.sourceID = sqlite3_column_int(query, 2);
1540 gatewayData.domainSinkID = sqlite3_column_int(query, 3);
1541 gatewayData.domainSourceID = sqlite3_column_int(query, 4);
1542 gatewayData.controlDomainID = sqlite3_column_int(query, 5);
1543 gatewayData.gatewayID = sqlite3_column_int(query, 6);
1546 ListConnectionFormat::const_iterator iter = mListConnectionFormat.begin();
1547 iter = mListConnectionFormat.find(gatewayData.gatewayID);
1548 if (iter == mListConnectionFormat.end())
1550 logError("DatabaseHandler::getGatewayInfoDB database error with convertionFormat");
1551 return E_DATABASE_ERROR;
1553 gatewayData.convertionMatrix = iter->second;
1555 //read out the connectionFormats
1556 std::string commandConnectionFormat = "SELECT soundFormat FROM GatewaySourceFormat" + i2s(gatewayData.gatewayID);
1557 sqlite3_prepare_v2(mDatabase, commandConnectionFormat.c_str(), -1, &qSourceConnectionFormat, NULL);
1558 while ((eCode = sqlite3_step(qSourceConnectionFormat)) == SQLITE_ROW)
1560 tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qSourceConnectionFormat, 0);
1561 gatewayData.listSourceFormats.push_back(tempConnectionFormat);
1564 if ((eCode = sqlite3_finalize(qSourceConnectionFormat)) != SQLITE_OK)
1566 logError("DatabaseHandler::getGatewayInfoDB SQLITE Finalize error code:", eCode);
1567 return E_DATABASE_ERROR;
1570 //read out sound properties
1571 commandConnectionFormat = "SELECT soundFormat FROM GatewaySinkFormat" + i2s(gatewayData.gatewayID);
1572 sqlite3_prepare_v2(mDatabase, commandConnectionFormat.c_str(), -1, &qSinkConnectionFormat, NULL);
1573 while ((eCode = sqlite3_step(qSinkConnectionFormat)) == SQLITE_ROW)
1575 tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qSinkConnectionFormat, 0);
1576 gatewayData.listSinkFormats.push_back(tempConnectionFormat);
1579 if ((eCode = sqlite3_finalize(qSinkConnectionFormat)) != SQLITE_OK)
1581 logError("DatabaseHandler::getGatewayInfoDB SQLITE Finalize error code:", eCode);
1582 return E_DATABASE_ERROR;
1587 if (eCode != SQLITE_DONE)
1589 logError("DatabaseHandler::getGatewayInfoDB SQLITE error code:", eCode);
1590 return E_DATABASE_ERROR;
1593 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1595 logError("DatabaseHandler::getGatewayInfoDB SQLITE Finalize error code:", eCode);
1596 return E_DATABASE_ERROR;
1603 am_Error_e DatabaseHandler::getCrossfaderInfoDB(const am_crossfaderID_t crossfaderID, am_Crossfader_s & crossfaderData) const
1605 //todo: implement crossfader
1606 (void) crossfaderID;
1607 (void) crossfaderData;
1611 am_Error_e DatabaseHandler::getListSinksOfDomain(const am_domainID_t domainID, std::vector<am_sinkID_t> & listSinkID) const
1613 assert(domainID!=0);
1615 if (!existDomain(domainID))
1617 return E_NON_EXISTENT;
1619 sqlite3_stmt* query = NULL;
1622 std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 AND domainID=" + (i2s(domainID));
1623 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1625 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1627 temp = sqlite3_column_int(query, 0);
1628 listSinkID.push_back(temp);
1631 if (eCode != SQLITE_DONE)
1633 logError("DatabaseHandler::getListSinksOfDomain SQLITE error code:", eCode);
1634 return E_DATABASE_ERROR;
1637 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1639 logError("DatabaseHandler::getListSinksOfDomain SQLITE Finalize error code:", eCode);
1640 return E_DATABASE_ERROR;
1646 am_Error_e DatabaseHandler::getListSourcesOfDomain(const am_domainID_t domainID, std::vector<am_sourceID_t> & listSourceID) const
1648 assert(domainID!=0);
1649 listSourceID.clear();
1650 if (!existDomain(domainID))
1652 return E_NON_EXISTENT;
1654 sqlite3_stmt* query = NULL;
1657 std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE reserved=0 AND domainID=" + i2s(domainID);
1659 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1661 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1663 temp = sqlite3_column_int(query, 0);
1664 listSourceID.push_back(temp);
1667 if (eCode != SQLITE_DONE)
1669 logError("DatabaseHandler::getListSourcesOfDomain SQLITE error code:", eCode);
1670 return E_DATABASE_ERROR;
1673 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1675 logError("DatabaseHandler::getListSourcesOfDomain SQLITE Finalize error code:", eCode);
1676 return E_DATABASE_ERROR;
1682 am_Error_e DatabaseHandler::getListCrossfadersOfDomain(const am_domainID_t domainID, std::vector<am_crossfaderID_t> & listGatewaysID) const
1684 //todo: implement crossfader
1685 (void) listGatewaysID;
1691 am_Error_e DatabaseHandler::getListGatewaysOfDomain(const am_domainID_t domainID, std::vector<am_gatewayID_t> & listGatewaysID) const
1693 assert(domainID!=0);
1694 listGatewaysID.clear();
1695 if (!existDomain(domainID))
1697 return E_NON_EXISTENT;
1699 sqlite3_stmt* query = NULL;
1701 am_gatewayID_t temp;
1703 std::string command = "SELECT gatewayID FROM " + std::string(GATEWAY_TABLE) + " WHERE controlDomainID=" + i2s(domainID);
1704 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1706 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1708 temp = sqlite3_column_int(query, 0);
1709 listGatewaysID.push_back(temp);
1712 if (eCode != SQLITE_DONE)
1714 logError("DatabaseHandler::getListGatewaysOfDomain SQLITE error code:", eCode);
1715 return E_DATABASE_ERROR;
1718 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1720 logError("DatabaseHandler::getListGatewaysOfDomain SQLITE Finalize error code:", eCode);
1721 return E_DATABASE_ERROR;
1727 am_Error_e DatabaseHandler::getListMainConnections(std::vector<am_MainConnection_s> & listMainConnections) const
1729 listMainConnections.clear();
1730 sqlite3_stmt *query = NULL, *query1 = NULL, *query2 = NULL;
1732 am_MainConnection_s temp;
1733 am_RoutingElement_s tempRoute;
1735 std::string command = "SELECT mainConnectionID, sourceID, sinkID, connectionState, delay FROM " + std::string(MAINCONNECTION_TABLE);
1736 std::string command1 = "SELECT connectionID FROM MainConnectionRoute";
1737 std::string command2 = "SELECT sourceID, sinkID, connectionFormat FROM " + std::string(CONNECTION_TABLE) + " WHERE connectionID=?";
1738 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1739 sqlite3_prepare_v2(mDatabase, command2.c_str(), -1, &query2, NULL);
1741 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1743 temp.connectionID = sqlite3_column_int(query, 0);
1744 temp.route.sourceID = sqlite3_column_int(query, 1);
1745 temp.route.sinkID = sqlite3_column_int(query, 2);
1746 temp.connectionState = (am_ConnectionState_e) sqlite3_column_int(query, 3);
1747 temp.delay = sqlite3_column_int(query, 4);
1748 std::string statement = command1 + i2s(temp.connectionID);
1749 sqlite3_prepare_v2(mDatabase, statement.c_str(), -1, &query1, NULL);
1750 while ((eCode = sqlite3_step(query1)) == SQLITE_ROW) //todo: check results of eCode1, eCode2
1752 int k = sqlite3_column_int(query1, 0);
1753 sqlite3_bind_int(query2, 1, k);
1754 while ((eCode = sqlite3_step(query2)) == SQLITE_ROW)
1756 tempRoute.sourceID = sqlite3_column_int(query2, 0);
1757 tempRoute.sinkID = sqlite3_column_int(query2, 1);
1758 tempRoute.connectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(query2, 2);
1759 getDomainOfSource(tempRoute.sourceID, tempRoute.domainID);
1760 temp.route.route.push_back(tempRoute);
1762 sqlite3_reset(query2);
1764 listMainConnections.push_back(temp);
1767 if (eCode != SQLITE_DONE)
1769 logError("DatabaseHandler::getListMainConnections SQLITE error code:", eCode);
1770 return E_DATABASE_ERROR;
1773 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1775 logError("DatabaseHandler::getListMainConnections SQLITE Finalize error code:", eCode);
1776 return E_DATABASE_ERROR;
1782 am_Error_e DatabaseHandler::getListDomains(std::vector<am_Domain_s> & listDomains) const
1784 listDomains.clear();
1785 sqlite3_stmt* query = NULL;
1788 std::string command = "SELECT domainID, name, busname, nodename, early, complete, state FROM " + std::string(DOMAIN_TABLE) + " WHERE reserved=0";
1789 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1791 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1793 temp.domainID = sqlite3_column_int(query, 0);
1794 temp.name = std::string((const char*) sqlite3_column_text(query, 1));
1795 temp.busname = std::string((const char*) sqlite3_column_text(query, 2));
1796 temp.nodename = std::string((const char*) sqlite3_column_text(query, 3));
1797 temp.early = sqlite3_column_int(query, 4);
1798 temp.complete = sqlite3_column_int(query, 5);
1799 temp.state = (am_DomainState_e) sqlite3_column_int(query, 6);
1800 listDomains.push_back(temp);
1803 if (eCode != SQLITE_DONE)
1805 logError("DatabaseHandler::getListDomains SQLITE error code:", eCode);
1807 return E_DATABASE_ERROR;
1810 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1812 logError("DatabaseHandler::getListDomains SQLITE Finalize error code:", eCode);
1814 return E_DATABASE_ERROR;
1820 am_Error_e DatabaseHandler::getListConnections(std::vector<am_Connection_s> & listConnections) const
1822 listConnections.clear();
1823 sqlite3_stmt* query = NULL;
1825 am_Connection_s temp;
1826 std::string command = "SELECT connectionID, sourceID, sinkID, delay, connectionFormat FROM " + std::string(CONNECTION_TABLE) + " WHERE reserved=0";
1827 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1829 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1831 temp.connectionID = sqlite3_column_int(query, 0);
1832 temp.sourceID = sqlite3_column_int(query, 1);
1833 temp.sinkID = sqlite3_column_int(query, 2);
1834 temp.delay = sqlite3_column_int(query, 3);
1835 temp.connectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(query, 4);
1836 listConnections.push_back(temp);
1839 if (eCode != SQLITE_DONE)
1841 logError("DatabaseHandler::getListConnections SQLITE error code:", eCode);
1842 return E_DATABASE_ERROR;
1845 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1847 logError("DatabaseHandler::getListConnections SQLITE Finalize error code:", eCode);
1848 return E_DATABASE_ERROR;
1854 am_Error_e DatabaseHandler::getListSinks(std::vector<am_Sink_s> & listSinks) const
1857 sqlite3_stmt* query = NULL, *qConnectionFormat = NULL, *qSoundProperty = NULL, *qMAinSoundProperty = NULL;
1860 am_ConnectionFormat_e tempConnectionFormat;
1861 am_SoundProperty_s tempSoundProperty;
1862 am_MainSoundProperty_s tempMainSoundProperty;
1863 std::string command = "SELECT name, domainID, sinkClassID, volume, visible, availability, availabilityReason, muteState, mainVolume, sinkID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0";
1864 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1866 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1868 temp.name = std::string((const char*) sqlite3_column_text(query, 0));
1869 temp.domainID = sqlite3_column_int(query, 1);
1870 temp.sinkClassID = sqlite3_column_int(query, 2);
1871 temp.volume = sqlite3_column_int(query, 3);
1872 temp.visible = sqlite3_column_int(query, 4);
1873 temp.available.availability = (am_Availablility_e) sqlite3_column_int(query, 5);
1874 temp.available.availabilityReason = (am_AvailabilityReason_e) sqlite3_column_int(query, 6);
1875 temp.muteState = (am_MuteState_e) sqlite3_column_int(query, 7);
1876 temp.mainVolume = sqlite3_column_int(query, 8);
1877 temp.sinkID = sqlite3_column_int(query, 9);
1879 //read out the connectionFormats
1880 std::string commandConnectionFormat = "SELECT soundFormat FROM SinkConnectionFormat" + i2s(temp.sinkID);
1881 sqlite3_prepare_v2(mDatabase, commandConnectionFormat.c_str(), -1, &qConnectionFormat, NULL);
1882 while ((eCode = sqlite3_step(qConnectionFormat)) == SQLITE_ROW)
1884 tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qConnectionFormat, 0);
1885 temp.listConnectionFormats.push_back(tempConnectionFormat);
1888 if ((eCode = sqlite3_finalize(qConnectionFormat)) != SQLITE_OK)
1890 logError("DatabaseHandler::getListSinks SQLITE Finalize error code:", eCode);
1892 return E_DATABASE_ERROR;
1895 //read out sound properties
1896 std::string commandSoundProperty = "SELECT soundPropertyType, value FROM SinkSoundProperty" + i2s(temp.sinkID);
1897 sqlite3_prepare_v2(mDatabase, commandSoundProperty.c_str(), -1, &qSoundProperty, NULL);
1898 while ((eCode = sqlite3_step(qSoundProperty)) == SQLITE_ROW)
1900 tempSoundProperty.type = (am_SoundPropertyType_e) sqlite3_column_int(qSoundProperty, 0);
1901 tempSoundProperty.value = sqlite3_column_int(qSoundProperty, 1);
1902 temp.listSoundProperties.push_back(tempSoundProperty);
1905 if ((eCode = sqlite3_finalize(qSoundProperty)) != SQLITE_OK)
1907 logError("DatabaseHandler::getListSinks SQLITE Finalize error code:", eCode);
1909 return E_DATABASE_ERROR;
1912 //read out MainSoundProperties
1913 std::string commandMainSoundProperty = "SELECT soundPropertyType, value FROM SinkMainSoundProperty" + i2s(temp.sinkID);
1914 sqlite3_prepare_v2(mDatabase, commandMainSoundProperty.c_str(), -1, &qMAinSoundProperty, NULL);
1915 while ((eCode = sqlite3_step(qMAinSoundProperty)) == SQLITE_ROW)
1917 tempMainSoundProperty.type = (am_MainSoundPropertyType_e) sqlite3_column_int(qMAinSoundProperty, 0);
1918 tempMainSoundProperty.value = sqlite3_column_int(qMAinSoundProperty, 1);
1919 temp.listMainSoundProperties.push_back(tempMainSoundProperty);
1922 if ((eCode = sqlite3_finalize(qMAinSoundProperty)) != SQLITE_OK)
1924 logError("DatabaseHandler::getListSinks SQLITE Finalize error code:", eCode);
1926 return E_DATABASE_ERROR;
1928 listSinks.push_back(temp);
1929 temp.listConnectionFormats.clear();
1930 temp.listMainSoundProperties.clear();
1931 temp.listSoundProperties.clear();
1934 if (eCode != SQLITE_DONE)
1936 logError("DatabaseHandler::getListSinks SQLITE error code:", eCode);
1938 return E_DATABASE_ERROR;
1941 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1943 logError("DatabaseHandler::getListSinks SQLITE Finalize error code:", eCode);
1945 return E_DATABASE_ERROR;
1951 am_Error_e DatabaseHandler::getListSources(std::vector<am_Source_s> & listSources) const
1953 listSources.clear();
1954 sqlite3_stmt* query = NULL, *qConnectionFormat = NULL, *qSoundProperty = NULL, *qMAinSoundProperty = NULL;
1957 am_ConnectionFormat_e tempConnectionFormat;
1958 am_SoundProperty_s tempSoundProperty;
1959 am_MainSoundProperty_s tempMainSoundProperty;
1960 std::string command = "SELECT name, domainID, sourceClassID, sourceState, volume, visible, availability, availabilityReason, interruptState, sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE reserved=0";
1961 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1963 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1965 temp.name = std::string((const char*) sqlite3_column_text(query, 0));
1966 temp.domainID = sqlite3_column_int(query, 1);
1967 temp.sourceClassID = sqlite3_column_int(query, 2);
1968 temp.sourceState = (am_SourceState_e) sqlite3_column_int(query, 3);
1969 temp.volume = sqlite3_column_int(query, 4);
1970 temp.visible = sqlite3_column_int(query, 5);
1971 temp.available.availability = (am_Availablility_e) sqlite3_column_int(query, 6);
1972 temp.available.availabilityReason = (am_AvailabilityReason_e) sqlite3_column_int(query, 7);
1973 temp.interruptState = (am_InterruptState_e) sqlite3_column_int(query, 8);
1974 temp.sourceID = sqlite3_column_int(query, 9);
1976 //read out the connectionFormats
1977 std::string commandConnectionFormat = "SELECT soundFormat FROM SourceConnectionFormat" + i2s(temp.sourceID);
1978 sqlite3_prepare_v2(mDatabase, commandConnectionFormat.c_str(), -1, &qConnectionFormat, NULL);
1979 while ((eCode = sqlite3_step(qConnectionFormat)) == SQLITE_ROW)
1981 tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qConnectionFormat, 0);
1982 temp.listConnectionFormats.push_back(tempConnectionFormat);
1985 if ((eCode = sqlite3_finalize(qConnectionFormat)) != SQLITE_OK)
1987 logError("DatabaseHandler::getListSources SQLITE Finalize error code:", eCode);
1989 return E_DATABASE_ERROR;
1992 //read out sound properties
1993 std::string commandSoundProperty = "SELECT soundPropertyType, value FROM SourceSoundProperty" + i2s(temp.sourceID);
1994 sqlite3_prepare_v2(mDatabase, commandSoundProperty.c_str(), -1, &qSoundProperty, NULL);
1995 while ((eCode = sqlite3_step(qSoundProperty)) == SQLITE_ROW)
1997 tempSoundProperty.type = (am_SoundPropertyType_e) sqlite3_column_int(qSoundProperty, 0);
1998 tempSoundProperty.value = sqlite3_column_int(qSoundProperty, 1);
1999 temp.listSoundProperties.push_back(tempSoundProperty);
2002 if ((eCode = sqlite3_finalize(qSoundProperty)) != SQLITE_OK)
2004 logError("DatabaseHandler::getListSources SQLITE Finalize error code:", eCode);
2006 return E_DATABASE_ERROR;
2009 //read out MainSoundProperties
2010 std::string commandMainSoundProperty = "SELECT soundPropertyType, value FROM SourceMainSoundProperty" + i2s(temp.sourceID);
2011 sqlite3_prepare_v2(mDatabase, commandMainSoundProperty.c_str(), -1, &qMAinSoundProperty, NULL);
2012 while ((eCode = sqlite3_step(qMAinSoundProperty)) == SQLITE_ROW)
2014 tempMainSoundProperty.type = (am_MainSoundPropertyType_e) sqlite3_column_int(qMAinSoundProperty, 0);
2015 tempMainSoundProperty.value = sqlite3_column_int(qMAinSoundProperty, 1);
2016 temp.listMainSoundProperties.push_back(tempMainSoundProperty);
2019 if ((eCode = sqlite3_finalize(qMAinSoundProperty)) != SQLITE_OK)
2021 logError("DatabaseHandler::getListSources SQLITE Finalize error code:", eCode);
2023 return E_DATABASE_ERROR;
2025 listSources.push_back(temp);
2026 temp.listConnectionFormats.clear();
2027 temp.listMainSoundProperties.clear();
2028 temp.listSoundProperties.clear();
2031 if (eCode != SQLITE_DONE)
2033 logError("DatabaseHandler::getListSources SQLITE error code:", eCode);
2035 return E_DATABASE_ERROR;
2038 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2040 logError("DatabaseHandler::getListSources SQLITE Finalize error code:", eCode);
2042 return E_DATABASE_ERROR;
2048 am_Error_e DatabaseHandler::getListSourceClasses(std::vector<am_SourceClass_s> & listSourceClasses) const
2050 listSourceClasses.clear();
2052 sqlite3_stmt* query = NULL, *subQuery = NULL;
2053 int eCode = 0, eCode1;
2054 am_SourceClass_s classTemp;
2055 am_ClassProperty_s propertyTemp;
2057 std::string command = "SELECT sourceClassID, name FROM " + std::string(SOURCE_CLASS_TABLE);
2058 std::string command2;
2059 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2061 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2063 classTemp.sourceClassID = sqlite3_column_int(query, 0);
2064 classTemp.name = std::string((const char*) sqlite3_column_text(query, 1));
2066 //read out Properties
2067 command2 = "SELECT classProperty, value FROM SourceClassProperties" + i2s(classTemp.sourceClassID);
2068 sqlite3_prepare_v2(mDatabase, command2.c_str(), -1, &subQuery, NULL);
2070 while ((eCode1 = sqlite3_step(subQuery)) == SQLITE_ROW)
2072 propertyTemp.classProperty = (am_ClassProperty_e) sqlite3_column_int(subQuery, 0);
2073 propertyTemp.value = sqlite3_column_int(subQuery, 1);
2074 classTemp.listClassProperties.push_back(propertyTemp);
2077 if (eCode1 != SQLITE_DONE)
2079 logError("DatabaseHandler::getListSourceClasses SQLITE error code:", eCode1);
2081 return E_DATABASE_ERROR;
2084 if ((eCode1 = sqlite3_finalize(subQuery)) != SQLITE_OK)
2086 logError("DatabaseHandler::getListSourceClasses SQLITE Finalize error code:", eCode1);
2088 return E_DATABASE_ERROR;
2090 listSourceClasses.push_back(classTemp);
2093 if (eCode != SQLITE_DONE)
2095 logError("DatabaseHandler::getListSourceClasses SQLITE error code:", eCode);
2097 return E_DATABASE_ERROR;
2100 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2102 logError("DatabaseHandler::getListSourceClasses SQLITE Finalize error code:", eCode);
2104 return E_DATABASE_ERROR;
2110 am_Error_e DatabaseHandler::getListCrossfaders(std::vector<am_Crossfader_s> & listCrossfaders) const
2112 //todo: implement crossfaders
2113 (void) listCrossfaders;
2117 am_Error_e DatabaseHandler::getListGateways(std::vector<am_Gateway_s> & listGateways) const
2119 listGateways.clear();
2120 sqlite3_stmt* query = NULL, *qSinkConnectionFormat = NULL, *qSourceConnectionFormat = NULL;
2123 am_ConnectionFormat_e tempConnectionFormat;
2125 std::string command = "SELECT name, sinkID, sourceID, domainSinkID, domainSourceID, controlDomainID, gatewayID FROM " + std::string(GATEWAY_TABLE);
2126 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2128 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2130 temp.name = std::string((const char*) sqlite3_column_text(query, 0));
2131 temp.sinkID = sqlite3_column_int(query, 1);
2132 temp.sourceID = sqlite3_column_int(query, 2);
2133 temp.domainSinkID = sqlite3_column_int(query, 3);
2134 temp.domainSourceID = sqlite3_column_int(query, 4);
2135 temp.controlDomainID = sqlite3_column_int(query, 5);
2136 temp.gatewayID = sqlite3_column_int(query, 6);
2139 ListConnectionFormat::const_iterator iter = mListConnectionFormat.begin();
2140 iter = mListConnectionFormat.find(temp.gatewayID);
2141 if (iter == mListConnectionFormat.end())
2143 logError("DatabaseHandler::getListGateways database error with convertionFormat");
2145 return E_DATABASE_ERROR;
2147 temp.convertionMatrix = iter->second;
2149 //read out the connectionFormats
2150 std::string commandConnectionFormat = "SELECT soundFormat FROM GatewaySourceFormat" + i2s(temp.gatewayID);
2151 sqlite3_prepare_v2(mDatabase, commandConnectionFormat.c_str(), -1, &qSourceConnectionFormat, NULL);
2152 while ((eCode = sqlite3_step(qSourceConnectionFormat)) == SQLITE_ROW)
2154 tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qSourceConnectionFormat, 0);
2155 temp.listSourceFormats.push_back(tempConnectionFormat);
2158 if ((eCode = sqlite3_finalize(qSourceConnectionFormat)) != SQLITE_OK)
2160 logError("DatabaseHandler::getListGateways SQLITE Finalize error code:", eCode);
2162 return E_DATABASE_ERROR;
2165 //read out sound properties
2166 commandConnectionFormat = "SELECT soundFormat FROM GatewaySinkFormat" + i2s(temp.gatewayID);
2167 sqlite3_prepare_v2(mDatabase, commandConnectionFormat.c_str(), -1, &qSinkConnectionFormat, NULL);
2168 while ((eCode = sqlite3_step(qSinkConnectionFormat)) == SQLITE_ROW)
2170 tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qSinkConnectionFormat, 0);
2171 temp.listSinkFormats.push_back(tempConnectionFormat);
2174 if ((eCode = sqlite3_finalize(qSinkConnectionFormat)) != SQLITE_OK)
2176 logError("DatabaseHandler::getListGateways SQLITE Finalize error code:", eCode);
2178 return E_DATABASE_ERROR;
2181 listGateways.push_back(temp);
2182 temp.listSinkFormats.clear();
2183 temp.listSourceFormats.clear();
2186 if (eCode != SQLITE_DONE)
2188 logError("DatabaseHandler::getListGateways SQLITE error code:", eCode);
2190 return E_DATABASE_ERROR;
2193 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2195 logError("DatabaseHandler::getListGateways SQLITE Finalize error code:", eCode);
2197 return E_DATABASE_ERROR;
2203 am_Error_e DatabaseHandler::getListSinkClasses(std::vector<am_SinkClass_s> & listSinkClasses) const
2205 listSinkClasses.clear();
2207 sqlite3_stmt* query = NULL, *subQuery = NULL;
2209 am_SinkClass_s classTemp;
2210 am_ClassProperty_s propertyTemp;
2212 std::string command = "SELECT sinkClassID, name FROM " + std::string(SINK_CLASS_TABLE);
2213 std::string command2;
2214 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2216 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2218 classTemp.sinkClassID = sqlite3_column_int(query, 0);
2219 classTemp.name = std::string((const char*) sqlite3_column_text(query, 1));
2221 //read out Properties
2222 command2 = "SELECT classProperty, value FROM SinkClassProperties" + i2s(classTemp.sinkClassID);
2223 sqlite3_prepare_v2(mDatabase, command2.c_str(), -1, &subQuery, NULL);
2225 while ((eCode = sqlite3_step(subQuery)) == SQLITE_ROW)
2227 propertyTemp.classProperty = (am_ClassProperty_e) sqlite3_column_int(subQuery, 0);
2228 propertyTemp.value = sqlite3_column_int(subQuery, 1);
2229 classTemp.listClassProperties.push_back(propertyTemp);
2232 if (eCode != SQLITE_DONE)
2234 logError("DatabaseHandler::getListSourceClasses SQLITE error code:", eCode);
2236 return E_DATABASE_ERROR;
2239 if ((eCode = sqlite3_finalize(subQuery)) != SQLITE_OK)
2241 logError("DatabaseHandler::getListSourceClasses SQLITE Finalize error code:", eCode);
2243 return E_DATABASE_ERROR;
2245 listSinkClasses.push_back(classTemp);
2248 if (eCode != SQLITE_DONE)
2250 logError("DatabaseHandler::getListSourceClasses SQLITE error code:", eCode);
2252 return E_DATABASE_ERROR;
2255 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2257 logError("DatabaseHandler::getListSourceClasses SQLITE Finalize error code:", eCode);
2259 return E_DATABASE_ERROR;
2265 am_Error_e DatabaseHandler::getListVisibleMainConnections(std::vector<am_MainConnectionType_s> & listConnections) const
2267 listConnections.clear();
2268 sqlite3_stmt *query = NULL;
2270 am_MainConnectionType_s temp;
2272 std::string command = "SELECT mainConnectionID, sourceID, sinkID, connectionState, delay FROM " + std::string(MAINCONNECTION_TABLE);
2273 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2275 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2277 temp.mainConnectionID = sqlite3_column_int(query, 0);
2278 temp.sourceID = sqlite3_column_int(query, 1);
2279 temp.sinkID = sqlite3_column_int(query, 2);
2280 temp.connectionState = (am_ConnectionState_e) sqlite3_column_int(query, 3);
2281 temp.delay = sqlite3_column_int(query, 4);
2282 listConnections.push_back(temp);
2285 if (eCode != SQLITE_DONE)
2287 logError("DatabaseHandler::getListVisibleMainConnections SQLITE error code:", eCode);
2289 return E_DATABASE_ERROR;
2292 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2294 logError("DatabaseHandler::getListVisibleMainConnections SQLITE Finalize error code:", eCode);
2296 return E_DATABASE_ERROR;
2302 am_Error_e DatabaseHandler::getListMainSinks(std::vector<am_SinkType_s> & listMainSinks) const
2304 listMainSinks.clear();
2305 sqlite3_stmt* query = NULL;
2309 std::string command = "SELECT name, sinkID, availability, availabilityReason, muteState, mainVolume, sinkClassID FROM " + std::string(SINK_TABLE) + " WHERE visible=1 AND reserved=0";
2310 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2312 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2314 temp.name = std::string((const char*) sqlite3_column_text(query, 0));
2315 temp.sinkID = sqlite3_column_int(query, 1);
2316 temp.availability.availability = (am_Availablility_e) sqlite3_column_int(query, 2);
2317 temp.availability.availabilityReason = (am_AvailabilityReason_e) sqlite3_column_int(query, 3);
2318 temp.muteState = (am_MuteState_e) sqlite3_column_int(query, 4);
2319 temp.volume = sqlite3_column_int(query, 5);
2320 temp.sinkClassID = sqlite3_column_int(query, 6);
2321 listMainSinks.push_back(temp);
2324 if (eCode != SQLITE_DONE)
2326 logError("DatabaseHandler::getListSinks SQLITE error code:", eCode);
2328 return E_DATABASE_ERROR;
2331 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2333 logError("DatabaseHandler::getListSinks SQLITE Finalize error code:", eCode);
2335 return E_DATABASE_ERROR;
2341 am_Error_e DatabaseHandler::getListMainSources(std::vector<am_SourceType_s> & listMainSources) const
2343 listMainSources.clear();
2344 sqlite3_stmt* query = NULL;
2346 am_SourceType_s temp;
2347 std::string command = "SELECT name, sourceClassID, availability, availabilityReason, sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE visible=1";
2348 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2350 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2352 temp.name = std::string((const char*) sqlite3_column_text(query, 0));
2353 temp.sourceClassID = sqlite3_column_int(query, 1);
2354 temp.availability.availability = (am_Availablility_e) sqlite3_column_int(query, 2);
2355 temp.availability.availabilityReason = (am_AvailabilityReason_e) sqlite3_column_int(query, 3);
2356 temp.sourceID = sqlite3_column_int(query, 4);
2358 listMainSources.push_back(temp);
2361 if (eCode != SQLITE_DONE)
2363 logError("DatabaseHandler::getListSources SQLITE error code:", eCode);
2365 return E_DATABASE_ERROR;
2368 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2370 logError("DatabaseHandler::getListSources SQLITE Finalize error code:", eCode);
2372 return E_DATABASE_ERROR;
2378 am_Error_e DatabaseHandler::getListMainSinkSoundProperties(const am_sinkID_t sinkID, std::vector<am_MainSoundProperty_s> & listSoundProperties) const
2381 if (!existSink(sinkID))
2382 return E_DATABASE_ERROR; // todo: here we could change to non existen, but not shown in sequences
2383 listSoundProperties.clear();
2385 sqlite3_stmt* query = NULL;
2387 am_MainSoundProperty_s temp;
2388 std::string command = "SELECT soundPropertyType, value FROM SinkMainSoundProperty" + i2s(sinkID);
2389 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2391 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2393 temp.type = (am_MainSoundPropertyType_e) sqlite3_column_int(query, 0);
2394 temp.value = sqlite3_column_int(query, 1);
2395 listSoundProperties.push_back(temp);
2398 if (eCode != SQLITE_DONE)
2400 logError("DatabaseHandler::getListMainSinkSoundProperties SQLITE error code:", eCode);
2402 return E_DATABASE_ERROR;
2405 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2407 logError("DatabaseHandler::getListMainSinkSoundProperties SQLITE Finalize error code:", eCode);
2409 return E_DATABASE_ERROR;
2415 am_Error_e DatabaseHandler::getListMainSourceSoundProperties(const am_sourceID_t sourceID, std::vector<am_MainSoundProperty_s> & listSourceProperties) const
2417 assert(sourceID!=0);
2418 if (!existSource(sourceID))
2419 return E_DATABASE_ERROR; // todo: here we could change to non existen, but not shown in sequences
2420 listSourceProperties.clear();
2422 sqlite3_stmt* query = NULL;
2424 am_MainSoundProperty_s temp;
2425 std::string command = "SELECT soundPropertyType, value FROM SourceMainSoundProperty" + i2s(sourceID);
2426 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2428 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2430 temp.type = (am_MainSoundPropertyType_e) sqlite3_column_int(query, 0);
2431 temp.value = sqlite3_column_int(query, 1);
2432 listSourceProperties.push_back(temp);
2435 if (eCode != SQLITE_DONE)
2437 logError("DatabaseHandler::getListMainSinkSoundProperties SQLITE error code:", eCode);
2439 return E_DATABASE_ERROR;
2442 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2444 logError("DatabaseHandler::getListMainSinkSoundProperties SQLITE Finalize error code:", eCode);
2446 return E_DATABASE_ERROR;
2452 am_Error_e DatabaseHandler::getListSystemProperties(std::vector<am_SystemProperty_s> & listSystemProperties) const
2454 listSystemProperties.clear();
2456 sqlite3_stmt* query = NULL;
2458 am_SystemProperty_s temp;
2459 std::string command = "SELECT type, value FROM " + std::string(SYSTEM_TABLE);
2460 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2462 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2464 temp.type = (am_SystemPropertyType_e) sqlite3_column_int(query, 0);
2465 temp.value = sqlite3_column_int(query, 1);
2466 listSystemProperties.push_back(temp);
2469 if (eCode != SQLITE_DONE)
2471 logError("DatabaseHandler::getListSystemProperties SQLITE error code:", eCode);
2473 return E_DATABASE_ERROR;
2476 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2478 logError("DatabaseHandler::getListSystemProperties SQLITE Finalize error code:", eCode);
2480 return E_DATABASE_ERROR;
2486 am_Error_e am::DatabaseHandler::getListSinkConnectionFormats(const am_sinkID_t sinkID, std::vector<am_ConnectionFormat_e> & listConnectionFormats) const
2488 listConnectionFormats.clear();
2489 sqlite3_stmt *qConnectionFormat = NULL;
2491 am_ConnectionFormat_e tempConnectionFormat;
2492 std::string commandConnectionFormat = "SELECT soundFormat FROM SinkConnectionFormat" + i2s(sinkID);
2493 sqlite3_prepare_v2(mDatabase, commandConnectionFormat.c_str(), -1, &qConnectionFormat, NULL);
2494 while ((eCode = sqlite3_step(qConnectionFormat)) == SQLITE_ROW)
2496 tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qConnectionFormat, 0);
2497 listConnectionFormats.push_back(tempConnectionFormat);
2500 if ((eCode = sqlite3_finalize(qConnectionFormat)) != SQLITE_OK)
2502 logError("DatabaseHandler::getListSinkConnectionFormats SQLITE Finalize error code:", eCode);
2504 return E_DATABASE_ERROR;
2510 am_Error_e am::DatabaseHandler::getListSourceConnectionFormats(const am_sourceID_t sourceID, std::vector<am_ConnectionFormat_e> & listConnectionFormats) const
2512 listConnectionFormats.clear();
2513 sqlite3_stmt* qConnectionFormat = NULL;
2515 am_ConnectionFormat_e tempConnectionFormat;
2517 //read out the connectionFormats
2518 std::string commandConnectionFormat = "SELECT soundFormat FROM SourceConnectionFormat" + i2s(sourceID);
2519 sqlite3_prepare_v2(mDatabase, commandConnectionFormat.c_str(), -1, &qConnectionFormat, NULL);
2520 while ((eCode = sqlite3_step(qConnectionFormat)) == SQLITE_ROW)
2522 tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qConnectionFormat, 0);
2523 listConnectionFormats.push_back(tempConnectionFormat);
2526 if ((eCode = sqlite3_finalize(qConnectionFormat)) != SQLITE_OK)
2528 logError("DatabaseHandler::getListSources SQLITE Finalize error code:", eCode);
2530 return E_DATABASE_ERROR;
2536 am_Error_e am::DatabaseHandler::getListGatewayConnectionFormats(const am_gatewayID_t gatewayID, std::vector<bool> & listConnectionFormat) const
2538 ListConnectionFormat::const_iterator iter = mListConnectionFormat.begin();
2539 iter = mListConnectionFormat.find(gatewayID);
2540 if (iter == mListConnectionFormat.end())
2542 logError("DatabaseHandler::getListGatewayConnectionFormats database error with convertionFormat");
2544 return (E_DATABASE_ERROR);
2546 listConnectionFormat = iter->second;
2551 am_Error_e DatabaseHandler::getTimingInformation(const am_mainConnectionID_t mainConnectionID, am_timeSync_t & delay) const
2553 assert(mainConnectionID!=0);
2555 sqlite3_stmt *query = NULL;
2558 std::string command = "SELECT delay FROM " + std::string(MAINCONNECTION_TABLE) + " WHERE mainConnectionID=" + i2s(mainConnectionID);
2559 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2561 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2563 delay = sqlite3_column_int(query, 0);
2566 if (eCode != SQLITE_DONE)
2568 logError("DatabaseHandler::getTimingInformation SQLITE error code:", eCode);
2570 return E_DATABASE_ERROR;
2573 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2575 logError("DatabaseHandler::getTimingInformation SQLITE Finalize error code:", eCode);
2577 return E_DATABASE_ERROR;
2581 return E_NOT_POSSIBLE;
2586 bool DatabaseHandler::sqQuery(const std::string& query)
2588 sqlite3_stmt* statement;
2590 if ((eCode = sqlite3_exec(mDatabase, query.c_str(), NULL, &statement, NULL)) != SQLITE_OK)
2592 logError("DatabaseHandler::sqQuery SQL Query failed:", query.c_str(), "error code:", eCode);
2598 bool DatabaseHandler::openDatabase()
2600 if (sqlite3_open_v2(mPath.c_str(), &mDatabase, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL) == SQLITE_OK)
2602 logInfo("DatabaseHandler::openDatabase opened database");
2605 logError("DatabaseHandler::openDatabase failed to open database");
2609 am_Error_e DatabaseHandler::changeDelayMainConnection(const am_timeSync_t & delay, const am_mainConnectionID_t & connectionID)
2611 assert(connectionID!=0);
2613 sqlite3_stmt* query = NULL;
2615 std::string command = "SELECT mainConnectionID FROM " + std::string(MAINCONNECTION_TABLE) + " WHERE delay=? AND mainConnectionID=?";
2616 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2617 sqlite3_bind_int(query, 1, delay);
2618 sqlite3_bind_int(query, 2, connectionID);
2619 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
2621 sqlite3_finalize(query);
2624 command = "UPDATE " + std::string(MAINCONNECTION_TABLE) + " SET delay=? WHERE mainConnectionID=?;";
2625 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2626 sqlite3_bind_int(query, 1, delay);
2627 sqlite3_bind_int(query, 2, connectionID);
2629 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
2631 logError("DatabaseHandler::changeDelayMainConnection SQLITE Step error code:", eCode);
2633 return E_DATABASE_ERROR;
2636 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2638 logError("DatabaseHandler::changeDelayMainConnection SQLITE Finalize error code:", eCode);
2640 return E_DATABASE_ERROR;
2643 if (mDatabaseObserver)
2644 mDatabaseObserver->timingInformationChanged(connectionID, delay);
2649 am_Error_e DatabaseHandler::enterConnectionDB(const am_Connection_s& connection, am_connectionID_t& connectionID)
2651 assert(connection.connectionID==0);
2652 assert(connection.sinkID!=0);
2653 assert(connection.sourceID!=0);
2654 //connection format is not checked, because it's project specific
2656 sqlite3_stmt* query = NULL;
2658 std::string command = "INSERT INTO " + std::string(CONNECTION_TABLE) + "(sinkID, sourceID, delay, connectionFormat, reserved) VALUES (?,?,?,?,?)";
2660 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2661 sqlite3_bind_int(query, 1, connection.sinkID);
2662 sqlite3_bind_int(query, 2, connection.sourceID);
2663 sqlite3_bind_int(query, 3, connection.delay);
2664 sqlite3_bind_int(query, 4, connection.connectionFormat);
2665 sqlite3_bind_int(query, 5, true);
2667 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
2669 logError("DatabaseHandler::enterConnectionDB SQLITE Step error code:", eCode);
2671 return E_DATABASE_ERROR;
2674 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2676 logError("DatabaseHandler::enterConnectionDB SQLITE Finalize error code:", eCode);
2678 return E_DATABASE_ERROR;
2681 connectionID = sqlite3_last_insert_rowid(mDatabase);
2683 logInfo("DatabaseHandler::enterConnectionDB entered new connection sourceID=", connection.sourceID, "sinkID=", connection.sinkID, "sourceID=", connection.sourceID, "connectionFormat=", connection.connectionFormat, "assigned ID=", connectionID);
2687 am_Error_e DatabaseHandler::enterSinkClassDB(const am_SinkClass_s & sinkClass, am_sinkClass_t & sinkClassID)
2689 assert(sinkClass.sinkClassID<DYNAMIC_ID_BOUNDARY);
2690 assert(!sinkClass.listClassProperties.empty());
2691 assert(!sinkClass.name.empty());
2693 sqlite3_stmt* query = NULL;
2695 std::string command;
2697 //if sinkID is zero and the first Static Sink was already entered, the ID is created
2698 if (sinkClass.sinkClassID == 0 && !mFirstStaticSinkClass)
2700 command = "INSERT INTO " + std::string(SINK_CLASS_TABLE) + "(name) VALUES (?)";
2704 //check if the ID already exists
2705 if (existSinkClass(sinkClass.sinkClassID))
2706 return E_ALREADY_EXISTS;
2707 command = "INSERT INTO " + std::string(SINK_CLASS_TABLE) + "(name, sinkClassID) VALUES (?,?)";
2710 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2711 sqlite3_bind_text(query, 1, sinkClass.name.c_str(), sinkClass.name.size(), SQLITE_STATIC);
2713 //if the ID is not created, we add it to the query
2714 if (sinkClass.sinkClassID != 0)
2716 sqlite3_bind_int(query, 2, sinkClass.sinkClassID);
2719 //if the first static sink is entered, we need to set it onto the boundary
2720 else if (mFirstStaticSinkClass)
2722 sqlite3_bind_int(query, 2, DYNAMIC_ID_BOUNDARY);
2723 mFirstStaticSinkClass = false;
2726 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
2728 logError("DatabaseHandler::enterSinkClassDB SQLITE Step error code:", eCode);
2730 return E_DATABASE_ERROR;
2733 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2735 logError("DatabaseHandler::enterSinkClassDB SQLITE Finalize error code:", eCode);
2737 return E_DATABASE_ERROR;
2740 sinkClassID = sqlite3_last_insert_rowid(mDatabase); //todo:change last_insert implementations for mulithread usage...
2742 //now we need to create the additional tables:
2743 command = "CREATE TABLE SinkClassProperties" + i2s(sinkClassID) + std::string("(classProperty INTEGER, value INTEGER)");
2744 assert(this->sqQuery(command));
2746 //fill ConnectionFormats
2747 command = "INSERT INTO SinkClassProperties" + i2s(sinkClassID) + std::string("(classProperty,value) VALUES (?,?)");
2748 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2749 std::vector<am_ClassProperty_s>::const_iterator Iterator = sinkClass.listClassProperties.begin();
2750 for (; Iterator < sinkClass.listClassProperties.end(); ++Iterator)
2752 sqlite3_bind_int(query, 1, Iterator->classProperty);
2753 sqlite3_bind_int(query, 2, Iterator->value);
2754 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
2756 logError("DatabaseHandler::enterSinkClassDB SQLITE Step error code:", eCode);
2758 return E_DATABASE_ERROR;
2760 sqlite3_reset(query);
2763 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2765 logError("DatabaseHandler::enterSinkClassDB SQLITE Finalize error code:", eCode);
2767 return E_DATABASE_ERROR;
2770 logInfo("DatabaseHandler::enterSinkClassDB entered new sinkClass");
2771 if (mDatabaseObserver)
2772 mDatabaseObserver->numberOfSinkClassesChanged();
2776 am_Error_e DatabaseHandler::enterSourceClassDB(am_sourceClass_t & sourceClassID, const am_SourceClass_s & sourceClass)
2778 assert(sourceClass.sourceClassID<DYNAMIC_ID_BOUNDARY);
2779 assert(!sourceClass.listClassProperties.empty());
2780 assert(!sourceClass.name.empty());
2782 sqlite3_stmt* query = NULL;
2784 std::string command;
2786 //if sinkID is zero and the first Static Sink was already entered, the ID is created
2787 if (sourceClass.sourceClassID == 0 && !mFirstStaticSourceClass)
2789 command = "INSERT INTO " + std::string(SOURCE_CLASS_TABLE) + "(name) VALUES (?)";
2793 //check if the ID already exists
2794 if (existSourceClass(sourceClass.sourceClassID))
2795 return E_ALREADY_EXISTS;
2796 command = "INSERT INTO " + std::string(SOURCE_CLASS_TABLE) + "(name, sourceClassID) VALUES (?,?)";
2799 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2800 sqlite3_bind_text(query, 1, sourceClass.name.c_str(), sourceClass.name.size(), SQLITE_STATIC);
2802 //if the ID is not created, we add it to the query
2803 if (sourceClass.sourceClassID != 0)
2805 sqlite3_bind_int(query, 2, sourceClass.sourceClassID);
2808 //if the first static sink is entered, we need to set it onto the boundary
2809 else if (mFirstStaticSourceClass)
2811 sqlite3_bind_int(query, 2, DYNAMIC_ID_BOUNDARY);
2812 mFirstStaticSourceClass = false;
2815 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
2817 logError("DatabaseHandler::enterSourceClassDB SQLITE Step error code:", eCode);
2819 return E_DATABASE_ERROR;
2822 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2824 logError("DatabaseHandler::enterSourceClassDB SQLITE Finalize error code:", eCode);
2826 return E_DATABASE_ERROR;
2829 sourceClassID = sqlite3_last_insert_rowid(mDatabase); //todo:change last_insert implementations for mulithread usage...
2831 //now we need to create the additional tables:
2832 command = "CREATE TABLE SourceClassProperties" + i2s(sourceClassID) + std::string("(classProperty INTEGER, value INTEGER)");
2833 assert(sqQuery(command));
2835 //fill ConnectionFormats
2836 command = "INSERT INTO SourceClassProperties" + i2s(sourceClassID) + std::string("(classProperty,value) VALUES (?,?)");
2837 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2838 std::vector<am_ClassProperty_s>::const_iterator Iterator = sourceClass.listClassProperties.begin();
2839 for (; Iterator < sourceClass.listClassProperties.end(); ++Iterator)
2841 sqlite3_bind_int(query, 1, Iterator->classProperty);
2842 sqlite3_bind_int(query, 2, Iterator->value);
2843 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
2845 logError("DatabaseHandler::enterSourceClassDB SQLITE Step error code:", eCode);
2847 return E_DATABASE_ERROR;
2849 sqlite3_reset(query);
2852 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2854 logError("DatabaseHandler::enterSourceClassDB SQLITE Finalize error code:", eCode);
2856 return E_DATABASE_ERROR;
2859 logInfo("DatabaseHandler::enterSourceClassDB entered new sourceClass");
2861 if (mDatabaseObserver)
2862 mDatabaseObserver->numberOfSourceClassesChanged();
2866 am_Error_e DatabaseHandler::enterSystemProperties(const std::vector<am_SystemProperty_s> & listSystemProperties)
2868 sqlite3_stmt* query = NULL;
2870 std::vector<am_SystemProperty_s>::const_iterator listIterator = listSystemProperties.begin();
2871 std::string command = "DELETE * FROM " + std::string(SYSTEM_TABLE);
2874 command = "INSERT INTO " + std::string(SYSTEM_TABLE) + " (type, value) VALUES (?,?)";
2876 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2877 for (; listIterator < listSystemProperties.end(); ++listIterator)
2879 sqlite3_bind_int(query, 1, listIterator->type);
2880 sqlite3_bind_int(query, 2, listIterator->value);
2882 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
2884 logError("DatabaseHandler::enterSystemProperties SQLITE Step error code:", eCode);
2886 return E_DATABASE_ERROR;
2889 sqlite3_reset(query);
2892 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2894 logError("DatabaseHandler::enterSystemProperties SQLITE Finalize error code:", eCode);
2896 return E_DATABASE_ERROR;
2899 logInfo("DatabaseHandler::enterSystemProperties entered system properties");
2903 bool DatabaseHandler::existMainConnection(const am_mainConnectionID_t mainConnectionID) const
2905 sqlite3_stmt* query = NULL;
2906 std::string command = "SELECT mainConnectionID FROM " + std::string(MAINCONNECTION_TABLE) + " WHERE mainConnectionID=" + i2s(mainConnectionID);
2908 bool returnVal = true;
2909 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2910 if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
2912 else if (eCode != SQLITE_ROW)
2915 logError("DatabaseHandler::existMainConnection database error!:", eCode);
2917 sqlite3_finalize(query);
2921 bool DatabaseHandler::existSource(const am_sourceID_t sourceID) const
2923 sqlite3_stmt* query = NULL;
2924 std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE reserved=0 AND sourceID=" + i2s(sourceID);
2926 bool returnVal = true;
2927 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2928 if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
2930 else if (eCode != SQLITE_ROW)
2933 logError("DatabaseHandler::existSource database error!:", eCode);
2935 sqlite3_finalize(query);
2939 bool DatabaseHandler::existSourceNameOrID(const am_sourceID_t sourceID, const std::string & name) const
2941 sqlite3_stmt* query = NULL;
2942 std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE reserved=0 AND (name=? OR sourceID=?)";
2944 bool returnVal = true;
2945 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2946 sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC);
2947 sqlite3_bind_int(query, 2, sourceID);
2948 if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
2950 else if (eCode != SQLITE_ROW)
2953 logError("DatabaseHandler::existSource database error!:", eCode);
2955 sqlite3_finalize(query);
2959 bool DatabaseHandler::existSourceName(const std::string & name) const
2961 sqlite3_stmt* query = NULL;
2962 std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE reserved=0 AND name=?";
2964 bool returnVal = true;
2965 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2966 sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC);
2967 if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
2969 else if (eCode != SQLITE_ROW)
2972 logError("DatabaseHandler::existSource database error!:", eCode);
2974 sqlite3_finalize(query);
2978 bool DatabaseHandler::existSink(const am_sinkID_t sinkID) const
2980 sqlite3_stmt* query = NULL;
2981 std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 AND sinkID=" + i2s(sinkID);
2983 bool returnVal = true;
2984 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2985 if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
2987 else if (eCode != SQLITE_ROW)
2990 logError("DatabaseHandler::existSink database error!:", eCode);
2992 sqlite3_finalize(query);
2996 bool DatabaseHandler::existSinkNameOrID(const am_sinkID_t sinkID, const std::string & name) const
2998 sqlite3_stmt* query = NULL;
2999 std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 AND (name=? OR sinkID=?)";
3001 bool returnVal = true;
3002 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3003 sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC);
3004 sqlite3_bind_int(query, 2, sinkID);
3005 if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3007 else if (eCode != SQLITE_ROW)
3010 logError("DatabaseHandler::existSink database error!:", eCode);
3012 sqlite3_finalize(query);
3016 bool DatabaseHandler::existSinkName(const std::string & name) const
3018 sqlite3_stmt* query = NULL;
3019 std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 AND name=?";
3021 bool returnVal = true;
3022 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3023 sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC);
3024 if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3026 else if (eCode != SQLITE_ROW)
3029 logError("DatabaseHandler::existSink database error!:", eCode);
3031 sqlite3_finalize(query);
3035 bool DatabaseHandler::existDomain(const am_domainID_t domainID) const
3037 sqlite3_stmt* query = NULL;
3038 std::string command = "SELECT domainID FROM " + std::string(DOMAIN_TABLE) + " WHERE reserved=0 AND domainID=" + i2s(domainID);
3040 bool returnVal = true;
3041 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3042 if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3044 else if (eCode != SQLITE_ROW)
3047 logError("DatabaseHandler::existDomain database error!:", eCode);
3049 sqlite3_finalize(query);
3053 bool DatabaseHandler::existGateway(const am_gatewayID_t gatewayID) const
3055 sqlite3_stmt* query = NULL;
3056 std::string command = "SELECT gatewayID FROM " + std::string(GATEWAY_TABLE) + " WHERE gatewayID=" + i2s(gatewayID);
3058 bool returnVal = true;
3059 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3060 if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3062 else if (eCode != SQLITE_ROW)
3065 logError("DatabaseHandler::existGateway database error!:", eCode);
3067 sqlite3_finalize(query);
3071 am_Error_e DatabaseHandler::getDomainOfSource(const am_sourceID_t sourceID, am_domainID_t & domainID) const
3073 assert(sourceID!=0);
3075 sqlite3_stmt* query = NULL;
3076 std::string command = "SELECT domainID FROM " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + i2s(sourceID);
3078 am_Error_e returnVal = E_DATABASE_ERROR;
3079 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3080 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3082 domainID = sqlite3_column_int(query, 0);
3087 logError("DatabaseHandler::getDomainOfSource database error!:", eCode);
3089 sqlite3_finalize(query);
3093 am_Error_e am::DatabaseHandler::getDomainOfSink(const am_sinkID_t sinkID, am_domainID_t & domainID) const
3097 sqlite3_stmt* query = NULL;
3098 std::string command = "SELECT domainID FROM " + std::string(SINK_TABLE) + " WHERE sinkID=" + i2s(sinkID);
3100 am_Error_e returnVal = E_DATABASE_ERROR;
3101 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3102 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3104 domainID = sqlite3_column_int(query, 0);
3109 logError("DatabaseHandler::getDomainOfSink database error!:", eCode);
3111 sqlite3_finalize(query);
3115 bool DatabaseHandler::existSinkClass(const am_sinkClass_t sinkClassID) const
3117 sqlite3_stmt* query = NULL;
3118 std::string command = "SELECT sinkClassID FROM " + std::string(SINK_CLASS_TABLE) + " WHERE sinkClassID=" + i2s(sinkClassID);
3120 bool returnVal = true;
3121 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3122 if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3124 else if (eCode != SQLITE_ROW)
3127 logError("DatabaseHandler::existSinkClass database error!:", eCode);
3129 sqlite3_finalize(query);
3133 bool DatabaseHandler::existSourceClass(const am_sourceClass_t sourceClassID) const
3135 sqlite3_stmt* query = NULL;
3136 std::string command = "SELECT sourceClassID FROM " + std::string(SOURCE_CLASS_TABLE) + " WHERE sourceClassID=" + i2s(sourceClassID);
3138 bool returnVal = true;
3139 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3140 if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3142 else if (eCode != SQLITE_ROW)
3145 logError("DatabaseHandler::existSinkClass database error!:", eCode);
3147 sqlite3_finalize(query);
3151 am_Error_e DatabaseHandler::changeConnectionTimingInformation(const am_connectionID_t connectionID, const am_timeSync_t delay)
3153 assert(connectionID!=0);
3155 sqlite3_stmt *query = NULL, *queryMainConnections, *queryMainConnectionSubIDs;
3156 int eCode = 0, eCode1 = 0;
3157 std::string command = "UPDATE " + std::string(CONNECTION_TABLE) + " set delay=? WHERE connectionID=?";
3159 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3160 sqlite3_bind_int(query, 1, delay);
3161 sqlite3_bind_int(query, 2, connectionID);
3163 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3165 logError("DatabaseHandler::changeConnectionTimingInformation SQLITE Step error code:", eCode);
3167 return E_DATABASE_ERROR;
3170 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3172 logError("DatabaseHandler::changeConnectionTimingInformation SQLITE Finalize error code:", eCode);
3174 return E_DATABASE_ERROR;
3177 //now we need to find all mainConnections that use the changed connection and update their timing
3179 int tempMainConnectionID;
3180 //first get all route tables for all mainconnections
3181 command = "SELECT name FROM sqlite_master WHERE type ='table' and name LIKE 'MainConnectionRoute%'";
3182 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &queryMainConnections, NULL);
3184 while ((eCode = sqlite3_step(queryMainConnections)) == SQLITE_ROW)
3186 //now check if the connection ID is in this table
3187 std::string tablename = std::string((const char*) sqlite3_column_text(queryMainConnections, 0));
3188 std::string command2 = "SELECT connectionID FROM " + tablename + " WHERE connectionID=" + i2s(connectionID);
3189 sqlite3_prepare_v2(mDatabase, command2.c_str(), -1, &queryMainConnectionSubIDs, NULL);
3190 if ((eCode1 = sqlite3_step(queryMainConnectionSubIDs)) == SQLITE_ROW)
3192 //if the connection ID is in, recalculate the mainconnection delay
3193 std::stringstream(tablename.substr(tablename.find_first_not_of("MainConnectionRoute"))) >> tempMainConnectionID;
3194 changeDelayMainConnection(calculateMainConnectionDelay(tempMainConnectionID), tempMainConnectionID);
3196 else if (eCode1 != SQLITE_DONE)
3198 logError("DatabaseHandler::changeConnectionTimingInformation SQLITE error code:", eCode1);
3200 return E_DATABASE_ERROR;
3204 if (eCode != SQLITE_DONE)
3206 logError("DatabaseHandler::changeConnectionTimingInformation SQLITE error code:", eCode);
3208 return E_DATABASE_ERROR;
3211 if ((eCode = sqlite3_finalize(queryMainConnections)) != SQLITE_OK)
3213 logError("DatabaseHandler::changeConnectionTimingInformation SQLITE Finalize error code:", eCode);
3215 return E_DATABASE_ERROR;
3221 am_Error_e DatabaseHandler::changeConnectionFinal(const am_connectionID_t connectionID)
3223 assert(connectionID!=0);
3225 sqlite3_stmt *query = NULL;
3227 std::string command = "UPDATE " + std::string(CONNECTION_TABLE) + " set reserved=0 WHERE connectionID=?";
3229 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3230 sqlite3_bind_int(query, 1, connectionID);
3232 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3234 logError("DatabaseHandler::changeConnectionFinal SQLITE Step error code:", eCode);
3236 return E_DATABASE_ERROR;
3239 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3241 logError("DatabaseHandler::changeConnectionFinal SQLITE Finalize error code:", eCode);
3243 return E_DATABASE_ERROR;
3248 am_timeSync_t DatabaseHandler::calculateMainConnectionDelay(const am_mainConnectionID_t mainConnectionID) const
3250 assert(mainConnectionID!=0);
3251 sqlite3_stmt* query = NULL;
3252 std::string command = "SELECT sum(Connections.delay),min(Connections.delay) FROM " + std::string(CONNECTION_TABLE) + ",MainConnectionRoute" + i2s(mainConnectionID) + " WHERE MainConnectionRoute" + i2s(mainConnectionID) + ".connectionID = Connections.connectionID";
3254 am_timeSync_t delay = 0;
3255 am_timeSync_t min = 0;
3256 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3257 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3259 delay = sqlite3_column_int(query, 0);
3260 min = sqlite3_column_int(query, 1);
3262 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3264 logError("DatabaseHandler::calculateMainConnectionDelay SQLITE Step error code:", eCode);
3266 return E_DATABASE_ERROR;
3269 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3271 logError("DatabaseHandler::calculateMainConnectionDelay SQLITE Finalize error code:", eCode);
3273 return E_DATABASE_ERROR;
3281 void DatabaseHandler::registerObserver(DatabaseObserver *iObserver)
3283 assert(iObserver!=NULL);
3284 mDatabaseObserver = iObserver;
3287 bool DatabaseHandler::sourceVisible(const am_sourceID_t sourceID) const
3289 assert(sourceID!=0);
3290 sqlite3_stmt* query = NULL;
3291 std::string command = "SELECT visible FROM " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + i2s(sourceID);
3293 bool returnVal = false;
3294 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3295 if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3297 returnVal = (bool) sqlite3_column_int(query, 0);
3299 else if (eCode != SQLITE_ROW)
3302 logError("DatabaseHandler::sourceVisible database error!:", eCode);
3304 sqlite3_finalize(query);
3308 bool DatabaseHandler::sinkVisible(const am_sinkID_t sinkID) const
3310 sqlite3_stmt* query = NULL;
3311 std::string command = "SELECT visible FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 AND sinkID=" + i2s(sinkID);
3313 bool returnVal = false;
3314 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3315 if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3317 returnVal = sqlite3_column_int(query, 0);
3319 else if (eCode != SQLITE_ROW)
3322 logError("DatabaseHandler::sinkVisible database error!:", eCode);
3324 sqlite3_finalize(query);
3328 bool DatabaseHandler::existConnection(const am_Connection_s connection)
3330 sqlite3_stmt* query = NULL;
3331 std::string command = "SELECT connectionID FROM " + std::string(CONNECTION_TABLE) + " WHERE sinkID=? AND sourceID=? AND connectionFormat=? AND reserved=0";
3333 bool returnVal = true;
3334 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3335 sqlite3_bind_int(query, 1, connection.sinkID);
3336 sqlite3_bind_int(query, 2, connection.sourceID);
3337 sqlite3_bind_int(query, 3, connection.connectionFormat);
3338 if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3340 else if (eCode != SQLITE_ROW)
3343 logError("DatabaseHandler::existMainConnection database error!:", eCode);
3345 sqlite3_finalize(query);
3349 bool DatabaseHandler::existConnectionID(const am_connectionID_t connectionID)
3351 sqlite3_stmt* query = NULL;
3352 std::string command = "SELECT connectionID FROM " + std::string(CONNECTION_TABLE) + " WHERE connectionID=? AND reserved=0";
3354 bool returnVal = true;
3355 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3356 sqlite3_bind_int(query, 1, connectionID);
3357 if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3359 else if (eCode != SQLITE_ROW)
3362 logError("DatabaseHandler::existMainConnection database error!:", eCode);
3364 sqlite3_finalize(query);
3368 bool DatabaseHandler::existcrossFader(const am_crossfaderID_t crossfaderID) const
3370 sqlite3_stmt* query = NULL;
3371 std::string command = "SELECT crossfaderID FROM " + std::string(CROSSFADER_TABLE) + " WHERE crossfaderID=?";
3373 bool returnVal = true;
3374 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3375 sqlite3_bind_int(query, 1, crossfaderID);
3376 if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3378 else if (eCode != SQLITE_ROW)
3381 logError("DatabaseHandler::existMainConnection database error!:", eCode);
3383 sqlite3_finalize(query);
3387 am_Error_e DatabaseHandler::getSoureState(const am_sourceID_t sourceID, am_SourceState_e & sourceState) const
3389 assert(sourceID!=0);
3390 sqlite3_stmt* query = NULL;
3391 sourceState = SS_MIN;
3392 std::string command = "SELECT sourceState FROM " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + i2s(sourceID);
3394 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3395 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3397 sourceState = (am_SourceState_e) sqlite3_column_int(query, 0);
3399 else if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3401 logError("DatabaseHandler::getSoureState database error!:", eCode);
3403 sqlite3_finalize(query);
3407 am_Error_e DatabaseHandler::changeSourceState(const am_sourceID_t sourceID, const am_SourceState_e sourceState)
3409 assert(sourceID!=0);
3410 sqlite3_stmt* query = NULL;
3411 std::string command = "UPDATE " + std::string(SOURCE_TABLE) + " SET sourceState=? WHERE sourceID=" + i2s(sourceID);
3413 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3414 sqlite3_bind_int(query, 1, sourceState);
3415 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3417 logError("DatabaseHandler::changeSourceState SQLITE Step error code:", eCode);
3419 return E_DATABASE_ERROR;
3422 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3424 logError("DatabaseHandler::changeSourceState SQLITE Finalize error code:", eCode);
3426 return E_DATABASE_ERROR;
3431 am_Error_e DatabaseHandler::getSinkVolume(const am_sinkID_t sinkID, am_volume_t & volume) const
3434 sqlite3_stmt* query = NULL;
3436 std::string command = "SELECT volume FROM " + std::string(SINK_TABLE) + " WHERE sinkID=" + i2s(sinkID);
3438 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3439 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3441 volume = sqlite3_column_int(query, 0);
3443 else if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3445 logError("DatabaseHandler::getSinkVolume database error!:", eCode);
3447 sqlite3_finalize(query);
3451 am_Error_e DatabaseHandler::getSourceVolume(const am_sourceID_t sourceID, am_volume_t & volume) const
3453 assert(sourceID!=0);
3454 sqlite3_stmt* query = NULL;
3456 std::string command = "SELECT volume FROM " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + i2s(sourceID);
3458 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3459 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3461 volume = sqlite3_column_int(query, 0);
3463 else if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3465 logError("DatabaseHandler::getSourceVolume database error!:", eCode);
3467 sqlite3_finalize(query);
3471 am_Error_e DatabaseHandler::getSinkSoundPropertyValue(const am_sinkID_t sinkID, const am_SoundPropertyType_e propertyType, uint16_t & value) const
3474 if (!existSink(sinkID))
3475 return E_DATABASE_ERROR; // todo: here we could change to non existent, but not shown in sequences
3477 sqlite3_stmt* query = NULL;
3479 std::string command = "SELECT value FROM SinkSoundProperty" + i2s(sinkID) + " WHERE soundPropertyType=" + i2s(propertyType);
3480 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3482 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3484 value = sqlite3_column_int(query, 0);
3487 if (eCode != SQLITE_DONE)
3489 logError("DatabaseHandler::getSinkSoundPropertyValue SQLITE error code:", eCode);
3491 return E_DATABASE_ERROR;
3494 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3496 logError("DatabaseHandler::getSinkSoundPropertyValue SQLITE Finalize error code:", eCode);
3498 return E_DATABASE_ERROR;
3504 am_Error_e DatabaseHandler::getSourceSoundPropertyValue(const am_sourceID_t sourceID, const am_SoundPropertyType_e propertyType, uint16_t & value) const
3506 assert(sourceID!=0);
3507 if (!existSource(sourceID))
3508 return E_DATABASE_ERROR; // todo: here we could change to non existent, but not shown in sequences
3510 sqlite3_stmt* query = NULL;
3512 std::string command = "SELECT value FROM SourceSoundProperty" + i2s(sourceID) + " WHERE soundPropertyType=" + i2s(propertyType);
3513 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3515 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3517 value = sqlite3_column_int(query, 0);
3520 if (eCode != SQLITE_DONE)
3522 logError("DatabaseHandler::getSinkSoundPropertyValue SQLITE error code:", eCode);
3524 return E_DATABASE_ERROR;
3527 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3529 logError("DatabaseHandler::getSinkSoundPropertyValue SQLITE Finalize error code:", eCode);
3531 return E_DATABASE_ERROR;
3537 am_Error_e DatabaseHandler::getDomainState(const am_domainID_t domainID, am_DomainState_e state) const
3539 assert(domainID!=0);
3540 sqlite3_stmt* query = NULL;
3542 std::string command = "SELECT domainState FROM " + std::string(DOMAIN_TABLE) + " WHERE domainID=" + i2s(domainID);
3544 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3545 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3547 state = (am_DomainState_e) sqlite3_column_int(query, 0);
3549 else if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3551 logError("DatabaseHandler::getDomainState database error!:", eCode);
3553 sqlite3_finalize(query);
3558 am_Error_e DatabaseHandler::peekDomain(const std::string & name, am_domainID_t & domainID)
3560 sqlite3_stmt* query = NULL, *queryInsert = NULL;
3561 std::string command = "SELECT domainID FROM " + std::string(DOMAIN_TABLE) + " WHERE name=?";
3562 int eCode = 0, eCode1 = 0;
3563 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3564 sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC);
3565 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3567 domainID = sqlite3_column_int(query, 0);
3569 else if (eCode != SQLITE_DONE)
3571 logError("DatabaseHandler::peekDomain database error!:", eCode);
3572 return E_DATABASE_ERROR;
3576 command = "INSERT INTO " + std::string(DOMAIN_TABLE) + " (name,reserved) VALUES (?,?)";
3577 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &queryInsert, NULL);
3578 sqlite3_bind_text(queryInsert, 1, name.c_str(), name.size(), SQLITE_STATIC);
3579 sqlite3_bind_int(queryInsert, 2, 1); //reservation flag
3580 if ((eCode1 = sqlite3_step(queryInsert)) != SQLITE_DONE)
3582 logError("DatabaseHandler::peekDomain SQLITE Step error code:", eCode1);
3583 return E_DATABASE_ERROR;
3586 if ((eCode1 = sqlite3_finalize(queryInsert)) != SQLITE_OK)
3588 logError("DatabaseHandler::peekDomain SQLITE Finalize error code:", eCode1);
3589 return E_DATABASE_ERROR;
3591 domainID = sqlite3_last_insert_rowid(mDatabase);
3593 sqlite3_finalize(query);
3597 am_Error_e DatabaseHandler::peekSink(const std::string & name, am_sinkID_t & sinkID)
3599 sqlite3_stmt* query = NULL, *queryInsert = NULL;
3600 std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE name=?";
3601 int eCode = 0, eCode1 = 0;
3602 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3603 sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC);
3604 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3606 sinkID = sqlite3_column_int(query, 0);
3608 else if (eCode != SQLITE_DONE)
3610 logError("DatabaseHandler::peekSink database error!:", eCode);
3611 return E_DATABASE_ERROR;
3615 if (mFirstStaticSink)
3617 command = "INSERT INTO " + std::string(SINK_TABLE) + " (name,reserved,sinkID) VALUES (?,?," + i2s(DYNAMIC_ID_BOUNDARY) + ")";
3618 mFirstStaticSink = false;
3622 command = "INSERT INTO " + std::string(SINK_TABLE) + " (name,reserved) VALUES (?,?)";
3624 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &queryInsert, NULL);
3625 sqlite3_bind_text(queryInsert, 1, name.c_str(), name.size(), SQLITE_STATIC);
3626 sqlite3_bind_int(queryInsert, 2, 1); //reservation flag
3627 if ((eCode1 = sqlite3_step(queryInsert)) != SQLITE_DONE)
3629 logError("DatabaseHandler::peekSink SQLITE Step error code:", eCode1);
3630 return E_DATABASE_ERROR;
3633 if ((eCode1 = sqlite3_finalize(queryInsert)) != SQLITE_OK)
3635 logError("DatabaseHandler::peekDomain SQLITE Finalize error code:", eCode1);
3636 return E_DATABASE_ERROR;
3638 sinkID = sqlite3_last_insert_rowid(mDatabase);
3640 sqlite3_finalize(query);
3644 am_Error_e DatabaseHandler::peekSource(const std::string & name, am_sourceID_t & sourceID)
3646 sqlite3_stmt* query = NULL, *queryInsert = NULL;
3647 std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE name=?";
3648 int eCode = 0, eCode1 = 0;
3649 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3650 sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC);
3651 if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3653 sourceID = sqlite3_column_int(query, 0);
3655 else if (eCode != SQLITE_DONE)
3657 logError("DatabaseHandler::peekSink database error!:", eCode);
3658 return E_DATABASE_ERROR;
3662 if (mFirstStaticSource)
3664 command = "INSERT INTO " + std::string(SOURCE_TABLE) + " (name,reserved,sourceID) VALUES (?,?," + i2s(DYNAMIC_ID_BOUNDARY) + ")";
3665 mFirstStaticSource = false;
3669 command = "INSERT INTO " + std::string(SOURCE_TABLE) + " (name,reserved) VALUES (?,?)";
3671 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &queryInsert, NULL);
3672 sqlite3_bind_text(queryInsert, 1, name.c_str(), name.size(), SQLITE_STATIC);
3673 sqlite3_bind_int(queryInsert, 2, 1); //reservation flag
3674 if ((eCode1 = sqlite3_step(queryInsert)) != SQLITE_DONE)
3676 logError("DatabaseHandler::peekSink SQLITE Step error code:", eCode1);
3677 return E_DATABASE_ERROR;
3680 if ((eCode1 = sqlite3_finalize(queryInsert)) != SQLITE_OK)
3682 logError("DatabaseHandler::peekDomain SQLITE Finalize error code:", eCode1);
3683 return E_DATABASE_ERROR;
3685 sourceID = sqlite3_last_insert_rowid(mDatabase);
3687 sqlite3_finalize(query);
3691 am_Error_e DatabaseHandler::changeSinkVolume(const am_sinkID_t sinkID, const am_volume_t volume)
3695 sqlite3_stmt* query = NULL;
3697 std::string command;
3699 if (!existSink(sinkID))
3701 return E_NON_EXISTENT;
3703 command = "UPDATE " + std::string(SINK_TABLE) + " SET volume=? WHERE sinkID=" + i2s(sinkID);
3704 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3705 sqlite3_bind_int(query, 1, volume);
3706 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3708 logError("DatabaseHandler::changeSinkVolume SQLITE Step error code:", eCode);
3710 return E_DATABASE_ERROR;
3712 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3714 logError("DatabaseHandler::changeSinkVolume SQLITE Finalize error code:", eCode);
3716 return E_DATABASE_ERROR;
3719 logInfo("DatabaseHandler::changeSinkVolume changed volume of sink:", sinkID, "to:", volume);
3724 am_Error_e DatabaseHandler::changeSourceVolume(const am_sourceID_t sourceID, const am_volume_t volume)
3726 assert(sourceID!=0);
3728 sqlite3_stmt* query = NULL;
3730 std::string command;
3732 if (!existSource(sourceID))
3734 return E_NON_EXISTENT;
3736 command = "UPDATE " + std::string(SOURCE_TABLE) + " SET volume=? WHERE sourceID=" + i2s(sourceID);
3737 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3738 sqlite3_bind_int(query, 1, volume);
3739 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3741 logError("DatabaseHandler::changeSourceVolume SQLITE Step error code:", eCode);
3743 return E_DATABASE_ERROR;
3745 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3747 logError("DatabaseHandler::changeSourceVolume SQLITE Finalize error code:", eCode);
3749 return E_DATABASE_ERROR;
3752 logInfo("DatabaseHandler::changeSourceVolume changed volume of source=:", sourceID, "to:", volume);
3757 am_Error_e DatabaseHandler::changeSourceSoundPropertyDB(const am_SoundProperty_s & soundProperty, const am_sourceID_t sourceID)
3759 //todo: add checks if soundproperty exists!
3760 assert(sourceID!=0);
3762 sqlite3_stmt* query = NULL;
3764 std::string command;
3766 if (!existSource(sourceID))
3768 return E_NON_EXISTENT;
3770 command = "UPDATE SourceSoundProperty" + i2s(sourceID) + " SET value=? WHERE soundPropertyType=" + i2s(soundProperty.type);
3771 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3772 sqlite3_bind_int(query, 1, soundProperty.value);
3773 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3775 logError("DatabaseHandler::changeSourceSoundPropertyDB SQLITE Step error code:", eCode);
3777 return E_DATABASE_ERROR;
3780 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3782 logError("DatabaseHandler::changeSourceSoundPropertyDB SQLITE Finalize error code:", eCode);
3784 return E_DATABASE_ERROR;
3787 logInfo("DatabaseHandler::changeSourceSoundPropertyDB changed SourceSoundProperty of source:", sourceID, "type:", soundProperty.type, "to:", soundProperty.value);
3792 am_Error_e DatabaseHandler::changeSinkSoundPropertyDB(const am_SoundProperty_s & soundProperty, const am_sinkID_t sinkID)
3794 //todo: add checks if soundproperty exists!
3797 sqlite3_stmt* query = NULL;
3799 std::string command;
3801 if (!existSink(sinkID))
3803 return E_NON_EXISTENT;
3805 command = "UPDATE SinkSoundProperty" + i2s(sinkID) + " SET value=? WHERE soundPropertyType=" + i2s(soundProperty.type);
3806 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3807 sqlite3_bind_int(query, 1, soundProperty.value);
3808 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3810 logError("DatabaseHandler::changeSinkSoundPropertyDB SQLITE Step error code:", eCode);
3811 return E_DATABASE_ERROR;
3814 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3816 logError("DatabaseHandler::changeSinkSoundPropertyDB SQLITE Finalize error code:", eCode);
3817 return E_DATABASE_ERROR;
3820 logInfo("DatabaseHandler::changeSinkSoundPropertyDB changed MainSinkSoundProperty of sink:", sinkID, "type:", soundProperty.type, "to:", soundProperty.value);
3825 am_Error_e DatabaseHandler::changeCrossFaderHotSink(const am_crossfaderID_t crossfaderID, const am_HotSink_e hotsink)
3827 assert(crossfaderID!=0);
3829 sqlite3_stmt* query = NULL;
3831 std::string command;
3833 if (!existcrossFader(crossfaderID))
3835 return E_NON_EXISTENT;
3837 command = "UPDATE " + std::string(CROSSFADER_TABLE) + " SET hotsink=? WHERE crossfaderID=" + i2s(crossfaderID);
3838 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3839 sqlite3_bind_int(query, 1, hotsink);
3840 if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3842 logError("DatabaseHandler::changeCrossFaderHotSink SQLITE Step error code:", eCode);
3844 return E_DATABASE_ERROR;
3846 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3848 logError("DatabaseHandler::changeCrossFaderHotSink SQLITE Finalize error code:", eCode);
3850 return E_DATABASE_ERROR;
3853 logInfo("DatabaseHandler::changeCrossFaderHotSink changed hotsink of crossfader=", crossfaderID, "to:", hotsink);
3857 am_Error_e DatabaseHandler::getRoutingTree(bool onlyfree, RoutingTree& tree, std::vector<RoutingTreeItem*>& flatTree)
3859 sqlite3_stmt* query = NULL;
3862 std::string command;
3863 am_domainID_t rootID = tree.returnRootDomainID();
3864 RoutingTreeItem *parent = tree.returnRootItem();
3866 command = "SELECT domainSourceID,gatewayID FROM " + std::string(GATEWAY_TABLE) + " WHERE domainSinkID=? AND inUse=?";
3872 parent = flatTree.at(i - 1);
3873 rootID = parent->returnDomainID();
3875 sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3876 sqlite3_bind_int(query, 1, rootID);
3877 sqlite3_bind_int(query, 2, onlyfree);
3879 while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3881 flatTree.push_back(tree.insertItem(sqlite3_column_int(query, 0), sqlite3_column_int(query, 1), parent));
3884 if (eCode != SQLITE_DONE)
3886 logError("DatabaseHandler::getRoutingTree SQLITE error code:", eCode);
3888 return (E_DATABASE_ERROR);
3891 if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3893 logError("DatabaseHandler::getRoutingTree SQLITE Finalize error code:", eCode);
3895 return (E_DATABASE_ERROR);
3898 } while (flatTree.size() > (i - 1));
3903 void DatabaseHandler::createTables()
3905 for (uint16_t i = 0; i < sizeof(databaseTables) / sizeof(databaseTables[0]); i++)
3907 assert(sqQuery("CREATE TABLE " + databaseTables[i]));