src/SQLHandler.h
branchv_0
changeset 2 8a30971d285f
parent 1 4c0366e1b4df
child 3 202ce847990c
equal deleted inserted replaced
1:4c0366e1b4df 2:8a30971d285f
    82 	void writeRecordCount() {
    82 	void writeRecordCount() {
    83 		// currently disabled due to relpipe-in-sql parser issues with last comment without any following expression
    83 		// currently disabled due to relpipe-in-sql parser issues with last comment without any following expression
    84 		// output << "-- Record count: " << recordCount << std::endl;
    84 		// output << "-- Record count: " << recordCount << std::endl;
    85 	}
    85 	}
    86 
    86 
       
    87 	void endRelation() {
       
    88 
       
    89 		if (configuration.insertMode == Configuration::InsertMode::MULTI) {
       
    90 			output << std::endl << ";" << std::endl;
       
    91 		}
       
    92 
       
    93 		writeRecordCount();
       
    94 	}
       
    95 
    87 public:
    96 public:
    88 
    97 
    89 	SQLHandler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) {
    98 	SQLHandler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) {
    90 	}
    99 	}
    91 
   100 
   102 		// TODO: optional wrapping at certain width (like 80 characters)?
   111 		// TODO: optional wrapping at certain width (like 80 characters)?
   103 		// TODO: optional syntax highlighting?
   112 		// TODO: optional syntax highlighting?
   104 		// TODO: share code/behavior with relpipe-tr-sql (but it uses parametrized statements)
   113 		// TODO: share code/behavior with relpipe-tr-sql (but it uses parametrized statements)
   105 
   114 
   106 		if (currentTable.size()) {
   115 		if (currentTable.size()) {
   107 			writeRecordCount();
   116 			endRelation();
   108 			output << std::endl;
   117 			output << std::endl;
   109 		}
   118 		}
   110 
   119 
   111 		currentTable = name;
   120 		currentTable = name;
   112 		currentAttributes = attributes;
   121 		currentAttributes = attributes;
   143 			// TODO: optional use of function/procedure instead of INSERT
   152 			// TODO: optional use of function/procedure instead of INSERT
   144 			// TODO: optional INSERT of multiple records
   153 			// TODO: optional INSERT of multiple records
   145 			// TODO: custom line-ends + indentation
   154 			// TODO: custom line-ends + indentation
   146 			// TODO: optionally write also the column names
   155 			// TODO: optionally write also the column names
   147 			recordCount++;
   156 			recordCount++;
   148 			output << "INSERT INTO ";
   157 
   149 			writeIdentifier(output, convertor.to_bytes(currentTable));
   158 			if (configuration.insertMode == Configuration::InsertMode::SINGLE) {
   150 			output << " VALUES (";
   159 				output << "INSERT INTO ";
       
   160 				writeIdentifier(output, convertor.to_bytes(currentTable));
       
   161 
       
   162 				output << " (";
       
   163 				for (size_t i = 0, limit = currentAttributes.size(); i < limit; i++) {
       
   164 					writeIdentifier(output, convertor.to_bytes(currentAttributes[i].getAttributeName()));
       
   165 					if (i < (limit - 1)) output << ", ";
       
   166 				}
       
   167 				output << ")";
       
   168 
       
   169 				output << " VALUES (";
       
   170 			} else if (configuration.insertMode == Configuration::InsertMode::MULTI) {
       
   171 				if (recordCount == 1) {
       
   172 					// --------
       
   173 					output << "INSERT INTO ";
       
   174 					writeIdentifier(output, convertor.to_bytes(currentTable));
       
   175 
       
   176 					output << "\n\t(";
       
   177 					for (size_t i = 0, limit = currentAttributes.size(); i < limit; i++) {
       
   178 						writeIdentifier(output, convertor.to_bytes(currentAttributes[i].getAttributeName()));
       
   179 						if (i < (limit - 1)) output << ", ";
       
   180 					}
       
   181 					output << ")";
       
   182 					// --------
       
   183 
       
   184 					output << std::endl << "VALUES" << std::endl;
       
   185 				} else {
       
   186 					output << "," << std::endl;
       
   187 				}
       
   188 				output << "\t(";
       
   189 			} else {
       
   190 				throw RelpipeSQLWriterException(L"Unsupported InsertMode: " + std::to_wstring((int) configuration.insertMode));
       
   191 			}
   151 		}
   192 		}
   152 
   193 
   153 		valueCount++;
   194 		valueCount++;
   154 
   195 
   155 		if (value.size() > 0) {
   196 		if (value.size() > 0) {
   161 		}
   202 		}
   162 
   203 
   163 		if (valueCount % currentAttributes.size()) {
   204 		if (valueCount % currentAttributes.size()) {
   164 			output << ", ";
   205 			output << ", ";
   165 		} else {
   206 		} else {
   166 			output << ");" << std::endl;
   207 			if (configuration.insertMode == Configuration::InsertMode::SINGLE) {
       
   208 				output << ");" << std::endl;
       
   209 			} else if (configuration.insertMode == Configuration::InsertMode::MULTI) {
       
   210 				output << ")";
       
   211 			} else {
       
   212 				throw RelpipeSQLWriterException(L"Unsupported InsertMode: " + std::to_wstring((int) configuration.insertMode));
       
   213 			}
   167 			valueCount = 0;
   214 			valueCount = 0;
   168 		}
   215 		}
   169 	}
   216 	}
   170 
   217 
   171 	void endOfPipe() {
   218 	void endOfPipe() {
   172 		if (currentTable.size()) {
   219 		if (currentTable.size()) {
   173 			writeRecordCount();
   220 			endRelation();
   174 		}
   221 		}
   175 		output.flush();
   222 		output.flush();
   176 	}
   223 	}
   177 
   224 
   178 };
   225 };