Ejemplo n.º 1
0
 private function generateCopyCommand($tempTableName, $columns, CsvFile $csvFile, array $options)
 {
     $tableNameEscaped = $this->tableNameEscaped($tempTableName);
     $columnsSql = implode(', ', array_map(function ($column) {
         return $this->quoteIdentifier($column);
     }, $columns));
     $command = "COPY {$tableNameEscaped} ({$columnsSql}) " . " FROM {$this->connection->quote($csvFile->getPathname())}" . " CREDENTIALS 'aws_access_key_id={$this->s3key};aws_secret_access_key={$this->s3secret}' " . " DELIMITER '{$csvFile->getDelimiter()}' ";
     if ($csvFile->getEnclosure()) {
         $command .= "QUOTE {$this->connection->quote($csvFile->getEnclosure())} ";
     }
     if ($csvFile->getEscapedBy()) {
         // raw format
         if ($csvFile->getEscapedBy() != '\\') {
             throw new Exception('Only backshlash can be used as escape character');
         }
         $command .= " ESCAPE ";
     } else {
         $command .= " CSV ";
     }
     if (!empty($options['isGzipped'])) {
         $command .= " GZIP ";
     }
     if (!empty($options['isManifest'])) {
         $command .= " MANIFEST ";
     }
     $command .= " IGNOREHEADER " . $this->getIgnoreLines();
     // custom options
     $command .= " " . implode(" ", $options['copyOptions']);
     return $command;
 }
Ejemplo n.º 2
0
 private function generateCopyCommand($tableName, CsvFile $csvFile)
 {
     $csvOptions = [];
     $csvOptions[] = sprintf('FIELD_DELIMITER = %s', $this->quote($csvFile->getDelimiter()));
     if ($this->getIgnoreLines()) {
         $csvOptions[] = sprintf('SKIP_HEADER = %d', $this->getIgnoreLines());
     }
     if ($csvFile->getEnclosure()) {
         $csvOptions[] = sprintf("FIELD_OPTIONALLY_ENCLOSED_BY = %s", $this->quote($csvFile->getEnclosure()));
         $csvOptions[] = "ESCAPE_UNENCLOSED_FIELD = NONE";
     } elseif ($csvFile->getEscapedBy()) {
         $csvOptions[] = sprintf("ESCAPE_UNENCLOSED_FIELD = %s", $this->quote($csvFile->getEscapedBy()));
     }
     $command = sprintf("COPY INTO %s FROM %s \n            CREDENTIALS = (AWS_KEY_ID = %s AWS_SECRET_KEY = %s)\n            REGION = %s\n            FILE_FORMAT = (TYPE=CSV %s)", $this->nameWithSchemaEscaped($tableName), $this->quote($csvFile->getPathname()), $this->quote($this->s3key), $this->quote($this->s3secret), $this->quote($this->s3region), implode(' ', $csvOptions));
     return $command;
 }
Ejemplo n.º 3
0
 private function importFile($stagingTableName, CsvFile $csvFile)
 {
     $files = $this->getFilesToDownloadFromManifest($csvFile->getPathname());
     foreach ($files as $path) {
         $newCsvPath = new CsvFile($path, $csvFile->getDelimiter(), $csvFile->getEnclosure(), $csvFile->getEscapedBy());
         $this->importTable($stagingTableName, $newCsvPath);
     }
 }