2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2014-08-21 13:51:22 +00:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-09-19 12:28:29 +00:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the demonstration applications of the Qt Toolkit.
|
|
|
|
**
|
2014-08-21 13:51:22 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL21$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2014-08-21 13:51:22 +00:00
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
2012-09-19 12:28:29 +00:00
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-08-21 13:51:22 +00:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
2014-08-21 13:51:22 +00:00
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2011-04-27 10:05:43 +00:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef INITDB_H
|
|
|
|
#define INITDB_H
|
|
|
|
|
|
|
|
#include <QtSql>
|
|
|
|
|
|
|
|
void addBook(QSqlQuery &q, const QString &title, int year, const QVariant &authorId,
|
|
|
|
const QVariant &genreId, int rating)
|
|
|
|
{
|
|
|
|
q.addBindValue(title);
|
|
|
|
q.addBindValue(year);
|
|
|
|
q.addBindValue(authorId);
|
|
|
|
q.addBindValue(genreId);
|
|
|
|
q.addBindValue(rating);
|
|
|
|
q.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant addGenre(QSqlQuery &q, const QString &name)
|
|
|
|
{
|
|
|
|
q.addBindValue(name);
|
|
|
|
q.exec();
|
|
|
|
return q.lastInsertId();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant addAuthor(QSqlQuery &q, const QString &name, const QDate &birthdate)
|
|
|
|
{
|
|
|
|
q.addBindValue(name);
|
|
|
|
q.addBindValue(birthdate);
|
|
|
|
q.exec();
|
|
|
|
return q.lastInsertId();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSqlError initDb()
|
|
|
|
{
|
|
|
|
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
|
|
|
|
db.setDatabaseName(":memory:");
|
|
|
|
|
|
|
|
if (!db.open())
|
|
|
|
return db.lastError();
|
|
|
|
|
|
|
|
QStringList tables = db.tables();
|
|
|
|
if (tables.contains("books", Qt::CaseInsensitive)
|
|
|
|
&& tables.contains("authors", Qt::CaseInsensitive))
|
|
|
|
return QSqlError();
|
|
|
|
|
|
|
|
QSqlQuery q;
|
|
|
|
if (!q.exec(QLatin1String("create table books(id integer primary key, title varchar, author integer, genre integer, year integer, rating integer)")))
|
|
|
|
return q.lastError();
|
|
|
|
if (!q.exec(QLatin1String("create table authors(id integer primary key, name varchar, birthdate date)")))
|
|
|
|
return q.lastError();
|
|
|
|
if (!q.exec(QLatin1String("create table genres(id integer primary key, name varchar)")))
|
|
|
|
return q.lastError();
|
|
|
|
|
|
|
|
if (!q.prepare(QLatin1String("insert into authors(name, birthdate) values(?, ?)")))
|
|
|
|
return q.lastError();
|
|
|
|
QVariant asimovId = addAuthor(q, QLatin1String("Isaac Asimov"), QDate(1920, 2, 1));
|
|
|
|
QVariant greeneId = addAuthor(q, QLatin1String("Graham Greene"), QDate(1904, 10, 2));
|
|
|
|
QVariant pratchettId = addAuthor(q, QLatin1String("Terry Pratchett"), QDate(1948, 4, 28));
|
|
|
|
|
|
|
|
if (!q.prepare(QLatin1String("insert into genres(name) values(?)")))
|
|
|
|
return q.lastError();
|
|
|
|
QVariant sfiction = addGenre(q, QLatin1String("Science Fiction"));
|
|
|
|
QVariant fiction = addGenre(q, QLatin1String("Fiction"));
|
|
|
|
QVariant fantasy = addGenre(q, QLatin1String("Fantasy"));
|
|
|
|
|
|
|
|
if (!q.prepare(QLatin1String("insert into books(title, year, author, genre, rating) values(?, ?, ?, ?, ?)")))
|
|
|
|
return q.lastError();
|
|
|
|
addBook(q, QLatin1String("Foundation"), 1951, asimovId, sfiction, 3);
|
|
|
|
addBook(q, QLatin1String("Foundation and Empire"), 1952, asimovId, sfiction, 4);
|
|
|
|
addBook(q, QLatin1String("Second Foundation"), 1953, asimovId, sfiction, 3);
|
|
|
|
addBook(q, QLatin1String("Foundation's Edge"), 1982, asimovId, sfiction, 3);
|
|
|
|
addBook(q, QLatin1String("Foundation and Earth"), 1986, asimovId, sfiction, 4);
|
|
|
|
addBook(q, QLatin1String("Prelude to Foundation"), 1988, asimovId, sfiction, 3);
|
|
|
|
addBook(q, QLatin1String("Forward the Foundation"), 1993, asimovId, sfiction, 3);
|
|
|
|
addBook(q, QLatin1String("The Power and the Glory"), 1940, greeneId, fiction, 4);
|
|
|
|
addBook(q, QLatin1String("The Third Man"), 1950, greeneId, fiction, 5);
|
|
|
|
addBook(q, QLatin1String("Our Man in Havana"), 1958, greeneId, fiction, 4);
|
|
|
|
addBook(q, QLatin1String("Guards! Guards!"), 1989, pratchettId, fantasy, 3);
|
|
|
|
addBook(q, QLatin1String("Night Watch"), 2002, pratchettId, fantasy, 3);
|
|
|
|
addBook(q, QLatin1String("Going Postal"), 2004, pratchettId, fantasy, 3);
|
|
|
|
|
|
|
|
return QSqlError();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|