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