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