From 0a66a182969ae3fde9f8e54308c4a35fa415685b Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 14 Jul 2020 17:57:39 +1200 Subject: [PATCH] dev/core#183 Fix import csv to use temp table builder --- CRM/Contact/Import/ImportJob.php | 7 +++-- CRM/Import/DataSource/CSV.php | 30 +++++++++++-------- CRM/Import/DataSource/SQL.php | 2 ++ .../Contact/Import/Form/DataSourceTest.php | 13 ++++++++ .../phpunit/CRM/Import/DataSource/CsvTest.php | 1 + 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/CRM/Contact/Import/ImportJob.php b/CRM/Contact/Import/ImportJob.php index e72d45a76d..75d570e637 100644 --- a/CRM/Contact/Import/ImportJob.php +++ b/CRM/Contact/Import/ImportJob.php @@ -141,7 +141,7 @@ class CRM_Contact_Import_ImportJob { //need to differentiate non location elements. // @todo merge this with duplicate code on MapField class. if ($selOne && (is_numeric($selOne) || $selOne === 'Primary')) { - if ($fldName == 'url') { + if ($fldName === 'url') { $header[] = $websiteTypes[$selOne]; $parserParameters['mapperWebsiteType'][$key] = $selOne; } @@ -149,11 +149,11 @@ class CRM_Contact_Import_ImportJob { $header[] = $locationTypes[$selOne]; $parserParameters['mapperLocType'][$key] = $selOne; if ($selTwo && is_numeric($selTwo)) { - if ($fldName == 'phone') { + if ($fldName === 'phone') { $header[] = $phoneTypes[$selTwo]; $parserParameters['mapperPhoneType'][$key] = $selTwo; } - elseif ($fldName == 'im') { + elseif ($fldName === 'im') { $header[] = $imProviders[$selTwo]; $parserParameters['mapperImProvider'][$key] = $selTwo; } @@ -337,6 +337,7 @@ class CRM_Contact_Import_ImportJob { * @param $newTagDesc * * @return array|bool + * @throws \CRM_Core_Exception */ private function _tagImportedContactsWithNewTag( $contactIds, diff --git a/CRM/Import/DataSource/CSV.php b/CRM/Import/DataSource/CSV.php index 33dce5584b..9651e47888 100644 --- a/CRM/Import/DataSource/CSV.php +++ b/CRM/Import/DataSource/CSV.php @@ -76,6 +76,8 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource { * @param array $params * @param string $db * @param \CRM_Core_Form $form + * + * @throws \CRM_Core_Exception */ public function postProcess(&$params, &$db, &$form) { $file = $params['uploadFile']['name']; @@ -102,19 +104,20 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource { * File name to load. * @param bool $headers * Whether the first row contains headers. - * @param string $table + * @param string $tableName * Name of table from which data imported. * @param string $fieldSeparator * Character that separates the various columns in the file. * - * @return string + * @return array * name of the created table + * @throws \CRM_Core_Exception */ private static function _CsvToTable( &$db, $file, $headers = FALSE, - $table = NULL, + $tableName = NULL, $fieldSeparator = ',' ) { $result = []; @@ -184,16 +187,17 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource { } } - // FIXME: we should regen this table's name if it exists rather than drop it - if (!$table) { - $table = 'civicrm_import_job_' . md5(uniqid(rand(), TRUE)); + if ($tableName) { + // Drop previous table if passed in and create new one. + $db->query("DROP TABLE IF EXISTS $tableName"); } - - $db->query("DROP TABLE IF EXISTS $table"); + $table = CRM_Utils_SQL_TempTable::build()->setDurable(); + $tableName = $table->getName(); + // Do we still need this? + $db->query("DROP TABLE IF EXISTS $tableName"); + $table->createWithColumns(implode(' text, ', $columns) . ' text'); $numColumns = count($columns); - $create = "CREATE TABLE $table (" . implode(' text, ', $columns) . " text) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"; - $db->query($create); // the proper approach, but some MySQL installs do not have this enabled // $load = "LOAD DATA LOCAL INFILE '$file' INTO TABLE $table FIELDS TERMINATED BY '$fieldSeparator' OPTIONALLY ENCLOSED BY '\"'"; @@ -230,7 +234,7 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource { $count++; if ($count >= self::NUM_ROWS_TO_INSERT && !empty($sql)) { - $sql = "INSERT IGNORE INTO $table VALUES $sql"; + $sql = "INSERT IGNORE INTO $tableName VALUES $sql"; $db->query($sql); $sql = NULL; @@ -240,14 +244,14 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource { } if (!empty($sql)) { - $sql = "INSERT IGNORE INTO $table VALUES $sql"; + $sql = "INSERT IGNORE INTO $tableName VALUES $sql"; $db->query($sql); } fclose($fd); //get the import tmp table name. - $result['import_table_name'] = $table; + $result['import_table_name'] = $tableName; return $result; } diff --git a/CRM/Import/DataSource/SQL.php b/CRM/Import/DataSource/SQL.php index e74e92242f..26d3e09faa 100644 --- a/CRM/Import/DataSource/SQL.php +++ b/CRM/Import/DataSource/SQL.php @@ -80,6 +80,8 @@ class CRM_Import_DataSource_SQL extends CRM_Import_DataSource { * @param array $params * @param string $db * @param \CRM_Core_Form $form + * + * @throws \CRM_Core_Exception */ public function postProcess(&$params, &$db, &$form) { $importJob = new CRM_Contact_Import_ImportJob( diff --git a/tests/phpunit/CRM/Contact/Import/Form/DataSourceTest.php b/tests/phpunit/CRM/Contact/Import/Form/DataSourceTest.php index 3bc911ea16..4fe0097e16 100644 --- a/tests/phpunit/CRM/Contact/Import/Form/DataSourceTest.php +++ b/tests/phpunit/CRM/Contact/Import/Form/DataSourceTest.php @@ -34,4 +34,17 @@ class CRM_Contact_Import_Form_DataSourceTest extends CiviUnitTestCase { $this->assertEquals([1 => 'Well dressed ducks'], CRM_Core_Smarty::singleton()->get_template_vars('savedMapping')); } + /** + * Check for (lack of) sql errors on sql import post process. + */ + public function testSQLSource() { + $this->callAPISuccess('Mapping', 'create', ['name' => 'Well dressed ducks', 'mapping_type_id' => 'Import Contact']); + /** @var CRM_Import_DataSource_SQL $form */ + $form = $this->getFormObject('CRM_Import_DataSource_SQL'); + $coreForm = $this->getFormObject('CRM_Core_Form'); + $db = NULL; + $params = ['sqlQuery' => 'SELECT 1 as id']; + $form->postProcess($params, $db, $coreForm); + } + } diff --git a/tests/phpunit/CRM/Import/DataSource/CsvTest.php b/tests/phpunit/CRM/Import/DataSource/CsvTest.php index 877ac89faa..a93f31aeb4 100644 --- a/tests/phpunit/CRM/Import/DataSource/CsvTest.php +++ b/tests/phpunit/CRM/Import/DataSource/CsvTest.php @@ -20,6 +20,7 @@ class CRM_Import_DataSource_CsvTest extends CiviUnitTestCase { * @param string $fileName * * @dataProvider getCsvFiles + * @throws \CRM_Core_Exception */ public function testToCsv($fileName) { $dataSource = new CRM_Import_DataSource_CSV(); -- 2.25.1