1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* Contains trait SqlCleanupTrait. |
5
|
|
|
* |
6
|
|
|
* PHP version 7.0+ |
7
|
|
|
* |
8
|
|
|
* LICENSE: |
9
|
|
|
* This file is part of Yet Another Php Eve Api Library also know as Yapeal |
10
|
|
|
* which can be used to access the Eve Online API data and place it into a |
11
|
|
|
* database. |
12
|
|
|
* Copyright (C) 2016-2017 Michael Cummings |
13
|
|
|
* |
14
|
|
|
* This program is free software: you can redistribute it and/or modify it |
15
|
|
|
* under the terms of the GNU Lesser General Public License as published by the |
16
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
17
|
|
|
* option) any later version. |
18
|
|
|
* |
19
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT |
20
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
22
|
|
|
* for more details. |
23
|
|
|
* |
24
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
25
|
|
|
* along with this program. If not, see |
26
|
|
|
* <http://spdx.org/licenses/LGPL-3.0.html>. |
27
|
|
|
* |
28
|
|
|
* You should be able to find a copy of this license in the COPYING-LESSER.md |
29
|
|
|
* file. A copy of the GNU GPL should also be available in the COPYING.md file. |
30
|
|
|
* |
31
|
|
|
* @author Michael Cummings <[email protected]> |
32
|
|
|
* @copyright 2016-2017 Michael Cummings |
33
|
|
|
* @license LGPL-3.0+ |
34
|
|
|
*/ |
35
|
|
|
namespace Yapeal\Sql; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Trait SqlCleanupTrait. |
39
|
|
|
*/ |
40
|
|
|
trait SqlCleanupTrait |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* Takes one or more SQL statements with comments and normalizes EOLs and also removes most comments. |
44
|
|
|
* |
45
|
|
|
* NOTE: Any comment 'code' like used by MySQL are also removed. |
46
|
|
|
* |
47
|
|
|
* @param string $sql |
48
|
|
|
* @param array $sqlSubs Expects associative array with the index being what is being replaced and the value is |
49
|
|
|
* what it will be replaced with. |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
2 |
|
protected function getCleanedUpSql(string $sql, array $sqlSubs): string |
54
|
|
|
{ |
55
|
|
|
// Remove multi-space indents. |
56
|
|
|
do { |
57
|
2 |
|
$sql = str_replace("\n ", "\n ", $sql, $count); |
58
|
2 |
|
} while (0 < $count); |
59
|
2 |
|
$sqlSubs = array_reverse($sqlSubs); |
60
|
2 |
|
$sqlSubs["\n)"] = ')'; |
61
|
2 |
|
$sqlSubs["\n "] = ' '; |
62
|
2 |
|
$sqlSubs["\r\n"] = "\n"; |
63
|
2 |
|
$sqlSubs = array_reverse($sqlSubs); |
64
|
|
|
// Normalize line ends and change pretty multiple lines sql and comments into single line ones |
65
|
|
|
// and do replacements. |
66
|
2 |
|
$sql = str_replace(array_keys($sqlSubs), array_values($sqlSubs), $sql); |
67
|
|
|
/** |
68
|
|
|
* @var string[] $lines |
69
|
|
|
*/ |
70
|
2 |
|
$lines = explode("\n", $sql); |
71
|
|
|
// Filter out non-sql lines like comments and blank lines. |
72
|
2 |
|
$lines = array_filter($lines, |
73
|
2 |
|
function ($line) { |
74
|
2 |
|
$line = trim($line); |
75
|
2 |
|
$nonSql = '' === $line |
76
|
2 |
|
|| 0 === strpos($line, '--') |
77
|
2 |
|
|| (0 === strpos($line, '/* ') && 0 === strpos(strrev($line), '/*')) |
78
|
2 |
|
|| (0 === strpos($line, '/** ') && 0 === strpos(strrev($line), '/*')); |
79
|
2 |
|
return !$nonSql; |
80
|
2 |
|
}); |
81
|
2 |
|
return implode("\n", $lines); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|