X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FImport%2FDataSource%2FSQL.php;h=d552d45103d9dc7a66d049729ad0ee2a8550a441;hb=655da1f919af8318da84d80422841599c332024d;hp=3d123ce6a900322581c4d92ca6fcf5c4324f29e0;hpb=5bab835a1cbe46e3c310cd28fd3f43b24f60a9ed;p=civicrm-core.git diff --git a/CRM/Import/DataSource/SQL.php b/CRM/Import/DataSource/SQL.php index 3d123ce6a9..d552d45103 100644 --- a/CRM/Import/DataSource/SQL.php +++ b/CRM/Import/DataSource/SQL.php @@ -36,14 +36,6 @@ class CRM_Import_DataSource_SQL extends CRM_Import_DataSource { ]; } - /** - * Set variables up before form is built. - * - * @param CRM_Core_Form $form - */ - public function preProcess(&$form) { - } - /** * This is function is called by the form object to get the DataSource's * form snippet. It should add all fields necesarry to get the data @@ -82,35 +74,38 @@ class CRM_Import_DataSource_SQL extends CRM_Import_DataSource { } /** - * Process the form submission. - * - * @param array $params - * @param string $db - * @param \CRM_Core_Form $form + * Initialize the datasource, based on the submitted values stored in the user job. * * @throws \API_Exception * @throws \CRM_Core_Exception - * @throws \Civi\API\Exception\UnauthorizedException */ - public function postProcess(&$params, &$db, &$form) { - $importJob = new CRM_Contact_Import_ImportJob( - CRM_Utils_Array::value('import_table_name', $params), - $params['sqlQuery'], TRUE - ); + public function initialize(): void { + $table = CRM_Utils_SQL_TempTable::build()->setDurable(); + $tableName = $table->getName(); + $table->createWithQuery($this->getSubmittedValue('sqlQuery')); - $form->set('importTableName', $importJob->getTableName()); - // Get the names of the fields to be imported. Any fields starting with an - // underscore are considered to be internal to the import process) + // Get the names of the fields to be imported. $columnsResult = CRM_Core_DAO::executeQuery( - 'SHOW FIELDS FROM ' . $importJob->getTableName() . " - WHERE Field NOT LIKE '\_%'"); + 'SHOW FIELDS FROM ' . $tableName); $columnNames = []; while ($columnsResult->fetch()) { - $columnNames[] = $columnsResult->Field; + if (strpos($columnsResult->Field, ' ') !== FALSE) { + // Remove spaces as the Database object does this + // $keys = str_replace(array(".", " "), "_", array_keys($array)); + // https://lab.civicrm.org/dev/core/-/issues/1337 + $usableColumnName = str_replace(' ', '_', $columnsResult->Field); + CRM_Core_DAO::executeQuery('ALTER TABLE ' . $tableName . ' CHANGE `' . $columnsResult->Field . '` ' . $usableColumnName . ' ' . $columnsResult->Type); + $columnNames[] = $usableColumnName; + } + else { + $columnNames[] = $columnsResult->Field; + } } + + $this->addTrackingFieldsToTable($tableName); $this->updateUserJobMetadata('DataSource', [ - 'table_name' => $importJob->getTableName(), + 'table_name' => $tableName, 'column_headers' => $columnNames, 'number_of_columns' => count($columnNames), ]);