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/BTables.hxx"
27cdf0e10cSrcweir #include "adabas/BViews.hxx"
28cdf0e10cSrcweir #include "adabas/BTable.hxx"
29cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp>
30cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSet.hpp>
31cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnValue.hpp>
32cdf0e10cSrcweir #include <com/sun/star/sdbc/KeyRule.hpp>
33cdf0e10cSrcweir #include <com/sun/star/sdbcx/KeyType.hpp>
34cdf0e10cSrcweir #include "adabas/BCatalog.hxx"
35cdf0e10cSrcweir #include "adabas/BConnection.hxx"
36cdf0e10cSrcweir #include <comphelper/extract.hxx>
37cdf0e10cSrcweir #include "connectivity/dbtools.hxx"
38cdf0e10cSrcweir #include "connectivity/dbexception.hxx"
39cdf0e10cSrcweir #include <cppuhelper/interfacecontainer.h>
40cdf0e10cSrcweir #include <comphelper/types.hxx>
41cdf0e10cSrcweir
42cdf0e10cSrcweir using namespace ::comphelper;
43cdf0e10cSrcweir using namespace connectivity;
44cdf0e10cSrcweir using namespace ::cppu;
45cdf0e10cSrcweir using namespace connectivity::adabas;
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 using namespace dbtools;
53cdf0e10cSrcweir typedef connectivity::sdbcx::OCollection OCollection_TYPE;
54cdf0e10cSrcweir
createObject(const::rtl::OUString & _rName)55cdf0e10cSrcweir sdbcx::ObjectType OTables::createObject(const ::rtl::OUString& _rName)
56cdf0e10cSrcweir {
57cdf0e10cSrcweir ::rtl::OUString aName,aSchema;
58cdf0e10cSrcweir sal_Int32 nLen = _rName.indexOf('.');
59cdf0e10cSrcweir aSchema = _rName.copy(0,nLen);
60cdf0e10cSrcweir aName = _rName.copy(nLen+1);
61cdf0e10cSrcweir
62cdf0e10cSrcweir Sequence< ::rtl::OUString > aTypes(1);
63cdf0e10cSrcweir aTypes[0] = ::rtl::OUString::createFromAscii("%");
64cdf0e10cSrcweir // aTypes[0] = ::rtl::OUString::createFromAscii("TABLE");
65cdf0e10cSrcweir // aTypes[1] = ::rtl::OUString::createFromAscii("SYSTEMTABLE");
66cdf0e10cSrcweir
67cdf0e10cSrcweir Reference< XResultSet > xResult = m_xMetaData->getTables(Any(),aSchema,aName,aTypes);
68cdf0e10cSrcweir
69cdf0e10cSrcweir sdbcx::ObjectType xRet = NULL;
70cdf0e10cSrcweir if(xResult.is())
71cdf0e10cSrcweir {
72cdf0e10cSrcweir Reference< XRow > xRow(xResult,UNO_QUERY);
73cdf0e10cSrcweir if(xResult->next()) // there can be only one table with this name
74cdf0e10cSrcweir {
75cdf0e10cSrcweir OAdabasTable* pRet = new OAdabasTable(this, static_cast<OAdabasCatalog&>(m_rParent).getConnection(),
76cdf0e10cSrcweir aName,xRow->getString(4),xRow->getString(5),aSchema);
77cdf0e10cSrcweir xRet = pRet;
78cdf0e10cSrcweir }
79cdf0e10cSrcweir ::comphelper::disposeComponent(xResult);
80cdf0e10cSrcweir }
81cdf0e10cSrcweir
82cdf0e10cSrcweir return xRet;
83cdf0e10cSrcweir }
84cdf0e10cSrcweir // -------------------------------------------------------------------------
impl_refresh()85cdf0e10cSrcweir void OTables::impl_refresh( ) throw(RuntimeException)
86cdf0e10cSrcweir {
87cdf0e10cSrcweir static_cast<OAdabasCatalog&>(m_rParent).refreshTables();
88cdf0e10cSrcweir }
89cdf0e10cSrcweir // -------------------------------------------------------------------------
disposing(void)90cdf0e10cSrcweir void OTables::disposing(void)
91cdf0e10cSrcweir {
92cdf0e10cSrcweir m_xMetaData.clear();
93cdf0e10cSrcweir OCollection::disposing();
94cdf0e10cSrcweir }
95cdf0e10cSrcweir // -------------------------------------------------------------------------
createDescriptor()96cdf0e10cSrcweir Reference< XPropertySet > OTables::createDescriptor()
97cdf0e10cSrcweir {
98cdf0e10cSrcweir return new OAdabasTable(this,static_cast<OAdabasCatalog&>(m_rParent).getConnection());
99cdf0e10cSrcweir }
100cdf0e10cSrcweir // -------------------------------------------------------------------------
101cdf0e10cSrcweir // XAppend
appendObject(const::rtl::OUString & _rForName,const Reference<XPropertySet> & descriptor)102cdf0e10cSrcweir sdbcx::ObjectType OTables::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
103cdf0e10cSrcweir {
104cdf0e10cSrcweir createTable(descriptor);
105cdf0e10cSrcweir return createObject( _rForName );
106cdf0e10cSrcweir }
107cdf0e10cSrcweir // -------------------------------------------------------------------------
setComments(const Reference<XPropertySet> & descriptor)108cdf0e10cSrcweir void OTables::setComments(const Reference< XPropertySet >& descriptor ) throw(SQLException, RuntimeException)
109cdf0e10cSrcweir {
110cdf0e10cSrcweir ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("CREATE TABLE ");
111cdf0e10cSrcweir ::rtl::OUString aQuote = static_cast<OAdabasCatalog&>(m_rParent).getConnection()->getMetaData()->getIdentifierQuoteString( );
112cdf0e10cSrcweir const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
113cdf0e10cSrcweir
114cdf0e10cSrcweir OAdabasConnection* pConnection = static_cast<OAdabasCatalog&>(m_rParent).getConnection();
115cdf0e10cSrcweir Reference< XStatement > xStmt = pConnection->createStatement( );
116cdf0e10cSrcweir aSql = ::rtl::OUString::createFromAscii("COMMENT ON TABLE ");
117cdf0e10cSrcweir ::rtl::OUString sSchema;
118cdf0e10cSrcweir descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= sSchema;
119cdf0e10cSrcweir if(sSchema.getLength())
120cdf0e10cSrcweir aSql += ::dbtools::quoteName(aQuote, sSchema) + sDot;
121cdf0e10cSrcweir
122cdf0e10cSrcweir aSql += aQuote + getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))) + aQuote
123cdf0e10cSrcweir + ::rtl::OUString::createFromAscii(" '")
124cdf0e10cSrcweir + getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DESCRIPTION)))
125cdf0e10cSrcweir + ::rtl::OUString::createFromAscii("'");
126cdf0e10cSrcweir xStmt->execute(aSql);
127cdf0e10cSrcweir
128cdf0e10cSrcweir // columns
129cdf0e10cSrcweir Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
130cdf0e10cSrcweir Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
131cdf0e10cSrcweir Reference< XPropertySet > xColProp;
132cdf0e10cSrcweir
133cdf0e10cSrcweir aSql = ::rtl::OUString::createFromAscii("COMMENT ON COLUMN ");
134cdf0e10cSrcweir if(sSchema.getLength())
135cdf0e10cSrcweir aSql += ::dbtools::quoteName(aQuote, sSchema) + sDot;
136cdf0e10cSrcweir aSql += aQuote + getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))) + aQuote + sDot
137cdf0e10cSrcweir + aQuote;
138cdf0e10cSrcweir
139cdf0e10cSrcweir for(sal_Int32 i=0;i<xColumns->getCount();++i)
140cdf0e10cSrcweir {
141cdf0e10cSrcweir ::cppu::extractInterface(xColProp,xColumns->getByIndex(i));
142cdf0e10cSrcweir if(xColProp.is())
143cdf0e10cSrcweir {
144cdf0e10cSrcweir ::rtl::OUString aDescription = getString(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DESCRIPTION)));
145cdf0e10cSrcweir if(aDescription.getLength())
146cdf0e10cSrcweir {
147cdf0e10cSrcweir ::rtl::OUString aCom = aSql + getString(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))) + aQuote
148cdf0e10cSrcweir + ::rtl::OUString::createFromAscii(" '")
149cdf0e10cSrcweir + aDescription
150cdf0e10cSrcweir + ::rtl::OUString::createFromAscii("'");
151cdf0e10cSrcweir xStmt->execute(aSql);
152cdf0e10cSrcweir ::comphelper::disposeComponent(xStmt);
153cdf0e10cSrcweir }
154cdf0e10cSrcweir }
155cdf0e10cSrcweir }
156cdf0e10cSrcweir ::comphelper::disposeComponent(xStmt);
157cdf0e10cSrcweir }
158cdf0e10cSrcweir // -------------------------------------------------------------------------
159cdf0e10cSrcweir // XDrop
dropObject(sal_Int32 _nPos,const::rtl::OUString _sElementName)160cdf0e10cSrcweir void OTables::dropObject(sal_Int32 _nPos,const ::rtl::OUString _sElementName)
161cdf0e10cSrcweir {
162cdf0e10cSrcweir Reference< XInterface > xObject( getObject( _nPos ) );
163cdf0e10cSrcweir sal_Bool bIsNew = connectivity::sdbcx::ODescriptor::isNew( xObject );
164cdf0e10cSrcweir if (!bIsNew)
165cdf0e10cSrcweir {
166cdf0e10cSrcweir OAdabasConnection* pConnection = static_cast<OAdabasCatalog&>(m_rParent).getConnection();
167cdf0e10cSrcweir Reference< XStatement > xStmt = pConnection->createStatement( );
168cdf0e10cSrcweir
169cdf0e10cSrcweir ::rtl::OUString aName,aSchema;
170cdf0e10cSrcweir sal_Int32 nLen = _sElementName.indexOf('.');
171cdf0e10cSrcweir aSchema = _sElementName.copy(0,nLen);
172cdf0e10cSrcweir aName = _sElementName.copy(nLen+1);
173cdf0e10cSrcweir ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("DROP ");
174cdf0e10cSrcweir const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
175cdf0e10cSrcweir
176cdf0e10cSrcweir Reference<XPropertySet> xProp(xObject,UNO_QUERY);
177cdf0e10cSrcweir sal_Bool bIsView;
178cdf0e10cSrcweir if((bIsView = (xProp.is() && ::comphelper::getString(xProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))) == ::rtl::OUString::createFromAscii("VIEW")))) // here we have a view
179cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii("VIEW ");
180cdf0e10cSrcweir else
181cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii("TABLE ");
182cdf0e10cSrcweir
183cdf0e10cSrcweir aSql += m_xMetaData->getIdentifierQuoteString( ) + aSchema + m_xMetaData->getIdentifierQuoteString( );
184cdf0e10cSrcweir aSql += sDot;
185cdf0e10cSrcweir aSql += m_xMetaData->getIdentifierQuoteString( ) + aName + m_xMetaData->getIdentifierQuoteString( );
186cdf0e10cSrcweir xStmt->execute(aSql);
187cdf0e10cSrcweir ::comphelper::disposeComponent(xStmt);
188cdf0e10cSrcweir // if no exception was thrown we must delete it from the views
189cdf0e10cSrcweir if(bIsView)
190cdf0e10cSrcweir {
191cdf0e10cSrcweir OViews* pViews = static_cast<OViews*>(static_cast<OAdabasCatalog&>(m_rParent).getPrivateViews());
192cdf0e10cSrcweir if(pViews && pViews->hasByName(_sElementName))
193cdf0e10cSrcweir pViews->dropByNameImpl(_sElementName);
194cdf0e10cSrcweir }
195cdf0e10cSrcweir }
196cdf0e10cSrcweir }
197cdf0e10cSrcweir // -------------------------------------------------------------------------
createTable(const Reference<XPropertySet> & descriptor)198cdf0e10cSrcweir void OTables::createTable( const Reference< XPropertySet >& descriptor )
199cdf0e10cSrcweir {
200cdf0e10cSrcweir ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("CREATE TABLE ");
201cdf0e10cSrcweir ::rtl::OUString aQuote = static_cast<OAdabasCatalog&>(m_rParent).getConnection()->getMetaData()->getIdentifierQuoteString( );
202cdf0e10cSrcweir const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
203cdf0e10cSrcweir ::rtl::OUString sSchema;
204cdf0e10cSrcweir
205cdf0e10cSrcweir descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= sSchema;
206cdf0e10cSrcweir if(sSchema.getLength())
207cdf0e10cSrcweir aSql += ::dbtools::quoteName(aQuote, sSchema) + sDot;
208cdf0e10cSrcweir else
209cdf0e10cSrcweir descriptor->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCHEMANAME),makeAny(static_cast<OAdabasCatalog&>(m_rParent).getConnection()->getUserName().
210cdf0e10cSrcweir toAsciiUpperCase()
211cdf0e10cSrcweir ));
212cdf0e10cSrcweir
213cdf0e10cSrcweir aSql += ::dbtools::quoteName(aQuote, getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))))
214cdf0e10cSrcweir + ::rtl::OUString::createFromAscii(" (");
215cdf0e10cSrcweir
216cdf0e10cSrcweir // columns
217cdf0e10cSrcweir Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
218cdf0e10cSrcweir Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
219cdf0e10cSrcweir Reference< XPropertySet > xColProp;
220cdf0e10cSrcweir
221cdf0e10cSrcweir Any aTypeName;
222cdf0e10cSrcweir sal_Int32 nCount = xColumns->getCount();
223cdf0e10cSrcweir if(!nCount)
224cdf0e10cSrcweir ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(this));
225cdf0e10cSrcweir
226cdf0e10cSrcweir for(sal_Int32 i=0;i<nCount;++i)
227cdf0e10cSrcweir {
228cdf0e10cSrcweir if(::cppu::extractInterface(xColProp,xColumns->getByIndex(i)) && xColProp.is())
229cdf0e10cSrcweir {
230cdf0e10cSrcweir
231cdf0e10cSrcweir aSql += aQuote + getString(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))) + aQuote;
232cdf0e10cSrcweir
233cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii(" ");
234cdf0e10cSrcweir aSql += OTables::getColumnSqlType(xColProp);
235cdf0e10cSrcweir aSql += OTables::getColumnSqlNotNullDefault(xColProp);
236cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii(",");
237cdf0e10cSrcweir }
238cdf0e10cSrcweir }
239cdf0e10cSrcweir
240cdf0e10cSrcweir // keys
241cdf0e10cSrcweir
242cdf0e10cSrcweir Reference<XKeysSupplier> xKeySup(descriptor,UNO_QUERY);
243cdf0e10cSrcweir
244cdf0e10cSrcweir Reference<XIndexAccess> xKeys = xKeySup->getKeys();
245cdf0e10cSrcweir if(xKeys.is())
246cdf0e10cSrcweir {
247cdf0e10cSrcweir sal_Bool bPKey = sal_False;
248cdf0e10cSrcweir for( sal_Int32 key=0; key<xKeys->getCount(); ++key )
249cdf0e10cSrcweir {
250cdf0e10cSrcweir if(::cppu::extractInterface(xColProp,xKeys->getByIndex(key)) && xColProp.is())
251cdf0e10cSrcweir {
252cdf0e10cSrcweir
253cdf0e10cSrcweir sal_Int32 nKeyType = getINT32(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)));
254cdf0e10cSrcweir
255cdf0e10cSrcweir if(nKeyType == KeyType::PRIMARY)
256cdf0e10cSrcweir {
257cdf0e10cSrcweir if(bPKey)
258cdf0e10cSrcweir throw SQLException();
259cdf0e10cSrcweir
260cdf0e10cSrcweir bPKey = sal_True;
261cdf0e10cSrcweir xColumnSup = Reference<XColumnsSupplier>(xColProp,UNO_QUERY);
262cdf0e10cSrcweir xColumns = Reference<XIndexAccess>(xColumnSup->getColumns(),UNO_QUERY);
263cdf0e10cSrcweir if(!xColumns->getCount())
264cdf0e10cSrcweir throw SQLException();
265cdf0e10cSrcweir
266cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii(" PRIMARY KEY (");
267cdf0e10cSrcweir for( sal_Int32 column=0; column<xColumns->getCount(); ++column )
268cdf0e10cSrcweir {
269cdf0e10cSrcweir if(::cppu::extractInterface(xColProp,xColumns->getByIndex(column)) && xColProp.is())
270cdf0e10cSrcweir aSql += aQuote + getString(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))) + aQuote
271cdf0e10cSrcweir + ::rtl::OUString::createFromAscii(",");
272cdf0e10cSrcweir }
273cdf0e10cSrcweir
274cdf0e10cSrcweir aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")"));
275cdf0e10cSrcweir }
276cdf0e10cSrcweir else if(nKeyType == KeyType::UNIQUE)
277cdf0e10cSrcweir {
278cdf0e10cSrcweir xColumnSup = Reference<XColumnsSupplier>(xColProp,UNO_QUERY);
279cdf0e10cSrcweir xColumns = Reference<XIndexAccess>(xColumnSup->getColumns(),UNO_QUERY);
280cdf0e10cSrcweir if(!xColumns->getCount())
281cdf0e10cSrcweir throw SQLException();
282cdf0e10cSrcweir
283cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii(" UNIQUE (");
284cdf0e10cSrcweir for( sal_Int32 column=0; column<xColumns->getCount(); ++column )
285cdf0e10cSrcweir {
286cdf0e10cSrcweir if(::cppu::extractInterface(xColProp,xColumns->getByIndex(column)) && xColProp.is())
287cdf0e10cSrcweir aSql += aQuote + getString(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))) + aQuote
288cdf0e10cSrcweir + ::rtl::OUString::createFromAscii(",");
289cdf0e10cSrcweir }
290cdf0e10cSrcweir
291cdf0e10cSrcweir aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")"));
292cdf0e10cSrcweir }
293cdf0e10cSrcweir else if(nKeyType == KeyType::FOREIGN)
294cdf0e10cSrcweir {
295cdf0e10cSrcweir sal_Int32 nDeleteRule = getINT32(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DELETERULE)));
296cdf0e10cSrcweir
297cdf0e10cSrcweir xColumnSup = Reference<XColumnsSupplier>(xColProp,UNO_QUERY);
298cdf0e10cSrcweir xColumns = Reference<XIndexAccess>(xColumnSup->getColumns(),UNO_QUERY);
299cdf0e10cSrcweir if(!xColumns->getCount())
300cdf0e10cSrcweir throw SQLException();
301cdf0e10cSrcweir
302cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii(" FOREIGN KEY ");
303cdf0e10cSrcweir ::rtl::OUString aName,aSchema,aRefTable = getString(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REFERENCEDTABLE)));
304cdf0e10cSrcweir sal_Int32 nLen = aRefTable.indexOf('.');
305cdf0e10cSrcweir aSchema = aRefTable.copy(0,nLen);
306cdf0e10cSrcweir aName = aRefTable.copy(nLen+1);
307cdf0e10cSrcweir
308cdf0e10cSrcweir aSql += aQuote + aSchema + aQuote + sDot
309cdf0e10cSrcweir + aQuote + aName + aQuote
310cdf0e10cSrcweir + ::rtl::OUString::createFromAscii(" (");
311cdf0e10cSrcweir
312cdf0e10cSrcweir for ( sal_Int32 column=0; column<xColumns->getCount(); ++column )
313cdf0e10cSrcweir {
314cdf0e10cSrcweir if(::cppu::extractInterface(xColProp,xColumns->getByIndex(column)) && xColProp.is())
315cdf0e10cSrcweir aSql += aQuote + getString(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))) + aQuote
316cdf0e10cSrcweir + ::rtl::OUString::createFromAscii(",");
317cdf0e10cSrcweir }
318cdf0e10cSrcweir
319cdf0e10cSrcweir aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")"));
320cdf0e10cSrcweir
321cdf0e10cSrcweir switch(nDeleteRule)
322cdf0e10cSrcweir {
323cdf0e10cSrcweir case KeyRule::CASCADE:
324cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii(" ON DELETE CASCADE ");
325cdf0e10cSrcweir break;
326cdf0e10cSrcweir case KeyRule::RESTRICT:
327cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii(" ON DELETE RESTRICT ");
328cdf0e10cSrcweir break;
329cdf0e10cSrcweir case KeyRule::SET_NULL:
330cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii(" ON DELETE SET NULL ");
331cdf0e10cSrcweir break;
332cdf0e10cSrcweir case KeyRule::SET_DEFAULT:
333cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii(" ON DELETE SET DEFAULT ");
334cdf0e10cSrcweir break;
335cdf0e10cSrcweir default:
336cdf0e10cSrcweir ;
337cdf0e10cSrcweir }
338cdf0e10cSrcweir }
339cdf0e10cSrcweir }
340cdf0e10cSrcweir }
341cdf0e10cSrcweir }
342cdf0e10cSrcweir
343cdf0e10cSrcweir if(aSql.lastIndexOf(',') == (aSql.getLength()-1))
344cdf0e10cSrcweir aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")"));
345cdf0e10cSrcweir else
346cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii(")");
347cdf0e10cSrcweir
348cdf0e10cSrcweir OAdabasConnection* pConnection = static_cast<OAdabasCatalog&>(m_rParent).getConnection();
349cdf0e10cSrcweir Reference< XStatement > xStmt = pConnection->createStatement( );
350cdf0e10cSrcweir xStmt->execute(aSql);
351cdf0e10cSrcweir ::comphelper::disposeComponent(xStmt);
352cdf0e10cSrcweir
353cdf0e10cSrcweir if(getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DESCRIPTION))).getLength())
354cdf0e10cSrcweir setComments(descriptor);
355cdf0e10cSrcweir }
356cdf0e10cSrcweir // -----------------------------------------------------------------------------
appendNew(const::rtl::OUString & _rsNewTable)357cdf0e10cSrcweir void OTables::appendNew(const ::rtl::OUString& _rsNewTable)
358cdf0e10cSrcweir {
359cdf0e10cSrcweir insertElement(_rsNewTable,NULL);
360cdf0e10cSrcweir
361cdf0e10cSrcweir // notify our container listeners
362cdf0e10cSrcweir ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(_rsNewTable), Any(), Any());
363cdf0e10cSrcweir OInterfaceIteratorHelper aListenerLoop(m_aContainerListeners);
364cdf0e10cSrcweir while (aListenerLoop.hasMoreElements())
365cdf0e10cSrcweir static_cast<XContainerListener*>(aListenerLoop.next())->elementInserted(aEvent);
366cdf0e10cSrcweir }
367cdf0e10cSrcweir // -----------------------------------------------------------------------------
getColumnSqlType(const Reference<XPropertySet> & _rxColProp)368cdf0e10cSrcweir ::rtl::OUString OTables::getColumnSqlType(const Reference<XPropertySet>& _rxColProp)
369cdf0e10cSrcweir {
370cdf0e10cSrcweir ::rtl::OUString sSql;
371cdf0e10cSrcweir sal_Int32 nDataType = 0;
372cdf0e10cSrcweir _rxColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)) >>= nDataType;
373cdf0e10cSrcweir switch(nDataType)
374cdf0e10cSrcweir {
375cdf0e10cSrcweir case DataType::VARBINARY:
376cdf0e10cSrcweir sSql += ::rtl::OUString::createFromAscii("VAR");
377cdf0e10cSrcweir /* run through*/
378cdf0e10cSrcweir case DataType::BINARY:
379cdf0e10cSrcweir sSql += ::rtl::OUString::createFromAscii("CHAR");
380cdf0e10cSrcweir break;
381cdf0e10cSrcweir default:
382cdf0e10cSrcweir {
383cdf0e10cSrcweir Any aTypeName = _rxColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME));
384cdf0e10cSrcweir if(aTypeName.hasValue() && getString(aTypeName).getLength())
385cdf0e10cSrcweir sSql += getString(aTypeName);
386cdf0e10cSrcweir else
387cdf0e10cSrcweir sSql += OTables::getTypeString(_rxColProp) + ::rtl::OUString::createFromAscii(" ");
388cdf0e10cSrcweir }
389cdf0e10cSrcweir }
390cdf0e10cSrcweir
391cdf0e10cSrcweir switch(nDataType)
392cdf0e10cSrcweir {
393cdf0e10cSrcweir case DataType::CHAR:
394cdf0e10cSrcweir case DataType::VARCHAR:
395cdf0e10cSrcweir case DataType::FLOAT:
396cdf0e10cSrcweir case DataType::REAL:
397cdf0e10cSrcweir sSql += ::rtl::OUString::createFromAscii("(")
398cdf0e10cSrcweir + ::rtl::OUString::valueOf(getINT32(_rxColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION))))
399cdf0e10cSrcweir + ::rtl::OUString::createFromAscii(")");
400cdf0e10cSrcweir break;
401cdf0e10cSrcweir
402cdf0e10cSrcweir case DataType::DECIMAL:
403cdf0e10cSrcweir case DataType::NUMERIC:
404cdf0e10cSrcweir sSql += ::rtl::OUString::createFromAscii("(")
405cdf0e10cSrcweir + ::rtl::OUString::valueOf(getINT32(_rxColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION))))
406cdf0e10cSrcweir + ::rtl::OUString::createFromAscii(",")
407cdf0e10cSrcweir + ::rtl::OUString::valueOf(getINT32(_rxColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE))))
408cdf0e10cSrcweir + ::rtl::OUString::createFromAscii(")");
409cdf0e10cSrcweir break;
410cdf0e10cSrcweir case DataType::BINARY:
411cdf0e10cSrcweir case DataType::VARBINARY:
412cdf0e10cSrcweir sSql += ::rtl::OUString::createFromAscii("(")
413cdf0e10cSrcweir + ::rtl::OUString::valueOf(getINT32(_rxColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION))))
414cdf0e10cSrcweir + ::rtl::OUString::createFromAscii(") BYTE");
415cdf0e10cSrcweir break;
416cdf0e10cSrcweir }
417cdf0e10cSrcweir return sSql;
418cdf0e10cSrcweir }
419cdf0e10cSrcweir // -----------------------------------------------------------------------------
getColumnSqlNotNullDefault(const Reference<XPropertySet> & _rxColProp)420cdf0e10cSrcweir ::rtl::OUString OTables::getColumnSqlNotNullDefault(const Reference<XPropertySet>& _rxColProp)
421cdf0e10cSrcweir {
422cdf0e10cSrcweir OSL_ENSURE(_rxColProp.is(),"OTables::getColumnSqlNotNullDefault: Column is null!");
423cdf0e10cSrcweir ::rtl::OUString sSql;
424cdf0e10cSrcweir ::rtl::OUString aDefault = getString(_rxColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE)));
425cdf0e10cSrcweir if(getINT32(_rxColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE))) == ColumnValue::NO_NULLS)
426cdf0e10cSrcweir {
427cdf0e10cSrcweir sSql += ::rtl::OUString::createFromAscii(" NOT NULL");
428cdf0e10cSrcweir if(aDefault.getLength())
429cdf0e10cSrcweir sSql += ::rtl::OUString::createFromAscii(" WITH DEFAULT");
430cdf0e10cSrcweir }
431cdf0e10cSrcweir else if(aDefault.getLength())
432cdf0e10cSrcweir {
433cdf0e10cSrcweir sSql +=::rtl::OUString::createFromAscii(" DEFAULT '") + aDefault;
434cdf0e10cSrcweir sSql += ::rtl::OUString::createFromAscii("'");
435cdf0e10cSrcweir }
436cdf0e10cSrcweir return sSql;
437cdf0e10cSrcweir }
438cdf0e10cSrcweir // -----------------------------------------------------------------------------
getTypeString(const Reference<XPropertySet> & _rxColProp)439cdf0e10cSrcweir ::rtl::OUString OTables::getTypeString(const Reference< XPropertySet >& _rxColProp)
440cdf0e10cSrcweir {
441cdf0e10cSrcweir ::rtl::OUString aValue;
442cdf0e10cSrcweir switch(getINT32(_rxColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))))
443cdf0e10cSrcweir {
444cdf0e10cSrcweir case DataType::BIT:
445cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("BOOLEAN");
446cdf0e10cSrcweir break;
447cdf0e10cSrcweir case DataType::TINYINT:
448cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("SMALLINT");
449cdf0e10cSrcweir break;
450cdf0e10cSrcweir case DataType::SMALLINT:
451cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("SMALLINT");
452cdf0e10cSrcweir break;
453cdf0e10cSrcweir case DataType::INTEGER:
454cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("INT");
455cdf0e10cSrcweir break;
456cdf0e10cSrcweir case DataType::FLOAT:
457cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("FLOAT");
458cdf0e10cSrcweir break;
459cdf0e10cSrcweir case DataType::REAL:
460cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("REAL");
461cdf0e10cSrcweir break;
462cdf0e10cSrcweir case DataType::DOUBLE:
463cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("DOUBLE");
464cdf0e10cSrcweir break;
465cdf0e10cSrcweir case DataType::NUMERIC:
466cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("DECIMAL");
467cdf0e10cSrcweir break;
468cdf0e10cSrcweir case DataType::DECIMAL:
469cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("DECIMAL");
470cdf0e10cSrcweir break;
471cdf0e10cSrcweir case DataType::CHAR:
472cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("CHAR");
473cdf0e10cSrcweir break;
474cdf0e10cSrcweir case DataType::VARCHAR:
475cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("VARCHAR");
476cdf0e10cSrcweir break;
477cdf0e10cSrcweir case DataType::LONGVARCHAR:
478cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("LONG VARCHAR");
479cdf0e10cSrcweir break;
480cdf0e10cSrcweir case DataType::DATE:
481cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("DATE");
482cdf0e10cSrcweir break;
483cdf0e10cSrcweir case DataType::TIME:
484cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("TIME");
485cdf0e10cSrcweir break;
486cdf0e10cSrcweir case DataType::TIMESTAMP:
487cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("TIMESTAMP");
488cdf0e10cSrcweir break;
489cdf0e10cSrcweir case DataType::BINARY:
490cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("CHAR () BYTE");
491cdf0e10cSrcweir break;
492cdf0e10cSrcweir case DataType::VARBINARY:
493cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("VARCHAR () BYTE");
494cdf0e10cSrcweir break;
495cdf0e10cSrcweir case DataType::LONGVARBINARY:
496cdf0e10cSrcweir aValue = ::rtl::OUString::createFromAscii("LONG BYTE");
497cdf0e10cSrcweir break;
498cdf0e10cSrcweir }
499cdf0e10cSrcweir return aValue;
500cdf0e10cSrcweir }
501cdf0e10cSrcweir // -----------------------------------------------------------------------------
getNameForObject(const sdbcx::ObjectType & _xObject)502cdf0e10cSrcweir ::rtl::OUString OTables::getNameForObject(const sdbcx::ObjectType& _xObject)
503cdf0e10cSrcweir {
504cdf0e10cSrcweir OSL_ENSURE(_xObject.is(),"OTables::getNameForObject: Object is NULL!");
505cdf0e10cSrcweir ::rtl::OUString sName,sTemp;
506cdf0e10cSrcweir _xObject->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= sName;
507cdf0e10cSrcweir if( sName.getLength() )
508cdf0e10cSrcweir {
509cdf0e10cSrcweir const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
510cdf0e10cSrcweir sName += sDot;
511cdf0e10cSrcweir }
512cdf0e10cSrcweir
513cdf0e10cSrcweir _xObject->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= sTemp;
514cdf0e10cSrcweir sName += sTemp;
515cdf0e10cSrcweir
516cdf0e10cSrcweir return sName;
517cdf0e10cSrcweir }
518cdf0e10cSrcweir // -----------------------------------------------------------------------------
519