1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef CONNECTIVITY_PREDICATEINPUT_HXX
25 #define CONNECTIVITY_PREDICATEINPUT_HXX
26 
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/sdbc/XConnection.hpp>
30 #include <com/sun/star/util/XNumberFormatter.hpp>
31 #include <com/sun/star/i18n/XLocaleData.hpp>
32 #include <connectivity/sqlparse.hxx>
33 #include "connectivity/dbtoolsdllapi.hxx"
34 
35 //.........................................................................
36 namespace dbtools
37 {
38 //.........................................................................
39 
40 	//=====================================================================
41 	//= OPredicateInputController
42 	//=====================================================================
43 	/** A class which allows input of an SQL predicate for a row set column
44 		into a edit field.
45 	*/
46 	class OOO_DLLPUBLIC_DBTOOLS OPredicateInputController
47 	{
48 	private:
49 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >
50 				m_xORB;
51 		::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >
52 				m_xConnection;
53 		::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter >
54 				m_xFormatter;
55 		::com::sun::star::uno::Reference< ::com::sun::star::i18n::XLocaleData >
56 				m_xLocaleData;
57 
58 		::connectivity::OSQLParser
59 				m_aParser;
60 
61 	public:
62 		OPredicateInputController(
63 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB,
64 			const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection,
65 			const ::connectivity::IParseContext* _pParseContext = NULL
66 		);
67 
68 		/** transforms a "raw" predicate value (usually obtained from a user input) into a valid predicate for the given column
69 		@param _rPredicateValue
70 			The text to normalize.
71 		@param _rxField
72 			The field for which the text should be a predicate value.
73 		@param _pErrorMessage
74 			If not <NULL/>, and a parsing error occurs, the error message will be copied to the string the argument
75 			points to.
76 		*/
77 		sal_Bool		normalizePredicateString(
78 			::rtl::OUString& _rPredicateValue,
79 			const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxField,
80 			::rtl::OUString* _pErrorMessage = NULL
81 		) const;
82 
83 		/** get's a value of the predicate which can be used in a WHERE clause.
84 		@param _rPredicateValue
85 			the value which has been normalized using normalizePredicateString
86 		@param _rxField
87 			is the field for which a predicate is to be entered
88 		@param _bForStatementUse
89 			If <TRUE/>, the returned value can be used in an SQL statement. If <FALSE/>, it can be used
90 			for instance for setting parameter values.
91 		@param _pErrorMessage
92 			If not <NULL/>, and a parsing error occurs, the error message will be copied to the string the argument
93 			points to.
94 		@see normalizePredicateString
95 		*/
96 		::rtl::OUString	getPredicateValue(
97 			const ::rtl::OUString& _rPredicateValue,
98 			const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > & _rxField,
99 			sal_Bool _bForStatementUse,
100 			::rtl::OUString* _pErrorMessage = NULL
101 		) const;
102 
103         ::rtl::OUString getPredicateValue(
104 		    const ::rtl::OUString& _sField
105             , const ::rtl::OUString& _rPredicateValue
106             , sal_Bool _bForStatementUse
107             , ::rtl::OUString* _pErrorMessage = NULL) const;
108 
109 	private:
110 		::connectivity::OSQLParseNode* implPredicateTree(
111 			::rtl::OUString& _rErrorMessage,
112 			const ::rtl::OUString& _rStatement,
113 			const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > & _rxField
114 		) const;
115 
116 		sal_Bool getSeparatorChars(
117 			const ::com::sun::star::lang::Locale& _rLocale,
118 			sal_Unicode& _rDecSep,
119 			sal_Unicode& _rThdSep
120 		) const;
121 
122         ::rtl::OUString implParseNode(::connectivity::OSQLParseNode* pParseNode,sal_Bool _bForStatementUse) const;
123 	};
124 
125 //.........................................................................
126 }	// namespace dbtools
127 //.........................................................................
128 
129 #endif // CONNECTIVITY_PREDICATEINPUT_HXX
130 
131