XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
sqlutility.php
1<?php
14
16{
27 public static function splitMySqlFile(&$ret, $sql)
28 {
29 $sql = trim($sql);
30 $sql_len = strlen($sql);
31 $char = '';
32 $string_start = '';
33 $in_string = false;
34
35 for ($i = 0; $i < $sql_len; ++$i) {
36 $char = $sql[$i];
37
38 // We are in a string, check for not escaped end of
39 // strings except for backquotes that can't be escaped
40 if ($in_string) {
41 for (;;) {
42 $i = strpos($sql, $string_start, $i);
43 // No end of string found -> add the current
44 // substring to the returned array
45 if (!$i) {
46 $ret[] = $sql;
47 return true;
48 }
49
50 if ('`' === $string_start || '\\' !== $sql[$i - 1]) {
51 $string_start = '';
52 $in_string = false;
53 break;
54 }
55 // Backquotes or no backslashes before
56 // quotes: it's indeed the end of the
57 // string -> exit the loop
58 // one or more Backslashes before the presumed
59 // end of string...
60
61 // first checks for escaped backslashes
62 $j = 2;
63 $escaped_backslash = false;
64 while ($i-$j > 0 && '\\' === $sql[$i - $j]) {
65 $escaped_backslash = !$escaped_backslash;
66 $j++;
67 }
68 // ... if escaped backslashes: it's really the
69 // end of the string -> exit the loop
70 if ($escaped_backslash) {
71 $string_start = '';
72 $in_string = false;
73 break;
74 }
75 // ... else loop
76
77 $i++; // end if...elseif...else
78 } // end for
79 } // end if (in string)
80 // We are not in a string, first check for delimiter...
81 elseif (';' === $char) {
82 // if delimiter found, add the parsed part to the returned array
83 $ret[] = substr($sql, 0, $i);
84 $sql = ltrim(substr($sql, min($i + 1, $sql_len)));
85 $sql_len = strlen($sql);
86 if ($sql_len) {
87 $i = -1;
88 } else {
89 // The submited statement(s) end(s) here
90 return true;
91 }
92 } // end else if (is delimiter)
93 // ... then check for start of a string,...
94 elseif (('"' === $char) || ('\'' === $char) || ('`' === $char)) {
95 $in_string = true;
96 $string_start = $char;
97 } // end else if (is start of string)
98
99 // for start of a comment (and remove this comment if found)...
100 elseif ('#' === $char || (' ' === $char && $i > 1 && '--' == $sql[$i - 2] . $sql[$i - 1])) {
101 // starting position of the comment depends on the comment type
102 $start_of_comment = (('#' === $sql[$i]) ? $i : $i - 2);
103 // if no "\n" exits in the remaining string, checks for "\r"
104 // (Mac eol style)
105 $end_of_comment = (strpos(' ' . $sql, "\012", $i+2))
106 ?: strpos(' ' . $sql, "\015", $i + 2);
107 if (!$end_of_comment) {
108 // no eol found after '#', add the parsed part to the returned
109 // array and exit
110 // RMV fix for comments at end of file
111 $last = trim(substr($sql, 0, $i-1));
112 if (!empty($last)) {
113 $ret[] = $last;
114 }
115 return true;
116 }
117
118 $sql = substr($sql, 0, $start_of_comment) . ltrim(substr($sql, $end_of_comment));
119 $sql_len = strlen($sql);
120 $i--; // end if...else
121 } // end else if (is comment)
122 } // end for
123
124 // add any rest to the returned array
125 if (!empty($sql) && '' !== trim($sql)) {
126 $ret[] = $sql;
127 }
128 return true;
129 }
130
138 public static function prefixQuery($query, $prefix)
139 {
140 $pattern = "/^(INSERT INTO|CREATE TABLE|ALTER TABLE|UPDATE)(\s)+([`]?)([^`\s]+)\\3(\s)+/siU";
141 $pattern2 = "/^(DROP TABLE)(\s)+([`]?)([^`\s]+)\\3(\s)?$/siU";
142 if (preg_match($pattern, $query, $matches) || preg_match($pattern2, $query, $matches)) {
143 $replace = "\\1 ".$prefix."_\\4\\5";
144 $matches[0] = preg_replace($pattern, $replace, $query);
145
146 // CREATE TABLE force utf8 to utf8mb4 - MySQL ENGINE = InnoDB
147 // DB Engine use default (remove MyISAM)
148 if ($matches[1] === 'CREATE TABLE') {
149 $matches[0] = preg_replace('/ ENGINE=MyISAM/i', '', $matches[0]);
150 if (!preg_match("/ CHARACTER SET /i", $matches[0])) {
151 //$matches[0] .= ' CHARACTER SET utf8';
152 $matches[0] .= ' CHARACTER SET utf8mb4 ';
153 }
154 }
155 return $matches;
156 }
157 return false;
158 }
159}
static prefixQuery($query, $prefix)
static splitMySqlFile(&$ret, $sql)