File indexing completed on 2024-03-24 16:11:22

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003-2010 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include <QDebug>
0021 #include <kcomponentdata.h>
0022 
0023 #include <KDbDriverManager>
0024 #include <KDbDriver>
0025 #include <KDbConnection>
0026 #include <KDbCursor>
0027 
0028 int main(int argc, char * argv[])
0029 {
0030     KComponentData componentData("newapi");
0031     KDbDriverManager manager;
0032     QStringList driverIds = manager.driverIds();
0033     qDebug() << "DRIVERS: ";
0034     for (QStringList::ConstIterator it = driverIds.constBegin(); it != driverIds.constEnd() ; ++it)
0035         qDebug() << *it;
0036     if (manager.error()) {
0037         qDebug() << manager.errorMsg();
0038         return 1;
0039     }
0040 
0041     //get driver
0042     KDbDriver *driver = manager.driver("mySQL");
0043     if (manager.error()) {
0044         qDebug() << manager.errorMsg();
0045         return 1;
0046     }
0047 
0048     //connection data that can be later reused
0049     KDbConnectionData conn_data;
0050 
0051     conn_data.userName = "root";
0052     if (argc > 1)
0053         conn_data.password = argv[1];
0054     else
0055         conn_data.password = "mysql";
0056     conn_data.hostName = "localhost";
0057 
0058     KDbConnection *conn = driver->createConnection(conn_data);
0059     if (driver->error()) {
0060         qDebug() << driver->errorMsg();
0061         return 1;
0062     }
0063     if (!conn->connect()) {
0064         qDebug() << conn->errorMsg();
0065         return 1;
0066     }
0067 
0068     if (!conn->useDatabase("test")) {
0069         qDebug() << "use db:" << conn->errorMsg();
0070         return 1;
0071     }
0072 
0073     qDebug() << "Creating first cursor";
0074     KDbCursor *c = conn->executeQuery("select * from Applications");
0075     if (!c) qDebug() << conn->errorMsg();
0076     qDebug() << "Creating second cursor";
0077     KDbCursor *c2 = conn->executeQuery("select * from Applications");
0078     if (!c2) qDebug() << conn->errorMsg();
0079 
0080     QStringList l = conn->databaseNames();
0081     if (l.isEmpty()) qDebug() << conn->errorMsg();
0082     qDebug() << "Databases:";
0083     for (QStringList::ConstIterator it = l.constBegin(); it != l.constEnd() ; ++it)
0084         qDebug() << *it;
0085 
0086     if (c) {
0087         while (c->moveNext()) {
0088             qDebug() << "KDbCursor: Value(0)" << c->value(0).toString();
0089             qDebug() << "KDbCursor: Value(1)" << c->value(1).toString();
0090         }
0091         qDebug() << "KDbCursor error:" << c->errorMsg();
0092     }
0093     if (c2) {
0094         while (c2->moveNext()) {
0095             qDebug() << "Cursor2: Value(0)" << c2->value(0).toString();
0096             qDebug() << "Cursor2: Value(1)" << c2->value(1).toString();
0097         }
0098     }
0099     if (c) {
0100         qDebug() << "KDbCursor::prev";
0101         while (c->movePrev()) {
0102             qDebug() << "KDbCursor: Value(0)" << c->value(0).toString();
0103             qDebug() << "KDbCursor: Value(1)" << c->value(1).toString();
0104 
0105         }
0106         qDebug() << "up/down";
0107         c->moveNext();
0108         qDebug() << "KDbCursor: Value(0)" << c->value(0).toString();
0109         qDebug() << "KDbCursor: Value(1)" << c->value(1).toString();
0110         c->moveNext();
0111         qDebug() << "KDbCursor: Value(0)" << c->value(0).toString();
0112         qDebug() << "KDbCursor: Value(1)" << c->value(1).toString();
0113         c->movePrev();
0114         qDebug() << "KDbCursor: Value(0)" << c->value(0).toString();
0115         qDebug() << "KDbCursor: Value(1)" << c->value(1).toString();
0116         c->movePrev();
0117         qDebug() << "KDbCursor: Value(0)" << c->value(0).toString();
0118         qDebug() << "KDbCursor: Value(1)" << c->value(1).toString();
0119 
0120     }
0121 #if 0
0122     KDbTable *t = conn->tableSchema("persons");
0123     if (t)
0124         t->debug();
0125     t = conn->tableSchema("cars");
0126     if (t)
0127         t->debug();
0128 
0129 // conn->tableNames();
0130 
0131     if (!conn->disconnect()) {
0132         qDebug() << conn->errorMsg();
0133         return 1;
0134     }
0135     debug("before del");
0136     delete conn;
0137     debug("after del");
0138 #endif
0139     return 0;
0140 }