mysql_query_macros_dhcp.h 31.9 KB
Newer Older
1
// Copyright (C) 2018-2020 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef MYSQL_QUERY_MACROS_DHCP_H
#define MYSQL_QUERY_MACROS_DHCP_H

/// @file mysql_query_macros_dhcp.h
/// Collection of common macros defining MySQL prepared statements used
/// to manage Kea DHCP configuration in the database.
///
/// Some of the macros are DHCPv4 specific, other are DHCPv6 specific.
/// Some macros are common for both DHCP server types. The first
/// parameter @c table_prefix should be set to @c dhcp4 or @c dhcp6,
/// depending which DHCP server type it relates to. Provided value
/// is used as a prefix for MySQL table names. For example, if the
/// prefix is set to @c dhcp4, the table name referred to in the
/// query may be dhcp4_subnet etc. The second argument in the variadic
/// macro is a part of the WHERE clause in the MySQL query. The fixed
/// part of the WHERE clause is included in the macro.

24 25 26
/// @todo Update queries to also fetch server tags to associate
/// returned configuration elements with particular servers.

27 28 29 30 31 32 33 34 35 36 37
namespace isc {
namespace dhcp {

namespace {

#ifndef MYSQL_GET_GLOBAL_PARAMETER
#define MYSQL_GET_GLOBAL_PARAMETER(table_prefix, ...) \
    "SELECT" \
    "  g.id," \
    "  g.name," \
    "  g.value," \
38
    "  g.parameter_type," \
39 40
    "  g.modification_ts," \
    "  s.tag " \
41 42 43 44
    "FROM " #table_prefix "_global_parameter AS g " \
    "INNER JOIN " #table_prefix "_global_parameter_server AS a " \
    "  ON g.id = a.parameter_id " \
    "INNER JOIN " #table_prefix "_server AS s " \
45
    "  ON (a.server_id = s.id) " \
46
    "WHERE (s.tag = ? OR s.id = 1) " #__VA_ARGS__ \
47
    " ORDER BY g.id, s.id"
48 49 50

#endif

51
#ifndef MYSQL_GET_SUBNET4
52
#define MYSQL_GET_SUBNET4_COMMON(server_join, ...) \
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
    "SELECT" \
    "  s.subnet_id," \
    "  s.subnet_prefix," \
    "  s.4o6_interface," \
    "  s.4o6_interface_id," \
    "  s.4o6_subnet," \
    "  s.boot_file_name," \
    "  s.client_class," \
    "  s.interface," \
    "  s.match_client_id," \
    "  s.modification_ts," \
    "  s.next_server," \
    "  s.rebind_timer," \
    "  s.relay," \
    "  s.renew_timer," \
    "  s.require_client_classes," \
    "  s.reservation_mode," \
    "  s.server_hostname," \
    "  s.shared_network_name," \
    "  s.user_context," \
    "  s.valid_lifetime," \
    "  p.id," \
    "  p.start_address," \
    "  p.end_address," \
    "  p.subnet_id," \
    "  p.modification_ts," \
    "  x.option_id," \
    "  x.code," \
    "  x.value," \
    "  x.formatted_value," \
    "  x.space," \
    "  x.persistent," \
    "  x.dhcp4_subnet_id," \
    "  x.scope_id," \
    "  x.user_context," \
    "  x.shared_network_name," \
    "  x.pool_id," \
    "  x.modification_ts," \
    "  o.option_id," \
    "  o.code," \
    "  o.value," \
    "  o.formatted_value," \
    "  o.space," \
    "  o.persistent," \
    "  o.dhcp4_subnet_id," \
    "  o.scope_id," \
    "  o.user_context," \
    "  o.shared_network_name," \
    "  o.pool_id," \
102 103 104
    "  o.modification_ts," \
    "  s.calculate_tee_times," \
    "  s.t1_percent," \
105
    "  s.t2_percent," \
106
    "  s.authoritative," \
107 108
    "  s.min_valid_lifetime," \
    "  s.max_valid_lifetime," \
109 110 111
    "  p.client_class," \
    "  p.require_client_classes," \
    "  p.user_context," \
112 113 114 115 116 117
    "  s.ddns_send_updates," \
    "  s.ddns_override_no_update," \
    "  s.ddns_override_client_update," \
    "  s.ddns_replace_client_name," \
    "  s.ddns_generated_prefix," \
    "  s.ddns_qualifying_suffix," \
118
    "  srv.tag " \
119
    "FROM dhcp4_subnet AS s " \
120
    server_join \
121 122 123
    "LEFT JOIN dhcp4_pool AS p ON s.subnet_id = p.subnet_id " \
    "LEFT JOIN dhcp4_options AS x ON x.scope_id = 5 AND p.id = x.pool_id " \
    "LEFT JOIN dhcp4_options AS o ON o.scope_id = 1 AND s.subnet_id = o.dhcp4_subnet_id " \
124
    #__VA_ARGS__ \
125
    " ORDER BY s.subnet_id, p.id, x.option_id, o.option_id"
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

#define MYSQL_GET_SUBNET4_NO_TAG(...) \
    MYSQL_GET_SUBNET4_COMMON( \
    "INNER JOIN dhcp4_subnet_server AS a " \
    "  ON s.subnet_id = a.subnet_id " \
    "INNER JOIN dhcp4_server AS srv " \
    "  ON (a.server_id = srv.id) ", \
    __VA_ARGS__)

#define MYSQL_GET_SUBNET4_ANY(...) \
    MYSQL_GET_SUBNET4_COMMON( \
    "LEFT JOIN dhcp4_subnet_server AS a "\
    "  ON s.subnet_id = a.subnet_id " \
    "LEFT JOIN dhcp4_server AS srv " \
    "  ON a.server_id = srv.id ", \
    __VA_ARGS__)

#define MYSQL_GET_SUBNET4_UNASSIGNED(...) \
    MYSQL_GET_SUBNET4_COMMON( \
    "LEFT JOIN dhcp4_subnet_server AS a "\
    "  ON s.subnet_id = a.subnet_id " \
    "LEFT JOIN dhcp4_server AS srv " \
    "  ON a.server_id = srv.id ", \
    WHERE a.subnet_id IS NULL __VA_ARGS__)

151 152
#endif

153
#ifndef MYSQL_GET_SUBNET6
154
#define MYSQL_GET_SUBNET6_COMMON(server_join, ...) \
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    "SELECT" \
    "  s.subnet_id," \
    "  s.subnet_prefix," \
    "  s.client_class," \
    "  s.interface," \
    "  s.modification_ts," \
    "  s.preferred_lifetime," \
    "  s.rapid_commit," \
    "  s.rebind_timer," \
    "  s.relay," \
    "  s.renew_timer," \
    "  s.require_client_classes," \
    "  s.reservation_mode," \
    "  s.shared_network_name," \
    "  s.user_context," \
    "  s.valid_lifetime," \
    "  p.id," \
    "  p.start_address," \
    "  p.end_address," \
    "  p.subnet_id," \
    "  p.modification_ts," \
    "  d.id," \
    "  d.prefix," \
    "  d.prefix_length," \
    "  d.delegated_prefix_length," \
180
    "  d.subnet_id," \
181 182 183 184 185 186 187 188 189 190 191 192 193
    "  d.modification_ts," \
    "  x.option_id," \
    "  x.code," \
    "  x.value," \
    "  x.formatted_value," \
    "  x.space," \
    "  x.persistent," \
    "  x.dhcp6_subnet_id," \
    "  x.scope_id," \
    "  x.user_context," \
    "  x.shared_network_name," \
    "  x.pool_id," \
    "  x.modification_ts," \
194
    "  x.pd_pool_id," \
195 196 197 198 199 200 201 202 203 204 205 206
    "  y.option_id," \
    "  y.code," \
    "  y.value," \
    "  y.formatted_value," \
    "  y.space," \
    "  y.persistent," \
    "  y.dhcp6_subnet_id," \
    "  y.scope_id," \
    "  y.user_context," \
    "  y.shared_network_name," \
    "  y.pool_id," \
    "  y.modification_ts," \
207
    "  y.pd_pool_id," \
208 209 210 211 212 213 214 215 216 217 218
    "  o.option_id," \
    "  o.code," \
    "  o.value," \
    "  o.formatted_value," \
    "  o.space," \
    "  o.persistent," \
    "  o.dhcp6_subnet_id," \
    "  o.scope_id," \
    "  o.user_context," \
    "  o.shared_network_name," \
    "  o.pool_id," \
219
    "  o.modification_ts," \
220 221 222
    "  o.pd_pool_id, " \
    "  s.calculate_tee_times," \
    "  s.t1_percent," \
223
    "  s.t2_percent," \
224
    "  s.interface_id," \
225 226 227 228
    "  s.min_preferred_lifetime," \
    "  s.max_preferred_lifetime," \
    "  s.min_valid_lifetime," \
    "  s.max_valid_lifetime," \
229 230 231 232 233 234 235 236
    "  p.client_class," \
    "  p.require_client_classes," \
    "  p.user_context," \
    "  d.excluded_prefix," \
    "  d.excluded_prefix_length," \
    "  d.client_class," \
    "  d.require_client_classes," \
    "  d.user_context," \
237 238 239 240 241 242
    "  s.ddns_send_updates," \
    "  s.ddns_override_no_update," \
    "  s.ddns_override_client_update," \
    "  s.ddns_replace_client_name," \
    "  s.ddns_generated_prefix," \
    "  s.ddns_qualifying_suffix," \
243
    "  srv.tag " \
244
    "FROM dhcp6_subnet AS s " \
245
    server_join \
246
    "LEFT JOIN dhcp6_pool AS p ON s.subnet_id = p.subnet_id " \
247
    "LEFT JOIN dhcp6_pd_pool AS d ON s.subnet_id = d.subnet_id " \
248 249 250
    "LEFT JOIN dhcp6_options AS x ON x.scope_id = 5 AND p.id = x.pool_id " \
    "LEFT JOIN dhcp6_options AS y ON y.scope_id = 6 AND d.id = y.pd_pool_id " \
    "LEFT JOIN dhcp6_options AS o ON o.scope_id = 1 AND s.subnet_id = o.dhcp6_subnet_id " \
251
    #__VA_ARGS__                                                        \
252
    " ORDER BY s.subnet_id, p.id, d.id, x.option_id, y.option_id, o.option_id"
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

#define MYSQL_GET_SUBNET6_NO_TAG(...) \
    MYSQL_GET_SUBNET6_COMMON( \
    "INNER JOIN dhcp6_subnet_server AS a " \
    "  ON s.subnet_id = a.subnet_id " \
    "INNER JOIN dhcp6_server AS srv " \
    "  ON (a.server_id = srv.id) ", \
    __VA_ARGS__)

#define MYSQL_GET_SUBNET6_ANY(...) \
    MYSQL_GET_SUBNET6_COMMON( \
    "LEFT JOIN dhcp6_subnet_server AS a "\
    "  ON s.subnet_id = a.subnet_id " \
    "LEFT JOIN dhcp6_server AS srv " \
    "  ON a.server_id = srv.id ", \
    __VA_ARGS__)

#define MYSQL_GET_SUBNET6_UNASSIGNED(...) \
    MYSQL_GET_SUBNET6_COMMON( \
    "LEFT JOIN dhcp6_subnet_server AS a "\
    "  ON s.subnet_id = a.subnet_id " \
    "LEFT JOIN dhcp6_server AS srv " \
    "  ON a.server_id = srv.id ", \
    WHERE a.subnet_id IS NULL __VA_ARGS__)

278 279
#endif

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
#ifndef MYSQL_GET_POOL4_COMMON
#define MYSQL_GET_POOL4_COMMON(server_join, ...) \
      "SELECT" \
      "  p.id," \
      "  p.start_address," \
      "  p.end_address," \
      "  p.subnet_id," \
      "  p.client_class," \
      "  p.require_client_classes," \
      "  p.user_context," \
      "  p.modification_ts," \
      "  x.option_id," \
      "  x.code," \
      "  x.value," \
      "  x.formatted_value," \
      "  x.space," \
      "  x.persistent," \
      "  x.dhcp4_subnet_id," \
      "  x.scope_id," \
      "  x.user_context," \
      "  x.shared_network_name," \
      "  x.pool_id," \
      "  x.modification_ts " \
      "FROM dhcp4_pool AS p " \
      server_join \
      "LEFT JOIN dhcp4_options AS x ON x.scope_id = 5 AND p.id = x.pool_id " \
      #__VA_ARGS__ \
      " ORDER BY p.id, x.option_id"

#define MYSQL_GET_POOL4_RANGE_WITH_TAG(...) \
    MYSQL_GET_POOL4_COMMON( \
       "INNER JOIN dhcp4_subnet_server AS s ON p.subnet_id = s.subnet_id " \
       "INNER JOIN dhcp4_server AS srv " \
       " ON (s.server_id = srv.id) OR (s.server_id = 1) ", \
       __VA_ARGS__)

#define MYSQL_GET_POOL4_RANGE_NO_TAG(...) \
    MYSQL_GET_POOL4_COMMON("", __VA_ARGS__)
#endif

#ifndef MYSQL_GET_POOL6_COMMON
#define MYSQL_GET_POOL6_COMMON(server_join, ...) \
    "SELECT" \
    "  p.id," \
    "  p.start_address," \
    "  p.end_address," \
    "  p.subnet_id," \
    "  p.client_class," \
    "  p.require_client_classes," \
    "  p.user_context," \
    "  p.modification_ts," \
    "  x.option_id," \
    "  x.code," \
    "  x.value," \
    "  x.formatted_value," \
    "  x.space," \
    "  x.persistent," \
    "  x.dhcp6_subnet_id," \
    "  x.scope_id," \
    "  x.user_context," \
    "  x.shared_network_name," \
    "  x.pool_id," \
    "  x.modification_ts," \
    "  x.pd_pool_id " \
    "FROM dhcp6_pool AS p " \
    server_join \
    "LEFT JOIN dhcp6_options AS x ON x.scope_id = 5 AND p.id = x.pool_id " \
    #__VA_ARGS__ \
    " ORDER BY p.id, x.option_id"

#define MYSQL_GET_POOL6_RANGE_WITH_TAG(...) \
    MYSQL_GET_POOL6_COMMON( \
    "INNER JOIN dhcp6_subnet_server AS s ON p.subnet_id = s.subnet_id " \
    "INNER JOIN dhcp6_server AS srv " \
    " ON (s.server_id = srv.id) OR (s.server_id = 1) ", \
    __VA_ARGS__)

#define MYSQL_GET_POOL6_RANGE_NO_TAG(...) \
    MYSQL_GET_POOL6_COMMON("", __VA_ARGS__)
#endif

#ifndef MYSQL_GET_PD_POOL_COMMON
#define MYSQL_GET_PD_POOL_COMMON(server_join, ...) \
    "SELECT" \
    "  p.id," \
    "  p.prefix," \
    "  p.prefix_length," \
    "  p.delegated_prefix_length," \
    "  p.subnet_id," \
    "  p.excluded_prefix," \
    "  p.excluded_prefix_length," \
    "  p.client_class," \
    "  p.require_client_classes," \
    "  p.user_context," \
    "  p.modification_ts," \
    "  x.option_id," \
    "  x.code," \
    "  x.value," \
    "  x.formatted_value," \
    "  x.space," \
    "  x.persistent," \
    "  x.dhcp6_subnet_id," \
    "  x.scope_id," \
    "  x.user_context," \
    "  x.shared_network_name," \
    "  x.pool_id," \
    "  x.modification_ts," \
    "  x.pd_pool_id " \
    "FROM dhcp6_pd_pool AS p " \
    server_join \
    "LEFT JOIN dhcp6_options AS x ON x.scope_id = 6 AND p.id = x.pd_pool_id " \
    #__VA_ARGS__ \
    " ORDER BY p.id, x.option_id" \

#define MYSQL_GET_PD_POOL_WITH_TAG(...) \
    MYSQL_GET_PD_POOL_COMMON( \
    "INNER JOIN dhcp6_subnet_server AS s ON p.subnet_id = s.subnet_id " \
    "INNER JOIN dhcp6_server AS srv " \
    " ON (s.server_id = srv.id) OR (s.server_id = 1) ", \
    __VA_ARGS__)

#define MYSQL_GET_PD_POOL_NO_TAG(...) \
    MYSQL_GET_PD_POOL_COMMON("", __VA_ARGS__)
#endif

405
#ifndef MYSQL_GET_SHARED_NETWORK4
406
#define MYSQL_GET_SHARED_NETWORK4_COMMON(server_join, ...) \
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
    "SELECT" \
    "  n.id," \
    "  n.name," \
    "  n.client_class," \
    "  n.interface," \
    "  n.match_client_id," \
    "  n.modification_ts," \
    "  n.rebind_timer," \
    "  n.relay," \
    "  n.renew_timer," \
    "  n.require_client_classes," \
    "  n.reservation_mode," \
    "  n.user_context," \
    "  n.valid_lifetime," \
    "  o.option_id," \
    "  o.code," \
    "  o.value," \
    "  o.formatted_value," \
    "  o.space," \
    "  o.persistent," \
    "  o.dhcp4_subnet_id," \
    "  o.scope_id," \
    "  o.user_context," \
    "  o.shared_network_name," \
    "  o.pool_id," \
432 433 434
    "  o.modification_ts," \
    "  n.calculate_tee_times," \
    "  n.t1_percent," \
435
    "  n.t2_percent," \
436 437 438
    "  n.authoritative," \
    "  n.boot_file_name," \
    "  n.next_server," \
439
    "  n.server_hostname," \
440 441
    "  n.min_valid_lifetime," \
    "  n.max_valid_lifetime," \
442 443 444 445 446 447
    "  n.ddns_send_updates," \
    "  n.ddns_override_no_update," \
    "  n.ddns_override_client_update," \
    "  n.ddns_replace_client_name," \
    "  n.ddns_generated_prefix," \
    "  n.ddns_qualifying_suffix," \
448
    "  s.tag " \
449
    "FROM dhcp4_shared_network AS n " \
450
    server_join \
451
    "LEFT JOIN dhcp4_options AS o ON o.scope_id = 4 AND n.name = o.shared_network_name " \
452 453 454 455
    #__VA_ARGS__ \
    " ORDER BY n.id, s.id, o.option_id"

#define MYSQL_GET_SHARED_NETWORK4_NO_TAG(...) \
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
    MYSQL_GET_SHARED_NETWORK4_COMMON( \
    "INNER JOIN dhcp4_shared_network_server AS a " \
    "  ON n.id = a.shared_network_id " \
    "INNER JOIN dhcp4_server AS s " \
    "  ON (a.server_id = s.id) ", \
    __VA_ARGS__)

#define MYSQL_GET_SHARED_NETWORK4_ANY(...) \
    MYSQL_GET_SHARED_NETWORK4_COMMON( \
    "LEFT JOIN dhcp4_shared_network_server AS a " \
    "  ON n.id = a.shared_network_id " \
    "LEFT JOIN dhcp4_server AS s " \
    "  ON a.server_id = s.id ", \
    __VA_ARGS__)

#define MYSQL_GET_SHARED_NETWORK4_UNASSIGNED(...) \
    MYSQL_GET_SHARED_NETWORK4_COMMON( \
    "LEFT JOIN dhcp4_shared_network_server AS a " \
    "  ON n.id = a.shared_network_id " \
    "LEFT JOIN dhcp4_server AS s " \
    "  ON a.server_id = s.id ", \
    WHERE a.shared_network_id IS NULL __VA_ARGS__)
478

479 480
#endif

481
#ifndef MYSQL_GET_SHARED_NETWORK6
482
#define MYSQL_GET_SHARED_NETWORK6_COMMON(server_join, ...) \
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    "SELECT" \
    "  n.id," \
    "  n.name," \
    "  n.client_class," \
    "  n.interface," \
    "  n.modification_ts," \
    "  n.preferred_lifetime," \
    "  n.rapid_commit," \
    "  n.rebind_timer," \
    "  n.relay," \
    "  n.renew_timer," \
    "  n.require_client_classes," \
    "  n.reservation_mode," \
    "  n.user_context," \
    "  n.valid_lifetime," \
    "  o.option_id," \
    "  o.code," \
    "  o.value," \
    "  o.formatted_value," \
    "  o.space," \
    "  o.persistent," \
    "  o.dhcp6_subnet_id," \
    "  o.scope_id," \
    "  o.user_context," \
    "  o.shared_network_name," \
    "  o.pool_id," \
509
    "  o.modification_ts," \
510 511 512
    "  o.pd_pool_id, " \
    "  n.calculate_tee_times," \
    "  n.t1_percent," \
513
    "  n.t2_percent," \
514
    "  n.interface_id," \
515 516 517 518
    "  n.min_preferred_lifetime," \
    "  n.max_preferred_lifetime," \
    "  n.min_valid_lifetime," \
    "  n.max_valid_lifetime," \
519 520 521 522 523 524
    "  n.ddns_send_updates," \
    "  n.ddns_override_no_update," \
    "  n.ddns_override_client_update," \
    "  n.ddns_replace_client_name," \
    "  n.ddns_generated_prefix," \
    "  n.ddns_qualifying_suffix," \
525
    "  s.tag " \
526
    "FROM dhcp6_shared_network AS n " \
527
    server_join \
528
    "LEFT JOIN dhcp6_options AS o ON o.scope_id = 4 AND n.name = o.shared_network_name " \
529 530 531 532
    #__VA_ARGS__ \
    " ORDER BY n.id, s.id, o.option_id"

#define MYSQL_GET_SHARED_NETWORK6_NO_TAG(...) \
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
    MYSQL_GET_SHARED_NETWORK6_COMMON( \
    "INNER JOIN dhcp6_shared_network_server AS a " \
    "  ON n.id = a.shared_network_id " \
    "INNER JOIN dhcp6_server AS s " \
    "  ON (a.server_id = s.id) ", \
    __VA_ARGS__)

#define MYSQL_GET_SHARED_NETWORK6_ANY(...) \
    MYSQL_GET_SHARED_NETWORK6_COMMON( \
    "LEFT JOIN dhcp6_shared_network_server AS a " \
    "  ON n.id = a.shared_network_id " \
    "LEFT JOIN dhcp6_server AS s " \
    "  ON a.server_id = s.id ", \
    __VA_ARGS__)

#define MYSQL_GET_SHARED_NETWORK6_UNASSIGNED(...) \
    MYSQL_GET_SHARED_NETWORK6_COMMON( \
    "LEFT JOIN dhcp6_shared_network_server AS a " \
    "  ON n.id = a.shared_network_id " \
    "LEFT JOIN dhcp6_server AS s " \
    "  ON a.server_id = s.id ", \
    WHERE a.shared_network_id IS NULL __VA_ARGS__)
555

556 557
#endif

558 559 560 561 562 563 564 565 566
#ifndef MYSQL_GET_OPTION_DEF
#define MYSQL_GET_OPTION_DEF(table_prefix, ...) \
    "SELECT" \
    "  d.id," \
    "  d.code," \
    "  d.name," \
    "  d.space," \
    "  d.type," \
    "  d.modification_ts," \
567
    "  d.is_array," \
568 569
    "  d.encapsulate," \
    "  d.record_types," \
570 571
    "  d.user_context," \
    "  s.tag " \
572 573 574 575
    "FROM " #table_prefix "_option_def AS d " \
    "INNER JOIN " #table_prefix "_option_def_server AS a" \
    "  ON d.id = a.option_def_id " \
    "INNER JOIN " #table_prefix "_server AS s " \
576
    "  ON a.server_id = s.id " \
577 578 579 580
    "WHERE (s.tag = ? OR s.id = 1) " #__VA_ARGS__ \
    " ORDER BY d.id"
#endif

581 582
#ifndef MYSQL_GET_OPTION_COMMON
#define MYSQL_GET_OPTION_COMMON(table_prefix, pd_pool_id, ...) \
583 584 585 586 587 588 589
    "SELECT" \
    "  o.option_id," \
    "  o.code," \
    "  o.value," \
    "  o.formatted_value," \
    "  o.space," \
    "  o.persistent," \
590
    "  o." #table_prefix "_subnet_id," \
591 592 593 594
    "  o.scope_id," \
    "  o.user_context," \
    "  o.shared_network_name," \
    "  o.pool_id," \
595 596
    "  o.modification_ts," \
    "  s.tag " \
597
    pd_pool_id \
598 599 600 601 602 603
    "FROM " #table_prefix "_options AS o " \
    "INNER JOIN " #table_prefix "_options_server AS a" \
    "  ON o.option_id = a.option_id " \
    "INNER JOIN " #table_prefix "_server AS s" \
    "  ON a.server_id = s.id " \
    "WHERE (s.tag = ? OR s.id = 1) " #__VA_ARGS__ \
604
    " ORDER BY o.option_id, s.id"
605 606 607

#define MYSQL_GET_OPTION4(...) \
    MYSQL_GET_OPTION_COMMON(dhcp4, "", __VA_ARGS__)
608 609
#define MYSQL_GET_OPTION6(...) \
    MYSQL_GET_OPTION_COMMON(dhcp6, ", o.pd_pool_id ", __VA_ARGS__)
610 611
#endif

612 613 614 615 616 617 618 619
#ifndef MYSQL_GET_AUDIT_ENTRIES_TIME
#define MYSQL_GET_AUDIT_ENTRIES_TIME(table_prefix) \
    "SELECT" \
    "  a.id," \
    "  a.object_type," \
    "  a.object_id," \
    "  a.modification_type," \
    "  r.modification_ts," \
620
    "  r.id, " \
621 622
    "  r.log_message " \
    "FROM " #table_prefix "_audit AS a " \
623
    "INNER JOIN " #table_prefix "_audit_revision AS r " \
624
    "  ON a.revision_id = r.id " \
625 626
    "INNER JOIN " #table_prefix "_server AS s" \
    "  ON r.server_id = s.id " \
627 628
    "WHERE (s.tag = ? OR s.id = 1) AND ((r.modification_ts, r.id)  > (?, ?))" \
    " ORDER BY r.modification_ts, r.id"
629 630
#endif

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
#ifndef MYSQL_GET_SERVERS_COMMON
#define MYSQL_GET_SERVERS_COMMON(table_prefix, ...) \
    "SELECT" \
    "  s.id," \
    "  s.tag," \
    "  s.description," \
    "  s.modification_ts " \
    "FROM " #table_prefix "_server AS s " \
    "WHERE s.id > 1 " \
    __VA_ARGS__ \
    "ORDER BY s.id"
#define MYSQL_GET_ALL_SERVERS(table_prefix) \
    MYSQL_GET_SERVERS_COMMON(table_prefix, "")
#define MYSQL_GET_SERVER(table_prefix) \
    MYSQL_GET_SERVERS_COMMON(table_prefix, "AND s.tag = ? ")
#endif

648 649 650 651 652
#ifndef MYSQL_INSERT_GLOBAL_PARAMETER
#define MYSQL_INSERT_GLOBAL_PARAMETER(table_prefix) \
    "INSERT INTO " #table_prefix "_global_parameter(" \
    "  name," \
    "  value," \
653
    "  parameter_type," \
654
    "  modification_ts" \
655
    ") VALUES (?, ?, ?, ?)"
656 657 658 659 660 661
#endif

#ifndef MYSQL_INSERT_GLOBAL_PARAMETER_SERVER
#define MYSQL_INSERT_GLOBAL_PARAMETER_SERVER(table_prefix) \
    "INSERT INTO " #table_prefix "_global_parameter_server(" \
    "  parameter_id," \
662 663 664
    "  modification_ts," \
    "  server_id" \
    ") VALUES (?, ?, (SELECT id FROM " #table_prefix "_server WHERE tag = ?))"
665 666 667 668 669 670
#endif

#ifndef MYSQL_INSERT_SUBNET_SERVER
#define MYSQL_INSERT_SUBNET_SERVER(table_prefix) \
    "INSERT INTO " #table_prefix "_subnet_server(" \
    "  subnet_id," \
671 672 673
    "  modification_ts," \
    "  server_id" \
    ") VALUES (?, ?, (SELECT id FROM " #table_prefix "_server WHERE tag = ?))"
674 675 676 677 678 679 680 681
#endif

#ifndef MYSQL_INSERT_POOL
#define MYSQL_INSERT_POOL(table_prefix) \
    "INSERT INTO " #table_prefix "_pool(" \
    "  start_address," \
    "  end_address," \
    "  subnet_id," \
682 683 684
    "  client_class," \
    "  require_client_classes," \
    "  user_context," \
685
    "  modification_ts" \
686
    ") VALUES (?, ?, ?, ?, ?, ?, ?)"
687 688
#endif

689 690 691 692 693 694
#ifndef MYSQL_INSERT_PD_POOL
#define MYSQL_INSERT_PD_POOL() \
    "INSERT INTO dhcp6_pd_pool(" \
    "  prefix," \
    "  prefix_length," \
    "  delegated_prefix_length," \
695
    "  subnet_id," \
696 697 698 699 700
    "  excluded_prefix," \
    "  excluded_prefix_length," \
    "  client_class," \
    "  require_client_classes," \
    "  user_context," \
701
    "  modification_ts" \
702
    ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
703 704
#endif

705 706 707 708
#ifndef MYSQL_INSERT_SHARED_NETWORK_SERVER
#define MYSQL_INSERT_SHARED_NETWORK_SERVER(table_prefix) \
    "INSERT INTO " #table_prefix "_shared_network_server(" \
    "  shared_network_id," \
709 710
    "  modification_ts," \
    "  server_id" \
711
    ") VALUES (" \
712 713
    "    (SELECT id FROM " #table_prefix "_shared_network WHERE name = ?), ?," \
    "    (SELECT id FROM " #table_prefix "_server WHERE tag = ?)" \
714 715 716 717 718 719 720 721 722 723 724
    ")"
#endif

#ifndef MYSQL_INSERT_OPTION_DEF
#define MYSQL_INSERT_OPTION_DEF(table_prefix) \
    "INSERT INTO " #table_prefix "_option_def (" \
    "  code," \
    "  name," \
    "  space," \
    "  type," \
    "  modification_ts," \
725
    "  is_array," \
726 727 728 729 730 731 732 733 734 735
    "  encapsulate," \
    "  record_types," \
    "  user_context" \
    ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
#endif

#ifndef MYSQL_INSERT_OPTION_DEF_SERVER
#define MYSQL_INSERT_OPTION_DEF_SERVER(table_prefix) \
    "INSERT INTO " #table_prefix "_option_def_server(" \
    "  option_def_id," \
736 737 738
    "  modification_ts," \
    "  server_id" \
    ") VALUES (?, ?, (SELECT id FROM " #table_prefix "_server WHERE tag = ?))"
739 740
#endif

741 742
#ifndef MYSQL_INSERT_OPTION_COMMON
#define MYSQL_INSERT_OPTION_COMMON(table_prefix, pd_pool_id, last) \
743 744 745 746 747 748 749 750 751 752 753 754 755
    "INSERT INTO " #table_prefix "_options (" \
    "  code," \
    "  value," \
    "  formatted_value," \
    "  space," \
    "  persistent," \
    "  dhcp_client_class," \
    " " #table_prefix "_subnet_id," \
    "  scope_id," \
    "  user_context," \
    "  shared_network_name," \
    "  pool_id," \
    "  modification_ts" \
756
    pd_pool_id \
757
    ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?" last ")"
758 759 760

#define MYSQL_INSERT_OPTION4() \
    MYSQL_INSERT_OPTION_COMMON(dhcp4, "", "")
761 762
#define MYSQL_INSERT_OPTION6() \
    MYSQL_INSERT_OPTION_COMMON(dhcp6, ", pd_pool_id ", ", ?")
763 764 765 766 767 768
#endif

#ifndef MYSQL_INSERT_OPTION_SERVER
#define MYSQL_INSERT_OPTION_SERVER(table_prefix) \
    "INSERT INTO " #table_prefix "_options_server (" \
    "  option_id," \
769 770 771
    "  modification_ts," \
    "  server_id" \
    ") VALUES (?, ?, (SELECT id FROM " #table_prefix "_server WHERE tag = ?))"
772 773
#endif

774 775 776 777 778 779 780 781 782
#ifndef MYSQL_INSERT_SERVER
#define MYSQL_INSERT_SERVER(table_prefix) \
    "INSERT INTO " #table_prefix "_server (" \
    "  tag," \
    "  description," \
    "  modification_ts" \
    ") VALUES (?, ?, ?)"
#endif

783 784 785 786 787 788 789 790 791 792
#ifndef MYSQL_UPDATE_GLOBAL_PARAMETER
#define MYSQL_UPDATE_GLOBAL_PARAMETER(table_prefix) \
    "UPDATE " #table_prefix "_global_parameter AS g " \
    "INNER JOIN " #table_prefix "_global_parameter_server AS a" \
    "  ON g.id = a.parameter_id " \
    "INNER JOIN " #table_prefix "_server AS s" \
    "  ON a.server_id = s.id " \
    "SET" \
    "  g.name = ?," \
    "  g.value = ?," \
793
    "  g.parameter_type = ?," \
794 795 796 797 798 799
    "  g.modification_ts = ? " \
    "WHERE s.tag = ? AND g.name = ?"
#endif

#ifndef MYSQL_UPDATE_OPTION_DEF
#define MYSQL_UPDATE_OPTION_DEF(table_prefix) \
800 801 802 803 804 805 806 807 808 809 810
    "UPDATE " #table_prefix "_option_def AS d " \
    "INNER JOIN " #table_prefix "_option_def_server AS a" \
    "  ON d.id = a.option_def_id " \
    "INNER JOIN " #table_prefix "_server AS s" \
    "  ON a.server_id = s.id " \
    "SET" \
    "  d.code = ?," \
    "  d.name = ?," \
    "  d.space = ?," \
    "  d.type = ?," \
    "  d.modification_ts = ?," \
811
    "  d.is_array = ?," \
812 813 814 815
    "  d.encapsulate = ?," \
    "  d.record_types = ?," \
    "  d.user_context = ? " \
    "WHERE s.tag = ? AND d.code = ? AND d.space = ?"
816 817
#endif

818
#ifndef MYSQL_UPDATE_OPTION_COMMON
819
#define MYSQL_UPDATE_OPTION_COMMON(table_prefix, server_join, pd_pool_id, ...) \
820
    "UPDATE " #table_prefix "_options AS o " \
821
    server_join \
822 823 824 825 826 827 828 829 830 831 832 833 834
    "SET" \
    "  o.code = ?," \
    "  o.value = ?," \
    "  o.formatted_value = ?," \
    "  o.space = ?," \
    "  o.persistent = ?," \
    "  o.dhcp_client_class = ?," \
    "  o." #table_prefix "_subnet_id = ?," \
    "  o.scope_id = ?," \
    "  o.user_context = ?," \
    "  o.shared_network_name = ?," \
    "  o.pool_id = ?," \
    "  o.modification_ts = ? " \
835
    pd_pool_id \
836
    "WHERE " #__VA_ARGS__
837

838
#define MYSQL_UPDATE_OPTION4_WITH_TAG(...) \
839 840 841 842 843 844
    MYSQL_UPDATE_OPTION_COMMON(dhcp4, \
    "INNER JOIN dhcp4_options_server AS a" \
    "  ON o.option_id = a.option_id " \
    "INNER JOIN dhcp4_server AS s" \
    "  ON a.server_id = s.id ", \
    "", s.tag = ? __VA_ARGS__)
845 846

#define MYSQL_UPDATE_OPTION4_NO_TAG(...) \
847
    MYSQL_UPDATE_OPTION_COMMON(dhcp4, "", "", __VA_ARGS__)
848 849

#define MYSQL_UPDATE_OPTION6_WITH_TAG(...) \
850 851 852 853 854 855
    MYSQL_UPDATE_OPTION_COMMON(dhcp6, \
    "INNER JOIN dhcp6_options_server AS a" \
    "  ON o.option_id = a.option_id " \
    "INNER JOIN dhcp6_server AS s" \
    "  ON a.server_id = s.id ", \
    ", o.pd_pool_id = ? ", s.tag = ? __VA_ARGS__)
856 857

#define MYSQL_UPDATE_OPTION6_NO_TAG(...) \
858
    MYSQL_UPDATE_OPTION_COMMON(dhcp6, "", ", o.pd_pool_id = ? ", __VA_ARGS__)
859 860
#endif

861 862 863 864 865 866 867 868 869 870
#ifndef MYSQL_UPDATE_SERVER
#define MYSQL_UPDATE_SERVER(table_prefix) \
    "UPDATE " #table_prefix "_server " \
    "SET" \
    "  tag = ?," \
    "  description = ?," \
    "  modification_ts = ? " \
    "WHERE tag = ?"
#endif

871 872 873 874 875 876 877 878 879 880
#ifndef MYSQL_DELETE_GLOBAL_PARAMETER
#define MYSQL_DELETE_GLOBAL_PARAMETER(table_prefix, ...) \
    "DELETE g FROM " #table_prefix "_global_parameter AS g " \
    "INNER JOIN " #table_prefix "_global_parameter_server AS a " \
    "  ON g.id = a.parameter_id " \
    "INNER JOIN " #table_prefix "_server AS s" \
    "  ON (a.server_id = s.id) " \
    "WHERE s.tag = ? " #__VA_ARGS__
#endif

881 882 883 884 885 886 887 888
#ifndef MYSQL_DELETE_GLOBAL_PARAMETER_UNASSIGNED
#define MYSQL_DELETE_GLOBAL_PARAMETER_UNASSIGNED(table_prefix, ...) \
    "DELETE g FROM " #table_prefix "_global_parameter AS g " \
    "LEFT JOIN " #table_prefix "_global_parameter_server AS a " \
    "  ON g.id = a.parameter_id " \
    "WHERE a.parameter_id IS NULL " #__VA_ARGS__
#endif

889
#ifndef MYSQL_DELETE_SUBNET
890
#define MYSQL_DELETE_SUBNET_COMMON(table_prefix, ...) \
891 892 893 894 895
    "DELETE s FROM " #table_prefix "_subnet AS s " \
    "INNER JOIN " #table_prefix "_subnet_server AS a " \
    "  ON s.subnet_id = a.subnet_id " \
    "INNER JOIN " #table_prefix "_server AS srv" \
    "  ON a.server_id = srv.id " \
896 897 898 899 900 901 902 903 904
    #__VA_ARGS__

#define MYSQL_DELETE_SUBNET_WITH_TAG(table_prefix, ...) \
    MYSQL_DELETE_SUBNET_COMMON(table_prefix, WHERE srv.tag = ? __VA_ARGS__)

#define MYSQL_DELETE_SUBNET_ANY(table_prefix, ...) \
    "DELETE s FROM " #table_prefix "_subnet AS s " \
    #__VA_ARGS__

905 906 907 908 909 910
#define MYSQL_DELETE_SUBNET_UNASSIGNED(table_prefix, ...) \
    "DELETE s FROM " #table_prefix "_subnet AS s " \
    "LEFT JOIN " #table_prefix "_subnet_server AS a" \
    "  ON s.subnet_id = a.subnet_id " \
    "WHERE a.subnet_id IS NULL " #__VA_ARGS__

911 912
#endif

913 914 915 916 917 918
#ifndef MYSQL_DELETE_SUBNET_SERVER
#define MYSQL_DELETE_SUBNET_SERVER(table_prefix) \
    "DELETE FROM " #table_prefix "_subnet_server " \
    "WHERE subnet_id = ?"
#endif

919 920 921
#ifndef MYSQL_DELETE_POOLS
#define MYSQL_DELETE_POOLS(table_prefix) \
    "DELETE FROM " #table_prefix "_pool " \
922 923 924
    "WHERE subnet_id = ? OR subnet_id = " \
    "(SELECT subnet_id FROM " #table_prefix "_subnet" \
    "    WHERE subnet_prefix = ?)"
925 926
#endif

927 928 929
#ifndef MYSQL_DELETE_PD_POOLS
#define MYSQL_DELETE_PD_POOLS() \
    "DELETE FROM dhcp6_pd_pool " \
930 931 932
    "WHERE subnet_id = ? OR subnet_id = " \
    "(SELECT subnet_id FROM dhcp6_subnet" \
    "    WHERE subnet_prefix = ?)"
933 934
#endif

935 936
#ifndef MYSQL_DELETE_SHARED_NETWORK_COMMON
#define MYSQL_DELETE_SHARED_NETWORK_COMMON(table_prefix, ...) \
937 938 939 940 941
    "DELETE n FROM " #table_prefix "_shared_network AS n " \
    "INNER JOIN " #table_prefix "_shared_network_server AS a" \
    "  ON n.id = a.shared_network_id " \
    "INNER JOIN " #table_prefix "_server AS s" \
    "  ON a.server_id = s.id " \
942 943 944 945 946
    #__VA_ARGS__

#define MYSQL_DELETE_SHARED_NETWORK_WITH_TAG(table_prefix, ...) \
    MYSQL_DELETE_SHARED_NETWORK_COMMON(table_prefix, WHERE s.tag = ? __VA_ARGS__)

947 948 949
#define MYSQL_DELETE_SHARED_NETWORK_ANY(table_prefix, ...) \
    "DELETE n FROM " #table_prefix "_shared_network AS n " \
    #__VA_ARGS__
950

951 952 953 954 955 956
#define MYSQL_DELETE_SHARED_NETWORK_UNASSIGNED(table_prefix, ...) \
    "DELETE n FROM " #table_prefix "_shared_network AS n " \
    "LEFT JOIN " #table_prefix "_shared_network_server AS a" \
    "  ON n.id = a.shared_network_id " \
    "WHERE a.shared_network_id IS NULL " #__VA_ARGS__

957 958
#endif

959 960 961 962 963 964 965
#ifndef MYSQL_DELETE_SHARED_NETWORK_SERVER
#define MYSQL_DELETE_SHARED_NETWORK_SERVER(table_prefix) \
    "DELETE FROM " #table_prefix "_shared_network_server " \
    "WHERE shared_network_id = " \
    "(SELECT id FROM " #table_prefix "_shared_network WHERE name = ?)"
#endif

966 967 968 969 970 971 972 973 974 975
#ifndef MYSQL_DELETE_OPTION_DEF
#define MYSQL_DELETE_OPTION_DEF(table_prefix, ...) \
    "DELETE d FROM " #table_prefix "_option_def AS d " \
    "INNER JOIN " #table_prefix "_option_def_server AS a" \
    "  ON d.id = a.option_def_id " \
    "INNER JOIN " #table_prefix "_server AS s" \
    "  ON a.server_id = s.id " \
    "WHERE s.tag = ? " #__VA_ARGS__
#endif

976 977 978 979 980 981 982 983
#ifndef MYSQL_DELETE_OPTION_DEF_UNASSIGNED
#define MYSQL_DELETE_OPTION_DEF_UNASSIGNED(table_prefix, ...) \
    "DELETE d FROM " #table_prefix "_option_def AS d " \
    "LEFT JOIN " #table_prefix "_option_def_server AS a " \
    "  ON d.id = a.option_def_id " \
    "WHERE a.option_def_id IS NULL " #__VA_ARGS__
#endif

984 985
#ifndef MYSQL_DELETE_OPTION_WITH_TAG
#define MYSQL_DELETE_OPTION_WITH_TAG(table_prefix, ...) \
986 987 988 989 990
    "DELETE o FROM " #table_prefix "_options AS o " \
    "INNER JOIN " #table_prefix "_options_server AS a" \
    "  ON o.option_id = a.option_id " \
    "INNER JOIN " #table_prefix "_server AS s" \
    "  ON a.server_id = s.id " \
991 992
    "WHERE s.tag = ? " #__VA_ARGS__
#endif
993

994
#ifndef MYSQL_DELETE_OPTION_NO_TAG
995
#define MYSQL_DELETE_OPTION_NO_TAG(table_prefix, ...) \
996 997
    "DELETE o FROM " #table_prefix "_options AS o " \
    #__VA_ARGS__
998 999
#endif

1000 1001 1002 1003 1004 1005 1006 1007
#ifndef MYSQL_DELETE_OPTION_SUBNET_ID_PREFIX
#define MYSQL_DELETE_OPTION_SUBNET_ID_PREFIX(table_prefix) \
    "DELETE o FROM " #table_prefix "_options AS o " \
    "INNER JOIN " #table_prefix "_subnet AS s " \
    "  ON s.subnet_id = o." #table_prefix "_subnet_id " \
    "WHERE o.scope_id = 1 AND (s.subnet_id = ? OR s.subnet_prefix = ?)"
#endif

1008 1009 1010 1011 1012 1013 1014 1015
#ifndef MYSQL_DELETE_OPTION_UNASSIGNED
#define MYSQL_DELETE_OPTION_UNASSIGNED(table_prefix, ...) \
    "DELETE o FROM " #table_prefix "_options AS o " \
    "LEFT JOIN " #table_prefix "_options_server AS a " \
    "  ON o.option_id = a.option_id " \
    "WHERE a.option_id IS NULL " #__VA_ARGS__
#endif

1016 1017 1018
#ifndef MYSQL_DELETE_OPTION_POOL_RANGE
#define MYSQL_DELETE_OPTION_POOL_RANGE(table_prefix, ...) \
    "DELETE o FROM " #table_prefix "_options AS o " \
1019
    "WHERE " #__VA_ARGS__ \
1020 1021 1022 1023 1024
    "  AND o.pool_id = " \
    "  (SELECT id FROM " #table_prefix "_pool" \
    "   WHERE start_address = ? AND end_address = ?)"
#endif

1025 1026 1027
#ifndef MYSQL_DELETE_OPTION_PD_POOL
#define MYSQL_DELETE_OPTION_PD_POOL(...) \
    "DELETE o FROM dhcp6_options AS o " \
1028
    "WHERE " #__VA_ARGS__ \
1029 1030 1031 1032 1033
    "  AND o.pd_pool_id = " \
    "  (SELECT id FROM dhcp6_pd_pool" \
    "   WHERE prefix = ? AND prefix_length = ?)"
#endif

1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
#ifndef MYSQL_DELETE_SERVER
#define MYSQL_DELETE_SERVER(table_prefix) \
    "DELETE FROM " #table_prefix "_server " \
    "WHERE tag = ?"
#endif

#ifndef MYSQL_DELETE_ALL_SERVERS
#define MYSQL_DELETE_ALL_SERVERS(table_prefix) \
    "DELETE FROM " #table_prefix "_server " \
    "WHERE id > 1"
#endif

1046 1047 1048 1049 1050 1051
} // end of anonymous namespace

} // end of namespace isc::dhcp
} // end of namespace isc

#endif