1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*9b5730f6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*9b5730f6SAndrew Rist * distributed with this work for additional information
6*9b5730f6SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*9b5730f6SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist * with the License. You may obtain a copy of the License at
10*9b5730f6SAndrew Rist *
11*9b5730f6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*9b5730f6SAndrew Rist *
13*9b5730f6SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist * KIND, either express or implied. See the License for the
17*9b5730f6SAndrew Rist * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist * under the License.
19*9b5730f6SAndrew Rist *
20*9b5730f6SAndrew Rist *************************************************************/
21*9b5730f6SAndrew Rist
22*9b5730f6SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir #include "adabas/BColumns.hxx"
27cdf0e10cSrcweir #include "connectivity/sdbcx/VColumn.hxx"
28cdf0e10cSrcweir #include "connectivity/sdbcx/VColumn.hxx"
29cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp>
30cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSet.hpp>
31cdf0e10cSrcweir #include <com/sun/star/sdbc/DataType.hpp>
32cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnValue.hpp>
33cdf0e10cSrcweir #include "adabas/BTable.hxx"
34cdf0e10cSrcweir #include "adabas/BTables.hxx"
35cdf0e10cSrcweir #include "adabas/BCatalog.hxx"
36cdf0e10cSrcweir #include <comphelper/types.hxx>
37cdf0e10cSrcweir #include "connectivity/dbtools.hxx"
38cdf0e10cSrcweir #include <comphelper/property.hxx>
39cdf0e10cSrcweir
40cdf0e10cSrcweir using namespace ::comphelper;
41cdf0e10cSrcweir
42cdf0e10cSrcweir
43cdf0e10cSrcweir using namespace connectivity::adabas;
44cdf0e10cSrcweir using namespace connectivity::sdbcx;
45cdf0e10cSrcweir using namespace connectivity;
46cdf0e10cSrcweir using namespace ::com::sun::star::uno;
47cdf0e10cSrcweir using namespace ::com::sun::star::beans;
48cdf0e10cSrcweir // using namespace ::com::sun::star::sdbcx;
49cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
50cdf0e10cSrcweir using namespace ::com::sun::star::container;
51cdf0e10cSrcweir using namespace ::com::sun::star::lang;
52cdf0e10cSrcweir typedef connectivity::sdbcx::OCollection OCollection_TYPE;
53cdf0e10cSrcweir
createObject(const::rtl::OUString & _rName)54cdf0e10cSrcweir sdbcx::ObjectType OColumns::createObject(const ::rtl::OUString& _rName)
55cdf0e10cSrcweir {
56cdf0e10cSrcweir Reference< XResultSet > xResult = m_pTable->getMetaData()->getColumns(Any(),
57cdf0e10cSrcweir m_pTable->getSchema(),m_pTable->getTableName(),_rName);
58cdf0e10cSrcweir
59cdf0e10cSrcweir sdbcx::ObjectType xRet = NULL;
60cdf0e10cSrcweir if(xResult.is())
61cdf0e10cSrcweir {
62cdf0e10cSrcweir Reference< XRow > xRow(xResult,UNO_QUERY);
63cdf0e10cSrcweir while(xResult->next())
64cdf0e10cSrcweir {
65cdf0e10cSrcweir if(xRow->getString(4) == _rName)
66cdf0e10cSrcweir {
67cdf0e10cSrcweir sal_Int32 nType = xRow->getInt(5);
68cdf0e10cSrcweir ::rtl::OUString sTypeName = xRow->getString(6);
69cdf0e10cSrcweir sal_Int32 nPrec = xRow->getInt(7);
70cdf0e10cSrcweir OAdabasCatalog::correctColumnProperties(nPrec,nType,sTypeName);
71cdf0e10cSrcweir sal_Bool bAutoIncrement = sal_False;
72cdf0e10cSrcweir if ( !_rName.equalsAscii("DEFAULT") && !m_pTable->getSchema().equalsAscii("DOMAIN") && !m_pTable->getTableName().equalsAscii("COLUMNS") )
73cdf0e10cSrcweir {
74cdf0e10cSrcweir Reference< XStatement > xStmt = m_pTable->getMetaData()->getConnection()->createStatement( );
75cdf0e10cSrcweir ::rtl::OUString sQuery(RTL_CONSTASCII_USTRINGPARAM("SELECT \"DEFAULT\" FROM DOMAIN.COLUMNS WHERE OWNER = '"));
76cdf0e10cSrcweir sQuery += m_pTable->getSchema();
77cdf0e10cSrcweir sQuery += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("' AND TABLENAME = '"));
78cdf0e10cSrcweir sQuery += m_pTable->getTableName() + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("' AND COLUMNNAME = '"));
79cdf0e10cSrcweir sQuery += _rName + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("'"));
80cdf0e10cSrcweir try
81cdf0e10cSrcweir {
82cdf0e10cSrcweir Reference< XResultSet > xResult2 = xStmt->executeQuery(sQuery);
83cdf0e10cSrcweir Reference< XRow > xRow2(xResult2,UNO_QUERY);
84cdf0e10cSrcweir if ( xRow2.is() && xResult2->next() )
85cdf0e10cSrcweir bAutoIncrement = xRow2->getString(1) == ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DEFAULT STAMP"));
86cdf0e10cSrcweir }
87cdf0e10cSrcweir catch(const Exception&)
88cdf0e10cSrcweir {
89cdf0e10cSrcweir }
90cdf0e10cSrcweir }
91cdf0e10cSrcweir
92cdf0e10cSrcweir xRet = new OColumn(_rName,
93cdf0e10cSrcweir sTypeName,
94cdf0e10cSrcweir xRow->getString(13),
95cdf0e10cSrcweir xRow->getString(12),
96cdf0e10cSrcweir xRow->getInt(11),
97cdf0e10cSrcweir nPrec,
98cdf0e10cSrcweir xRow->getInt(9),
99cdf0e10cSrcweir nType,
100cdf0e10cSrcweir bAutoIncrement,sal_False,sal_False,sal_True);
101cdf0e10cSrcweir break;
102cdf0e10cSrcweir }
103cdf0e10cSrcweir }
104cdf0e10cSrcweir ::comphelper::disposeComponent(xResult);
105cdf0e10cSrcweir }
106cdf0e10cSrcweir
107cdf0e10cSrcweir return xRet;
108cdf0e10cSrcweir }
109cdf0e10cSrcweir
110cdf0e10cSrcweir // -------------------------------------------------------------------------
impl_refresh()111cdf0e10cSrcweir void OColumns::impl_refresh() throw(RuntimeException)
112cdf0e10cSrcweir {
113cdf0e10cSrcweir m_pTable->refreshColumns();
114cdf0e10cSrcweir }
115cdf0e10cSrcweir // -------------------------------------------------------------------------
createDescriptor()116cdf0e10cSrcweir Reference< XPropertySet > OColumns::createDescriptor()
117cdf0e10cSrcweir {
118cdf0e10cSrcweir return new OColumn(sal_True);
119cdf0e10cSrcweir }
120cdf0e10cSrcweir // -------------------------------------------------------------------------
121cdf0e10cSrcweir // XAppend
appendObject(const::rtl::OUString & _rForName,const Reference<XPropertySet> & descriptor)122cdf0e10cSrcweir sdbcx::ObjectType OColumns::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
123cdf0e10cSrcweir {
124cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_rMutex);
125cdf0e10cSrcweir if ( m_pTable->isNew() )
126cdf0e10cSrcweir return cloneDescriptor( descriptor );
127cdf0e10cSrcweir
128cdf0e10cSrcweir ::rtl::OUString aSql(RTL_CONSTASCII_USTRINGPARAM("ALTER TABLE "));
129cdf0e10cSrcweir ::rtl::OUString sQuote = m_pTable->getMetaData()->getIdentifierQuoteString( );
130cdf0e10cSrcweir const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
131cdf0e10cSrcweir
132cdf0e10cSrcweir m_pTable->beginTransAction();
133cdf0e10cSrcweir try
134cdf0e10cSrcweir {
135cdf0e10cSrcweir aSql += ::dbtools::quoteName(sQuote,m_pTable->getSchema()) + sDot + ::dbtools::quoteName(sQuote,m_pTable->getTableName());
136cdf0e10cSrcweir aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ADD ("));
137cdf0e10cSrcweir aSql += ::dbtools::quoteName(sQuote,_rForName);
138cdf0e10cSrcweir aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" "));
139cdf0e10cSrcweir aSql += OTables::getColumnSqlType(descriptor);
140cdf0e10cSrcweir aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" )"));
141cdf0e10cSrcweir
142cdf0e10cSrcweir Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement();
143cdf0e10cSrcweir xStmt->execute(aSql);
144cdf0e10cSrcweir ::comphelper::disposeComponent(xStmt);
145cdf0e10cSrcweir
146cdf0e10cSrcweir m_pTable->alterNotNullValue(getINT32(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE))),_rForName);
147cdf0e10cSrcweir }
148cdf0e10cSrcweir catch(const Exception&)
149cdf0e10cSrcweir {
150cdf0e10cSrcweir m_pTable->rollbackTransAction();
151cdf0e10cSrcweir throw;
152cdf0e10cSrcweir }
153cdf0e10cSrcweir m_pTable->endTransAction();
154cdf0e10cSrcweir
155cdf0e10cSrcweir return createObject( _rForName );
156cdf0e10cSrcweir }
157cdf0e10cSrcweir // -------------------------------------------------------------------------
158cdf0e10cSrcweir // XDrop
dropObject(sal_Int32,const::rtl::OUString _sElementName)159cdf0e10cSrcweir void OColumns::dropObject(sal_Int32 /*_nPos*/,const ::rtl::OUString _sElementName)
160cdf0e10cSrcweir {
161cdf0e10cSrcweir OSL_ENSURE(m_pTable,"OColumns::dropByName: Table is null!");
162cdf0e10cSrcweir if(!m_pTable->isNew())
163cdf0e10cSrcweir {
164cdf0e10cSrcweir ::rtl::OUString aSql(RTL_CONSTASCII_USTRINGPARAM("ALTER TABLE "));
165cdf0e10cSrcweir ::rtl::OUString sQuote = m_pTable->getMetaData()->getIdentifierQuoteString( );
166cdf0e10cSrcweir const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
167cdf0e10cSrcweir
168cdf0e10cSrcweir aSql += ::dbtools::quoteName(sQuote,m_pTable->getSchema()) + sDot + ::dbtools::quoteName(sQuote,m_pTable->getTableName());
169cdf0e10cSrcweir aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" DROP "));
170cdf0e10cSrcweir aSql += ::dbtools::quoteName(sQuote,_sElementName);
171cdf0e10cSrcweir
172cdf0e10cSrcweir Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
173cdf0e10cSrcweir xStmt->execute(aSql);
174cdf0e10cSrcweir ::comphelper::disposeComponent(xStmt);
175cdf0e10cSrcweir }
176cdf0e10cSrcweir }
177cdf0e10cSrcweir // -----------------------------------------------------------------------------
178cdf0e10cSrcweir
179cdf0e10cSrcweir
180cdf0e10cSrcweir
181