KDb

KDbSimpleCommandLineApp.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2006 Jarosław Staniek <staniek@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 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 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "KDbSimpleCommandLineApp.h"
21
22#include <QFileInfo>
23#include <QTextStream>
24
25#include <KCmdLineArgs>
26#include <KComponentData>
27
28#include "KDbConnectionData.h"
29#include "KDbDriverManager.h"
30#include "KDb.h"
31
32//! @internal used for KDbSimpleCommandLineApp
33class Q_DECL_HIDDEN KDbSimpleCommandLineApp::Private
34{
35public:
36 Private()
37 : conn(0) {}
38 ~Private() {
39 if (conn) {
40 conn->disconnect();
41 delete(KDbConnection*)conn;
42 }
43 }
44
45 KDbDriverManager manager;
46 KComponentData componentData;
47 KDbConnectionData connData;
49private:
50 Q_DISABLE_COPY(Private)
51};
52
53//-----------------------------------------
54
56 int argc, char** argv, const KCmdLineOptions &options,
57 const char *programName, const char *version,
58 const char *shortDescription, KAboutData::LicenseKey licenseType,
59 const char *copyrightStatement, const char *text,
60 const char *homePageAddress, const char *bugsEmailAddress)
61 : KDbObject()
62 , d(new Private())
63{
64 QFileInfo fi(argv[0]);
65 QByteArray appName(fi.baseName().toLatin1());
66 KCmdLineArgs::init(argc, argv,
67 new KAboutData(appName, 0, tr(programName),
68 version, tr(shortDescription), licenseType, tr(copyrightStatement), tr(text),
69 homePageAddress, bugsEmailAddress));
70
71 d->componentData = KComponentData(appName);
72
73 KCmdLineOptions allOptions;
74
75 // add predefined options
76 allOptions.add("drv", KLocalizedString(), KDb::defaultFileBasedDriverId().toUtf8());
77 allOptions.add("driver <id>", tr("Database driver ID"));
78 allOptions.add("u");
79 allOptions.add("user <name>", tr("Database user name"));
80 allOptions.add("p");
81 allOptions.add("password", tr("Prompt for password"));
82 allOptions.add("h");
83 allOptions.add("host <name>", tr("Host (server) name"));
84 allOptions.add("port <number>", tr("Server's port number"));
85 allOptions.add("s");
86 allOptions.add("local-socket <filename>", tr("Server's local socket filename"));
87
88 // add user options
89 allOptions.add(options);
90
91 KCmdLineArgs::addCmdLineOptions(allOptions);
92 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
93
94 d->connData.driverId = args->getOption("driver");
95 d->connData.userName = args->getOption("user");
96 d->connData.hostName = args->getOption("host");
97 d->connData.localSocketFileName = args->getOption("local-socket");
98 d->connData.port = args->getOption("port").toInt();
99 d->connData.useLocalSocketFile = args->isSet("local-socket");
100
101 if (args->isSet("password")) {
102 QString userAtHost = d->connData.userName;
103 if (!d->connData.userName.isEmpty())
104 userAtHost += '@';
105 userAtHost += (d->connData.hostName.isEmpty() ? "localhost" : d->connData.hostName);
106 QTextStream cout(stdout, IO_WriteOnly);
107 cout << tr("Enter password for %1: ", "Enter password for <user>").arg(userAtHost);
108//! @todo make use of pty/tty here! (and care about portability)
110 cin >> d->connData.password;
111 kdbDebug() << d->connData.password;
112 }
113}
114
115KDbSimpleCommandLineApp::~KDbSimpleCommandLineApp()
116{
118 delete d;
119}
120
122{
123 if (!d->conn) {
124 if (d->manager.error()) {
125 setError(&d->manager);
126 return false;
127 }
128
129 //get the driver
130 KDbDriver *driver = d->manager.driver(d->connData.driverId);
131 if (!driver || d->manager.error()) {
132 setError(&d->manager);
133 return false;
134 }
135
136 if (driver->metaData()->isFileBased())
137 d->connData.setFileName(databaseName);
138
139 d->conn = driver->createConnection(&d->connData);
140 if (!d->conn || driver->error()) {
141 setError(driver);
142 return false;
143 }
144 }
145 if (d->conn->isConnected()) {
146 // db already opened
147 if (d->conn->isDatabaseUsed() && d->conn->currentDatabase() == databaseName) //the same: do nothing
148 return true;
149 if (!closeDatabase()) // differs: close the first
150 return false;
151 }
152 if (!d->conn->connect()) {
153 setError(d->conn);
154 delete d->conn;
155 d->conn = 0;
156 return false;
157 }
158
159 if (!d->conn->useDatabase(databaseName)) {
160 setError(d->conn);
161 delete d->conn;
162 d->conn = 0;
163 return false;
164 }
165 return true;
166}
167
169{
170 if (!d->conn)
171 return true;
172 if (!d->conn->disconnect()) {
173 setError(d->conn);
174 return false;
175 }
176 return true;
177}
178
179const KComponentData &KDbSimpleCommandLineApp::componentData() const
180{
181 return d->componentData;
182}
183
185{
186 return &d->connData;
187}
188
190{
191 return d->conn;
192}
Database specific connection data, e.g. host, port.
Provides database connection, allowing queries and data modification.
A driver manager for finding and loading driver plugins.
KDbDriver * driver(const QString &id)
Database driver's abstraction.
Definition KDbDriver.h:50
KDbConnection * createConnection(const KDbConnectionData &connData, const KDbConnectionOptions &options)
const KDbDriverMetaData * metaData() const
A skeleton for creating a simple command line database application.
bool openDatabase(const QString &databaseName)
KDbConnectionData * connectionData() const
const KComponentData & componentData() const
KDbSimpleCommandLineApp(int argc, char **argv, const KCmdLineOptions &options, const char *programName, const char *version, const char *shortDescription=0, KAboutData::LicenseKey licenseType=KAboutData::License_Unknown, const char *copyrightStatement=0, const char *text=0, const char *homePageAddress=0, const char *bugsEmailAddress="submit@bugs.kde.org")
KDbConnection * connection() const
KDB_EXPORT QString defaultFileBasedDriverId()
QString baseName() const const
bool isEmpty() const const
QByteArray toLatin1() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:38:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.