File indexing completed on 2024-04-21 15:30:21

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003-2004 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 #ifndef TABLETEST_P_H
0021 #define TABLETEST_P_H
0022 
0023 int tablesTest_createTables(KDbConnection *conn)
0024 {
0025     conn->setAutoCommit(false);
0026     KDbTransaction t = conn->beginTransaction();
0027     if (conn->result().isError()) {
0028         qDebug() << conn->result();
0029         return 1;
0030     }
0031 
0032     //now: lets create tables:
0033     KDbField *f;
0034     KDbTableSchema *t_persons = new KDbTableSchema("persons");
0035     t_persons->setCaption("Persons in our factory");
0036     t_persons->addField(f = new KDbField("id", KDbField::Integer, KDbField::PrimaryKey | KDbField::AutoInc, KDbField::Unsigned));
0037     f->setCaption("ID");
0038     t_persons->addField(f = new KDbField("age", KDbField::Integer, nullptr, KDbField::Unsigned));
0039     f->setCaption("Age");
0040     t_persons->addField(f = new KDbField("name", KDbField::Text));
0041     f->setCaption("Name");
0042     t_persons->addField(f = new KDbField("surname", KDbField::Text));
0043     f->setCaption("Surname");
0044     if (!conn->createTable(t_persons)) {
0045         qDebug() << conn->result();
0046         return 1;
0047     }
0048     qDebug() << "-- PERSONS created --";
0049     qDebug() << *t_persons;
0050 
0051     if (!conn->insertRecord(t_persons, QVariant(1), QVariant(27), QVariant("Jaroslaw"), QVariant("Staniek"))
0052             || !conn->insertRecord(t_persons, QVariant(2), QVariant(60), QVariant("Lech"), QVariant("Walesa"))
0053             || !conn->insertRecord(t_persons, QVariant(3), QVariant(45), QVariant("Bill"), QVariant("Gates"))
0054             || !conn->insertRecord(t_persons, QVariant(4), QVariant(35), QVariant("John"), QVariant("Smith"))
0055        ) {
0056         qDebug() << "-- PERSONS data err. --";
0057         return 1;
0058     }
0059     qDebug() << "-- PERSONS data created --";
0060 
0061 
0062     KDbTableSchema *t_cars = new KDbTableSchema("cars");
0063     t_cars->setCaption("Cars owned by persons");
0064     t_cars->addField(f = new KDbField("id", KDbField::Integer, KDbField::PrimaryKey | KDbField::AutoInc, KDbField::Unsigned));
0065     f->setCaption("ID");
0066     t_cars->addField(f = new KDbField("owner", KDbField::Integer, nullptr, KDbField::Unsigned));
0067     f->setCaption("Car owner");
0068     t_cars->addField(f = new KDbField("model", KDbField::Text));
0069     f->setCaption("Car model");
0070     if (!conn->createTable(t_cars)) {
0071         qDebug() << conn->result();
0072         return 1;
0073     }
0074     qDebug() << "-- CARS created --";
0075     if (!conn->insertRecord(t_cars, QVariant(1), QVariant(1), QVariant("Fiat"))
0076             || !conn->insertRecord(t_cars, QVariant(2), QVariant(2), QVariant("Syrena"))
0077             || !conn->insertRecord(t_cars, QVariant(3), QVariant(3), QVariant("Chrysler"))
0078             || !conn->insertRecord(t_cars, QVariant(4), QVariant(3), QVariant("BMW"))
0079             || !conn->insertRecord(t_cars, QVariant(5), QVariant(4), QVariant("Volvo"))
0080        )
0081     {
0082         qDebug() << "-- CARS data err. --";
0083         return 1;
0084     }
0085     qDebug() << "-- CARS data created --";
0086 
0087     if (!conn->commitTransaction(t)) {
0088         qDebug() << conn->result();
0089         return 1;
0090     }
0091 
0092     qDebug() << "NOW, TABLE LIST: ";
0093     QStringList tnames = conn->tableNames();
0094     for (QStringList::iterator it = tnames.begin(); it != tnames.end(); ++it) {
0095         qDebug() << " - " << (*it);
0096     }
0097     return 0;
0098 }
0099 
0100 #endif