summary refs log tree commit diff stats
path: root/lib/database.cpp
blob: d5ff37e14d0fffff626020f3adacdc20bd3a2cb7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "database.h"
#include <sstream>
#include "query.h"
#include "version.h"

namespace verbly {

  database::database(
    std::string path) :
      ppdb_(std::move(path), hatkirby::dbmode::read)
  {
    hatkirby::row version =
      ppdb_.queryFirst("SELECT major, minor FROM version");

    major_ = std::get<int>(version[0]);
    minor_ = std::get<int>(version[1]);

    if (major_ != DATABASE_MAJOR_VERSION)
    {
      throw database_version_mismatch(DATABASE_MAJOR_VERSION, major_);
    }
  }

  query<notion> database::notions(filter where, order sortOrder, int limit) const
  {
    return query<notion>(*this, ppdb_, std::move(where), std::move(sortOrder), limit);
  }

  query<word> database::words(filter where, order sortOrder, int limit) const
  {
    return query<word>(*this, ppdb_, std::move(where), std::move(sortOrder), limit);
  }

  query<frame> database::frames(filter where, order sortOrder, int limit) const
  {
    return query<frame>(*this, ppdb_, std::move(where), std::move(sortOrder), limit);
  }

  query<part> database::parts(filter where, order sortOrder, int limit) const
  {
    return query<part>(*this, ppdb_, std::move(where), std::move(sortOrder), limit);
  }

  query<form> database::forms(filter where, order sortOrder, int limit) const
  {
    return query<form>(*this, ppdb_, std::move(where), std::move(sortOrder), limit);
  }

  query<pronunciation> database::pronunciations(filter where, order sortOrder, int limit) const
  {
    return query<pronunciation>(*this, ppdb_, std::move(where), std::move(sortOrder), limit);
  }

  std::set<std::string> database::selrestrs(int partId) const
  {
    std::vector<hatkirby::row> rows =
      ppdb_.queryAll(
        "SELECT selrestr FROM selrestrs WHERE part_id = ?",
        { partId });

    std::set<std::string> result;

    for (hatkirby::row& r : rows)
    {
      result.emplace(std::move(std::get<std::string>(r[0])));
    }

    return result;
  }

  std::set<std::string> database::synrestrs(int partId) const
  {
    std::vector<hatkirby::row> rows =
      ppdb_.queryAll(
        "SELECT synrestr FROM synrestrs WHERE part_id = ?",
        { partId });

    std::set<std::string> result;

    for (hatkirby::row& r : rows)
    {
      result.emplace(std::move(std::get<std::string>(r[0])));
    }

    return result;
  }

  std::string database_version_mismatch::generateMessage(int right, int wrong)
  {
    std::ostringstream msgbuilder;
    msgbuilder << "Incompatible database version: needed ";
    msgbuilder << right;
    msgbuilder << ", found ";
    msgbuilder << wrong;

    return msgbuilder.str();
  }

};