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