/lib/Magento/Db/Sql/Trigger.php

https://github.com/speedupmate/Magento-CE-Mirror · PHP · 353 lines · 159 code · 28 blank · 166 comment · 15 complexity · f8839147e4b617a7f6676c40457d5104 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Magento
  22. * @package Magento_Db
  23. * @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Magento_Db_Sql_Trigger
  28. *
  29. * @category Magento
  30. * @package Magento_Db
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Magento_Db_Sql_Trigger
  34. {
  35. const NAME = 'name';
  36. const TARGET = 'target';
  37. const TIME = 'time';
  38. const EVENT = 'event';
  39. const SCOPE = 'scope';
  40. const BODY = 'body';
  41. /**
  42. * SQL constants
  43. */
  44. const SQL_TIME_BEFORE = 'BEFORE';
  45. const SQL_TIME_AFTER = 'AFTER';
  46. const SQL_EVENT_INSERT = 'INSERT';
  47. const SQL_EVENT_UPDATE = 'UPDATE';
  48. const SQL_EVENT_DELETE = 'DELETE';
  49. const SQL_FOR_EACH_ROW = 'FOR EACH ROW';
  50. /**
  51. * Trigger parts
  52. *
  53. * @var array
  54. */
  55. protected $_parts = array();
  56. /**
  57. * Allowed time types
  58. *
  59. * @var array
  60. */
  61. protected $_timeTypes = array(
  62. self::SQL_TIME_AFTER,
  63. self::SQL_TIME_BEFORE
  64. );
  65. /**
  66. * Allowed event types
  67. *
  68. * @var array
  69. */
  70. protected $_eventTypes = array(
  71. self::SQL_EVENT_INSERT,
  72. self::SQL_EVENT_UPDATE,
  73. self::SQL_EVENT_DELETE
  74. );
  75. /**
  76. * Initial trigger structure, for MySQL scope is always "FOR EACH ROW".
  77. * Time "AFTER" is default
  78. *
  79. * @var array
  80. */
  81. protected static $_partsInit = array(
  82. self::TARGET => null,
  83. self::TIME => self::SQL_TIME_AFTER,
  84. self::EVENT => null,
  85. self::SCOPE => self::SQL_FOR_EACH_ROW,
  86. self::BODY => array()
  87. );
  88. /**
  89. * Constructor
  90. */
  91. public function __construct()
  92. {
  93. $this->_parts = self::$_partsInit;
  94. }
  95. /**
  96. * Validate where all trigger parts set?
  97. *
  98. * @return Magento_Db_Sql_Trigger
  99. * @throws Exception
  100. */
  101. protected function _validateIsComplete()
  102. {
  103. foreach (array_keys(self::$_partsInit) as $part) {
  104. if (empty($this->_parts[$part])) {
  105. throw new Exception('Part [' . $part . '] should be set');
  106. }
  107. }
  108. return $this;
  109. }
  110. /**
  111. * Set trigger part
  112. *
  113. * @param $part
  114. * @param $value
  115. * @return Magento_Db_Sql_Trigger
  116. * @throws InvalidArgumentException
  117. */
  118. protected function _setPart($part, $value)
  119. {
  120. if ($value != null) {
  121. $this->_parts[$part] = $value;
  122. } else {
  123. throw new InvalidArgumentException('Part [' . $part . '] can not be empty');
  124. }
  125. return $this;
  126. }
  127. /**
  128. * Set trigger part
  129. *
  130. * @param $part
  131. * @return string|array
  132. * @throws Exception
  133. */
  134. protected function _getPart($part)
  135. {
  136. if (isset($this->_parts[$part])) {
  137. return $this->_parts[$part];
  138. }
  139. throw new Exception('Part [' . $part . '] does\'t exists');
  140. }
  141. /**
  142. * Set body part to trigger
  143. *
  144. * @param $part
  145. * @param $value
  146. * @return Magento_Db_Sql_Trigger
  147. * @throws InvalidArgumentException
  148. */
  149. public function setBodyPart($part, $value)
  150. {
  151. if ($value != null) {
  152. $this->_parts[self::BODY][$part] = $value;
  153. } else {
  154. throw new InvalidArgumentException('Part [' . $part . '] can not be empty');
  155. }
  156. return $this;
  157. }
  158. /**
  159. * Set body part to trigger
  160. *
  161. * @param string $part
  162. * @return string
  163. * @throws Exception
  164. */
  165. public function getBodyPart($part)
  166. {
  167. if (isset($this->_parts[self::BODY][$part])) {
  168. return $this->_parts[self::BODY][$part];
  169. }
  170. throw new Exception('Part [' . $part . '] does\'t exists');
  171. }
  172. /**
  173. * Generate trigger name
  174. *
  175. * @return string
  176. */
  177. protected function _generateTriggerName()
  178. {
  179. return strtolower('trg_' . $this->_parts[self::TARGET]
  180. . '_' . $this->_parts[self::TIME]
  181. . '_' . $this->_parts[self::EVENT]);
  182. }
  183. /**
  184. * Set trigger time {BEFORE/AFTER}
  185. * @param $time
  186. * @return Magento_Db_Sql_Trigger
  187. * @throws InvalidArgumentException
  188. */
  189. public function setTime($time)
  190. {
  191. if (in_array($time, $this->getTimeTypes())) {
  192. $this->_setPart(self::TIME, $time);
  193. } else {
  194. throw new InvalidArgumentException('Unsupported time type!');
  195. }
  196. return $this;
  197. }
  198. /**
  199. * Set trigger event {INSERT/UPDATE/DELETE}
  200. *
  201. * @param $event
  202. * @return Magento_Db_Sql_Trigger
  203. * @throws InvalidArgumentException
  204. */
  205. public function setEvent($event)
  206. {
  207. if (in_array($event, $this->getEventTypes())) {
  208. $this->_setPart(self::EVENT, $event);
  209. } else {
  210. throw new InvalidArgumentException('Unsupported event type!');
  211. }
  212. return $this;
  213. }
  214. /**
  215. * Set trigger target, table name
  216. *
  217. * @param $target
  218. * @return Magento_Db_Sql_Trigger
  219. */
  220. public function setTarget($target)
  221. {
  222. $this->_setPart(self::TARGET, $target);
  223. return $this;
  224. }
  225. /**
  226. * Set trigger name
  227. *
  228. * @param $name
  229. * @return Magento_Db_Sql_Trigger
  230. */
  231. public function setName($name)
  232. {
  233. $this->_setPart(self::NAME, $name);
  234. return $this;
  235. }
  236. /**
  237. * Retrieve trigger name.
  238. * If trigger name does not exists generate it by template 'trg_{TARGET}_{TIME}_{EVENT}'.
  239. *
  240. * @return mixed
  241. */
  242. public function getName()
  243. {
  244. if (empty($this->_parts[self::NAME])) {
  245. $this->_parts[self::NAME] = $this->_generateTriggerName();
  246. }
  247. return $this->_parts[self::NAME];
  248. }
  249. /**
  250. * Set trigger body
  251. *
  252. * @param array|string $body
  253. * @return Magento_Db_Sql_Trigger
  254. */
  255. public function setBody($body)
  256. {
  257. if (!is_array($body)) {
  258. $body = array($body);
  259. }
  260. $this->_setPart(self::BODY, $body);
  261. return $this;
  262. }
  263. /**
  264. * Get body parts of trigger
  265. *
  266. * @return array
  267. */
  268. public function getBody()
  269. {
  270. return $this->_getPart(self::BODY);
  271. }
  272. /**
  273. * Get trigger creating SQL script
  274. *
  275. * @return string
  276. */
  277. public function assemble()
  278. {
  279. $this->_validateIsComplete();
  280. return "CREATE TRIGGER "
  281. . $this->getName() . "\n"
  282. . $this->_parts[self::TIME] . " " . $this->_parts[self::EVENT] . "\n"
  283. . "ON " . $this->_parts[self::TARGET] . " " . $this->_parts[self::SCOPE] . "\n"
  284. . "BEGIN\n"
  285. . implode("\n", $this->_parts[self::BODY]) . "\n"
  286. . "END;\n";
  287. }
  288. /**
  289. * Implement magic method
  290. *
  291. * @return string
  292. */
  293. public function __toString()
  294. {
  295. return $this->assemble();
  296. }
  297. /**
  298. * Retrieve list of allowed events
  299. *
  300. * @return array
  301. */
  302. public function getEventTypes()
  303. {
  304. return $this->_eventTypes;
  305. }
  306. /**
  307. * Retrieve list of allowed time types
  308. *
  309. * @return array
  310. */
  311. public function getTimeTypes()
  312. {
  313. return $this->_timeTypes;
  314. }
  315. /**
  316. * Reset trigger parts
  317. *
  318. * @return Magento_Db_Sql_Trigger
  319. */
  320. public function reset()
  321. {
  322. $this->_parts = self::$_partsInit;
  323. return $this;
  324. }
  325. }