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 _CONNECTIVITY_FILE_OSTATEMENT_HXX_
29 #define _CONNECTIVITY_FILE_OSTATEMENT_HXX_
30 
31 #include <com/sun/star/sdbc/XStatement.hpp>
32 #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
33 #include <com/sun/star/sdbc/XMultipleResults.hpp>
34 #include <com/sun/star/sdbc/XBatchExecution.hpp>
35 #include <com/sun/star/sdbc/XCloseable.hpp>
36 #include <com/sun/star/sdbc/SQLWarning.hpp>
37 #include <com/sun/star/util/XCancellable.hpp>
38 #include <comphelper/proparrhlp.hxx>
39 #include <cppuhelper/compbase3.hxx>
40 #include <cppuhelper/implbase2.hxx>
41 #include <comphelper/uno3.hxx>
42 #include "connectivity/CommonTools.hxx"
43 #include "file/FConnection.hxx"
44 #include "file/filedllapi.hxx"
45 #ifndef _LIST_
46 #include <list>
47 #endif
48 #include <com/sun/star/lang/XServiceInfo.hpp>
49 #include <comphelper/propertycontainer.hxx>
50 #include "file/fanalyzer.hxx"
51 #include <comphelper/broadcasthelper.hxx>
52 #include "TSortIndex.hxx"
53 
54 #define SQL_COLUMN_NOTFOUND STRING_NOTFOUND
55 
56 namespace connectivity
57 {
58 	namespace file
59 	{
60 		class OResultSet;
61 		class OFileTable;
62         typedef ::cppu::WeakComponentImplHelper3<   ::com::sun::star::sdbc::XWarningsSupplier,
63                                                     ::com::sun::star::util::XCancellable,
64                                                     ::com::sun::star::sdbc::XCloseable> OStatement_BASE;
65 
66 		//**************************************************************
67 		//************ Class: java.sql.Statement
68 		//**************************************************************
69 		class OOO_DLLPUBLIC_FILE OStatement_Base :
70                                         public	comphelper::OBaseMutex,
71 										public	OStatement_BASE,
72 										public	::comphelper::OPropertyContainer,
73 										public	::comphelper::OPropertyArrayUsageHelper<OStatement_Base>
74 
75 		{
76 		protected:
77 			::std::vector<sal_Int32>					m_aColMapping; // pos 0 is unused so we don't have to decrement 1 everytime
78 			::std::vector<sal_Int32>					m_aParameterIndexes; // maps the parameter index to column index
79 			::std::vector<sal_Int32>					m_aOrderbyColumnNumber;
80             ::std::vector<TAscendingOrder>              m_aOrderbyAscending;
81 
82 			::com::sun::star::sdbc::SQLWarning                                           m_aLastWarning;
83 			::com::sun::star::uno::WeakReference< ::com::sun::star::sdbc::XResultSet>    m_xResultSet;   // The last ResultSet created
84 			::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData> m_xDBMetaData;
85 			::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess>	 m_xColNames; // table columns															//  for this Statement
86 
87 
88 			connectivity::OSQLParser					m_aParser;
89 			connectivity::OSQLParseTreeIterator			m_aSQLIterator;
90 
91 			OConnection*								m_pConnection;// The owning Connection object
92 			connectivity::OSQLParseNode*				m_pParseTree;
93 			OSQLAnalyzer*								m_pSQLAnalyzer; //the sql analyzer used by the resultset
94 
95 			::std::vector<sal_Int32>*					m_pEvaluationKeySet;
96 
97 			OFileTable*									m_pTable;		// the current table
98 			OValueRefRow								m_aSelectRow;
99 			OValueRefRow								m_aRow;
100 			OValueRefRow								m_aEvaluateRow; // contains all values of a row
101 			ORefAssignValues							m_aAssignValues; // needed for insert,update and parameters
102 																	// to compare with the restrictions
103 
104 			::rtl::OUString								m_aCursorName;
105 			sal_Int32									m_nMaxFieldSize;
106 			sal_Int32									m_nMaxRows;
107 			sal_Int32									m_nQueryTimeOut;
108 			sal_Int32									m_nFetchSize;
109 			sal_Int32									m_nResultSetType;
110 			sal_Int32									m_nFetchDirection;
111 			sal_Int32									m_nResultSetConcurrency;
112 			sal_Bool									m_bEscapeProcessing;
113 
114             ::cppu::OBroadcastHelper&                   rBHelper;
115 
116 		protected:
117 			// initialize the column index map (mapping select columns to table columns)
118 			void createColumnMapping();
119 			// searches the statement for sort criteria
120 			void anylizeSQL();
121 			void setOrderbyColumn( connectivity::OSQLParseNode* pColumnRef,
122 									 connectivity::OSQLParseNode* pAscendingDescending);
123 
124 			virtual void initializeResultSet(OResultSet* _pResult);
125 			// create the analyzer
126 			virtual OSQLAnalyzer* createAnalyzer();
127 
128             void reset () throw( ::com::sun::star::sdbc::SQLException);
129             void clearMyResultSet () throw( ::com::sun::star::sdbc::SQLException);
130             void setWarning (const  ::com::sun::star::sdbc::SQLWarning &ex) throw( ::com::sun::star::sdbc::SQLException);
131 			sal_Int32 getPrecision ( sal_Int32 sqlType);
132 
133 			void disposeResultSet();
134 			void GetAssignValues();
135 			void SetAssignValue(const String& aColumnName,
136 								   const String& aValue,
137 								   sal_Bool bSetNull = sal_False,
138 								   sal_uInt32 nParameter=SQL_NO_PARAMETER);
139 			void ParseAssignValues(	const ::std::vector< String>& aColumnNameList,
140 									connectivity::OSQLParseNode* pRow_Value_Constructor_Elem,xub_StrLen nIndex);
141 
142 			virtual void parseParamterElem(const String& _sColumnName,OSQLParseNode* pRow_Value_Constructor_Elem);
143 			// factory method for resultset's
144 			virtual OResultSet* createResultSet() = 0;
145 			// OPropertyArrayUsageHelper
146 			virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const;
147 			// OPropertySetHelper
148 			virtual ::cppu::IPropertyArrayHelper & SAL_CALL getInfoHelper();
149 			virtual ~OStatement_Base();
150 		public:
151 			connectivity::OSQLParseNode* getParseTree() const { return m_pParseTree;}
152 
153 			OStatement_Base(OConnection* _pConnection );
154 
155 			OConnection* getOwnConnection() const { return m_pConnection;}
156 
157 			using OStatement_BASE::operator ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >;
158 
159 			virtual void construct(const ::rtl::OUString& sql)  throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
160 
161 			// OComponentHelper
162 			virtual void SAL_CALL disposing(void);
163 			// XInterface
164             //      virtual void SAL_CALL release() throw(::com::sun::star::uno::RuntimeException) = 0;
165 			virtual void SAL_CALL acquire() throw();
166 			// XInterface
167             virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException);
168 			//XTypeProvider
169             virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getTypes(  ) throw(::com::sun::star::uno::RuntimeException);
170 
171 			// XPropertySet
172             virtual ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo(  ) throw(::com::sun::star::uno::RuntimeException);
173 			// XWarningsSupplier
174             virtual ::com::sun::star::uno::Any SAL_CALL getWarnings(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
175             virtual void SAL_CALL clearWarnings(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
176 			// XCancellable
177             virtual void SAL_CALL cancel(  ) throw(::com::sun::star::uno::RuntimeException);
178 			// XCloseable
179             virtual void SAL_CALL close(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
180 		};
181 
182 		class OOO_DLLPUBLIC_FILE OStatement_BASE2 :
183                                     public OStatement_Base,
184 									public connectivity::OSubComponent<OStatement_BASE2, OStatement_BASE>
185 
186 		{
187 			friend class connectivity::OSubComponent<OStatement_BASE2, OStatement_BASE>;
188 		public:
189 			OStatement_BASE2(OConnection* _pConnection ) :  OStatement_Base(_pConnection ),
190 									connectivity::OSubComponent<OStatement_BASE2, OStatement_BASE>((::cppu::OWeakObject*)_pConnection, this){}
191 			// OComponentHelper
192 			virtual void SAL_CALL disposing(void);
193 			// XInterface
194             virtual void SAL_CALL release() throw();
195 		};
196 
197 		typedef ::cppu::ImplHelper2< ::com::sun::star::sdbc::XStatement,::com::sun::star::lang::XServiceInfo > OStatement_XStatement;
198 		class OOO_DLLPUBLIC_FILE OStatement :
199                             public OStatement_BASE2,
200 							public OStatement_XStatement
201 		{
202 		protected:
203 			// factory method for resultset's
204 			virtual OResultSet* createResultSet();
205 		public:
206 			// ein Konstruktor, der fuer das Returnen des Objektes benoetigt wird:
207 			OStatement( OConnection* _pConnection) : OStatement_BASE2( _pConnection){}
208 			DECLARE_SERVICE_INFO();
209 
210             virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException);
211 			virtual void SAL_CALL acquire() throw();
212             virtual void SAL_CALL release() throw();
213 
214 			// XStatement
215             virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet > SAL_CALL executeQuery( const ::rtl::OUString& sql ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) ;
216             virtual sal_Int32 SAL_CALL executeUpdate( const ::rtl::OUString& sql ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) ;
217             virtual sal_Bool SAL_CALL execute( const ::rtl::OUString& sql ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) ;
218             virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection > SAL_CALL getConnection(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) ;
219 		};
220 	}
221 }
222 #endif // _CONNECTIVITY_FILE_OSTATEMENT_HXX_
223 
224