MythTV  master
newsdbutil.cpp
Go to the documentation of this file.
1 // Myth headers
2 #include <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 }
bool findInDB(const QString &name)
Definition: newsdbutil.cpp:8
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:863
QString m_category
Definition: newssite.h:31
QString m_name
Definition: newssite.h:30
bool m_podcast
Definition: newssite.h:35
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
int size(void) const
Definition: mythdbcon.h:203
QString m_ico
Definition: newssite.h:33
bool isActive(void) const
Definition: mythdbcon.h:204
static MSqlQueryInfo InitCon(ConnectionReuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
const char * name
Definition: ParseText.cpp:328
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:807
int numRowsAffected() const
Definition: mythdbcon.h:206
bool insertInDB(NewsSiteItem *site)
Definition: newsdbutil.cpp:21
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
QString m_url
Definition: newssite.h:32
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:179
bool removeFromDB(NewsSiteItem *site)
Definition: newsdbutil.cpp:52