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 "connectivity/TColumnsHelper.hxx"
27 #include "connectivity/sdbcx/VColumn.hxx"
28 #include "connectivity/sdbcx/VColumn.hxx"
29 #include <com/sun/star/sdbc/XRow.hpp>
30 #include <com/sun/star/sdbc/XResultSet.hpp>
31 #include <com/sun/star/sdbc/DataType.hpp>
32 #include <com/sun/star/sdbc/ColumnValue.hpp>
33 #include <com/sun/star/sdbcx/KeyType.hpp>
34 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
35 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
36 #include <comphelper/types.hxx>
37 #include "connectivity/dbtools.hxx"
38 #include "TConnection.hxx"
39 #include "connectivity/TTableHelper.hxx"
40 #include <comphelper/property.hxx>
41 
42 using namespace ::comphelper;
43 
44 
45 using namespace connectivity::sdbcx;
46 using namespace connectivity;
47 using namespace dbtools;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::beans;
50 using namespace ::com::sun::star::sdbcx;
51 using namespace ::com::sun::star::sdbc;
52 using namespace ::com::sun::star::container;
53 using namespace ::com::sun::star::lang;
54 typedef connectivity::sdbcx::OCollection OCollection_TYPE;
55 
56 namespace connectivity
57 {
58 	class OColumnsHelperImpl
59 	{
60 	public:
OColumnsHelperImpl(sal_Bool _bCase)61 		OColumnsHelperImpl(sal_Bool _bCase)
62 			: m_aColumnInfo(_bCase)
63 		{
64 		}
65 		ColumnInformationMap m_aColumnInfo;
66 	};
67 }
68 
OColumnsHelper(::cppu::OWeakObject & _rParent,sal_Bool _bCase,::osl::Mutex & _rMutex,const TStringVector & _rVector,sal_Bool _bUseHardRef)69 OColumnsHelper::OColumnsHelper(	::cppu::OWeakObject& _rParent
70 								,sal_Bool _bCase
71 								,::osl::Mutex& _rMutex
72 								,const TStringVector &_rVector
73 								,sal_Bool _bUseHardRef
74 			) : OCollection(_rParent,_bCase,_rMutex,_rVector,sal_False,_bUseHardRef)
75 	,m_pImpl(NULL)
76 	,m_pTable(NULL)
77 {
78 }
79 // -----------------------------------------------------------------------------
~OColumnsHelper()80 OColumnsHelper::~OColumnsHelper()
81 {
82 	delete m_pImpl;
83 	m_pImpl = NULL;
84 }
85 // -----------------------------------------------------------------------------
86 
createObject(const::rtl::OUString & _rName)87 sdbcx::ObjectType OColumnsHelper::createObject(const ::rtl::OUString& _rName)
88 {
89 	OSL_ENSURE(m_pTable,"NO Table set. Error!");
90 	Reference<XConnection> xConnection = m_pTable->getConnection();
91 
92 	if ( !m_pImpl )
93 		m_pImpl = new OColumnsHelperImpl(isCaseSensitive());
94 
95 	sal_Bool bQueryInfo		= sal_True;
96 	sal_Bool bAutoIncrement = sal_False;
97 	sal_Bool bIsCurrency	= sal_False;
98 	sal_Int32 nDataType		= DataType::OTHER;
99 
100 	ColumnInformationMap::iterator aFind = m_pImpl->m_aColumnInfo.find(_rName);
101 	if ( aFind == m_pImpl->m_aColumnInfo.end() ) // we have to fill it
102 	{
103         ::rtl::OUString sComposedName = ::dbtools::composeTableNameForSelect( xConnection, m_pTable );
104 		collectColumnInformation(xConnection,sComposedName,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("*")) ,m_pImpl->m_aColumnInfo);
105 		aFind = m_pImpl->m_aColumnInfo.find(_rName);
106 	}
107 	if ( aFind != m_pImpl->m_aColumnInfo.end() )
108 	{
109 		bQueryInfo		= sal_False;
110 		bAutoIncrement	= aFind->second.first.first;
111 		bIsCurrency		= aFind->second.first.second;
112 		nDataType		= aFind->second.second;
113 	} // if ( aFind != m_pImpl->m_aColumnInfo.end() )
114 
115     sdbcx::ObjectType xRet;
116     const ColumnDesc* pColDesc = m_pTable->getColumnDescription(_rName);
117     if ( pColDesc )
118     {
119         Reference<XPropertySet> xPr = m_pTable;
120         const Reference<XNameAccess> xPrimaryKeyColumns = getPrimaryKeyColumns_throw(xPr);
121         sal_Int32 nField11 = pColDesc->nField11;
122         if ( nField11 != ColumnValue::NO_NULLS && xPrimaryKeyColumns.is() && xPrimaryKeyColumns->hasByName(_rName) )
123         {
124             nField11 = ColumnValue::NO_NULLS;
125         } // if ( xKeys.is() )
126         connectivity::sdbcx::OColumn* pRet = new connectivity::sdbcx::OColumn(_rName,
127 												pColDesc->aField6,
128 												pColDesc->sField13,
129                                                 pColDesc->sField12,
130 												nField11,
131 												pColDesc->nField7,
132 												pColDesc->nField9,
133 												pColDesc->nField5,
134 												bAutoIncrement,
135 												sal_False,
136 												bIsCurrency,
137 												isCaseSensitive());
138 
139 		xRet = pRet;
140     }
141     else
142     {
143 
144 	    xRet.set(::dbtools::createSDBCXColumn(	m_pTable,
145 											    xConnection,
146 											    _rName,
147 											    isCaseSensitive(),
148 											    bQueryInfo,
149 											    bAutoIncrement,
150 											    bIsCurrency,
151 											    nDataType),UNO_QUERY);
152     }
153 	return xRet;
154 }
155 
156 // -------------------------------------------------------------------------
impl_refresh()157 void OColumnsHelper::impl_refresh() throw(RuntimeException)
158 {
159 	if ( m_pTable )
160 	{
161 		m_pImpl->m_aColumnInfo.clear();
162 		m_pTable->refreshColumns();
163 	}
164 }
165 // -------------------------------------------------------------------------
createDescriptor()166 Reference< XPropertySet > OColumnsHelper::createDescriptor()
167 {
168 	return new OColumn(sal_True);
169 }
170 // -----------------------------------------------------------------------------
171 // XAppend
appendObject(const::rtl::OUString & _rForName,const Reference<XPropertySet> & descriptor)172 sdbcx::ObjectType OColumnsHelper::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
173 {
174 	::osl::MutexGuard aGuard(m_rMutex);
175 	OSL_ENSURE(m_pTable,"OColumnsHelper::appendByDescriptor: Table is null!");
176 	if ( !m_pTable || m_pTable->isNew() )
177         return cloneDescriptor( descriptor );
178 
179     Reference<XDatabaseMetaData> xMetaData = m_pTable->getConnection()->getMetaData();
180 	::rtl::OUString aSql	= ::rtl::OUString::createFromAscii("ALTER TABLE ");
181 	::rtl::OUString aQuote	= xMetaData->getIdentifierQuoteString(  );
182 
183 	aSql += ::dbtools::composeTableName( xMetaData, m_pTable, ::dbtools::eInTableDefinitions, false, false, true );
184 	aSql += ::rtl::OUString::createFromAscii(" ADD ");
185 	aSql += ::dbtools::createStandardColumnPart(descriptor,m_pTable->getConnection(),NULL,m_pTable->getTypeCreatePattern());
186 
187 	Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement(  );
188 	if ( xStmt.is() )
189 	{
190 		xStmt->execute(aSql);
191 		::comphelper::disposeComponent(xStmt);
192 	}
193     return createObject( _rForName );
194 }
195 // -------------------------------------------------------------------------
196 // XDrop
dropObject(sal_Int32,const::rtl::OUString _sElementName)197 void OColumnsHelper::dropObject(sal_Int32 /*_nPos*/,const ::rtl::OUString _sElementName)
198 {
199 	OSL_ENSURE(m_pTable,"OColumnsHelper::dropByName: Table is null!");
200 	if ( m_pTable && !m_pTable->isNew() )
201 	{
202 		::rtl::OUString aSql	= ::rtl::OUString::createFromAscii("ALTER TABLE ");
203 		Reference<XDatabaseMetaData> xMetaData = m_pTable->getConnection()->getMetaData();
204 		::rtl::OUString aQuote	= xMetaData->getIdentifierQuoteString(  );
205 
206 		aSql += ::dbtools::composeTableName( xMetaData, m_pTable, ::dbtools::eInTableDefinitions, false, false, true );
207 		aSql += ::rtl::OUString::createFromAscii(" DROP ");
208 		aSql += ::dbtools::quoteName( aQuote,_sElementName);
209 
210         Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement(  );
211 		if ( xStmt.is() )
212 		{
213 			xStmt->execute(aSql);
214 			::comphelper::disposeComponent(xStmt);
215 		}
216 	}
217 }
218 // -----------------------------------------------------------------------------
219 
220 
221 
222