cid#1640468 Dereference after null check
[LibreOffice.git] / connectivity / source / drivers / evoab2 / NStatement.hxx
blob02bed5ea318b43999177d9933baca4f79840734e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #pragma once
22 #include <com/sun/star/sdbc/XStatement.hpp>
23 #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
24 #include <com/sun/star/sdbc/XMultipleResults.hpp>
25 #include <com/sun/star/sdbc/XCloseable.hpp>
26 #include <com/sun/star/sdbc/SQLWarning.hpp>
27 #include <comphelper/proparrhlp.hxx>
28 #include <cppuhelper/implbase2.hxx>
29 #include <cppuhelper/basemutex.hxx>
30 #include <comphelper/uno3.hxx>
31 #include <connectivity/CommonTools.hxx>
32 #include <com/sun/star/lang/XServiceInfo.hpp>
33 #include <connectivity/sqliterator.hxx>
34 #include <connectivity/sqlparse.hxx>
35 #include <connectivity/FValue.hxx>
36 #include <com/sun/star/util/XCancellable.hpp>
37 #include <cppuhelper/compbase.hxx>
38 #include <comphelper/propertycontainer.hxx>
39 #include <unotools/weakref.hxx>
41 #include "EApi.h"
42 #include "NConnection.hxx"
44 #include <vector>
46 namespace connectivity::evoab
48 class OEvoabResultSet;
50 typedef ::cppu::WeakComponentImplHelper< css::sdbc::XWarningsSupplier
51 , css::sdbc::XCloseable
52 > OCommonStatement_IBase;
54 struct FieldSort
56 sal_Int32 nField;
57 bool bAscending;
59 FieldSort( const sal_Int32 _nField, const bool _bAscending ) : nField( _nField ), bAscending( _bAscending ) { }
61 typedef std::vector< FieldSort > SortDescriptor;
63 enum QueryFilterType
65 eFilterAlwaysFalse,
66 eFilterNone,
67 eFilterOther
70 class EBookQueryWrapper
72 private:
73 EBookQuery* mpQuery;
74 public:
75 EBookQueryWrapper()
76 : mpQuery(nullptr)
79 EBookQueryWrapper(const EBookQueryWrapper& rhs)
80 : mpQuery(rhs.mpQuery)
82 if (mpQuery)
83 e_book_query_ref(mpQuery);
85 EBookQueryWrapper(EBookQueryWrapper&& rhs) noexcept
86 : mpQuery(rhs.mpQuery)
88 rhs.mpQuery = nullptr;
90 void reset(EBookQuery* pQuery)
92 if (mpQuery)
93 e_book_query_unref(mpQuery);
94 mpQuery = pQuery;
95 if (mpQuery)
96 e_book_query_ref(mpQuery);
98 EBookQueryWrapper& operator=(const EBookQueryWrapper& rhs)
100 if (this != &rhs)
101 reset(rhs.mpQuery);
102 return *this;
104 EBookQueryWrapper& operator=(EBookQueryWrapper&& rhs)
106 if (mpQuery)
107 e_book_query_unref(mpQuery);
108 mpQuery = rhs.mpQuery;
109 rhs.mpQuery = nullptr;
110 return *this;
112 ~EBookQueryWrapper()
114 if (mpQuery)
115 e_book_query_unref(mpQuery);
117 EBookQuery* getQuery() const
119 return mpQuery;
123 struct QueryData
125 private:
126 EBookQueryWrapper aQuery;
128 public:
129 OUString sTable;
130 QueryFilterType eFilterType;
131 rtl::Reference<connectivity::OSQLColumns> xSelectColumns;
132 SortDescriptor aSortOrder;
134 QueryData()
135 : sTable()
136 , eFilterType( eFilterOther )
137 , xSelectColumns()
138 , aSortOrder()
142 EBookQuery* getQuery() const { return aQuery.getQuery(); }
143 void setQuery(EBookQuery* pQuery) { aQuery.reset(pQuery); }
146 //************ Class: OCommonStatement
147 // is a base class for the normal statement and for the prepared statement
149 class OCommonStatement :public cppu::BaseMutex
150 ,public OCommonStatement_IBase
151 ,public ::comphelper::OPropertyContainer
152 ,public ::comphelper::OPropertyArrayUsageHelper< OCommonStatement >
154 private:
155 unotools::WeakReference< OEvoabResultSet> m_xResultSet; // The last ResultSet created
156 rtl::Reference<OEvoabConnection> m_xConnection;
157 connectivity::OSQLParser m_aParser;
158 connectivity::OSQLParseTreeIterator m_aSQLIterator;
159 connectivity::OSQLParseNode *m_pParseTree;
161 // <properties>
162 OUString m_aCursorName;
163 sal_Int32 m_nMaxFieldSize;
164 sal_Int32 m_nMaxRows;
165 sal_Int32 m_nQueryTimeOut;
166 sal_Int32 m_nFetchSize;
167 sal_Int32 m_nResultSetType;
168 sal_Int32 m_nFetchDirection;
169 sal_Int32 m_nResultSetConcurrency;
170 bool m_bEscapeProcessing;
171 // </properties>
173 protected:
175 void disposeResultSet();
177 // OPropertyArrayUsageHelper
178 virtual ::cppu::IPropertyArrayHelper* createArrayHelper() const override;
179 // OPropertySetHelper
180 virtual ::cppu::IPropertyArrayHelper & SAL_CALL getInfoHelper() override;
182 virtual ~OCommonStatement() override;
184 protected:
185 void parseSql( const OUString& sql, QueryData& _out_rQueryData );
186 EBookQuery *whereAnalysis( const OSQLParseNode* parseTree );
187 void orderByAnalysis( const OSQLParseNode* _pOrderByClause, SortDescriptor& _out_rSort );
188 OUString getTableName() const;
190 public:
192 // other methods
193 OEvoabConnection* getOwnConnection() const { return m_xConnection.get(); }
195 using OCommonStatement_IBase::operator css::uno::Reference< css::uno::XInterface >;
197 protected:
198 explicit OCommonStatement( OEvoabConnection* _pConnection );
200 // OComponentHelper
201 virtual void SAL_CALL disposing() override;
202 // XInterface
203 virtual void SAL_CALL release() noexcept override;
204 virtual void SAL_CALL acquire() noexcept override;
205 // XInterface
206 virtual css::uno::Any SAL_CALL queryInterface( const css::uno::Type & rType ) override;
207 //XTypeProvider
208 virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes( ) override;
210 // XPropertySet
211 virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo( ) override;
213 // XWarningsSupplier
214 virtual css::uno::Any SAL_CALL getWarnings( ) override;
215 virtual void SAL_CALL clearWarnings( ) override;
217 // XCloseable
218 virtual void SAL_CALL close( ) override;
220 protected:
221 /** will return the EBookQuery representing the statement WHERE condition, or throw
223 Also, all statement dependent members (such as the parser/iterator) will be inited afterwards.
225 QueryData
226 impl_getEBookQuery_throw( const OUString& _rSql );
228 css::uno::Reference< css::sdbc::XResultSet >
229 impl_executeQuery_throw( const OUString& _rSql );
231 css::uno::Reference< css::sdbc::XResultSet >
232 impl_executeQuery_throw( const QueryData& _rData );
234 css::uno::Reference< css::sdbc::XConnection >
235 impl_getConnection() { return css::uno::Reference< css::sdbc::XConnection >( m_xConnection ); }
237 OUString
238 impl_getColumnRefColumnName_throw( const ::connectivity::OSQLParseNode& _rColumnRef );
241 typedef ::cppu::ImplHelper2 < css::lang::XServiceInfo
242 , css::sdbc::XStatement
243 > OStatement_IBase;
244 class OStatement :public OCommonStatement
245 ,public OStatement_IBase
247 protected:
248 virtual ~OStatement() override {}
250 public:
251 explicit OStatement( OEvoabConnection* _pConnection)
252 :OCommonStatement( _pConnection)
256 // XInterface
257 virtual css::uno::Any SAL_CALL queryInterface( const css::uno::Type & rType ) override;
258 virtual void SAL_CALL acquire() noexcept override;
259 virtual void SAL_CALL release() noexcept override;
261 // XTypeProvider
262 DECLARE_XTYPEPROVIDER()
264 // XServiceInfo
265 DECLARE_SERVICE_INFO();
267 // XStatement
268 virtual css::uno::Reference< css::sdbc::XResultSet > SAL_CALL executeQuery( const OUString& sql ) override ;
269 virtual sal_Int32 SAL_CALL executeUpdate( const OUString& sql ) override ;
270 virtual sal_Bool SAL_CALL execute( const OUString& sql ) override ;
271 virtual css::uno::Reference< css::sdbc::XConnection > SAL_CALL getConnection( ) override ;
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */