adds dataset-specific version of GetSingleValue, allowing different datasets to be...
authorJonathan Marshall <jmarshall@never.you.mind>
Fri, 25 May 2012 03:37:47 +0000 (15:37 +1200)
committerJonathan Marshall <jmarshall@never.you.mind>
Fri, 25 May 2012 03:40:18 +0000 (15:40 +1200)
xbmc/dbwrappers/Database.cpp
xbmc/dbwrappers/Database.h

index bf35173..96a9f8d 100644 (file)
@@ -111,41 +111,35 @@ CStdString CDatabase::PrepareSQL(CStdString strStmt, ...) const
   return strResult;
 }
 
-CStdString CDatabase::GetSingleValue(const CStdString &strTable, const CStdString &strColumn, const CStdString &strWhereClause /* = CStdString() */, const CStdString &strOrderBy /* = CStdString() */)
+std::string CDatabase::GetSingleValue(const std::string &query, std::auto_ptr<Dataset> &ds)
 {
-  CStdString strReturn;
-
+  std::string ret;
   try
   {
-    if (NULL == m_pDB.get()) return strReturn;
-    if (NULL == m_pDS.get()) return strReturn;
-
-    CStdString strQueryBase = "SELECT %s FROM %s";
-    if (!strWhereClause.IsEmpty())
-      strQueryBase.AppendFormat(" WHERE %s", strWhereClause.c_str());
-    if (!strOrderBy.IsEmpty())
-      strQueryBase.AppendFormat(" ORDER BY %s", strOrderBy.c_str());
-    strQueryBase.append(" LIMIT 1");
-
-    CStdString strQuery = PrepareSQL(strQueryBase,
-        strColumn.c_str(), strTable.c_str());
-
-    if (!m_pDS->query(strQuery.c_str())) return strReturn;
+    if (!m_pDB.get() || !ds.get())
+      return ret;
 
-    if (m_pDS->num_rows() > 0)
-    {
-      strReturn = m_pDS->fv(0).get_asString();
-    }
+    if (ds->query(query.c_str()) && ds->num_rows() > 0)
+      ret = ds->fv(0).get_asString();
 
-    m_pDS->close();
+    ds->close();
   }
   catch(...)
   {
-    CLog::Log(LOGERROR, "%s - failed to get value '%s' from table '%s'",
-        __FUNCTION__, strColumn.c_str(), strTable.c_str());
+    CLog::Log(LOGERROR, "%s - failed on query '%s'", __FUNCTION__, query.c_str());
   }
+  return ret;
+}
 
-  return strReturn;
+CStdString CDatabase::GetSingleValue(const CStdString &strTable, const CStdString &strColumn, const CStdString &strWhereClause /* = CStdString() */, const CStdString &strOrderBy /* = CStdString() */)
+{
+  CStdString query = PrepareSQL("SELECT %s FROM %s", strColumn.c_str(), strTable.c_str());
+  if (!strWhereClause.empty())
+    query += " WHERE %s" + strWhereClause;
+  if (!strOrderBy.empty())
+    query += " ORDER BY %s" + strOrderBy;
+  query += " LIMIT 1";
+  return GetSingleValue(query, m_pDS);
 }
 
 bool CDatabase::DeleteValues(const CStdString &strTable, const CStdString &strWhereClause /* = CStdString() */)
index 541ce8d..52c4aa2 100644 (file)
@@ -63,6 +63,13 @@ public:
    */
   CStdString GetSingleValue(const CStdString &strTable, const CStdString &strColumn, const CStdString &strWhereClause = CStdString(), const CStdString &strOrderBy = CStdString());
 
+  /*! \brief Get a single value from a query on a dataset.
+   \param query the query in question.
+   \param ds the dataset to use for the query.
+   \return the value from the query, empty on failure.
+   */
+  std::string GetSingleValue(const std::string &query, std::auto_ptr<dbiplus::Dataset> &ds);
+
   /*!
    * @brief Delete values from a table.
    * @remarks The value of the strWhereClause parameter has to be FormatSQL'ed when used.