Minahito
minah****@users*****
2006年 11月 11日 (土) 17:37:04 JST
Index: xoops2jp/html/class/database/oldsqlutility.php diff -u xoops2jp/html/class/database/oldsqlutility.php:1.1.2.1 xoops2jp/html/class/database/oldsqlutility.php:removed --- xoops2jp/html/class/database/oldsqlutility.php:1.1.2.1 Tue Dec 27 00:17:58 2005 +++ xoops2jp/html/class/database/oldsqlutility.php Sat Nov 11 17:37:04 2006 @@ -1,160 +0,0 @@ -<?php -// $Id: oldsqlutility.php,v 1.1.2.1 2005/12/26 15:17:58 minahito 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 - */ -class OldSqlUtility -{ - /** - * 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; - } -} -?> Index: xoops2jp/html/class/database/sqlutility.php diff -u xoops2jp/html/class/database/sqlutility.php:1.2.8.5 xoops2jp/html/class/database/sqlutility.php:1.2.8.5.2.1 --- xoops2jp/html/class/database/sqlutility.php:1.2.8.5 Thu Jun 1 11:41:48 2006 +++ xoops2jp/html/class/database/sqlutility.php Sat Nov 11 17:37:04 2006 @@ -1,122 +1,160 @@ <?php - +// $Id: sqlutility.php,v 1.2.8.5.2.1 2006/11/11 08:37:04 minahito 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 + */ /** - * @author Marijuana + * provide some utility methods for databases + * + * @author Kazumi Ono <onoka****@xoops*****> + * @copyright copyright (c) 2000-2003 XOOPS.org + * + * @package kernel + * @subpackage database */ -class sqlutility +class SqlUtility { - 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; - - // - // If sql file is created in Mac OS, file() can not detect the line end. - // - ini_set("auto_detect_line_endings" ,true); - $sql_query = file(XOOPS_MODULE_PATH.'/'.$dirname.'/'.$file); - - $pattern = "/^(INSERT INTO|CREATE TABLE|UPDATE)(\s)+/siU"; - foreach ( $sql_query as $fline ) { - $fline=trim($fline); - if ( preg_match($pattern,$fline) ) { - $i++; - $this->sql[$i] = trim($fline)."\n"; - } elseif (strlen($fline)>0 && strpos($fline,"#")===false) { - if(isset($this->sql[$i])) { - $this->sql[$i] .= trim($fline)."\n"; - } - else { - $this->sql[$i] = trim($fline)."\n"; + /** + * 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; } - } - } - - 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],$this->safetables) ) { - $this->create[] = $matches[4]; - } - } - return $sql; - } - - function etcchange($sql) - { - $patterns[] = '{dirname}'; - $replacements[] = $this->dirname; - $sql = str_replace($patterns,$replacements,$sql); - $sql = preg_replace('/;\s*$/s','',$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; - } - */ + return false; + } } -?> \ No newline at end of file +?> Index: xoops2jp/html/class/database/mysqldatabase.php diff -u xoops2jp/html/class/database/mysqldatabase.php:1.2.8.5 xoops2jp/html/class/database/mysqldatabase.php:1.2.8.5.2.1 --- xoops2jp/html/class/database/mysqldatabase.php:1.2.8.5 Fri Apr 21 12:03:19 2006 +++ xoops2jp/html/class/database/mysqldatabase.php Sat Nov 11 17:37:04 2006 @@ -1,5 +1,5 @@ <?php -// $Id: mysqldatabase.php,v 1.2.8.5 2006/04/21 03:03:19 minahito Exp $ +// $Id: mysqldatabase.php,v 1.2.8.5.2.1 2006/11/11 08:37:04 minahito Exp $ // ------------------------------------------------------------------------ // // XOOPS - PHP Content Management System // // Copyright (c) 2000 XOOPS.org // @@ -276,13 +276,13 @@ */ function queryFromFile($file){ if (false !== ($fp = fopen($file, 'r'))) { - include_once XOOPS_ROOT_PATH.'/class/database/oldsqlutility.php'; + include_once XOOPS_ROOT_PATH.'/class/database/sqlutility.php'; $sql_queries = trim(fread($fp, filesize($file))); - OldSqlUtility::splitMySqlFile($pieces, $sql_queries); + SqlUtility::splitMySqlFile($pieces, $sql_queries); foreach ($pieces as $query) { // [0] contains the prefixed query // [4] contains unprefixed table name - $prefixed_query = OldSqlUtility::prefixQuery(trim($query), $this->prefix()); + $prefixed_query = SqlUtility::prefixQuery(trim($query), $this->prefix()); if ($prefixed_query != false) { $this->query($prefixed_query[0]); }