1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef CONNECTIVITY_ROWFUNCTIONPARSER_HXX_INCLUDED
29 #define CONNECTIVITY_ROWFUNCTIONPARSER_HXX_INCLUDED
30 
31 #include <sal/config.h>
32 #ifndef BOOST_SHARED_PTR_HPP_INCLUDED
33 #include <boost/shared_ptr.hpp>
34 #endif
35 #include "FDatabaseMetaDataResultSet.hxx"
36 #include <vector>
37 #include "connectivity/dbtoolsdllapi.hxx"
38 
39 // -------------------------------------------------------------------------
40 namespace connectivity
41 {
42 // -------------------------------------------------------------------------
43 struct OOO_DLLPUBLIC_DBTOOLS RowEquation
44 {
45 	sal_Int32	nOperation;
46 	ORowSetValueDecoratorRef	nPara[ 3 ];
47 
48 	RowEquation() :
49 		nOperation	( 0 )
50 		{
51 		}
52 };
53 
54 enum ExpressionFunct
55 {
56 	FUNC_CONST,
57 
58 	ENUM_FUNC_EQUATION,
59 
60     UNARY_FUNC_COLUMN,
61     ENUM_FUNC_AND,
62     ENUM_FUNC_OR
63 };
64 
65 #define EXPRESSION_FLAG_SUMANGLE_MODE 1
66 
67 class OOO_DLLPUBLIC_DBTOOLS SAL_NO_VTABLE ExpressionNode
68 {
69 public:
70     virtual ~ExpressionNode(){}
71 
72     /** Operator to calculate function value.
73 
74         This method calculates the function value.
75 	*/
76     virtual ORowSetValueDecoratorRef evaluate(const ODatabaseMetaDataResultSet::ORow& _aRow ) const = 0;
77 
78     virtual void fill(const ODatabaseMetaDataResultSet::ORow& _aRow ) const = 0;
79 
80 	/** Operator to retrieve the type of expression node
81 	*/
82 	virtual ExpressionFunct getType() const = 0;
83 
84 	/** Operator to retrieve the ms version of expression
85 	*/
86 	virtual ODatabaseMetaDataResultSet::ORow fillNode(
87 		std::vector< RowEquation >& rEquations, ExpressionNode* pOptionalArg, sal_uInt32 nFlags ) = 0;
88 };
89 typedef ::boost::shared_ptr< ExpressionNode > ExpressionNodeSharedPtr;
90 
91 /** This exception is thrown, when the arithmetic expression
92     parser failed to parse a string.
93     */
94 struct OOO_DLLPUBLIC_DBTOOLS ParseError
95 {
96     ParseError() {}
97     ParseError( const char* ) {}
98 };
99 
100 class OOO_DLLPUBLIC_DBTOOLS FunctionParser
101 {
102 public:
103 
104     /** Parse a string
105 
106         The following grammar is accepted by this method:
107         <code>
108 
109 		number_digit = '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'
110 
111 		number = number number_digit | number_digit
112 
113 		equal_function = '='
114 		ternary_function = 'if'
115 
116 		string_reference = 'a-z,A-Z,0-9' ' '
117 		modifier_reference = '$' '0-9' ' '
118 
119 		basic_expression =
120 			number |
121             string_reference |
122             additive_expression equal_function additive_expression |
123             unary_function '(' number_digit ')'
124 			ternary_function '(' additive_expression ',' additive_expression ',
125 					           ' additive_expression ')' | '(' additive_expression ')'
126 
127         </code>
128 
129         @param rFunction
130         The string to parse
131 
132         @throws ParseError if an invalid expression is given.
133 
134         @return the generated function object.
135        */
136 
137     static ExpressionNodeSharedPtr parseFunction( const ::rtl::OUString& _sFunction);
138 
139 private:
140     // disabled constructor/destructor, since this is
141     // supposed to be a singleton
142     FunctionParser();
143 
144     // default: disabled copy/assignment
145     FunctionParser(const FunctionParser&);
146     FunctionParser& operator=( const FunctionParser& );
147 };
148 
149 // -------------------------------------------------------------------------
150 } // namespace connectivity
151 // -------------------------------------------------------------------------
152 #endif /* CONNECTIVITY_ROWFUNCTIONPARSER_HXX_INCLUDED */
153