aboutsummaryrefslogtreecommitdiff
path: root/libbrep/build-package.hxx
blob: 0d6b5bf5d2fe2abc07ed832528d75445f5bf5115 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// file      : libbrep/build-package.hxx -*- C++ -*-
// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef LIBBREP_BUILD_PACKAGE_HXX
#define LIBBREP_BUILD_PACKAGE_HXX

#include <odb/core.hxx>

#include <libbrep/types.hxx>
#include <libbrep/utility.hxx>

#include <libbrep/common.hxx> // Must be included last (see assert).

namespace brep
{
  // These are "foreign objects" that are mapped to subsets of the package
  // database objects using the PostgreSQL foreign table mechanism. Note that
  // since we maintain the pair in sync by hand, we should only have a minimal
  // subset of "core" members (ideally just the primary key) that are unlikly
  // to disappear or change.
  //
  // The mapping is established in build-extra.sql. We also explicitly mark
  // non-primary key foreign-mapped members in the source object.
  //
  // Foreign object that is mapped to a subset of the tenant object.
  //
  #pragma db object table("build_tenant") pointer(shared_ptr) readonly
  class build_tenant
  {
  public:
    string id;

    bool archived;

    // Database mapping.
    //
    #pragma db member(id) id

  private:
    friend class odb::access;
    build_tenant () = default;
  };

  // Foreign object that is mapped to a subset of the repository object.
  //
  #pragma db object table("build_repository") pointer(shared_ptr) readonly
  class build_repository
  {
  public:
    repository_id id;

    const string& canonical_name;             // Tracks id.canonical_name.
    repository_location location;
    optional<string> certificate_fingerprint;

    // Database mapping.
    //
    #pragma db member(id) id column("")

    #pragma db member(canonical_name) transient

    #pragma db member(location)                                            \
      set(this.location = std::move (?);                                   \
          assert (this.canonical_name == this.location.canonical_name ()))

  private:
    friend class odb::access;
    build_repository (): canonical_name (id.canonical_name) {}
  };

  // "Foreign" value type that is mapped to a subset of the build_constraint
  // value type (see libbpkg/manifest.hxx for details).
  //
  #pragma db value
  struct build_constraint_subset
  {
    bool exclusion;
    string config;
    optional<string> target;
  };

  // Foreign object that is mapped to a subset of the package object.
  //
  #pragma db object table("build_package") pointer(shared_ptr) readonly
  class build_package
  {
  public:
    package_id id;
    upstream_version version;
    lazy_shared_ptr<build_repository> internal_repository;

    // Mapped to a subset of the package object build_constraints member
    // using the PostgreSQL foreign table mechanism.
    //
    vector<build_constraint_subset> constraints;

    // Database mapping.
    //
    #pragma db member(id) id column("")
    #pragma db member(version) set(this.version.init (this.id.version, (?)))
    #pragma db member(constraints) id_column("") value_column("")

  private:
    friend class odb::access;
    build_package () = default;
  };

  // Packages that can potentially be built (internal non-stub).
  //
  // Note that ADL can't find the equal operator, so we use the function call
  // notation.
  //
  #pragma db view                                                      \
    object(build_package)                                              \
    object(build_repository inner:                                     \
           brep::operator== (build_package::internal_repository,       \
                             build_repository::id) &&                  \
           brep::compare_version_ne (build_package::id.version,        \
                                     brep::wildcard_version,           \
                                     false))                           \
    object(build_tenant: build_package::id.tenant == build_tenant::id)
  struct buildable_package
  {
    package_id id;
    upstream_version version;

    // Database mapping.
    //
    #pragma db member(version) set(this.version.init (this.id.version, (?)))
  };

  #pragma db view                                                      \
    object(build_package)                                              \
    object(build_repository inner:                                     \
           brep::operator== (build_package::internal_repository,       \
                             build_repository::id) &&                  \
           brep::compare_version_ne (build_package::id.version,        \
                                     brep::wildcard_version,           \
                                     false))                           \
    object(build_tenant: build_package::id.tenant == build_tenant::id)
  struct buildable_package_count
  {
    size_t result;

    operator size_t () const {return result;}

    // Database mapping.
    //
    #pragma db member(result) column("count(" + build_package::id.name + ")")
  };

  // Packages that have the build constraints. Note that only buildable
  // (internal and non-stub) packages can have such constraints, so there is
  // no need for additional checks.
  //
  #pragma db view                                                           \
    table("build_package_constraints" = "c")                                \
    object(build_package inner:                                             \
           "c.exclusion AND "                                               \
           "c.tenant = " + build_package::id.tenant + "AND" +               \
           "c.name = " + build_package::id.name + "AND" +                   \
           "c.version_epoch = " + build_package::id.version.epoch + "AND" + \
           "c.version_canonical_upstream = " +                              \
             build_package::id.version.canonical_upstream + "AND" +         \
           "c.version_canonical_release = " +                               \
             build_package::id.version.canonical_release + "AND" +          \
           "c.version_revision = " + build_package::id.version.revision)    \
    object(build_tenant: build_package::id.tenant == build_tenant::id)      \
    query(distinct)
  struct build_constrained_package
  {
    shared_ptr<build_package> package;
  };
}

#endif // LIBBREP_BUILD_PACKAGE_HXX