1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino (http://webino.sk) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino/WebinoDbDump/ for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2014-2017 Webino, s. r. o. (http://webino.sk) |
7
|
|
|
* @author Peter Bačinský <[email protected]> |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace WebinoDbDump\Db\Dump\Table; |
12
|
|
|
|
13
|
|
|
use SplFileObject as File; |
14
|
|
|
use WebinoDbDump\Db\Dump\Dump; |
15
|
|
|
use WebinoDbDump\Db\Dump\Adapter; |
16
|
|
|
use WebinoDbDump\Exception; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Base class for a dump utility platform table |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractTable |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Adapter |
30
|
|
|
*/ |
31
|
|
|
protected $adapter; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool|null |
35
|
|
|
*/ |
36
|
|
|
protected $view; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $name |
40
|
|
|
*/ |
41
|
|
|
public function __construct($name, Adapter $adapter) |
42
|
|
|
{ |
43
|
|
|
$this->adapter = $adapter; |
44
|
|
|
$this->setName($name); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return \Zend\Db\ResultSet\ResultSet |
49
|
|
|
*/ |
50
|
|
|
abstract protected function showCreate(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return \Zend\Db\ResultSet\ResultSet |
54
|
|
|
*/ |
55
|
|
|
abstract protected function showColumns(); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return \Zend\Db\ResultSet\ResultSet |
59
|
|
|
*/ |
60
|
|
|
abstract protected function select(); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return ColumnsInterface |
64
|
|
|
*/ |
65
|
|
|
abstract protected function createColumns(); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return ExtraInterface[] |
69
|
|
|
*/ |
70
|
|
|
abstract protected function createExtras(); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
abstract protected function create(); |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
abstract protected function startInsert(); |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
abstract protected function continueInsert(); |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
abstract protected function finishInsert(); |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $name |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
protected function setName($name) |
97
|
|
|
{ |
98
|
|
|
$this->name = $name; |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return bool |
104
|
|
|
* @throws Exception\RuntimeException |
105
|
|
|
*/ |
106
|
|
|
protected function isView() |
107
|
|
|
{ |
108
|
|
|
if (null === $this->view) { |
109
|
|
|
throw new Exception\RuntimeException('Don\'t know if is view'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->view; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param bool $view |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
protected function setView($view) |
120
|
|
|
{ |
121
|
|
|
$this->view = (bool) $view; |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return \Zend\Db\Adapter\Platform\PlatformInterface |
127
|
|
|
*/ |
128
|
|
|
protected function getPlatform() |
129
|
|
|
{ |
130
|
|
|
return $this->adapter->getPlatform(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param File $file |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function write(File $file) |
138
|
|
|
{ |
139
|
|
|
$file->fwrite($this->create()); |
140
|
|
|
$this->isView() or $this->writeData($file); |
141
|
|
|
|
142
|
|
|
foreach ($this->createExtras() as $extra) { |
143
|
|
|
$extra->writeIfAny($file); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param File $file |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
|
|
private function writeData(File $file) |
154
|
|
|
{ |
155
|
|
|
$data = $this->select(); |
156
|
|
|
if (!$data->count()) { |
|
|
|
|
157
|
|
|
// nothing to write |
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$columns = $this->createColumnsInternal(); |
162
|
|
|
$maxIndex = $data->count() - 1; |
163
|
|
|
$sqlSize = 0; |
164
|
|
|
|
165
|
|
|
foreach ($data as $index => $row) { |
166
|
|
|
$line = ''; |
167
|
|
|
|
168
|
|
|
if (0 === $sqlSize) { |
169
|
|
|
$line .= $this->startInsert(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$line .= $columns->tableRowValues($row); |
|
|
|
|
173
|
|
|
|
174
|
|
|
$sqlSize += strlen($line); |
175
|
|
|
if ($index >= $maxIndex |
176
|
|
|
|| $sqlSize > Dump::MAX_SQL_SIZE |
177
|
|
|
) { |
178
|
|
|
$sqlSize = 0; |
179
|
|
|
$line .= $this->finishInsert(); |
180
|
|
|
} else { |
181
|
|
|
$line .= $this->continueInsert(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$file->fwrite($line . PHP_EOL); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return ColumnsInterface |
192
|
|
|
*/ |
193
|
|
|
private function createColumnsInternal() |
194
|
|
|
{ |
195
|
|
|
$columns = $this->createColumns(); |
196
|
|
|
foreach ($this->showColumns() as $column) { |
197
|
|
|
$columns->addColumn($column); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $columns; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: