forked from ArtSkills/common
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDB.php
59 lines (52 loc) · 1.56 KB
/
DB.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
namespace ArtSkills\Lib;
use ArtSkills\Traits\Library;
use Cake\Database\Connection;
use Cake\Database\Statement\MysqlStatement;
use Cake\Database\StatementInterface;
use Cake\Datasource\ConnectionManager;
/**
* @SuppressWarnings(PHPMD.ShortClassName)
*/
class DB
{
use Library;
const CONNECTION_DEFAULT = 'default';
const CONNECTION_TEST = 'test';
/**
* Дефолтный коннекшн
*
* @param string $name
* @param bool $useAliases
* @return Connection
*/
public static function getConnection(string $name = self::CONNECTION_DEFAULT, bool $useAliases = true): Connection
{
return ConnectionManager::get($name, $useAliases); // @phpstan-ignore-line
}
/**
* переподсоединиться, если отвалился
*
* @param string $connectionName
* @return void
*/
public static function restoreConnection(string $connectionName = self::CONNECTION_DEFAULT)
{
$connection = self::getConnection($connectionName);
if (!$connection->isConnected()) {
$connection->connect();
}
}
/**
* Выполнить запрос не через построитель
*
* @param string $sql
* @param string $connectionName
* @return MysqlStatement|StatementInterface
*/
public static function customQuery(string $sql, string $connectionName = self::CONNECTION_DEFAULT): StatementInterface
{
return self::getConnection($connectionName)->execute($sql);
}
}