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