1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9b5730f6SAndrew Rist  * distributed with this work for additional information
6*9b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9b5730f6SAndrew Rist  *
11*9b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9b5730f6SAndrew Rist  *
13*9b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist  * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9b5730f6SAndrew Rist  * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist  * under the License.
19*9b5730f6SAndrew Rist  *
20*9b5730f6SAndrew Rist  *************************************************************/
21*9b5730f6SAndrew Rist 
22*9b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir #include "ado/AColumn.hxx"
27cdf0e10cSrcweir #include "ado/AConnection.hxx"
28cdf0e10cSrcweir #include "ado/Awrapado.hxx"
29cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
30cdf0e10cSrcweir #include <comphelper/sequence.hxx>
31cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnValue.hpp>
32cdf0e10cSrcweir #include <comphelper/extract.hxx>
33cdf0e10cSrcweir #include <comphelper/types.hxx>
34cdf0e10cSrcweir #include "ado/ACatalog.hxx"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using namespace ::comphelper;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using namespace connectivity::ado;
39cdf0e10cSrcweir using namespace com::sun::star::uno;
40cdf0e10cSrcweir using namespace com::sun::star::lang;
41cdf0e10cSrcweir using namespace com::sun::star::beans;
42cdf0e10cSrcweir using namespace com::sun::star::sdbc;
43cdf0e10cSrcweir 
Create()44cdf0e10cSrcweir void WpADOColumn::Create()
45cdf0e10cSrcweir {
46cdf0e10cSrcweir 	HRESULT         hr = -1;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 	_ADOColumn* pColumn = NULL;
49cdf0e10cSrcweir 	hr = CoCreateInstance(ADOS::CLSID_ADOCOLUMN_25,
50cdf0e10cSrcweir 						  NULL,
51cdf0e10cSrcweir 						  CLSCTX_INPROC_SERVER,
52cdf0e10cSrcweir 						  ADOS::IID_ADOCOLUMN_25,
53cdf0e10cSrcweir 						  (void**)&pColumn );
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 	if( !FAILED( hr ) )
57cdf0e10cSrcweir 	{
58cdf0e10cSrcweir 		operator=( pColumn );
59cdf0e10cSrcweir 		pColumn->Release( );
60cdf0e10cSrcweir 	}
61cdf0e10cSrcweir }
62cdf0e10cSrcweir // -------------------------------------------------------------------------
OAdoColumn(sal_Bool _bCase,OConnection * _pConnection,_ADOColumn * _pColumn)63cdf0e10cSrcweir OAdoColumn::OAdoColumn(sal_Bool _bCase,OConnection* _pConnection,_ADOColumn* _pColumn)
64cdf0e10cSrcweir 	: connectivity::sdbcx::OColumn(::rtl::OUString(),::rtl::OUString(),::rtl::OUString(),::rtl::OUString(),0,0,0,0,sal_False,sal_False,sal_False,_bCase)
65cdf0e10cSrcweir 	,m_pConnection(_pConnection)
66cdf0e10cSrcweir {
67cdf0e10cSrcweir 	construct();
68cdf0e10cSrcweir 	OSL_ENSURE(_pColumn,"Column can not be null!");
69cdf0e10cSrcweir 	m_aColumn = WpADOColumn(_pColumn);
70cdf0e10cSrcweir 	//	m_aColumn.put_ParentCatalog(_pConnection->getAdoCatalog()->getCatalog());
71cdf0e10cSrcweir 	fillPropertyValues();
72cdf0e10cSrcweir }
73cdf0e10cSrcweir // -------------------------------------------------------------------------
OAdoColumn(sal_Bool _bCase,OConnection * _pConnection)74cdf0e10cSrcweir OAdoColumn::OAdoColumn(sal_Bool _bCase,OConnection* _pConnection)
75cdf0e10cSrcweir 	: connectivity::sdbcx::OColumn(_bCase)
76cdf0e10cSrcweir 	,m_pConnection(_pConnection)
77cdf0e10cSrcweir {
78cdf0e10cSrcweir 	m_aColumn.Create();
79cdf0e10cSrcweir 	m_aColumn.put_ParentCatalog(_pConnection->getAdoCatalog()->getCatalog());
80cdf0e10cSrcweir 	construct();
81cdf0e10cSrcweir 	fillPropertyValues();
82cdf0e10cSrcweir 	m_Type = DataType::OTHER;
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir //--------------------------------------------------------------------------
getUnoTunnelImplementationId()86cdf0e10cSrcweir Sequence< sal_Int8 > OAdoColumn::getUnoTunnelImplementationId()
87cdf0e10cSrcweir {
88cdf0e10cSrcweir 	static ::cppu::OImplementationId * pId = 0;
89cdf0e10cSrcweir 	if (! pId)
90cdf0e10cSrcweir 	{
91cdf0e10cSrcweir 		::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
92cdf0e10cSrcweir 		if (! pId)
93cdf0e10cSrcweir 		{
94cdf0e10cSrcweir 			static ::cppu::OImplementationId aId;
95cdf0e10cSrcweir 			pId = &aId;
96cdf0e10cSrcweir 		}
97cdf0e10cSrcweir 	}
98cdf0e10cSrcweir 	return pId->getImplementationId();
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir // com::sun::star::lang::XUnoTunnel
102cdf0e10cSrcweir //------------------------------------------------------------------
getSomething(const Sequence<sal_Int8> & rId)103cdf0e10cSrcweir sal_Int64 OAdoColumn::getSomething( const Sequence< sal_Int8 > & rId ) throw (RuntimeException)
104cdf0e10cSrcweir {
105cdf0e10cSrcweir 	return (rId.getLength() == 16 && 0 == rtl_compareMemory(getUnoTunnelImplementationId().getConstArray(),  rId.getConstArray(), 16 ) )
106cdf0e10cSrcweir 				? reinterpret_cast< sal_Int64 >( this )
107cdf0e10cSrcweir 				: OColumn_ADO::getSomething(rId);
108cdf0e10cSrcweir }
109cdf0e10cSrcweir // -------------------------------------------------------------------------
construct()110cdf0e10cSrcweir void OAdoColumn::construct()
111cdf0e10cSrcweir {
112cdf0e10cSrcweir 	sal_Int32 nAttrib = isNew() ? 0 : PropertyAttribute::READONLY;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISASCENDING),		PROPERTY_ID_ISASCENDING,	nAttrib,&m_IsAscending,	::getBooleanCppuType());
115cdf0e10cSrcweir 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RELATEDCOLUMN),	PROPERTY_ID_RELATEDCOLUMN,	nAttrib,&m_ReferencedColumn,	::getCppuType(reinterpret_cast< ::rtl::OUString*>(NULL)));
116cdf0e10cSrcweir }
117cdf0e10cSrcweir // -----------------------------------------------------------------------------
setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any & rValue)118cdf0e10cSrcweir void OAdoColumn::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any& rValue)throw (Exception)
119cdf0e10cSrcweir {
120cdf0e10cSrcweir 	if(m_aColumn.IsValid())
121cdf0e10cSrcweir 	{
122cdf0e10cSrcweir 		const sal_Char* pAdoPropertyName = NULL;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 		switch(nHandle)
125cdf0e10cSrcweir 		{
126cdf0e10cSrcweir 			case PROPERTY_ID_ISASCENDING:
127cdf0e10cSrcweir 				m_aColumn.put_SortOrder(::cppu::any2bool(rValue) ? adSortAscending : adSortDescending);
128cdf0e10cSrcweir 				break;
129cdf0e10cSrcweir 			case PROPERTY_ID_RELATEDCOLUMN:
130cdf0e10cSrcweir 				{
131cdf0e10cSrcweir 					::rtl::OUString aVal;
132cdf0e10cSrcweir 					rValue >>= aVal;
133cdf0e10cSrcweir 					m_aColumn.put_RelatedColumn(aVal);
134cdf0e10cSrcweir 				}
135cdf0e10cSrcweir 				break;
136cdf0e10cSrcweir 			case PROPERTY_ID_NAME:
137cdf0e10cSrcweir 				{
138cdf0e10cSrcweir 					::rtl::OUString aVal;
139cdf0e10cSrcweir 					rValue >>= aVal;
140cdf0e10cSrcweir 					m_aColumn.put_Name(aVal);
141cdf0e10cSrcweir 				}
142cdf0e10cSrcweir 				break;
143cdf0e10cSrcweir 			case PROPERTY_ID_TYPE:
144cdf0e10cSrcweir 				{
145cdf0e10cSrcweir 					sal_Int32 nVal=0;
146cdf0e10cSrcweir 					rValue >>= nVal;
147cdf0e10cSrcweir 					m_aColumn.put_Type(ADOS::MapJdbc2ADOType(nVal,m_pConnection->getEngineType()));
148cdf0e10cSrcweir 				}
149cdf0e10cSrcweir 				break;
150cdf0e10cSrcweir 			case PROPERTY_ID_TYPENAME:
151cdf0e10cSrcweir 				//	rValue <<= m_pTable->getCatalog()->getConnection()->getTypeInfo()->find();
152cdf0e10cSrcweir 				break;
153cdf0e10cSrcweir 			case PROPERTY_ID_PRECISION:
154cdf0e10cSrcweir 				{
155cdf0e10cSrcweir 					sal_Int32 nVal=0;
156cdf0e10cSrcweir 					rValue >>= nVal;
157cdf0e10cSrcweir 					m_aColumn.put_Precision(nVal);
158cdf0e10cSrcweir 				}
159cdf0e10cSrcweir 				break;
160cdf0e10cSrcweir 			case PROPERTY_ID_SCALE:
161cdf0e10cSrcweir 				{
162cdf0e10cSrcweir 					sal_Int32 nVal=0;
163cdf0e10cSrcweir 					rValue >>= nVal;
164cdf0e10cSrcweir                     if ( !m_IsCurrency )
165cdf0e10cSrcweir 					    m_aColumn.put_NumericScale((sal_Int8)nVal);
166cdf0e10cSrcweir 				}
167cdf0e10cSrcweir 				break;
168cdf0e10cSrcweir 			case PROPERTY_ID_ISNULLABLE:
169cdf0e10cSrcweir 				{
170cdf0e10cSrcweir 					sal_Int32 nVal=0;
171cdf0e10cSrcweir 					rValue >>= nVal;
172cdf0e10cSrcweir 					if ( nVal == ColumnValue::NULLABLE )
173cdf0e10cSrcweir 						m_aColumn.put_Attributes( adColNullable );
174cdf0e10cSrcweir 				}
175cdf0e10cSrcweir 				break;
176cdf0e10cSrcweir 			case PROPERTY_ID_ISROWVERSION:
177cdf0e10cSrcweir 				break;
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 			case PROPERTY_ID_ISAUTOINCREMENT:
180cdf0e10cSrcweir 				OTools::putValue( m_aColumn.get_Properties(), ::rtl::OUString::createFromAscii( "Autoincrement" ), getBOOL( rValue ) );
181cdf0e10cSrcweir 				break;
182cdf0e10cSrcweir 
183cdf0e10cSrcweir             case PROPERTY_ID_IM001:
184cdf0e10cSrcweir 			case PROPERTY_ID_DESCRIPTION:
185cdf0e10cSrcweir                 pAdoPropertyName = "Description";
186cdf0e10cSrcweir 				break;
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 			case PROPERTY_ID_DEFAULTVALUE:
189cdf0e10cSrcweir 				pAdoPropertyName = "Default";
190cdf0e10cSrcweir 				break;
191cdf0e10cSrcweir 		}
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 		if ( pAdoPropertyName )
194cdf0e10cSrcweir 			OTools::putValue( m_aColumn.get_Properties(), ::rtl::OUString::createFromAscii( pAdoPropertyName ), getString( rValue ) );
195cdf0e10cSrcweir 	}
196cdf0e10cSrcweir 	OColumn_ADO::setFastPropertyValue_NoBroadcast(nHandle,rValue);
197cdf0e10cSrcweir }
198cdf0e10cSrcweir // -----------------------------------------------------------------------------
fillPropertyValues()199cdf0e10cSrcweir void OAdoColumn::fillPropertyValues()
200cdf0e10cSrcweir {
201cdf0e10cSrcweir 	if(m_aColumn.IsValid())
202cdf0e10cSrcweir 	{
203cdf0e10cSrcweir 		m_IsAscending		= m_aColumn.get_SortOrder() == adSortAscending;
204cdf0e10cSrcweir 		m_ReferencedColumn	= m_aColumn.get_RelatedColumn();
205cdf0e10cSrcweir 		m_Name				= m_aColumn.get_Name();
206cdf0e10cSrcweir 		m_Precision			= m_aColumn.get_Precision();
207cdf0e10cSrcweir 		m_Scale				= m_aColumn.get_NumericScale();
208cdf0e10cSrcweir 		m_IsNullable		= ((m_aColumn.get_Attributes() & adColNullable) == adColNullable) ? ColumnValue::NULLABLE : ColumnValue::NO_NULLS;
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 		DataTypeEnum eType	= m_aColumn.get_Type();
211cdf0e10cSrcweir 		m_IsCurrency		= (eType == adCurrency);
212cdf0e10cSrcweir         if ( m_IsCurrency && !m_Scale)
213cdf0e10cSrcweir             m_Scale = 4;
214cdf0e10cSrcweir 		m_Type				= ADOS::MapADOType2Jdbc(eType);
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 		sal_Bool bForceTo = sal_True;
217cdf0e10cSrcweir 		const OTypeInfoMap* pTypeInfoMap = m_pConnection->getTypeInfo();
218cdf0e10cSrcweir 		const OExtendedTypeInfo* pTypeInfo = OConnection::getTypeInfoFromType(*m_pConnection->getTypeInfo(),eType,::rtl::OUString(),m_Precision,m_Scale,bForceTo);
219cdf0e10cSrcweir 		if ( pTypeInfo )
220cdf0e10cSrcweir 			m_TypeName = pTypeInfo->aSimpleType.aTypeName;
221cdf0e10cSrcweir 		else if ( eType == adVarBinary && ADOS::isJetEngine(m_pConnection->getEngineType()) )
222cdf0e10cSrcweir 		{
223cdf0e10cSrcweir 			::comphelper::TStringMixEqualFunctor aCase(sal_False);
224cdf0e10cSrcweir 			OTypeInfoMap::const_iterator aFind = ::std::find_if(pTypeInfoMap->begin(),
225cdf0e10cSrcweir 															pTypeInfoMap->end(),
226cdf0e10cSrcweir 															::std::compose1(
227cdf0e10cSrcweir 															::std::bind2nd(aCase, ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("VarBinary"))),
228cdf0e10cSrcweir 																::std::compose1(
229cdf0e10cSrcweir 																	::std::mem_fun(&OExtendedTypeInfo::getDBName),
230cdf0e10cSrcweir 																	::std::select2nd<OTypeInfoMap::value_type>())
231cdf0e10cSrcweir 																)
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 													);
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 			if ( aFind != pTypeInfoMap->end() ) // change column type if necessary
236cdf0e10cSrcweir 			{
237cdf0e10cSrcweir 				eType = aFind->first;
238cdf0e10cSrcweir 				pTypeInfo = aFind->second;
239cdf0e10cSrcweir 			}
240cdf0e10cSrcweir 
241cdf0e10cSrcweir 			if ( !pTypeInfo )
242cdf0e10cSrcweir 			{
243cdf0e10cSrcweir 				pTypeInfo = OConnection::getTypeInfoFromType(*m_pConnection->getTypeInfo(),adBinary,::rtl::OUString(),m_Precision,m_Scale,bForceTo);
244cdf0e10cSrcweir 				eType = adBinary;
245cdf0e10cSrcweir 			}
246cdf0e10cSrcweir 
247cdf0e10cSrcweir 			if ( pTypeInfo )
248cdf0e10cSrcweir 			{
249cdf0e10cSrcweir 				m_TypeName = pTypeInfo->aSimpleType.aTypeName;
250cdf0e10cSrcweir 				m_Type	= ADOS::MapADOType2Jdbc(eType);
251cdf0e10cSrcweir 			}
252cdf0e10cSrcweir 		}
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 
255cdf0e10cSrcweir 		// fill some specific props
256cdf0e10cSrcweir 		{
257cdf0e10cSrcweir 			WpADOProperties aProps( m_aColumn.get_Properties() );
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 			if ( aProps.IsValid() )
260cdf0e10cSrcweir 			{
261cdf0e10cSrcweir 				m_IsAutoIncrement = OTools::getValue( aProps, ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Autoincrement")) );
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 				m_Description = OTools::getValue( aProps, ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Description")) );
264cdf0e10cSrcweir 
265cdf0e10cSrcweir 				m_DefaultValue = OTools::getValue( aProps, ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Default")) );
266cdf0e10cSrcweir 
267cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
268cdf0e10cSrcweir 				sal_Int32 nCount = aProps.GetItemCount();
269cdf0e10cSrcweir 				for (sal_Int32 i = 0; i<nCount; ++i)
270cdf0e10cSrcweir 				{
271cdf0e10cSrcweir 					WpADOProperty aProp = aProps.GetItem(i);
272cdf0e10cSrcweir 					::rtl::OUString sName = aProp.GetName();
273cdf0e10cSrcweir 					::rtl::OUString sVal = aProp.GetValue();
274cdf0e10cSrcweir 				}
275cdf0e10cSrcweir #endif
276cdf0e10cSrcweir 			}
277cdf0e10cSrcweir 		}
278cdf0e10cSrcweir 	}
279cdf0e10cSrcweir }
280cdf0e10cSrcweir // -----------------------------------------------------------------------------
getColumnImpl() const281cdf0e10cSrcweir WpADOColumn	OAdoColumn::getColumnImpl() const
282cdf0e10cSrcweir {
283cdf0e10cSrcweir 	return m_aColumn;
284cdf0e10cSrcweir }
285cdf0e10cSrcweir // -----------------------------------------------------------------------------
286cdf0e10cSrcweir // -----------------------------------------------------------------------------
acquire()287cdf0e10cSrcweir void SAL_CALL OAdoColumn::acquire() throw()
288cdf0e10cSrcweir {
289cdf0e10cSrcweir 	OColumn_ADO::acquire();
290cdf0e10cSrcweir }
291cdf0e10cSrcweir // -----------------------------------------------------------------------------
release()292cdf0e10cSrcweir void SAL_CALL OAdoColumn::release() throw()
293cdf0e10cSrcweir {
294cdf0e10cSrcweir 	OColumn_ADO::release();
295cdf0e10cSrcweir }
296cdf0e10cSrcweir // -----------------------------------------------------------------------------
297cdf0e10cSrcweir 
298cdf0e10cSrcweir 
299cdf0e10cSrcweir 
300