xref: /trunk/main/connectivity/source/drivers/ado/ATable.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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/ATable.hxx"
31*cdf0e10cSrcweir #include "ado/AIndexes.hxx"
32*cdf0e10cSrcweir #include "ado/AColumns.hxx"
33*cdf0e10cSrcweir #include "ado/AColumn.hxx"
34*cdf0e10cSrcweir #include "ado/AKeys.hxx"
35*cdf0e10cSrcweir #include "ado/AConnection.hxx"
36*cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSet.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/sdbcx/KeyType.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/sdbc/KeyRule.hpp>
40*cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
41*cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnValue.hpp>
43*cdf0e10cSrcweir #include "ado/Awrapado.hxx"
44*cdf0e10cSrcweir #include <comphelper/sequence.hxx>
45*cdf0e10cSrcweir #include "TConnection.hxx"
46*cdf0e10cSrcweir #include <comphelper/types.hxx>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir using namespace ::comphelper;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir using namespace connectivity;
51*cdf0e10cSrcweir using namespace connectivity::ado;
52*cdf0e10cSrcweir using namespace com::sun::star::uno;
53*cdf0e10cSrcweir using namespace com::sun::star::lang;
54*cdf0e10cSrcweir using namespace com::sun::star::beans;
55*cdf0e10cSrcweir using namespace com::sun::star::sdbc;
56*cdf0e10cSrcweir using namespace com::sun::star::container;
57*cdf0e10cSrcweir using namespace com::sun::star::lang;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir // -------------------------------------------------------------------------
60*cdf0e10cSrcweir OAdoTable::OAdoTable(sdbcx::OCollection* _pTables,sal_Bool _bCase,OCatalog* _pCatalog,_ADOTable* _pTable)
61*cdf0e10cSrcweir     : OTable_TYPEDEF(_pTables,_bCase,::rtl::OUString(),::rtl::OUString())
62*cdf0e10cSrcweir     ,m_pCatalog(_pCatalog)
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir     construct();
65*cdf0e10cSrcweir     m_aTable = WpADOTable(_pTable);
66*cdf0e10cSrcweir     //  m_aTable.putref_ParentCatalog(_pCatalog->getCatalog());
67*cdf0e10cSrcweir     fillPropertyValues();
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir }
70*cdf0e10cSrcweir // -----------------------------------------------------------------------------
71*cdf0e10cSrcweir OAdoTable::OAdoTable(sdbcx::OCollection* _pTables,sal_Bool _bCase,OCatalog* _pCatalog)
72*cdf0e10cSrcweir     : OTable_TYPEDEF(_pTables,_bCase)
73*cdf0e10cSrcweir     ,m_pCatalog(_pCatalog)
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir     construct();
76*cdf0e10cSrcweir     m_aTable.Create();
77*cdf0e10cSrcweir     m_aTable.putref_ParentCatalog(_pCatalog->getCatalog());
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir }
80*cdf0e10cSrcweir // -----------------------------------------------------------------------------
81*cdf0e10cSrcweir void SAL_CALL OAdoTable::disposing(void)
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir     OTable_TYPEDEF::disposing();
84*cdf0e10cSrcweir     m_aTable.clear();
85*cdf0e10cSrcweir }
86*cdf0e10cSrcweir // -------------------------------------------------------------------------
87*cdf0e10cSrcweir void OAdoTable::refreshColumns()
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir     TStringVector aVector;
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     WpADOColumns aColumns;
92*cdf0e10cSrcweir     if ( m_aTable.IsValid() )
93*cdf0e10cSrcweir     {
94*cdf0e10cSrcweir         aColumns = m_aTable.get_Columns();
95*cdf0e10cSrcweir         aColumns.fillElementNames(aVector);
96*cdf0e10cSrcweir     }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     if(m_pColumns)
99*cdf0e10cSrcweir         m_pColumns->reFill(aVector);
100*cdf0e10cSrcweir     else
101*cdf0e10cSrcweir         m_pColumns = new OColumns(*this,m_aMutex,aVector,aColumns,isCaseSensitive(),m_pCatalog->getConnection());
102*cdf0e10cSrcweir }
103*cdf0e10cSrcweir // -------------------------------------------------------------------------
104*cdf0e10cSrcweir void OAdoTable::refreshKeys()
105*cdf0e10cSrcweir {
106*cdf0e10cSrcweir     TStringVector aVector;
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     WpADOKeys aKeys;
109*cdf0e10cSrcweir     if(m_aTable.IsValid())
110*cdf0e10cSrcweir     {
111*cdf0e10cSrcweir         aKeys = m_aTable.get_Keys();
112*cdf0e10cSrcweir         aKeys.fillElementNames(aVector);
113*cdf0e10cSrcweir     }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     if(m_pKeys)
116*cdf0e10cSrcweir         m_pKeys->reFill(aVector);
117*cdf0e10cSrcweir     else
118*cdf0e10cSrcweir         m_pKeys = new OKeys(*this,m_aMutex,aVector,aKeys,isCaseSensitive(),m_pCatalog->getConnection());
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir // -------------------------------------------------------------------------
121*cdf0e10cSrcweir void OAdoTable::refreshIndexes()
122*cdf0e10cSrcweir {
123*cdf0e10cSrcweir     TStringVector aVector;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     WpADOIndexes aIndexes;
126*cdf0e10cSrcweir     if(m_aTable.IsValid())
127*cdf0e10cSrcweir     {
128*cdf0e10cSrcweir         aIndexes = m_aTable.get_Indexes();
129*cdf0e10cSrcweir         aIndexes.fillElementNames(aVector);
130*cdf0e10cSrcweir     }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir     if(m_pIndexes)
133*cdf0e10cSrcweir         m_pIndexes->reFill(aVector);
134*cdf0e10cSrcweir     else
135*cdf0e10cSrcweir         m_pIndexes = new OIndexes(*this,m_aMutex,aVector,aIndexes,isCaseSensitive(),m_pCatalog->getConnection());
136*cdf0e10cSrcweir }
137*cdf0e10cSrcweir //--------------------------------------------------------------------------
138*cdf0e10cSrcweir Sequence< sal_Int8 > OAdoTable::getUnoTunnelImplementationId()
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir     static ::cppu::OImplementationId * pId = 0;
141*cdf0e10cSrcweir     if (! pId)
142*cdf0e10cSrcweir     {
143*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
144*cdf0e10cSrcweir         if (! pId)
145*cdf0e10cSrcweir         {
146*cdf0e10cSrcweir             static ::cppu::OImplementationId aId;
147*cdf0e10cSrcweir             pId = &aId;
148*cdf0e10cSrcweir         }
149*cdf0e10cSrcweir     }
150*cdf0e10cSrcweir     return pId->getImplementationId();
151*cdf0e10cSrcweir }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir // com::sun::star::lang::XUnoTunnel
154*cdf0e10cSrcweir //------------------------------------------------------------------
155*cdf0e10cSrcweir sal_Int64 OAdoTable::getSomething( const Sequence< sal_Int8 > & rId ) throw (RuntimeException)
156*cdf0e10cSrcweir {
157*cdf0e10cSrcweir     return (rId.getLength() == 16 && 0 == rtl_compareMemory(getUnoTunnelImplementationId().getConstArray(),  rId.getConstArray(), 16 ) )
158*cdf0e10cSrcweir                 ? reinterpret_cast< sal_Int64 >( this )
159*cdf0e10cSrcweir                 : OTable_TYPEDEF::getSomething(rId);
160*cdf0e10cSrcweir }
161*cdf0e10cSrcweir // -------------------------------------------------------------------------
162*cdf0e10cSrcweir // XRename
163*cdf0e10cSrcweir void SAL_CALL OAdoTable::rename( const ::rtl::OUString& newName ) throw(SQLException, ElementExistException, RuntimeException)
164*cdf0e10cSrcweir {
165*cdf0e10cSrcweir     ::osl::MutexGuard aGuard(m_aMutex);
166*cdf0e10cSrcweir     checkDisposed(OTableDescriptor_BASE_TYPEDEF::rBHelper.bDisposed);
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     m_aTable.put_Name(newName);
169*cdf0e10cSrcweir     ADOS::ThrowException(*(m_pCatalog->getConnection()->getConnection()),*this);
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir     OTable_TYPEDEF::rename(newName);
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir // -----------------------------------------------------------------------------
174*cdf0e10cSrcweir Reference< XDatabaseMetaData> OAdoTable::getMetaData() const
175*cdf0e10cSrcweir {
176*cdf0e10cSrcweir     return m_pCatalog->getConnection()->getMetaData();
177*cdf0e10cSrcweir }
178*cdf0e10cSrcweir // -------------------------------------------------------------------------
179*cdf0e10cSrcweir // XAlterTable
180*cdf0e10cSrcweir void SAL_CALL OAdoTable::alterColumnByName( const ::rtl::OUString& colName, const Reference< XPropertySet >& descriptor ) throw(SQLException, NoSuchElementException, RuntimeException)
181*cdf0e10cSrcweir {
182*cdf0e10cSrcweir     ::osl::MutexGuard aGuard(m_aMutex);
183*cdf0e10cSrcweir     checkDisposed(OTableDescriptor_BASE_TYPEDEF::rBHelper.bDisposed);
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir     sal_Bool bError = sal_True;
186*cdf0e10cSrcweir     OAdoColumn* pColumn = NULL;
187*cdf0e10cSrcweir     if(::comphelper::getImplementation(pColumn,descriptor) && pColumn != NULL)
188*cdf0e10cSrcweir     {
189*cdf0e10cSrcweir         WpADOColumns aColumns = m_aTable.get_Columns();
190*cdf0e10cSrcweir         bError = !aColumns.Delete(colName);
191*cdf0e10cSrcweir         bError = bError || !aColumns.Append(pColumn->getColumnImpl());
192*cdf0e10cSrcweir     }
193*cdf0e10cSrcweir     if(bError)
194*cdf0e10cSrcweir         ADOS::ThrowException(*(m_pCatalog->getConnection()->getConnection()),*this);
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir     m_pColumns->refresh();
197*cdf0e10cSrcweir     refreshColumns();
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir // -------------------------------------------------------------------------
200*cdf0e10cSrcweir void SAL_CALL OAdoTable::alterColumnByIndex( sal_Int32 index, const Reference< XPropertySet >& descriptor ) throw(SQLException, ::com::sun::star::lang::IndexOutOfBoundsException, RuntimeException)
201*cdf0e10cSrcweir {
202*cdf0e10cSrcweir     ::osl::MutexGuard aGuard(m_aMutex);
203*cdf0e10cSrcweir     checkDisposed(OTableDescriptor_BASE_TYPEDEF::rBHelper.bDisposed);
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir     Reference< XPropertySet > xOld;
206*cdf0e10cSrcweir     m_pColumns->getByIndex(index) >>= xOld;
207*cdf0e10cSrcweir     if(xOld.is())
208*cdf0e10cSrcweir         alterColumnByName(getString(xOld->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))),descriptor);
209*cdf0e10cSrcweir }
210*cdf0e10cSrcweir // -------------------------------------------------------------------------
211*cdf0e10cSrcweir void OAdoTable::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any& rValue)throw (Exception)
212*cdf0e10cSrcweir {
213*cdf0e10cSrcweir     if(m_aTable.IsValid())
214*cdf0e10cSrcweir     {
215*cdf0e10cSrcweir         switch(nHandle)
216*cdf0e10cSrcweir         {
217*cdf0e10cSrcweir             case PROPERTY_ID_NAME:
218*cdf0e10cSrcweir                 m_aTable.put_Name(getString(rValue));
219*cdf0e10cSrcweir                 break;
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir             case PROPERTY_ID_TYPE:
222*cdf0e10cSrcweir                 OTools::putValue(   m_aTable.get_Properties(),
223*cdf0e10cSrcweir                                 OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE),
224*cdf0e10cSrcweir                                 getString(rValue));
225*cdf0e10cSrcweir                 break;
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir             case PROPERTY_ID_DESCRIPTION:
228*cdf0e10cSrcweir                 OTools::putValue(   m_aTable.get_Properties(),
229*cdf0e10cSrcweir                                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Description")),
230*cdf0e10cSrcweir                                 getString(rValue));
231*cdf0e10cSrcweir                 break;
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir             case PROPERTY_ID_SCHEMANAME:
234*cdf0e10cSrcweir                 break;
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir             default:
237*cdf0e10cSrcweir                                 throw Exception();
238*cdf0e10cSrcweir         }
239*cdf0e10cSrcweir     }
240*cdf0e10cSrcweir     OTable_TYPEDEF::setFastPropertyValue_NoBroadcast(nHandle,rValue);
241*cdf0e10cSrcweir }
242*cdf0e10cSrcweir // -------------------------------------------------------------------------
243*cdf0e10cSrcweir void SAL_CALL OAdoTable::acquire() throw()
244*cdf0e10cSrcweir {
245*cdf0e10cSrcweir     OTable_TYPEDEF::acquire();
246*cdf0e10cSrcweir }
247*cdf0e10cSrcweir // -----------------------------------------------------------------------------
248*cdf0e10cSrcweir void SAL_CALL OAdoTable::release() throw()
249*cdf0e10cSrcweir {
250*cdf0e10cSrcweir     OTable_TYPEDEF::release();
251*cdf0e10cSrcweir }
252*cdf0e10cSrcweir // -----------------------------------------------------------------------------
253*cdf0e10cSrcweir ::rtl::OUString SAL_CALL OAdoTable::getName() throw(::com::sun::star::uno::RuntimeException)
254*cdf0e10cSrcweir {
255*cdf0e10cSrcweir       return m_aTable.get_Name();
256*cdf0e10cSrcweir }
257*cdf0e10cSrcweir 
258