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