mysql_query_macros_dhcp.h 21.5 KB
Newer Older
1
// Copyright (C) 2018-2019 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 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
#define MYSQL_GET_SUBNET4(...) \
    "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
    "  srv.tag " \
110 111 112 113 114 115 116 117 118 119 120 121
    "FROM dhcp4_subnet AS s " \
    "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) OR (a.server_id = 1) " \
    "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 " \
    "WHERE (srv.tag = ? OR srv.id = 1) " #__VA_ARGS__ \
    " ORDER BY s.subnet_id, p.id, x.option_id, o.option_id"
#endif

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
#ifndef MYSQL_GET_SUBNET6
#define MYSQL_GET_SUBNET6(...) \
    "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," \
149
    "  d.subnet_id," \
150 151 152 153 154 155 156 157 158 159 160 161 162
    "  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," \
163
    "  x.pd_pool_id," \
164 165 166 167 168 169 170 171 172 173 174 175
    "  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," \
176
    "  y.pd_pool_id," \
177 178 179 180 181 182 183 184 185 186 187
    "  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," \
188
    "  o.modification_ts," \
189 190 191
    "  o.pd_pool_id, " \
    "  s.calculate_tee_times," \
    "  s.t1_percent," \
192
    "  s.t2_percent," \
193
    "  s.interface_id," \
194 195 196 197
    "  s.min_preferred_lifetime," \
    "  s.max_preferred_lifetime," \
    "  s.min_valid_lifetime," \
    "  s.max_valid_lifetime," \
198
    "  srv.tag " \
199 200 201 202 203 204
    "FROM dhcp6_subnet AS s " \
    "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) OR (a.server_id = 1) " \
    "LEFT JOIN dhcp6_pool AS p ON s.subnet_id = p.subnet_id " \
205
    "LEFT JOIN dhcp6_pd_pool AS d ON s.subnet_id = d.subnet_id " \
206 207 208 209
    "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 " \
    "WHERE (srv.tag = ? OR srv.id = 1) " #__VA_ARGS__ \
210
    " ORDER BY s.subnet_id, p.id, d.id, x.option_id, o.option_id"
211 212
#endif

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
#ifndef MYSQL_GET_SHARED_NETWORK4
#define MYSQL_GET_SHARED_NETWORK4(...) \
    "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," \
240 241 242
    "  o.modification_ts," \
    "  n.calculate_tee_times," \
    "  n.t1_percent," \
243
    "  n.t2_percent," \
244 245 246
    "  n.authoritative," \
    "  n.boot_file_name," \
    "  n.next_server," \
247
    "  n.server_hostname," \
248 249
    "  n.min_valid_lifetime," \
    "  n.max_valid_lifetime," \
250
    "  s.tag " \
251 252 253 254 255 256 257 258 259 260
    "FROM dhcp4_shared_network AS n " \
    "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) OR (a.server_id = 1) " \
    "LEFT JOIN dhcp4_options AS o ON o.scope_id = 4 AND n.name = o.shared_network_name " \
    "WHERE (s.tag = ? OR s.id = 1) " #__VA_ARGS__ \
    " ORDER BY n.id, o.option_id"
#endif

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
#ifndef MYSQL_GET_SHARED_NETWORK6
#define MYSQL_GET_SHARED_NETWORK6(...) \
    "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," \
289
    "  o.modification_ts," \
290 291 292
    "  o.pd_pool_id, " \
    "  n.calculate_tee_times," \
    "  n.t1_percent," \
293
    "  n.t2_percent," \
294
    "  n.interface_id," \
295 296 297 298
    "  n.min_preferred_lifetime," \
    "  n.max_preferred_lifetime," \
    "  n.min_valid_lifetime," \
    "  n.max_valid_lifetime," \
299
    "  s.tag " \
300 301 302 303 304 305 306 307 308 309
    "FROM dhcp6_shared_network AS n " \
    "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) OR (a.server_id = 1) " \
    "LEFT JOIN dhcp6_options AS o ON o.scope_id = 4 AND n.name = o.shared_network_name " \
    "WHERE (s.tag = ? OR s.id = 1) " #__VA_ARGS__ \
    " ORDER BY n.id, o.option_id"
#endif

310 311 312 313 314 315 316 317 318 319 320 321
#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," \
    "  d.array," \
    "  d.encapsulate," \
    "  d.record_types," \
322 323
    "  d.user_context," \
    "  s.tag " \
324 325 326 327 328 329 330 331 332
    "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) OR (a.server_id = 1) " \
    "WHERE (s.tag = ? OR s.id = 1) " #__VA_ARGS__ \
    " ORDER BY d.id"
#endif

333 334
#ifndef MYSQL_GET_OPTION_COMMON
#define MYSQL_GET_OPTION_COMMON(table_prefix, pd_pool_id, ...) \
335 336 337 338 339 340 341
    "SELECT" \
    "  o.option_id," \
    "  o.code," \
    "  o.value," \
    "  o.formatted_value," \
    "  o.space," \
    "  o.persistent," \
342
    "  o." #table_prefix "_subnet_id," \
343 344 345 346
    "  o.scope_id," \
    "  o.user_context," \
    "  o.shared_network_name," \
    "  o.pool_id," \
347 348
    "  o.modification_ts," \
    "  s.tag " \
349
    pd_pool_id \
350 351 352 353 354 355 356
    "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__ \
    " ORDER BY o.option_id"
357 358 359

#define MYSQL_GET_OPTION4(...) \
    MYSQL_GET_OPTION_COMMON(dhcp4, "", __VA_ARGS__)
360 361
#define MYSQL_GET_OPTION6(...) \
    MYSQL_GET_OPTION_COMMON(dhcp6, ", o.pd_pool_id ", __VA_ARGS__)
362 363
#endif

364 365 366 367 368 369 370 371 372 373
#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," \
    "  r.log_message " \
    "FROM " #table_prefix "_audit AS a " \
374
    "INNER JOIN " #table_prefix "_audit_revision AS r " \
375
    "  ON a.revision_id = r.id " \
376 377 378
    "INNER JOIN " #table_prefix "_server AS s" \
    "  ON r.server_id = s.id " \
    "WHERE (s.tag = ? OR s.id = 1) AND (r.modification_ts > ?) " \
379
    "ORDER BY r.modification_ts, r.id"
380 381
#endif

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
#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

399 400 401 402 403
#ifndef MYSQL_INSERT_GLOBAL_PARAMETER
#define MYSQL_INSERT_GLOBAL_PARAMETER(table_prefix) \
    "INSERT INTO " #table_prefix "_global_parameter(" \
    "  name," \
    "  value," \
404
    "  parameter_type," \
405
    "  modification_ts" \
406
    ") VALUES (?, ?, ?, ?)"
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 432 433 434 435 436
#endif

#ifndef MYSQL_INSERT_GLOBAL_PARAMETER_SERVER
#define MYSQL_INSERT_GLOBAL_PARAMETER_SERVER(table_prefix) \
    "INSERT INTO " #table_prefix "_global_parameter_server(" \
    "  parameter_id," \
    "  server_id," \
    "  modification_ts" \
    ") VALUES (?, (SELECT id FROM " #table_prefix "_server WHERE tag = ?), ?)"
#endif

#ifndef MYSQL_INSERT_SUBNET_SERVER
#define MYSQL_INSERT_SUBNET_SERVER(table_prefix) \
    "INSERT INTO " #table_prefix "_subnet_server(" \
    "  subnet_id," \
    "  server_id," \
    "  modification_ts" \
    ") VALUES (?, (SELECT id FROM " #table_prefix "_server WHERE tag = ?), ?)"
#endif

#ifndef MYSQL_INSERT_POOL
#define MYSQL_INSERT_POOL(table_prefix) \
    "INSERT INTO " #table_prefix "_pool(" \
    "  start_address," \
    "  end_address," \
    "  subnet_id," \
    "  modification_ts" \
    ") VALUES (?, ?, ?, ?)"
#endif

437 438 439 440 441 442
#ifndef MYSQL_INSERT_PD_POOL
#define MYSQL_INSERT_PD_POOL() \
    "INSERT INTO dhcp6_pd_pool(" \
    "  prefix," \
    "  prefix_length," \
    "  delegated_prefix_length," \
443
    "  subnet_id," \
444 445 446 447
    "  modification_ts" \
    ") VALUES (?, ?, ?, ?, ?)"
#endif

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
#ifndef MYSQL_INSERT_SHARED_NETWORK_SERVER
#define MYSQL_INSERT_SHARED_NETWORK_SERVER(table_prefix) \
    "INSERT INTO " #table_prefix "_shared_network_server(" \
    "  shared_network_id," \
    "  server_id," \
    "  modification_ts" \
    ") VALUES (" \
    "    (SELECT id FROM " #table_prefix "_shared_network WHERE name = ?)," \
    "    (SELECT id FROM " #table_prefix "_server WHERE tag = ?), ?" \
    ")"
#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," \
    "  array," \
    "  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," \
    "  server_id," \
    "  modification_ts" \
    ") VALUES (?, (SELECT id FROM " #table_prefix "_server WHERE tag = ?), ?)"
#endif

484 485
#ifndef MYSQL_INSERT_OPTION_COMMON
#define MYSQL_INSERT_OPTION_COMMON(table_prefix, pd_pool_id, last) \
486 487 488 489 490 491 492 493 494 495 496 497 498
    "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" \
499
    pd_pool_id \
500
    ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?" last ")"
501 502 503

#define MYSQL_INSERT_OPTION4() \
    MYSQL_INSERT_OPTION_COMMON(dhcp4, "", "")
504 505
#define MYSQL_INSERT_OPTION6() \
    MYSQL_INSERT_OPTION_COMMON(dhcp6, ", pd_pool_id ", ", ?")
506 507 508 509 510 511 512 513 514 515 516
#endif

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

517 518 519 520 521 522 523 524 525
#ifndef MYSQL_INSERT_SERVER
#define MYSQL_INSERT_SERVER(table_prefix) \
    "INSERT INTO " #table_prefix "_server (" \
    "  tag," \
    "  description," \
    "  modification_ts" \
    ") VALUES (?, ?, ?)"
#endif

526 527 528 529 530 531 532 533 534 535
#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 = ?," \
536
    "  g.parameter_type = ?," \
537 538 539 540 541 542
    "  g.modification_ts = ? " \
    "WHERE s.tag = ? AND g.name = ?"
#endif

#ifndef MYSQL_UPDATE_OPTION_DEF
#define MYSQL_UPDATE_OPTION_DEF(table_prefix) \
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
    "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 = ?," \
    "  d.array = ?," \
    "  d.encapsulate = ?," \
    "  d.record_types = ?," \
    "  d.user_context = ? " \
    "WHERE s.tag = ? AND d.code = ? AND d.space = ?"
559 560
#endif

561 562
#ifndef MYSQL_UPDATE_OPTION_COMMON
#define MYSQL_UPDATE_OPTION_COMMON(table_prefix, pd_pool_id, ...) \
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    "UPDATE " #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 " \
    "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 = ? " \
581
    pd_pool_id \
582
    "WHERE s.tag = ? " #__VA_ARGS__
583 584 585

#define MYSQL_UPDATE_OPTION4(...) \
    MYSQL_UPDATE_OPTION_COMMON(dhcp4, "", __VA_ARGS__)
586 587
#define MYSQL_UPDATE_OPTION6(...) \
    MYSQL_UPDATE_OPTION_COMMON(dhcp6, ", o.pd_pool_id = ? ", __VA_ARGS__)
588 589
#endif

590 591 592 593 594 595 596 597 598 599
#ifndef MYSQL_UPDATE_SERVER
#define MYSQL_UPDATE_SERVER(table_prefix) \
    "UPDATE " #table_prefix "_server " \
    "SET" \
    "  tag = ?," \
    "  description = ?," \
    "  modification_ts = ? " \
    "WHERE tag = ?"
#endif

600 601 602 603 604 605 606 607 608 609
#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

610 611 612 613 614 615 616 617
#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

618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
#ifndef MYSQL_DELETE_SUBNET
#define MYSQL_DELETE_SUBNET(table_prefix, ...) \
    "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 " \
    "WHERE srv.tag = ? " #__VA_ARGS__
#endif

#ifndef MYSQL_DELETE_POOLS
#define MYSQL_DELETE_POOLS(table_prefix) \
    "DELETE FROM " #table_prefix "_pool " \
    "WHERE subnet_id = ?"
#endif

634 635 636
#ifndef MYSQL_DELETE_PD_POOLS
#define MYSQL_DELETE_PD_POOLS() \
    "DELETE FROM dhcp6_pd_pool " \
637
    "WHERE subnet_id = ?"
638 639
#endif

640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
#ifndef MYSQL_DELETE_SHARED_NETWORK
#define MYSQL_DELETE_SHARED_NETWORK(table_prefix, ...) \
    "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 " \
    "WHERE s.tag = ? " #__VA_ARGS__
#endif

#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

#ifndef MYSQL_DELETE_OPTION
#define MYSQL_DELETE_OPTION(table_prefix, ...) \
    "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 " \
    "WHERE s.tag = ? " #__VA_ARGS__
#endif

#ifndef MYSQL_DELETE_OPTION_POOL_RANGE
#define MYSQL_DELETE_OPTION_POOL_RANGE(table_prefix, ...) \
    "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 " \
    "WHERE s.tag = ? " #__VA_ARGS__ \
    "  AND o.pool_id = " \
    "  (SELECT id FROM " #table_prefix "_pool" \
    "   WHERE start_address = ? AND end_address = ?)"
#endif

683 684 685 686 687 688 689 690 691 692 693 694 695
#ifndef MYSQL_DELETE_OPTION_PD_POOL
#define MYSQL_DELETE_OPTION_PD_POOL(...) \
    "DELETE o FROM dhcp6_options AS o " \
    "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 " \
    "WHERE s.tag = ? " #__VA_ARGS__ \
    "  AND o.pd_pool_id = " \
    "  (SELECT id FROM dhcp6_pd_pool" \
    "   WHERE prefix = ? AND prefix_length = ?)"
#endif

696 697 698 699 700 701 702 703 704 705 706 707
#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

708 709 710 711 712 713
} // end of anonymous namespace

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

#endif