Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
DbDumper.php
1 <?php
2 /**
3  * Gentics Portal.Node PHP
4  * Author & Copyright (c) by Gentics Software GmbH
5  * sales@gentics.com
6  * http://www.gentics.com
7  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
8  * You must not use this software without a valid license agreement.
9  *
10  * Creates DB dump.
11  */
12 class DbDumper
13 {
14  private $_constraints;
15 
16  /**
17  * Dump all tables
18  *
19  * @return file|strings
20  */
21  public function getDump()
22  {
23  ob_start();
24  foreach ($this->_getTables() as $key => $val) {
25  $this->_dumpTable($key);
26  }
27  $result = $this->_setHeader();
28  $result .= ob_get_contents();
29  $result .= $this->_getConstraints();
30  $result .= $this->_setFooter();
31  ob_end_clean();
32 
33  return $result;
34  }
35 
36  /**
37  * Generate constraints to all tables
38  *
39  * @return string
40  */
41  private function _getConstraints()
42  {
43  $sql = "--\r\n-- Constraints for dumped tables\r\n--" . PHP_EOL . PHP_EOL;
44  $first = true;
45  foreach ($this->_constraints as $key => $value) {
46  if ($first && count($value[0]) > 0) {
47  $sql .= "--\r\n-- Constraints for table `$key`\r\n--" . PHP_EOL . PHP_EOL;
48  $sql .= "ALTER TABLE $key" . PHP_EOL;
49  }
50  if (count($value[0]) > 0) {
51  for ($i = 0; $i < count($value[0]); $i++) {
52  if (strpos($value[0][$i], 'CONSTRAINT') === false) {
53  $sql .= preg_replace('/(FOREIGN[\s]+KEY)/', "\tADD $1", $value[0][$i]);
54  } else {
55  $sql .= preg_replace('/(CONSTRAINT)/', "\tADD $1", $value[0][$i]);
56  }
57  if ($i == count($value[0]) - 1) {
58  $sql .= ";" . PHP_EOL;
59  }
60  if ($i < count($value[0]) - 1) {
61  $sql .= PHP_EOL;
62  }
63  }
64  }
65  }
66 
67  return $sql;
68  }
69 
70 
71  /**
72  * Set sql file header
73  *
74  * @return string
75  */
76  private function _setHeader()
77  {
78  $header = '';
79  return $header;
80  }
81 
82 
83  /**
84  * Set sql file footer
85  *
86  * @return string
87  */
88  private function _setFooter()
89  {
90  $footer = '';
91  return $footer;
92  }
93 
94 
95  /**
96  * Create table dump
97  *
98  * @param string $tableName table name
99  *
100  * @return mixed
101  */
102  private function _dumpTable($tableName)
103  {
104  $db = Yii::app()->db;
105  $pdo = $db->getPdoInstance();
106 
107  echo PHP_EOL . "--\n-- Structure for table `$tableName`\n--" . PHP_EOL;
108  echo PHP_EOL . 'DROP TABLE IF EXISTS ' . $db->quoteTableName($tableName) . ';' . PHP_EOL . PHP_EOL;
109 
110  $q = $db->createCommand('SHOW CREATE TABLE ' . $db->quoteTableName($tableName) . ';')->queryRow();
111 
112  $create_query = $q['Create Table'];
113 
114  $pattern = '/CONSTRAINT.*|FOREIGN[\s]+KEY/';
115 
116  // constraints to $tablename
117  preg_match_all($pattern, $create_query, $this->_constraints[$tableName]);
118 
119  echo $create_query . ';' . PHP_EOL;
120 
121  $rows = $db->createCommand('SELECT * FROM ' . $db->quoteTableName($tableName) . ';')->queryAll();
122 
123  if (empty($rows)) {
124  return;
125  }
126 
127  echo PHP_EOL . "--\n-- Data for table `$tableName`\n--" . PHP_EOL . PHP_EOL;
128 
129  $attrs = array_map(array($db, 'quoteColumnName'), array_keys($rows[0]));
130  echo 'INSERT INTO ' . $db->quoteTableName($tableName) . '' . " (", implode(', ', $attrs), ') VALUES' . PHP_EOL;
131  $i = 0;
132  $rowsCount = count($rows);
133  foreach ($rows AS $row) {
134  // Process row
135  foreach ($row AS $key => $value) {
136  if ($value === null) {
137  $row[$key] = 'NULL';
138  } else {
139  $row[$key] = $pdo->quote($value);
140  }
141  }
142 
143  echo " (", implode(', ', $row), ')';
144  if ($i < $rowsCount - 1) {
145  echo ',';
146  } else {
147  echo ';';
148  }
149  echo PHP_EOL;
150  $i++;
151  }
152  echo PHP_EOL;
153  echo PHP_EOL;
154  }
155 
156 
157  /**
158  * Get mysql tables list
159  *
160  * @return array
161  */
162  private function _getTables()
163  {
164  $db = Yii::app()->db;
165  return $db->getSchema()->getTables();
166  }
167 }