Ejemplo n.º 1
0
DTC::SettingList *Myth::GetSettingList(const QString &sHostName)
{

    MSqlQuery query(MSqlQuery::InitCon());

    if (!query.isConnected())
    {
        throw( QString("Database not open while trying to load settings for host: %1")
                  .arg( sHostName ));
    }

    DTC::SettingList *pList = new DTC::SettingList();

    //pList->setObjectName( "Settings" );
    pList->setHostName  ( sHostName  );

    // ------------------------------------------------------------------
    // Looking to return all Setting for supplied hostname
    // ------------------------------------------------------------------

    if (sHostName.isEmpty())
    {
        query.prepare("SELECT value, data FROM settings "
                        "WHERE (hostname IS NULL)" );
    }
    else
    {
        query.prepare("SELECT value, data FROM settings "
                        "WHERE (hostname = :HOSTNAME)" );

        query.bindValue(":HOSTNAME", sHostName );
    }

    if (!query.exec())
    {
        // clean up unused object we created.

        delete pList;

        MythDB::DBError("MythAPI::GetSetting() w/o key ", query);
        throw( QString( "Database Error executing query." ));
    }

    while (query.next())
        pList->Settings().insert( query.value(0).toString(), query.value(1) );

    return pList;
}
Ejemplo n.º 2
0
DTC::SettingList *Myth::GetSetting( const QString &sHostName,
                                    const QString &sKey,
                                    const QString &sDefault )
{

    MSqlQuery query(MSqlQuery::InitCon());

    if (!query.isConnected())
    {
        throw( QString("Database not open while trying to load setting: %1")
                  .arg( sKey ));
    }

    // ----------------------------------------------------------------------

    DTC::SettingList *pList = new DTC::SettingList();

    //pList->setObjectName( "Settings" );
    pList->setHostName  ( sHostName  );

    // ----------------------------------------------------------------------
    // Format Results
    // ----------------------------------------------------------------------

    if (!sKey.isEmpty())
    {
        // ------------------------------------------------------------------
        // Key Supplied, lookup just its value.
        // ------------------------------------------------------------------

        query.prepare("SELECT data, hostname FROM settings "
                      "WHERE value = :KEY AND "
                      "(hostname = :HOSTNAME OR hostname IS NULL) "
                      "ORDER BY hostname DESC;" );

        query.bindValue(":KEY"     , sKey      );
        query.bindValue(":HOSTNAME", sHostName );

        if (!query.exec())
        {
            // clean up unused object we created.

            delete pList;

            MythDB::DBError("MythAPI::GetSetting() w/key ", query);

            throw( QString( "Database Error executing query." ));
        }

        if (query.next())
        {
            if ( (sHostName.isEmpty()) ||
                 ((!sHostName.isEmpty()) &&
                  (sHostName == query.value(1).toString())))
            {
                pList->setHostName( query.value(1).toString() );

                pList->Settings().insert( sKey, query.value(0) );
            }
        }
    }
    else
    {
        // ------------------------------------------------------------------
        // Looking to return all Setting for supplied hostname
        // ------------------------------------------------------------------

        if (sHostName.isEmpty())
        {
            query.prepare("SELECT value, data FROM settings "
                          "WHERE (hostname IS NULL)" );
        }
        else
        {
            query.prepare("SELECT value, data FROM settings "
                          "WHERE (hostname = :HOSTNAME)" );

            query.bindValue(":HOSTNAME", sHostName );
        }

        if (!query.exec())
        {
            // clean up unused object we created.

            delete pList;

            MythDB::DBError("MythAPI::GetSetting() w/o key ", query);
            throw( QString( "Database Error executing query." ));
        }

        while (query.next())
            pList->Settings().insert( query.value(0).toString(), query.value(1) );
    }

    // ----------------------------------------------------------------------
    // Not found, so return the supplied default value
    // ----------------------------------------------------------------------

    if (pList->Settings().count() == 0)
        pList->Settings().insert( sKey, sDefault );

    return pList;
}