MythTV  master
mythstorage.cpp
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 // Myth headers
4 #include "mythstorage.h"
5 
6 #include "mythdb.h"
7 #include "mythcorecontext.h"
8 
10 {
12  MSqlBindings bindings;
13  query.prepare(
14  "SELECT CAST(" + GetColumnName() + " AS CHAR)"
15  " FROM " + GetTableName() +
16  " WHERE " + GetWhereClause(bindings));
17  query.bindValues(bindings);
18 
19  if (!query.exec() || !query.isActive())
20  {
21  MythDB::DBError("SimpleDBStorage::Load()", query);
22  }
23  else if (query.next())
24  {
25  QString result = query.value(0).toString();
26  // a 'NULL' QVariant does not get converted to a 'NULL' QString
27  if (!query.value(0).isNull())
28  {
29  m_initval = result;
30  m_user->SetDBValue(result);
31  }
32  }
33 }
34 
35 void SimpleDBStorage::Save(const QString &table)
36 {
37  if (!IsSaveRequired())
38  return;
39 
40  MSqlBindings bindings;
41  QString querystr = "SELECT * FROM " + table + " WHERE "
42  + GetWhereClause(bindings) + ';';
43 
45  query.prepare(querystr);
46  query.bindValues(bindings);
47 
48  if (!query.exec())
49  {
50  MythDB::DBError("SimpleDBStorage::Save() query", query);
51  return;
52  }
53 
54  if (query.isActive() && query.next())
55  {
56  // Row already exists
57  // Don"t change this QString. See the CVS logs rev 1.91.
58  querystr = "UPDATE " + table + " SET " + GetSetClause(bindings) +
59  " WHERE " + GetWhereClause(bindings) + ';';
60 
61  query.prepare(querystr);
62  query.bindValues(bindings);
63 
64  if (!query.exec())
65  MythDB::DBError("SimpleDBStorage::Save() update", query);
66  }
67  else
68  {
69  // Row does not exist yet
70  querystr = "INSERT INTO " + table + " SET "
71  + GetSetClause(bindings) + ';';
72 
73  query.prepare(querystr);
74  query.bindValues(bindings);
75 
76  if (!query.exec())
77  MythDB::DBError("SimpleDBStorage::Save() insert", query);
78  }
79 }
80 
82 {
83  Save(GetTableName());
84 }
85 
87 {
88  QString tagname(":SET" + GetColumnName().toUpper());
89  QString clause(GetColumnName() + " = " + tagname);
90 
91  bindings.insert(tagname, m_user->GetDBValue());
92 
93  return clause;
94 }
95 
97 {
98  return m_user->GetDBValue() != m_initval;
99 }
100 
102 {
103  m_initval.clear();
104 }
105 
107 
109 {
110  QString keycolumnTag = ":WHERE" + m_keycolumn.toUpper();
111 
112  bindings.insert(keycolumnTag, m_keyvalue);
113 
114  return m_keycolumn + " = " + keycolumnTag;
115 }
116 
118 {
119  QString keycolumnTag = ":SETKEY" + m_keycolumn.toUpper();
120  QString columnTag = ":SETCOL" + GetColumnName().toUpper();
121 
122  bindings.insert(keycolumnTag, m_keyvalue);
123  bindings.insert(columnTag, m_user->GetDBValue());
124 
125  return m_keycolumn + " = " + keycolumnTag + ", " +
126  GetColumnName() + " = " + columnTag;
127 }
128 
130 
132  SimpleDBStorage(_user, "settings", "data"), m_settingname(std::move(name))
133 {
134 }
135 
137 {
138  /* Returns a where clause of the form:
139  * "value = :VALUE AND hostname = :HOSTNAME"
140  * The necessary bindings are added to the MSQLBindings&
141  */
142  QString valueTag(":WHEREVALUE");
143  QString hostnameTag(":WHEREHOSTNAME");
144 
145  QString clause("value = " + valueTag + " AND hostname = " + hostnameTag);
146 
147  bindings.insert(valueTag, m_settingname);
148  bindings.insert(hostnameTag, MythDB::getMythDB()->GetHostName());
149 
150  return clause;
151 }
152 
154 {
155  QString valueTag(":SETVALUE");
156  QString dataTag(":SETDATA");
157  QString hostnameTag(":SETHOSTNAME");
158  QString clause("value = " + valueTag + ", data = " + dataTag
159  + ", hostname = " + hostnameTag);
160 
161  bindings.insert(valueTag, m_settingname);
162  bindings.insert(dataTag, m_user->GetDBValue());
163  bindings.insert(hostnameTag, MythDB::getMythDB()->GetHostName());
164 
165  return clause;
166 }
167 
169 {
172  MythDB::getMythDB()->GetHostName() + ' ' + m_settingname);
173 }
174 
176 
178  StorageUser *_user, QString name) :
179  SimpleDBStorage(_user, "settings", "data"), m_settingname(std::move(name))
180 {
181 }
182 
184 {
185  QString valueTag(":WHEREVALUE");
186  QString clause("value = " + valueTag);
187 
188  bindings.insert(valueTag, m_settingname);
189 
190  return clause;
191 }
192 
194 {
195  QString valueTag(":SETVALUE");
196  QString dataTag(":SETDATA");
197 
198  QString clause("value = " + valueTag + ", data = " + dataTag);
199 
200  bindings.insert(valueTag, m_settingname);
201  bindings.insert(dataTag, m_user->GetDBValue());
202 
203  return clause;
204 }
205 
207 {
210 }
SimpleDBStorage::IsSaveRequired
bool IsSaveRequired(void) const override
Definition: mythstorage.cpp:96
MSqlBindings
QMap< QString, QVariant > MSqlBindings
typedef for a map of string -> string bindings for generic queries.
Definition: mythdbcon.h:98
MSqlQuery::isActive
bool isActive(void) const
Definition: mythdbcon.h:204
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:783
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
HostDBStorage::HostDBStorage
HostDBStorage(StorageUser *_user, QString name)
Definition: mythstorage.cpp:131
mythdb.h
GlobalDBStorage::GetWhereClause
QString GetWhereClause(MSqlBindings &bindings) const override
Definition: mythstorage.cpp:183
DBStorage::m_user
StorageUser * m_user
Definition: mythstorage.h:50
MSqlQuery::bindValues
void bindValues(const MSqlBindings &bindings)
Add all the bindings in the passed in bindings.
Definition: mythdbcon.cpp:879
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:198
HostDBStorage::GetSetClause
QString GetSetClause(MSqlBindings &bindings) const override
Definition: mythstorage.cpp:153
DBStorage::GetColumnName
QString GetColumnName(void) const
Definition: mythstorage.h:47
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
StorageUser::GetDBValue
virtual QString GetDBValue(void) const =0
SimpleDBStorage
Definition: mythstorage.h:55
HostDBStorage::GetWhereClause
QString GetWhereClause(MSqlBindings &bindings) const override
Definition: mythstorage.cpp:136
GlobalDBStorage::m_settingname
QString m_settingname
Definition: mythstorage.h:138
GenericDBStorage::GetWhereClause
QString GetWhereClause(MSqlBindings &bindings) const override
Definition: mythstorage.cpp:108
GenericDBStorage::GetSetClause
QString GetSetClause(MSqlBindings &bindings) const override
Definition: mythstorage.cpp:117
SimpleDBStorage::GetWhereClause
virtual QString GetWhereClause(MSqlBindings &bindings) const =0
GlobalDBStorage::Save
void Save(void) override
Definition: mythstorage.cpp:206
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:179
GlobalDBStorage::GlobalDBStorage
GlobalDBStorage(StorageUser *_user, QString name)
Definition: mythstorage.cpp:177
DBStorage::GetTableName
QString GetTableName(void) const
Definition: mythstorage.h:48
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:57
GenericDBStorage::m_keyvalue
QString m_keyvalue
Definition: mythstorage.h:97
MythDB::getMythDB
static MythDB * getMythDB()
Definition: mythdb.cpp:25
SimpleDBStorage::Save
void Save(void) override
Definition: mythstorage.cpp:81
mythcorecontext.h
SimpleDBStorage::GetSetClause
virtual QString GetSetClause(MSqlBindings &bindings) const
Definition: mythstorage.cpp:86
GlobalDBStorage::GetSetClause
QString GetSetClause(MSqlBindings &bindings) const override
Definition: mythstorage.cpp:193
HostDBStorage::Save
void Save(void) override
Definition: mythstorage.cpp:168
SimpleDBStorage::Load
void Load(void) override
Definition: mythstorage.cpp:9
GenericDBStorage::m_keycolumn
QString m_keycolumn
Definition: mythstorage.h:96
HostDBStorage::m_settingname
QString m_settingname
Definition: mythstorage.h:123
MythCoreContext::ClearSettingsCache
void ClearSettingsCache(const QString &myKey=QString(""))
Definition: mythcorecontext.cpp:846
mythstorage.h
StorageUser::SetDBValue
virtual void SetDBValue(const QString &)=0
query
MSqlQuery query(MSqlQuery::InitCon())
SimpleDBStorage::m_initval
QString m_initval
Definition: mythstorage.h:74
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:808
SimpleDBStorage::SetSaveRequired
void SetSaveRequired(void) override
Definition: mythstorage.cpp:101
StorageUser
Definition: mythstorage.h:15