File: querysource.cpp

package info (click to toggle)
openrpt 3.3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,220 kB
  • ctags: 4,263
  • sloc: cpp: 31,332; ansic: 12,175; xml: 7,401; sh: 376; sql: 32; makefile: 21
file content (257 lines) | stat: -rw-r--r-- 5,238 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * OpenRPT report writer and rendering engine
 * Copyright (C) 2001-2014 by OpenMFG, LLC
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * Please contact info@openmfg.com with any questions on this license.
 */

#include "querysource.h"
#include "xsqlquery.h"

#include <QVariant>

//
// QuerySource method implementations
//
QuerySource::QuerySource()
  : _inList(0)
{
}

QuerySource::QuerySource(const QString & n, const QString & q, bool fdb, const QString & mg, const QString & mn)
  : _name(n), _query(q), _loadFromDb(fdb), _mqlGroup(mg), _mqlName(mn), _inList(0)
{
}

QuerySource::~QuerySource()
{
  if (_inList != 0)
    _inList->remove(this);
}

QString QuerySource::name() const
{
  return _name;
}
void QuerySource::setName(const QString & n)
{
  if (_name != n)
  {
    _name = n;
    updated();
  }
}

QString QuerySource::query() const
{
  return _query;
}
void QuerySource::setQuery(const QString & q)
{
  if (_query != q)
  {
    _query = q;
    updated();
  }
}

bool QuerySource::loadFromDb() const
{
  return _loadFromDb;
}
void QuerySource::setLoadFromDb(bool b)
{
  if (_loadFromDb != b)
  {
    _loadFromDb = b;
    updated();
  }
}

QString QuerySource::metaSqlGroup() const
{
  return _mqlGroup;
}
void QuerySource::setMetaSqlGroup(const QString & g)
{
  if (_mqlGroup != g)
  {
    _mqlGroup = g;
    updated();
  }
}

QString QuerySource::metaSqlName() const
{
  return _mqlName;
}
void QuerySource::setMetaSqlName(const QString & g)
{
  if (_mqlName != g)
  {
    _mqlName = g;
    updated();
  }
}

void QuerySource::updated()
{
  if (_inList != 0)
    _inList->childUpdated(this);
}

QString QuerySource::query(const QSqlDatabase & db) const
{
  if(_loadFromDb)
  {
    XSqlQuery xqry(db);
    xqry.prepare("SELECT metasql_query"
                 "  FROM metasql"
                 " WHERE ((metasql_group=:group)"
                 "    AND (metasql_name=:name))"
                 " ORDER BY metasql_grade DESC"
                 " LIMIT 1;");
    xqry.bindValue(":group", _mqlGroup);
    xqry.bindValue(":name", _mqlName);
    xqry.exec();
    xqry.first();
    return xqry.value(0).toString();
  }

  return query();
}

//
// QuerySourceList method implementations
//
QuerySourceList::QuerySourceList(QObject * parent)
  : QObject(parent)
{
  _srcList.clear();
}

QuerySourceList::~QuerySourceList()
{
  // since we are coupled with QuerySource pretty firmly
  // We will go through the _srcList and remove the values
  // so as to prevent any confusion about what gets deleted
  // when and how
  QuerySource *qs = 0;
  while (size() > 0)
  {
    qs = remove(0);             // remove from list
    if (qs)
    {
      delete qs;                // delete
      qs = 0;
    }
  }
}

unsigned int QuerySourceList::size()
{
  return _srcList.count();
}

bool QuerySourceList::add(QuerySource * qs)
{
  if (qs && !qs->_name.isEmpty())
  {
    if (get(qs->_name) == 0)
    {
      if (qs->_inList != 0)
        qs->_inList->remove(qs);
      qs->_inList = this;
      _srcList.append(qs);
      emit updated();
      return true;
    }
  }
  return false;
}

QuerySource *QuerySourceList::remove(int i)
{
  QuerySource *qs = 0;

  if (i >= 0 && (unsigned int)i < size())
  {
    qs = (QuerySource *) _srcList.at(i);
    _srcList.takeAt(i);
  }

  if (qs != 0)
  {
    qs->_inList = 0;            // mark this object as not being in any list
    emit updated();
  }

  return qs;
}

QuerySource *QuerySourceList::remove(QuerySource * qs)
{
  if (qs && !qs->_name.isEmpty())
    return remove(qs->_name);
  return 0;
}

QuerySource *QuerySourceList::remove(const QString & name)
{
  if (!name.isEmpty())
  {
    QuerySource *qs = 0;
    for (unsigned int i = 0; i < size(); i++)
    {
      qs = (QuerySource *) _srcList.at(i);
      if (qs->_name == name)
        return remove(i);
    }
  }
  return 0;
}

QuerySource *QuerySourceList::get(int i)
{
  if (i >= 0 && (unsigned int)i < size())
    return _srcList.at(i);
  return 0;
}

QuerySource *QuerySourceList::get(const QString & name)
{
  if (!name.isEmpty())
  {
    QuerySource *qs = 0;
    for (unsigned int i = 0; i < size(); i++)
    {
      qs = (QuerySource *) _srcList.at(i);
      if (qs->_name == name)
        return qs;
    }
  }
  return 0;
}

void QuerySourceList::childUpdated(QuerySource * /*qs */ )
{
  // one of the items in our list has been updated
  // don't really care which one although we do have
  // that information. Just pass along that our list
  // has been updated to any one who wants to know that
  // information
  emit updated();
}