1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <odbc/ODriver.hxx>
21 #include <odbc/OTools.hxx>
22 #include <odbc/OFunctions.hxx>
23 
24 namespace connectivity::odbc
25 {
26         namespace {
27 
28         class ORealOdbcDriver : public ODBCDriver
29         {
30         protected:
31             virtual oslGenericFunction  getOdbcFunction(ODBC3SQLFunctionId _nIndex)  const override;
32             virtual SQLHANDLE   EnvironmentHandle(OUString &_rPath) override;
33         public:
ORealOdbcDriver(const css::uno::Reference<css::uno::XComponentContext> & _rxContext)34             explicit ORealOdbcDriver(const css::uno::Reference< css::uno::XComponentContext >& _rxContext) : ODBCDriver(_rxContext) {}
35         };
36 
37         }
38 
getOdbcFunction(ODBC3SQLFunctionId _nIndex) const39 oslGenericFunction ORealOdbcDriver::getOdbcFunction(ODBC3SQLFunctionId _nIndex) const
40 {
41     oslGenericFunction pFunction = nullptr;
42     switch(_nIndex)
43     {
44         case ODBC3SQLFunctionId::AllocHandle:
45             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLAllocHandle);
46             break;
47         case ODBC3SQLFunctionId::Connect:
48             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLConnect);
49             break;
50         case ODBC3SQLFunctionId::DriverConnect:
51             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLDriverConnect);
52             break;
53         case ODBC3SQLFunctionId::BrowseConnect:
54             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLBrowseConnect);
55             break;
56         case ODBC3SQLFunctionId::DataSources:
57             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLDataSources);
58             break;
59         case ODBC3SQLFunctionId::Drivers:
60             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLDrivers);
61             break;
62         case ODBC3SQLFunctionId::GetInfo:
63 
64             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLGetInfo);
65             break;
66         case ODBC3SQLFunctionId::GetFunctions:
67 
68             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLGetFunctions);
69             break;
70         case ODBC3SQLFunctionId::GetTypeInfo:
71 
72             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLGetTypeInfo);
73             break;
74         case ODBC3SQLFunctionId::SetConnectAttr:
75 
76             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLSetConnectAttr);
77             break;
78         case ODBC3SQLFunctionId::GetConnectAttr:
79 
80             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLGetConnectAttr);
81             break;
82         case ODBC3SQLFunctionId::SetEnvAttr:
83 
84             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLSetEnvAttr);
85             break;
86         case ODBC3SQLFunctionId::GetEnvAttr:
87 
88             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLGetEnvAttr);
89             break;
90         case ODBC3SQLFunctionId::SetStmtAttr:
91 
92             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLSetStmtAttr);
93             break;
94         case ODBC3SQLFunctionId::GetStmtAttr:
95 
96             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLGetStmtAttr);
97             break;
98         case ODBC3SQLFunctionId::Prepare:
99 
100             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLPrepare);
101             break;
102         case ODBC3SQLFunctionId::BindParameter:
103 
104             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLBindParameter);
105             break;
106         case ODBC3SQLFunctionId::SetCursorName:
107 
108             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLSetCursorName);
109             break;
110         case ODBC3SQLFunctionId::Execute:
111 
112             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLExecute);
113             break;
114         case ODBC3SQLFunctionId::ExecDirect:
115 
116             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLExecDirect);
117             break;
118         case ODBC3SQLFunctionId::DescribeParam:
119 
120             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLDescribeParam);
121             break;
122         case ODBC3SQLFunctionId::NumParams:
123 
124             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLNumParams);
125             break;
126         case ODBC3SQLFunctionId::ParamData:
127 
128             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLParamData);
129             break;
130         case ODBC3SQLFunctionId::PutData:
131 
132             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLPutData);
133             break;
134         case ODBC3SQLFunctionId::RowCount:
135 
136             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLRowCount);
137             break;
138         case ODBC3SQLFunctionId::NumResultCols:
139 
140             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLNumResultCols);
141             break;
142         case ODBC3SQLFunctionId::DescribeCol:
143 
144             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLDescribeCol);
145             break;
146         case ODBC3SQLFunctionId::ColAttribute:
147 
148             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLColAttribute);
149             break;
150         case ODBC3SQLFunctionId::BindCol:
151 
152             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLBindCol);
153             break;
154         case ODBC3SQLFunctionId::Fetch:
155 
156             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLFetch);
157             break;
158         case ODBC3SQLFunctionId::FetchScroll:
159 
160             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLFetchScroll);
161             break;
162         case ODBC3SQLFunctionId::GetData:
163 
164             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLGetData);
165             break;
166         case ODBC3SQLFunctionId::SetPos:
167 
168             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLSetPos);
169             break;
170         case ODBC3SQLFunctionId::BulkOperations:
171 
172             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLBulkOperations);
173             break;
174         case ODBC3SQLFunctionId::MoreResults:
175 
176             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLMoreResults);
177             break;
178         case ODBC3SQLFunctionId::GetDiagRec:
179 
180             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLGetDiagRec);
181             break;
182         case ODBC3SQLFunctionId::ColumnPrivileges:
183 
184             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLColumnPrivileges);
185             break;
186         case ODBC3SQLFunctionId::Columns:
187 
188             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLColumns);
189             break;
190         case ODBC3SQLFunctionId::ForeignKeys:
191 
192             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLForeignKeys);
193             break;
194         case ODBC3SQLFunctionId::PrimaryKeys:
195 
196             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLPrimaryKeys);
197             break;
198         case ODBC3SQLFunctionId::ProcedureColumns:
199 
200             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLProcedureColumns);
201             break;
202         case ODBC3SQLFunctionId::Procedures:
203 
204             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLProcedures);
205             break;
206         case ODBC3SQLFunctionId::SpecialColumns:
207 
208             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLSpecialColumns);
209             break;
210         case ODBC3SQLFunctionId::Statistics:
211 
212             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLStatistics);
213             break;
214         case ODBC3SQLFunctionId::TablePrivileges:
215 
216             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLTablePrivileges);
217             break;
218         case ODBC3SQLFunctionId::Tables:
219 
220             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLTables);
221             break;
222         case ODBC3SQLFunctionId::FreeStmt:
223 
224             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLFreeStmt);
225             break;
226         case ODBC3SQLFunctionId::CloseCursor:
227 
228             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLCloseCursor);
229             break;
230         case ODBC3SQLFunctionId::Cancel:
231 
232             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLCancel);
233             break;
234         case ODBC3SQLFunctionId::EndTran:
235 
236             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLEndTran);
237             break;
238         case ODBC3SQLFunctionId::Disconnect:
239 
240             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLDisconnect);
241             break;
242         case ODBC3SQLFunctionId::FreeHandle:
243 
244             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLFreeHandle);
245             break;
246         case ODBC3SQLFunctionId::GetCursorName:
247 
248             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLGetCursorName);
249             break;
250         case ODBC3SQLFunctionId::NativeSql:
251 
252             pFunction = reinterpret_cast<oslGenericFunction>(pODBC3SQLNativeSql);
253             break;
254         default:
255             OSL_FAIL("Function unknown!");
256     }
257     return pFunction;
258 }
259 
260 
261 // ODBC Environment (common for all Connections):
EnvironmentHandle(OUString & _rPath)262 SQLHANDLE ORealOdbcDriver::EnvironmentHandle(OUString &_rPath)
263 {
264     // Is (for this instance) already an Environment made?
265     if (!m_pDriverHandle)
266     {
267         SQLHANDLE h = SQL_NULL_HANDLE;
268         // allocate Environment
269 
270         // load ODBC-DLL now:
271         if (!LoadLibrary_ODBC3(_rPath) || N3SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&h) != SQL_SUCCESS)
272             return SQL_NULL_HANDLE;
273 
274         // Save in global Structure
275         m_pDriverHandle = h;
276         N3SQLSetEnvAttr(h, SQL_ATTR_ODBC_VERSION, reinterpret_cast<SQLPOINTER>(SQL_OV_ODBC3), SQL_IS_UINTEGER);
277         //N3SQLSetEnvAttr(h, SQL_ATTR_CONNECTION_POOLING,(SQLPOINTER) SQL_CP_ONE_PER_HENV, SQL_IS_INTEGER);
278     }
279 
280     return m_pDriverHandle;
281 }
282 
283 }
284 
285 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
connectivity_odbc_ORealOdbcDriver_get_implementation(css::uno::XComponentContext * context,css::uno::Sequence<css::uno::Any> const &)286 connectivity_odbc_ORealOdbcDriver_get_implementation(
287     css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
288 {
289     return cppu::acquire(new connectivity::odbc::ORealOdbcDriver(context));
290 }
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
292