Minahito
minah****@users*****
2005年 12月 27日 (火) 00:45:45 JST
Index: xoops2jp/html/class/database/sqlutility.php diff -u xoops2jp/html/class/database/sqlutility.php:1.2 xoops2jp/html/class/database/sqlutility.php:1.2.8.1 --- xoops2jp/html/class/database/sqlutility.php:1.2 Fri Mar 18 21:51:55 2005 +++ xoops2jp/html/class/database/sqlutility.php Tue Dec 27 00:45:45 2005 @@ -1,160 +1,109 @@ <?php -// $Id: sqlutility.php,v 1.2 2005/03/18 12:51:55 onokazu Exp $ -// sqlutility.php - defines utility class for MySQL database -/** - * @package kernel - * @subpackage database - * - * @author Kazumi Ono <onoka****@xoops*****> - * @copyright copyright (c) 2000-2003 XOOPS.org - */ + /** - * provide some utility methods for databases - * - * @author Kazumi Ono <onoka****@xoops*****> - * @copyright copyright (c) 2000-2003 XOOPS.org - * - * @package kernel - * @subpackage database + * @author Marijuana */ -class SqlUtility +class sqlutility { - /** - * Function from phpMyAdmin (http://phpwizard.net/projects/phpMyAdmin/) - * - * Removes comment and splits large sql files into individual queries - * - * Last revision: September 23, 2001 - gandon - * - * @param array the splitted sql commands - * @param string the sql commands - * @return boolean always true - * @access public - */ - function splitMySqlFile(&$ret, $sql) - { - $sql = trim($sql); - $sql_len = strlen($sql); - $char = ''; - $string_start = ''; - $in_string = false; - - for ($i = 0; $i < $sql_len; ++$i) { - $char = $sql[$i]; - - // We are in a string, check for not escaped end of - // strings except for backquotes that can't be escaped - if ($in_string) { - for (;;) { - $i = strpos($sql, $string_start, $i); - // No end of string found -> add the current - // substring to the returned array - if (!$i) { - $ret[] = $sql; - return true; - } - // Backquotes or no backslashes before - // quotes: it's indeed the end of the - // string -> exit the loop - else if ($string_start == '`' || $sql[$i-1] != '\\') { - $string_start = ''; - $in_string = false; - break; - } - // one or more Backslashes before the presumed - // end of string... - else { - // first checks for escaped backslashes - $j = 2; - $escaped_backslash = false; - while ($i-$j > 0 && $sql[$i-$j] == '\\') { - $escaped_backslash = !$escaped_backslash; - $j++; - } - // ... if escaped backslashes: it's really the - // end of the string -> exit the loop - if ($escaped_backslash) { - $string_start = ''; - $in_string = false; - break; - } - // ... else loop - else { - $i++; - } - } // end if...elseif...else - } // end for - } // end if (in string) - // We are not in a string, first check for delimiter... - else if ($char == ';') { - // if delimiter found, add the parsed part to the returned array - $ret[] = substr($sql, 0, $i); - $sql = ltrim(substr($sql, min($i + 1, $sql_len))); - $sql_len = strlen($sql); - if ($sql_len) { - $i = -1; - } else { - // The submited statement(s) end(s) here - return true; - } - } // end else if (is delimiter) - // ... then check for start of a string,... - else if (($char == '"') || ($char == '\'') || ($char == '`')) { - $in_string = true; - $string_start = $char; - } // end else if (is start of string) - - // for start of a comment (and remove this comment if found)... - else if ($char == '#' || ($char == ' ' && $i > 1 && $sql[$i-2] . $sql[$i-1] == '--')) { - // starting position of the comment depends on the comment type - $start_of_comment = (($sql[$i] == '#') ? $i : $i-2); - // if no "\n" exits in the remaining string, checks for "\r" - // (Mac eol style) - $end_of_comment = (strpos(' ' . $sql, "\012", $i+2)) - ? strpos(' ' . $sql, "\012", $i+2) - : strpos(' ' . $sql, "\015", $i+2); - if (!$end_of_comment) { - // no eol found after '#', add the parsed part to the returned - // array and exit - // RMV fix for comments at end of file - $last = trim(substr($sql, 0, $i-1)); - if (!empty($last)) { - $ret[] = $last; - } - return true; - } else { - $sql = substr($sql, 0, $start_of_comment) . ltrim(substr($sql, $end_of_comment)); - $sql_len = strlen($sql); - $i--; - } // end if...else - } // end else if (is comment) - } // end for - - // add any rest to the returned array - if (!empty($sql) && trim($sql) != '') { - $ret[] = $sql; - } - return true; - } - - /** - * add a prefix.'_' to all tablenames in a query - * - * @param string $query valid SQL query string - * @param string $prefix prefix to add to all table names - * @return mixed FALSE on failure - */ - function prefixQuery($query, $prefix) - { - $pattern = "/^(INSERT INTO|CREATE TABLE|ALTER TABLE|UPDATE)(\s)+([`]?)([^`\s]+)\\3(\s)+/siU"; - $pattern2 = "/^(DROP TABLE)(\s)+([`]?)([^`\s]+)\\3(\s)?$/siU"; - if (preg_match($pattern, $query, $matches) || preg_match($pattern2, $query, $matches)) { - $replace = "\\1 ".$prefix."_\\4\\5"; - $matches[0] = preg_replace($pattern, $replace, $query); - return $matches; - } - return false; - } + var $sql = array(); + var $create = array(); + var $dirname; + var $safetables = array( + 'avatar', + 'avatar_user_link', + 'block_module_link', + 'config', + 'configcategory', + 'configoption', + 'group_permission', + 'groups', + 'groups_users_link', + 'modules', + 'newblocks', + 'users'); + + + function sqlutility($dirname,$file) + { + $this->dirname = $dirname; + $i = 0; + $sql_query = file(ORETEKI_ROOT_PATH.'/modules/'.$dirname.'/'.$file); + $pattern = "/^(INSERT INTO|CREATE TABLE|UPDATE)/siU"; + foreach ( $sql_query as $fline ) { + if ( preg_match($pattern,$fline) ) { + $i++; + $this->sql[$i] = trim($fline)."\n"; + } else { + $this->sql[$i].= trim($fline)."\n"; + } + } + + foreach ( $this->sql as $i => $sql ) { + $sql = $this->prefix($sql); + $this->sql[$i] = $this->etcchange($sql); + } + } + + function prefix($sql) + { + $pattern = "/^(INSERT INTO|CREATE TABLE|UPDATE)(\s)+([`]?)([^`\s]+)\\3(\s)+/siU"; + if ( preg_match($pattern, $sql, $matches) ) { + $replace = "\\1 ".XOOPS_DB_PREFIX."_\\4\\5"; + $sql = preg_replace($pattern, $replace, $sql); + if ( preg_match("/^CREATE TABLE/siU", $sql) && !in_array($matches[4],$safetables) ) { + $this->create[] = $matches[4]; + } + } + return $sql; + } + + function etcchange($sql) + { + $patterns[] = '{dirname}'; + $replacements[] = $this->dirname; + $sql = str_replace($patterns,$replacements,$sql); + return $sql; + } + + function get_sql() + { + return $this->sql; + } + + function get_create() + { + return $this->create; + } + + /* + function changesql($dirname,$file) + { + $sql = array(); + $i = 0; + $sql_query = file(ORETEKI_ROOT_PATH.'/modules/'.$dirname.'/'.$file); + $pattern = "/^(INSERT INTO|CREATE TABLE|UPDATE)/siU"; + foreach ( $sql_query as $fline ) { + if ( preg_match($pattern,$fline) ) { + $i++; + $sql[$i] = trim($fline)."\n"; + } else { + $sql[$i].= trim($fline)."\n"; + } + } + + $pattern = "/^(INSERT INTO|CREATE TABLE|UPDATE)(\s)+([`]?)([^`\s]+)\\3(\s)+/siU"; + foreach ( $sql as $i => $query ) { + if ( preg_match($pattern, $query, $matches) ) { + $replace = "\\1 ".ORETEKI_DB_PREFIX."_\\4\\5"; + $sql[$i] = preg_replace($pattern, $replace, $query); + } + } + $patterns[] = '{dirname}'; + $replacements[] = $dirname; + $sql = str_replace($patterns,$replacements,$sql); + return $sql; + } + */ } -?> +?> \ No newline at end of file