1
0
mirror of https://github.com/e107inc/e107.git synced 2025-07-31 20:00:37 +02:00

Merge pull request #3190 from SimSync/mysql_check_index

Mysql check index, DB Verify
This commit is contained in:
Cameron
2018-06-15 12:55:37 -07:00
committed by GitHub
2 changed files with 84 additions and 3 deletions

View File

@@ -641,7 +641,10 @@ class db_verify
// print_a($data);
if($data['type'])
{
return $data['type']." (".$data['field'].");";
//return $data['type']." (".$data['field'].");";
// Check if index keyname exists and add backticks
$keyname = (!empty($data['keyname']) ? " `".$data['keyname']."`" : "");
return $data['type'] . $keyname . " (" . $data['field'] . ");";
}
else
{
@@ -919,9 +922,15 @@ class db_verify
function getIndex($data, $print = false)
{
// $regex = "/(?:(PRIMARY|UNIQUE|FULLTEXT))?[\s]*?KEY (?: ?`?([\w]*)`?)[\s]* ?(?:\([\s]?`?([\w,]*[\s]?)`?\))?,?/i";
$regex = "/(?:(PRIMARY|UNIQUE|FULLTEXT|FOREIGN))?[\s]*?KEY (?: ?`?([\w]*)`?)[\s]* ?(?:\([\s]?([\w\s,`]*[\s]?)`?\))?,?/i";
// $regex = "/(?:(PRIMARY|UNIQUE|FULLTEXT|FOREIGN))?[\s]*?KEY (?: ?`?([\w]*)`?)[\s]* ?(?:\([\s]?([\w\s,`]*[\s]?)`?\))?,?/i";
$regex = "/(?:(PRIMARY|UNIQUE|FULLTEXT|FOREIGN))?[\s]*?(INDEX|KEY) (?: ?`?([\w]*)`?)[\s]* ?(?:\([\s]?([\w\s,`]*[\s]?)`?\))?,?/i";
preg_match_all($regex,$data,$m);
if (count($m) > 0)
{
unset($m[2]);
$m = array_combine(range(0, count($m)-1), array_values($m));
}
$ret = array();
if($print)
@@ -933,6 +942,7 @@ class db_verify
$fieldReplace = array("`"," ");
foreach($m[3] as $k=>$val)
{
if(!$val) continue;

View File

@@ -2356,6 +2356,77 @@ class e_db_mysql
}
/**
* Determines if a table index (key) exist.
*
* @param string $table - table name (no prefix)
* @param string $keyname - Name of the key to
* @param array $fields - OPTIONAL list of fieldnames, the index (key) must contain
* @param boolean $retinfo = FALSE - just returns true|false. TRUE - returns all key info
* @return array|boolean - FALSE on error, key information on success
*/
function index($table, $keyname, $fields=null, $retinfo = FALSE)
{
if(!$this->mySQLdefaultdb)
{
global $mySQLdefaultdb;
$this->mySQLdefaultdb = $mySQLdefaultdb;
}
if(!$this->mySQLaccess)
{
global $db_ConnectionID;
$this->mySQLaccess = $db_ConnectionID;
}
if (!empty($fields) && !is_array($fields))
{
$fields = explode(',', str_replace(' ', '', $fields));
}
elseif(empty($fields))
{
$fields = array();
}
$check_field = count($fields) > 0;
$info = array();
$result = $this->gen("SHOW INDEX FROM ".$this->mySQLPrefix.$table);
if ($result && ($this->rowCount() > 0))
{
$c=0;
while ($row = $this->fetch())
{
// Check for match of key name - and allow that key might not be used
if($keyname == $row['Key_name'])
{
// a key can contain severeal fields which are returned as 1 row per field
if (!$check_field)
{ // Check only for keyname
$info[] = $row;
}
elseif ($check_field && in_array($row['Column_name'], $fields))
{ // Check also for fieldnames
$info[] = $row;
}
$c++;
}
}
if (count($info) > 0)
{
// Kex does not consist of all keys
if ($check_field && $c != count($fields)) return false;
// Return full information
if ($retinfo) return $info;
// Return only if index was found
return true;
}
}
return FALSE;
}
/**
* A pointer to mysql_real_escape_string() - see http://www.php.net/mysql_real_escape_string
*