Skip to content

Commit

Permalink
Merge pull request #3 from jcralmeida/implement-connection
Browse files Browse the repository at this point in the history
Implement connection
  • Loading branch information
Rafael Telles authored Nov 26, 2021
2 parents b1a7a68 + 65d2353 commit 92aac3c
Show file tree
Hide file tree
Showing 19 changed files with 991 additions and 0 deletions.
29 changes: 29 additions & 0 deletions flightsql-odbc/flightsql-odbc-clone/flightsql-odbc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
thirdparty/*.tar*
CMakeFiles/
CMakeCache.txt
CTestTestfile.cmake
Makefile
cmake_install.cmake
build/
*-build/
Testing/
build-support/boost_*

# Build directories created by Clion
cmake-build-*/

#########################################
# Editor temporary/working/backup files #
.#*
*\#*\#
[#]*#
*~
*$
*.bak
*flymake*
*.kdev4
*.log
*.swp

.idea
vcpkg_installed
24 changes: 24 additions & 0 deletions flightsql-odbc/flightsql-odbc-clone/flightsql-odbc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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.

cmake_minimum_required(VERSION 3.11)
set(CMAKE_CXX_STANDARD 11)

project(flightsql_odbc)

add_subdirectory(flight_sql)
add_subdirectory(spi)
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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.

cmake_minimum_required(VERSION 3.11)
set(CMAKE_CXX_STANDARD 11)

include_directories(${CMAKE_SOURCE_DIR}/spi)

SET(Arrow_STATIC ON)
find_package(Arrow REQUIRED)
find_package(ArrowFlight REQUIRED PATHS /usr/local/lib/cmake/arrow/)
find_package(ArrowFlightSql REQUIRED PATHS /usr/local/lib/cmake/arrow/)
find_package(GTest REQUIRED)

enable_testing()

add_library(flight_odbc_driver flight_sql_driver.cc flight_sql_connection.cc flight_sql_auth_method.cc)
target_link_libraries(flight_odbc_driver spi arrow_static arrow_flight arrow_flight_sql)
target_include_directories(flight_odbc_driver PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

# CLI
add_executable(flight_odbc_driver_cli main.cc)
target_link_libraries(flight_odbc_driver_cli flight_odbc_driver)

# Unit tests
add_executable(flight_odbc_driver_test flight_sql_connection_test.cc)
target_link_libraries(flight_odbc_driver_test flight_odbc_driver GTest::gtest GTest::gtest_main)
add_test(connection_test flight_odbc_driver_test)
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// 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 "flight_sql_auth_method.h"
#include "exceptions.h"
#include "flight_sql_connection.h"

#include <arrow/flight/client.h>
#include <arrow/result.h>

#include <utility>

using namespace driver::flight_sql;

namespace driver {
namespace flight_sql {

using arrow::Result;
using arrow::flight::FlightCallOptions;
using arrow::flight::FlightClient;
using arrow::flight::TimeoutDuration;
using driver::spi::AuthenticationException;
using driver::spi::Connection;

namespace {
class NoOpAuthMethod : public FlightSqlAuthMethod {
public:
void Authenticate(FlightSqlConnection &connection,
FlightCallOptions &call_options) override {
// Do nothing
}
};

class UserPasswordAuthMethod : public FlightSqlAuthMethod {
public:
UserPasswordAuthMethod(FlightClient &client, std::string user,
std::string password)
: client_(client), user_(std::move(user)),
password_(std::move(password)) {}

void Authenticate(FlightSqlConnection &connection,
FlightCallOptions &call_options) override {
FlightCallOptions auth_call_options;
const boost::optional<Connection::Attribute> &login_timeout =
connection.GetAttribute(Connection::LOGIN_TIMEOUT);
if (login_timeout.has_value()) {
// ODBC's LOGIN_TIMEOUT attribute and FlightCallOptions.timeout use
// seconds as time unit.
double timeout_seconds = boost::get<double>(login_timeout.value());
if (timeout_seconds > 0) {
auth_call_options.timeout = TimeoutDuration{timeout_seconds};
}
}

Result<std::pair<std::string, std::string>> bearer_result =
client_.AuthenticateBasicToken(auth_call_options, user_, password_);
if (!bearer_result.ok()) {
throw AuthenticationException(
"Failed to authenticate with user and password: " +
bearer_result.status().ToString());
}

call_options.headers.push_back(bearer_result.ValueOrDie());
}

private:
FlightClient &client_;
std::string user_;
std::string password_;
};
} // namespace

std::unique_ptr<FlightSqlAuthMethod> FlightSqlAuthMethod::FromProperties(
const std::unique_ptr<FlightClient> &client,
const std::map<std::string, Connection::Property> &properties) {

// Check if should use user-password authentication
const auto &it_user = properties.find(Connection::USER);
const auto &it_password = properties.find(Connection::PASSWORD);
if (it_user != properties.end() || it_password != properties.end()) {
const std::string &user = it_user != properties.end()
? boost::get<std::string>(it_user->second)
: "";
const std::string &password =
it_password != properties.end()
? boost::get<std::string>(it_password->second)
: "";

return std::unique_ptr<FlightSqlAuthMethod>(
new UserPasswordAuthMethod(*client, user, password));
}

return std::unique_ptr<FlightSqlAuthMethod>(new NoOpAuthMethod);
}

} // namespace flight_sql
} // namespace driver
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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.

#pragma once

#include "connection.h"
#include "flight_sql_connection.h"
#include <arrow/flight/client.h>
#include <map>
#include <memory>

namespace driver {
namespace flight_sql {

class FlightSqlAuthMethod {
public:
virtual ~FlightSqlAuthMethod() = default;

virtual void Authenticate(FlightSqlConnection &connection,
arrow::flight::FlightCallOptions &call_options) = 0;

static std::unique_ptr<FlightSqlAuthMethod> FromProperties(
const std::unique_ptr<arrow::flight::FlightClient> &client,
const std::map<std::string, spi::Connection::Property> &properties);

protected:
FlightSqlAuthMethod() = default;
};

} // namespace flight_sql
} // namespace driver
Loading

0 comments on commit 92aac3c

Please sign in to comment.