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
|
|
|
use Xoops\Core\Kernel\CriteriaElement; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Xmf\Database\TableLoad |
19
|
|
|
* |
20
|
|
|
* load a database table |
21
|
|
|
* |
22
|
|
|
* @category Xmf\Database\TableLoad |
23
|
|
|
* @package Xmf |
24
|
|
|
* @author Richard Griffith <[email protected]> |
25
|
|
|
* @copyright 2013-2016 XOOPS Project (http://xoops.org) |
26
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
27
|
|
|
* @version Release: 1.0 |
28
|
|
|
* @link http://xoops.org |
29
|
|
|
*/ |
30
|
|
|
class TableLoad |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* loadTableFromArray |
35
|
|
|
* |
36
|
|
|
* @param string $table name of table to load without prefix |
37
|
|
|
* @param array $data array of rows to insert |
38
|
|
|
* Each element of the outer array represents a single table row. |
39
|
|
|
* Each row is an associative array in 'column' => 'value' format. |
40
|
|
|
* |
41
|
|
|
* @return int number of rows inserted |
42
|
|
|
*/ |
43
|
|
|
public static function loadTableFromArray($table, $data) |
44
|
|
|
{ |
45
|
|
|
$db = \Xoops::getInstance()->db(); |
46
|
|
|
$count = 0; |
47
|
|
|
$db->beginTransaction(); |
48
|
|
|
foreach ($data as $row) { |
49
|
|
|
$count += $db->insertPrefix($table, $row); |
50
|
|
|
} |
51
|
|
|
$db->commit(); |
52
|
|
|
return $count; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* loadTableFromYamlFile |
57
|
|
|
* |
58
|
|
|
* @param string $table name of table to load without prefix |
59
|
|
|
* @param string $yamlFile name of file containing data dump in YAML format |
60
|
|
|
* |
61
|
|
|
* @return int number of rows inserted |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public static function loadTableFromYamlFile($table, $yamlFile) |
64
|
|
|
{ |
65
|
|
|
$count = 0; |
66
|
|
|
|
67
|
|
|
$data = Yaml::loadWrapped($yamlFile); // work with phpmyadmin YAML dumps |
68
|
|
|
if (false !== $data) { |
69
|
|
|
$count = static::loadTableFromArray($table, $data); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $count; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* truncateTable - empty a database table |
77
|
|
|
* |
78
|
|
|
* @param string $table name of table to truncate |
79
|
|
|
* |
80
|
|
|
* @return int number of affected rows |
81
|
|
|
*/ |
82
|
|
|
public static function truncateTable($table) |
83
|
|
|
{ |
84
|
|
|
$db = \Xoops::getInstance()->db(); |
85
|
|
|
$platform = $db->getDatabasePlatform(); |
86
|
|
|
$sql = $platform->getTruncateTableSQL($db->prefix($table)); |
87
|
|
|
|
88
|
|
|
return $db->exec($sql); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* countRows - get count of rows in a table |
93
|
|
|
* |
94
|
|
|
* @param string $table name of table to count |
95
|
|
|
* @param CriteriaElement $criteria optional criteria |
96
|
|
|
* |
97
|
|
|
* @return int number of rows |
98
|
|
|
*/ |
99
|
1 |
|
public static function countRows($table, CriteriaElement $criteria = null) |
100
|
|
|
{ |
101
|
1 |
|
$db = \Xoops::getInstance()->db(); |
102
|
1 |
|
$qb = $db->createXoopsQueryBuilder(); |
103
|
1 |
|
$qb->select('COUNT(*)') |
104
|
1 |
|
->fromPrefix($table, ''); |
105
|
1 |
|
if (isset($criteria)) { |
106
|
|
|
$qb = $criteria->renderQb($qb, ''); |
107
|
|
|
} |
108
|
1 |
|
$result = $qb->execute(); |
109
|
1 |
|
$count = $result->fetchColumn(); |
110
|
1 |
|
return (int)$count; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* extractRows - get rows, all or a subset, from a table as an array |
115
|
|
|
* |
116
|
|
|
* @param string $table name of table to read |
117
|
|
|
* @param CriteriaElement $criteria optional criteria |
118
|
|
|
* @param string[] $skipColumns do not include these columns in the extract |
119
|
|
|
* |
120
|
|
|
* @return array of table rows |
121
|
|
|
*/ |
122
|
|
|
public static function extractRows($table, CriteriaElement $criteria = null, $skipColumns = array()) |
123
|
|
|
{ |
124
|
|
|
$db = \Xoops::getInstance()->db(); |
125
|
|
|
$qb = $db->createXoopsQueryBuilder(); |
126
|
|
|
$qb->select('*')->fromPrefix($table, ''); |
127
|
|
|
if (isset($criteria)) { |
128
|
|
|
$qb = $criteria->renderQb($qb, ''); |
129
|
|
|
} |
130
|
|
|
$result = $qb->execute(); |
131
|
|
|
$rows = $result->fetchAll(); |
132
|
|
|
|
133
|
|
|
if (!empty($skipColumns)) { |
134
|
|
|
foreach ($rows as $index => $row) { |
135
|
|
|
foreach ($skipColumns as $column) { |
136
|
|
|
unset($rows[$index][$column]); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $rows; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Save table data to a YAML file |
146
|
|
|
* |
147
|
|
|
* @param string $table name of table to load without prefix |
148
|
|
|
* @param string $yamlFile name of file containing data dump in YAML format |
149
|
|
|
* @param CriteriaElement $criteria optional criteria |
150
|
|
|
* @param string[] $skipColumns do not include columns in this list |
151
|
|
|
* |
152
|
|
|
* @return bool true on success, false on error |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
public static function saveTableToYamlFile($table, $yamlFile, $criteria = null, $skipColumns = array()) |
155
|
|
|
{ |
156
|
|
|
$rows = static::extractRows($table, $criteria, $skipColumns); |
157
|
|
|
|
158
|
|
|
$count = Yaml::save($rows, $yamlFile); |
159
|
|
|
|
160
|
|
|
return (false !== $count); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.