PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-content/plugins/wordpress-seo/vendor_prefixed/ruckusing/lib/Ruckusing/Adapter/MySQL/TableDefinition.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 268 lines | 137 code | 2 blank | 129 comment | 34 complexity | 0f7fb4576a31384c5dd0dd2283d16318 MD5 | raw file
  1. <?php
  2. namespace YoastSEO_Vendor;
  3. /**
  4. * Ruckusing
  5. *
  6. * @category Ruckusing
  7. * @package Ruckusing_Adapter
  8. * @subpackage MySQL
  9. * @author Cody Caughlan <codycaughlan % gmail . com>
  10. * @link https://github.com/ruckus/ruckusing-migrations
  11. */
  12. /**
  13. * Ruckusing_Adapter_MySQL_TableDefinition
  14. *
  15. * @category Ruckusing
  16. * @package Ruckusing_Adapter
  17. * @subpackage MySQL
  18. * @author Cody Caughlan <codycaughlan % gmail . com>
  19. * @link https://github.com/ruckus/ruckusing-migrations
  20. */
  21. class Ruckusing_Adapter_MySQL_TableDefinition
  22. {
  23. /**
  24. * adapter MySQL
  25. *
  26. * @var Ruckusing_Adapter_Mysql_Base
  27. */
  28. private $_adapter;
  29. /**
  30. * Name
  31. *
  32. * @var string
  33. */
  34. private $_name;
  35. /**
  36. * options
  37. *
  38. * @var array
  39. */
  40. private $_options;
  41. /**
  42. * sql
  43. *
  44. * @var string
  45. */
  46. private $_sql = "";
  47. /**
  48. * initialized
  49. *
  50. * @var boolean
  51. */
  52. private $_initialized = \false;
  53. /**
  54. * Columns
  55. *
  56. * @var array
  57. */
  58. private $_columns = array();
  59. /**
  60. * Table definition
  61. *
  62. * @var array
  63. */
  64. private $_table_def;
  65. /**
  66. * primary keys
  67. *
  68. * @var array
  69. */
  70. private $_primary_keys = array();
  71. /**
  72. * auto generate id
  73. *
  74. * @var boolean
  75. */
  76. private $_auto_generate_id = \true;
  77. /**
  78. * Creates an instance of Ruckusing_Adapters_MySQL_Adapter
  79. *
  80. * @param Ruckusing_Adapter_MySQL_Base $adapter the current adapter
  81. * @param string $name the table name
  82. * @param array $options the options
  83. *
  84. * @throws Ruckusing_Exception
  85. * @return Ruckusing_Adapter_MySQL_TableDefinition
  86. */
  87. public function __construct($adapter, $name, $options = array())
  88. {
  89. //sanity check
  90. if (!$adapter instanceof \YoastSEO_Vendor\Ruckusing_Adapter_MySQL_Base) {
  91. throw new \YoastSEO_Vendor\Ruckusing_Exception("Invalid MySQL Adapter instance.", \YoastSEO_Vendor\Ruckusing_Exception::INVALID_ADAPTER);
  92. }
  93. if (!$name) {
  94. throw new \YoastSEO_Vendor\Ruckusing_Exception("Invalid 'name' parameter", \YoastSEO_Vendor\Ruckusing_Exception::INVALID_ARGUMENT);
  95. }
  96. $this->_adapter = $adapter;
  97. $this->_name = $name;
  98. $this->_options = $options;
  99. $this->init_sql($name, $options);
  100. $this->_table_def = new \YoastSEO_Vendor\Ruckusing_Adapter_TableDefinition($this->_adapter, $this->_options);
  101. if (\array_key_exists('id', $options)) {
  102. if (\is_bool($options['id']) && $options['id'] == \false) {
  103. $this->_auto_generate_id = \false;
  104. }
  105. //if its a string then we want to auto-generate an integer-based
  106. //primary key with this name
  107. if (\is_string($options['id'])) {
  108. $this->_auto_generate_id = \true;
  109. $this->_primary_keys[] = $options['id'];
  110. }
  111. }
  112. }
  113. /*
  114. public function primary_key($name, $auto_increment)
  115. {
  116. $options = array('auto_increment' => $auto_increment);
  117. $this->column($name, "primary_key", $options);
  118. }
  119. */
  120. /**
  121. * Create a column
  122. *
  123. * @param string $column_name the column name
  124. * @param string $type the column type
  125. * @param array $options
  126. */
  127. public function column($column_name, $type, $options = array())
  128. {
  129. //if there is already a column by the same name then silently fail
  130. //and continue
  131. if ($this->_table_def->included($column_name) == \true) {
  132. return;
  133. }
  134. $column_options = array();
  135. if (\array_key_exists('primary_key', $options)) {
  136. if ($options['primary_key'] == \true) {
  137. $this->_primary_keys[] = $column_name;
  138. }
  139. }
  140. if (\array_key_exists('auto_increment', $options)) {
  141. if ($options['auto_increment'] == \true) {
  142. $column_options['auto_increment'] = \true;
  143. }
  144. }
  145. $column_options = \array_merge($column_options, $options);
  146. $column = new \YoastSEO_Vendor\Ruckusing_Adapter_ColumnDefinition($this->_adapter, $column_name, $type, $column_options);
  147. $this->_columns[] = $column;
  148. }
  149. //column
  150. /**
  151. * Shortcut to create timestamps columns (default created_at, updated_at)
  152. *
  153. * @param string $created_column_name Created at column name
  154. * @param string $updated_column_name Updated at column name
  155. *
  156. */
  157. public function timestamps($created_column_name = "created_at", $updated_column_name = "updated_at")
  158. {
  159. $this->column($created_column_name, "datetime");
  160. $this->column($updated_column_name, "timestamp", array("null" => \false, 'default' => 'CURRENT_TIMESTAMP', 'extra' => 'ON UPDATE CURRENT_TIMESTAMP'));
  161. }
  162. /**
  163. * Get all primary keys
  164. *
  165. * @return string
  166. */
  167. private function keys()
  168. {
  169. if (\count($this->_primary_keys) > 0) {
  170. $lead = ' PRIMARY KEY (';
  171. $quoted = array();
  172. foreach ($this->_primary_keys as $key) {
  173. $quoted[] = \sprintf("%s", $this->_adapter->identifier($key));
  174. }
  175. $primary_key_sql = ",\n" . $lead . \implode(",", $quoted) . ")";
  176. return $primary_key_sql;
  177. } else {
  178. return '';
  179. }
  180. }
  181. /**
  182. * Table definition
  183. *
  184. * @param boolean $wants_sql
  185. *
  186. * @throws Ruckusing_Exception
  187. * @return boolean | string
  188. */
  189. public function finish($wants_sql = \false)
  190. {
  191. if ($this->_initialized == \false) {
  192. throw new \YoastSEO_Vendor\Ruckusing_Exception(\sprintf("Table Definition: '%s' has not been initialized", $this->_name), \YoastSEO_Vendor\Ruckusing_Exception::INVALID_TABLE_DEFINITION);
  193. }
  194. $opt_str = '';
  195. if (\is_array($this->_options) && \array_key_exists('options', $this->_options)) {
  196. $opt_str = $this->_options['options'];
  197. } else {
  198. if (isset($this->_adapter->db_info['charset'])) {
  199. $opt_str = " DEFAULT CHARSET=" . $this->_adapter->db_info['charset'];
  200. } else {
  201. $opt_str = " DEFAULT CHARSET=utf8";
  202. }
  203. }
  204. $close_sql = \sprintf(") %s;", $opt_str);
  205. $create_table_sql = $this->_sql;
  206. if ($this->_auto_generate_id === \true) {
  207. $this->_primary_keys[] = 'id';
  208. $primary_id = new \YoastSEO_Vendor\Ruckusing_Adapter_ColumnDefinition($this->_adapter, 'id', 'integer', array('unsigned' => \true, 'null' => \false, 'auto_increment' => \true));
  209. $create_table_sql .= $primary_id->to_sql() . ",\n";
  210. }
  211. $create_table_sql .= $this->columns_to_str();
  212. $create_table_sql .= $this->keys() . $close_sql;
  213. if ($wants_sql) {
  214. return $create_table_sql;
  215. } else {
  216. return $this->_adapter->execute_ddl($create_table_sql);
  217. }
  218. }
  219. //finish
  220. /**
  221. * get all columns
  222. *
  223. * @return string
  224. */
  225. private function columns_to_str()
  226. {
  227. $str = "";
  228. $fields = array();
  229. $len = \count($this->_columns);
  230. for ($i = 0; $i < $len; $i++) {
  231. $c = $this->_columns[$i];
  232. $fields[] = $c->__toString();
  233. }
  234. return \join(",\n", $fields);
  235. }
  236. /**
  237. * Init create sql
  238. *
  239. * @param string $name
  240. * @param array $options
  241. * @throws Exception
  242. * @throws Ruckusing_Exception
  243. */
  244. private function init_sql($name, $options)
  245. {
  246. //are we forcing table creation? If so, drop it first
  247. if (\array_key_exists('force', $options) && $options['force'] == \true) {
  248. try {
  249. $this->_adapter->drop_table($name);
  250. } catch (\YoastSEO_Vendor\Ruckusing_Exception $e) {
  251. if ($e->getCode() != \YoastSEO_Vendor\Ruckusing_Exception::MISSING_TABLE) {
  252. throw $e;
  253. }
  254. //do nothing
  255. }
  256. }
  257. $temp = "";
  258. if (\array_key_exists('temporary', $options)) {
  259. $temp = " TEMPORARY";
  260. }
  261. $create_sql = \sprintf("CREATE%s TABLE ", $temp);
  262. $create_sql .= \sprintf("%s (\n", $this->_adapter->identifier($name));
  263. $this->_sql .= $create_sql;
  264. $this->_initialized = \true;
  265. }
  266. }