| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikWP - Libraries |
| 4 |
* @subpackage adapter.database |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Helper class to run some useful functions for the database framework. |
| 16 |
* |
| 17 |
* @since 10.0 |
| 18 |
*/ |
| 19 |
abstract class JDatabaseHelper |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Splits the given SQL statement in single queries. |
| 23 |
* |
| 24 |
* @param string $sql A staring containing multiple queries. |
| 25 |
* |
| 26 |
* @return array A list of queries. |
| 27 |
*/ |
| 28 |
public static function splitSql($sql) |
| 29 |
{ |
| 30 |
$start = 0; |
| 31 |
$open = false; |
| 32 |
$comment = false; |
| 33 |
$endString = ''; |
| 34 |
$end = strlen($sql); |
| 35 |
$queries = array(); |
| 36 |
$query = ''; |
| 37 |
|
| 38 |
for ($i = 0; $i < $end; $i++) |
| 39 |
{ |
| 40 |
$current = substr($sql, $i, 1); |
| 41 |
$current2 = substr($sql, $i, 2); |
| 42 |
$current3 = substr($sql, $i, 3); |
| 43 |
$lenEndString = strlen($endString); |
| 44 |
$testEnd = substr($sql, $i, $lenEndString); |
| 45 |
|
| 46 |
if ($current == '"' || $current == "'" || $current2 == '--' |
| 47 |
|| ($current2 == '/*' && $current3 != '/*!' && $current3 != '/*+') |
| 48 |
|| ($current == '#' && $current3 != '#__') |
| 49 |
|| ($comment && $testEnd == $endString)) |
| 50 |
{ |
| 51 |
// check if quoted with previous backslash |
| 52 |
$n = 2; |
| 53 |
|
| 54 |
while (substr($sql, $i - $n + 1, 1) == '\\' && $n < $i) |
| 55 |
{ |
| 56 |
$n++; |
| 57 |
} |
| 58 |
|
| 59 |
// not quoted |
| 60 |
if ($n % 2 == 0) |
| 61 |
{ |
| 62 |
if ($open) |
| 63 |
{ |
| 64 |
if ($testEnd == $endString) |
| 65 |
{ |
| 66 |
if ($comment) |
| 67 |
{ |
| 68 |
$comment = false; |
| 69 |
if ($lenEndString > 1) |
| 70 |
{ |
| 71 |
$i += ($lenEndString - 1); |
| 72 |
$current = substr($sql, $i, 1); |
| 73 |
} |
| 74 |
$start = $i + 1; |
| 75 |
} |
| 76 |
$open = false; |
| 77 |
$endString = ''; |
| 78 |
} |
| 79 |
} |
| 80 |
else |
| 81 |
{ |
| 82 |
$open = true; |
| 83 |
if ($current2 == '--') |
| 84 |
{ |
| 85 |
$endString = "\n"; |
| 86 |
$comment = true; |
| 87 |
} |
| 88 |
elseif ($current2 == '/*') |
| 89 |
{ |
| 90 |
$endString = '*/'; |
| 91 |
$comment = true; |
| 92 |
} |
| 93 |
elseif ($current == '#') |
| 94 |
{ |
| 95 |
$endString = "\n"; |
| 96 |
$comment = true; |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
$endString = $current; |
| 101 |
} |
| 102 |
if ($comment && $start < $i) |
| 103 |
{ |
| 104 |
$query = $query . substr($sql, $start, ($i - $start)); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if ($comment) |
| 111 |
{ |
| 112 |
$start = $i + 1; |
| 113 |
} |
| 114 |
|
| 115 |
if (($current == ';' && !$open) || $i == $end - 1) |
| 116 |
{ |
| 117 |
if ($start <= $i) |
| 118 |
{ |
| 119 |
$query = $query . substr($sql, $start, ($i - $start + 1)); |
| 120 |
} |
| 121 |
$query = trim($query); |
| 122 |
|
| 123 |
if ($query) |
| 124 |
{ |
| 125 |
if (($i == $end - 1) && ($current != ';')) |
| 126 |
{ |
| 127 |
$query = $query . ';'; |
| 128 |
} |
| 129 |
$queries[] = $query; |
| 130 |
} |
| 131 |
|
| 132 |
$query = ''; |
| 133 |
$start = $i + 1; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
return $queries; |
| 138 |
} |
| 139 |
} |
| 140 |
|