Skip to content

Commit

Permalink
Fixes #4268 - missing field in table definition. Tests added.
Browse files Browse the repository at this point in the history
  • Loading branch information
CaMer0n committed Nov 29, 2020
1 parent 35ad3e7 commit 072c1b3
Show file tree
Hide file tree
Showing 4 changed files with 480 additions and 118 deletions.
104 changes: 92 additions & 12 deletions e107_handlers/db_table_admin_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
*/


// DEPRECATED - USE db_verify_class where possible.


class db_table_admin
{
var $file_buffer = ''; // Contents of a file
var $last_file = '';
protected $file_buffer = ''; // Contents of a file
protected $last_file = '';
protected $errors = array();

// Get list of fields and keys for a table - return FALSE if unsuccessful
// Return as for get_table_def
Expand Down Expand Up @@ -61,6 +62,12 @@ function get_current_table($table_name, $prefix = "")
{
return "No matches";
}

if(isset($matches[0][2]) && is_string($matches[0][2]))
{
$matches[0][2] = trim($matches[0][2]);
}

return $matches;
}

Expand All @@ -73,6 +80,7 @@ function get_current_table($table_name, $prefix = "")
*
* @param string $table_name - If specified, returns only that table's info; otherwise returns a list of all tables
* The table name must include a prefix where appropriate (although not required with standard E107 table definition files)
* @param string $file_name
* @return string|array
* - if error, returns a brief text message
* - if successful, returns an array of table definitions, each of which is itself an array:
Expand Down Expand Up @@ -124,6 +132,8 @@ function parse_field_defs($text)
);
$text = str_replace("\r", "\n", $text);
$field_lines = explode("\n", $text);

$defs = array();
foreach ($field_lines as $fv)
{
unset($defs);
Expand All @@ -148,7 +158,7 @@ function parse_field_defs($text)
case 'UNIQUE':
if (count($fd) < 3)
{
echo "Truncated definition after UNIQUE {$i}: ".$fd[1]."<br />";
$this->errors[] = "Truncated definition after UNIQUE {$fv}: ".$fd[1]."<br />";
}
elseif (strtoupper($fd[1]) == 'KEY')
{
Expand All @@ -159,14 +169,14 @@ function parse_field_defs($text)
}
else
{
echo "Unrecognised word after UNIQUE in definition {$i}: ".$fd[1]."<br />";
$this->errors[] = "Unrecognised word after UNIQUE in definition {$fv}: ".$fd[1]."<br />";
}
break;

case 'FULLTEXT':
if (count($fd) < 3)
{
echo "Truncated definition after FULLTEXT {$i}: ".$fd[1]."<br />";
$this->errors[] = "Truncated definition after FULLTEXT {$fv}: ".$fd[1]."<br />";
}
elseif (strtoupper($fd[1]) == 'KEY')
{
Expand All @@ -177,7 +187,7 @@ function parse_field_defs($text)
}
else
{
echo "Unrecognised word after FULLTEXT in definition {$i}: ".$fd[1]."<br />";
$this->errors[] = "Unrecognised word after FULLTEXT in definition {$fv}: ".$fd[1]."<br />";
}
break;

Expand Down Expand Up @@ -218,7 +228,7 @@ function parse_field_defs($text)
}
else
{ // Syntax error
echo "Unrecognised word in definition {$i} after 'NOT': ".$fd[$i + 1]."<br />";
$this->errors[] = "Unrecognised word in definition {$i} after 'NOT': ".$fd[$i + 1]."<br />";
}
break;
case 'DEFAULT':
Expand Down Expand Up @@ -250,7 +260,7 @@ function parse_field_defs($text)
}
else
{
echo "Partial definition<br />";
$this->errors[] = "Partial definition<br />";
}
}
}
Expand Down Expand Up @@ -678,12 +688,12 @@ function update_table_structure($newStructure, $justCheck = FALSE, $makeNewifNot
}
return TRUE; // Success even if no changes required
}
return FALSE;

}

function createTable($pathToSqlFile = '', $tableName = '', $addPrefix = true, $renameTable = '')
{
$e107 = e107::getInstance();
// $e107 = e107::getInstance();
$tmp = $this->get_table_def($tableName, $pathToSqlFile);
$createText = $tmp[0][0];
$newTableName = ($renameTable ? $renameTable : $tableName);
Expand All @@ -697,7 +707,77 @@ function createTable($pathToSqlFile = '', $tableName = '', $addPrefix = true, $r
}
return e107::getDb()->gen($createText);
}



/**
* Used by $sql->makeTableDef() to create an e_CACHE_DB.$tableName.'.php' file.
* @param array $fieldDefs
* @return array as returned by parse_field_defs()
*/
public function make_field_types($fieldDefs=array())
{
$outDefs = array();

foreach ($fieldDefs as $k => $v)
{
switch ($v['type'])
{
case 'field' :
// if (!empty($v['autoinc']))
// {
//break; Probably include autoinc fields in array
// }

$baseType = preg_replace('#\(.*?\)#', '', $v['fieldtype']); // Should strip any length

switch ($baseType)
{
case 'int' :
case 'integer':
case 'smallint':
case 'shortint' :
case 'tinyint' :
case 'mediumint':
case 'bigint':
$outDefs['_FIELD_TYPES'][$v['name']] = 'int';
break;

case 'char' :
case 'text' :
case 'varchar' :
case 'tinytext' :
case 'mediumtext' :
case 'longtext' :
case 'enum' :
$outDefs['_FIELD_TYPES'][$v['name']] = 'escape'; //XXX toDB() causes serious BC issues.
break;
}

// if($v['name'])


if (isset($v['nulltype']) && !isset($v['default']))
{
$outDefs['_NOTNULL'][$v['name']] = '';
}
break;
case 'pkey' :
case 'ukey' :
case 'key' :
case 'ftkey' :
break; // Do nothing with keys for now
default :
$this->errors[] = "Unexpected field type: {$k} => {$v['type']}<br />";
}
}

return $outDefs;


}



}


Expand Down
54 changes: 1 addition & 53 deletions e107_handlers/e_db_pdo_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2728,60 +2728,8 @@ protected function makeTableDef($tableName)
$fieldDefs = $dbAdm->parse_field_defs($baseStruct); // Required definitions
if (!$fieldDefs) return false;

$outDefs = array();
$outDefs = $dbAdm->make_field_types($fieldDefs);


foreach ($fieldDefs as $k => $v)
{
switch ($v['type'])
{
case 'field' :
if (vartrue($v['autoinc']))
{
//break; Probably include autoinc fields in array
}

$baseType = preg_replace('#\(\d+?\)#', '', $v['fieldtype']); // Should strip any length

switch ($baseType)
{
case 'int' :
case 'integer':
case 'smallint':
case 'shortint' :
case 'tinyint' :
case 'mediumint':
$outDefs['_FIELD_TYPES'][$v['name']] = 'int';
break;

case 'char' :
case 'text' :
case 'varchar' :
case 'tinytext' :
case 'mediumtext' :
case 'longtext' :
$outDefs['_FIELD_TYPES'][$v['name']] = 'escape'; //XXX toDB() causes serious BC issues.
break;
}

// if($v['name'])


if (isset($v['nulltype']) && !isset($v['default']))
{
$outDefs['_NOTNULL'][$v['name']] = '';
}
break;
case 'pkey' :
case 'ukey' :
case 'key' :
case 'ftkey' :
break; // Do nothing with keys for now
default :
echo "Unexpected field type: {$k} => {$v['type']}<br />";
}
}
// $array = e107::getArrayStorage();
$this->dbFieldDefs[$tableName] = $outDefs;
$toSave = e107::serialize($outDefs, false); // 2nd parameter to TRUE if needs to be written to DB

Expand Down
55 changes: 2 additions & 53 deletions e107_handlers/mysql_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ function _getFieldValue($fieldKey, $fieldValue, &$fieldTypes)
case 'array':
if(is_array($fieldValue))
{
return "'".e107::getArrayStorage()->writeArray($fieldValue, true)."'";
return "'".e107::getArrayStorage()->WriteArray($fieldValue, true)."'";
}
return "'". (string) $fieldValue."'";
break;
Expand Down Expand Up @@ -2565,59 +2565,8 @@ protected function makeTableDef($tableName)
$fieldDefs = $dbAdm->parse_field_defs($baseStruct); // Required definitions
if (!$fieldDefs) return false;

$outDefs = array();
$outDefs = $dbAdm->make_field_types($fieldDefs);


foreach ($fieldDefs as $k => $v)
{
switch ($v['type'])
{
case 'field' :
if (vartrue($v['autoinc']))
{
//break; Probably include autoinc fields in array
}

$baseType = preg_replace('#\(\d+?\)#', '', $v['fieldtype']); // Should strip any length

switch ($baseType)
{
case 'int' :
case 'integer':
case 'shortint' :
case 'tinyint' :
case 'mediumint':
$outDefs['_FIELD_TYPES'][$v['name']] = 'int';
break;

case 'char' :
case 'text' :
case 'varchar' :
case 'tinytext' :
case 'mediumtext' :
case 'longtext' :
$outDefs['_FIELD_TYPES'][$v['name']] = 'escape'; //XXX toDB() causes serious BC issues.
break;
}

// if($v['name'])


if (isset($v['nulltype']) && !isset($v['default']))
{
$outDefs['_NOTNULL'][$v['name']] = '';
}
break;
case 'pkey' :
case 'ukey' :
case 'key' :
case 'ftkey' :
break; // Do nothing with keys for now
default :
echo "Unexpected field type: {$k} => {$v['type']}<br />";
}
}
// $array = e107::getArrayStorage();
$this->dbFieldDefs[$tableName] = $outDefs;
$toSave = e107::serialize($outDefs, false); // 2nd parameter to TRUE if needs to be written to DB

Expand Down
Loading

0 comments on commit 072c1b3

Please sign in to comment.