1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 #include "ado/ATable.hxx"
31 #include "ado/AIndexes.hxx"
32 #include "ado/AColumns.hxx"
33 #include "ado/AColumn.hxx"
34 #include "ado/AKeys.hxx"
35 #include "ado/AConnection.hxx"
36 #include <com/sun/star/sdbc/XRow.hpp>
37 #include <com/sun/star/sdbc/XResultSet.hpp>
38 #include <com/sun/star/sdbcx/KeyType.hpp>
39 #include <com/sun/star/sdbc/KeyRule.hpp>
40 #include <cppuhelper/typeprovider.hxx>
41 #include <com/sun/star/lang/DisposedException.hpp>
42 #include <com/sun/star/sdbc/ColumnValue.hpp>
43 #include "ado/Awrapado.hxx"
44 #include <comphelper/sequence.hxx>
45 #include "TConnection.hxx"
46 #include <comphelper/types.hxx>
47 
48 using namespace ::comphelper;
49 
50 using namespace connectivity;
51 using namespace connectivity::ado;
52 using namespace com::sun::star::uno;
53 using namespace com::sun::star::lang;
54 using namespace com::sun::star::beans;
55 using namespace com::sun::star::sdbc;
56 using namespace com::sun::star::container;
57 using namespace com::sun::star::lang;
58 
59 // -------------------------------------------------------------------------
60 OAdoTable::OAdoTable(sdbcx::OCollection* _pTables,sal_Bool _bCase,OCatalog* _pCatalog,_ADOTable* _pTable)
61 	: OTable_TYPEDEF(_pTables,_bCase,::rtl::OUString(),::rtl::OUString())
62 	,m_pCatalog(_pCatalog)
63 {
64 	construct();
65 	m_aTable = WpADOTable(_pTable);
66 	//	m_aTable.putref_ParentCatalog(_pCatalog->getCatalog());
67 	fillPropertyValues();
68 
69 }
70 // -----------------------------------------------------------------------------
71 OAdoTable::OAdoTable(sdbcx::OCollection* _pTables,sal_Bool _bCase,OCatalog* _pCatalog)
72 	: OTable_TYPEDEF(_pTables,_bCase)
73 	,m_pCatalog(_pCatalog)
74 {
75 	construct();
76 	m_aTable.Create();
77 	m_aTable.putref_ParentCatalog(_pCatalog->getCatalog());
78 
79 }
80 // -----------------------------------------------------------------------------
81 void SAL_CALL OAdoTable::disposing(void)
82 {
83 	OTable_TYPEDEF::disposing();
84 	m_aTable.clear();
85 }
86 // -------------------------------------------------------------------------
87 void OAdoTable::refreshColumns()
88 {
89 	TStringVector aVector;
90 
91 	WpADOColumns aColumns;
92 	if ( m_aTable.IsValid() )
93 	{
94 		aColumns = m_aTable.get_Columns();
95 		aColumns.fillElementNames(aVector);
96 	}
97 
98 	if(m_pColumns)
99 		m_pColumns->reFill(aVector);
100 	else
101 		m_pColumns = new OColumns(*this,m_aMutex,aVector,aColumns,isCaseSensitive(),m_pCatalog->getConnection());
102 }
103 // -------------------------------------------------------------------------
104 void OAdoTable::refreshKeys()
105 {
106 	TStringVector aVector;
107 
108 	WpADOKeys aKeys;
109 	if(m_aTable.IsValid())
110 	{
111 		aKeys = m_aTable.get_Keys();
112 		aKeys.fillElementNames(aVector);
113 	}
114 
115 	if(m_pKeys)
116 		m_pKeys->reFill(aVector);
117 	else
118 		m_pKeys = new OKeys(*this,m_aMutex,aVector,aKeys,isCaseSensitive(),m_pCatalog->getConnection());
119 }
120 // -------------------------------------------------------------------------
121 void OAdoTable::refreshIndexes()
122 {
123 	TStringVector aVector;
124 
125 	WpADOIndexes aIndexes;
126 	if(m_aTable.IsValid())
127 	{
128 		aIndexes = m_aTable.get_Indexes();
129 		aIndexes.fillElementNames(aVector);
130 	}
131 
132 	if(m_pIndexes)
133 		m_pIndexes->reFill(aVector);
134 	else
135 		m_pIndexes = new OIndexes(*this,m_aMutex,aVector,aIndexes,isCaseSensitive(),m_pCatalog->getConnection());
136 }
137 //--------------------------------------------------------------------------
138 Sequence< sal_Int8 > OAdoTable::getUnoTunnelImplementationId()
139 {
140 	static ::cppu::OImplementationId * pId = 0;
141 	if (! pId)
142 	{
143 		::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
144 		if (! pId)
145 		{
146 			static ::cppu::OImplementationId aId;
147 			pId = &aId;
148 		}
149 	}
150 	return pId->getImplementationId();
151 }
152 
153 // com::sun::star::lang::XUnoTunnel
154 //------------------------------------------------------------------
155 sal_Int64 OAdoTable::getSomething( const Sequence< sal_Int8 > & rId ) throw (RuntimeException)
156 {
157 	return (rId.getLength() == 16 && 0 == rtl_compareMemory(getUnoTunnelImplementationId().getConstArray(),  rId.getConstArray(), 16 ) )
158 				? reinterpret_cast< sal_Int64 >( this )
159 				: OTable_TYPEDEF::getSomething(rId);
160 }
161 // -------------------------------------------------------------------------
162 // XRename
163 void SAL_CALL OAdoTable::rename( const ::rtl::OUString& newName ) throw(SQLException, ElementExistException, RuntimeException)
164 {
165 	::osl::MutexGuard aGuard(m_aMutex);
166 	checkDisposed(OTableDescriptor_BASE_TYPEDEF::rBHelper.bDisposed);
167 
168 	m_aTable.put_Name(newName);
169 	ADOS::ThrowException(*(m_pCatalog->getConnection()->getConnection()),*this);
170 
171 	OTable_TYPEDEF::rename(newName);
172 }
173 // -----------------------------------------------------------------------------
174 Reference< XDatabaseMetaData> OAdoTable::getMetaData() const
175 {
176 	return m_pCatalog->getConnection()->getMetaData();
177 }
178 // -------------------------------------------------------------------------
179 // XAlterTable
180 void SAL_CALL OAdoTable::alterColumnByName( const ::rtl::OUString& colName, const Reference< XPropertySet >& descriptor ) throw(SQLException, NoSuchElementException, RuntimeException)
181 {
182 	::osl::MutexGuard aGuard(m_aMutex);
183 	checkDisposed(OTableDescriptor_BASE_TYPEDEF::rBHelper.bDisposed);
184 
185 	sal_Bool bError = sal_True;
186 	OAdoColumn* pColumn = NULL;
187     if(::comphelper::getImplementation(pColumn,descriptor) && pColumn != NULL)
188 	{
189 		WpADOColumns aColumns = m_aTable.get_Columns();
190 		bError = !aColumns.Delete(colName);
191 		bError = bError || !aColumns.Append(pColumn->getColumnImpl());
192 	}
193 	if(bError)
194 		ADOS::ThrowException(*(m_pCatalog->getConnection()->getConnection()),*this);
195 
196 	m_pColumns->refresh();
197 	refreshColumns();
198 }
199 // -------------------------------------------------------------------------
200 void SAL_CALL OAdoTable::alterColumnByIndex( sal_Int32 index, const Reference< XPropertySet >& descriptor ) throw(SQLException, ::com::sun::star::lang::IndexOutOfBoundsException, RuntimeException)
201 {
202 	::osl::MutexGuard aGuard(m_aMutex);
203 	checkDisposed(OTableDescriptor_BASE_TYPEDEF::rBHelper.bDisposed);
204 
205 	Reference< XPropertySet > xOld;
206 	m_pColumns->getByIndex(index) >>= xOld;
207 	if(xOld.is())
208 		alterColumnByName(getString(xOld->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))),descriptor);
209 }
210 // -------------------------------------------------------------------------
211 void OAdoTable::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any& rValue)throw (Exception)
212 {
213 	if(m_aTable.IsValid())
214 	{
215 		switch(nHandle)
216 		{
217 			case PROPERTY_ID_NAME:
218 				m_aTable.put_Name(getString(rValue));
219 				break;
220 
221 			case PROPERTY_ID_TYPE:
222 				OTools::putValue(	m_aTable.get_Properties(),
223 								OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE),
224 								getString(rValue));
225 				break;
226 
227 			case PROPERTY_ID_DESCRIPTION:
228 				OTools::putValue(	m_aTable.get_Properties(),
229 								::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Description")),
230 								getString(rValue));
231 				break;
232 
233 			case PROPERTY_ID_SCHEMANAME:
234 				break;
235 
236 			default:
237                                 throw Exception();
238 		}
239 	}
240 	OTable_TYPEDEF::setFastPropertyValue_NoBroadcast(nHandle,rValue);
241 }
242 // -------------------------------------------------------------------------
243 void SAL_CALL OAdoTable::acquire() throw()
244 {
245 	OTable_TYPEDEF::acquire();
246 }
247 // -----------------------------------------------------------------------------
248 void SAL_CALL OAdoTable::release() throw()
249 {
250 	OTable_TYPEDEF::release();
251 }
252 // -----------------------------------------------------------------------------
253 ::rtl::OUString SAL_CALL OAdoTable::getName() throw(::com::sun::star::uno::RuntimeException)
254 {
255       return m_aTable.get_Name();
256 }
257 
258