adamdebreceni commented on a change in pull request #1004:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1004#discussion_r601475374



##########
File path: extensions/sql/processors/ExecuteSQL.cpp
##########
@@ -51,72 +41,87 @@ namespace processors {
 
 const std::string ExecuteSQL::ProcessorName("ExecuteSQL");
 
-const core::Property ExecuteSQL::s_sqlSelectQuery(
-  core::PropertyBuilder::createProperty("SQL select 
query")->isRequired(true)->withDescription(
+const core::Property ExecuteSQL::SQLSelectQuery(
+  core::PropertyBuilder::createProperty("SQL select query")
+  ->withDescription(
     "The SQL select query to execute. The query can be empty, a constant 
value, or built from attributes using Expression Language. "
     "If this property is specified, it will be used regardless of the content 
of incoming flowfiles. "
     "If this property is empty, the content of the incoming flow file is 
expected to contain a valid SQL select query, to be issued by the processor to 
the database. "
-    "Note that Expression Language is not evaluated for flow file 
contents.")->supportsExpressionLanguage(true)->build());
+    "Note that Expression Language is not evaluated for flow file contents.")
+  ->supportsExpressionLanguage(true)->build());
 
-const core::Property ExecuteSQL::s_maxRowsPerFlowFile(
-  core::PropertyBuilder::createProperty("Max Rows Per Flow 
File")->isRequired(true)->withDefaultValue<int>(0)->withDescription(
-    "The maximum number of result rows that will be included intoi a flow 
file. If zero then all will be placed into the flow 
file")->supportsExpressionLanguage(true)->build());
+const core::Relationship ExecuteSQL::Success("success", "Successfully created 
FlowFile from SQL query result set.");
 
-const core::Relationship ExecuteSQL::s_success("success", "Successfully 
created FlowFile from SQL query result set.");
-
-static const std::string ResultRowCount = "executesql.row.count";
+const std::string ExecuteSQL::RESULT_ROW_COUNT = "executesql.row.count";
+const std::string ExecuteSQL::INPUT_FLOW_FILE_UUID = "input.flowfile.uuid";
 
 ExecuteSQL::ExecuteSQL(const std::string& name, utils::Identifier uuid)
-  : SQLProcessor(name, uuid), max_rows_(0) {
+  : SQLProcessor(name, uuid, logging::LoggerFactory<ExecuteSQL>::getLogger()) {
 }
 
-ExecuteSQL::~ExecuteSQL() = default;
-
 void ExecuteSQL::initialize() {
   //! Set the supported properties
-  setSupportedProperties({ dbControllerService(), outputFormat(), 
s_sqlSelectQuery, s_maxRowsPerFlowFile});
+  setSupportedProperties({ DBControllerService, OutputFormat, SQLSelectQuery, 
MaxRowsPerFlowFile});
 
   //! Set the supported relationships
-  setSupportedRelationships({ s_success });
+  setSupportedRelationships({ Success });
 }
 
-void ExecuteSQL::processOnSchedule(core::ProcessContext &context) {
-  initOutputFormat(context);
+void ExecuteSQL::processOnSchedule(core::ProcessContext& context) {
+  context.getProperty(OutputFormat.getName(), output_format_);
 
-  context.getProperty(s_sqlSelectQuery.getName(), sqlSelectQuery_);
-  context.getProperty(s_maxRowsPerFlowFile.getName(), max_rows_);
+  max_rows_ = [&] {
+    uint64_t max_rows;
+    context.getProperty(MaxRowsPerFlowFile.getName(), max_rows);
+    return gsl::narrow<size_t>(max_rows);
+  }();
 }
 
-void ExecuteSQL::processOnTrigger(core::ProcessSession& session) {
-  auto statement = connection_->prepareStatement(sqlSelectQuery_);
+void ExecuteSQL::processOnTrigger(core::ProcessContext& context, 
core::ProcessSession& session) {
+  auto input_flow_file = session.get();
 
-  auto rowset = statement->execute();
-
-  int count = 0;
-  size_t rowCount = 0;
-  sql::JSONSQLWriter sqlWriter(isJSONPretty());
-  sql::SQLRowsetProcessor sqlRowsetProcessor(rowset, { &sqlWriter });
+  std::string query;
+  if (!context.getProperty(SQLSelectQuery, query, input_flow_file)) {
+    if (!input_flow_file) {
+      throw Exception(PROCESSOR_EXCEPTION,
+                      "No incoming FlowFile and the \"SQL select query\" 
processor property is not specified");

Review comment:
       done

##########
File path: extensions/sql/processors/ExecuteSQL.cpp
##########
@@ -51,72 +41,87 @@ namespace processors {
 
 const std::string ExecuteSQL::ProcessorName("ExecuteSQL");
 
-const core::Property ExecuteSQL::s_sqlSelectQuery(
-  core::PropertyBuilder::createProperty("SQL select 
query")->isRequired(true)->withDescription(
+const core::Property ExecuteSQL::SQLSelectQuery(
+  core::PropertyBuilder::createProperty("SQL select query")
+  ->withDescription(
     "The SQL select query to execute. The query can be empty, a constant 
value, or built from attributes using Expression Language. "
     "If this property is specified, it will be used regardless of the content 
of incoming flowfiles. "
     "If this property is empty, the content of the incoming flow file is 
expected to contain a valid SQL select query, to be issued by the processor to 
the database. "
-    "Note that Expression Language is not evaluated for flow file 
contents.")->supportsExpressionLanguage(true)->build());
+    "Note that Expression Language is not evaluated for flow file contents.")
+  ->supportsExpressionLanguage(true)->build());
 
-const core::Property ExecuteSQL::s_maxRowsPerFlowFile(
-  core::PropertyBuilder::createProperty("Max Rows Per Flow 
File")->isRequired(true)->withDefaultValue<int>(0)->withDescription(
-    "The maximum number of result rows that will be included intoi a flow 
file. If zero then all will be placed into the flow 
file")->supportsExpressionLanguage(true)->build());
+const core::Relationship ExecuteSQL::Success("success", "Successfully created 
FlowFile from SQL query result set.");
 
-const core::Relationship ExecuteSQL::s_success("success", "Successfully 
created FlowFile from SQL query result set.");
-
-static const std::string ResultRowCount = "executesql.row.count";
+const std::string ExecuteSQL::RESULT_ROW_COUNT = "executesql.row.count";
+const std::string ExecuteSQL::INPUT_FLOW_FILE_UUID = "input.flowfile.uuid";
 
 ExecuteSQL::ExecuteSQL(const std::string& name, utils::Identifier uuid)
-  : SQLProcessor(name, uuid), max_rows_(0) {
+  : SQLProcessor(name, uuid, logging::LoggerFactory<ExecuteSQL>::getLogger()) {
 }
 
-ExecuteSQL::~ExecuteSQL() = default;
-
 void ExecuteSQL::initialize() {
   //! Set the supported properties
-  setSupportedProperties({ dbControllerService(), outputFormat(), 
s_sqlSelectQuery, s_maxRowsPerFlowFile});
+  setSupportedProperties({ DBControllerService, OutputFormat, SQLSelectQuery, 
MaxRowsPerFlowFile});
 
   //! Set the supported relationships
-  setSupportedRelationships({ s_success });
+  setSupportedRelationships({ Success });
 }
 
-void ExecuteSQL::processOnSchedule(core::ProcessContext &context) {
-  initOutputFormat(context);
+void ExecuteSQL::processOnSchedule(core::ProcessContext& context) {
+  context.getProperty(OutputFormat.getName(), output_format_);
 
-  context.getProperty(s_sqlSelectQuery.getName(), sqlSelectQuery_);
-  context.getProperty(s_maxRowsPerFlowFile.getName(), max_rows_);
+  max_rows_ = [&] {
+    uint64_t max_rows;
+    context.getProperty(MaxRowsPerFlowFile.getName(), max_rows);
+    return gsl::narrow<size_t>(max_rows);
+  }();
 }
 
-void ExecuteSQL::processOnTrigger(core::ProcessSession& session) {
-  auto statement = connection_->prepareStatement(sqlSelectQuery_);
+void ExecuteSQL::processOnTrigger(core::ProcessContext& context, 
core::ProcessSession& session) {
+  auto input_flow_file = session.get();
 
-  auto rowset = statement->execute();
-
-  int count = 0;
-  size_t rowCount = 0;
-  sql::JSONSQLWriter sqlWriter(isJSONPretty());
-  sql::SQLRowsetProcessor sqlRowsetProcessor(rowset, { &sqlWriter });
+  std::string query;
+  if (!context.getProperty(SQLSelectQuery, query, input_flow_file)) {
+    if (!input_flow_file) {
+      throw Exception(PROCESSOR_EXCEPTION,
+                      "No incoming FlowFile and the \"SQL select query\" 
processor property is not specified");
+    }
+    logger_->log_debug("Using the contents of the flow file as the SQL 
statement");
+    auto buffer = std::make_shared<io::BufferStream>();
+    InputStreamPipe content_reader{buffer};
+    session.read(input_flow_file, &content_reader);
+    query = std::string{reinterpret_cast<const char *>(buffer->getBuffer()), 
buffer->size()};
+  }
+  if (query.empty()) {
+    throw Exception(PROCESSOR_EXCEPTION, "Empty SQL statement");
+  }
+
+  auto row_set = 
connection_->prepareStatement(query)->execute(collectArguments(input_flow_file));
+
+  sql::JSONSQLWriter sqlWriter{output_format_ == OutputType::JSONPretty};

Review comment:
       done

##########
File path: extensions/sql/processors/ExecuteSQL.cpp
##########
@@ -51,72 +41,87 @@ namespace processors {
 
 const std::string ExecuteSQL::ProcessorName("ExecuteSQL");
 
-const core::Property ExecuteSQL::s_sqlSelectQuery(
-  core::PropertyBuilder::createProperty("SQL select 
query")->isRequired(true)->withDescription(
+const core::Property ExecuteSQL::SQLSelectQuery(
+  core::PropertyBuilder::createProperty("SQL select query")
+  ->withDescription(
     "The SQL select query to execute. The query can be empty, a constant 
value, or built from attributes using Expression Language. "
     "If this property is specified, it will be used regardless of the content 
of incoming flowfiles. "
     "If this property is empty, the content of the incoming flow file is 
expected to contain a valid SQL select query, to be issued by the processor to 
the database. "
-    "Note that Expression Language is not evaluated for flow file 
contents.")->supportsExpressionLanguage(true)->build());
+    "Note that Expression Language is not evaluated for flow file contents.")
+  ->supportsExpressionLanguage(true)->build());
 
-const core::Property ExecuteSQL::s_maxRowsPerFlowFile(
-  core::PropertyBuilder::createProperty("Max Rows Per Flow 
File")->isRequired(true)->withDefaultValue<int>(0)->withDescription(
-    "The maximum number of result rows that will be included intoi a flow 
file. If zero then all will be placed into the flow 
file")->supportsExpressionLanguage(true)->build());
+const core::Relationship ExecuteSQL::Success("success", "Successfully created 
FlowFile from SQL query result set.");
 
-const core::Relationship ExecuteSQL::s_success("success", "Successfully 
created FlowFile from SQL query result set.");
-
-static const std::string ResultRowCount = "executesql.row.count";
+const std::string ExecuteSQL::RESULT_ROW_COUNT = "executesql.row.count";
+const std::string ExecuteSQL::INPUT_FLOW_FILE_UUID = "input.flowfile.uuid";
 
 ExecuteSQL::ExecuteSQL(const std::string& name, utils::Identifier uuid)
-  : SQLProcessor(name, uuid), max_rows_(0) {
+  : SQLProcessor(name, uuid, logging::LoggerFactory<ExecuteSQL>::getLogger()) {
 }
 
-ExecuteSQL::~ExecuteSQL() = default;
-
 void ExecuteSQL::initialize() {
   //! Set the supported properties
-  setSupportedProperties({ dbControllerService(), outputFormat(), 
s_sqlSelectQuery, s_maxRowsPerFlowFile});
+  setSupportedProperties({ DBControllerService, OutputFormat, SQLSelectQuery, 
MaxRowsPerFlowFile});
 
   //! Set the supported relationships
-  setSupportedRelationships({ s_success });
+  setSupportedRelationships({ Success });
 }
 
-void ExecuteSQL::processOnSchedule(core::ProcessContext &context) {
-  initOutputFormat(context);
+void ExecuteSQL::processOnSchedule(core::ProcessContext& context) {
+  context.getProperty(OutputFormat.getName(), output_format_);
 
-  context.getProperty(s_sqlSelectQuery.getName(), sqlSelectQuery_);
-  context.getProperty(s_maxRowsPerFlowFile.getName(), max_rows_);
+  max_rows_ = [&] {
+    uint64_t max_rows;
+    context.getProperty(MaxRowsPerFlowFile.getName(), max_rows);
+    return gsl::narrow<size_t>(max_rows);
+  }();
 }
 
-void ExecuteSQL::processOnTrigger(core::ProcessSession& session) {
-  auto statement = connection_->prepareStatement(sqlSelectQuery_);
+void ExecuteSQL::processOnTrigger(core::ProcessContext& context, 
core::ProcessSession& session) {
+  auto input_flow_file = session.get();
 
-  auto rowset = statement->execute();
-
-  int count = 0;
-  size_t rowCount = 0;
-  sql::JSONSQLWriter sqlWriter(isJSONPretty());
-  sql::SQLRowsetProcessor sqlRowsetProcessor(rowset, { &sqlWriter });
+  std::string query;
+  if (!context.getProperty(SQLSelectQuery, query, input_flow_file)) {
+    if (!input_flow_file) {
+      throw Exception(PROCESSOR_EXCEPTION,
+                      "No incoming FlowFile and the \"SQL select query\" 
processor property is not specified");
+    }
+    logger_->log_debug("Using the contents of the flow file as the SQL 
statement");
+    auto buffer = std::make_shared<io::BufferStream>();
+    InputStreamPipe content_reader{buffer};
+    session.read(input_flow_file, &content_reader);
+    query = std::string{reinterpret_cast<const char *>(buffer->getBuffer()), 
buffer->size()};
+  }
+  if (query.empty()) {
+    throw Exception(PROCESSOR_EXCEPTION, "Empty SQL statement");
+  }
+
+  auto row_set = 
connection_->prepareStatement(query)->execute(collectArguments(input_flow_file));
+
+  sql::JSONSQLWriter sqlWriter{output_format_ == OutputType::JSONPretty};
+  FlowFileGenerator flow_file_creator{session, sqlWriter};
+  sql::SQLRowsetProcessor sqlRowsetProcessor(row_set, {sqlWriter, 
flow_file_creator});
 
   // Process rowset.
-  do {
-    rowCount = sqlRowsetProcessor.process(max_rows_ == 0 ? 
std::numeric_limits<size_t>::max() : max_rows_);
-    count++;
-    if (rowCount == 0)
-      break;
-
-    const auto output = sqlWriter.toString();
-    if (!output.empty()) {
-      WriteCallback writer(output);
-      auto newflow = session.create();
-      newflow->addAttribute(ResultRowCount, std::to_string(rowCount));
-      session.write(newflow, &writer);
-      session.transfer(newflow, s_success);
+  while (size_t row_count = sqlRowsetProcessor.process(max_rows_)) {
+    auto new_file = flow_file_creator.getLastFlowFile();
+    new_file->addAttribute(RESULT_ROW_COUNT, std::to_string(row_count));

Review comment:
       done

##########
File path: extensions/sql/processors/FlowFileSource.cpp
##########
@@ -0,0 +1,72 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "FlowFileSource.h"
+
+#include "FlowFile.h"
+#include "data/JSONSQLWriter.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace processors {
+
+const core::Property FlowFileSource::OutputFormat(
+  core::PropertyBuilder::createProperty("Output Format")
+  ->isRequired(true)
+  ->supportsExpressionLanguage(true)
+  ->withDefaultValue(toString(OutputType::JSONPretty))
+  ->withAllowableValues<std::string>(OutputType::values())
+  ->withDescription("Set the output format type.")->build());
+
+const core::Property FlowFileSource::MaxRowsPerFlowFile(
+  core::PropertyBuilder::createProperty("Max Rows Per Flow File")
+  ->isRequired(true)
+  ->supportsExpressionLanguage(true)
+  ->withDefaultValue<uint64_t>(0)
+  ->withDescription(
+      "The maximum number of result rows that will be included in a single 
FlowFile. This will allow you to break up very large result sets into multiple 
FlowFiles. "
+      "If the value specified is zero, then all rows are returned in a single 
FlowFile.")->build());
+
+const std::string FlowFileSource::FRAGMENT_IDENTIFIER = "fragment.identifier";
+const std::string FlowFileSource::FRAGMENT_COUNT = "fragment.count";
+const std::string FlowFileSource::FRAGMENT_INDEX = "fragment.index";
+
+void FlowFileSource::FlowFileGenerator::endProcessBatch(Progress progress) {
+  if (progress == Progress::DONE) {
+    // annotate the flow files with the fragment.count
+    std::string fragment_count = std::to_string(flow_files_.size());
+    for (const auto& flow_file : flow_files_) {
+      flow_file->addAttribute(FRAGMENT_COUNT, fragment_count);
+    }
+    return;
+  }

Review comment:
       done




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to