void sql_database::async_send_query(boost::asio::io_service::work &, sql_callback cb, const query_list & sql) { auto & c = get_free_connection(); auto guard = make_guard(std::bind(&std::mutex::unlock, &c.lock)); std::string error; auto qres = std::make_shared<query_result>(send_query(c, sql, error)); _main_service.post(std::bind(cb, std::move(qres), error)); }
query_result sql_database::query(const query_list & sql) { if (sql.empty()) return { }; auto & c = get_free_connection(); auto guard = make_guard(std::bind(&std::mutex::unlock, &c.lock)); std::string error; auto result = send_query(c, sql, error); if (!error.empty()) throw sql_error { error.c_str() }; return result; }
void sql_database::load_queries(const std::unordered_map<std::string, std::string> & queries) { auto & c = get_free_connection(); auto guard = make_guard(std::bind(&std::mutex::unlock, &c.lock)); for(auto && it : queries) { auto res = PQprepare(c.conn, it.first.c_str(), it.second.c_str(), 0, nullptr); auto res_guard = make_guard(std::bind(PQclear, res)); auto status = PQresultStatus(res); if (status == PGRES_FATAL_ERROR) { throw sql_error { "sql query failed due to "_s + PQresultErrorMessage(res) + ", query: " + it.first }; } } }
/**@brief Function for registering a new connection instance. * * @param[in] conn_handle The handle of the new connection. * @param[in] p_ble_addr The address used to connect. * * @return Either the index of the new connection in the array or IM_NO_INVALID_CONN_HANDLES if no * free position exists. */ uint8_t new_connection(uint16_t conn_handle, ble_gap_addr_t * p_ble_addr) { uint8_t conn_index = IM_NO_INVALID_CONN_HANDLES; if ((p_ble_addr != NULL) && (conn_handle != BLE_CONN_HANDLE_INVALID)) { ble_conn_state_user_flag_set(conn_handle, m_conn_state_user_flag_id, true); conn_index = get_connection_by_conn_handle(conn_handle); if (conn_index == IM_NO_INVALID_CONN_HANDLES) { conn_index = get_free_connection(); } if (conn_index != IM_NO_INVALID_CONN_HANDLES) { m_connections[conn_index].conn_handle = conn_handle; m_connections[conn_index].peer_id = PM_PEER_ID_INVALID; m_connections[conn_index].peer_address = *p_ble_addr; } } return conn_index; }
static ConnCacheEntry * new_connection (int32_t cell, uint32_t host, uint16_t port, uint16_t service, nnpfs_pag_t cred, int securityindex, int (*probe)(struct rx_connection *), struct rx_securityClass *securityobject) { ConnCacheEntry *e; assert (probe != NULL); e = get_free_connection (); e->cell = cell; e->host = host; e->port = port; e->service = service; e->flags.alivep = TRUE; e->flags.old = FALSE; e->refcount = 0; e->cred = cred; e->securityindex = securityindex; e->probe = probe; e->connection = rx_NewConnection (host, htons (port), service, securityobject, securityindex); if (e->connection == NULL) arla_errx (1, ADEBERROR, "rx_NewConnection failed"); return e; }