Skip to content

Commit a8c094c

Browse files
author
Carlos Garcia
committed
Más correcciones a la gestión de errores en la base de datos.
1 parent f8710da commit a8c094c

File tree

7 files changed

+73
-39
lines changed

7 files changed

+73
-39
lines changed

Core/Base/DataBase.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of FacturaScripts
4-
* Copyright (C) 2015-2024 Carlos Garcia Gomez <carlos@facturascripts.com>
4+
* Copyright (C) 2015-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as
@@ -223,9 +223,10 @@ public function exec(string $sql): bool
223223
// adds the sql query to the history
224224
self::$miniLog->debug($sql, ['duration' => $stop - $start]);
225225

226-
227-
if (!$result) {
226+
// check errors
227+
if (!$result || self::$engine->hasError()) {
228228
self::$miniLog->error(self::$engine->errorMessage(self::$link), ['sql' => $sql]);
229+
self::$engine->clearError();
229230
}
230231

231232
if ($inTransaction) {

Core/Base/DataBase/DataBaseEngine.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of FacturaScripts
4-
* Copyright (C) 2015-2023 Carlos Garcia Gomez <carlos@facturascripts.com>
4+
* Copyright (C) 2015-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as
@@ -172,6 +172,11 @@ public function __construct()
172172
$this->i18n = new Translator();
173173
}
174174

175+
public function clearError(): void
176+
{
177+
$this->lastErrorMsg = '';
178+
}
179+
175180
/**
176181
* Compares the data types from a numeric column. Returns true if they are equal
177182
*
@@ -180,17 +185,17 @@ public function __construct()
180185
*
181186
* @return bool
182187
*/
183-
public function compareDataTypes($dbType, $xmlType)
188+
public function compareDataTypes($dbType, $xmlType): bool
184189
{
185-
return \FS_DB_TYPE_CHECK === false || $dbType === $xmlType || strtolower($xmlType) == 'serial' || substr($dbType, 0, 4) == 'time' && substr($xmlType, 0, 4) == 'time';
190+
return FS_DB_TYPE_CHECK === false || $dbType === $xmlType || strtolower($xmlType) == 'serial' || substr($dbType, 0, 4) == 'time' && substr($xmlType, 0, 4) == 'time';
186191
}
187192

188193
/**
189194
* Returns the date format from the database engine
190195
*
191196
* @return string
192197
*/
193-
public function dateStyle()
198+
public function dateStyle(): string
194199
{
195200
return 'Y-m-d';
196201
}
@@ -202,18 +207,23 @@ public function dateStyle()
202207
*
203208
* @return string
204209
*/
205-
public function getOperator($operator)
210+
public function getOperator($operator): string
206211
{
207212
return $operator;
208213
}
209214

215+
public function hasError(): bool
216+
{
217+
return $this->lastErrorMsg !== '';
218+
}
219+
210220
/**
211221
*
212222
* @param mixed $link
213223
* @param string $tableName
214224
* @param array $fields
215225
*/
216-
public function updateSequence($link, $tableName, $fields)
226+
public function updateSequence($link, $tableName, $fields): void
217227
{
218228
;
219229
}

Core/Base/DataBase/MysqlEngine.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of FacturaScripts
4-
* Copyright (C) 2013-2023 Carlos Garcia Gomez <carlos@facturascripts.com>
4+
* Copyright (C) 2013-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as
@@ -69,7 +69,7 @@ public function __destruct()
6969
*
7070
* @return bool
7171
*/
72-
public function beginTransaction($link)
72+
public function beginTransaction($link): bool
7373
{
7474
$result = $this->exec($link, 'START TRANSACTION;');
7575
if ($result) {
@@ -79,7 +79,7 @@ public function beginTransaction($link)
7979
return $result;
8080
}
8181

82-
public function castInteger($link, $column)
82+
public function castInteger($link, $column): string
8383
{
8484
return 'CAST(' . $this->escapeColumn($link, $column) . ' AS unsigned)';
8585
}
@@ -91,7 +91,7 @@ public function castInteger($link, $column)
9191
*
9292
* @return bool
9393
*/
94-
public function close($link)
94+
public function close($link): bool
9595
{
9696
$this->rollbackTransactions();
9797
return $link->close();
@@ -104,7 +104,7 @@ public function close($link)
104104
*
105105
* @return array
106106
*/
107-
public function columnFromData($colData)
107+
public function columnFromData($colData): array
108108
{
109109
$result = array_change_key_case($colData);
110110
$result['is_nullable'] = $result['null'];
@@ -120,7 +120,7 @@ public function columnFromData($colData)
120120
*
121121
* @return bool
122122
*/
123-
public function commit($link)
123+
public function commit($link): bool
124124
{
125125
$result = $this->exec($link, 'COMMIT;');
126126
if ($result && in_array($link, $this->transactions, false)) {
@@ -138,7 +138,7 @@ public function commit($link)
138138
*
139139
* @return bool
140140
*/
141-
public function compareDataTypes($dbType, $xmlType)
141+
public function compareDataTypes($dbType, $xmlType): bool
142142
{
143143
if (parent::compareDataTypes($dbType, $xmlType)) {
144144
return true;
@@ -199,7 +199,7 @@ public function connect(&$error)
199199
*
200200
* @return string
201201
*/
202-
public function errorMessage($link)
202+
public function errorMessage($link): string
203203
{
204204
return empty($link->error) ? $this->lastErrorMsg : $link->error;
205205
}
@@ -212,7 +212,7 @@ public function errorMessage($link)
212212
*
213213
* @return string
214214
*/
215-
public function escapeColumn($link, $name)
215+
public function escapeColumn($link, $name): string
216216
{
217217
return '`' . $name . '`';
218218
}
@@ -225,7 +225,7 @@ public function escapeColumn($link, $name)
225225
*
226226
* @return string
227227
*/
228-
public function escapeString($link, $str)
228+
public function escapeString($link, $str): string
229229
{
230230
return $link->escape_string($str);
231231
}
@@ -238,15 +238,19 @@ public function escapeString($link, $str)
238238
*
239239
* @return bool
240240
*/
241-
public function exec($link, $sql)
241+
public function exec($link, $sql): bool
242242
{
243243
try {
244244
if ($link->multi_query($sql)) {
245245
do {
246246
$more = $link->more_results() && $link->next_result();
247247
} while ($more);
248248
}
249-
return $link->errno === 0;
249+
if ($link->errno !== 0) {
250+
$this->lastErrorMsg = $link->error;
251+
return false;
252+
}
253+
return true;
250254
} catch (Exception $err) {
251255
$this->lastErrorMsg = $err->getMessage();
252256
}
@@ -271,7 +275,7 @@ public function getSQL()
271275
*
272276
* @return bool
273277
*/
274-
public function inTransaction($link)
278+
public function inTransaction($link): bool
275279
{
276280
return in_array($link, $this->transactions, false);
277281
}
@@ -283,7 +287,7 @@ public function inTransaction($link)
283287
*
284288
* @return array
285289
*/
286-
public function listTables($link)
290+
public function listTables($link): array
287291
{
288292
$tables = [];
289293
foreach ($this->select($link, 'SHOW TABLES;') as $row) {
@@ -303,7 +307,7 @@ public function listTables($link)
303307
*
304308
* @return bool
305309
*/
306-
public function rollback($link)
310+
public function rollback($link): bool
307311
{
308312
$result = $this->exec($link, 'ROLLBACK;');
309313
if (in_array($link, $this->transactions, false)) {
@@ -322,7 +326,7 @@ public function rollback($link)
322326
*
323327
* @return array
324328
*/
325-
public function select($link, $sql)
329+
public function select($link, $sql): array
326330
{
327331
$result = [];
328332
try {
@@ -334,6 +338,9 @@ public function select($link, $sql)
334338
}
335339
$aux->free();
336340
}
341+
if ($link->errno !== 0) {
342+
$this->lastErrorMsg = $link->error;
343+
}
337344
} catch (Exception $err) {
338345
$this->lastErrorMsg = $err->getMessage();
339346
$result = [];
@@ -357,7 +364,7 @@ public function version($link): string
357364
/**
358365
* Rollback all active transactions.
359366
*/
360-
private function rollbackTransactions()
367+
private function rollbackTransactions(): void
361368
{
362369
foreach ($this->transactions as $link) {
363370
$this->rollback($link);
@@ -369,7 +376,7 @@ private function rollbackTransactions()
369376
*
370377
* @param mysqli $link
371378
*/
372-
private function unsetTransaction($link)
379+
private function unsetTransaction($link): void
373380
{
374381
$count = 0;
375382
foreach ($this->transactions as $trans) {

Core/Base/DataBase/PostgresqlEngine.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of FacturaScripts
4-
* Copyright (C) 2013-2024 Carlos Garcia Gomez <carlos@facturascripts.com>
4+
* Copyright (C) 2013-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as
@@ -59,7 +59,7 @@ public function beginTransaction($link): bool
5959
return $this->exec($link, 'BEGIN TRANSACTION;');
6060
}
6161

62-
public function castInteger($link, $column)
62+
public function castInteger($link, $column): string
6363
{
6464
return 'CAST(' . $this->escapeColumn($link, $column) . ' AS unsigned)';
6565
}
@@ -295,10 +295,10 @@ public function select($link, $sql): array
295295
* @param string $tableName
296296
* @param array $fields
297297
*/
298-
public function updateSequence($link, $tableName, $fields)
298+
public function updateSequence($link, $tableName, $fields): void
299299
{
300300
foreach ($fields as $colName => $field) {
301-
/// serial type
301+
// serial type
302302
if (!empty($field['default']) && stripos($field['default'], 'nextval(') !== false) {
303303
$sql = "SELECT setval('" . $tableName . "_" . $colName . "_seq', (SELECT MAX(" . $colName . ") from " . $tableName . "));";
304304
$this->exec($link, $sql);

Core/DbUpdater.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of FacturaScripts
4-
* Copyright (C) 2023-2024 Carlos Garcia Gomez <carlos@facturascripts.com>
4+
* Copyright (C) 2023-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as
@@ -43,10 +43,12 @@ final class DbUpdater
4343
public static function createTable(string $tableName, array $structure = [], string $sqlAfter = ''): bool
4444
{
4545
if (self::isTableChecked($tableName)) {
46+
Tools::log()->warning('Table ' . $tableName . ' already checked');
4647
return false;
4748
}
4849

4950
if (self::db()->tableExists($tableName)) {
51+
Tools::log()->warning('Table ' . $tableName . ' already exists');
5052
return false;
5153
}
5254

@@ -58,11 +60,11 @@ public static function createTable(string $tableName, array $structure = [], str
5860
$sql = self::sqlTool()->sqlCreateTable($tableName, $structure['columns'], $structure['constraints']) . $sqlAfter;
5961
if (self::db()->exec($sql)) {
6062
self::save($tableName);
61-
6263
Tools::log()->debug('table-checked', ['%tableName%' => $tableName]);
6364
return true;
6465
}
6566

67+
Tools::log()->critical('Failed to create table ' . $tableName, ['sql' => $sql]);
6668
self::save($tableName);
6769
return false;
6870
}
@@ -179,7 +181,8 @@ public static function rebuild(): void
179181
public static function updateTable(string $tableName, array $structure = []): bool
180182
{
181183
if (self::isTableChecked($tableName)) {
182-
return false;
184+
Tools::log()->warning('Table ' . $tableName . ' is already checked');
185+
return true;
183186
}
184187

185188
if (empty($structure)) {
@@ -192,17 +195,24 @@ public static function updateTable(string $tableName, array $structure = []): bo
192195
$dbCons = self::db()->getConstraints($tableName);
193196
$sql = self::compareColumns($tableName, $structure['columns'], $dbCols) .
194197
self::compareConstraints($tableName, $structure['constraints'], $dbCons);
195-
if (!empty($sql) && self::db()->exec($sql)) {
198+
if (empty($sql)) {
196199
self::save($tableName);
197-
198200
Tools::log()->debug('table-checked', ['%tableName%' => $tableName]);
199201
return true;
200202
}
201203

202-
self::save($tableName);
204+
if (false === self::db()->exec($sql)) {
205+
self::save($tableName);
206+
Tools::log()->critical('error-updating-table', [
207+
'%tableName%' => $tableName,
208+
'sql' => $sql
209+
]);
210+
return false;
211+
}
203212

213+
self::save($tableName);
204214
Tools::log()->debug('table-checked', ['%tableName%' => $tableName]);
205-
return false;
215+
return true;
206216
}
207217

208218
private static function db(): DataBase

Test/Core/Model/EjercicioCierreTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919

2020
namespace FacturaScripts\Test\Core\Model;
2121

22+
use FacturaScripts\Core\Base\DataBase;
2223
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
2324
use FacturaScripts\Core\Lib\Accounting\AccountingPlanImport;
2425
use FacturaScripts\Core\Lib\Accounting\ClosingToAcounting;
2526
use FacturaScripts\Core\Model\Almacen;
2627
use FacturaScripts\Core\Model\Asiento;
28+
use FacturaScripts\Core\Model\Cuenta;
2729
use FacturaScripts\Core\Model\Ejercicio;
2830
use FacturaScripts\Dinamic\Model\Subcuenta;
2931
use FacturaScripts\Test\Traits\DefaultSettingsTrait;
@@ -46,6 +48,10 @@ public static function setUpBeforeClass(): void
4648

4749
public function testCloseExercise(): void
4850
{
51+
// comprobamos si existe la tabla de cuentas
52+
$db = new DataBase();
53+
$this->assertTrue($db->tableExists(Cuenta::tableName()));
54+
4955
// creamos una nueva empresa
5056
$empresa = $this->getRandomCompany();
5157
$this->assertTrue($empresa->save());

0 commit comments

Comments
 (0)