SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/ExportDBMS_MySQL.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. class ExportDBMS_MySQL
  17. {
  18.  
  19. public function hdr()
  20. {
  21. return "--\n"
  22. . "-- Sprout3 Database Dump\n"
  23. . "-- Export date: " . date('Y-m-d H:i:s') . "\n"
  24. . "-- Export format: MySQL\n"
  25. . "--\n";
  26. }
  27.  
  28.  
  29. /**
  30.   * Return a query to drop this table
  31.   **/
  32. public function drop($table_def)
  33. {
  34. return "DROP TABLE IF EXISTS `{$table_def->name}`;\n";
  35. }
  36.  
  37.  
  38. /**
  39.   * Remove CONSTRAINT clauses from a CREATE TABLE sql query
  40.   *
  41.   * @param string $create_sql SQL query for CREATE TABLE which may contain CONSTRAINT clauses
  42.   * @return string SQL query which does not contain CONSTRAINT clauses
  43.   */
  44. private static function delConstraintsCreate($create_sql)
  45. {
  46. $open_paren = strpos($create_sql, '(') + 1;
  47. $close_paren = strrpos($create_sql, ')');
  48.  
  49. if ($open_paren === false or $close_paren === false) {
  50. return $create_sql;
  51. }
  52.  
  53. // Split up the column/index/constraint definitions
  54. $column_defs = substr($create_sql, $open_paren, $close_paren - $open_paren);
  55. $column_defs = explode(',', $column_defs);
  56.  
  57. // Remove all constraint definitions
  58. foreach ($column_defs as $index => $def) {
  59. if (strpos($def, 'CONSTRAINT') !== false) {
  60. unset($column_defs[$index]);
  61. }
  62. }
  63.  
  64. // Put the query back together again
  65. $new_create_sql = substr($create_sql, 0, $open_paren);
  66. $new_create_sql .= implode(',', $column_defs);
  67. $new_create_sql .= PHP_EOL . substr($create_sql, $close_paren);
  68.  
  69. return $new_create_sql;
  70. }
  71.  
  72.  
  73. /**
  74.   * Return a query to create the table
  75.   **/
  76. public function structure($table_def)
  77. {
  78. $q = "SHOW CREATE TABLE `{$table_def->name}`";
  79. $res = Pdb::query($q, [], 'row-num');
  80. $create_sql = $res[1];
  81.  
  82. // Importing will fail trying to create tables if tables are out-of-order
  83. // Remove the constraints so that the CREATEs succeed, and then a dbsync can bring them back
  84. $create_sql = self::delConstraintsCreate($create_sql);
  85.  
  86. return $create_sql . ";\n";
  87. }
  88.  
  89.  
  90. /**
  91.   * Create an INSERT query
  92.   **/
  93. public function insert($table_def, $row)
  94. {
  95. $str = "INSERT INTO `{$table_def->name}` SET ";
  96. $str .= $this->createKvpString($row);
  97. $str .= ";\n";
  98.  
  99. return $str;
  100. }
  101.  
  102.  
  103. /**
  104.   * Create an UPDATE query
  105.   **/
  106. public function update($table_def, $pk_names, $row)
  107. {
  108. $pk = array();
  109. foreach ($pk_names as $col) {
  110. $pk[$col] = $row[$col];
  111. }
  112.  
  113. $row = array_diff_key($row, $pk);
  114.  
  115. $str = "UPDATE `{$table_def->name}` SET ";
  116. $str .= $this->createKvpString($row);
  117. $str .= " WHERE ";
  118. $str .= $this->createKvpString($pk, ' AND ');
  119. $str .= ";\n";
  120.  
  121. return $str;
  122. }
  123.  
  124.  
  125. /**
  126.   * Create an INSERT...UPDATE query
  127.   **/
  128. public function insertUpdate($table_def, $pk_names, $row)
  129. {
  130. $row_no_pk = $row;
  131. foreach ($pk_names as $name) {
  132. unset ($row_no_pk[$name]);
  133. }
  134.  
  135. if (count($row_no_pk) == 0) {
  136. $str = "INSERT IGNORE INTO `{$table_def->name}` SET ";
  137. $str .= $this->createKvpString($row);
  138. $str .= ";\n";
  139. } else {
  140. $str = "INSERT INTO `{$table_def->name}` SET ";
  141. $str .= $this->createKvpString($row);
  142. $str .= " ON DUPLICATE KEY UPDATE ";
  143. $str .= $this->createKvpString($row_no_pk);
  144. $str .= ";\n";
  145. }
  146.  
  147. return $str;
  148. }
  149.  
  150.  
  151. /**
  152.   * Creates a key-value-pair string for use in a sql query
  153.   **/
  154. private function createKvpString($row, $sep = ', ')
  155. {
  156. static $conn;
  157. $str = '';
  158.  
  159. if (!$conn) $conn = Pdb::getConnection();
  160.  
  161. $j = 0;
  162. foreach ($row as $key => $val) {
  163. if ($j++ > 0) $str .= $sep;
  164. if ($val === null) {
  165. $val = 'NULL';
  166. } else {
  167. $val = $conn->quote($val);
  168. }
  169. $str .= "`{$key}` = " . $val;
  170. }
  171.  
  172. return $str;
  173. }
  174.  
  175. }
  176.  
  177.