Skip to content

Commit 9f7bc40

Browse files
author
Karel Wintersky
committed
0.9.0
- Initial imported code from `karelwintersky/arris` package
0 parents  commit 9f7bc40

File tree

10 files changed

+1176
-0
lines changed

10 files changed

+1176
-0
lines changed

.gitattributes

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
* text=auto
2+
* eol=lf
3+
4+
.git export-ignore
5+
.gitattributes export-ignore
6+
.gitignore export-ignore
7+
makefile export-ignore
8+
phpunit.xml export-ignore
9+
tests export-ignore
10+
vendor export-ignore
11+
docs export-ignore
12+
13+
14+
15+

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
.idea
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Arris\Database;
4+
5+
use Psr\Log\LoggerInterface;
6+
use RuntimeException;
7+
8+
interface DBMultiConnectorInterface
9+
{
10+
const MYSQL_ERROR_DUPLICATE_ENTRY = 1062;
11+
12+
/**
13+
* DB constructor.
14+
* @param $suffix
15+
* @throws \RuntimeException
16+
*/
17+
public function __construct($suffix = null);
18+
19+
/**
20+
* Predicted (early) initialization
21+
*
22+
*
23+
* @param $suffix
24+
* @param $config
25+
* $config must have fields:
26+
* <code>
27+
* 'driver' (default mysql)
28+
* 'hostname' (default localhost)
29+
* 'database' (default mysql)
30+
* 'username' (default root)
31+
* 'password' (default empty)
32+
* 'port' (default 3306)
33+
*
34+
* optional:
35+
* 'charset'
36+
* 'charset_collate'
37+
* </code>
38+
* @param LoggerInterface|null $logger
39+
* @param $options - options [optional]: 'collect_time' => false|true, 'collect_query => true
40+
*
41+
* @throws \Exception
42+
*/
43+
public static function init($suffix, $config, LoggerInterface $logger = null, array $options = []);
44+
45+
public static function C($suffix = null): \PDO;
46+
47+
public static function getConnection($suffix = null): \PDO;
48+
49+
/**
50+
* Get class instance == connection instance
51+
*
52+
* @param null $suffix
53+
* @return \PDO
54+
* @throws RuntimeException
55+
*/
56+
public static function getInstance($suffix = null): \PDO;
57+
58+
59+
60+
61+
62+
}

makefile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/make
2+
3+
help:
4+
@perl -e '$(HELP_ACTION)' $(MAKEFILE_LIST)
5+
6+
test: ##@test PHPUnit tests
7+
@php ./vendor/bin/phpunit --bootstrap vendor/autoload.php --testdox tests
8+
9+
# ------------------------------------------------
10+
# Add the following 'help' target to your makefile, add help text after each target name starting with '\#\#'
11+
# A category can be added with @category
12+
GREEN := $(shell tput -Txterm setaf 2)
13+
YELLOW := $(shell tput -Txterm setaf 3)
14+
WHITE := $(shell tput -Txterm setaf 7)
15+
RESET := $(shell tput -Txterm sgr0)
16+
HELP_ACTION = \
17+
%help; while(<>) { push @{$$help{$$2 // 'options'}}, [$$1, $$3] if /^([a-zA-Z\-_]+)\s*:.*\#\#(?:@([a-zA-Z\-]+))?\s(.*)$$/ }; \
18+
print "usage: make [target]\n\n"; for (sort keys %help) { print "${WHITE}$$_:${RESET}\n"; \
19+
for (@{$$help{$$_}}) { $$sep = " " x (32 - length $$_->[0]); print " ${YELLOW}$$_->[0]${RESET}$$sep${GREEN}$$_->[1]${RESET}\n"; }; \
20+
print "\n"; }
21+
22+
# -eof-

sources/Database/DBConfig.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
namespace Arris\Database;
4+
5+
use Psr\Log\AbstractLogger;
6+
use Psr\Log\LoggerInterface;
7+
use Psr\Log\NullLogger;
8+
9+
class DBConfig
10+
{
11+
const DEFAULT_CHARSET = 'utf8';
12+
const DEFAULT_CHARSET_COLLATE = 'utf8_general_ci';
13+
14+
/**
15+
* @var AbstractLogger
16+
*/
17+
public $logger;
18+
19+
/**
20+
* @var array
21+
*/
22+
public $db_config;
23+
24+
/**
25+
* @var string
26+
*/
27+
public $driver;
28+
29+
/**
30+
* @var string
31+
*/
32+
public $hostname;
33+
34+
/**
35+
* @var int
36+
*/
37+
public $port;
38+
39+
/**
40+
* @var string
41+
*/
42+
public $username;
43+
44+
/**
45+
* @var string
46+
*/
47+
public $password;
48+
49+
/**
50+
* @var string
51+
*/
52+
public $database;
53+
54+
/**
55+
* @var bool
56+
*/
57+
public $is_lazy;
58+
59+
/**
60+
* @var string
61+
*/
62+
public $charset;
63+
64+
/**
65+
* @var string
66+
*/
67+
public $charset_collate;
68+
69+
/**
70+
* @var float
71+
*/
72+
public $slow_query_threshold;
73+
74+
/**
75+
* @var int
76+
*/
77+
public $total_queries = 0;
78+
79+
/**
80+
* @var float
81+
*/
82+
public $total_time = 0;
83+
84+
public function __construct(array $connection_config, array $options = [], LoggerInterface $logger = null)
85+
{
86+
$this->logger = is_null($logger) ? new NullLogger() : $logger;
87+
88+
if (empty($connection_config)) {
89+
$this->logger->emergency("[DBWrapper Error] Connection config is empty");
90+
throw new \RuntimeException("[DBWrapper Error] Connection config is empty");
91+
}
92+
93+
$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->is_lazy = true;
101+
102+
if (!array_key_exists('charset', $this->db_config)) {
103+
$this->charset = self::DEFAULT_CHARSET;
104+
} elseif (!is_null($this->db_config['charset'])) {
105+
$this->charset = $this->db_config['charset'];
106+
} else {
107+
$this->charset = null;
108+
}
109+
110+
if (!array_key_exists('charset_collate', $this->db_config)) {
111+
$this->charset_collate = self::DEFAULT_CHARSET_COLLATE;
112+
} elseif (!is_null($this->db_config['charset_collate'])) {
113+
$this->charset_collate = $this->db_config['charset_collate'];
114+
} else {
115+
$this->charset_collate = null;
116+
}
117+
118+
// ms
119+
$this->slow_query_threshold = (array_key_exists('slow_query_threshold', $options)) ? (float)$options['slow_query_threshold'] : 1000;
120+
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.
123+
124+
// microseconds
125+
$this->slow_query_threshold /= 1000;
126+
}
127+
128+
/**
129+
* @param $time
130+
* @param int $decimals
131+
* @param string $decimal_separator
132+
* @param string $thousands_separator
133+
* @return string
134+
*/
135+
public function formatTime($time = 0, int $decimals = 6, string $decimal_separator = '.', string $thousands_separator = ''): string
136+
{
137+
return \number_format($time, $decimals, $decimal_separator, $thousands_separator);
138+
}
139+
140+
}

0 commit comments

Comments
 (0)