OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
import_export::QueryExporterCSV Class Reference

#include <QueryExporterCSV.h>

+ Inheritance diagram for import_export::QueryExporterCSV:
+ Collaboration diagram for import_export::QueryExporterCSV:

Public Member Functions

 QueryExporterCSV ()
 
 ~QueryExporterCSV () override
 
void beginExport (const std::string &file_path, const std::string &layer_name, const CopyParams &copy_params, const std::vector< TargetMetaInfo > &column_infos, const FileCompression file_compression, const ArrayNullHandling array_null_handling) final
 
void exportResults (const std::vector< AggregatedResult > &query_results) final
 
void endExport () final
 
- Public Member Functions inherited from import_export::QueryExporter
 QueryExporter (const FileType file_type)
 
 QueryExporter ()=delete
 
virtual ~QueryExporter ()
 

Private Attributes

std::ofstream outfile_
 
CopyParams copy_params_
 

Additional Inherited Members

- Public Types inherited from import_export::QueryExporter
enum  FileType {
  FileType::kCSV, FileType::kGeoJSON, FileType::kGeoJSONL, FileType::kShapefile,
  FileType::kFlatGeobuf
}
 
enum  FileCompression { FileCompression::kNone, FileCompression::kGZip, FileCompression::kZip }
 
enum  ArrayNullHandling { ArrayNullHandling::kAbortWithWarning, ArrayNullHandling::kExportSentinels, ArrayNullHandling::kExportZeros, ArrayNullHandling::kNullEntireField }
 
- Static Public Member Functions inherited from import_export::QueryExporter
static std::unique_ptr
< QueryExporter
create (const FileType file_type)
 
- Protected Member Functions inherited from import_export::QueryExporter
void validateFileExtensions (const std::string &file_path, const std::string &file_type, const std::unordered_set< std::string > &valid_extensions) const
 
std::string safeColumnName (const std::string &resname, const int column_index)
 
- Protected Attributes inherited from import_export::QueryExporter
const FileType file_type_
 

Detailed Description

Definition at line 25 of file QueryExporterCSV.h.

Constructor & Destructor Documentation

import_export::QueryExporterCSV::QueryExporterCSV ( )
import_export::QueryExporterCSV::~QueryExporterCSV ( )
override

Definition at line 28 of file QueryExporterCSV.cpp.

28 {}

Member Function Documentation

void import_export::QueryExporterCSV::beginExport ( const std::string &  file_path,
const std::string &  layer_name,
const CopyParams copy_params,
const std::vector< TargetMetaInfo > &  column_infos,
const FileCompression  file_compression,
const ArrayNullHandling  array_null_handling 
)
finalvirtual

Implements import_export::QueryExporter.

Definition at line 30 of file QueryExporterCSV.cpp.

References copy_params_, import_export::CopyParams::delimiter, import_export::CopyParams::has_header, import_export::kNoHeader, import_export::QueryExporter::kNone, import_export::CopyParams::line_delim, outfile_, import_export::QueryExporter::safeColumnName(), and import_export::QueryExporter::validateFileExtensions().

35  {
36  validateFileExtensions(file_path, "CSV", {".csv", ".tsv"});
37 
38  // compression?
39  auto actual_file_path{file_path};
40  if (file_compression != FileCompression::kNone) {
41  // @TODO(se) implement post-export compression
42  throw std::runtime_error("Compression not yet supported for this file type");
43  }
44 
45  // open file
46  outfile_.open(actual_file_path);
47  if (!outfile_) {
48  throw std::runtime_error("Failed to create file '" + actual_file_path + "'");
49  }
50 
51  // write header?
52  if (copy_params.has_header != import_export::ImportHeaderRow::kNoHeader) {
53  bool not_first{false};
54  int column_index = 0;
55  for (auto const& column_info : column_infos) {
56  // get name or default
57  auto column_name = safeColumnName(column_info.get_resname(), column_index + 1);
58  // output to header line
59  if (not_first) {
60  outfile_ << copy_params.delimiter;
61  } else {
62  not_first = true;
63  }
64  outfile_ << column_name;
65  column_index++;
66  }
67  outfile_ << copy_params.line_delim;
68  }
69 
70  // keep these
71  copy_params_ = copy_params;
72 }
std::string safeColumnName(const std::string &resname, const int column_index)
void validateFileExtensions(const std::string &file_path, const std::string &file_type, const std::unordered_set< std::string > &valid_extensions) const

+ Here is the call graph for this function:

void import_export::QueryExporterCSV::endExport ( )
finalvirtual

Implements import_export::QueryExporter.

Definition at line 266 of file QueryExporterCSV.cpp.

References outfile_.

266  {
267  // just close the file
268  outfile_.close();
269 }
void import_export::QueryExporterCSV::exportResults ( const std::vector< AggregatedResult > &  query_results)
finalvirtual

Implements import_export::QueryExporter.

Definition at line 142 of file QueryExporterCSV.cpp.

References CHECK, CHECK_EQ, shared::convert_temporal_to_iso_format(), copy_params_, import_export::CopyParams::delimiter, import_export::CopyParams::escape, is_null(), kBIGINT, kBOOLEAN, kDATE, kFLOAT, kINT, kNUMERIC, kSMALLINT, kTIME, kTIMESTAMP, kTINYINT, import_export::CopyParams::line_delim, NULL_BIGINT, NULL_BOOLEAN, NULL_DOUBLE, NULL_FLOAT, NULL_INT, NULL_SMALLINT, import_export::CopyParams::null_str, NULL_TINYINT, outfile_, import_export::CopyParams::quote, import_export::CopyParams::quoted, and import_export::anonymous_namespace{QueryExporterCSV.cpp}::target_value_to_string().

142  {
143  for (auto& agg_result : query_results) {
144  auto results = agg_result.rs;
145  auto const& targets = agg_result.targets_meta;
146 
147  while (true) {
148  auto const crt_row = results->getNextRow(true, true);
149  if (crt_row.empty()) {
150  break;
151  }
152  bool not_first = false;
153  for (size_t i = 0; i < results->colCount(); ++i) {
154  bool is_null{false};
155  auto const tv = crt_row[i];
156  auto const scalar_tv = boost::get<ScalarTargetValue>(&tv);
157  if (not_first) {
159  } else {
160  not_first = true;
161  }
162  if (copy_params_.quoted) {
164  }
165  auto const& ti = targets[i].get_type_info();
166  if (!scalar_tv) {
167  outfile_ << target_value_to_string(crt_row[i], ti, " | ");
168  if (copy_params_.quoted) {
170  }
171  continue;
172  }
173  if (boost::get<int64_t>(scalar_tv)) {
174  auto int_val = *(boost::get<int64_t>(scalar_tv));
175  switch (ti.get_type()) {
176  case kBOOLEAN:
177  is_null = (int_val == NULL_BOOLEAN);
178  break;
179  case kTINYINT:
180  is_null = (int_val == NULL_TINYINT);
181  break;
182  case kSMALLINT:
183  is_null = (int_val == NULL_SMALLINT);
184  break;
185  case kINT:
186  is_null = (int_val == NULL_INT);
187  break;
188  case kBIGINT:
189  is_null = (int_val == NULL_BIGINT);
190  break;
191  case kTIME:
192  case kTIMESTAMP:
193  case kDATE:
194  is_null = (int_val == NULL_BIGINT);
195  break;
196  default:
197  is_null = false;
198  }
199  if (is_null) {
201  } else if (ti.is_time()) {
203  } else if (ti.is_boolean()) {
204  outfile_ << (int_val ? "true" : "false");
205  } else {
206  outfile_ << int_val;
207  }
208  } else if (boost::get<double>(scalar_tv)) {
209  auto real_val = *(boost::get<double>(scalar_tv));
210  if (ti.get_type() == kFLOAT) {
211  is_null = (real_val == NULL_FLOAT);
212  } else {
213  is_null = (real_val == NULL_DOUBLE);
214  }
215  if (is_null) {
217  } else if (ti.get_type() == kNUMERIC) {
218  outfile_ << std::setprecision(ti.get_precision()) << real_val;
219  } else {
220  outfile_ << std::setprecision(std::numeric_limits<double>::digits10 + 1)
221  << real_val;
222  }
223  } else if (boost::get<float>(scalar_tv)) {
224  CHECK_EQ(kFLOAT, ti.get_type());
225  auto real_val = *(boost::get<float>(scalar_tv));
226  if (real_val == NULL_FLOAT) {
228  } else {
229  outfile_ << std::setprecision(std::numeric_limits<float>::digits10 + 1)
230  << real_val;
231  }
232  } else {
233  auto s = boost::get<NullableString>(scalar_tv);
234  is_null = !s || boost::get<void*>(s);
235  if (is_null) {
237  } else {
238  auto s_notnull = boost::get<std::string>(s);
239  CHECK(s_notnull);
240  if (!copy_params_.quoted) {
241  outfile_ << *s_notnull;
242  } else {
243  size_t q = s_notnull->find(copy_params_.quote);
244  if (q == std::string::npos) {
245  outfile_ << *s_notnull;
246  } else {
247  std::string str(*s_notnull);
248  while (q != std::string::npos) {
249  str.insert(q, 1, copy_params_.escape);
250  q = str.find(copy_params_.quote, q + 2);
251  }
252  outfile_ << str;
253  }
254  }
255  }
256  }
257  if (copy_params_.quoted) {
259  }
260  }
262  }
263  }
264 }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
#define NULL_DOUBLE
Definition: sqltypes.h:76
#define NULL_FLOAT
#define NULL_BIGINT
std::string convert_temporal_to_iso_format(const SQLTypeInfo &type_info, int64_t unix_time)
Definition: misc.cpp:109
std::string target_value_to_string(const TargetValue &tv, const SQLTypeInfo &ti, const std::string &delim)
#define NULL_INT
CONSTEXPR DEVICE bool is_null(const T &value)
#define NULL_BOOLEAN
Definition: sqltypes.h:80
#define NULL_TINYINT
#define CHECK(condition)
Definition: Logger.h:291
#define NULL_SMALLINT
Definition: sqltypes.h:72

+ Here is the call graph for this function:

Member Data Documentation

CopyParams import_export::QueryExporterCSV::copy_params_
private

Definition at line 41 of file QueryExporterCSV.h.

Referenced by beginExport(), and exportResults().

std::ofstream import_export::QueryExporterCSV::outfile_
private

Definition at line 40 of file QueryExporterCSV.h.

Referenced by beginExport(), endExport(), and exportResults().


The documentation for this class was generated from the following files: