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: BColumns.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/BColumns.hxx"
34 #include "connectivity/sdbcx/VColumn.hxx"
35 #include "connectivity/sdbcx/VColumn.hxx"
36 #include <com/sun/star/sdbc/XRow.hpp>
37 #include <com/sun/star/sdbc/XResultSet.hpp>
38 #include <com/sun/star/sdbc/DataType.hpp>
39 #include <com/sun/star/sdbc/ColumnValue.hpp>
40 #include "adabas/BTable.hxx"
41 #include "adabas/BTables.hxx"
42 #include "adabas/BCatalog.hxx"
43 #include <comphelper/types.hxx>
44 #include "connectivity/dbtools.hxx"
45 #include <comphelper/property.hxx>
47 using namespace ::comphelper
;
50 using namespace connectivity::adabas
;
51 using namespace connectivity::sdbcx
;
52 using namespace connectivity
;
53 using namespace ::com::sun::star::uno
;
54 using namespace ::com::sun::star::beans
;
55 // using namespace ::com::sun::star::sdbcx;
56 using namespace ::com::sun::star::sdbc
;
57 using namespace ::com::sun::star::container
;
58 using namespace ::com::sun::star::lang
;
59 typedef connectivity::sdbcx::OCollection OCollection_TYPE
;
61 sdbcx::ObjectType
OColumns::createObject(const ::rtl::OUString
& _rName
)
63 Reference
< XResultSet
> xResult
= m_pTable
->getMetaData()->getColumns(Any(),
64 m_pTable
->getSchema(),m_pTable
->getTableName(),_rName
);
66 sdbcx::ObjectType xRet
= NULL
;
69 Reference
< XRow
> xRow(xResult
,UNO_QUERY
);
70 while(xResult
->next())
72 if(xRow
->getString(4) == _rName
)
74 sal_Int32 nType
= xRow
->getInt(5);
75 ::rtl::OUString sTypeName
= xRow
->getString(6);
76 sal_Int32 nPrec
= xRow
->getInt(7);
77 OAdabasCatalog::correctColumnProperties(nPrec
,nType
,sTypeName
);
78 sal_Bool bAutoIncrement
= sal_False
;
79 if ( !_rName
.equalsAscii("DEFAULT") && !m_pTable
->getSchema().equalsAscii("DOMAIN") && !m_pTable
->getTableName().equalsAscii("COLUMNS") )
81 Reference
< XStatement
> xStmt
= m_pTable
->getMetaData()->getConnection()->createStatement( );
82 ::rtl::OUString
sQuery(RTL_CONSTASCII_USTRINGPARAM("SELECT \"DEFAULT\" FROM DOMAIN.COLUMNS WHERE OWNER = '"));
83 sQuery
+= m_pTable
->getSchema();
84 sQuery
+= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("' AND TABLENAME = '"));
85 sQuery
+= m_pTable
->getTableName() + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("' AND COLUMNNAME = '"));
86 sQuery
+= _rName
+ ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("'"));
89 Reference
< XResultSet
> xResult2
= xStmt
->executeQuery(sQuery
);
90 Reference
< XRow
> xRow2(xResult2
,UNO_QUERY
);
91 if ( xRow2
.is() && xResult2
->next() )
92 bAutoIncrement
= xRow2
->getString(1) == ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DEFAULT STAMP"));
94 catch(const Exception
&)
99 xRet
= new OColumn(_rName
,
106 bAutoIncrement
,sal_False
,sal_False
,sal_True
);
110 ::comphelper::disposeComponent(xResult
);
116 // -------------------------------------------------------------------------
117 void OColumns::impl_refresh() throw(RuntimeException
)
119 m_pTable
->refreshColumns();
121 // -------------------------------------------------------------------------
122 Reference
< XPropertySet
> OColumns::createDescriptor()
124 return new OColumn(sal_True
);
126 // -------------------------------------------------------------------------
128 sdbcx::ObjectType
OColumns::appendObject( const ::rtl::OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
130 ::osl::MutexGuard
aGuard(m_rMutex
);
131 if ( m_pTable
->isNew() )
132 return cloneDescriptor( descriptor
);
134 ::rtl::OUString
aSql(RTL_CONSTASCII_USTRINGPARAM("ALTER TABLE "));
135 ::rtl::OUString sQuote
= m_pTable
->getMetaData()->getIdentifierQuoteString( );
136 const ::rtl::OUString
& sDot
= OAdabasCatalog::getDot();
138 m_pTable
->beginTransAction();
141 aSql
+= ::dbtools::quoteName(sQuote
,m_pTable
->getSchema()) + sDot
+ ::dbtools::quoteName(sQuote
,m_pTable
->getTableName());
142 aSql
+= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ADD ("));
143 aSql
+= ::dbtools::quoteName(sQuote
,_rForName
);
144 aSql
+= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" "));
145 aSql
+= OTables::getColumnSqlType(descriptor
);
146 aSql
+= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" )"));
148 Reference
< XStatement
> xStmt
= m_pTable
->getConnection()->createStatement();
149 xStmt
->execute(aSql
);
150 ::comphelper::disposeComponent(xStmt
);
152 m_pTable
->alterNotNullValue(getINT32(descriptor
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE
))),_rForName
);
154 catch(const Exception
&)
156 m_pTable
->rollbackTransAction();
159 m_pTable
->endTransAction();
161 return createObject( _rForName
);
163 // -------------------------------------------------------------------------
165 void OColumns::dropObject(sal_Int32
/*_nPos*/,const ::rtl::OUString _sElementName
)
167 OSL_ENSURE(m_pTable
,"OColumns::dropByName: Table is null!");
168 if(!m_pTable
->isNew())
170 ::rtl::OUString
aSql(RTL_CONSTASCII_USTRINGPARAM("ALTER TABLE "));
171 ::rtl::OUString sQuote
= m_pTable
->getMetaData()->getIdentifierQuoteString( );
172 const ::rtl::OUString
& sDot
= OAdabasCatalog::getDot();
174 aSql
+= ::dbtools::quoteName(sQuote
,m_pTable
->getSchema()) + sDot
+ ::dbtools::quoteName(sQuote
,m_pTable
->getTableName());
175 aSql
+= ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" DROP "));
176 aSql
+= ::dbtools::quoteName(sQuote
,_sElementName
);
178 Reference
< XStatement
> xStmt
= m_pTable
->getConnection()->createStatement( );
179 xStmt
->execute(aSql
);
180 ::comphelper::disposeComponent(xStmt
);
183 // -----------------------------------------------------------------------------