WARNING: USE THIS SOFTWARE AT YOUR OWN RISK! THIS IS EXPERIMENTAL SOFTWARE NOT INTENDED FOR PRODUCTION USE! Zuble is currently an early stage prototype. As such Zuble is minimally tested and inherently unstable. It is provided for experimental, development, and demonstration purposes only. Zuble QML Types   |  Zuble C++ Classes   |  Zuble Overview
Zuble  0.1
Zuble Framework C++/QML extension API
ZsqlQuery.h
Go to the documentation of this file.
1 /*
2  * Zuble - A run-time system for QML/Javascript applications
3  * Copyright (C) 2013, 2014 Bob Dinitto
4  *
5  * ZsqlQuery.h
6  *
7  * Created on: 06-Aug, 2014
8  * Author: Bob Dinitto bob@ninzo.com
9  *
10  * Zuble is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 #ifndef ZSQLQUERY_H
27 #define ZSQLQUERY_H
28 
29 #include <QObject>
30 #include <QSqlQuery>
31 #include <QMap>
32 #include <QVariant>
33 #include "zblcore_global.h"
34 #include "ZblException.h"
35 //#include "ZblScriptOb.h"
36 
37 namespace Zbl
38 {
39 
40 class ZsqlDatabase;
41 class ZTableModel;
42 
43 
67 class ZBLCORESHARED_EXPORT ZsqlQuery : public QObject
68 {
69  Q_OBJECT
70 public:
71  typedef QList<QString> zMetaRow;
72 
73  Q_ENUMS(BatchExecutionMode)
74 
76  {
77  ValuesAsRows = QSqlQuery::ValuesAsRows,
78  ValuesAsColumns = QSqlQuery::ValuesAsColumns
79  };
80 
81  Q_ENUMS(ParamType)
82 
83  enum ParamType
84  {
85  In = QSql::In,
86  Out = QSql::Out,
87  InOut = QSql::InOut,
88  Binary = QSql::Binary
89  };
90 
91  Q_ENUMS(ResultType)
92 
94  {
95  StringSequence = 0,
96  TableModel
97 
98  };
99 
100 
101  explicit ZsqlQuery(QSqlQuery* query, QObject *parent = 0);
102 
103  virtual ~ZsqlQuery();
104 
105 
106  QSqlQuery& queryObject() const;
107 
108  bool getTableModel(ZTableModel* model);
109 
116  QVariant getTags();
117 
123  Q_PROPERTY(QVariant tags READ getTags)
124 
125 
129  Q_PROPERTY(bool isValid READ isValid)
130 
138  Q_PROPERTY(bool isActive READ isActive)
139 
144  Q_PROPERTY(bool forewardOnly READ isForwardOnly WRITE setForwardOnly)
145 
150  Q_PROPERTY(int position READ getPosition)
151 
156  Q_PROPERTY(QString lastQuery READ lastQuery)
157 
162  Q_PROPERTY(int numRowsAffected READ numRowsAffected)
163 
169  Q_PROPERTY(QObject* lastError READ getLastError)
170 
175  Q_PROPERTY(bool isSelect READ isSelect)
176 
183  Q_PROPERTY(int size READ size)
184 
188  Q_PROPERTY(int numericalPrecisionPolicy READ getNumericalPrecisionPolicy
189  WRITE setNumericalPrecisionPolicy)
190 
194  Q_PROPERTY(QVariant boundValues READ getBoundValues)
195 
199  Q_PROPERTY(QString executedQuery READ getExecutedQuery)
200 
204  Q_PROPERTY(QVariant lastInsertId READ getLastInsertId)
205 
209  Q_PROPERTY(QVariant metaRow READ getJsMetaRow)
210 
211 
212  // property methods
213 
214  bool isValid() const;
215  bool isActive() const;
216  bool isForwardOnly() const;
217  void setForwardOnly(bool forward);
218  int getPosition() const;
219  QString lastQuery() const;
220  int numRowsAffected() const;
221  QObject* getLastError();
222  bool isSelect() const;
223  int size() const;
224  void setNumericalPrecisionPolicy(int policy);
225  int getNumericalPrecisionPolicy() const;
226  QVariant getBoundValues() const;
227  QString getExecutedQuery() const;
228  QVariant getLastInsertId() const;
229  zMetaRow getMetaRow() const;
230  QVariant getJsMetaRow() const;
231 
232 
241  static ZsqlQuery* newQuery(ZsqlDatabase& database);
242 
253  Q_INVOKABLE void release();
254 
255 
262  Q_INVOKABLE void bindValue(
263  const QString& placeholder,
264  const QVariant& value,
265  int paramType);
266 
272  Q_INVOKABLE QVariant boundValue(const QString& placeholder) const;
273 
279  Q_INVOKABLE QVariant value(const QString& name) const;
280 
286  Q_INVOKABLE bool isNull(int field) const;
287 
292  Q_INVOKABLE bool exec();
293 
299  Q_INVOKABLE bool prepare(const QString& query);
300 
315  //Q_INVOKABLE bool exec(
316  // const QString& query,
317  // ZsqlQuery::ResultType resultType = ZsqlQuery::StringSequence,
318  // QObject* tableModel = NULL);
319  Q_INVOKABLE bool exec(
320  const QString& query,
321  int resultType,
322  QObject* tableModel);
323 
337  Q_INVOKABLE QVariant value(int i) const;
338 
345  Q_INVOKABLE bool seek(int i, bool relative = false);
346 
356  Q_INVOKABLE bool next();
357 
367  Q_INVOKABLE bool previous();
368 
378  Q_INVOKABLE bool first();
379 
389  Q_INVOKABLE bool last();
390 
395  Q_INVOKABLE void clear();
396 
407  Q_INVOKABLE void finish();
408 
414  Q_INVOKABLE bool nextResult();
415 
416 
417 
422  Q_INVOKABLE QObject* record();
423 
424 
434  Q_INVOKABLE bool populateTableModel(QObject* model);
435 
436 
438  // Begin - Binding and prepared queries methods
439  // WARNING: untested!
441 
442 
448  Q_INVOKABLE QVariant boundValue(int pos) const;
449 
450 
461  Q_INVOKABLE bool execBatch(int mode = ValuesAsRows);
462 
463 
475  Q_INVOKABLE void bindValue(int pos, const QVariant& val, ParamType type = In);
476 
488  Q_INVOKABLE void addBindValue(const QVariant& val, ZsqlQuery::ParamType = In);
489 
490 
492  // End - Binding and prepared queries
494 
495 
496 
497 
498 /****** Not wrapped ***********
499 * const QSqlDriver* driver() const;
500 * const QSqlResult* result() const;
501 * //QSqlRecord record() const;
502 ******************************/
503 
504 signals:
505 
506  void queryResult(QObject* qResult); // experiment!
507 
508 
509 public slots:
510 
511 protected:
512 
519  void initGetTableModel();
520 
526  inline void validateQuery() const;
527 
532  void createTags();
533 
538  void ensureParamType(int paramType);
539 
543  QSqlQuery* m_q;
544  //ZqSqlQueryPtr m_q;
545 
546  //ZPlaceMap m_values;
547 
555  static QVariant m_tags;
556 
557 
558 };
559 
560 inline void ZsqlQuery::validateQuery() const
561 {
562  if(!m_q)
563  throw ZblException(
564  "Error: ZsqlQuery's embedded QSqlQuery object has already "
565  "been released.");
566 }
567 
568 inline QSqlQuery& ZsqlQuery::queryObject() const
569 {
570  validateQuery();
571  return *m_q;
572 }
573 
574 
575 } // Zbl
576 
577 Q_DECLARE_METATYPE(Zbl::ZsqlQuery::ResultType)
578 
579 
580 #endif // ZSQLQUERY_H
#define ZBLCORESHARED_EXPORT
A javascript wrapper class for QSqlDatabase objects. This object represents a database connection...
Definition: ZsqlDatabase.h:71
Definition: ZAndGate.cpp:6
QSqlQuery & queryObject() const
Definition: ZsqlQuery.h:568
A javascript wrapper class for QSqlQuery objects. This object represents a query on a database connec...
Definition: ZsqlQuery.h:67
QList< QString > zMetaRow
Definition: ZsqlQuery.h:71
This two dimensional table model is used to store and manipulate data.
Definition: ZTableModel.h:96
Zuble&#39;s Qt Exception Object.
Definition: ZblException.h:45