|
26 | 26 | class Connection extends BaseConnection |
27 | 27 | { |
28 | 28 | /** |
29 | | - * Run a select statement and return a single result. |
30 | | - * |
31 | | - * @param string $query |
32 | | - * @param array $bindings |
33 | | - * @param bool $useReadPdo |
34 | | - * @return mixed |
| 29 | + * @inheritDoc |
35 | 30 | */ |
36 | | - public function selectOne($query, $bindings = [], $useReadPdo = true) |
| 31 | + protected function run($query, $bindings, \Closure $callback) |
37 | 32 | { |
38 | | - $query = $this->getSqlComments($query); |
39 | | - $records = parent::select($query, $bindings, $useReadPdo); |
40 | | - |
41 | | - if (count($records) > 0) { |
42 | | - return $records; |
43 | | - } |
44 | | - return null; |
45 | | - } |
46 | | - |
47 | | - /** |
48 | | - * Run a select statement against the database. |
49 | | - * |
50 | | - * @param string $query |
51 | | - * @param array $bindings |
52 | | - * @param bool $useReadPdo |
53 | | - * @return array |
54 | | - */ |
55 | | - public function select($query, $bindings = [], $useReadPdo = true) |
56 | | - { |
57 | | - $query = $this->getSqlComments($query); |
58 | | - $records = parent::select($query, $bindings, $useReadPdo); |
59 | | - return $records; |
| 33 | + return parent::run( |
| 34 | + $this->appendSqlComments($query), |
| 35 | + $bindings, |
| 36 | + $callback |
| 37 | + ); |
60 | 38 | } |
61 | 39 |
|
62 | | - /** |
63 | | - * Run an insert statement against the database. |
64 | | - * |
65 | | - * @param string $query |
66 | | - * @param array $bindings |
67 | | - * @return bool |
68 | | - */ |
69 | | - public function insert($query, $bindings = []) |
| 40 | + private function appendSqlComments(string $query): string |
70 | 41 | { |
71 | | - $query = $this->getSqlComments($query); |
72 | | - $records = parent::insert($query, $bindings); |
73 | | - |
74 | | - return $records; |
75 | | - } |
76 | | - |
77 | | - /** |
78 | | - * Run an update statement against the database. |
79 | | - * |
80 | | - * @param string $query |
81 | | - * @param array $bindings |
82 | | - * @return int |
83 | | - */ |
84 | | - public function update($query, $bindings = []) |
85 | | - { |
86 | | - $query = $this->getSqlComments($query); |
87 | | - |
88 | | - return $this->affectingStatement($query, $bindings); |
89 | | - } |
90 | | - |
91 | | - /** |
92 | | - * Run a delete statement against the database. |
93 | | - * |
94 | | - * @param string $query |
95 | | - * @param array $bindings |
96 | | - * @return int |
97 | | - */ |
98 | | - public function delete($query, $bindings = []) |
99 | | - { |
100 | | - |
101 | | - $query = $this->getSqlComments($query); |
102 | | - |
103 | | - return $this->affectingStatement($query, $bindings); |
104 | | - } |
105 | | - |
106 | | - private function getSqlComments($query) |
107 | | - { |
108 | | - $configurationKey = 'google_sqlcommenter.include.'; |
109 | | - $comment = []; |
| 42 | + static $configurationKey = 'google_sqlcommenter.include'; |
| 43 | + $comments = []; |
110 | 44 | $action = null; |
111 | 45 |
|
112 | | - if (!empty(app('request')->route())) { |
113 | | - $action = app('request')->route()->getAction(); |
| 46 | + if (!empty(request()->route())) { |
| 47 | + $action = request()->route()->getAction(); |
114 | 48 | } |
115 | | - if (config($configurationKey . 'framework', true)) { |
116 | | - $comment['framework'] = "laravel-" . app()->version(); |
| 49 | + if (config("{$configurationKey}.framework", true)) { |
| 50 | + $comments['framework'] = "laravel-" . app()->version(); |
117 | 51 | } |
118 | | - if (config($configurationKey . 'controller', true) and !empty($action['controller'])) { |
119 | | - $comment['controller'] = explode("@", class_basename($action['controller']))[0]; |
| 52 | + if (config("{$configurationKey}.controller", true) && !empty($action['controller'])) { |
| 53 | + $comments['controller'] = explode("@", class_basename($action['controller']))[0]; |
120 | 54 | } |
121 | | - if (config($configurationKey . 'action', true) and !empty($action and $action['controller'] and str_contains($action['controller'], '@'))) { |
122 | | - $comment['action'] = explode("@", class_basename($action['controller']))[1]; |
| 55 | + if (config("{$configurationKey}.action", true) && !empty($action and $action['controller'] && str_contains($action['controller'], '@'))) { |
| 56 | + $comments['action'] = explode("@", class_basename($action['controller']))[1]; |
123 | 57 | } |
124 | | - if (config($configurationKey . 'route', true)) { |
125 | | - $comment['route'] = request()->getRequestUri(); |
| 58 | + if (config("{$configurationKey}.route", true)) { |
| 59 | + $comments['route'] = request()->getRequestUri(); |
126 | 60 | } |
127 | | - if (config($configurationKey . 'db_driver', true)) { |
| 61 | + if (config("{$configurationKey}.db_driver", true)) { |
128 | 62 | $connection = config('database.default'); |
129 | | - $comment['db_driver'] = config("database.connections.{$connection}.driver"); |
| 63 | + $comments['db_driver'] = config("database.connections.{$connection}.driver"); |
130 | 64 | } |
131 | | - if (config($configurationKey . 'opentelemetry', true)) { |
| 65 | + if (config("{$configurationKey}.opentelemetry", true)) { |
132 | 66 | $carrier = Opentelemetry::getOpentelemetryValues(); |
133 | | - $comment = array_merge($comment, $carrier); |
| 67 | + $comments = $comments + $carrier; |
134 | 68 | } |
135 | 69 |
|
136 | | - $query=trim($query); |
137 | | - |
138 | | - if ($query[-1] == ';'){ |
139 | | - return rtrim($query ,";"). Utils::formatComments(array_filter(($comment))). ';'; |
140 | | - } |
141 | | - return $query . Utils::formatComments(array_filter(($comment))); |
| 70 | + $query = trim($query); |
| 71 | + $hasSemicolon = $query[-1] === ';'; |
| 72 | + $query = rtrim($query, ';'); |
142 | 73 |
|
| 74 | + return $query . Utils::formatComments(array_filter($comments)) . ($hasSemicolon ? ';' : ''); |
143 | 75 | } |
144 | 76 | } |
0 commit comments