XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
sqlutility.php
Go to the documentation of this file.
1 <?php
20 defined('XOOPS_ROOT_PATH') or die('Restricted access');
21 
29 class SqlUtility {
42  function splitMySqlFile(&$ret, $sql)
43  {
44  $sql = trim($sql);
45  $sql_len = strlen($sql);
46  $char = '';
47  $string_start = '';
48  $in_string = false;
49 
50  for ($i = 0; $i < $sql_len; ++$i) {
51  $char = $sql[$i];
52  // We are in a string, check for not escaped end of
53  // strings except for backquotes that can't be escaped
54  if ($in_string) {
55  for (;;) {
56  $i = strpos($sql, $string_start, $i);
57  // No end of string found -> add the current
58  // substring to the returned array
59  if (!$i) {
60  $ret[] = $sql;
61  return true;
62  }
63  // Backquotes or no backslashes before
64  // quotes: it's indeed the end of the
65  // string -> exit the loop
66  else if ($string_start == '`' || $sql[$i-1] != '\\') {
67  $string_start = '';
68  $in_string = false;
69  break;
70  }
71  // one or more Backslashes before the presumed
72  // end of string...
73  else {
74  // first checks for escaped backslashes
75  $j = 2;
76  $escaped_backslash = false;
77  while ($i - $j > 0 && $sql[$i - $j] == '\\') {
78  $escaped_backslash = !$escaped_backslash;
79  $j++;
80  }
81  // ... if escaped backslashes: it's really the
82  // end of the string -> exit the loop
83  if ($escaped_backslash) {
84  $string_start = '';
85  $in_string = false;
86  break;
87  }
88  // ... else loop
89  else {
90  $i++;
91  }
92  } // end if...elseif...else
93  } // end for
94  } // end if (in string)
95  // We are not in a string, first check for delimiter...
96  else if ($char == ';') {
97  // if delimiter found, add the parsed part to the returned array
98  $ret[] = substr($sql, 0, $i);
99  $sql = ltrim(substr($sql, min($i + 1, $sql_len)));
100  $sql_len = strlen($sql);
101  if ($sql_len) {
102  $i = -1;
103  } else {
104  // The submited statement(s) end(s) here
105  return true;
106  }
107  } // end else if (is delimiter)
108  // ... then check for start of a string,...
109  else if (($char == '"') || ($char == '\'') || ($char == '`')) {
110  $in_string = true;
111  $string_start = $char;
112  } // end else if (is start of string)
113  // for start of a comment (and remove this comment if found)...
114  else if ($char == '#' || ($char == ' ' && $i > 1 && $sql[$i-2] . $sql[$i-1] == '--')) {
115  // starting position of the comment depends on the comment type
116  $start_of_comment = (($sql[$i] == '#') ? $i : $i-2);
117  // if no "\n" exits in the remaining string, checks for "\r"
118  // (Mac eol style)
119  $end_of_comment = (strpos(' ' . $sql, "\012", $i + 2))
120  ? strpos(' ' . $sql, "\012", $i + 2)
121  : strpos(' ' . $sql, "\015", $i + 2);
122  if (!$end_of_comment) {
123  // no eol found after '#', add the parsed part to the returned
124  // array and exit
125  // RMV fix for comments at end of file
126  //$last = trim(substr($sql, 0, $i-1));
127  //if (!empty($last)) {
128  // $ret[] = $last;
129  //}
130  return true;
131  } else {
132  $sql = substr($sql, 0, $start_of_comment) . ltrim(substr($sql, $end_of_comment));
133  $sql_len = strlen($sql);
134  $i--;
135  } // end if...else
136  } // end else if (is comment)
137  } // end for
138  // add any rest to the returned array
139  if (!empty($sql) && trim($sql) != '') {
140  $ret[] = $sql;
141  }
142  return true; // end for
143  }
144 
152  function prefixQuery($query, $prefix)
153  {
154  $pattern = "/^(INSERT[\s]+INTO|CREATE[\s]+TABLE|ALTER[\s]+TABLE|UPDATE)(\s)+([`]?)([^`\s]+)\\3(\s)+/siU";
155  $pattern2 = "/^(DROP TABLE)(\s)+([`]?)([^`\s]+)\\3(\s)?$/siU";
156  if (preg_match($pattern, $query, $matches) || preg_match($pattern2, $query, $matches)) {
157  $replace = "\\1 " . $prefix . "_\\4\\5";
158  $matches[0] = preg_replace($pattern, $replace, $query);
159  return $matches;
160  }
161  return false;
162  }
163 }
164 
165 ?>