summary refs log tree commit diff stats
path: root/lib/database.cpp
blob: fb00ef367fd289cf590825aa03c86a1913d2d089 (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
#include "database.h"
#include <sqlite3.h>
#include <stdexcept>
#include "query.h"

namespace verbly {

  database::database(std::string path)
  {
    if (sqlite3_open_v2(path.c_str(), &ppdb_, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK)
    {
      // We still have to free the resources allocated. In the event that
      // allocation failed, ppdb will be null and sqlite3_close_v2 will just
      // ignore it.
      std::string errmsg(sqlite3_errmsg(ppdb_));
      sqlite3_close_v2(ppdb_);

      throw database_error("Could not open verbly datafile", errmsg);
    }
  }

  database::database(database&& other) : database()
  {
    swap(*this, other);
  }

  database& database::operator=(database&& other)
  {
    swap(*this, other);

    return *this;
  }

  void swap(database& first, database& second)
  {
    std::swap(first.ppdb_, second.ppdb_);
  }

  database::~database()
  {
    sqlite3_close_v2(ppdb_);
  }

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

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

  query<group> database::groups(filter where, bool random, int limit) const
  {
    return query<group>(*this, ppdb_, std::move(where), random, limit);
  }

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

  query<lemma> database::lemmas(filter where, bool random, int limit) const
  {
    return query<lemma>(*this, ppdb_, std::move(where), random, limit);
  }

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

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

};