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 DBAUI_QUERYCONTROLLER_HXX
25 #define DBAUI_QUERYCONTROLLER_HXX
26 
27 #include "JoinController.hxx"
28 #include "JoinTableView.hxx"
29 #include "querycontainerwindow.hxx"
30 #include "queryview.hxx"
31 #include "svx/ParseContext.hxx"
32 #include "TableFieldDescription.hxx"
33 
34 /** === begin UNO includes === **/
35 #include <com/sun/star/io/XObjectInputStream.hpp>
36 #include <com/sun/star/io/XObjectOutputStream.hpp>
37 #include <com/sun/star/sdb/CommandType.hpp>
38 #include <com/sun/star/sdb/XSQLQueryComposer.hpp>
39 #include <com/sun/star/sdbc/XConnection.hpp>
40 #include <com/sun/star/sdbcx/XAlterView.hpp>
41 /** === end UNO includes === **/
42 
43 #include <comphelper/proparrhlp.hxx>
44 #include <comphelper/propertycontainer.hxx>
45 #include <comphelper/uno3.hxx>
46 #include <comphelper/uno3.hxx>
47 #include <connectivity/sqliterator.hxx>
48 #include <connectivity/sqlnode.hxx>
49 #include <connectivity/sqlparse.hxx>
50 #include <svl/undo.hxx>
51 
52 namespace comphelper
53 {
54     class NamedValueCollection;
55 }
56 
57 class VCLXWindow;
58 namespace dbaui
59 {
60 	class OQueryView;
61 	class OQueryContainerWindow;
62 	class OTableConnectionData;
63 	class OTableWindowData;
64 	class OAddTableDlg;
65 	class OTableFieldDesc;
66 	class OQueryTableWindow;
67 
68     class OQueryController;
69     typedef ::comphelper::OPropertyContainer                            OQueryController_PBase;
70     typedef ::comphelper::OPropertyArrayUsageHelper< OQueryController > OQueryController_PABase;
71     class OQueryController  :public OJoinController
72                             ,public OQueryController_PBase
73                             ,public OQueryController_PABase
74 	{
75 		OTableFields							m_vTableFieldDesc;
76 		OTableFields							m_vUnUsedFieldsDesc; // contains fields which aren't visible and don't have any criteria
77 
78         ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >       m_aFieldInformation;
79 
80 		::svxform::OSystemParseContext*			m_pParseContext;
81 		::connectivity::OSQLParser              m_aSqlParser;
82 		::connectivity::OSQLParseTreeIterator*  m_pSqlIterator;
83 
84 		::com::sun::star::uno::Reference< ::com::sun::star::sdb::XSQLQueryComposer > 	m_xComposer;
85         /// if we're editing an existing view, this is non-NULL
86         ::com::sun::star::uno::Reference< ::com::sun::star::sdbcx::XAlterView >         m_xAlterView;
87 
88 		::rtl::OUString	m_sStatement;		    // contains the current sql statement
89 		::rtl::OUString	m_sUpdateCatalogName;   // catalog for update data
90 		::rtl::OUString	m_sUpdateSchemaName;    // schema for update data
91 		::rtl::OUString	m_sUpdateTableName;     // table for update data
92 		mutable ::rtl::OUString
93                         m_sName;			    // name of the query
94 
95 		sal_Int32		m_nVisibleRows;		// which rows the selection browse should show
96 		sal_Int32		m_nSplitPos;		// the position of the splitter
97         sal_Int32       m_nCommandType;     // the type of the object we're designing
98 		sal_Bool		m_bGraphicalDesign; // are we in the graphical design mode (sal_True) or in the text design (sal_False)?
99 		sal_Bool		m_bDistinct;		// true when you want "select distinct" otherwise false
100 		sal_Bool		m_bViewAlias;		// show the alias row in the design view
101 		sal_Bool		m_bViewTable;		// show the table row in the design view
102 		sal_Bool		m_bViewFunction;	// show the function row in the design view
103 		sal_Bool		m_bEscapeProcessing;// is true when we shouldn't parse the statement
104 
105 
106         /** returns the container of queries, views, or command definitions, depending on what object type
107             we design currently.
108 
109             Not allowed to be called if we design an independent SQL command.
110         */
111 		::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >
112             getObjectContainer() const;
113 
editingView() const114         inline  bool    editingView() const    { return m_nCommandType == ::com::sun::star::sdb::CommandType::TABLE; }
editingQuery() const115         inline  bool    editingQuery() const   { return m_nCommandType == ::com::sun::star::sdb::CommandType::QUERY; }
editingCommand() const116         inline  bool    editingCommand() const { return m_nCommandType == ::com::sun::star::sdb::CommandType::COMMAND; }
117 
118 		sal_Bool askForNewName(	const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess>& _xElements,
119 							sal_Bool _bSaveAs);
120 		// creates the querycomposer
121 		void setQueryComposer();
122 		void deleteIterator();
123 		void executeQuery();
124 		bool doSaveAsDoc(sal_Bool _bSaveAs);
125 
126 		void saveViewSettings( ::comphelper::NamedValueCollection& o_rViewSettings, const bool i_includingCriteria ) const;
127 		void loadViewSettings( const ::comphelper::NamedValueCollection& o_rViewSettings );
128 		::rtl::OUString translateStatement( bool _bFireStatementChange = true );
129 
130         ::rtl::OUString getDefaultName() const;
131 
132 	protected:
133 		// all the features which should be handled by this class
134 		virtual void			describeSupportedFeatures();
135 		// state of a feature. 'feature' may be the handle of a ::com::sun::star::util::URL somebody requested a dispatch interface for OR a toolbar slot.
136 		virtual FeatureState	GetState(sal_uInt16 nId) const;
137 		// execute a feature
138 		virtual void			Execute(sal_uInt16 nId, const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue>& aArgs);
139 
140 		virtual void			reconnect( sal_Bool _bUI );
141 		virtual ::rtl::OUString getPrivateTitle( ) const;
142 
getContainer() const143         OQueryContainerWindow* getContainer() const { return static_cast< OQueryContainerWindow* >( getView() ); }
144 
145 	public:
146 		OQueryController(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM);
147 
148 		virtual ~OQueryController();
getTableFieldDesc()149 		OTableFields&	getTableFieldDesc()			{ return m_vTableFieldDesc; }
getUnUsedFields()150 		OTableFields&	getUnUsedFields()			{ return m_vUnUsedFieldsDesc; }
151 
152 		void			clearFields();
153 
154         virtual void impl_onModifyChanged();
155 
156 		// should the statement be parsed by our own sql parser
isEsacpeProcessing() const157 		sal_Bool		isEsacpeProcessing()	const { return m_bEscapeProcessing; }
isGraphicalDesign() const158 		sal_Bool		isGraphicalDesign()     const { return m_bGraphicalDesign; }
isDistinct() const159 		sal_Bool		isDistinct()			const { return m_bDistinct; }
160 
getStatement() const161 		::rtl::OUString	getStatement()			const { return m_sStatement; }
getSplitPos() const162 		sal_Int32		getSplitPos()			const { return m_nSplitPos;}
getVisibleRows() const163 		sal_Int32		getVisibleRows()		const { return m_nVisibleRows; }
164 
setDistinct(sal_Bool _bDistinct)165 		void			setDistinct(sal_Bool _bDistinct)		{ m_bDistinct = _bDistinct;}
setSplitPos(sal_Int32 _nSplitPos)166 		void			setSplitPos(sal_Int32 _nSplitPos)		{ m_nSplitPos = _nSplitPos;}
setVisibleRows(sal_Int32 _nVisibleRows)167 		void			setVisibleRows(sal_Int32 _nVisibleRows)	{ m_nVisibleRows = _nVisibleRows;}
168 
169         sal_Int32       getColWidth(sal_uInt16 _nColPos) const;
170 
171         const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >&
getFieldInformation() const172                         getFieldInformation() const { return m_aFieldInformation; }
173 
getParser()174 		::connectivity::OSQLParser&             getParser()         { return m_aSqlParser;  }
getParseIterator()175 		::connectivity::OSQLParseTreeIterator&	getParseIterator()  { return *m_pSqlIterator; }
176 
177 		virtual sal_Bool Construct(Window* pParent);
178 
179         DECLARE_XINTERFACE( )
180         DECLARE_XTYPEPROVIDER( )
181 		// XPropertySet
182 		virtual com::sun::star::uno::Reference<com::sun::star::beans::XPropertySetInfo>  SAL_CALL getPropertySetInfo() throw(com::sun::star::uno::RuntimeException);
183 		virtual ::cppu::IPropertyArrayHelper& SAL_CALL getInfoHelper();
184 
185 		// XEventListener
186 		virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw(::com::sun::star::uno::RuntimeException);
187 
188 		// ::com::sun::star::lang::XComponent
189 		virtual void		SAL_CALL disposing();
190 
191 		virtual ::rtl::OUString SAL_CALL getImplementationName() throw(::com::sun::star::uno::RuntimeException);
192 		virtual ::com::sun::star::uno::Sequence< ::rtl::OUString> SAL_CALL getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException);
193 		// need by registration
194 		static ::rtl::OUString getImplementationName_Static() throw( ::com::sun::star::uno::RuntimeException );
195 		static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static(void) throw( ::com::sun::star::uno::RuntimeException );
196 		static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
197 				SAL_CALL Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&);
198 
199         // XController
200 		virtual ::com::sun::star::uno::Any SAL_CALL getViewData(void) throw( ::com::sun::star::uno::RuntimeException );
201 		virtual void SAL_CALL restoreViewData(const ::com::sun::star::uno::Any& Data) throw( ::com::sun::star::uno::RuntimeException );
202 
203 	private:
204         virtual void    onLoadedMenu(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XLayoutManager >& _xLayoutManager);
205         // OPropertyArrayUsageHelper
206         virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const;
207 
208         // OPropertySetHelper
209 	    virtual sal_Bool SAL_CALL convertFastPropertyValue(
210                                     ::com::sun::star::uno::Any& rConvertedValue,
211                                     ::com::sun::star::uno::Any& rOldValue,
212 		                            sal_Int32 nHandle,
213                                     const ::com::sun::star::uno::Any& rValue
214                                 ) throw (::com::sun::star::lang::IllegalArgumentException);
215         virtual void SAL_CALL setFastPropertyValue_NoBroadcast(
216 		                            sal_Int32 nHandle,
217 		                            const ::com::sun::star::uno::Any& rValue
218                                 ) throw (::com::sun::star::uno::Exception );
219 	    virtual void SAL_CALL getFastPropertyValue(
220 		                            ::com::sun::star::uno::Any& rValue,
221 		                            sal_Int32 nHandle
222                                 ) const;
223 
224 		virtual OJoinDesignView*  getJoinView();
225 		// ask the user if the design should be saved when it is modified
226 		virtual short saveModified();
227 		virtual void reset();
228 		virtual void impl_initialize();
229 
230         void    impl_reset( const bool i_bIgnoreQuerySettings = false );
231         /// tells the user that we needed to switch to SQL view automatically
232         void    impl_showAutoSQLViewError( const ::com::sun::star::uno::Any& _rErrorDetails );
233 
234         /** switches to the graphical or SQL view mode, as determined by m_bGraphicalDesign
235         */
236         bool    impl_setViewMode( ::dbtools::SQLExceptionInfo* _pErrorInfo );
237 
238         /// sets m_sStatement, and notifies our respective property change listeners
239         void    setStatement_fireEvent( const ::rtl::OUString& _rNewStatement, bool _bFireStatementChange = true );
240         /// sets the m_bEscapeProcessing member, and notifies our respective property change listeners
241         void    setEscapeProcessing_fireEvent( const sal_Bool _bEscapeProcessing );
242 
243         // OJoinController overridables
244         virtual bool allowViews() const;
245         virtual bool allowQueries() const;
246 
247     private:
248         DECL_LINK( OnExecuteAddTable, void* );
249 
250     private:
251         using OQueryController_PBase::getFastPropertyValue;
252 	};
253 }
254 #endif // DBAUI_QUERYCONTROLLER_HXX
255 
256