MythTV  master
newsdbutil.cpp
Go to the documentation of this file.
1 // Myth headers
2 #include <libmythbase/mythdb.h>
3 
4 // MythNews headers
5 #include "newsdbutil.h"
6 #include "newssite.h"
7 
8 bool findInDB(const QString& name)
9 {
11  query.prepare("SELECT name FROM newssites WHERE name = :NAME ;");
12  query.bindValue(":NAME", name);
13  if (!query.exec() || !query.isActive()) {
14  MythDB::DBError("new find in db", query);
15  return false;
16  }
17 
18  return query.size() > 0;
19 }
20 
22 {
23  if (!site) return false;
24 
25  return insertInDB(site->m_name, site->m_url, site->m_ico, site->m_category,
26  site->m_podcast);
27 }
28 
29 bool insertInDB(const QString &name, const QString &url,
30  const QString &icon, const QString &category,
31  const bool podcast)
32 {
33  if (findInDB(name))
34  return false;
35 
37  query.prepare("INSERT INTO newssites (name,category,url,ico,podcast,updated) "
38  " VALUES( :NAME, :CATEGORY, :URL, :ICON, :PODCAST, 0);");
39  query.bindValue(":NAME", name);
40  query.bindValue(":CATEGORY", category);
41  query.bindValue(":URL", url);
42  query.bindValue(":ICON", icon);
43  query.bindValue(":PODCAST", podcast);
44  if (!query.exec() || !query.isActive()) {
45  MythDB::DBError("news: inserting in DB", query);
46  return false;
47  }
48 
49  return (query.numRowsAffected() > 0);
50 }
51 
53 {
54  if (!site) return false;
55 
56  return removeFromDB(site->m_name);
57 }
58 
59 bool removeFromDB(const QString &name)
60 {
62  query.prepare("DELETE FROM newssites WHERE name = :NAME ;");
63  query.bindValue(":NAME", name);
64  if (!query.exec() || !query.isActive()) {
65  MythDB::DBError("news: delete from db", query);
66  return false;
67  }
68 
69  return (query.numRowsAffected() > 0);
70 }
MSqlQuery::isActive
bool isActive(void) const
Definition: mythdbcon.h:215
newsdbutil.h
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:127
MSqlQuery::size
int size(void) const
Definition: mythdbcon.h:214
findInDB
bool findInDB(const QString &name)
Definition: newsdbutil.cpp:8
mythdb.h
insertInDB
bool insertInDB(NewsSiteItem *site)
Definition: newsdbutil.cpp:21
NewsSiteItem::m_category
QString m_category
Definition: newssite.h:29
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:619
removeFromDB
bool removeFromDB(NewsSiteItem *site)
Definition: newsdbutil.cpp:52
NewsSiteItem::m_podcast
bool m_podcast
Definition: newssite.h:33
NewsSiteItem
Definition: newssite.h:23
NewsSiteItem::m_url
QString m_url
Definition: newssite.h:30
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:551
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:226
NewsSiteItem::m_name
QString m_name
Definition: newssite.h:28
newssite.h
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:889
MSqlQuery::numRowsAffected
int numRowsAffected() const
Definition: mythdbcon.h:217
NewsSiteItem::m_ico
QString m_ico
Definition: newssite.h:31
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:838