bnghtz

Flag system in SQL

Nov 21st, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. MySQL can handle hexa digits well (0x0020 == 32 == b0100000)
  2. Hexa digits are easier to read and write, than binary numbers. Actually it processes the binary logical operations.
  3. flag_a = 0x0001
  4. flag_b = 0x0002
  5. flag_c = 0x0004
  6. flag_d = 0x0008
  7. flag_e = 0x0010
  8. flag_f = 0x0020
  9. ...
  10.  
  11.  
  12. Theorem:
  13. ^^^^^^^
  14.  
  15. bit union: |
  16. 0100 (4)
  17. 0010 (2)
  18. -----------
  19. | 0110 (6)
  20.  
  21. bit section: &
  22. 0110 (6)
  23. 0010 (2)
  24. -----------
  25. & 0010 (2)
  26.  
  27. bit abstraction: & ~
  28. 0110 (6)
  29. 0010 (2)
  30. .........
  31. 0110 (6)
  32. 1101 (~2)
  33. -----------
  34. &~0100 (4)
  35.  
  36.  
  37.  
  38. Examples with numbers:
  39. ^^^^^^^^^^^^^^^^^^^^^
  40.  
  41. CHECK:
  42. SELECT 33 & 0x0020; # is flag set? -> yes 32
  43. SELECT 12 & 0x0020; # is flag set? -> no 0
  44.  
  45. SET:
  46. SELECT (33 | 0x0020); # -> 33
  47. SELECT (10 | 0x0020); # -> 42
  48.  
  49. DELETE:
  50. SELECT (33 & ~ 0x0020); # -> 1
  51.  
  52. TOGGLE:
  53. SELECT (33 ^ 0x0020); # -> 1
  54. SELECT ((33 ^ 0x0020) ^ 0x0020); # -> 33
  55.  
  56.  
  57.  
  58. Database examples:
  59. ^^^^^^^^^^^^^^^^^
  60.  
  61. [ CHECK ]
  62. Is flag set?
  63. SELECT * FROM table WHERE flags & 0x0020 = 0x0020 ; # this is nice and readable
  64. SELECT * FROM table WHERE (flags & 0x0020); # this is easy
  65.  
  66. Is flag unset?
  67. SELECT * FROM table WHERE flags & 0x0020 = 0; # this is nice and readable
  68. SELECT * FROM table WHERE !(flags & 0x0020); # this is easy
  69.  
  70. [ SET ]
  71. UPDATE table SET flags = flags | 0x0020;
  72.  
  73. [ DELETE ]
  74. UPDATE table SET flags = flags & ~0x0020;
  75.  
  76. [ TOGGLE ]
  77. UPDATE table SET flags = flags ^ 0x0020;
  78.  
  79.  
  80.  
  81. [ Multiple flag CHECKing ]
  82. Let user privileges
  83. PRIV_ADMIN = 0x0001;
  84. PRIV_LEARNER = 0x0008;
  85.  
  86. Get users which have ADMIN or LEARNER privileges!
  87. SELECT * FROM users WHERE priv & (0x0008 | 0x0001) = (0x0008 | 0x0001);
  88.  
  89. Get users which have both ADMIN and LEARNER privileges!
  90. SELECT * FROM users WHERE priv & (0x0008 | 0x0001) > 0; # this is nice and readable
  91. SELECT * FROM users WHERE (priv & (0x0008 | 0x0001)); # this is easy
  92.  
  93. Get users which have nor ADMIN, nor LEARNER privileges!
  94. SELECT * FROM users WHERE priv & (0x0008 | 0x0001) = 0; # this is nice and readable
  95. SELECT * FROM users WHERE !(priv & (0x0008 | 0x0001)); # this is easy
  96.  
  97. [ Multiple flag SETting ]
  98. Let user be an ADMIN and a LEARNER too!
  99. UPDATE users SET priv = priv | (0x0008 | 0x0001);
  100.  
  101. [ Multiple flag DELETEing ]
  102. Delete ADMIN and LEARNER privilege of the user!
  103. UPDATE users SET priv = priv & ~(0x0008 | 0x0001);
  104.  
  105. [ SET and DELETE flags at the same time ]
  106. Give the user ADMIN privilege but take LEARNER privilege away!
  107. UPDATE users SET priv = priv | (0x0001) & ~ (0x0008);
  108.  
  109. In this case we can use multiple flags to SET and UNSET using the | operation.
  110. UPDATE users SET priv = priv | (0x0001 | 0x0020) & ~ (0x0008 | 0x0040);
Advertisement
Add Comment
Please, Sign In to add comment