You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2019/12/04 12:13:09 UTC

[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #656: MINIFI-1013 Used soci library.

arpadboda commented on a change in pull request #656: MINIFI-1013 Used soci library.
URL: https://github.com/apache/nifi-minifi-cpp/pull/656#discussion_r353708062
 
 

 ##########
 File path: extensions/sql/processors/PutSQL.cpp
 ##########
 @@ -0,0 +1,134 @@
+/**
+ * @file PutSQL.cpp
+ * PutSQL class declaration
+ *
+ * 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 "PutSQL.h"
+
+#include <vector>
+#include <queue>
+#include <map>
+#include <set>
+#include <sstream>
+#include <stdio.h>
+#include <string>
+#include <iostream>
+#include <memory>
+#include <codecvt>
+#include <soci.h>
+
+#include "io/DataStream.h"
+#include "core/ProcessContext.h"
+#include "core/ProcessSession.h"
+#include "Exception.h"
+#include "utils/OsUtils.h"
+#include "data/DatabaseConnectors.h"
+#include "data/JSONSQLWriter.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace processors {
+
+const std::string PutSQL::ProcessorName("PutSQL");
+
+static core::Property DBCControllerService(
+    core::PropertyBuilder::createProperty("DB Controller Service")->isRequired(true)->withDescription("Database Controller Service.")->supportsExpressionLanguage(true)->build());
+
+static core::Property s_SQLStatements(
+    core::PropertyBuilder::createProperty("SQL statements")->isRequired(true)->withDefaultValue("System")->withDescription(
+        "A semicolon-delimited list of SQL statements to execute. The statement 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 statements, to be issued by the processor to the database.")
+        ->supportsExpressionLanguage(true)->build());
+
+core::Relationship PutSQL::Success("success", "Database is successfully updated.");
+
+PutSQL::PutSQL(const std::string& name, utils::Identifier uuid)
+    : core::Processor(name, uuid), logger_(logging::LoggerFactory<PutSQL>::getLogger()) {
+}
+
+PutSQL::~PutSQL() {
+}
+
+void PutSQL::initialize() {
+  //! Set the supported properties
+  setSupportedProperties( { DBCControllerService, s_SQLStatements });
+
+  //! Set the supported relationships
+  setSupportedRelationships( { Success });
+}
+
+void PutSQL::onSchedule(const std::shared_ptr<core::ProcessContext> &context, const std::shared_ptr<core::ProcessSessionFactory> &sessionFactory) {
+  context->getProperty(DBCControllerService.getName(), db_controller_service_);
+
+  std::string sqlStatements;
+  context->getProperty(s_SQLStatements.getName(), sqlStatements);
+
+  // SQL statements separated by ';'.
+  std::stringstream strStream(sqlStatements);
+  std::string sqlStatement;
+  while (getline(strStream, sqlStatement, ';')) {
+    sqlStatements_.push_back(sqlStatement);
+  }
+
+  database_service_ = std::dynamic_pointer_cast<sql::controllers::DatabaseService>(context->getControllerService(db_controller_service_));
+  if (database_service_ == nullptr) {
+    logger_->log_error("'DB Controller Service' must be defined");
+  } else {
+    onScheduleOK_ = true;
+  }
+}
+
+void PutSQL::onTrigger(const std::shared_ptr<core::ProcessContext> &context, const std::shared_ptr<core::ProcessSession> &session) {
+  if (!onScheduleOK_) {
+    logger_->log_error("'DB Controller Service' must be defined, 'onTrigger' is not processed.");
+    return;
+  }
+
+  if (database_service_) {
+    std::unique_ptr<sql::Connection> connection = database_service_->getConnection();
+    if (connection) {
+      const auto dbSession = connection->getSession();
+
+      try {
 
 Review comment:
   What are we supposed to do here?
   Given the name (PutSQL) I would expect content of flowfiles to be put to the DB.
   Although I don't see any sign of that in the code, we only execute the statements provided by the user. 
   
   Have a success relationship hardly makes sense, but I think we should have a failure and work as:
   -Send to success in case we could properly execute the put.
   -Send to failure in case SQL operation failed. (Optional rollback here)
   
   Please take a look at PutSQL in NiFi:
   https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-standard-nar/1.6.0/org.apache.nifi.processors.standard.PutSQL/
   
   Especially the flowfile-dependent attributes it reads. 
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services