Skip to content

Commit

Permalink
#182: Correctly handle 'enum' database field types on import.
Browse files Browse the repository at this point in the history
  • Loading branch information
lat9 committed Dec 30, 2020
1 parent e472c58 commit 17e1eec
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
27 changes: 26 additions & 1 deletion YOUR_ADMIN/includes/classes/dbio/DbIoHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,9 @@ public function importGetHeader($header)
case 'datetime':
$field_type = $this->tables[$table_name]['fields'][$current_field]['data_type'];
break;
case 'enum':
$field_type = 'enum';
break;
case 'char':
case 'text':
case 'varchar':
Expand Down Expand Up @@ -1464,6 +1467,12 @@ protected function importProcessField($table_name, $field_name, $language_id, $f
$field_value = dbio_substr($field_value, 0, $max_field_length);
}
break;
case 'enum':
if (!in_array($field_value, $this->tables[$table_name]['fields'][$field_name]['enum_values'])) {
$field_error = true;
$this->debugMessage("[*] $import_table_name.$field_name, line#" . $this->stats['record_count'] . ": Value ($field_value) is not one of the field's enumerated values (" . implode(',', $this->tables[$table_name]['fields'][$field_name]['enum_values']) . ").", self::DBIO_ERROR);
}
break;
default:
$field_error = true;
$message = "Unknown datatype (" . $field_type . ") for $table_name::$field_name on line #" . $this->stats['record_count'];
Expand Down Expand Up @@ -1562,7 +1571,8 @@ private function initializeTableFields($table_name, $table_config)

$sql_query = "
SELECT COLUMN_NAME as `column_name`, COLUMN_DEFAULT as `default`, IS_NULLABLE as `nullable`, DATA_TYPE as `data_type`,
CHARACTER_MAXIMUM_LENGTH as `max_length`, NUMERIC_PRECISION as `precision`, COLUMN_KEY as `key`, EXTRA as `extra`
CHARACTER_MAXIMUM_LENGTH as `max_length`, NUMERIC_PRECISION as `precision`, COLUMN_KEY as `key`, EXTRA as `extra`,
COLUMN_TYPE as `column_type`
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '" . DB_DATABASE . "'
AND TABLE_NAME = '$table_name'
Expand All @@ -1582,6 +1592,21 @@ private function initializeTableFields($table_name, $table_config)
} else {
$table_info->fields['include_in_export'] = ($field_overrides !== false && isset($field_overrides[$column_name])) ? $field_overrides[$column_name] : true;
}

// -----
// If the field is an 'enum' type, the possible 'enum' (character) values are present in the 'column_type'
// element returned. That value is formatted in a manner similar to:
//
// enum('lbs','kgs')
//
// ... and the allowed values will be one of lbs or kgs (without the quotes).
//
if ($table_info->fields['data_type'] == 'enum') {
$enum_values = str_replace(array('enum(', "'"), '', $table_info->fields['column_type']);
$table_info->fields['enum_values'] = explode(',', rtrim($enum_values, ')'));
}
unset($table_info->fields['column_type']);

$table_info->fields['nullable'] = ($table_info->fields['nullable'] === 'YES' || $table_info->fields['nullable'] === 'TRUE');
$table_info->fields['sort_order'] = 0;
$this->tables[$table_name]['fields'][$column_name] = $table_info->fields;
Expand Down
4 changes: 2 additions & 2 deletions YOUR_ADMIN/includes/init_includes/init_dbio_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
return;
}

define('DBIO_CURRENT_VERSION', '1.6.4');
define('DBIO_CURRENT_UPDATE_DATE', '2020-10-17');
define('DBIO_CURRENT_VERSION', '1.6.5-beta1');
define('DBIO_CURRENT_UPDATE_DATE', '2020-12-30');

$version_release_date = DBIO_CURRENT_VERSION . ' (' . DBIO_CURRENT_UPDATE_DATE . ')';

Expand Down
9 changes: 8 additions & 1 deletion docs/dbio/readme.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@

<body>
<h1 id="title">Database I/O Manager (DbIo) for Zen Cart&reg;</h1>
<p>Version 1.6.4 Copyright &copy; 2015-2020, <a href="https://vinosdefrutastropicales.com" target="_blank">Vinos de Frutas Tropicales</a>.</p>
<p>Version 1.6.5 Copyright &copy; 2015-2020, <a href="https://vinosdefrutastropicales.com" target="_blank">Vinos de Frutas Tropicales</a>.</p>
<p>Current Support Thread on the Zen Cart forums: <a href="https://www.zen-cart.com/showthread.php?220569-Database-I-O-Manager-(DbIo)-Support-Thread" target="_blank">https://www.zen-cart.com/showthread.php?220569-Database-I-O-Manager-(DbIo)-Support-Thread</a></p>

<div class="tab-container" id="outer-container">
Expand Down Expand Up @@ -593,6 +593,13 @@ <h1 id="title">Database I/O Manager (DbIo) for Zen Cart&reg;</h1>
<noscript><h3>Version History</h3></noscript>
<p>You can view the details of these changes on the plugin's <a href="https://github.com/lat9/dbio/issues" target="_blank">GitHub repository.</a></p>
<ul>
<li>v1.6.5-beta1, 2020-12-30:<ul>
<li>BUGFIX: Properly handle 'enum' type database fields on import.</li>
<li>The following files were changed:<ol>
<li>/YOUR_ADMIN/includes/classes/dbio/DbIoHandler.php</li>
<li>/YOUR_ADMIN/includes/init_includes/init_dbio_admin.php</li>
</ol></li>
</ul></li>
<li>v1.6.4, 2020-10-17:<ul>
<li>BUGFIX: Correct CSV-split handling; split could occur mid-record.</li>
<li>BUGFIX: Correct multi-lingual, customized-template export might have missing fields or fields in the wrong order.</li>
Expand Down

0 comments on commit 17e1eec

Please sign in to comment.