DBAccessObjectUtils.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * This file contains database access object related constants.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Database
  22. */
  23. /**
  24. * Helper class for DAO classes
  25. *
  26. * @since 1.26
  27. */
  28. class DBAccessObjectUtils implements IDBAccessObject {
  29. /**
  30. * @param int $bitfield
  31. * @param int $flags IDBAccessObject::READ_* constant
  32. * @return bool Bitfield has flag $flag set
  33. */
  34. public static function hasFlags( $bitfield, $flags ) {
  35. return ( $bitfield & $flags ) == $flags;
  36. }
  37. /**
  38. * Get an appropriate DB index, options, and fallback DB index for a query
  39. *
  40. * The fallback DB index and options are to be used if the entity is not found
  41. * with the initial DB index, typically querying the master DB to avoid lag
  42. *
  43. * @param int $bitfield Bitfield of IDBAccessObject::READ_* constants
  44. * @return array List of DB indexes and options in this order:
  45. * - DB_MASTER or DB_REPLICA constant for the initial query
  46. * - SELECT options array for the initial query
  47. * - DB_MASTER constant for the fallback query; null if no fallback should happen
  48. * - SELECT options array for the fallback query; empty if no fallback should happen
  49. */
  50. public static function getDBOptions( $bitfield ) {
  51. if ( self::hasFlags( $bitfield, self::READ_LATEST_IMMUTABLE ) ) {
  52. $index = DB_REPLICA; // override READ_LATEST if set
  53. $fallbackIndex = DB_MASTER;
  54. } elseif ( self::hasFlags( $bitfield, self::READ_LATEST ) ) {
  55. $index = DB_MASTER;
  56. $fallbackIndex = null;
  57. } else {
  58. $index = DB_REPLICA;
  59. $fallbackIndex = null;
  60. }
  61. $lockingOptions = [];
  62. if ( self::hasFlags( $bitfield, self::READ_EXCLUSIVE ) ) {
  63. $lockingOptions[] = 'FOR UPDATE';
  64. } elseif ( self::hasFlags( $bitfield, self::READ_LOCKING ) ) {
  65. $lockingOptions[] = 'LOCK IN SHARE MODE';
  66. }
  67. if ( $fallbackIndex !== null ) {
  68. $options = []; // locks on DB_REPLICA make no sense
  69. $fallbackOptions = $lockingOptions;
  70. } else {
  71. $options = $lockingOptions;
  72. $fallbackOptions = []; // no fallback
  73. }
  74. return [ $index, $options, $fallbackIndex, $fallbackOptions ];
  75. }
  76. }