KDb

KDbSimpleCommandLineApp.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2006 JarosÅ‚aw Staniek <[email protected]>
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
33 class Q_DECL_HIDDEN KDbSimpleCommandLineApp::Private
34 {
35 public:
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;
49 private:
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]);
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 
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)
109  QTextStream cin(stdin, QIODevice::ReadOnly);
110  cin >> d->connData.password;
111  kdbDebug() << d->connData.password;
112  }
113 }
114 
115 KDbSimpleCommandLineApp::~KDbSimpleCommandLineApp()
116 {
117  closeDatabase();
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 
180 {
181  return d->componentData;
182 }
183 
185 {
186  return &d->connData;
187 }
188 
190 {
191  return d->conn;
192 }
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="[email protected]")
static void addCmdLineOptions(const KCmdLineOptions &options, const KLocalizedString &name=KLocalizedString(), const QByteArray &id=QByteArray(), const QByteArray &afterId=QByteArray())
QString getOption(const QByteArray &option) const
static KCmdLineArgs * parsedArgs(const QByteArray &id=QByteArray())
Database driver's abstraction.
Definition: KDbDriver.h:49
bool isSet(const QByteArray &option) const
QByteArray toLatin1() const const
static void init(const K4AboutData *about)
KDbConnection * connection() const
KCmdLineOptions & add(const KCmdLineOptions &options)
KDbConnection * createConnection(const KDbConnectionData &connData, const KDbConnectionOptions &options)
Definition: KDbDriver.cpp:137
QString baseName() const const
A skeleton for creating a simple command line database application.
QCA_EXPORT QString appName()
int toInt(bool *ok, int base) const const
KDB_EXPORT QString defaultFileBasedDriverId()
Definition: KDb.cpp:1975
bool openDatabase(const QString &databaseName)
unsigned int version()
Definition: KDb.cpp:336
const KComponentData & componentData() const
Database specific connection data, e.g. host, port.
A driver manager for finding and loading driver plugins.
Provides database connection, allowing queries and data modification.
Definition: KDbConnection.h:51
const KDbDriverMetaData * metaData() const
Definition: KDbDriver.cpp:101
KDbConnectionData * connectionData() const
bool isFileBased() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jun 25 2022 06:21:33 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.