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
ZsqlDatabase.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  * ZsqlDatabase.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 ZSQLDATABASE_H
27 #define ZSQLDATABASE_H
28 
29 #include "zblcore_global.h"
30 #include "zglobal.h"
31 #include "ZblException.h"
32 #include <QObject>
33 #include <QSqlDatabase>
34 #include <QReadWriteLock>
35 #include <QObjectCleanupHandler>
36 #include <QStringList>
37 #include <QVariant>
38 
39 namespace Zbl
40 {
41 
42 class ZsqlQuery;
43 
71 class ZBLCORESHARED_EXPORT ZsqlDatabase : public QObject
72 {
73  friend class Zbl::ZsqlQuery;
74 
75  //typedef QSharedPointer<ZsqlDatabase> ZsqlDatabasePtr;
76  typedef QPointer<ZsqlQuery> ZsqlQueryPtr;
77 
78  Q_OBJECT
79 
80  typedef QMap<QString, ZsqlDatabase*> ZsqlDbMap;
81  typedef QList<ZsqlQuery*> ZsqlQueryList;
82 
83 public:
84 
85  // to participate in automatic enum propagation
86  // for QML programs we redeclare these enums;
87  // note background Javascript programs can't use QML
88  // type engine, those programs must use tags property instead
89 
90  Q_ENUMS(TableType)
91 
92  enum TableType {
93  Tables = QSql::Tables,
94  SystemTables = QSql::SystemTables,
95  Views = QSql::Views,
96  AllTables = QSql::AllTables,
97 
98  };
99  Q_DECLARE_FLAGS(TableTypes, TableType)
100 
101 
102  Q_ENUMS(NumericalPrecisionPolicy)
103 
105  {
106  LowPrecisionInt32 = QSql::LowPrecisionInt32,
107  LowPrecisionInt64 = QSql::LowPrecisionInt64,
108  LowPrecisionDouble = QSql::LowPrecisionDouble,
109  HighPrecision = QSql::HighPrecision
110  };
111 
112  explicit ZsqlDatabase(QObject *parent = 0);
113 
114  virtual ~ZsqlDatabase();
115 
122  Q_PROPERTY(QString hostName READ getHostname WRITE setHostname)
123 
124 
130  Q_PROPERTY(QString databaseName READ getDbName WRITE setDbName)
131 
138  Q_PROPERTY(int portNumber READ getPortNumber WRITE setPortNumber)
139 
144  Q_PROPERTY(QString connectOptions READ getConnectOptions
145  WRITE setConnectOptions)
146 
153  Q_PROPERTY(bool isValid READ isValid)
154 
161  Q_PROPERTY(bool isOpen READ isOpen)
162 
169  Q_PROPERTY(bool isOpenError READ isOpenError)
170 
177  Q_PROPERTY(QObject* lastError READ getLastError)
178 
183  Q_PROPERTY(QVariant tags READ getTags)
184 
185 
186 
187  bool isValid() const;
188 
189  bool isOpen() const;
190 
191  bool isOpenError() const;
192 
193  QString getHostname() const;
194 
195  void setHostname(const QString& hostname);
196 
197  QString getDbName() const;
198 
199  void setDbName(const QString& hostname);
200 
201  int getPortNumber() const;
202 
203  void setPortNumber(int);
204 
205  void setConnectOptions(const QString& options);
206 
207  QString getConnectOptions();
208 
209  QObject* getLastError();
210 
211  QVariant getTags();
212 
213 
222  Q_INVOKABLE bool open(const QString & user, const QString & password);
223 
224 
230  Q_INVOKABLE void close();
231 
245  Q_INVOKABLE QStringList getTableNames(int tableType) const;
246 
252  Q_INVOKABLE bool transaction();
253 
263  Q_INVOKABLE bool commit();
264 
274  Q_INVOKABLE bool rollback();
275 
282  Q_INVOKABLE QObject* newQuery();
283 
290  Q_INVOKABLE QObject* record(const QString& tablename);
291 
299  Q_INVOKABLE void release();
300 
315  static QObject* addDatabase(const QString& driverType,
316  const QString& connectionName );
317 
331  static void removeDatabase(const QString& connectionName );
332 
339  static void removeThreadDatabases();
340 
341 
355  static QObject* database(const QString& connectionName);
356 
357 signals:
358 
359 public slots:
360 
361 
362 protected:
363 
368  void createTags();
369 
375  inline void validateDb() const;
376 
381  inline void validateThread();
382 
387  QSqlDatabase* m_db;
388 
398  static ZsqlDbMap m_databaseObjects;
399 
403  static QReadWriteLock m_lock;
404 
412  static QVariant m_tags;
413 
414 };
415 
416 inline void ZsqlDatabase::validateThread()
417 {
418  if(!inObjectThread(this))
419  throw ZblException("ZsqlDatabase objects should only be called from the "
420  "thread in which they were created.");
421 }
422 
423 inline void ZsqlDatabase::validateDb() const
424 {
425  if(!m_db)
426  throw ZblException(
427  "Error: ZsqlDatabase's embedded QSqlDatabase object has already "
428  "been released.");
429 }
430 
431 } // Zbl
432 
433 #endif // ZSQLDATABASE_H
#define ZBLCORESHARED_EXPORT
void validateDb() const
Checks that the embedded QSqlDatabase object exists and throws a ZblException if not.
Definition: ZsqlDatabase.h:423
QObject lastError
Returns a ZsqlError object representing the last error encountered by this query. ...
Definition: ZsqlQuery.h:169
void createTags()
Create the m_tag object that presents a Javascript interface for ZsqlQuery enumeration values...
Definition: ZsqlQuery.cpp:54
QPointer< ZsqlQuery > ZsqlQueryPtr
Definition: ZsqlDatabase.h:76
QVariant tags
Obtains a javascript dictionary object that maps constants, enumerations and flags for use with ZsqlQ...
Definition: ZsqlQuery.h:123
bool isValid() const
QVariant getTags()
Obtains a javascript dictionary object that maps constants, enumerations and flags for use with ZsqlQ...
Definition: ZsqlQuery.cpp:189
A javascript wrapper class for QSqlDatabase objects. This object represents a database connection...
Definition: ZsqlDatabase.h:71
Definition: ZAndGate.cpp:6
A javascript wrapper class for QSqlQuery objects. This object represents a query on a database connec...
Definition: ZsqlQuery.h:67
QObject * getLastError()
Definition: ZsqlQuery.cpp:417
static QVariant m_tags
A QVariantMap used to pass ZsqlQuery enumeration values to Javascript programs.
Definition: ZsqlQuery.h:555
QMap< QString, ZsqlDatabase * > ZsqlDbMap
Definition: ZsqlDatabase.h:80
static ZsqlQuery * newQuery(ZsqlDatabase &database)
Constructs an empty ZsqlQuery object for the specified database connection.
Definition: ZsqlQuery.cpp:149
bool inObjectThread(const QObject &object)
Definition: zglobal.h:173
Zuble&#39;s Qt Exception Object.
Definition: ZblException.h:45
Q_INVOKABLE QObject * record()
Obtains a ZsqlRecord object representing the query result.
Definition: ZsqlQuery.cpp:630
QList< ZsqlQuery * > ZsqlQueryList
Definition: ZsqlDatabase.h:81
Q_INVOKABLE void release()
Releases references from this object to wrapped Qt C++ objects.
Definition: ZsqlQuery.cpp:194