File indexing completed on 2024-12-22 03:46:48

0001 /****************************************************************************
0002 **
0003 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
0004 ** Contact: Qt Software Information (qt-info@nokia.com)
0005 **
0006 ** This file is part of the demonstration applications of the Qt Toolkit.
0007 **
0008 ** $QT_BEGIN_LICENSE:LGPL$
0009 ** No Commercial Usage
0010 ** This file contains pre-release code and may not be distributed.
0011 ** You may use this file in accordance with the terms and conditions
0012 ** contained in the either Technology Preview License Agreement or the
0013 ** Beta Release License Agreement.
0014 **
0015 ** GNU Lesser General Public License Usage
0016 ** Alternatively, this file may be used under the terms of the GNU Lesser
0017 ** General Public License version 2.1 as published by the Free Software
0018 ** Foundation and appearing in the file LICENSE.LGPL included in the
0019 ** packaging of this file.  Please review the following information to
0020 ** ensure the GNU Lesser General Public License version 2.1 requirements
0021 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
0022 **
0023 ** In addition, as a special exception, Nokia gives you certain
0024 ** additional rights. These rights are described in the Nokia Qt LGPL
0025 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
0026 ** package.
0027 **
0028 ** GNU General Public License Usage
0029 ** Alternatively, this file may be used under the terms of the GNU
0030 ** General Public License version 3.0 as published by the Free Software
0031 ** Foundation and appearing in the file LICENSE.GPL included in the
0032 ** packaging of this file.  Please review the following information to
0033 ** ensure the GNU General Public License version 3.0 requirements will be
0034 ** met: http://www.gnu.org/copyleft/gpl.html.
0035 **
0036 ** If you are unsure which license is appropriate for your use, please
0037 ** contact the sales department at qt-sales@nokia.com.
0038 ** $QT_END_LICENSE$
0039 **
0040 ****************************************************************************/
0041 
0042 #ifndef INITDB_H
0043 #define INITDB_H
0044 
0045 #include <QtSql>
0046 
0047 void addBook(QSqlQuery &q, const QString &title, int year, const QVariant &authorId, const QVariant &genreId, int rating)
0048 {
0049     q.addBindValue(title);
0050     q.addBindValue(year);
0051     q.addBindValue(authorId);
0052     q.addBindValue(genreId);
0053     q.addBindValue(rating);
0054     q.exec();
0055 }
0056 
0057 QVariant addGenre(QSqlQuery &q, const QString &name)
0058 {
0059     q.addBindValue(name);
0060     q.exec();
0061     return q.lastInsertId();
0062 }
0063 
0064 QVariant addAuthor(QSqlQuery &q, const QString &name, const QDate &birthdate)
0065 {
0066     q.addBindValue(name);
0067     q.addBindValue(birthdate);
0068     q.exec();
0069     return q.lastInsertId();
0070 }
0071 
0072 QSqlError initDb()
0073 {
0074     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
0075     db.setDatabaseName(":memory:");
0076 
0077     if (!db.open())
0078         return db.lastError();
0079 
0080     QStringList tables = db.tables();
0081     if (tables.contains("books", Qt::CaseInsensitive) && tables.contains("authors", Qt::CaseInsensitive))
0082         return QSqlError();
0083 
0084     QSqlQuery q;
0085     if (!q.exec(QStringLiteral("create table books(id integer primary key, title varchar, author integer, genre integer, year integer, rating integer)")))
0086         return q.lastError();
0087     if (!q.exec(QStringLiteral("create table authors(id integer primary key, name varchar, birthdate date)")))
0088         return q.lastError();
0089     if (!q.exec(QStringLiteral("create table genres(id integer primary key, name varchar)")))
0090         return q.lastError();
0091 
0092     if (!q.prepare(QStringLiteral("insert into authors(name, birthdate) values(?, ?)")))
0093         return q.lastError();
0094     QVariant asimovId = addAuthor(q, QStringLiteral("Isaac Asimov"), QDate(1920, 2, 1));
0095     QVariant greeneId = addAuthor(q, QStringLiteral("Graham Greene"), QDate(1904, 10, 2));
0096     QVariant pratchettId = addAuthor(q, QStringLiteral("Terry Pratchett"), QDate(1948, 4, 28));
0097 
0098     if (!q.prepare(QStringLiteral("insert into genres(name) values(?)")))
0099         return q.lastError();
0100     QVariant sfiction = addGenre(q, QStringLiteral("Science Fiction"));
0101     QVariant fiction = addGenre(q, QStringLiteral("Fiction"));
0102     QVariant fantasy = addGenre(q, QStringLiteral("Fantasy"));
0103 
0104     if (!q.prepare(QStringLiteral("insert into books(title, year, author, genre, rating) values(?, ?, ?, ?, ?)")))
0105         return q.lastError();
0106     addBook(q, QStringLiteral("Foundation"), 1951, asimovId, sfiction, 3);
0107     addBook(q, QStringLiteral("Foundation and Empire"), 1952, asimovId, sfiction, 4);
0108     addBook(q, QStringLiteral("Second Foundation"), 1953, asimovId, sfiction, 3);
0109     addBook(q, QStringLiteral("Foundation's Edge"), 1982, asimovId, sfiction, 3);
0110     addBook(q, QStringLiteral("Foundation and Earth"), 1986, asimovId, sfiction, 4);
0111     addBook(q, QStringLiteral("Prelude to Foundation"), 1988, asimovId, sfiction, 3);
0112     addBook(q, QStringLiteral("Forward the Foundation"), 1993, asimovId, sfiction, 3);
0113     addBook(q, QStringLiteral("The Power and the Glory"), 1940, greeneId, fiction, 4);
0114     addBook(q, QStringLiteral("The Third Man"), 1950, greeneId, fiction, 5);
0115     addBook(q, QStringLiteral("Our Man in Havana"), 1958, greeneId, fiction, 4);
0116     addBook(q, QStringLiteral("Guards! Guards!"), 1989, pratchettId, fantasy, 3);
0117     addBook(q, QStringLiteral("Night Watch"), 2002, pratchettId, fantasy, 3);
0118     addBook(q, QStringLiteral("Going Postal"), 2004, pratchettId, fantasy, 3);
0119 
0120     return QSqlError();
0121 }
0122 
0123 #endif