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 #ifndef INCLUDED_CONNECTIVITY_SOURCE_DRIVERS_MORK_MQUERYHELPER_HXX
21 #define INCLUDED_CONNECTIVITY_SOURCE_DRIVERS_MORK_MQUERYHELPER_HXX
22 
23 #include <connectivity/FValue.hxx>
24 #include "MErrorResource.hxx"
25 
26 namespace connectivity
27 {
28     namespace mork
29     {
30         class OConnection;
31         class MQueryHelper;
32         class ErrorDescriptor;
33 
34         namespace MQueryOp {
35              typedef enum {
36                  Exists         = 0,
37                  DoesNotExist   = 1,
38                  Contains       = 2,
39                  DoesNotContain = 3,
40                  Is             = 4,
41                  IsNot          = 5,
42                  BeginsWith     = 6,
43                  EndsWith       = 7,
44                  RegExp         = 8
45             } cond_type;
46         }
47 
48         class MQueryExpressionBase {
49         public:
50             enum class node_type {
51                 StringExpr,
52                 Expr
53             };
54 
55         protected:
56             node_type   m_eNodeType;
57 
MQueryExpressionBase(node_type _eNodeType)58             explicit MQueryExpressionBase( node_type _eNodeType ) : m_eNodeType( _eNodeType ) {}
59 
60         public:
~MQueryExpressionBase()61             virtual ~MQueryExpressionBase() {}
62 
isStringExpr() const63             bool   isStringExpr( ) const { return m_eNodeType == node_type::StringExpr; }
isExpr() const64             bool   isExpr( ) const { return m_eNodeType == node_type::Expr; }
65         };
66 
67         class MQueryExpressionString final : public MQueryExpressionBase {
68             OUString     m_aName;         // LHS
69             MQueryOp::cond_type m_aBooleanCondition;
70             OUString     m_aValue;        // RHS
71 
72         public:
73 
MQueryExpressionString(const OUString & lhs,MQueryOp::cond_type cond,const OUString & rhs)74             MQueryExpressionString( const OUString&     lhs,
75                                     MQueryOp::cond_type cond,
76                                     const OUString&     rhs )
77                 : MQueryExpressionBase( MQueryExpressionBase::node_type::StringExpr )
78                 , m_aName( lhs )
79                 , m_aBooleanCondition( cond )
80                 , m_aValue( rhs )
81             {
82             }
83 
MQueryExpressionString(const OUString & lhs,MQueryOp::cond_type cond)84             MQueryExpressionString( const OUString&     lhs,
85                                     MQueryOp::cond_type cond )
86                 : MQueryExpressionBase( MQueryExpressionBase::node_type::StringExpr )
87                 , m_aName( lhs )
88                 , m_aBooleanCondition( cond )
89                 , m_aValue( OUString() )
90             {
91             }
92 
getName() const93             const OUString&    getName() const { return m_aName; }
getCond() const94             MQueryOp::cond_type getCond() const { return m_aBooleanCondition; }
getValue() const95             const OUString&    getValue() const { return m_aValue; }
96         };
97 
98         class MQueryExpression final : public MQueryExpressionBase
99         {
100             friend class MQueryHelper;
101 
102         public:
103             typedef std::vector< MQueryExpressionBase* > ExprVector;
104 
105             typedef enum {
106                 AND,
107                 OR
108             } bool_cond;
109 
110             // All expressions on a peer level use same condition operator
setExpressionCondition(bool_cond _cond)111             void setExpressionCondition( bool_cond _cond )
112                             { m_aExprCondType = _cond; }
113 
addExpression(MQueryExpressionBase * expr)114             void addExpression(MQueryExpressionBase * expr)
115                             { m_aExprVector.push_back(expr); }
116 
getExpressions() const117             ExprVector const & getExpressions( ) const
118                             { return m_aExprVector; }
119 
120             // All expressions on a peer level use same condition operator
getExpressionCondition() const121             bool_cond getExpressionCondition( ) const
122                             { return m_aExprCondType; }
123 
MQueryExpression()124             MQueryExpression() : MQueryExpressionBase( MQueryExpressionBase::node_type::Expr ),
125                                  m_aExprCondType( OR )
126                             {}
127 
128         private:
129             ExprVector          m_aExprVector;
130             bool_cond           m_aExprCondType;
131 
132            MQueryExpression(const MQueryExpression&) = delete;
133            MQueryExpression& operator=(const MQueryExpression&) = delete;
134         };
135 
136         class MQueryHelperResultEntry
137         {
138         private:
139             typedef std::unordered_map< OString, OUString >  FieldMap;
140 
141             FieldMap                m_Fields;
142 
143         public:
144             MQueryHelperResultEntry();
145             ~MQueryHelperResultEntry();
146 
147             OUString   getValue( const OString &key ) const;
148             void            setValue( const OString &key, const OUString & rValue);
149         };
150 
151         class MQueryHelper final
152         {
153         private:
154             typedef std::vector< std::unique_ptr<MQueryHelperResultEntry> > resultsArray;
155 
156             mutable ::osl::Mutex        m_aMutex;
157             resultsArray        m_aResults;
158             void            append(std::unique_ptr<MQueryHelperResultEntry> resEnt );
159             void            clear_results();
160             OColumnAlias        m_rColumnAlias;
161             ErrorDescriptor     m_aError;
162             OUString     m_aAddressbook;
163 
164         public:
165             explicit                   MQueryHelper(const OColumnAlias& _ca);
166                                        ~MQueryHelper();
167 
168             void                       reset();
169             MQueryHelperResultEntry*   getByIndex( sal_uInt32 nRow );
170             sal_Int32                  getResultCount() const;
171             bool                       getRowValue( ORowSetValue& rValue, sal_Int32 nDBRow,const OUString& aDBColumnName, sal_Int32 nType );
172             sal_Int32                  executeQuery(OConnection* xConnection, MQueryExpression & expr);
getColumnAlias() const173             const OColumnAlias&        getColumnAlias() const { return m_rColumnAlias; }
hadError() const174             bool                       hadError() const { return m_aError.is(); }
getError()175             ErrorDescriptor&    getError() { return m_aError; }
176 
177             void                       setAddressbook( OUString const &);
178         };
179     }
180 }
181 
182 #endif // INCLUDED_CONNECTIVITY_SOURCE_DRIVERS_MORK_MQUERYHELPER_HXX
183 
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
185