Skip to content

Commit cb3da6b

Browse files
author
Karel Wintersky
committed
0.9.9
- PHP8 compatible code
1 parent 9f7bc40 commit cb3da6b

File tree

11 files changed

+299
-94
lines changed

11 files changed

+299
-94
lines changed

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "karelwintersky/arris.database",
3+
"description": "Arris µFramework database wrapper",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Karel Wintersky",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"keywords": [ "database" ],
13+
"config": {
14+
"optimize-autoloader": true,
15+
"platform-check": false
16+
},
17+
"require": {
18+
"php": ">=7.4 | 8.*",
19+
"ext-pdo": "*",
20+
"psr/log": "1.1.4"
21+
},
22+
"require-dev": {
23+
"predis/predis": "^2.0",
24+
"vlucas/phpdotenv": "^3.3",
25+
"symfony/var-dumper": "^5.4",
26+
"php-sage/sage": "^1.3",
27+
"phpunit/phpunit": "^4.8 || ^5.7 || ^6.5",
28+
"php-coveralls/php-coveralls": "^1.1",
29+
"rector/rector": "^1.1"
30+
},
31+
"suggest": {
32+
"karelwintersky/arris.entity": "Arris µFramework: Entity Types",
33+
"karelwintersky/arris.helpers": "Arris µFramework: helpers",
34+
"ajur-media/fluentpdo": "^1.0.3"
35+
},
36+
"autoload": {
37+
"Arris\\": ["sources", "interfaces"]
38+
}
39+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Arris\Database;
4+
5+
use Psr\Log\LoggerInterface;
6+
7+
interface DBConfigInterface
8+
{
9+
/**
10+
*
11+
* @param array $connection_config
12+
* @param array $options
13+
* @param LoggerInterface|null $logger
14+
*/
15+
public function __construct(array $connection_config, array $options = [], LoggerInterface $logger = null);
16+
17+
/**
18+
* @param $time
19+
* @param int $decimals
20+
* @param string $decimal_separator
21+
* @param string $thousands_separator
22+
* @return string
23+
*/
24+
public function formatTime($time = 0, int $decimals = 6, string $decimal_separator = '.', string $thousands_separator = ''): string;
25+
26+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Arris\Database;
4+
5+
interface DBHelperInterface
6+
{
7+
/**
8+
* Строит INSERT-запрос на основе массива данных для указанной таблицы.
9+
* В массиве допустима конструкция 'key' => 'NOW()'
10+
* В этом случае она будет добавлена в запрос и удалена из набора данных (он пере).
11+
*
12+
* @param $table -- таблица
13+
* @param $dataset -- передается по ссылке, мутабелен
14+
* @return string -- результирующая строка запроса
15+
*/
16+
public static function makeInsertQuery(string $table, &$dataset, bool $pretty = true):string;
17+
18+
/**
19+
* Build UPDATE query by dataset for given table
20+
*
21+
* @param string $table
22+
* @param $dataset
23+
* @param $where_condition
24+
* @param bool $pretty
25+
* @return bool|string
26+
*/
27+
public static function makeUpdateQuery(string $table, &$dataset, $where_condition, bool $pretty = true):string;
28+
29+
/**
30+
* @param string $table
31+
* @param array $dataset
32+
* @param string $where
33+
* @param bool $pretty
34+
* @return false|string
35+
*/
36+
public static function makeReplaceQuery(string $table, array &$dataset, string $where = '', bool $pretty = true);
37+
38+
/**
39+
* Не поддерживает NOW() и UUID() в запросах
40+
*
41+
* @param string $table
42+
* @param array $dataset
43+
* @return string
44+
*/
45+
public static function buildReplaceQuery(string $table, array $dataset):string;
46+
47+
/**
48+
* @param string $table
49+
* @param array $dataset
50+
* @param null $where_condition - строка условия без WHERE ('x=0 AND y=0' ) или массив условий ['x=0', 'y=0']
51+
* @return string
52+
*/
53+
public static function buildUpdateQuery(string $table, array $dataset = [], $where_condition = null):string;
54+
55+
/**
56+
* Применять как:
57+
*
58+
* list($update_query, $newdataset) = BuildReplaceQueryMVA($table, $original_dataset, $mva_attributes_list);
59+
* $update_statement = $sphinx->prepare($update_query);
60+
* $update_statement->execute($newdataset);
61+
*
62+
*
63+
* @param string $table -- имя таблицы
64+
* @param array $dataset -- сет данных.
65+
* @param array $mva_attributes -- массив с именами ключей MVA-атрибутов (они вставятся как значения, а не как placeholder-ы)
66+
* @return array -- возвращает массив с двумя значениями. Первый ключ - запрос, сет данных, очищенный от MVA-атрибутов.
67+
*/
68+
public static function buildReplaceQueryMVA(string $table, array $dataset, array $mva_attributes):array;
69+
70+
}

interfaces/Database/DBMultiConnectorInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
interface DBMultiConnectorInterface
99
{
10-
const MYSQL_ERROR_DUPLICATE_ENTRY = 1062;
10+
public const MYSQL_ERROR_DUPLICATE_ENTRY = 1062;
1111

1212
/**
1313
* DB constructor.
1414
* @param $suffix
15-
* @throws \RuntimeException
15+
* @throws RuntimeException
1616
*/
1717
public function __construct($suffix = null);
1818

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Arris\Database;
4+
5+
use Exception;
6+
use PDO;
7+
8+
interface DBPoolInterface
9+
{
10+
/**
11+
* DBPool constructor
12+
*
13+
* @param PDO|DBWrapper $pdo_connection
14+
* @param int $pool_max_size
15+
* @param string $db_table
16+
* @param array $db_columns
17+
*/
18+
public function __construct($pdo_connection, int $pool_max_size, string $db_table, array $db_columns);
19+
20+
/**
21+
*
22+
* @param array $dataset
23+
* @throws Exception
24+
*/
25+
public function push(array $dataset);
26+
27+
/**
28+
*
29+
* @throws Exception
30+
*/
31+
public function commit();
32+
}

sources/Database/DBConfig.php

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
use Psr\Log\LoggerInterface;
77
use Psr\Log\NullLogger;
88

9-
class DBConfig
9+
class DBConfig implements DBConfigInterface
1010
{
11-
const DEFAULT_CHARSET = 'utf8';
12-
const DEFAULT_CHARSET_COLLATE = 'utf8_general_ci';
11+
public const DEFAULT_CHARSET = 'utf8';
12+
public const DEFAULT_CHARSET_COLLATE = 'utf8_general_ci';
1313

1414
/**
1515
* @var AbstractLogger
@@ -19,7 +19,7 @@ class DBConfig
1919
/**
2020
* @var array
2121
*/
22-
public $db_config;
22+
public array $db_config;
2323

2424
/**
2525
* @var string
@@ -54,7 +54,7 @@ class DBConfig
5454
/**
5555
* @var bool
5656
*/
57-
public $is_lazy;
57+
public bool $is_lazy;
5858

5959
/**
6060
* @var string
@@ -74,52 +74,61 @@ class DBConfig
7474
/**
7575
* @var int
7676
*/
77-
public $total_queries = 0;
77+
public int $total_queries = 0;
7878

7979
/**
8080
* @var float
8181
*/
8282
public $total_time = 0;
8383

84+
/**
85+
*
86+
* @param array $connection_config
87+
* @param array $options
88+
* @param LoggerInterface|null $logger
89+
*/
8490
public function __construct(array $connection_config, array $options = [], LoggerInterface $logger = null)
8591
{
86-
$this->logger = is_null($logger) ? new NullLogger() : $logger;
92+
$this->logger = \is_null($logger) ? new NullLogger() : $logger;
8793

8894
if (empty($connection_config)) {
8995
$this->logger->emergency("[DBWrapper Error] Connection config is empty");
9096
throw new \RuntimeException("[DBWrapper Error] Connection config is empty");
9197
}
9298

9399
$this->db_config = $connection_config;
94-
$this->driver = $this->db_config['driver'] ?? 'mysql';
95-
$this->hostname = $this->db_config['hostname'] ?? '127.0.0.1';
96-
$this->port = $this->db_config['port'] ?? 3306;
97-
$this->username = $this->db_config['username'] ?? 'root';
98-
$this->password = $this->db_config['password'];
99-
$this->database = $this->db_config['database'];
100+
$this->driver = $connection_config['driver'] ?? 'mysql';
101+
$this->hostname = $connection_config['hostname'] ?? '127.0.0.1';
102+
$this->port = $connection_config['port'] ?? 3306;
103+
$this->username = $connection_config['username'] ?? 'root';
104+
$this->password = $connection_config['password'];
105+
$this->database = $connection_config['database'];
100106
$this->is_lazy = true;
101107

102-
if (!array_key_exists('charset', $this->db_config)) {
108+
if (!\array_key_exists('charset', $this->db_config)) {
103109
$this->charset = self::DEFAULT_CHARSET;
104-
} elseif (!is_null($this->db_config['charset'])) {
110+
} elseif (!\is_null($this->db_config['charset'])) {
105111
$this->charset = $this->db_config['charset'];
106112
} else {
107113
$this->charset = null;
108114
}
109115

110-
if (!array_key_exists('charset_collate', $this->db_config)) {
116+
if (!\array_key_exists('charset_collate', $this->db_config)) {
111117
$this->charset_collate = self::DEFAULT_CHARSET_COLLATE;
112-
} elseif (!is_null($this->db_config['charset_collate'])) {
118+
} elseif (!\is_null($this->db_config['charset_collate'])) {
113119
$this->charset_collate = $this->db_config['charset_collate'];
114120
} else {
115121
$this->charset_collate = null;
116122
}
117123

118124
// ms
119-
$this->slow_query_threshold = (array_key_exists('slow_query_threshold', $options)) ? (float)$options['slow_query_threshold'] : 1000;
125+
$this->slow_query_threshold
126+
= \array_key_exists('slow_query_threshold', $options)
127+
? (float)$options['slow_query_threshold']
128+
: 1000;
120129

121-
// $this->is_lazy = array_key_exists('lazy', $options) ? (bool)$options['lazy'] : true;
122-
$this->is_lazy = !array_key_exists('lazy', $options) || (bool)$options['lazy']; // if default = false -> remove '!' from first part.
130+
$this->is_lazy
131+
= !\array_key_exists('lazy', $options) || (bool)$options['lazy'];
123132

124133
// microseconds
125134
$this->slow_query_threshold /= 1000;

0 commit comments

Comments
 (0)