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