src/CLIParser.h
branchv_0
changeset 3 202ce847990c
parent 0 7727b4d5560d
equal deleted inserted replaced
2:8a30971d285f 3:202ce847990c
    44 		if (value == L"true") return true;
    44 		if (value == L"true") return true;
    45 		else if (value == L"false") return false;
    45 		else if (value == L"false") return false;
    46 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    46 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    47 	}
    47 	}
    48 
    48 
       
    49 	Configuration::InsertMode parseInsertMode(const relpipe::reader::string_t& value) {
       
    50 		if (value == L"single") return Configuration::InsertMode::SINGLE;
       
    51 		else if (value == L"multi") return Configuration::InsertMode::MULTI;
       
    52 		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse InsertMode value: " + value + L" (expecting single or multi)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
       
    53 	}
       
    54 
       
    55 	void updateRelationName(Configuration::RelationConfiguration& currentRelation) {
       
    56 		if (currentRelation.relation.size() == 0) currentRelation.relation = L".*";
       
    57 	}
       
    58 
       
    59 	void addRelation(Configuration& c, Configuration::RelationConfiguration& currentRelation) {
       
    60 		if (currentRelation.relation.size()) {
       
    61 			currentRelation.relationPattern = std::wregex(currentRelation.relation);
       
    62 			c.relationConfigurations.push_back(currentRelation);
       
    63 			currentRelation = Configuration::RelationConfiguration();
       
    64 		}
       
    65 	}
       
    66 
    49 public:
    67 public:
    50 
    68 
    51 	static const relpipe::reader::string_t OPTION_WRITE_HEADER;
    69 	static const relpipe::reader::string_t OPTION_RELATION;
    52 	static const relpipe::reader::string_t OPTION_WRITE_TYPES;
    70 	static const relpipe::reader::string_t OPTION_WRITE_DDL;
       
    71 	static const relpipe::reader::string_t OPTION_WRITE_DML;
       
    72 	static const relpipe::reader::string_t OPTION_WRITE_COLUMN_NAMES;
       
    73 	static const relpipe::reader::string_t OPTION_INSERT_MODE;
       
    74 	static const relpipe::reader::string_t OPTION_TYPE_CAST;
    53 
    75 
    54 	Configuration parse(const std::vector<relpipe::reader::string_t>& arguments) {
    76 	Configuration parse(const std::vector<relpipe::reader::string_t>& arguments) {
    55 		Configuration c;
    77 		Configuration c;
       
    78 		Configuration::RelationConfiguration currentRelation;
    56 
    79 
    57 		for (int i = 0; i < arguments.size();) {
    80 		for (int i = 0; i < arguments.size();) {
    58 			relpipe::reader::string_t option = readNext(arguments, i);
    81 			relpipe::reader::string_t option = readNext(arguments, i);
    59 
    82 
    60 			if (option == OPTION_WRITE_HEADER) {
    83 			if (option == OPTION_RELATION) {
    61 				c.writeHeader = parseBoolean(readNext(arguments, i));
    84 				addRelation(c, currentRelation); // previous relation
    62 			} else if (option == OPTION_WRITE_TYPES) {
    85 				currentRelation.relation = readNext(arguments, i);
    63 				c.writeTypes = parseBoolean(readNext(arguments, i));
    86 			} else if (option == OPTION_WRITE_DDL) {
       
    87 				updateRelationName(currentRelation);
       
    88 				currentRelation.writeDDL = parseBoolean(readNext(arguments, i));
       
    89 			} else if (option == OPTION_WRITE_DML) {
       
    90 				updateRelationName(currentRelation);
       
    91 				currentRelation.writeDML = parseBoolean(readNext(arguments, i));
       
    92 			} else if (option == OPTION_WRITE_COLUMN_NAMES) {
       
    93 				updateRelationName(currentRelation);
       
    94 				currentRelation.writeColumnNames = parseBoolean(readNext(arguments, i));
       
    95 			} else if (option == OPTION_INSERT_MODE) {
       
    96 				updateRelationName(currentRelation);
       
    97 				currentRelation.insertMode = parseInsertMode(readNext(arguments, i));
       
    98 			} else if (option == OPTION_TYPE_CAST) {
       
    99 				updateRelationName(currentRelation);
       
   100 				Configuration::TypeCastRule tcr;
       
   101 				tcr.attribute = std::wregex(readNext(arguments, i));
       
   102 				tcr.type = std::wregex(readNext(arguments, i));
       
   103 				tcr.sqlType = readNext(arguments, i);
       
   104 				currentRelation.typeCastRules.push_back(tcr);
    64 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
   105 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
    65 		}
   106 		}
       
   107 		addRelation(c, currentRelation); // last relation
    66 
   108 
    67 		return c;
   109 		return c;
    68 	}
   110 	}
    69 
   111 
    70 	virtual ~CLIParser() {
   112 	virtual ~CLIParser() {
    71 	}
   113 	}
    72 };
   114 };
    73 
   115 
    74 const relpipe::reader::string_t CLIParser::OPTION_WRITE_HEADER = L"--write-header";
   116 const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
    75 const relpipe::reader::string_t CLIParser::OPTION_WRITE_TYPES = L"--write-types";
   117 const relpipe::common::type::StringX CLIParser::OPTION_WRITE_DDL = L"--write-ddl";
       
   118 const relpipe::common::type::StringX CLIParser::OPTION_WRITE_DML = L"--write-dml";
       
   119 const relpipe::common::type::StringX CLIParser::OPTION_WRITE_COLUMN_NAMES = L"--write-column-names";
       
   120 const relpipe::common::type::StringX CLIParser::OPTION_INSERT_MODE = L"--insert-mode";
       
   121 const relpipe::common::type::StringX CLIParser::OPTION_TYPE_CAST = L"--type-cast";
    76 
   122 
    77 }
   123 }
    78 }
   124 }
    79 }
   125 }