Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
35.14% |
13 / 37 |
|
14.29% |
1 / 7 |
CRAP | |
0.00% |
0 / 1 |
Gateway | |
35.14% |
13 / 37 |
|
14.29% |
1 / 7 |
85.87 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
initializeFeatures | |
9.09% |
1 / 11 |
|
0.00% |
0 / 1 |
16.02 | |||
createRow | |
28.57% |
4 / 14 |
|
0.00% |
0 / 1 |
19.12 | |||
getDbTable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
beginTransaction | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
commitTransaction | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
rollBackTransaction | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Generic VuFind table gateway. |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Db_Table |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org Main Site |
28 | */ |
29 | |
30 | namespace VuFind\Db\Table; |
31 | |
32 | use Exception; |
33 | use Laminas\Db\Adapter\Adapter; |
34 | use Laminas\Db\TableGateway\AbstractTableGateway; |
35 | use Laminas\Db\TableGateway\Feature; |
36 | use VuFind\Db\Row\RowGateway; |
37 | |
38 | use function count; |
39 | use function is_object; |
40 | |
41 | /** |
42 | * Generic VuFind table gateway. |
43 | * |
44 | * @category VuFind |
45 | * @package Db_Table |
46 | * @author Demian Katz <demian.katz@villanova.edu> |
47 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
48 | * @link https://vufind.org Main Site |
49 | */ |
50 | class Gateway extends AbstractTableGateway |
51 | { |
52 | /** |
53 | * Table manager |
54 | * |
55 | * @var PluginManager |
56 | */ |
57 | protected $tableManager; |
58 | |
59 | /** |
60 | * Constructor |
61 | * |
62 | * @param Adapter $adapter Database adapter |
63 | * @param PluginManager $tm Table manager |
64 | * @param array $cfg Laminas configuration |
65 | * @param RowGateway $rowObj Row prototype object (null for default) |
66 | * @param string $table Name of database table to interface with |
67 | */ |
68 | public function __construct( |
69 | Adapter $adapter, |
70 | PluginManager $tm, |
71 | $cfg, |
72 | ?RowGateway $rowObj, |
73 | $table |
74 | ) { |
75 | $this->adapter = $adapter; |
76 | $this->tableManager = $tm; |
77 | $this->table = $table; |
78 | |
79 | $this->initializeFeatures($cfg); |
80 | $this->initialize(); |
81 | |
82 | if (null !== $rowObj) { |
83 | $resultSetPrototype = $this->getResultSetPrototype(); |
84 | $resultSetPrototype->setArrayObjectPrototype($rowObj); |
85 | } |
86 | } |
87 | |
88 | /** |
89 | * Initialize features |
90 | * |
91 | * @param array $cfg Laminas configuration |
92 | * |
93 | * @return void |
94 | */ |
95 | public function initializeFeatures($cfg) |
96 | { |
97 | // Special case for PostgreSQL sequences: |
98 | if ($this->adapter->getDriver()->getDatabasePlatformName() == 'Postgresql') { |
99 | $maps = $cfg['vufind']['pgsql_seq_mapping'] ?? null; |
100 | if (isset($maps[$this->table])) { |
101 | if (!is_object($this->featureSet)) { |
102 | $this->featureSet = new Feature\FeatureSet(); |
103 | } |
104 | $this->featureSet->addFeature( |
105 | new Feature\SequenceFeature( |
106 | $maps[$this->table][0], |
107 | $maps[$this->table][1] |
108 | ) |
109 | ); |
110 | } |
111 | } |
112 | } |
113 | |
114 | /** |
115 | * Create a new row. |
116 | * |
117 | * @return object |
118 | */ |
119 | public function createRow() |
120 | { |
121 | $obj = clone $this->getResultSetPrototype()->getArrayObjectPrototype(); |
122 | |
123 | // If this is a PostgreSQL connection, we may need to initialize the ID |
124 | // from a sequence: |
125 | if ( |
126 | $this->adapter |
127 | && $this->adapter->getDriver()->getDatabasePlatformName() == 'Postgresql' |
128 | && $obj instanceof \VuFind\Db\Row\RowGateway |
129 | ) { |
130 | // Do we have a sequence feature? |
131 | $feature = $this->featureSet->getFeatureByClassName( |
132 | 'Laminas\Db\TableGateway\Feature\SequenceFeature' |
133 | ); |
134 | if ($feature) { |
135 | $key = $obj->getPrimaryKeyColumn(); |
136 | if (count($key) != 1) { |
137 | throw new \Exception('Unexpected number of key columns.'); |
138 | } |
139 | $col = $key[0]; |
140 | $obj->$col = $feature->nextSequenceId(); |
141 | } |
142 | } |
143 | |
144 | return $obj; |
145 | } |
146 | |
147 | /** |
148 | * Get access to another table. |
149 | * |
150 | * @param string $table Table name |
151 | * |
152 | * @return Gateway |
153 | */ |
154 | public function getDbTable($table) |
155 | { |
156 | return $this->tableManager->get($table); |
157 | } |
158 | |
159 | /** |
160 | * Begin a database transaction. |
161 | * |
162 | * @return void |
163 | * @throws Exception |
164 | */ |
165 | public function beginTransaction(): void |
166 | { |
167 | $this->getAdapter()->getDriver()->getConnection()->beginTransaction(); |
168 | } |
169 | |
170 | /** |
171 | * Commit a database transaction. |
172 | * |
173 | * @return void |
174 | * @throws Exception |
175 | */ |
176 | public function commitTransaction(): void |
177 | { |
178 | $this->getAdapter()->getDriver()->getConnection()->commit(); |
179 | } |
180 | |
181 | /** |
182 | * Roll back a database transaction. |
183 | * |
184 | * @return void |
185 | * @throws Exception |
186 | */ |
187 | public function rollBackTransaction(): void |
188 | { |
189 | $this->getAdapter()->getDriver()->getConnection()->rollback(); |
190 | } |
191 | } |