Skip to content

Commit

Permalink
- Añadidas funciones restantes a la nueva clase Database.
Browse files Browse the repository at this point in the history
- Modificado ModelClass para usar Database.
- Cargadp appsettings para mayor compatibilidad.
- Corregido el bootstrap de los tests.
  • Loading branch information
NeoRazorX committed May 24, 2022
1 parent 39baf5f commit b90458e
Show file tree
Hide file tree
Showing 24 changed files with 121 additions and 223 deletions.
26 changes: 25 additions & 1 deletion Core/Bridge/DatabaseMysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ public function beginTransaction(): bool
return $this->exec('START TRANSACTION;');
}

public function castColumn(string $name, string $type): string
{
switch ($type) {
case 'int':
case 'integer':
return 'CAST(' . $name . ' AS UNSIGNED)';

case 'float':
return 'CAST(' . $name . ' AS DECIMAL(10,2))';

case 'string':
return 'CAST(' . $name . ' AS CHAR)';

case 'date':
return 'CAST(' . $name . ' AS DATE)';

case 'datetime':
return 'CAST(' . $name . ' AS DATETIME)';

default:
return $name;
}
}

public function clearLastErrorMsg(): void
{
$this->lastErrorMsg = '';
Expand Down Expand Up @@ -182,7 +206,7 @@ public function select(string $sql, int $limit = 0, int $offset = 0): array
return $rows;
}

public function updateSequence(string $tableName, string $fields): void
public function updateSequence(string $tableName, array $fields): void
{
// unnecessary on mysql
}
Expand Down
29 changes: 28 additions & 1 deletion Core/Bridge/DatabasePostgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ public function beginTransaction(): bool
return $this->exec('START TRANSACTION;');
}

public function castColumn(string $name, string $type): string
{
switch ($type) {
case 'int':
case 'integer':
return "CAST($name AS INTEGER)";

case 'double':
return "CAST($name AS DOUBLE PRECISION)";

case 'string':
return "CAST($name AS TEXT)";

case 'boolean':
return "CAST($name AS BOOLEAN)";

case 'date':
return "CAST($name AS DATE)";

case 'datetime':
return "CAST($name AS TIMESTAMP)";

default:
return $name;
}
}

public function clearLastErrorMsg(): void
{
$this->lastErrorMsg = '';
Expand Down Expand Up @@ -162,7 +189,7 @@ public function select(string $sql, int $limit, int $offset): array
return $rows;
}

public function updateSequence(string $tableName, string $fields): void
public function updateSequence(string $tableName, array $fields): void
{
foreach ($fields as $colName => $field) {
// serial type
Expand Down
4 changes: 3 additions & 1 deletion Core/Contract/DbInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface DbInterface
{
public function beginTransaction(): bool;

public function castColumn(string $name, string $type): string;

public function clearLastErrorMsg(): void;

public function close(): bool;
Expand Down Expand Up @@ -51,7 +53,7 @@ public function rollback(): bool;

public function select(string $sql, int $limit, int $offset): array;

public function updateSequence(string $tableName, string $fields): void;
public function updateSequence(string $tableName, array $fields): void;

public function version(): string;
}
78 changes: 43 additions & 35 deletions Core/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,41 +59,9 @@ public static function beginTransaction(): bool
/**
* @throws KernelException
*/
private static function bridge(): DbInterface
public static function castColumn(string $name, string $type): string
{
if (isset(self::$bridge)) {
return self::$bridge;
}

if (empty(Setup::get('db_host'))) {
throw new KernelException('DatabaseError', 'no-db-setup');
}

switch (Setup::get('db_type')) {
case 'mysql':
self::$bridge = new DatabaseMysql(
Setup::get('db_host'),
Setup::get('db_user'),
Setup::get('db_pass'),
Setup::get('db_name'),
Setup::get('db_port')
);
break;

case 'postgresql':
self::$bridge = new DatabasePostgres(
Setup::get('db_host'),
Setup::get('db_user'),
Setup::get('db_pass'),
Setup::get('db_name'),
Setup::get('db_port')
);
break;

default:
throw new KernelException('DatabaseError', 'no-db-type');
}
return self::$bridge;
return self::bridge()->castColumn($name, $type);
}

/**
Expand Down Expand Up @@ -263,7 +231,7 @@ public static function var2str($val): string
/**
* @throws KernelException
*/
public static function updateSequence(string $tableName, string $fields): void
public static function updateSequence(string $tableName, array $fields): void
{
self::bridge()->updateSequence($tableName, $fields);
}
Expand All @@ -276,6 +244,46 @@ public static function version(): string
return self::bridge()->version();
}

/**
* @throws KernelException
*/
private static function bridge(): DbInterface
{
if (isset(self::$bridge)) {
return self::$bridge;
}

if (empty(Setup::get('db_host'))) {
throw new KernelException('DatabaseError', 'no-db-setup');
}

switch (Setup::get('db_type')) {
case 'mysql':
self::$bridge = new DatabaseMysql(
Setup::get('db_host'),
Setup::get('db_user'),
Setup::get('db_pass'),
Setup::get('db_name'),
Setup::get('db_port')
);
break;

case 'postgresql':
self::$bridge = new DatabasePostgres(
Setup::get('db_host'),
Setup::get('db_user'),
Setup::get('db_pass'),
Setup::get('db_name'),
Setup::get('db_port')
);
break;

default:
throw new KernelException('DatabaseError', 'no-db-type');
}
return self::$bridge;
}

private static function log(): Logger
{
if (!isset(self::$logger)) {
Expand Down
5 changes: 3 additions & 2 deletions Core/Model/Base/ModelClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace FacturaScripts\Core\Model\Base;

use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\Database;
use FacturaScripts\Dinamic\Model\CodeModel;

/**
Expand Down Expand Up @@ -212,7 +213,7 @@ public function newCode(string $field = '', array $where = [])
if (false === in_array($modelFields[$field]['type'], ['integer', 'int', 'serial'])) {
// Set Where to Integers values only
$where[] = new DataBaseWhere($field, '^-?[0-9]+$', 'REGEXP');
$field = self::$dataBase->getEngine()->getSQL()->sql2Int($field);
$field = Database::castColumn($field, 'int');
}

// Search for new code value
Expand Down Expand Up @@ -359,7 +360,7 @@ protected function saveInsert(array $values = [])
if ($this->primaryColumnValue() === null) {
$this->{static::primaryColumn()} = self::$dataBase->lastval();
} else {
self::$dataBase->updateSequence(static::tableName(), $this->getModelFields());
Database::updateSequence(static::tableName(), $this->getModelFields());
}

$this->pipe('saveInsert');
Expand Down
18 changes: 0 additions & 18 deletions Core/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,6 @@ public static function load(string $folder): void
}
}

public static function loadCompatibility(): void
{
$constants = [
'FS_CODPAIS' => ['property' => 'codpais', 'default' => 'ESP'],
'FS_NF0' => ['property' => 'decimals', 'default' => 2],
'FS_NF1' => ['property' => 'decimal_separator', 'default' => ','],
'FS_NF2' => ['property' => 'thousands_separator', 'default' => ' '],
'FS_CURRENCY_POS' => ['property' => 'currency_position', 'default' => 'right'],
'FS_ITEM_LIMIT' => ['property' => 'item_limit', 'default' => 50],
];

foreach ($constants as $key => $value) {
if (!defined($key)) {
define($key, self::read('default', $value['property'], $value['default']));
}
}
}

public static function set(string $property, $value): void
{
// properties are case-insensitive
Expand Down
9 changes: 0 additions & 9 deletions Dinamic/Lib/AjaxForms/AccountingFooterHTML.php

This file was deleted.

9 changes: 0 additions & 9 deletions Dinamic/Lib/AjaxForms/AccountingHeaderHTML.php

This file was deleted.

9 changes: 0 additions & 9 deletions Dinamic/Lib/AjaxForms/AccountingLineHTML.php

This file was deleted.

9 changes: 0 additions & 9 deletions Dinamic/Lib/AjaxForms/AccountingModalHTML.php

This file was deleted.

9 changes: 0 additions & 9 deletions Dinamic/Lib/BusinessDocumentFormTools.php

This file was deleted.

9 changes: 0 additions & 9 deletions Dinamic/Lib/CommissionTools.php

This file was deleted.

9 changes: 0 additions & 9 deletions Dinamic/Lib/ExtendedController/BusinessDocumentController.php

This file was deleted.

9 changes: 0 additions & 9 deletions Dinamic/Lib/ExtendedController/BusinessDocumentView.php

This file was deleted.

9 changes: 0 additions & 9 deletions Dinamic/Lib/ExtendedController/GridView.php

This file was deleted.

9 changes: 0 additions & 9 deletions Dinamic/Lib/ExtendedController/PurchaseDocumentController.php

This file was deleted.

9 changes: 0 additions & 9 deletions Dinamic/Lib/ExtendedController/SalesDocumentController.php

This file was deleted.

9 changes: 0 additions & 9 deletions Dinamic/Model/Base/GridModelInterface.php

This file was deleted.

9 changes: 0 additions & 9 deletions Dinamic/Model/Comision.php

This file was deleted.

9 changes: 0 additions & 9 deletions Dinamic/Model/Join/LiquidacionComisionFactura.php

This file was deleted.

0 comments on commit b90458e

Please sign in to comment.