| 40 | void LoadPropertiesFromDSN(const std::string& dsn, Connection::ConnPropertyMap& props); |
| 41 | |
| 42 | SQLRETURN SQLAllocHandle(SQLSMALLINT type, SQLHANDLE parent, SQLHANDLE* result) { |
| 43 | ARROW_LOG(DEBUG) << "SQLAllocHandle called with type: " << type |
| 44 | << ", parent: " << parent |
| 45 | << ", result: " << static_cast<const void*>(result); |
| 46 | *result = nullptr; |
| 47 | switch (type) { |
| 48 | case SQL_HANDLE_ENV: { |
| 49 | using ODBC::ODBCEnvironment; |
| 50 | |
| 51 | *result = SQL_NULL_HENV; |
| 52 | |
| 53 | try { |
| 54 | static std::shared_ptr<FlightSqlDriver> odbc_driver = |
| 55 | std::make_shared<FlightSqlDriver>(); |
| 56 | *result = reinterpret_cast<SQLHENV>(new ODBCEnvironment(odbc_driver)); |
| 57 | |
| 58 | return SQL_SUCCESS; |
| 59 | } catch (const std::bad_alloc&) { |
| 60 | // allocating environment failed so cannot log diagnostic error here |
| 61 | return SQL_ERROR; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | case SQL_HANDLE_DBC: { |
| 66 | using ODBC::ODBCConnection; |
| 67 | using ODBC::ODBCEnvironment; |
| 68 | |
| 69 | *result = SQL_NULL_HDBC; |
| 70 | |
| 71 | ODBCEnvironment* environment = reinterpret_cast<ODBCEnvironment*>(parent); |
| 72 | |
| 73 | return ODBCEnvironment::ExecuteWithDiagnostics(environment, SQL_ERROR, [=]() { |
| 74 | std::shared_ptr<ODBCConnection> conn = environment->CreateConnection(); |
| 75 | |
| 76 | if (conn) { |
| 77 | // Inside `CreateConnection`, the shared_ptr `conn` is kept |
| 78 | // in a `std::vector` of connections inside the environment handle. |
| 79 | // As long as the parent environment handle is alive, the connection shared_ptr |
| 80 | // will be kept alive unless the user frees the connection. |
| 81 | *result = reinterpret_cast<SQLHDBC>(conn.get()); |
| 82 | |
| 83 | return SQL_SUCCESS; |
| 84 | } |
| 85 | |
| 86 | return SQL_ERROR; |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | case SQL_HANDLE_STMT: { |
| 91 | using ODBC::ODBCConnection; |
| 92 | using ODBC::ODBCStatement; |
| 93 | |
| 94 | *result = SQL_NULL_HSTMT; |
| 95 | |
| 96 | ODBCConnection* connection = reinterpret_cast<ODBCConnection*>(parent); |
| 97 | |
| 98 | return ODBCConnection::ExecuteWithDiagnostics(connection, SQL_ERROR, [=]() { |
| 99 | std::shared_ptr<ODBCStatement> statement = connection->CreateStatement(); |