-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseAdapter.php
More file actions
115 lines (99 loc) · 2.73 KB
/
BaseAdapter.php
File metadata and controls
115 lines (99 loc) · 2.73 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
/*
* Studio 107 (c) 2018 Maxim Falaleev
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mindy\QueryBuilder;
use Doctrine\DBAL\Connection;
abstract class BaseAdapter implements AdapterInterface
{
/**
* @var string
*/
protected $tablePrefix = '';
/**
* @var Connection
*/
protected $connection;
/**
* BaseAdapter constructor.
*
* @param Connection $connection
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
/**
* @return ExpressionBuilder|LookupCollectionInterface
*/
abstract public function getLookupCollection();
/**
* TODO remove
* {@inheritdoc}
*/
public function quoteSql(string $sql): string
{
$tablePrefix = $this->tablePrefix;
return preg_replace_callback(
'/(\\[\\[([\w\-\. ]+)\\]\\]|\\@([\w\-\. \/\%\:]+)\\@)/',
function ($matches) use ($tablePrefix) {
if (isset($matches[4])) {
return $this->connection->quote($this->getSqlType($matches[4]));
} elseif (isset($matches[3])) {
return $this->getQuotedName($matches[3]);
}
return str_replace('%', $tablePrefix, $this->getQuotedName($matches[2]));
},
$sql
);
}
/**
* @param $value
*
* @return string
*/
public function getSqlType($value)
{
if ('boolean' === gettype($value)) {
return $this->connection->getDatabasePlatform()->convertBooleans($value);
} elseif (null === $value || 'null' === $value) {
return 'NULL';
}
return $this->connection->quote($value);
}
/**
* @return string
*/
abstract public function getRandomOrder();
/**
* @param bool $check
* @param string $schema
* @param string $table
*
* @return string
*/
abstract public function sqlCheckIntegrity($check = true, $schema = '', $table = '');
/**
* // TODO move from here to expression builder
*
* @param string $str
*
* @throws \Doctrine\DBAL\DBALException
*
* @return string
*/
public function getQuotedName($str): string
{
$platform = $this->connection->getDatabasePlatform();
$keywords = $platform->getReservedKeywordsList();
$parts = explode('.', (string) $str);
foreach ($parts as $k => $v) {
$parts[$k] = ($keywords->isKeyword($v)) ? $platform->quoteIdentifier($v) : $v;
}
return implode('.', $parts);
}
}