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 _CONNECTIVITY_MORK_QUERYHELPER_HXX_
21 : #define _CONNECTIVITY_MORK_QUERYHELPER_HXX_
22 :
23 : #include <sal/types.h>
24 : #include <rtl/ustring.hxx>
25 : #include <osl/mutex.hxx>
26 : #include <osl/conditn.hxx>
27 : #include <comphelper/stl_types.hxx>
28 : #include <osl/thread.hxx>
29 : #include <connectivity/FValue.hxx>
30 :
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 5 : MQueryExpressionBase( node_type _eNodeType ) : m_eNodeType( _eNodeType ) {}
70 :
71 : public:
72 : sal_Bool isUnknown( ) const { return m_eNodeType == Unknown; }
73 5 : sal_Bool isStringExpr( ) const { return m_eNodeType == StringExpr; }
74 0 : sal_Bool isExpr( ) const { return m_eNodeType == Expr; }
75 : };
76 :
77 : class MQueryExpressionString : public MQueryExpressionBase {
78 : protected:
79 : ::rtl::OUString m_aName; // LHS
80 : MQueryOp::cond_type m_aBooleanCondition;
81 : ::rtl::OUString m_aValue; // RHS
82 :
83 : public:
84 :
85 1 : MQueryExpressionString( ::rtl::OUString& lhs,
86 : MQueryOp::cond_type cond,
87 : ::rtl::OUString rhs )
88 : : MQueryExpressionBase( MQueryExpressionBase::StringExpr )
89 : , m_aName( lhs )
90 : , m_aBooleanCondition( cond )
91 1 : , m_aValue( rhs )
92 : {
93 1 : }
94 :
95 0 : MQueryExpressionString( ::rtl::OUString& lhs,
96 : MQueryOp::cond_type cond )
97 : : MQueryExpressionBase( MQueryExpressionBase::StringExpr )
98 : , m_aName( lhs )
99 : , m_aBooleanCondition( cond )
100 0 : , m_aValue( ::rtl::OUString() )
101 : {
102 0 : }
103 :
104 5 : const ::rtl::OUString& getName() const { return m_aName; }
105 25 : MQueryOp::cond_type getCond() const { return m_aBooleanCondition; }
106 5 : const ::rtl::OUString& getValue() const { return m_aValue; }
107 : };
108 :
109 4 : class MQueryExpression : public MQueryExpressionBase
110 : {
111 : friend class MQueryHelper;
112 :
113 : public:
114 : typedef ::std::vector< MQueryExpressionBase* > ExprVector;
115 :
116 : typedef enum {
117 : AND,
118 : OR
119 : } bool_cond;
120 :
121 : void setExpressions( ExprVector& _exprVector )
122 : { m_aExprVector = _exprVector; }
123 :
124 : // All expressions on a peer level use same condition operator
125 0 : void setExpressionCondition( bool_cond _cond )
126 0 : { m_aExprCondType = _cond; }
127 :
128 36 : ExprVector& getExpressions( )
129 36 : { return m_aExprVector; }
130 :
131 : // All expressions on a peer level use same condition operator
132 0 : bool_cond getExpressionCondition( ) const
133 0 : { return m_aExprCondType; }
134 :
135 4 : MQueryExpression() : MQueryExpressionBase( MQueryExpressionBase::Expr ),
136 4 : m_aExprCondType( OR )
137 4 : { m_aExprVector.clear(); }
138 :
139 :
140 : protected:
141 : ExprVector m_aExprVector;
142 : bool_cond m_aExprCondType;
143 :
144 : };
145 :
146 : class MQueryHelperResultEntry
147 : {
148 : private:
149 : typedef ::boost::unordered_map< ::rtl::OString, ::rtl::OUString, ::rtl::OStringHash > FieldMap;
150 :
151 : mutable ::osl::Mutex m_aMutex;
152 : FieldMap m_Fields;
153 :
154 : public:
155 : MQueryHelperResultEntry();
156 : ~MQueryHelperResultEntry();
157 :
158 : rtl::OUString getValue( const rtl::OString &key ) const;
159 : void setValue( const rtl::OString &key, const rtl::OUString & rValue);
160 : };
161 :
162 : class MQueryHelper
163 : {
164 : private:
165 : typedef std::vector< MQueryHelperResultEntry* > resultsArray;
166 :
167 : mutable ::osl::Mutex m_aMutex;
168 : ::osl::Condition m_aCondition;
169 : resultsArray m_aResults;
170 : sal_uInt32 m_nIndex;
171 : sal_Bool m_bHasMore;
172 : sal_Bool m_bAtEnd;
173 : void append(MQueryHelperResultEntry* resEnt );
174 : void clear_results();
175 : OColumnAlias m_rColumnAlias;
176 : ErrorDescriptor m_aError;
177 : ::rtl::OUString m_aAddressbook;
178 : MQueryExpression m_aExpr;
179 :
180 : /*
181 : void clearResultOrComplete();
182 : void notifyResultOrComplete();
183 : sal_Bool waitForResultOrComplete( );
184 : void getCardValues(nsIAbCard *card,sal_uInt32 rowIndex=0);
185 : */
186 :
187 : sal_Int32 doQueryDefaultTable(OConnection* xConnection);
188 : sal_Int32 doQueryListTable(OConnection* xConnection, rtl::OString& ouStringTable);
189 :
190 : public:
191 : MQueryHelper(const OColumnAlias& _ca);
192 : virtual ~MQueryHelper();
193 :
194 : void reset();
195 : MQueryHelperResultEntry* getByIndex( sal_uInt32 nRow );
196 : sal_Bool isError() const;
197 : sal_Bool queryComplete() const;
198 : sal_Int32 getResultCount() const;
199 : sal_Bool checkRowAvailable( sal_Int32 nDBRow );
200 : sal_Bool getRowValue( ORowSetValue& rValue, sal_Int32 nDBRow,const rtl::OUString& aDBColumnName, sal_Int32 nType );
201 : sal_Int32 executeQuery(OConnection* xConnection);
202 5 : const OColumnAlias& getColumnAlias() const { return m_rColumnAlias; }
203 2 : bool hadError() const { return m_aError.is(); }
204 0 : inline ErrorDescriptor& getError() { return m_aError; }
205 :
206 : void setAddressbook( ::rtl::OUString&);
207 : void setExpression( MQueryExpression &_expr );
208 :
209 : };
210 : }
211 : }
212 :
213 : #endif // _CONNECTIVITY_MORK_QUERYHELPER_HXX_
214 :
215 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|