1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "connectivity/TColumnsHelper.hxx"
30 #include "connectivity/sdbcx/VColumn.hxx"
31 #include <com/sun/star/sdbc/XRow.hpp>
32 #include <com/sun/star/sdbc/XResultSet.hpp>
33 #include <com/sun/star/sdbc/DataType.hpp>
34 #include <com/sun/star/sdbc/ColumnValue.hpp>
35 #include <com/sun/star/sdbcx/KeyType.hpp>
36 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
37 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
38 #include <comphelper/types.hxx>
39 #include "connectivity/dbtools.hxx"
40 #include "TConnection.hxx"
41 #include "connectivity/TTableHelper.hxx"
42 #include <comphelper/property.hxx>
44 using namespace ::comphelper
;
47 using namespace connectivity::sdbcx
;
48 using namespace connectivity
;
49 using namespace dbtools
;
50 using namespace ::com::sun::star::uno
;
51 using namespace ::com::sun::star::beans
;
52 using namespace ::com::sun::star::sdbcx
;
53 using namespace ::com::sun::star::sdbc
;
54 using namespace ::com::sun::star::container
;
55 using namespace ::com::sun::star::lang
;
57 namespace connectivity
59 class OColumnsHelperImpl
62 OColumnsHelperImpl(sal_Bool _bCase
)
63 : m_aColumnInfo(_bCase
)
66 ColumnInformationMap m_aColumnInfo
;
70 OColumnsHelper::OColumnsHelper( ::cppu::OWeakObject
& _rParent
72 ,::osl::Mutex
& _rMutex
73 ,const TStringVector
&_rVector
74 ,sal_Bool _bUseHardRef
75 ) : OCollection(_rParent
,_bCase
,_rMutex
,_rVector
,sal_False
,_bUseHardRef
)
80 // -----------------------------------------------------------------------------
81 OColumnsHelper::~OColumnsHelper()
86 // -----------------------------------------------------------------------------
88 sdbcx::ObjectType
OColumnsHelper::createObject(const ::rtl::OUString
& _rName
)
90 OSL_ENSURE(m_pTable
,"NO Table set. Error!");
91 Reference
<XConnection
> xConnection
= m_pTable
->getConnection();
94 m_pImpl
= new OColumnsHelperImpl(isCaseSensitive());
96 sal_Bool bQueryInfo
= sal_True
;
97 sal_Bool bAutoIncrement
= sal_False
;
98 sal_Bool bIsCurrency
= sal_False
;
99 sal_Int32 nDataType
= DataType::OTHER
;
101 ColumnInformationMap::iterator aFind
= m_pImpl
->m_aColumnInfo
.find(_rName
);
102 if ( aFind
== m_pImpl
->m_aColumnInfo
.end() ) // we have to fill it
104 ::rtl::OUString sComposedName
= ::dbtools::composeTableNameForSelect( xConnection
, m_pTable
);
105 collectColumnInformation(xConnection
,sComposedName
,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("*")) ,m_pImpl
->m_aColumnInfo
);
106 aFind
= m_pImpl
->m_aColumnInfo
.find(_rName
);
108 if ( aFind
!= m_pImpl
->m_aColumnInfo
.end() )
110 bQueryInfo
= sal_False
;
111 bAutoIncrement
= aFind
->second
.first
.first
;
112 bIsCurrency
= aFind
->second
.first
.second
;
113 nDataType
= aFind
->second
.second
;
114 } // if ( aFind != m_pImpl->m_aColumnInfo.end() )
116 sdbcx::ObjectType xRet
;
117 const ColumnDesc
* pColDesc
= m_pTable
->getColumnDescription(_rName
);
120 Reference
<XPropertySet
> xPr
= m_pTable
;
121 const Reference
<XNameAccess
> xPrimaryKeyColumns
= getPrimaryKeyColumns_throw(xPr
);
122 sal_Int32 nField11
= pColDesc
->nField11
;
123 if ( nField11
!= ColumnValue::NO_NULLS
&& xPrimaryKeyColumns
.is() && xPrimaryKeyColumns
->hasByName(_rName
) )
125 nField11
= ColumnValue::NO_NULLS
;
126 } // if ( xKeys.is() )
127 connectivity::sdbcx::OColumn
* pRet
= new connectivity::sdbcx::OColumn(_rName
,
145 xRet
.set(::dbtools::createSDBCXColumn( m_pTable
,
152 nDataType
),UNO_QUERY
);
157 // -------------------------------------------------------------------------
158 void OColumnsHelper::impl_refresh() throw(RuntimeException
)
162 m_pImpl
->m_aColumnInfo
.clear();
163 m_pTable
->refreshColumns();
166 // -------------------------------------------------------------------------
167 Reference
< XPropertySet
> OColumnsHelper::createDescriptor()
169 return new OColumn(sal_True
);
171 // -----------------------------------------------------------------------------
173 sdbcx::ObjectType
OColumnsHelper::appendObject( const ::rtl::OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
175 ::osl::MutexGuard
aGuard(m_rMutex
);
176 OSL_ENSURE(m_pTable
,"OColumnsHelper::appendByDescriptor: Table is null!");
177 if ( !m_pTable
|| m_pTable
->isNew() )
178 return cloneDescriptor( descriptor
);
180 Reference
<XDatabaseMetaData
> xMetaData
= m_pTable
->getConnection()->getMetaData();
181 ::rtl::OUString
aSql( RTL_CONSTASCII_USTRINGPARAM( "ALTER TABLE " ));
182 ::rtl::OUString aQuote
= xMetaData
->getIdentifierQuoteString( );
184 aSql
+= ::dbtools::composeTableName( xMetaData
, m_pTable
, ::dbtools::eInTableDefinitions
, false, false, true );
185 aSql
+= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ADD "));
186 aSql
+= ::dbtools::createStandardColumnPart(descriptor
,m_pTable
->getConnection(),NULL
,m_pTable
->getTypeCreatePattern());
188 Reference
< XStatement
> xStmt
= m_pTable
->getConnection()->createStatement( );
191 xStmt
->execute(aSql
);
192 ::comphelper::disposeComponent(xStmt
);
194 return createObject( _rForName
);
196 // -------------------------------------------------------------------------
198 void OColumnsHelper::dropObject(sal_Int32
/*_nPos*/,const ::rtl::OUString _sElementName
)
200 OSL_ENSURE(m_pTable
,"OColumnsHelper::dropByName: Table is null!");
201 if ( m_pTable
&& !m_pTable
->isNew() )
203 ::rtl::OUString
aSql( RTL_CONSTASCII_USTRINGPARAM( "ALTER TABLE " ));
204 Reference
<XDatabaseMetaData
> xMetaData
= m_pTable
->getConnection()->getMetaData();
205 ::rtl::OUString aQuote
= xMetaData
->getIdentifierQuoteString( );
207 aSql
+= ::dbtools::composeTableName( xMetaData
, m_pTable
, ::dbtools::eInTableDefinitions
, false, false, true );
208 aSql
+= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" DROP "));
209 aSql
+= ::dbtools::quoteName( aQuote
,_sElementName
);
211 Reference
< XStatement
> xStmt
= m_pTable
->getConnection()->createStatement( );
214 xStmt
->execute(aSql
);
215 ::comphelper::disposeComponent(xStmt
);
219 // -----------------------------------------------------------------------------
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */