automotive-message-broker  0.14.803
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
db.h
1 /*
2  Copyright (C) 2012 Intel Corporation
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include <QObject>
20 #include <QSqlDatabase>
21 #include <QSqlQuery>
22 #include <QVariant>
23 #include <QVariantList>
24 
25 class Database;
26 class Query;
27 
28 class Database: public QObject
29 {
30  Q_OBJECT
31 public:
32  Database(QObject* parent = nullptr):QObject(parent){ }
33  ~Database() { close(); }
34 
35  QSqlDatabase database() { return db; }
36 
37 public Q_SLOTS:
38 
39  bool open(QString connectionName, QString filename);
40  void close();
41 
42  QObject* exec(QString query);
43 
44  QString lastError();
45 
46 private:
47  QSqlDatabase db;
48 };
49 
50 class Query: public QObject
51 {
52  Q_OBJECT
53 public:
54  Query(QObject* parent = nullptr):QObject(parent){}
55  Query(QSqlQuery q, QObject* parent = nullptr);
56  Q_INVOKABLE Query(QString connectionName);
57 public Q_SLOTS:
58  void setConnectionName(QString connectionName);
59 
60  bool exec(QString queryStr);
61 
62  QVariantList results();
63 
64 private:
65  QSqlQuery query;
66  QString mConnectionName;
67  QSqlDatabase db;
68 };
69 
70 
71 class BluemonkeyDatabaseModule : public QObject
72 {
73  Q_OBJECT
74 public:
75  BluemonkeyDatabaseModule(QObject* parent = nullptr): QObject(parent) { }
76 
77 public Q_SLOTS:
78  QObject* createNewDatabase()
79  {
80  return new Database(this);
81  }
82 
83  QObject* createNewQuery()
84  {
85  return new Query(this);
86  }
87 };