-
Notifications
You must be signed in to change notification settings - Fork 609
Expand file tree
/
Copy pathQueryException.php
More file actions
170 lines (146 loc) · 4.12 KB
/
QueryException.php
File metadata and controls
170 lines (146 loc) · 4.12 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Illuminate\Database;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use PDOException;
use Throwable;
class QueryException extends PDOException
{
/**
* The database connection name.
*
* @var string
*/
public $connectionName;
/**
* The SQL for the query.
*
* @var string
*/
protected $sql;
/**
* The bindings for the query.
*
* @var array
*/
protected $bindings;
/**
* The PDO read / write type for the executed query.
*
* @var null|'read'|'write'
*/
public $readWriteType;
/**
* The connection details for the query (host, port, database, etc.).
*
* @var array
*/
protected $connectionDetails = [];
/**
* Create a new query exception instance.
*
* @param string $connectionName
* @param string $sql
* @param array $bindings
* @param \Throwable $previous
* @param null|'read'|'write' $readWriteType
* @param array $connectionDetails
*/
public function __construct($connectionName, $sql, array $bindings, Throwable $previous, array $connectionDetails = [], $readWriteType = null)
{
parent::__construct('', 0, $previous);
$this->connectionName = $connectionName;
$this->sql = $sql;
$this->bindings = $bindings;
$this->connectionDetails = $connectionDetails;
$this->readWriteType = $readWriteType;
$this->code = $previous->getCode();
$this->message = $this->formatMessage($connectionName, $sql, $bindings, $previous);
if ($previous instanceof PDOException) {
$this->errorInfo = $previous->errorInfo;
}
}
/**
* Format the SQL error message.
*
* @param string $connectionName
* @param string $sql
* @param array $bindings
* @param \Throwable $previous
* @return string
*/
protected function formatMessage($connectionName, $sql, $bindings, Throwable $previous)
{
$details = $this->formatConnectionDetails();
return $previous->getMessage().' (Connection: '.$connectionName.$details.', SQL: '.Str::replaceArray('?', $bindings, $sql).')';
}
/**
* Format the connection details for the error message.
*
* @return string
*/
protected function formatConnectionDetails()
{
if (empty($this->connectionDetails)) {
return '';
}
$driver = $this->connectionDetails['driver'] ?? '';
$segments = [];
if ($driver !== 'sqlite') {
if (! empty($this->connectionDetails['unix_socket'])) {
$segments[] = 'Socket: '.$this->connectionDetails['unix_socket'];
} else {
$host = $this->connectionDetails['host'] ?? '';
$segments[] = 'Host: '.(is_array($host) ? implode(', ', $host) : $host);
$segments[] = 'Port: '.($this->connectionDetails['port'] ?? '');
}
}
$segments[] = 'Database: '.($this->connectionDetails['database'] ?? '');
return ', '.implode(', ', $segments);
}
/**
* Get the connection name for the query.
*
* @return string
*/
public function getConnectionName()
{
return $this->connectionName;
}
/**
* Get the SQL for the query.
*
* @return string
*/
public function getSql()
{
return $this->sql;
}
/**
* Get the raw SQL representation of the query with embedded bindings.
*/
public function getRawSql(): string
{
return DB::connection($this->getConnectionName())
->getQueryGrammar()
->substituteBindingsIntoRawSql($this->getSql(), $this->getBindings());
}
/**
* Get the bindings for the query.
*
* @return array
*/
public function getBindings()
{
return $this->bindings;
}
/**
* Get information about the connection such as host, port, database, etc.
*
* @return array
*/
public function getConnectionDetails()
{
return $this->connectionDetails;
}
}