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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 
27 #include "connectivity/PColumn.hxx"
28 #include "connectivity/dbtools.hxx"
29 #include "TConnection.hxx"
30 
31 #include <comphelper/types.hxx>
32 #include <tools/diagnose_ex.h>
33 
34 using namespace ::comphelper;
35 using namespace connectivity;
36 using namespace dbtools;
37 using namespace connectivity::parse;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::sdbc;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::container;
42 
43 // -------------------------------------------------------------------------
44 OParseColumn::OParseColumn(const Reference<XPropertySet>& _xColumn,sal_Bool		_bCase)
45 	: connectivity::sdbcx::OColumn(	getString(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)))
46 								,	getString(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME)))
47 								,	getString(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE)))
48                                 ,	getString(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DESCRIPTION)))
49 								,	getINT32(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE)))
50 								,	getINT32(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION)))
51 								,	getINT32(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE)))
52 								,	getINT32(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)))
53 								,	getBOOL(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISAUTOINCREMENT)))
54 								,	sal_False
55 								,	getBOOL(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISCURRENCY)))
56 								,	_bCase
57 								)
58 	, m_bFunction(sal_False)
59 	, m_bDbasePrecisionChanged(sal_False)
60 	, m_bAggregateFunction(sal_False)
61     , m_bIsSearchable( sal_True )
62 {
63 	construct();
64 }
65 
66 // -------------------------------------------------------------------------
67 OParseColumn::OParseColumn(	const ::rtl::OUString& _Name,
68 					const ::rtl::OUString& _TypeName,
69 					const ::rtl::OUString& _DefaultValue,
70                     const ::rtl::OUString& _Description,
71 					sal_Int32		_IsNullable,
72 					sal_Int32		_Precision,
73 					sal_Int32		_Scale,
74 					sal_Int32		_Type,
75 					sal_Bool		_IsAutoIncrement,
76 					sal_Bool		_IsCurrency,
77 					sal_Bool		_bCase
78 				) : connectivity::sdbcx::OColumn(_Name,
79 								  _TypeName,
80 								  _DefaultValue,
81                                   _Description,
82 								  _IsNullable,
83 								  _Precision,
84 								  _Scale,
85 								  _Type,
86 								  _IsAutoIncrement,
87 								  sal_False,
88 								  _IsCurrency,
89 								  _bCase)
90 	, m_bFunction(sal_False)
91 	, m_bDbasePrecisionChanged(sal_False)
92 	, m_bAggregateFunction(sal_False)
93     , m_bIsSearchable( sal_True )
94 {
95 	construct();
96 }
97 
98 // -------------------------------------------------------------------------
99 ::vos::ORef< OSQLColumns > OParseColumn::createColumnsForResultSet( const Reference< XResultSetMetaData >& _rxResMetaData,
100     const Reference< XDatabaseMetaData >& _rxDBMetaData,const Reference< XNameAccess>& i_xQueryColumns )
101 {
102     sal_Int32 nColumnCount = _rxResMetaData->getColumnCount();
103     ::vos::ORef< OSQLColumns > aReturn( new OSQLColumns ); aReturn->get().reserve( nColumnCount );
104 
105 	StringMap aColumnMap;
106     for ( sal_Int32 i = 1; i <= nColumnCount; ++i )
107     {
108         OParseColumn* pColumn = createColumnForResultSet( _rxResMetaData, _rxDBMetaData, i,aColumnMap );
109         aReturn->get().push_back( pColumn );
110         if ( i_xQueryColumns.is() && i_xQueryColumns->hasByName(pColumn->getRealName()) )
111         {
112             Reference<XPropertySet> xColumn(i_xQueryColumns->getByName(pColumn->getRealName()),UNO_QUERY_THROW);
113             ::rtl::OUString sLabel;
114             xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_LABEL)) >>= sLabel;
115             if ( sLabel.getLength() )
116                 pColumn->setLabel(sLabel);
117         }
118     }
119 
120     return aReturn;
121 }
122 
123 // -------------------------------------------------------------------------
124 OParseColumn* OParseColumn::createColumnForResultSet( const Reference< XResultSetMetaData >& _rxResMetaData,
125     const Reference< XDatabaseMetaData >& _rxDBMetaData, sal_Int32 _nColumnPos,StringMap& _rColumns )
126 {
127     ::rtl::OUString sLabel = _rxResMetaData->getColumnLabel( _nColumnPos );
128     // retrieve the name of the column
129 	// check for duplicate entries
130 	if(_rColumns.find(sLabel) != _rColumns.end())
131 	{
132 		::rtl::OUString sAlias(sLabel);
133 		sal_Int32 searchIndex=1;
134 		while(_rColumns.find(sAlias) != _rColumns.end())
135 		{
136             (sAlias = sLabel) += ::rtl::OUString::valueOf(searchIndex++);
137 		}
138 		sLabel = sAlias;
139 	}
140     _rColumns.insert(StringMap::value_type(sLabel,0));
141 	OParseColumn* pColumn = new OParseColumn(
142         sLabel,
143 		_rxResMetaData->getColumnTypeName( _nColumnPos ),
144         ::rtl::OUString(),
145         ::rtl::OUString(),
146         _rxResMetaData->isNullable( _nColumnPos ),
147         _rxResMetaData->getPrecision( _nColumnPos ),
148         _rxResMetaData->getScale( _nColumnPos ),
149         _rxResMetaData->getColumnType( _nColumnPos ),
150         _rxResMetaData->isAutoIncrement( _nColumnPos ),
151         _rxResMetaData->isCurrency( _nColumnPos ),
152         _rxDBMetaData->supportsMixedCaseQuotedIdentifiers()
153     );
154     const ::rtl::OUString sTableName = _rxResMetaData->getTableName( _nColumnPos );
155     if ( sTableName.getLength() )
156         pColumn->setTableName(  ::dbtools::composeTableName( _rxDBMetaData,
157             _rxResMetaData->getCatalogName( _nColumnPos ),
158             _rxResMetaData->getSchemaName( _nColumnPos ),
159             sTableName,
160             sal_False,
161             eComplete
162         ) );
163     pColumn->setIsSearchable( _rxResMetaData->isSearchable( _nColumnPos ) );
164     pColumn->setRealName(_rxResMetaData->getColumnName( _nColumnPos ));
165     pColumn->setLabel(sLabel);
166     return pColumn;
167 }
168 
169 // -------------------------------------------------------------------------
170 OParseColumn::~OParseColumn()
171 {
172 }
173 // -------------------------------------------------------------------------
174 void OParseColumn::construct()
175 {
176 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FUNCTION),				PROPERTY_ID_FUNCTION,				0,  &m_bFunction,		        ::getCppuType(reinterpret_cast< sal_Bool*>(NULL)));
177 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_AGGREGATEFUNCTION),		PROPERTY_ID_AGGREGATEFUNCTION,		0,  &m_bAggregateFunction,		::getCppuType(reinterpret_cast< sal_Bool*>(NULL)));
178 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TABLENAME),				PROPERTY_ID_TABLENAME,				0,  &m_aTableName,		        ::getCppuType(reinterpret_cast< ::rtl::OUString*>(NULL)));
179 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REALNAME),				PROPERTY_ID_REALNAME,				0,  &m_aRealName,		        ::getCppuType(reinterpret_cast< ::rtl::OUString*>(NULL)));
180 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DBASEPRECISIONCHANGED),	PROPERTY_ID_DBASEPRECISIONCHANGED,	0,  &m_bDbasePrecisionChanged,	::getCppuType(reinterpret_cast<sal_Bool*>(NULL)));
181     registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISSEARCHABLE),	        PROPERTY_ID_ISSEARCHABLE,			0,  &m_bIsSearchable,           ::getCppuType(reinterpret_cast< sal_Bool*>(NULL)));
182     registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_LABEL),				    PROPERTY_ID_LABEL,				    0,  &m_sLabel,		            ::getCppuType(reinterpret_cast< ::rtl::OUString*>(NULL)));
183 }
184 // -----------------------------------------------------------------------------
185 ::cppu::IPropertyArrayHelper* OParseColumn::createArrayHelper() const
186 {
187     return doCreateArrayHelper();
188 }
189 // -----------------------------------------------------------------------------
190 ::cppu::IPropertyArrayHelper & SAL_CALL OParseColumn::getInfoHelper()
191 {
192     OSL_ENSURE( !isNew(), "OParseColumn::getInfoHelper: a *new* ParseColumn?" );
193 	return *OParseColumn_PROP::getArrayHelper();
194 }
195 
196 // -----------------------------------------------------------------------------
197 namespace
198 {
199     ::rtl::OUString lcl_getColumnTableName( const Reference< XPropertySet >& i_parseColumn )
200     {
201         ::rtl::OUString sColumnTableName;
202         try
203         {
204             OSL_VERIFY( i_parseColumn->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_TABLENAME ) ) >>= sColumnTableName );
205         }
206         catch( const Exception& )
207         {
208         	DBG_UNHANDLED_EXCEPTION();
209         }
210         return sColumnTableName;
211     }
212 }
213 
214 // -----------------------------------------------------------------------------
215 OOrderColumn::OOrderColumn(	const Reference<XPropertySet>& _xColumn, const ::rtl::OUString& i_rOriginatingTableName,
216                             sal_Bool	_bCase, sal_Bool _bAscending )
217 	: connectivity::sdbcx::OColumn(
218         getString(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))),
219 		getString(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME))),
220 		getString(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE))),
221         getString(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DESCRIPTION))),
222 		getINT32(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE))),
223 		getINT32(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION))),
224 		getINT32(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE))),
225 		getINT32(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))),
226 		getBOOL(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISAUTOINCREMENT))),
227 		sal_False,
228 		getBOOL(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISCURRENCY))),
229 		_bCase
230     )
231 	,m_bAscending(_bAscending)
232     ,m_sTableName( i_rOriginatingTableName )
233 {
234 	construct();
235 }
236 
237 // -----------------------------------------------------------------------------
238 OOrderColumn::OOrderColumn(	const Reference<XPropertySet>& _xColumn, sal_Bool _bCase, sal_Bool _bAscending )
239 	: connectivity::sdbcx::OColumn(
240         getString(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))),
241 		getString(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME))),
242 		getString(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE))),
243         getString(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DESCRIPTION))),
244 		getINT32(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE))),
245 		getINT32(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION))),
246 		getINT32(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE))),
247 		getINT32(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))),
248 		getBOOL(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISAUTOINCREMENT))),
249 		sal_False,
250 		getBOOL(_xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISCURRENCY))),
251 		_bCase
252     )
253 	,m_bAscending(_bAscending)
254     ,m_sTableName( lcl_getColumnTableName( _xColumn ) )
255 {
256 	construct();
257 }
258 
259 // -------------------------------------------------------------------------
260 OOrderColumn::~OOrderColumn()
261 {
262 }
263 
264 // -------------------------------------------------------------------------
265 void OOrderColumn::construct()
266 {
267 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISASCENDING), PROPERTY_ID_ISASCENDING,
268         PropertyAttribute::READONLY,  const_cast< sal_Bool* >( &m_bAscending ),    ::getCppuType( reinterpret_cast< sal_Bool* >( NULL ) ) );
269     registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TABLENAME),   PROPERTY_ID_TABLENAME,
270         PropertyAttribute::READONLY,  const_cast< ::rtl::OUString* >( &m_sTableName ),  ::getCppuType(reinterpret_cast< ::rtl::OUString*>(NULL)));
271 }
272 // -----------------------------------------------------------------------------
273 ::cppu::IPropertyArrayHelper* OOrderColumn::createArrayHelper() const
274 {
275     return doCreateArrayHelper();
276 }
277 // -----------------------------------------------------------------------------
278 ::cppu::IPropertyArrayHelper & SAL_CALL OOrderColumn::getInfoHelper()
279 {
280     OSL_ENSURE( !isNew(), "OOrderColumn::getInfoHelper: a *new* OrderColumn?" );
281 	return *OOrderColumn_PROP::getArrayHelper();
282 }
283 // -----------------------------------------------------------------------------
284 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL OOrderColumn::getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException)
285 {
286 	::com::sun::star::uno::Sequence< ::rtl::OUString > aSupported(1);
287 	aSupported[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdb.OrderColumn");
288 
289 	return aSupported;
290 }
291 // -----------------------------------------------------------------------------
292