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 #include "ado/AColumns.hxx"
27 #include "ado/AColumn.hxx"
28 #include "ado/AConnection.hxx"
29 #include "ado/Awrapado.hxx"
30 #include <com/sun/star/sdbc/XRow.hpp>
31 #include <com/sun/star/sdbc/XResultSet.hpp>
32 #include <com/sun/star/sdbc/DataType.hpp>
33 #include <com/sun/star/sdbc/ColumnValue.hpp>
34 #include <comphelper/property.hxx>
35 #include <comphelper/types.hxx>
36 #include <connectivity/dbexception.hxx>
37 #ifdef __MINGW32__
38 #include <algorithm>
39 #endif
40 #include "resource/ado_res.hrc"
41 
42 using namespace connectivity::ado;
43 using namespace connectivity;
44 using namespace comphelper;
45 using namespace com::sun::star::uno;
46 using namespace com::sun::star::lang;
47 using namespace com::sun::star::beans;
48 using namespace com::sun::star::sdbc;
49 using namespace com::sun::star::container;
50 
51 sdbcx::ObjectType OColumns::createObject(const ::rtl::OUString& _rName)
52 {
53 	return new OAdoColumn(isCaseSensitive(),m_pConnection,m_aCollection.GetItem(_rName));
54 }
55 
56 // -------------------------------------------------------------------------
57 void OColumns::impl_refresh() throw(RuntimeException)
58 {
59 	m_aCollection.Refresh();
60 }
61 // -------------------------------------------------------------------------
62 Reference< XPropertySet > OColumns::createDescriptor()
63 {
64 	return new OAdoColumn(isCaseSensitive(),m_pConnection);
65 }
66 // -------------------------------------------------------------------------
67 // XAppend
68 sdbcx::ObjectType OColumns::appendObject( const ::rtl::OUString&, const Reference< XPropertySet >& descriptor )
69 {
70 	OAdoColumn* pColumn = NULL;
71     Reference< XPropertySet > xColumn;
72 	if ( !getImplementation( pColumn, descriptor ) || pColumn == NULL )
73     {
74         // m_pConnection->throwGenericSQLException( STR_INVALID_COLUMN_DESCRIPTOR_ERROR,static_cast<XTypeProvider*>(this) );
75         pColumn = new OAdoColumn(isCaseSensitive(),m_pConnection);
76         xColumn = pColumn;
77         ::comphelper::copyProperties(descriptor,xColumn);
78     }
79 
80 	WpADOColumn aColumn = pColumn->getColumnImpl();
81 
82 #if OSL_DEBUG_LEVEL > 0
83     sal_Int32 nPrecision;
84     sal_Int32 nScale;
85     sal_Int32 nType;
86     nPrecision = aColumn.get_Precision();
87     nScale = aColumn.get_NumericScale();
88     nType = ADOS::MapADOType2Jdbc(aColumn.get_Type());
89 #endif
90 
91 	::rtl::OUString sTypeName;
92 	pColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME)) >>= sTypeName;
93 
94 	const OTypeInfoMap* pTypeInfoMap = m_pConnection->getTypeInfo();
95 	::comphelper::TStringMixEqualFunctor aCase(sal_False);
96 	// search for typeinfo where the typename is equal sTypeName
97 	OTypeInfoMap::const_iterator aFind = ::std::find_if(pTypeInfoMap->begin(),
98 														pTypeInfoMap->end(),
99 														::std::compose1(
100 															::std::bind2nd(aCase, sTypeName),
101 															::std::compose1(
102 																::std::mem_fun(&OExtendedTypeInfo::getDBName),
103 																::std::select2nd<OTypeInfoMap::value_type>())
104 															)
105 
106 												);
107 
108 	if ( aFind != pTypeInfoMap->end() ) // change column type if necessary
109 		aColumn.put_Type(aFind->first);
110 
111 	if ( SUCCEEDED(((ADOColumns*)m_aCollection)->Append(OLEVariant(aColumn.get_Name()),aColumn.get_Type(),aColumn.get_DefinedSize())) )
112 	{
113 		WpADOColumn aAddedColumn = m_aCollection.GetItem(OLEVariant(aColumn.get_Name()));
114 		if ( aAddedColumn.IsValid() )
115 		{
116 			sal_Bool bAutoIncrement = sal_False;
117 			pColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISAUTOINCREMENT)) >>= bAutoIncrement;
118 			if ( bAutoIncrement )
119 				OTools::putValue( aAddedColumn.get_Properties(), ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Autoincrement")), bAutoIncrement );
120 
121 			if ( aFind != pTypeInfoMap->end() &&  aColumn.get_Type() != aAddedColumn.get_Type() ) // change column type if necessary
122 				aColumn.put_Type(aFind->first);
123 			aAddedColumn.put_Precision(aColumn.get_Precision());
124 			aAddedColumn.put_NumericScale(aColumn.get_NumericScale());
125 			aAddedColumn.put_Attributes(aColumn.get_Attributes());
126 			aAddedColumn.put_SortOrder(aColumn.get_SortOrder());
127 			aAddedColumn.put_RelatedColumn(aColumn.get_RelatedColumn());
128 		}
129 	}
130 	ADOS::ThrowException(*m_pConnection->getConnection(),static_cast<XTypeProvider*>(this));
131 
132 	return new OAdoColumn(isCaseSensitive(),m_pConnection,pColumn->getColumnImpl());
133 }
134 // -------------------------------------------------------------------------
135 // XDrop
136 void OColumns::dropObject(sal_Int32 /*_nPos*/,const ::rtl::OUString _sElementName)
137 {
138 	if(!m_aCollection.Delete(_sElementName))
139 		ADOS::ThrowException(*m_pConnection->getConnection(),static_cast<XTypeProvider*>(this));
140 }
141 // -----------------------------------------------------------------------------
142 
143 
144 
145