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