-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathNextrasAdapter.php
110 lines (85 loc) · 2.48 KB
/
NextrasAdapter.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
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
<?php declare(strict_types = 1);
/**
* This file is part of the Nextras community extensions of Nette Framework
*
* @license New BSD License
* @link https://github.com/nextras/migrations
*/
namespace Nextras\Migrations\Bridges\NextrasDbal;
use DateTimeInterface;
use Nextras\Dbal\Connection;
use Nextras\Dbal\Drivers\IDriver;
use Nextras\Dbal\Result\Row;
use Nextras\Migrations\IDbal;
class NextrasAdapter implements IDbal
{
/** @var Connection */
private $conn;
/** @var int */
private $version;
public function __construct(Connection $connection)
{
$this->conn = $connection;
if (method_exists($connection->getDriver(), 'convertToSql')) {
$this->version = 1;
} elseif (method_exists($connection->getDriver(), 'convertBoolToSql')) {
$this->version = 2;
} else {
$this->version = 5;
}
}
public function query(string $sql): array
{
return array_map(
function (Row $row) { return $row->toArray(); },
iterator_to_array($this->conn->query('%raw', $sql))
);
}
public function exec(string $sql): int
{
$this->conn->query('%raw', $sql);
return $this->conn->getAffectedRows();
}
public function escapeString(string $value): string
{
if ($this->version >= 2) {
return $this->conn->getDriver()->convertStringToSql($value);
} else {
return $this->conn->getDriver()->convertToSql($value, IDriver::TYPE_STRING);
}
}
public function escapeInt(int $value): string
{
return (string) (int) $value;
}
public function escapeBool(bool $value): string
{
if ($this->version >= 5) {
return $this->conn->getPlatform()->formatBool($value);
} elseif ($this->version >= 2) {
return $this->conn->getDriver()->convertBoolToSql($value);
} else {
return $this->conn->getDriver()->convertToSql($value, IDriver::TYPE_BOOL);
}
}
public function escapeDateTime(DateTimeInterface $value): string
{
if ($this->version >= 5) {
return $this->conn->getPlatform()->formatDateTime($value);
} elseif ($this->version >= 2) {
return $this->conn->getDriver()->convertDateTimeToSql($value);
} else {
return $this->conn->getDriver()->convertToSql($value, IDriver::TYPE_DATETIME);
}
}
public function escapeIdentifier(string $value): string
{
if ($this->version >= 5) {
return $this->conn->getPlatform()->formatIdentifier($value);
} elseif ($this->version >= 2) {
return $this->conn->getDriver()->convertIdentifierToSql($value);
} else {
return $this->conn->getDriver()->convertToSql($value, IDriver::TYPE_IDENTIFIER);
}
}
}