1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: BIndexes.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "adabas/BIndexes.hxx"
34 #include "adabas/BIndex.hxx"
35 #include "adabas/BTable.hxx"
36 #include "connectivity/sdbcx/VIndex.hxx"
37 #include <com/sun/star/sdbc/XRow.hpp>
38 #include <com/sun/star/sdbc/XResultSet.hpp>
39 #include <com/sun/star/sdbc/IndexType.hpp>
40 #include <comphelper/types.hxx>
41 #include <comphelper/types.hxx>
42 #include "adabas/BCatalog.hxx"
43 #include "connectivity/dbexception.hxx"
46 using namespace ::comphelper
;
47 using namespace connectivity
;
48 using namespace connectivity::adabas
;
49 using namespace ::com::sun::star::uno
;
50 using namespace ::com::sun::star::beans
;
51 using namespace ::com::sun::star::sdbcx
;
52 using namespace ::com::sun::star::sdbc
;
53 using namespace ::com::sun::star::container
;
54 using namespace ::com::sun::star::lang
;
55 typedef connectivity::sdbcx::OCollection OCollection_TYPE
;
57 sdbcx::ObjectType
OIndexes::createObject(const ::rtl::OUString
& _rName
)
59 ::rtl::OUString aName
,aQualifier
;
60 sal_Int32 nLen
= _rName
.indexOf('.');
63 aQualifier
= _rName
.copy(0,nLen
);
64 aName
= _rName
.copy(nLen
+1);
70 Reference
< XResultSet
> xResult
= m_pTable
->getMetaData()->getIndexInfo(Any(),
71 m_pTable
->getSchema(),m_pTable
->getTableName(),sal_False
,sal_False
);
73 sdbcx::ObjectType xRet
= NULL
;
76 Reference
< XRow
> xRow(xResult
,UNO_QUERY
);
77 while(xResult
->next())
79 if(xRow
->getString(6) == aName
&& (!aQualifier
.getLength() || xRow
->getString(5) == aQualifier
))
81 OAdabasIndex
* pRet
= new OAdabasIndex(m_pTable
,aName
,aQualifier
,!xRow
->getBoolean(4),
82 aName
== ::rtl::OUString::createFromAscii("SYSPRIMARYKEYINDEX"),
83 xRow
->getShort(7) == IndexType::CLUSTERED
);
88 ::comphelper::disposeComponent(xResult
);
93 // -------------------------------------------------------------------------
94 void OIndexes::impl_refresh() throw(RuntimeException
)
96 m_pTable
->refreshIndexes();
98 // -------------------------------------------------------------------------
99 Reference
< XPropertySet
> OIndexes::createDescriptor()
101 return new OAdabasIndex(m_pTable
);
103 // -------------------------------------------------------------------------
105 sdbcx::ObjectType
OIndexes::appendObject( const ::rtl::OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
107 if ( m_pTable
->isNew() )
108 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider
*>(this));
110 ::rtl::OUString aSql
= ::rtl::OUString::createFromAscii("CREATE ");
111 ::rtl::OUString aQuote
= m_pTable
->getMetaData()->getIdentifierQuoteString( );
112 const ::rtl::OUString
& sDot
= OAdabasCatalog::getDot();
114 if(getBOOL(descriptor
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISUNIQUE
))))
115 aSql
= aSql
+ ::rtl::OUString::createFromAscii("UNIQUE ");
116 aSql
= aSql
+ ::rtl::OUString::createFromAscii("INDEX ");
119 if(_rForName
.getLength())
121 aSql
= aSql
+ aQuote
+ _rForName
+ aQuote
122 + ::rtl::OUString::createFromAscii(" ON ")
123 + aQuote
+ m_pTable
->getSchema() + aQuote
+ sDot
124 + aQuote
+ m_pTable
->getTableName() + aQuote
125 + ::rtl::OUString::createFromAscii(" ( ");
127 Reference
<XColumnsSupplier
> xColumnSup(descriptor
,UNO_QUERY
);
128 Reference
<XIndexAccess
> xColumns(xColumnSup
->getColumns(),UNO_QUERY
);
129 Reference
< XPropertySet
> xColProp
;
130 sal_Int32 nCount
= xColumns
->getCount();
131 for(sal_Int32 i
=0;i
<nCount
;++i
)
133 xColumns
->getByIndex(i
) >>= xColProp
;
134 aSql
= aSql
+ aQuote
+ getString(xColProp
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME
))) + aQuote
;
135 aSql
= aSql
+ (getBOOL(xColProp
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISASCENDING
)))
137 ::rtl::OUString::createFromAscii(" ASC")
139 ::rtl::OUString::createFromAscii(" DESC"))
140 + ::rtl::OUString::createFromAscii(",");
142 aSql
= aSql
.replaceAt(aSql
.getLength()-1,1,::rtl::OUString::createFromAscii(")"));
146 aSql
= aSql
+ aQuote
+ m_pTable
->getSchema() + aQuote
+ sDot
+ aQuote
+ m_pTable
->getTableName() + aQuote
;
148 Reference
<XColumnsSupplier
> xColumnSup(descriptor
,UNO_QUERY
);
149 Reference
<XIndexAccess
> xColumns(xColumnSup
->getColumns(),UNO_QUERY
);
150 Reference
< XPropertySet
> xColProp
;
151 if(xColumns
->getCount() != 1)
152 throw SQLException();
154 xColumns
->getByIndex(0) >>= xColProp
;
156 aSql
= aSql
+ sDot
+ aQuote
+ getString(xColProp
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME
))) + aQuote
;
159 Reference
< XStatement
> xStmt
= m_pTable
->getConnection()->createStatement( );
160 xStmt
->execute(aSql
);
161 ::comphelper::disposeComponent(xStmt
);
163 return createObject( _rForName
);
165 // -------------------------------------------------------------------------
167 void OIndexes::dropObject(sal_Int32
/*_nPos*/,const ::rtl::OUString _sElementName
)
169 if(!m_pTable
->isNew())
171 ::rtl::OUString aName
,aSchema
;
172 sal_Int32 nLen
= _sElementName
.indexOf('.');
173 aSchema
= _sElementName
.copy(0,nLen
);
174 aName
= _sElementName
.copy(nLen
+1);
176 ::rtl::OUString aSql
= ::rtl::OUString::createFromAscii("DROP INDEX ");
177 ::rtl::OUString aQuote
= m_pTable
->getMetaData()->getIdentifierQuoteString( );
178 const ::rtl::OUString
& sDot
= OAdabasCatalog::getDot();
180 if (aSchema
.getLength())
181 (((aSql
+= aQuote
) += aSchema
) += aQuote
) += sDot
;
183 (((aSql
+= aQuote
) += aName
) += aQuote
) += ::rtl::OUString::createFromAscii(" ON ");
185 (((aSql
+= aQuote
) += m_pTable
->getSchema()) += aQuote
) += sDot
;
186 ((aSql
+= aQuote
) += m_pTable
->getTableName()) += aQuote
;
188 Reference
< XStatement
> xStmt
= m_pTable
->getConnection()->createStatement( );
189 xStmt
->execute(aSql
);
190 ::comphelper::disposeComponent(xStmt
);
193 // -------------------------------------------------------------------------