| 67 | } |
| 68 | |
| 69 | void ExecuteSQL::onTrigger(const std::shared_ptr<core::ProcessContext> &context, |
| 70 | const std::shared_ptr<core::ProcessSession> &session) { |
| 71 | auto flow_file = session->get(); |
| 72 | |
| 73 | try { |
| 74 | // Use an existing context, if one is available |
| 75 | std::shared_ptr<minifi::sqlite::SQLiteConnection> db; |
| 76 | |
| 77 | if (conn_q_.try_dequeue(db)) { |
| 78 | logger_->log_debug("Using available SQLite connection"); |
| 79 | } |
| 80 | |
| 81 | if (!db) { |
| 82 | logger_->log_info("Creating new SQLite connection"); |
| 83 | if (db_url_.substr(0, 9) == "sqlite://") { |
| 84 | db = std::make_shared<minifi::sqlite::SQLiteConnection>(db_url_.substr(9)); |
| 85 | } else { |
| 86 | std::stringstream err_msg; |
| 87 | err_msg << "Connection URL '" << db_url_ << "' is unsupported"; |
| 88 | logger_->log_error(err_msg.str().c_str()); |
| 89 | throw std::runtime_error("Connection Error"); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | auto dynamic_sql = std::make_shared<std::string>(); |
| 94 | |
| 95 | if (flow_file) { |
| 96 | if (sql_.empty()) { |
| 97 | // SQL is not defined as a property, so get SQL from the file content |
| 98 | SQLReadCallback cb(dynamic_sql); |
| 99 | session->read(flow_file, &cb); |
| 100 | } else { |
| 101 | // SQL is defined as a property, so get the property dynamically w/ EL support |
| 102 | context->getProperty(SQLStatement, *dynamic_sql, flow_file); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | auto stmt = flow_file |
| 107 | ? db->prepare(*dynamic_sql) |
| 108 | : db->prepare(sql_); |
| 109 | |
| 110 | if (flow_file) { |
| 111 | for (int i = 1; i < INT_MAX; i++) { |
| 112 | std::string val; |
| 113 | std::stringstream val_key; |
| 114 | val_key << "sql.args." << i << ".value"; |
| 115 | |
| 116 | if (!flow_file->getAttribute(val_key.str(), val)) { |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | stmt.bind_text(i, val); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | stmt.step(); |
| 125 | |
| 126 | if (!stmt.is_ok()) { |
nothing calls this directly
no test coverage detected