OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
import_helpers.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef IMPORT_HELPERS_H_
18 #define IMPORT_HELPERS_H_
19 
21 
23 #include <boost/algorithm/string.hpp>
24 #include <boost/algorithm/string/replace.hpp>
25 
26 namespace ImportHelpers {
27 
28 inline bool is_reserved_name(const std::string& name) {
29  return reserved_keywords.find(boost::to_upper_copy<std::string>(name)) !=
30  reserved_keywords.end();
31 }
32 
33 inline std::string sanitize_name(const std::string& name, const bool underscore = false) {
34  boost::regex invalid_chars{R"([^0-9a-z_])",
35  boost::regex::extended | boost::regex::icase};
36  std::string sanitized_name =
37  boost::regex_replace(name, invalid_chars, underscore ? "_" : "");
38  boost::regex starts_with_digit{R"(^[0-9].*)"};
39  if (boost::regex_match(sanitized_name, starts_with_digit)) {
40  sanitized_name = "_" + sanitized_name;
41  }
42  if (is_reserved_name(sanitized_name)) {
43  sanitized_name += "_";
44  }
45  return sanitized_name;
46 }
47 
48 template <typename DatumStringType>
49 inline bool is_null_datum(const DatumStringType& datum,
50  const std::string& null_indicator) {
51  return datum == null_indicator || datum == "NULL" || datum == "\\N";
52 }
53 
54 } // namespace ImportHelpers
55 
56 #endif // IMPORT_HELPERS_H_
static std::set< std::string > reserved_keywords
bool is_null_datum(const DatumStringType &datum, const std::string &null_indicator)
bool is_reserved_name(const std::string &name)
std::string sanitize_name(const std::string &name, const bool underscore=false)
string name
Definition: setup.in.py:72