#include "dbunit.h" #include #include #include DBUnit::DBUnit() { db_ins = QSqlDatabase::addDatabase("QSQLITE", "datas"); db_ins.setDatabaseName("./core.db"); if (!db_ins.open()) qDebug() << db_ins.lastError(); init_basic_tables(db_ins); } QSqlQuery DBUnit::getQuery() const { return QSqlQuery(db_ins); } void DBUnit::init_basic_tables(QSqlDatabase& db_ins) { QSqlQuery q(db_ins); q.prepare("create table if not exists country_information( " "cid integer primary key autoincrement, " "name text not null, describe text)"); if (!q.exec()) { qDebug() << q.lastError(); return; } q.prepare("create table if not exists publishing_information( " "pid integer primary key autoincrement, name text not null, " "country integer not null, describe text, " "constraint fk_pi foreign key (country) references country_information(cid) " "on update cascade on delete cascade)"); if (!q.exec()) { qDebug() << q.lastError(); return; } q.prepare("create table if not exists authors_information( " "author_id integer primary key autoincrement, name text not null, " "country integer not null, describe text, " "constraint fk_ai foreign key (country) references country_information(cid) " "on delete cascade on update cascade)"); if (!q.exec()) { qDebug() << q.lastError(); return; } q.prepare("create table if not exists books_information(" "book_id integer primary key autoincrement,isbn text not null," "name text not null,publishing integer not null,author integer not null," "author2 integer not null,language text,time text,abstract text,image text," "constraint fk_bi0 foreign key (publishing) references publishing_information on delete cascade on update cascade," "constraint fk_bi1 foreign key (author) references authors_information on delete cascade on update cascade," "constraint fk_bi1 foreign key (author2) references authors_information on delete cascade on update cascade)"); if (!q.exec()) { qDebug() << q.lastError(); return; } q.prepare("create table if not exists groups_records(" "record_id integer primary key autoincrement," "name text not null)"); if (!q.exec()) { qDebug() << q.lastError(); return; } q.prepare("create table if not exists labels_records( " "record_id integer primary key autoincrement, " "name text not null, describe text)"); if (!q.exec()) { qDebug() << q.lastError(); return; } q.prepare("create table if not exists books_manage(" "record_id integer primary key autoincrement," "book integer not null,`group` integer not null," "constraint fk_bm0 foreign key (book) references books_information(record_id) on delete cascade on update cascade," "constraint fk_bm1 foreign key (`group`) references groups_records(record_id) on delete cascade on update cascade)"); if (!q.exec()) { qDebug() << q.lastError(); return; } q.prepare("create table if not exists book_labels_manage( " "record_id integer primary key autoincrement, " "book integer not null,label integer not null, " "constraint fk_blm0 foreign key (book) references books_information(book_id) on delete cascade on update cascade," "constraint fk_blm1 foreign key (label) references labels_records(record_id) on delete cascade on update cascade)"); if (!q.exec()) { qDebug() << q.lastError(); return; } }