1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
You may not change or alter any portion of this comment or credits |
4
|
|
|
of supporting developers from this source code or any supporting source code |
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xmf\Database; |
13
|
|
|
|
14
|
|
|
use Xmf\Yaml; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Xmf\Database\TableLoad |
18
|
|
|
* |
19
|
|
|
* load a database table |
20
|
|
|
* |
21
|
|
|
* @category Xmf\Database\TableLoad |
22
|
|
|
* @package Xmf |
23
|
|
|
* @author Richard Griffith <[email protected]> |
24
|
|
|
* @copyright 2013-2016 XOOPS Project (http://xoops.org) |
25
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
26
|
|
|
* @version Release: 1.0 |
27
|
|
|
* @link http://xoops.org |
28
|
|
|
*/ |
29
|
|
|
class TableLoad |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* loadTableFromArray |
34
|
|
|
* |
35
|
|
|
* @param string $table name of table to load without prefix |
36
|
|
|
* @param array $data array of rows to insert |
37
|
|
|
* Each element of the outer array represents a single table row. |
38
|
|
|
* Each row is an associative array in 'column' => 'value' format. |
39
|
|
|
* |
40
|
|
|
* @return int number of rows inserted |
41
|
|
|
*/ |
42
|
|
|
public static function loadTableFromArray($table, $data) |
43
|
|
|
{ |
44
|
|
|
/** @var \XoopsDatabase */ |
45
|
|
|
$db = \XoopsDatabaseFactory::getDatabaseConnection(); |
46
|
|
|
|
47
|
|
|
$prefixedTable = $db->prefix($table); |
48
|
|
|
$count = 0; |
49
|
|
|
|
50
|
|
|
foreach ($data as $row) { |
51
|
|
|
$insertInto = 'INSERT INTO ' . $prefixedTable . ' ('; |
52
|
|
|
$valueClause = ' VALUES ('; |
53
|
|
|
$first = true; |
54
|
|
|
foreach ($row as $column => $value) { |
55
|
|
|
if ($first) { |
56
|
|
|
$first = false; |
57
|
|
|
} else { |
58
|
|
|
$insertInto .= ', '; |
59
|
|
|
$valueClause .= ', '; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$insertInto .= $column; |
63
|
|
|
$valueClause .= $db->quote($value); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$sql = $insertInto . ') ' . $valueClause . ')'; |
67
|
|
|
|
68
|
|
|
$result = $db->queryF($sql); |
69
|
|
|
if (false !== $result) { |
70
|
|
|
++$count; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $count; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* loadTableFromYamlFile |
79
|
|
|
* |
80
|
|
|
* @param string $table name of table to load without prefix |
81
|
|
|
* @param string $yamlFile name of file containing data dump in YAML format |
82
|
|
|
* |
83
|
|
|
* @return int number of rows inserted |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public static function loadTableFromYamlFile($table, $yamlFile) |
86
|
|
|
{ |
87
|
|
|
$count = 0; |
88
|
|
|
|
89
|
|
|
$data = Yaml::loadWrapped($yamlFile); // work with phpmyadmin YAML dumps |
90
|
|
|
if (false !== $data) { |
91
|
|
|
$count = static::loadTableFromArray($table, $data); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $count; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* truncateTable - empty a database table |
99
|
|
|
* |
100
|
|
|
* @param string $table name of table to truncate |
101
|
|
|
* |
102
|
|
|
* @return int number of affected rows |
103
|
|
|
*/ |
104
|
|
|
public static function truncateTable($table) |
105
|
|
|
{ |
106
|
|
|
/** @var \XoopsDatabase */ |
107
|
|
|
$db = \XoopsDatabaseFactory::getDatabaseConnection(); |
108
|
|
|
|
109
|
|
|
$prefixedTable = $db->prefix($table); |
110
|
|
|
$sql = 'TRUNCATE TABLE ' . $prefixedTable; |
111
|
|
|
$result = $db->queryF($sql); |
112
|
|
|
if (false !== $result) { |
113
|
|
|
$result = $db->getAffectedRows(); |
114
|
|
|
} |
115
|
|
|
return $result; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* countRows - get count of rows in a table |
120
|
|
|
* |
121
|
|
|
* @param string $table name of table to count |
122
|
|
|
* @param \CriteriaElement $criteria optional criteria |
123
|
|
|
* |
124
|
|
|
* @return int number of rows |
125
|
|
|
*/ |
126
|
|
|
public static function countRows($table, $criteria = null) |
127
|
|
|
{ |
128
|
|
|
/** @var \XoopsDatabase */ |
129
|
|
|
$db = \XoopsDatabaseFactory::getDatabaseConnection(); |
130
|
|
|
|
131
|
|
|
$prefixedTable = $db->prefix($table); |
132
|
|
|
$sql = 'SELECT COUNT(*) as count FROM ' . $prefixedTable . ' '; |
133
|
|
|
if (isset($criteria) && is_subclass_of($criteria, '\CriteriaElement')) { |
134
|
|
|
$sql .= $criteria->renderWhere(); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
$result = $db->query($sql); |
137
|
|
|
$row = $db->fetchArray($result); |
138
|
|
|
$count = $row['count']; |
139
|
|
|
$db->freeRecordSet($result); |
140
|
|
|
return (int)$count; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* extractRows - get rows, all or a subset, from a table as an array |
145
|
|
|
* |
146
|
|
|
* @param string $table name of table to count |
147
|
|
|
* @param \CriteriaElement $criteria optional criteria |
148
|
|
|
* @param string[] $skipColumns do not include columns in this list |
149
|
|
|
* |
150
|
|
|
* @return array of table rows |
151
|
|
|
*/ |
152
|
|
|
public static function extractRows($table, $criteria = null, $skipColumns = array()) |
153
|
|
|
{ |
154
|
|
|
/** @var \XoopsDatabase */ |
155
|
|
|
$db = \XoopsDatabaseFactory::getDatabaseConnection(); |
156
|
|
|
|
157
|
|
|
$prefixedTable = $db->prefix($table); |
158
|
|
|
$sql = 'SELECT * FROM ' . $prefixedTable . ' '; |
159
|
|
|
if (isset($criteria) && is_subclass_of($criteria, '\CriteriaElement')) { |
160
|
|
|
$sql .= $criteria->renderWhere(); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
$rows = array(); |
163
|
|
|
$result = $db->query($sql); |
164
|
|
|
if ($result) { |
165
|
|
|
while ($row = $db->fetchArray($result)) { |
166
|
|
|
$rows[] = $row; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$db->freeRecordSet($result); |
171
|
|
|
|
172
|
|
|
if (!empty($skipColumns)) { |
173
|
|
|
foreach ($rows as $index => $row) { |
174
|
|
|
foreach ($skipColumns as $column) { |
175
|
|
|
unset($rows[$index][$column]); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $rows; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Save table data to a YAML file |
185
|
|
|
* |
186
|
|
|
* @param string $table name of table to load without prefix |
187
|
|
|
* @param string $yamlFile name of file containing data dump in YAML format |
188
|
|
|
* @param \CriteriaElement $criteria optional criteria |
189
|
|
|
* @param string[] $skipColumns do not include columns in this list |
190
|
|
|
* |
191
|
|
|
* @return bool true on success, false on error |
192
|
|
|
*/ |
193
|
|
View Code Duplication |
public static function saveTableToYamlFile($table, $yamlFile, $criteria = null, $skipColumns = array()) |
194
|
|
|
{ |
195
|
|
|
$rows = static::extractRows($table, $criteria, $skipColumns); |
196
|
|
|
|
197
|
|
|
$count = Yaml::save($rows, $yamlFile); |
198
|
|
|
|
199
|
|
|
return (false !== $count); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.