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