xref: /core/connectivity/source/drivers/firebird/Statement.cxx (revision 4d97239b7a92c8dc1c1ee9a6474768ed1e744ef5)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 "Connection.hxx"
21 #include "ResultSet.hxx"
22 #include "Statement.hxx"
23 #include "Util.hxx"
24 
25 #include <comphelper/sequence.hxx>
26 #include <cppuhelper/queryinterface.hxx>
27 #include <sal/log.hxx>
28 
29 using namespace connectivity::firebird;
30 
31 using namespace com::sun::star;
32 using namespace com::sun::star::uno;
33 using namespace com::sun::star::lang;
34 using namespace com::sun::star::beans;
35 using namespace com::sun::star::sdbc;
36 using namespace com::sun::star::sdbcx;
37 using namespace com::sun::star::container;
38 using namespace com::sun::star::io;
39 using namespace com::sun::star::util;
40 
41 using namespace ::comphelper;
42 using namespace ::osl;
43 
44 // ---- XBatchExecution - UNSUPPORTED ----------------------------------------
addBatch(const OUString &)45 void SAL_CALL OStatement::addBatch(const OUString&)
46 {
47 }
48 
clearBatch()49 void SAL_CALL OStatement::clearBatch()
50 {
51 }
52 
executeBatch()53 Sequence< sal_Int32 > SAL_CALL OStatement::executeBatch()
54 {
55     return Sequence< sal_Int32 >();
56 }
57 
58 IMPLEMENT_SERVICE_INFO(OStatement,u"com.sun.star.sdbcx.OStatement"_ustr,u"com.sun.star.sdbc.Statement"_ustr);
59 
acquire()60 void SAL_CALL OStatement::acquire() noexcept
61 {
62     OStatementCommonBase::acquire();
63 }
64 
release()65 void SAL_CALL OStatement::release() noexcept
66 {
67     OStatementCommonBase::release();
68 }
69 
disposeResultSet()70 void OStatement::disposeResultSet()
71 {
72     MutexGuard aGuard(m_aMutex);
73     if (OStatementCommonBase_Base::rBHelper.bDisposed)
74         return;
75 
76     OStatementCommonBase::disposeResultSet();
77 
78     if (m_pSqlda)
79     {
80         freeSQLVAR(m_pSqlda);
81         free(m_pSqlda);
82         m_pSqlda = nullptr;
83     }
84 }
85 
86 // ---- XStatement -----------------------------------------------------------
executeUpdate(const OUString & sql)87 sal_Int32 SAL_CALL OStatement::executeUpdate(const OUString& sql)
88 {
89     execute(sql);
90     return getStatementChangeCount();
91 }
92 
93 
executeQuery(const OUString & sql)94 uno::Reference< XResultSet > SAL_CALL OStatement::executeQuery(const OUString& sql)
95 {
96     MutexGuard aGuard(m_aMutex);
97     checkDisposed(OStatementCommonBase_Base::rBHelper.bDisposed);
98 
99     SAL_INFO("connectivity.firebird", "executeQuery(" << sql << ")");
100 
101     ISC_STATUS aErr = 0;
102 
103     disposeResultSet();
104 
105     prepareAndDescribeStatement(sql,
106                                 m_pSqlda);
107 
108     aErr = isc_dsql_execute(m_statusVector,
109                             &m_pConnection->getTransaction(),
110                             &m_aStatementHandle,
111                             1,
112                             nullptr);
113     if (aErr)
114         SAL_WARN("connectivity.firebird", "isc_dsql_execute failed");
115 
116     m_xResultSet = new OResultSet(m_pConnection.get(),
117                                   m_aMutex,
118                                   uno::Reference< XInterface >(*this),
119                                   m_aStatementHandle,
120                                   m_pSqlda );
121 
122     // TODO: deal with cleanup
123 
124     evaluateStatusVector(m_statusVector, sql, *this);
125 
126     if (isDDLStatement())
127     {
128         m_pConnection->commit();
129     }
130 
131     return m_xResultSet;
132 }
133 
execute(const OUString & sql)134 sal_Bool SAL_CALL OStatement::execute(const OUString& sql)
135 {
136     uno::Reference< XResultSet > xResults = executeQuery(sql);
137     return xResults.is();
138     // TODO: what if we have multiple results?
139 }
140 
getConnection()141 uno::Reference< XConnection > SAL_CALL OStatement::getConnection()
142 {
143     MutexGuard aGuard(m_aMutex);
144     checkDisposed(OStatementCommonBase_Base::rBHelper.bDisposed);
145 
146     return m_pConnection;
147 }
148 
queryInterface(const Type & rType)149 Any SAL_CALL OStatement::queryInterface( const Type & rType )
150 {
151     Any aRet = OStatement_Base::queryInterface(rType);
152     if(!aRet.hasValue())
153         aRet = ::cppu::queryInterface(rType,static_cast< XBatchExecution*> (this));
154     if(!aRet.hasValue())
155         aRet = OStatementCommonBase::queryInterface(rType);
156     return aRet;
157 }
158 
getTypes()159 uno::Sequence< Type > SAL_CALL OStatement::getTypes()
160 {
161     return concatSequences(OStatement_Base::getTypes(),
162                            OStatementCommonBase::getTypes());
163 }
164 
disposing()165 void SAL_CALL OStatement::disposing()
166 {
167     disposeResultSet();
168     close();
169 }
170 
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
172