Line data Source code
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 <sal/types.h>
24 : #include <rtl/ustring.hxx>
25 : #include <osl/mutex.hxx>
26 : #include <osl/conditn.hxx>
27 : #include <osl/thread.hxx>
28 : #include <connectivity/FValue.hxx>
29 :
30 : #include <boost/noncopyable.hpp>
31 : #include <boost/unordered_map.hpp>
32 :
33 : #include "MErrorResource.hxx"
34 :
35 : namespace connectivity
36 : {
37 : namespace mork
38 : {
39 : class OConnection;
40 : class MQueryHelper;
41 : class ErrorDescriptor;
42 :
43 : namespace MQueryOp {
44 : typedef enum {
45 : Exists = 0,
46 : DoesNotExist = 1,
47 : Contains = 2,
48 : DoesNotContain = 3,
49 : Is = 4,
50 : IsNot = 5,
51 : BeginsWith = 6,
52 : EndsWith = 7,
53 : RegExp = 8
54 : } cond_type;
55 : }
56 :
57 : class MQueryExpressionBase {
58 : public:
59 : typedef enum {
60 : Unknown,
61 : StringExpr,
62 : Expr
63 : } node_type;
64 :
65 : protected:
66 : node_type m_eNodeType;
67 :
68 : MQueryExpressionBase() : m_eNodeType( Unknown ) {}
69 6 : MQueryExpressionBase( node_type _eNodeType ) : m_eNodeType( _eNodeType ) {}
70 :
71 : public:
72 6 : virtual ~MQueryExpressionBase() {}
73 :
74 : bool isUnknown( ) const { return m_eNodeType == Unknown; }
75 10 : bool isStringExpr( ) const { return m_eNodeType == StringExpr; }
76 0 : bool isExpr( ) const { return m_eNodeType == Expr; }
77 : };
78 :
79 4 : class MQueryExpressionString : public MQueryExpressionBase {
80 : protected:
81 : OUString m_aName; // LHS
82 : MQueryOp::cond_type m_aBooleanCondition;
83 : OUString m_aValue; // RHS
84 :
85 : public:
86 :
87 2 : MQueryExpressionString( const OUString& lhs,
88 : MQueryOp::cond_type cond,
89 : const OUString& rhs )
90 : : MQueryExpressionBase( MQueryExpressionBase::StringExpr )
91 : , m_aName( lhs )
92 : , m_aBooleanCondition( cond )
93 2 : , m_aValue( rhs )
94 : {
95 2 : }
96 :
97 0 : MQueryExpressionString( const OUString& lhs,
98 : MQueryOp::cond_type cond )
99 : : MQueryExpressionBase( MQueryExpressionBase::StringExpr )
100 : , m_aName( lhs )
101 : , m_aBooleanCondition( cond )
102 0 : , m_aValue( OUString() )
103 : {
104 0 : }
105 :
106 10 : const OUString& getName() const { return m_aName; }
107 50 : MQueryOp::cond_type getCond() const { return m_aBooleanCondition; }
108 10 : const OUString& getValue() const { return m_aValue; }
109 : };
110 :
111 : class MQueryExpression : public MQueryExpressionBase, private boost::noncopyable
112 : {
113 : friend class MQueryHelper;
114 :
115 : public:
116 : typedef ::std::vector< MQueryExpressionBase* > ExprVector;
117 :
118 : typedef enum {
119 : AND,
120 : OR
121 : } bool_cond;
122 :
123 : // All expressions on a peer level use same condition operator
124 0 : void setExpressionCondition( bool_cond _cond )
125 0 : { m_aExprCondType = _cond; }
126 :
127 2 : void addExpression(MQueryExpressionBase * expr)
128 2 : { m_aExprVector.push_back(expr); }
129 :
130 70 : ExprVector const & getExpressions( ) const
131 70 : { return m_aExprVector; }
132 :
133 : // All expressions on a peer level use same condition operator
134 0 : bool_cond getExpressionCondition( ) const
135 0 : { return m_aExprCondType; }
136 :
137 4 : MQueryExpression() : MQueryExpressionBase( MQueryExpressionBase::Expr ),
138 4 : m_aExprCondType( OR )
139 4 : {}
140 :
141 8 : virtual ~MQueryExpression() {
142 18 : for (ExprVector::iterator i(m_aExprVector.begin());
143 12 : i != m_aExprVector.end(); ++i)
144 : {
145 2 : delete *i;
146 : }
147 4 : }
148 :
149 : protected:
150 : ExprVector m_aExprVector;
151 : bool_cond m_aExprCondType;
152 :
153 : };
154 :
155 : class MQueryHelperResultEntry
156 : {
157 : private:
158 : typedef ::boost::unordered_map< OString, OUString, OStringHash > FieldMap;
159 :
160 : FieldMap m_Fields;
161 :
162 : public:
163 : MQueryHelperResultEntry();
164 : ~MQueryHelperResultEntry();
165 :
166 : OUString getValue( const OString &key ) const;
167 : void setValue( const OString &key, const OUString & rValue);
168 : };
169 :
170 : class MQueryHelper
171 : {
172 : private:
173 : typedef std::vector< MQueryHelperResultEntry* > resultsArray;
174 :
175 : mutable ::osl::Mutex m_aMutex;
176 : ::osl::Condition m_aCondition;
177 : resultsArray m_aResults;
178 : sal_uInt32 m_nIndex;
179 : bool m_bHasMore;
180 : bool m_bAtEnd;
181 : void append(MQueryHelperResultEntry* resEnt );
182 : void clear_results();
183 : OColumnAlias m_rColumnAlias;
184 : ErrorDescriptor m_aError;
185 : OUString m_aAddressbook;
186 :
187 : /*
188 : void clearResultOrComplete();
189 : void notifyResultOrComplete();
190 : sal_Bool waitForResultOrComplete( );
191 : void getCardValues(nsIAbCard *card,sal_uInt32 rowIndex=0);
192 : */
193 :
194 : sal_Int32 doQueryDefaultTable(OConnection* xConnection);
195 : sal_Int32 doQueryListTable(OConnection* xConnection, OString& ouStringTable);
196 :
197 : public:
198 : MQueryHelper(const OColumnAlias& _ca);
199 : virtual ~MQueryHelper();
200 :
201 : void reset();
202 : MQueryHelperResultEntry* getByIndex( sal_uInt32 nRow );
203 : bool isError() const;
204 : bool queryComplete() const;
205 : sal_Int32 getResultCount() const;
206 : bool checkRowAvailable( sal_Int32 nDBRow );
207 : bool getRowValue( ORowSetValue& rValue, sal_Int32 nDBRow,const OUString& aDBColumnName, sal_Int32 nType );
208 : sal_Int32 executeQuery(OConnection* xConnection, MQueryExpression & expr);
209 10 : const OColumnAlias& getColumnAlias() const { return m_rColumnAlias; }
210 4 : bool hadError() const { return m_aError.is(); }
211 0 : inline ErrorDescriptor& getError() { return m_aError; }
212 :
213 : void setAddressbook( OUString&);
214 : };
215 : }
216 : }
217 :
218 : #endif // INCLUDED_CONNECTIVITY_SOURCE_DRIVERS_MORK_MQUERYHELPER_HXX
219 :
220 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|