create all needed tables
This commit is contained in:
parent
eeba4b11e0
commit
1778f48bc6
1 changed files with 23 additions and 5 deletions
|
@ -1,8 +1,8 @@
|
|||
#include "database.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
Database::Database(const std::string& dbname) : db_(nullptr)
|
||||
{
|
||||
|
@ -30,9 +30,12 @@ void Database::exec(const std::string& sql)
|
|||
|
||||
void Database::createNew()
|
||||
{
|
||||
std::vector<std::string> sqlStrings{};
|
||||
|
||||
std::string sqlCreateKima2{"CREATE TABLE IF NOT EXISTS kima2 ("
|
||||
"version INTEGER NOT NULL);"
|
||||
"INSERT INTO kima2 (version) VALUES (1);"};
|
||||
sqlStrings.push_back(sqlCreateKima2);
|
||||
std::string sqlCreateSellers{"CREATE TABLE IF NOT EXISTS sellers ("
|
||||
"id TEXT PRIMARY KEY NOT NULL, "
|
||||
"seller_no INTEGER, "
|
||||
|
@ -41,7 +44,7 @@ void Database::createNew()
|
|||
"offered_articles INTEGER, "
|
||||
"UNIQUE (seller_no)"
|
||||
");"};
|
||||
|
||||
sqlStrings.push_back(sqlCreateSellers);
|
||||
std::string sqlCreateArticles{
|
||||
"CREATE TABLE IF NOT EXISTS articles ("
|
||||
"id TEXT PRIMARY KEY NOT NULL, "
|
||||
|
@ -54,11 +57,26 @@ void Database::createNew()
|
|||
"FOREIGN KEY (seller_id) REFERENCES sellers(id) ON DELETE CASCADE, "
|
||||
"CHECK (article_no BETWEEN 0 AND 99999)"
|
||||
");"};
|
||||
sqlStrings.push_back(sqlCreateArticles);
|
||||
std::string sqlCreateSales{"CREATE TABLE IF NOT EXISTS sales ("
|
||||
" id TEXT PRIMARY KEY NOT NULL,"
|
||||
" source_no INTEGER NOT NULL,"
|
||||
" sold_at TEXT"
|
||||
");"};
|
||||
sqlStrings.push_back(sqlCreateSales);
|
||||
std::string sqlCreateSalesItems{
|
||||
"CREATE TABLE IF NOT EXISTS sales_items("
|
||||
" sale_id TEXT NOT NULL,"
|
||||
" article_id TEXT NOT NULL,"
|
||||
" FOREIGN KEY (sale_id) REFERENCES sales(id) ON DELETE CASCADE,"
|
||||
" FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE"
|
||||
");"};
|
||||
sqlStrings.push_back(sqlCreateSalesItems);
|
||||
|
||||
beginTransaction();
|
||||
exec(sqlCreateKima2);
|
||||
exec(sqlCreateSellers);
|
||||
exec(sqlCreateArticles);
|
||||
for (const auto& sql : sqlStrings) {
|
||||
exec(sql);
|
||||
}
|
||||
endTransaction();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue