xref: /AOO41X/main/dbaccess/source/ui/tabledesign/TableController.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_dbaccess.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "FieldDescriptions.hxx"
32*cdf0e10cSrcweir #include "TEditControl.hxx"
33*cdf0e10cSrcweir #include "TableController.hxx"
34*cdf0e10cSrcweir #include "TableDesignView.hxx"
35*cdf0e10cSrcweir #include "TableRow.hxx"
36*cdf0e10cSrcweir #include "TypeInfo.hxx"
37*cdf0e10cSrcweir #include "UITools.hxx"
38*cdf0e10cSrcweir #include "browserids.hxx"
39*cdf0e10cSrcweir #include "dbu_reghelper.hxx"
40*cdf0e10cSrcweir #include "dbu_tbl.hrc"
41*cdf0e10cSrcweir #include "dbustrings.hrc"
42*cdf0e10cSrcweir #include "defaultobjectnamecheck.hxx"
43*cdf0e10cSrcweir #include "dlgsave.hxx"
44*cdf0e10cSrcweir #include "dsmeta.hxx"
45*cdf0e10cSrcweir #include "indexdialog.hxx"
46*cdf0e10cSrcweir #include "sqlmessage.hxx"
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir /** === begin UNO includes === **/
49*cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
50*cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
51*cdf0e10cSrcweir #include <com/sun/star/frame/FrameSearchFlag.hpp>
52*cdf0e10cSrcweir #include <com/sun/star/frame/XTitleChangeListener.hpp>
53*cdf0e10cSrcweir #include <com/sun/star/frame/XUntitledNumbers.hpp>
54*cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSink.hpp>
55*cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
56*cdf0e10cSrcweir #include <com/sun/star/sdb/CommandType.hpp>
57*cdf0e10cSrcweir #include <com/sun/star/sdb/SQLContext.hpp>
58*cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnValue.hpp>
59*cdf0e10cSrcweir #include <com/sun/star/sdbc/SQLWarning.hpp>
60*cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp>
61*cdf0e10cSrcweir #include <com/sun/star/sdbcx/KeyType.hpp>
62*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XAlterTable.hpp>
63*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XAppend.hpp>
64*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
65*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XDrop.hpp>
66*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XIndexesSupplier.hpp>
67*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
68*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
69*cdf0e10cSrcweir /** === end UNO includes === **/
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir #include <comphelper/extract.hxx>
72*cdf0e10cSrcweir #include <comphelper/streamsection.hxx>
73*cdf0e10cSrcweir #include <comphelper/types.hxx>
74*cdf0e10cSrcweir #include <connectivity/dbexception.hxx>
75*cdf0e10cSrcweir #include <connectivity/dbtools.hxx>
76*cdf0e10cSrcweir #include <connectivity/dbmetadata.hxx>
77*cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
78*cdf0e10cSrcweir #include <sfx2/sfxsids.hrc>
79*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
80*cdf0e10cSrcweir #include <tools/string.hxx>
81*cdf0e10cSrcweir #include <vcl/msgbox.hxx>
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir #include <boost/mem_fn.hpp>
84*cdf0e10cSrcweir #include <boost/bind.hpp>
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir #include <algorithm>
87*cdf0e10cSrcweir #include <functional>
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir extern "C" void SAL_CALL createRegistryInfo_OTableControl()
90*cdf0e10cSrcweir {
91*cdf0e10cSrcweir 	static ::dbaui::OMultiInstanceAutoRegistration< ::dbaui::OTableController > aAutoRegistration;
92*cdf0e10cSrcweir }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir using namespace ::com::sun::star;
95*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
96*cdf0e10cSrcweir using namespace ::com::sun::star::io;
97*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
98*cdf0e10cSrcweir using namespace ::com::sun::star::frame;
99*cdf0e10cSrcweir using namespace ::com::sun::star::util;
100*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
101*cdf0e10cSrcweir using namespace ::com::sun::star::container;
102*cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
103*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
104*cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
105*cdf0e10cSrcweir using namespace ::com::sun::star::ui;
106*cdf0e10cSrcweir using namespace ::com::sun::star::util;
107*cdf0e10cSrcweir using namespace ::dbtools;
108*cdf0e10cSrcweir using namespace ::dbaui;
109*cdf0e10cSrcweir using namespace ::comphelper;
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir // Anzahl Spalten beim Neuanlegen
112*cdf0e10cSrcweir #define NEWCOLS		   128
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir namespace
115*cdf0e10cSrcweir {
116*cdf0e10cSrcweir 	void dropTable(const Reference<XNameAccess>& _rxTable,const ::rtl::OUString& _sTableName)
117*cdf0e10cSrcweir 	{
118*cdf0e10cSrcweir 		if ( _rxTable->hasByName(_sTableName) )
119*cdf0e10cSrcweir 		{
120*cdf0e10cSrcweir 			Reference<XDrop> xNameCont(_rxTable,UNO_QUERY);
121*cdf0e10cSrcweir 			OSL_ENSURE(xNameCont.is(),"No drop interface for tables!");
122*cdf0e10cSrcweir 			if ( xNameCont.is() )
123*cdf0e10cSrcweir 				xNameCont->dropByName(_sTableName);
124*cdf0e10cSrcweir 		}
125*cdf0e10cSrcweir 	}
126*cdf0e10cSrcweir 	//------------------------------------------------------------------------------
127*cdf0e10cSrcweir 	struct OTableRowCompare : public ::std::binary_function<  ::boost::shared_ptr<OTableRow> , ::rtl::OUString, bool>
128*cdf0e10cSrcweir 	{
129*cdf0e10cSrcweir 		bool operator() (const  ::boost::shared_ptr<OTableRow>  lhs, const ::rtl::OUString& rhs) const
130*cdf0e10cSrcweir 		{
131*cdf0e10cSrcweir 			OFieldDescription* pField = lhs->GetActFieldDescr();
132*cdf0e10cSrcweir 			return pField && pField->GetName() == rhs;
133*cdf0e10cSrcweir 		}
134*cdf0e10cSrcweir 	};
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir //------------------------------------------------------------------------------
139*cdf0e10cSrcweir ::rtl::OUString SAL_CALL OTableController::getImplementationName() throw( RuntimeException )
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir 	return getImplementationName_Static();
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir //------------------------------------------------------------------------------
145*cdf0e10cSrcweir ::rtl::OUString OTableController::getImplementationName_Static() throw( RuntimeException )
146*cdf0e10cSrcweir {
147*cdf0e10cSrcweir 	return ::rtl::OUString::createFromAscii("org.openoffice.comp.dbu.OTableDesign");
148*cdf0e10cSrcweir }
149*cdf0e10cSrcweir //------------------------------------------------------------------------------
150*cdf0e10cSrcweir Sequence< ::rtl::OUString> OTableController::getSupportedServiceNames_Static(void) throw( RuntimeException )
151*cdf0e10cSrcweir {
152*cdf0e10cSrcweir 	Sequence< ::rtl::OUString> aSupported(1);
153*cdf0e10cSrcweir 	aSupported.getArray()[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdb.TableDesign");
154*cdf0e10cSrcweir 	return aSupported;
155*cdf0e10cSrcweir }
156*cdf0e10cSrcweir //-------------------------------------------------------------------------
157*cdf0e10cSrcweir Sequence< ::rtl::OUString> SAL_CALL OTableController::getSupportedServiceNames() throw(RuntimeException)
158*cdf0e10cSrcweir {
159*cdf0e10cSrcweir 	return getSupportedServiceNames_Static();
160*cdf0e10cSrcweir }
161*cdf0e10cSrcweir // -------------------------------------------------------------------------
162*cdf0e10cSrcweir Reference< XInterface > SAL_CALL OTableController::Create(const Reference<XMultiServiceFactory >& _rxFactory)
163*cdf0e10cSrcweir {
164*cdf0e10cSrcweir 	return *(new OTableController(_rxFactory));
165*cdf0e10cSrcweir }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir DBG_NAME(OTableController)
168*cdf0e10cSrcweir // -----------------------------------------------------------------------------
169*cdf0e10cSrcweir OTableController::OTableController(const Reference< XMultiServiceFactory >& _rM) : OTableController_BASE(_rM)
170*cdf0e10cSrcweir 	,m_sTypeNames(ModuleRes(STR_TABLEDESIGN_DBFIELDTYPES))
171*cdf0e10cSrcweir 	,m_pTypeInfo()
172*cdf0e10cSrcweir 	,m_bAllowAutoIncrementValue(sal_False)
173*cdf0e10cSrcweir 	,m_bNew(sal_True)
174*cdf0e10cSrcweir {
175*cdf0e10cSrcweir     DBG_CTOR(OTableController,NULL);
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 	InvalidateAll();
178*cdf0e10cSrcweir 	m_pTypeInfo = TOTypeInfoSP(new OTypeInfo());
179*cdf0e10cSrcweir 	m_pTypeInfo->aUIName = m_sTypeNames.GetToken(TYPE_OTHER);
180*cdf0e10cSrcweir }
181*cdf0e10cSrcweir // -----------------------------------------------------------------------------
182*cdf0e10cSrcweir OTableController::~OTableController()
183*cdf0e10cSrcweir {
184*cdf0e10cSrcweir 	m_aTypeInfoIndex.clear();
185*cdf0e10cSrcweir 	m_aTypeInfo.clear();
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir     DBG_DTOR(OTableController,NULL);
188*cdf0e10cSrcweir }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir // -----------------------------------------------------------------------------
191*cdf0e10cSrcweir void OTableController::startTableListening()
192*cdf0e10cSrcweir {
193*cdf0e10cSrcweir 	Reference< XComponent >  xComponent(m_xTable, UNO_QUERY);
194*cdf0e10cSrcweir 	if (xComponent.is())
195*cdf0e10cSrcweir 		xComponent->addEventListener(static_cast<XModifyListener*>(this));
196*cdf0e10cSrcweir }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir // -----------------------------------------------------------------------------
199*cdf0e10cSrcweir void OTableController::stopTableListening()
200*cdf0e10cSrcweir {
201*cdf0e10cSrcweir 	Reference< XComponent >  xComponent(m_xTable, UNO_QUERY);
202*cdf0e10cSrcweir 	if (xComponent.is())
203*cdf0e10cSrcweir 		xComponent->removeEventListener(static_cast<XModifyListener*>(this));
204*cdf0e10cSrcweir }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir // -----------------------------------------------------------------------------
207*cdf0e10cSrcweir void OTableController::disposing()
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir 	OTableController_BASE::disposing();
210*cdf0e10cSrcweir     clearView();
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     m_vRowList.clear();
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir // -----------------------------------------------------------------------------
215*cdf0e10cSrcweir FeatureState OTableController::GetState(sal_uInt16 _nId) const
216*cdf0e10cSrcweir {
217*cdf0e10cSrcweir 	FeatureState aReturn;
218*cdf0e10cSrcweir 	// (disabled automatically)
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 	switch (_nId)
221*cdf0e10cSrcweir 	{
222*cdf0e10cSrcweir 		case ID_BROWSER_CLOSE:
223*cdf0e10cSrcweir 			aReturn.bEnabled = sal_True;
224*cdf0e10cSrcweir 			break;
225*cdf0e10cSrcweir 		case ID_BROWSER_EDITDOC:
226*cdf0e10cSrcweir 			aReturn.bChecked = isEditable();
227*cdf0e10cSrcweir 			aReturn.bEnabled = m_bNew || isEditable();// the editable flag is set through this one -> || isAddAllowed() || isDropAllowed() || isAlterAllowed();
228*cdf0e10cSrcweir 			break;
229*cdf0e10cSrcweir 		case ID_BROWSER_SAVEDOC:
230*cdf0e10cSrcweir 			aReturn.bEnabled = impl_isModified();
231*cdf0e10cSrcweir 			if ( aReturn.bEnabled )
232*cdf0e10cSrcweir 			{
233*cdf0e10cSrcweir 				::std::vector< ::boost::shared_ptr<OTableRow> >::const_iterator aIter = ::std::find_if(m_vRowList.begin(),m_vRowList.end(),
234*cdf0e10cSrcweir                     ::boost::mem_fn(&OTableRow::isValid));
235*cdf0e10cSrcweir 				aReturn.bEnabled = aIter != m_vRowList.end();
236*cdf0e10cSrcweir 			}
237*cdf0e10cSrcweir 			break;
238*cdf0e10cSrcweir 		case ID_BROWSER_SAVEASDOC:
239*cdf0e10cSrcweir 			aReturn.bEnabled = isConnected() && isEditable();
240*cdf0e10cSrcweir 			if ( aReturn.bEnabled )
241*cdf0e10cSrcweir 			{
242*cdf0e10cSrcweir 				::std::vector< ::boost::shared_ptr<OTableRow> >::const_iterator aIter = ::std::find_if(m_vRowList.begin(),m_vRowList.end(),
243*cdf0e10cSrcweir                     ::boost::mem_fn(&OTableRow::isValid));
244*cdf0e10cSrcweir 				aReturn.bEnabled = aIter != m_vRowList.end();
245*cdf0e10cSrcweir 			}
246*cdf0e10cSrcweir 			break;
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 		case ID_BROWSER_CUT:
249*cdf0e10cSrcweir 			aReturn.bEnabled = isEditable() && m_aCurrentFrame.isActive() && getView() && static_cast<OTableDesignView*>(getView())->isCutAllowed();
250*cdf0e10cSrcweir 			break;
251*cdf0e10cSrcweir 		case ID_BROWSER_COPY:
252*cdf0e10cSrcweir 			aReturn.bEnabled = m_aCurrentFrame.isActive() && getView() && static_cast<OTableDesignView*>(getView())->isCopyAllowed();
253*cdf0e10cSrcweir 			break;
254*cdf0e10cSrcweir 		case ID_BROWSER_PASTE:
255*cdf0e10cSrcweir 			aReturn.bEnabled = isEditable() && m_aCurrentFrame.isActive() && getView() && static_cast<OTableDesignView*>(getView())->isPasteAllowed();
256*cdf0e10cSrcweir 			break;
257*cdf0e10cSrcweir 		case SID_INDEXDESIGN:
258*cdf0e10cSrcweir 			aReturn.bEnabled =
259*cdf0e10cSrcweir 				(	(	((!m_bNew && impl_isModified()) || impl_isModified())
260*cdf0e10cSrcweir 					||	Reference< XIndexesSupplier >(m_xTable, UNO_QUERY).is()
261*cdf0e10cSrcweir 					)
262*cdf0e10cSrcweir 				&&	isConnected()
263*cdf0e10cSrcweir 				);
264*cdf0e10cSrcweir 			if ( aReturn.bEnabled )
265*cdf0e10cSrcweir 			{
266*cdf0e10cSrcweir 				::std::vector< ::boost::shared_ptr<OTableRow> >::const_iterator aIter = ::std::find_if(m_vRowList.begin(),m_vRowList.end(),
267*cdf0e10cSrcweir                     ::boost::mem_fn(&OTableRow::isValid));
268*cdf0e10cSrcweir 				aReturn.bEnabled = aIter != m_vRowList.end();
269*cdf0e10cSrcweir 			}
270*cdf0e10cSrcweir 			break;
271*cdf0e10cSrcweir 		default:
272*cdf0e10cSrcweir 			aReturn = OTableController_BASE::GetState(_nId);
273*cdf0e10cSrcweir 	}
274*cdf0e10cSrcweir 	return aReturn;
275*cdf0e10cSrcweir }
276*cdf0e10cSrcweir // -----------------------------------------------------------------------------
277*cdf0e10cSrcweir void OTableController::Execute(sal_uInt16 _nId, const Sequence< PropertyValue >& aArgs)
278*cdf0e10cSrcweir {
279*cdf0e10cSrcweir 	switch(_nId)
280*cdf0e10cSrcweir 	{
281*cdf0e10cSrcweir 		case ID_BROWSER_EDITDOC:
282*cdf0e10cSrcweir 			setEditable(!isEditable());
283*cdf0e10cSrcweir 			static_cast<OTableDesignView*>(getView())->setReadOnly(!isEditable());
284*cdf0e10cSrcweir 			InvalidateFeature(ID_BROWSER_PASTE);
285*cdf0e10cSrcweir 			InvalidateFeature(SID_BROWSER_CLEAR_QUERY);
286*cdf0e10cSrcweir 			break;
287*cdf0e10cSrcweir 		case ID_BROWSER_SAVEASDOC:
288*cdf0e10cSrcweir 			doSaveDoc(sal_True);
289*cdf0e10cSrcweir 			break;
290*cdf0e10cSrcweir 		case ID_BROWSER_SAVEDOC:
291*cdf0e10cSrcweir 			static_cast<OTableDesignView*>(getView())->GetEditorCtrl()->SaveCurRow();
292*cdf0e10cSrcweir 			doSaveDoc(sal_False);
293*cdf0e10cSrcweir 			break;
294*cdf0e10cSrcweir 		case ID_BROWSER_CUT:
295*cdf0e10cSrcweir 			static_cast<OTableDesignView*>(getView())->cut();
296*cdf0e10cSrcweir 			break;
297*cdf0e10cSrcweir 		case ID_BROWSER_COPY:
298*cdf0e10cSrcweir 			static_cast<OTableDesignView*>(getView())->copy();
299*cdf0e10cSrcweir 			break;
300*cdf0e10cSrcweir 		case ID_BROWSER_PASTE:
301*cdf0e10cSrcweir 			static_cast<OTableDesignView*>(getView())->paste();
302*cdf0e10cSrcweir 			break;
303*cdf0e10cSrcweir 		case SID_INDEXDESIGN:
304*cdf0e10cSrcweir 			doEditIndexes();
305*cdf0e10cSrcweir 			break;
306*cdf0e10cSrcweir 		default:
307*cdf0e10cSrcweir 			OTableController_BASE::Execute(_nId,aArgs);
308*cdf0e10cSrcweir 	}
309*cdf0e10cSrcweir 	InvalidateFeature(_nId);
310*cdf0e10cSrcweir }
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir // -----------------------------------------------------------------------------
313*cdf0e10cSrcweir sal_Bool OTableController::doSaveDoc(sal_Bool _bSaveAs)
314*cdf0e10cSrcweir {
315*cdf0e10cSrcweir 	if (!isConnected())
316*cdf0e10cSrcweir 		reconnect(sal_True); // ask the user for a new connection
317*cdf0e10cSrcweir 	Reference<XTablesSupplier> xTablesSup(getConnection(),UNO_QUERY);
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir 	if (!xTablesSup.is())
320*cdf0e10cSrcweir 	{
321*cdf0e10cSrcweir 		String aMessage(ModuleRes(STR_TABLEDESIGN_CONNECTION_MISSING));
322*cdf0e10cSrcweir         OSQLWarningBox( getView(), aMessage ).Execute();
323*cdf0e10cSrcweir 		return sal_False;
324*cdf0e10cSrcweir 	}
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir 	// check if a column exists
327*cdf0e10cSrcweir 	// TODO
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir 	Reference<XNameAccess> xTables;
330*cdf0e10cSrcweir 	::rtl::OUString sCatalog, sSchema;
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir 	sal_Bool bNew = (0 == m_sName.getLength());
333*cdf0e10cSrcweir 	bNew = bNew || m_bNew || _bSaveAs;
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir 	try
336*cdf0e10cSrcweir 	{
337*cdf0e10cSrcweir 		xTables = xTablesSup->getTables();
338*cdf0e10cSrcweir 		OSL_ENSURE(xTables.is(),"The tables can't be null!");
339*cdf0e10cSrcweir 		bNew = bNew || (xTables.is() && !xTables->hasByName(m_sName));
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir 		// first we need a name for our query so ask the user
342*cdf0e10cSrcweir 		if(bNew)
343*cdf0e10cSrcweir 		{
344*cdf0e10cSrcweir 			String aDefaultName;
345*cdf0e10cSrcweir 			if (_bSaveAs && !bNew)
346*cdf0e10cSrcweir 				 aDefaultName = String(m_sName);
347*cdf0e10cSrcweir 			else
348*cdf0e10cSrcweir             {
349*cdf0e10cSrcweir                 String aName = String(ModuleRes(STR_TBL_TITLE));
350*cdf0e10cSrcweir 			    aDefaultName = aName.GetToken(0,' ');
351*cdf0e10cSrcweir                 //aDefaultName = getPrivateTitle();
352*cdf0e10cSrcweir                 aDefaultName = ::dbtools::createUniqueName(xTables,aDefaultName);
353*cdf0e10cSrcweir             }
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir             DynamicTableOrQueryNameCheck aNameChecker( getConnection(), CommandType::TABLE );
356*cdf0e10cSrcweir 			OSaveAsDlg aDlg( getView(), CommandType::TABLE, getORB(), getConnection(), aDefaultName, aNameChecker );
357*cdf0e10cSrcweir 			if ( aDlg.Execute() != RET_OK )
358*cdf0e10cSrcweir                 return sal_False;
359*cdf0e10cSrcweir 
360*cdf0e10cSrcweir             m_sName = aDlg.getName();
361*cdf0e10cSrcweir 			sCatalog = aDlg.getCatalog();
362*cdf0e10cSrcweir 			sSchema	 = aDlg.getSchema();
363*cdf0e10cSrcweir 		}
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir 		// did we get a name
366*cdf0e10cSrcweir 		if(!m_sName.getLength())
367*cdf0e10cSrcweir 			return sal_False;
368*cdf0e10cSrcweir 	}
369*cdf0e10cSrcweir 	catch(Exception&)
370*cdf0e10cSrcweir 	{
371*cdf0e10cSrcweir 		OSL_ENSURE(sal_False, "OTableController::doSaveDoc: nothing is expected to happen here!");
372*cdf0e10cSrcweir 	}
373*cdf0e10cSrcweir 
374*cdf0e10cSrcweir 	sal_Bool bAlter = sal_False;
375*cdf0e10cSrcweir 	sal_Bool bError = sal_False;
376*cdf0e10cSrcweir 	SQLExceptionInfo aInfo;
377*cdf0e10cSrcweir 	try
378*cdf0e10cSrcweir 	{
379*cdf0e10cSrcweir 		// check the columns for double names
380*cdf0e10cSrcweir 		if(!checkColumns(bNew || !xTables->hasByName(m_sName)))
381*cdf0e10cSrcweir 		{
382*cdf0e10cSrcweir 			// #105323# OJ
383*cdf0e10cSrcweir 			return sal_False;
384*cdf0e10cSrcweir 		}
385*cdf0e10cSrcweir 
386*cdf0e10cSrcweir 		Reference<XPropertySet> xTable;
387*cdf0e10cSrcweir 		if(bNew || !xTables->hasByName(m_sName)) // just to make sure the table already exists
388*cdf0e10cSrcweir 		{
389*cdf0e10cSrcweir 			dropTable(xTables,m_sName);
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir 			Reference<XDataDescriptorFactory> xFact(xTables,UNO_QUERY);
392*cdf0e10cSrcweir 			OSL_ENSURE(xFact.is(),"OTableController::doSaveDoc: No XDataDescriptorFactory available!");
393*cdf0e10cSrcweir 			xTable = xFact->createDataDescriptor();
394*cdf0e10cSrcweir 			OSL_ENSURE(xTable.is(),"OTableController::doSaveDoc: Create query failed!");
395*cdf0e10cSrcweir 			// to set the name is only allowed when the wuery is new
396*cdf0e10cSrcweir 			xTable->setPropertyValue(PROPERTY_CATALOGNAME,makeAny(sCatalog));
397*cdf0e10cSrcweir 			xTable->setPropertyValue(PROPERTY_SCHEMANAME,makeAny(sSchema));
398*cdf0e10cSrcweir 			xTable->setPropertyValue(PROPERTY_NAME,makeAny(m_sName));
399*cdf0e10cSrcweir 
400*cdf0e10cSrcweir 			// now append the columns
401*cdf0e10cSrcweir 			Reference<XColumnsSupplier> xColSup(xTable,UNO_QUERY);
402*cdf0e10cSrcweir 			appendColumns(xColSup,bNew);
403*cdf0e10cSrcweir 			// now append the primary key
404*cdf0e10cSrcweir 			Reference<XKeysSupplier> xKeySup(xTable,UNO_QUERY);
405*cdf0e10cSrcweir 			appendPrimaryKey(xKeySup,bNew);
406*cdf0e10cSrcweir 		}
407*cdf0e10cSrcweir 		// now set the properties
408*cdf0e10cSrcweir 		if(bNew)
409*cdf0e10cSrcweir 		{
410*cdf0e10cSrcweir 			Reference<XAppend> xAppend(xTables,UNO_QUERY);
411*cdf0e10cSrcweir 			OSL_ENSURE(xAppend.is(),"OTableController::doSaveDoc: No XAppend Interface!");
412*cdf0e10cSrcweir 			xAppend->appendByDescriptor(xTable);
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir 			assignTable();
415*cdf0e10cSrcweir 			if(!m_xTable.is()) // correct name and try again
416*cdf0e10cSrcweir 			{
417*cdf0e10cSrcweir 				// it can be that someone inserted new data for us
418*cdf0e10cSrcweir 				m_sName = ::dbtools::composeTableName( getConnection()->getMetaData(), xTable, ::dbtools::eInDataManipulation, false, false, false );
419*cdf0e10cSrcweir 				assignTable();
420*cdf0e10cSrcweir 			}
421*cdf0e10cSrcweir 			// now check if our datasource has set a tablefilter and if append the new table name to it
422*cdf0e10cSrcweir 			::dbaui::appendToFilter(getConnection(),m_sName,getORB(),getView()); // we are not interessted in the return value
423*cdf0e10cSrcweir             Reference< frame::XTitleChangeListener> xEventListener(impl_getTitleHelper_throw(),UNO_QUERY);
424*cdf0e10cSrcweir             if ( xEventListener.is() )
425*cdf0e10cSrcweir             {
426*cdf0e10cSrcweir                 frame::TitleChangedEvent aEvent;
427*cdf0e10cSrcweir                 xEventListener->titleChanged(aEvent);
428*cdf0e10cSrcweir             }
429*cdf0e10cSrcweir             releaseNumberForComponent();
430*cdf0e10cSrcweir 		}
431*cdf0e10cSrcweir 		else if(m_xTable.is())
432*cdf0e10cSrcweir 		{
433*cdf0e10cSrcweir 			bAlter = sal_True;
434*cdf0e10cSrcweir 			alterColumns();
435*cdf0e10cSrcweir 		}
436*cdf0e10cSrcweir 		reSyncRows();
437*cdf0e10cSrcweir 	}
438*cdf0e10cSrcweir 	catch(const SQLContext& e)
439*cdf0e10cSrcweir 	{
440*cdf0e10cSrcweir 		aInfo = SQLExceptionInfo(e);
441*cdf0e10cSrcweir 	}
442*cdf0e10cSrcweir 	catch(const SQLWarning& e)
443*cdf0e10cSrcweir 	{
444*cdf0e10cSrcweir 		aInfo = SQLExceptionInfo(e);
445*cdf0e10cSrcweir 	}
446*cdf0e10cSrcweir 	catch(const SQLException& e)
447*cdf0e10cSrcweir 	{
448*cdf0e10cSrcweir 		aInfo = SQLExceptionInfo(e);
449*cdf0e10cSrcweir 	}
450*cdf0e10cSrcweir 	catch(const ElementExistException& )
451*cdf0e10cSrcweir 	{
452*cdf0e10cSrcweir 		String sText( ModuleRes( STR_NAME_ALREADY_EXISTS ) );
453*cdf0e10cSrcweir 		sText.SearchAndReplaceAscii( "#" , m_sName);
454*cdf0e10cSrcweir 		OSQLMessageBox aDlg( getView(), String( ModuleRes( STR_ERROR_DURING_CREATION ) ), sText, WB_OK, OSQLMessageBox::Error );
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir 		aDlg.Execute();
457*cdf0e10cSrcweir 		bError = sal_True;
458*cdf0e10cSrcweir 	}
459*cdf0e10cSrcweir     catch( const Exception& )
460*cdf0e10cSrcweir     {
461*cdf0e10cSrcweir         bError = sal_True;
462*cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
463*cdf0e10cSrcweir     }
464*cdf0e10cSrcweir 
465*cdf0e10cSrcweir     if ( aInfo.isValid() )
466*cdf0e10cSrcweir         aInfo.prepend( String( ModuleRes( STR_TABLEDESIGN_SAVE_ERROR ) ) );
467*cdf0e10cSrcweir 	showError(aInfo);
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir 	if (aInfo.isValid() || bError)
470*cdf0e10cSrcweir 	{
471*cdf0e10cSrcweir 		if(!bAlter || bNew)
472*cdf0e10cSrcweir 		{
473*cdf0e10cSrcweir 			m_sName = ::rtl::OUString();
474*cdf0e10cSrcweir 			stopTableListening();
475*cdf0e10cSrcweir 			m_xTable = NULL;
476*cdf0e10cSrcweir 		}
477*cdf0e10cSrcweir 		//	reload(); // a error occured so we have to reload
478*cdf0e10cSrcweir 	}
479*cdf0e10cSrcweir 	return ! (aInfo.isValid() || bError);
480*cdf0e10cSrcweir }
481*cdf0e10cSrcweir 
482*cdf0e10cSrcweir // -----------------------------------------------------------------------------
483*cdf0e10cSrcweir void OTableController::doEditIndexes()
484*cdf0e10cSrcweir {
485*cdf0e10cSrcweir 	// table needs to be saved before editing indexes
486*cdf0e10cSrcweir 	if (m_bNew || isModified())
487*cdf0e10cSrcweir 	{
488*cdf0e10cSrcweir 		QueryBox aAsk(getView(), ModuleRes(QUERY_SAVE_TABLE_EDIT_INDEXES));
489*cdf0e10cSrcweir 		if (RET_YES != aAsk.Execute())
490*cdf0e10cSrcweir 			return;
491*cdf0e10cSrcweir 
492*cdf0e10cSrcweir 		if (!doSaveDoc(sal_False))
493*cdf0e10cSrcweir 			return;
494*cdf0e10cSrcweir 
495*cdf0e10cSrcweir 		OSL_ENSURE(!m_bNew && !isModified(), "OTableController::doEditIndexes: what the hell did doSaveDoc do?");
496*cdf0e10cSrcweir 	}
497*cdf0e10cSrcweir 
498*cdf0e10cSrcweir 	Reference< XNameAccess > xIndexes;			// will be the keys of the table
499*cdf0e10cSrcweir 	Sequence< ::rtl::OUString > aFieldNames;	// will be the column names of the table
500*cdf0e10cSrcweir 	try
501*cdf0e10cSrcweir 	{
502*cdf0e10cSrcweir 		// get the keys
503*cdf0e10cSrcweir 		Reference< XIndexesSupplier > xIndexesSupp(m_xTable, UNO_QUERY);
504*cdf0e10cSrcweir 		if (xIndexesSupp.is())
505*cdf0e10cSrcweir 		{
506*cdf0e10cSrcweir 			xIndexes = xIndexesSupp->getIndexes();
507*cdf0e10cSrcweir 			OSL_ENSURE(xIndexes.is(), "OTableController::doEditIndexes: no keys got from the indexes supplier!");
508*cdf0e10cSrcweir 		}
509*cdf0e10cSrcweir 		else
510*cdf0e10cSrcweir 			OSL_ENSURE(sal_False, "OTableController::doEditIndexes: should never have reached this (no indexes supplier)!");
511*cdf0e10cSrcweir 
512*cdf0e10cSrcweir 		// get the field names
513*cdf0e10cSrcweir 		Reference< XColumnsSupplier > xColSupp(m_xTable, UNO_QUERY);
514*cdf0e10cSrcweir 		OSL_ENSURE(xColSupp.is(), "OTableController::doEditIndexes: no columns supplier!");
515*cdf0e10cSrcweir 		if (xColSupp.is())
516*cdf0e10cSrcweir 		{
517*cdf0e10cSrcweir 			Reference< XNameAccess > xCols = xColSupp->getColumns();
518*cdf0e10cSrcweir 			OSL_ENSURE(xCols.is(), "OTableController::doEditIndexes: no columns!");
519*cdf0e10cSrcweir 			if (xCols.is())
520*cdf0e10cSrcweir 				aFieldNames = xCols->getElementNames();
521*cdf0e10cSrcweir 		}
522*cdf0e10cSrcweir 	}
523*cdf0e10cSrcweir     catch( const Exception& )
524*cdf0e10cSrcweir     {
525*cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
526*cdf0e10cSrcweir     }
527*cdf0e10cSrcweir 
528*cdf0e10cSrcweir 	if (!xIndexes.is())
529*cdf0e10cSrcweir 		return;
530*cdf0e10cSrcweir 
531*cdf0e10cSrcweir 	DbaIndexDialog aDialog(getView(), aFieldNames, xIndexes, getConnection(),getORB(),isConnected() ? getConnection()->getMetaData().is() && getConnection()->getMetaData()->getMaxColumnsInIndex() : sal_Int32(0));
532*cdf0e10cSrcweir 	if (RET_OK != aDialog.Execute())
533*cdf0e10cSrcweir 		return;
534*cdf0e10cSrcweir 
535*cdf0e10cSrcweir }
536*cdf0e10cSrcweir 
537*cdf0e10cSrcweir // -----------------------------------------------------------------------------
538*cdf0e10cSrcweir void OTableController::impl_initialize()
539*cdf0e10cSrcweir {
540*cdf0e10cSrcweir 	try
541*cdf0e10cSrcweir 	{
542*cdf0e10cSrcweir 		OTableController_BASE::impl_initialize();
543*cdf0e10cSrcweir 
544*cdf0e10cSrcweir         const NamedValueCollection& rArguments( getInitParams() );
545*cdf0e10cSrcweir 
546*cdf0e10cSrcweir         rArguments.get_ensureType( (::rtl::OUString)PROPERTY_CURRENTTABLE, m_sName );
547*cdf0e10cSrcweir 
548*cdf0e10cSrcweir         // read autoincrement value set in the datasource
549*cdf0e10cSrcweir 		::dbaui::fillAutoIncrementValue(getDataSource(),m_bAllowAutoIncrementValue,m_sAutoIncrementValue);
550*cdf0e10cSrcweir 
551*cdf0e10cSrcweir 		assignTable();
552*cdf0e10cSrcweir 	}
553*cdf0e10cSrcweir     catch( const Exception& )
554*cdf0e10cSrcweir     {
555*cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
556*cdf0e10cSrcweir     }
557*cdf0e10cSrcweir 
558*cdf0e10cSrcweir 	try
559*cdf0e10cSrcweir 	{
560*cdf0e10cSrcweir 		::dbaui::fillTypeInfo(getConnection(),m_sTypeNames,m_aTypeInfo,m_aTypeInfoIndex);				// fill the needed type information
561*cdf0e10cSrcweir 	}
562*cdf0e10cSrcweir 	catch(const SQLException&)
563*cdf0e10cSrcweir 	{
564*cdf0e10cSrcweir 		OSQLWarningBox( getView(), ModuleRes( STR_NO_TYPE_INFO_AVAILABLE ) ).Execute();
565*cdf0e10cSrcweir 		throw;
566*cdf0e10cSrcweir 	}
567*cdf0e10cSrcweir 	try
568*cdf0e10cSrcweir 	{
569*cdf0e10cSrcweir 		loadData();					// fill the column information form the table
570*cdf0e10cSrcweir 		getView()->initialize();	// show the windows and fill with our informations
571*cdf0e10cSrcweir 		ClearUndoManager();
572*cdf0e10cSrcweir 		setModified(sal_False);		// and we are not modified yet
573*cdf0e10cSrcweir 	}
574*cdf0e10cSrcweir     catch( const Exception& )
575*cdf0e10cSrcweir     {
576*cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
577*cdf0e10cSrcweir     }
578*cdf0e10cSrcweir }
579*cdf0e10cSrcweir // -----------------------------------------------------------------------------
580*cdf0e10cSrcweir sal_Bool OTableController::Construct(Window* pParent)
581*cdf0e10cSrcweir {
582*cdf0e10cSrcweir 	setView( * new OTableDesignView( pParent, getORB(), *this ) );
583*cdf0e10cSrcweir 	OTableController_BASE::Construct(pParent);
584*cdf0e10cSrcweir //	m_pView->Construct();
585*cdf0e10cSrcweir //	m_pView->Show();
586*cdf0e10cSrcweir 	return sal_True;
587*cdf0e10cSrcweir }
588*cdf0e10cSrcweir // -----------------------------------------------------------------------------
589*cdf0e10cSrcweir sal_Bool SAL_CALL OTableController::suspend(sal_Bool /*_bSuspend*/) throw( RuntimeException )
590*cdf0e10cSrcweir {
591*cdf0e10cSrcweir 	if ( getBroadcastHelper().bInDispose || getBroadcastHelper().bDisposed )
592*cdf0e10cSrcweir 		return sal_True;
593*cdf0e10cSrcweir 
594*cdf0e10cSrcweir 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
595*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( getMutex() );
596*cdf0e10cSrcweir     if ( getView() && getView()->IsInModalMode() )
597*cdf0e10cSrcweir         return sal_False;
598*cdf0e10cSrcweir     if ( getView() )
599*cdf0e10cSrcweir         static_cast<OTableDesignView*>(getView())->GrabFocus();
600*cdf0e10cSrcweir 	sal_Bool bCheck = sal_True;
601*cdf0e10cSrcweir 	if ( isModified() )
602*cdf0e10cSrcweir 	{
603*cdf0e10cSrcweir 		::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aIter = ::std::find_if(m_vRowList.begin(),m_vRowList.end(),
604*cdf0e10cSrcweir             ::boost::mem_fn(&OTableRow::isValid));
605*cdf0e10cSrcweir 		if ( aIter != m_vRowList.end() )
606*cdf0e10cSrcweir 		{
607*cdf0e10cSrcweir 			QueryBox aQry(getView(), ModuleRes(TABLE_DESIGN_SAVEMODIFIED));
608*cdf0e10cSrcweir 			switch (aQry.Execute())
609*cdf0e10cSrcweir 			{
610*cdf0e10cSrcweir 				case RET_YES:
611*cdf0e10cSrcweir 					Execute(ID_BROWSER_SAVEDOC,Sequence<PropertyValue>());
612*cdf0e10cSrcweir 					if ( isModified() )
613*cdf0e10cSrcweir 						bCheck = sal_False; // when we save the table this must be false else some press cancel
614*cdf0e10cSrcweir 					break;
615*cdf0e10cSrcweir 				case RET_CANCEL:
616*cdf0e10cSrcweir 					bCheck = sal_False;
617*cdf0e10cSrcweir 				default:
618*cdf0e10cSrcweir 					break;
619*cdf0e10cSrcweir 			}
620*cdf0e10cSrcweir 		}
621*cdf0e10cSrcweir 		else if ( !m_bNew )
622*cdf0e10cSrcweir 		{
623*cdf0e10cSrcweir 			QueryBox aQry(getView(), ModuleRes(TABLE_DESIGN_ALL_ROWS_DELETED));
624*cdf0e10cSrcweir 			switch (aQry.Execute())
625*cdf0e10cSrcweir 			{
626*cdf0e10cSrcweir 				case RET_YES:
627*cdf0e10cSrcweir 					{
628*cdf0e10cSrcweir 						try
629*cdf0e10cSrcweir 						{
630*cdf0e10cSrcweir 							Reference<XTablesSupplier> xTablesSup(getConnection(),UNO_QUERY);
631*cdf0e10cSrcweir 							Reference<XNameAccess> xTables = xTablesSup->getTables();
632*cdf0e10cSrcweir 							dropTable(xTables,m_sName);
633*cdf0e10cSrcweir 						}
634*cdf0e10cSrcweir 						catch(const Exception&)
635*cdf0e10cSrcweir 						{
636*cdf0e10cSrcweir 							OSL_ENSURE(sal_False, "OTableController::suspend: nothing is expected to happen here!");
637*cdf0e10cSrcweir 						}
638*cdf0e10cSrcweir 
639*cdf0e10cSrcweir 					}
640*cdf0e10cSrcweir 					break;
641*cdf0e10cSrcweir 				case RET_CANCEL:
642*cdf0e10cSrcweir 					bCheck = sal_False;
643*cdf0e10cSrcweir 				default:
644*cdf0e10cSrcweir 					break;
645*cdf0e10cSrcweir 			}
646*cdf0e10cSrcweir 		}
647*cdf0e10cSrcweir 	}
648*cdf0e10cSrcweir /*
649*cdf0e10cSrcweir 	if ( bCheck )
650*cdf0e10cSrcweir 		OSingleDocumentController::suspend(_bSuspend);
651*cdf0e10cSrcweir */
652*cdf0e10cSrcweir 	return bCheck;
653*cdf0e10cSrcweir }
654*cdf0e10cSrcweir // -----------------------------------------------------------------------------
655*cdf0e10cSrcweir void OTableController::describeSupportedFeatures()
656*cdf0e10cSrcweir {
657*cdf0e10cSrcweir 	OSingleDocumentController::describeSupportedFeatures();
658*cdf0e10cSrcweir 
659*cdf0e10cSrcweir     implDescribeSupportedFeature( ".uno:Redo",          ID_BROWSER_REDO,        CommandGroup::EDIT );
660*cdf0e10cSrcweir 	implDescribeSupportedFeature( ".uno:Save",          ID_BROWSER_SAVEDOC,     CommandGroup::EDIT );
661*cdf0e10cSrcweir 	implDescribeSupportedFeature( ".uno:Undo",          ID_BROWSER_UNDO,        CommandGroup::EDIT );
662*cdf0e10cSrcweir 	implDescribeSupportedFeature( ".uno:HelpMenu",      SID_HELPMENU,           CommandGroup::APPLICATION );
663*cdf0e10cSrcweir 	implDescribeSupportedFeature( ".uno:NewDoc",        SID_NEWDOC,             CommandGroup::DOCUMENT );
664*cdf0e10cSrcweir 	implDescribeSupportedFeature( ".uno:SaveAs",        ID_BROWSER_SAVEASDOC,   CommandGroup::DOCUMENT );
665*cdf0e10cSrcweir 	implDescribeSupportedFeature( ".uno:DBIndexDesign", SID_INDEXDESIGN,        CommandGroup::APPLICATION );
666*cdf0e10cSrcweir 	implDescribeSupportedFeature( ".uno:EditDoc",       ID_BROWSER_EDITDOC,     CommandGroup::EDIT );
667*cdf0e10cSrcweir }
668*cdf0e10cSrcweir // -----------------------------------------------------------------------------
669*cdf0e10cSrcweir void OTableController::impl_onModifyChanged()
670*cdf0e10cSrcweir {
671*cdf0e10cSrcweir     OSingleDocumentController::impl_onModifyChanged();
672*cdf0e10cSrcweir     InvalidateFeature( SID_INDEXDESIGN );
673*cdf0e10cSrcweir }
674*cdf0e10cSrcweir // -----------------------------------------------------------------------------
675*cdf0e10cSrcweir void SAL_CALL OTableController::disposing( const EventObject& _rSource ) throw(RuntimeException)
676*cdf0e10cSrcweir {
677*cdf0e10cSrcweir 	if ( _rSource.Source == m_xTable )
678*cdf0e10cSrcweir 	{	// some deleted our table so we have a new one
679*cdf0e10cSrcweir 		stopTableListening();
680*cdf0e10cSrcweir 		m_xTable	= NULL;
681*cdf0e10cSrcweir 		m_bNew		= sal_True;
682*cdf0e10cSrcweir 		setModified(sal_True);
683*cdf0e10cSrcweir 	}
684*cdf0e10cSrcweir 	else
685*cdf0e10cSrcweir 		OTableController_BASE::disposing( _rSource );
686*cdf0e10cSrcweir }
687*cdf0e10cSrcweir // -----------------------------------------------------------------------------
688*cdf0e10cSrcweir void OTableController::Save(const Reference< XObjectOutputStream>& _rxOut)
689*cdf0e10cSrcweir {
690*cdf0e10cSrcweir 	OStreamSection aSection(_rxOut.get());
691*cdf0e10cSrcweir 
692*cdf0e10cSrcweir }
693*cdf0e10cSrcweir // -----------------------------------------------------------------------------
694*cdf0e10cSrcweir void OTableController::Load(const Reference< XObjectInputStream>& _rxIn)
695*cdf0e10cSrcweir {
696*cdf0e10cSrcweir 	OStreamSection aSection(_rxIn.get());
697*cdf0e10cSrcweir }
698*cdf0e10cSrcweir 
699*cdf0e10cSrcweir // -----------------------------------------------------------------------------
700*cdf0e10cSrcweir void OTableController::losingConnection( )
701*cdf0e10cSrcweir {
702*cdf0e10cSrcweir 	// let the base class do it's reconnect
703*cdf0e10cSrcweir 	OTableController_BASE::losingConnection( );
704*cdf0e10cSrcweir 
705*cdf0e10cSrcweir 	// remove from the table
706*cdf0e10cSrcweir 	Reference< XComponent >  xComponent(m_xTable, UNO_QUERY);
707*cdf0e10cSrcweir 	if (xComponent.is())
708*cdf0e10cSrcweir 	{
709*cdf0e10cSrcweir 		Reference<XEventListener> xEvtL( static_cast< ::cppu::OWeakObject*>(this), UNO_QUERY);
710*cdf0e10cSrcweir 		xComponent->removeEventListener(xEvtL);
711*cdf0e10cSrcweir 	}
712*cdf0e10cSrcweir 	stopTableListening();
713*cdf0e10cSrcweir 	m_xTable	= NULL;
714*cdf0e10cSrcweir 	assignTable();
715*cdf0e10cSrcweir 	if(!m_xTable.is())
716*cdf0e10cSrcweir 	{
717*cdf0e10cSrcweir 		m_bNew		= sal_True;
718*cdf0e10cSrcweir 		setModified(sal_True);
719*cdf0e10cSrcweir 	}
720*cdf0e10cSrcweir 	InvalidateAll();
721*cdf0e10cSrcweir }
722*cdf0e10cSrcweir // -----------------------------------------------------------------------------
723*cdf0e10cSrcweir TOTypeInfoSP OTableController::getTypeInfoByType(sal_Int32 _nDataType) const
724*cdf0e10cSrcweir {
725*cdf0e10cSrcweir 	return queryTypeInfoByType(_nDataType,m_aTypeInfo);
726*cdf0e10cSrcweir }
727*cdf0e10cSrcweir // -----------------------------------------------------------------------------
728*cdf0e10cSrcweir void OTableController::appendColumns(Reference<XColumnsSupplier>& _rxColSup,sal_Bool _bNew,sal_Bool _bKeyColumns)
729*cdf0e10cSrcweir {
730*cdf0e10cSrcweir 	try
731*cdf0e10cSrcweir 	{
732*cdf0e10cSrcweir 		// now append the columns
733*cdf0e10cSrcweir 		OSL_ENSURE(_rxColSup.is(),"No columns supplier");
734*cdf0e10cSrcweir 		if(!_rxColSup.is())
735*cdf0e10cSrcweir 			return;
736*cdf0e10cSrcweir 		Reference<XNameAccess> xColumns = _rxColSup->getColumns();
737*cdf0e10cSrcweir 		OSL_ENSURE(xColumns.is(),"No columns");
738*cdf0e10cSrcweir 		Reference<XDataDescriptorFactory> xColumnFactory(xColumns,UNO_QUERY);
739*cdf0e10cSrcweir 
740*cdf0e10cSrcweir 		Reference<XAppend> xAppend(xColumns,UNO_QUERY);
741*cdf0e10cSrcweir 		OSL_ENSURE(xAppend.is(),"No XAppend Interface!");
742*cdf0e10cSrcweir 
743*cdf0e10cSrcweir 		::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aIter = m_vRowList.begin();
744*cdf0e10cSrcweir 		::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aEnd = m_vRowList.end();
745*cdf0e10cSrcweir 		for(;aIter != aEnd;++aIter)
746*cdf0e10cSrcweir 		{
747*cdf0e10cSrcweir 			OSL_ENSURE(*aIter,"OTableRow is null!");
748*cdf0e10cSrcweir 			OFieldDescription* pField = (*aIter)->GetActFieldDescr();
749*cdf0e10cSrcweir 			if ( !pField || (!_bNew && (*aIter)->IsReadOnly() && !_bKeyColumns) )
750*cdf0e10cSrcweir 				continue;
751*cdf0e10cSrcweir 
752*cdf0e10cSrcweir 			Reference<XPropertySet> xColumn;
753*cdf0e10cSrcweir 			if(pField->IsPrimaryKey() || !_bKeyColumns)
754*cdf0e10cSrcweir 				xColumn = xColumnFactory->createDataDescriptor();
755*cdf0e10cSrcweir 			if(xColumn.is())
756*cdf0e10cSrcweir 			{
757*cdf0e10cSrcweir 				if(!_bKeyColumns)
758*cdf0e10cSrcweir 					::dbaui::setColumnProperties(xColumn,pField);
759*cdf0e10cSrcweir 				else
760*cdf0e10cSrcweir 					xColumn->setPropertyValue(PROPERTY_NAME,makeAny(pField->GetName()));
761*cdf0e10cSrcweir 
762*cdf0e10cSrcweir 				xAppend->appendByDescriptor(xColumn);
763*cdf0e10cSrcweir 				xColumn = NULL;
764*cdf0e10cSrcweir 				// now only the settings are missing
765*cdf0e10cSrcweir 				if(xColumns->hasByName(pField->GetName()))
766*cdf0e10cSrcweir 				{
767*cdf0e10cSrcweir 					xColumns->getByName(pField->GetName()) >>= xColumn;
768*cdf0e10cSrcweir 					if(xColumn.is())
769*cdf0e10cSrcweir 						pField->copyColumnSettingsTo(xColumn);
770*cdf0e10cSrcweir 				}
771*cdf0e10cSrcweir 				else
772*cdf0e10cSrcweir 				{
773*cdf0e10cSrcweir 					OSL_ENSURE(sal_False, "OTableController::appendColumns: invalid field name!");
774*cdf0e10cSrcweir 				}
775*cdf0e10cSrcweir 
776*cdf0e10cSrcweir 			}
777*cdf0e10cSrcweir 		}
778*cdf0e10cSrcweir 	}
779*cdf0e10cSrcweir 	catch(const SQLException& )
780*cdf0e10cSrcweir 	{
781*cdf0e10cSrcweir         showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
782*cdf0e10cSrcweir 	}
783*cdf0e10cSrcweir     catch( const Exception& )
784*cdf0e10cSrcweir     {
785*cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
786*cdf0e10cSrcweir     }
787*cdf0e10cSrcweir }
788*cdf0e10cSrcweir // -----------------------------------------------------------------------------
789*cdf0e10cSrcweir void OTableController::appendPrimaryKey(Reference<XKeysSupplier>& _rxSup,sal_Bool _bNew)
790*cdf0e10cSrcweir {
791*cdf0e10cSrcweir 	if(!_rxSup.is())
792*cdf0e10cSrcweir 		return; // the database doesn't support keys
793*cdf0e10cSrcweir 
794*cdf0e10cSrcweir 	OSL_ENSURE(_rxSup.is(),"No XKeysSupplier!");
795*cdf0e10cSrcweir     Reference<XIndexAccess> xKeys(_rxSup->getKeys(),UNO_QUERY);
796*cdf0e10cSrcweir     Reference<XPropertySet> xProp;
797*cdf0e10cSrcweir     const sal_Int32 nCount = xKeys->getCount();
798*cdf0e10cSrcweir 	for(sal_Int32 i=0;i< nCount ;++i)
799*cdf0e10cSrcweir 	{
800*cdf0e10cSrcweir 		xKeys->getByIndex(i) >>= xProp;
801*cdf0e10cSrcweir 		sal_Int32 nKeyType = 0;
802*cdf0e10cSrcweir 		xProp->getPropertyValue(PROPERTY_TYPE) >>= nKeyType;
803*cdf0e10cSrcweir 		if(KeyType::PRIMARY == nKeyType)
804*cdf0e10cSrcweir 		{
805*cdf0e10cSrcweir             return; // primary key already exists after appending a column
806*cdf0e10cSrcweir 		}
807*cdf0e10cSrcweir 	}
808*cdf0e10cSrcweir 	Reference<XDataDescriptorFactory> xKeyFactory(xKeys,UNO_QUERY);
809*cdf0e10cSrcweir 	OSL_ENSURE(xKeyFactory.is(),"No XDataDescriptorFactory Interface!");
810*cdf0e10cSrcweir 	if ( !xKeyFactory.is() )
811*cdf0e10cSrcweir 		return;
812*cdf0e10cSrcweir 	Reference<XAppend> xAppend(xKeyFactory,UNO_QUERY);
813*cdf0e10cSrcweir 	OSL_ENSURE(xAppend.is(),"No XAppend Interface!");
814*cdf0e10cSrcweir 
815*cdf0e10cSrcweir 	Reference<XPropertySet> xKey = xKeyFactory->createDataDescriptor();
816*cdf0e10cSrcweir 	OSL_ENSURE(xKey.is(),"Key is null!");
817*cdf0e10cSrcweir 	xKey->setPropertyValue(PROPERTY_TYPE,makeAny(KeyType::PRIMARY));
818*cdf0e10cSrcweir 
819*cdf0e10cSrcweir 	Reference<XColumnsSupplier> xColSup(xKey,UNO_QUERY);
820*cdf0e10cSrcweir 	if(xColSup.is())
821*cdf0e10cSrcweir 	{
822*cdf0e10cSrcweir 		appendColumns(xColSup,_bNew,sal_True);
823*cdf0e10cSrcweir 		Reference<XNameAccess> xColumns = xColSup->getColumns();
824*cdf0e10cSrcweir 		if(xColumns->hasElements())
825*cdf0e10cSrcweir 			xAppend->appendByDescriptor(xKey);
826*cdf0e10cSrcweir 	}
827*cdf0e10cSrcweir }
828*cdf0e10cSrcweir // -----------------------------------------------------------------------------
829*cdf0e10cSrcweir void OTableController::loadData()
830*cdf0e10cSrcweir {
831*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////
832*cdf0e10cSrcweir 	// Wenn Datenstruktur bereits vorhanden, Struktur leeren
833*cdf0e10cSrcweir 	m_vRowList.clear();
834*cdf0e10cSrcweir 
835*cdf0e10cSrcweir 	::boost::shared_ptr<OTableRow>  pTabEdRow;
836*cdf0e10cSrcweir 	Reference< XDatabaseMetaData> xMetaData = getMetaData( );
837*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////
838*cdf0e10cSrcweir 	// Datenstruktur mit Daten aus DatenDefinitionsObjekt fuellen
839*cdf0e10cSrcweir 	if(m_xTable.is() && xMetaData.is())
840*cdf0e10cSrcweir 	{
841*cdf0e10cSrcweir 		Reference<XColumnsSupplier> xColSup(m_xTable,UNO_QUERY);
842*cdf0e10cSrcweir 		OSL_ENSURE(xColSup.is(),"No XColumnsSupplier!");
843*cdf0e10cSrcweir 		Reference<XNameAccess> xColumns = xColSup->getColumns();
844*cdf0e10cSrcweir 		OFieldDescription* pActFieldDescr = NULL;
845*cdf0e10cSrcweir 		String aType;
846*cdf0e10cSrcweir 		//////////////////////////////////////////////////////////////////////
847*cdf0e10cSrcweir 		// ReadOnly-Flag
848*cdf0e10cSrcweir 		// Bei Drop darf keine Zeile editierbar sein.
849*cdf0e10cSrcweir 		// Bei Add duerfen nur die leeren Zeilen editierbar sein.
850*cdf0e10cSrcweir 		// Bei Add und Drop koennen alle Zeilen editiert werden.
851*cdf0e10cSrcweir 		//	sal_Bool bReadOldRow = xMetaData->supportsAlterTableWithAddColumn() && xMetaData->supportsAlterTableWithDropColumn();
852*cdf0e10cSrcweir 		sal_Bool bIsAlterAllowed = isAlterAllowed();
853*cdf0e10cSrcweir 		Sequence< ::rtl::OUString> aColumns = xColumns->getElementNames();
854*cdf0e10cSrcweir 		const ::rtl::OUString* pIter	= aColumns.getConstArray();
855*cdf0e10cSrcweir 		const ::rtl::OUString* pEnd		= pIter + aColumns.getLength();
856*cdf0e10cSrcweir 
857*cdf0e10cSrcweir 		for(;pIter != pEnd;++pIter)
858*cdf0e10cSrcweir 		{
859*cdf0e10cSrcweir 			Reference<XPropertySet> xColumn;
860*cdf0e10cSrcweir 			xColumns->getByName(*pIter) >>= xColumn;
861*cdf0e10cSrcweir 			sal_Int32 nType			= 0;
862*cdf0e10cSrcweir 			sal_Int32 nScale		= 0;
863*cdf0e10cSrcweir 			sal_Int32 nPrecision	= 0;
864*cdf0e10cSrcweir 			sal_Int32 nNullable		= 0;
865*cdf0e10cSrcweir 			sal_Int32 nFormatKey	= 0;
866*cdf0e10cSrcweir 			sal_Int32 nAlign		= 0;
867*cdf0e10cSrcweir 
868*cdf0e10cSrcweir 			sal_Bool bIsAutoIncrement = false, bIsCurrency = false;
869*cdf0e10cSrcweir 			::rtl::OUString sName,sDescription,sTypeName,sHelpText;
870*cdf0e10cSrcweir 			Any aControlDefault;
871*cdf0e10cSrcweir 
872*cdf0e10cSrcweir 			// get the properties from the column
873*cdf0e10cSrcweir 			xColumn->getPropertyValue(PROPERTY_NAME)			>>= sName;
874*cdf0e10cSrcweir 			xColumn->getPropertyValue(PROPERTY_TYPENAME)		>>= sTypeName;
875*cdf0e10cSrcweir 			xColumn->getPropertyValue(PROPERTY_ISNULLABLE)		>>= nNullable;
876*cdf0e10cSrcweir 			xColumn->getPropertyValue(PROPERTY_ISAUTOINCREMENT)	>>= bIsAutoIncrement;
877*cdf0e10cSrcweir 			xColumn->getPropertyValue(PROPERTY_ISCURRENCY)		>>= bIsCurrency;
878*cdf0e10cSrcweir 			xColumn->getPropertyValue(PROPERTY_TYPE)			>>= nType;
879*cdf0e10cSrcweir 			xColumn->getPropertyValue(PROPERTY_SCALE)			>>= nScale;
880*cdf0e10cSrcweir 			xColumn->getPropertyValue(PROPERTY_PRECISION)		>>= nPrecision;
881*cdf0e10cSrcweir             xColumn->getPropertyValue(PROPERTY_DESCRIPTION)	    >>= sDescription;
882*cdf0e10cSrcweir 
883*cdf0e10cSrcweir 			if(xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_HELPTEXT))
884*cdf0e10cSrcweir 				xColumn->getPropertyValue(PROPERTY_HELPTEXT)	>>= sHelpText;
885*cdf0e10cSrcweir 
886*cdf0e10cSrcweir 			if(xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_CONTROLDEFAULT))
887*cdf0e10cSrcweir 				aControlDefault = xColumn->getPropertyValue(PROPERTY_CONTROLDEFAULT);
888*cdf0e10cSrcweir 			if(xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_FORMATKEY))
889*cdf0e10cSrcweir 				xColumn->getPropertyValue(PROPERTY_FORMATKEY)	>>= nFormatKey;
890*cdf0e10cSrcweir 			if(xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_ALIGN))
891*cdf0e10cSrcweir 				xColumn->getPropertyValue(PROPERTY_ALIGN)		>>= nAlign;
892*cdf0e10cSrcweir 
893*cdf0e10cSrcweir 			pTabEdRow.reset(new OTableRow());
894*cdf0e10cSrcweir 			pTabEdRow->SetReadOnly(!bIsAlterAllowed);
895*cdf0e10cSrcweir 			// search for type
896*cdf0e10cSrcweir 			sal_Bool bForce;
897*cdf0e10cSrcweir 			::rtl::OUString sCreate(RTL_CONSTASCII_USTRINGPARAM("x"));
898*cdf0e10cSrcweir 			TOTypeInfoSP pTypeInfo = ::dbaui::getTypeInfoFromType(m_aTypeInfo,nType,sTypeName,sCreate,nPrecision,nScale,bIsAutoIncrement,bForce);
899*cdf0e10cSrcweir 			if ( !pTypeInfo.get() )
900*cdf0e10cSrcweir 				pTypeInfo = m_pTypeInfo;
901*cdf0e10cSrcweir 			pTabEdRow->SetFieldType( pTypeInfo, bForce );
902*cdf0e10cSrcweir 
903*cdf0e10cSrcweir 			pActFieldDescr = pTabEdRow->GetActFieldDescr();
904*cdf0e10cSrcweir 			OSL_ENSURE(pActFieldDescr, "OTableController::loadData: invalid field description generated by the table row!");
905*cdf0e10cSrcweir 			if ( pActFieldDescr )
906*cdf0e10cSrcweir 			{
907*cdf0e10cSrcweir 				pActFieldDescr->SetName(sName);
908*cdf0e10cSrcweir 				pActFieldDescr->SetFormatKey(nFormatKey);
909*cdf0e10cSrcweir 				//	pActFieldDescr->SetPrimaryKey(pPrimary->GetValue());
910*cdf0e10cSrcweir 				pActFieldDescr->SetDescription(sDescription);
911*cdf0e10cSrcweir                 pActFieldDescr->SetHelpText(sHelpText);
912*cdf0e10cSrcweir 				pActFieldDescr->SetAutoIncrement(bIsAutoIncrement);
913*cdf0e10cSrcweir 				pActFieldDescr->SetHorJustify(dbaui::mapTextJustify(nAlign));
914*cdf0e10cSrcweir 				pActFieldDescr->SetCurrency(bIsCurrency);
915*cdf0e10cSrcweir 
916*cdf0e10cSrcweir 				//////////////////////////////////////////////////////////////////////
917*cdf0e10cSrcweir 				// Spezielle Daten
918*cdf0e10cSrcweir 				pActFieldDescr->SetIsNullable(nNullable);
919*cdf0e10cSrcweir 				pActFieldDescr->SetControlDefault(aControlDefault);
920*cdf0e10cSrcweir 				pActFieldDescr->SetPrecision(nPrecision);
921*cdf0e10cSrcweir 				pActFieldDescr->SetScale(nScale);
922*cdf0e10cSrcweir 			}
923*cdf0e10cSrcweir 			m_vRowList.push_back( pTabEdRow);
924*cdf0e10cSrcweir 		}
925*cdf0e10cSrcweir 		// fill the primary  key information
926*cdf0e10cSrcweir 		Reference<XNameAccess> xKeyColumns	= getKeyColumns();
927*cdf0e10cSrcweir 		if(xKeyColumns.is())
928*cdf0e10cSrcweir 		{
929*cdf0e10cSrcweir 			Sequence< ::rtl::OUString> aKeyColumns = xKeyColumns->getElementNames();
930*cdf0e10cSrcweir 			const ::rtl::OUString* pKeyBegin	= aKeyColumns.getConstArray();
931*cdf0e10cSrcweir 			const ::rtl::OUString* pKeyEnd		= pKeyBegin + aKeyColumns.getLength();
932*cdf0e10cSrcweir 
933*cdf0e10cSrcweir 			for(;pKeyBegin != pKeyEnd;++pKeyBegin)
934*cdf0e10cSrcweir 			{
935*cdf0e10cSrcweir 				::std::vector< ::boost::shared_ptr<OTableRow> >::iterator rowIter = m_vRowList.begin();
936*cdf0e10cSrcweir                 ::std::vector< ::boost::shared_ptr<OTableRow> >::iterator rowEnd = m_vRowList.end();
937*cdf0e10cSrcweir                 for(;rowIter != rowEnd;++rowIter)
938*cdf0e10cSrcweir 				{
939*cdf0e10cSrcweir 					if((*rowIter)->GetActFieldDescr()->GetName() == *pKeyBegin)
940*cdf0e10cSrcweir 					{
941*cdf0e10cSrcweir 						(*rowIter)->SetPrimaryKey(sal_True);
942*cdf0e10cSrcweir 						break;
943*cdf0e10cSrcweir 					}
944*cdf0e10cSrcweir 				}
945*cdf0e10cSrcweir 			}
946*cdf0e10cSrcweir 		}
947*cdf0e10cSrcweir 	}
948*cdf0e10cSrcweir 
949*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////
950*cdf0e10cSrcweir 	// Leere Zeilen fuellen
951*cdf0e10cSrcweir 
952*cdf0e10cSrcweir 	OTypeInfoMap::iterator aTypeIter = m_aTypeInfo.find(DataType::VARCHAR);
953*cdf0e10cSrcweir 	if(aTypeIter == m_aTypeInfo.end())
954*cdf0e10cSrcweir 		aTypeIter = m_aTypeInfo.begin();
955*cdf0e10cSrcweir 
956*cdf0e10cSrcweir 	OSL_ENSURE(aTypeIter != m_aTypeInfo.end(),"We have no type infomation!");
957*cdf0e10cSrcweir 
958*cdf0e10cSrcweir 	bool bReadRow = !isAddAllowed();
959*cdf0e10cSrcweir 	for(sal_Int32 i=m_vRowList.size(); i < NEWCOLS; i++ )
960*cdf0e10cSrcweir 	{
961*cdf0e10cSrcweir 		pTabEdRow.reset(new OTableRow());
962*cdf0e10cSrcweir 		pTabEdRow->SetReadOnly(bReadRow);
963*cdf0e10cSrcweir 		m_vRowList.push_back( pTabEdRow);
964*cdf0e10cSrcweir 	}
965*cdf0e10cSrcweir }
966*cdf0e10cSrcweir // -----------------------------------------------------------------------------
967*cdf0e10cSrcweir Reference<XNameAccess> OTableController::getKeyColumns() const
968*cdf0e10cSrcweir {
969*cdf0e10cSrcweir     return getPrimaryKeyColumns_throw(m_xTable);
970*cdf0e10cSrcweir }
971*cdf0e10cSrcweir // -----------------------------------------------------------------------------
972*cdf0e10cSrcweir sal_Bool OTableController::checkColumns(sal_Bool _bNew) throw(::com::sun::star::sdbc::SQLException)
973*cdf0e10cSrcweir {
974*cdf0e10cSrcweir 	sal_Bool bOk = sal_True;
975*cdf0e10cSrcweir 	sal_Bool bFoundPKey = sal_False;
976*cdf0e10cSrcweir 	Reference< XDatabaseMetaData > xMetaData = getMetaData( );
977*cdf0e10cSrcweir     DatabaseMetaData aMetaData( getConnection() );
978*cdf0e10cSrcweir 
979*cdf0e10cSrcweir 	::comphelper::UStringMixEqual bCase(xMetaData.is() ? xMetaData->supportsMixedCaseQuotedIdentifiers() : sal_True);
980*cdf0e10cSrcweir 	::std::vector< ::boost::shared_ptr<OTableRow> >::const_iterator aIter = m_vRowList.begin();
981*cdf0e10cSrcweir     ::std::vector< ::boost::shared_ptr<OTableRow> >::const_iterator aEnd = m_vRowList.end();
982*cdf0e10cSrcweir 	for(;aIter != aEnd;++aIter)
983*cdf0e10cSrcweir 	{
984*cdf0e10cSrcweir 		OFieldDescription* pFieldDesc = (*aIter)->GetActFieldDescr();
985*cdf0e10cSrcweir 		if (pFieldDesc && pFieldDesc->GetName().getLength())
986*cdf0e10cSrcweir 		{
987*cdf0e10cSrcweir 			bFoundPKey |=  (*aIter)->IsPrimaryKey();
988*cdf0e10cSrcweir 			// first check for duplicate names
989*cdf0e10cSrcweir 			::std::vector< ::boost::shared_ptr<OTableRow> >::const_iterator aIter2 = aIter+1;
990*cdf0e10cSrcweir 			for(;aIter2 != aEnd;++aIter2)
991*cdf0e10cSrcweir 			{
992*cdf0e10cSrcweir 				OFieldDescription* pCompareDesc = (*aIter2)->GetActFieldDescr();
993*cdf0e10cSrcweir 				if (pCompareDesc && bCase(pCompareDesc->GetName(),pFieldDesc->GetName()))
994*cdf0e10cSrcweir 				{
995*cdf0e10cSrcweir 					String strMessage = String(ModuleRes(STR_TABLEDESIGN_DUPLICATE_NAME));
996*cdf0e10cSrcweir 					strMessage.SearchAndReplaceAscii("$column$", pFieldDesc->GetName());
997*cdf0e10cSrcweir 					OSQLWarningBox( getView(), strMessage ).Execute();
998*cdf0e10cSrcweir 					return sal_False;
999*cdf0e10cSrcweir 				}
1000*cdf0e10cSrcweir 			}
1001*cdf0e10cSrcweir 		}
1002*cdf0e10cSrcweir 	}
1003*cdf0e10cSrcweir 	if ( _bNew && !bFoundPKey && aMetaData.supportsPrimaryKeys() )
1004*cdf0e10cSrcweir 	{
1005*cdf0e10cSrcweir         String sTitle(ModuleRes(STR_TABLEDESIGN_NO_PRIM_KEY_HEAD));
1006*cdf0e10cSrcweir 	    String sMsg(ModuleRes(STR_TABLEDESIGN_NO_PRIM_KEY));
1007*cdf0e10cSrcweir 	    OSQLMessageBox aBox(getView(), sTitle,sMsg, WB_YES_NO_CANCEL | WB_DEF_YES);
1008*cdf0e10cSrcweir 
1009*cdf0e10cSrcweir 	    switch ( aBox.Execute() )
1010*cdf0e10cSrcweir         {
1011*cdf0e10cSrcweir         case RET_YES:
1012*cdf0e10cSrcweir 	    {
1013*cdf0e10cSrcweir 		    ::boost::shared_ptr<OTableRow>  pNewRow(new OTableRow());
1014*cdf0e10cSrcweir 		    TOTypeInfoSP pTypeInfo = ::dbaui::queryPrimaryKeyType(m_aTypeInfo);
1015*cdf0e10cSrcweir 		    if ( !pTypeInfo.get() )
1016*cdf0e10cSrcweir                 break;
1017*cdf0e10cSrcweir 
1018*cdf0e10cSrcweir             pNewRow->SetFieldType( pTypeInfo );
1019*cdf0e10cSrcweir 		    OFieldDescription* pActFieldDescr = pNewRow->GetActFieldDescr();
1020*cdf0e10cSrcweir 
1021*cdf0e10cSrcweir 		    pActFieldDescr->SetAutoIncrement(sal_False); // #95927# pTypeInfo->bAutoIncrement
1022*cdf0e10cSrcweir 		    pActFieldDescr->SetIsNullable(ColumnValue::NO_NULLS);
1023*cdf0e10cSrcweir 
1024*cdf0e10cSrcweir 		    pActFieldDescr->SetName( createUniqueName(::rtl::OUString::createFromAscii("ID") ));
1025*cdf0e10cSrcweir 		    pActFieldDescr->SetPrimaryKey( sal_True );
1026*cdf0e10cSrcweir 		    m_vRowList.insert(m_vRowList.begin(),pNewRow);
1027*cdf0e10cSrcweir 
1028*cdf0e10cSrcweir 		    static_cast<OTableDesignView*>(getView())->GetEditorCtrl()->Invalidate();
1029*cdf0e10cSrcweir 		    static_cast<OTableDesignView*>(getView())->GetEditorCtrl()->RowInserted(0);
1030*cdf0e10cSrcweir 	    }
1031*cdf0e10cSrcweir         break;
1032*cdf0e10cSrcweir         case RET_CANCEL:
1033*cdf0e10cSrcweir             bOk = sal_False;
1034*cdf0e10cSrcweir             break;
1035*cdf0e10cSrcweir         }
1036*cdf0e10cSrcweir 	}
1037*cdf0e10cSrcweir 	return bOk;
1038*cdf0e10cSrcweir }
1039*cdf0e10cSrcweir // -----------------------------------------------------------------------------
1040*cdf0e10cSrcweir void OTableController::alterColumns()
1041*cdf0e10cSrcweir {
1042*cdf0e10cSrcweir 	Reference<XColumnsSupplier> xColSup(m_xTable,UNO_QUERY_THROW);
1043*cdf0e10cSrcweir 	OSL_ENSURE(xColSup.is(),"What happen here?!");
1044*cdf0e10cSrcweir 
1045*cdf0e10cSrcweir 	Reference<XNameAccess> xColumns = xColSup->getColumns();
1046*cdf0e10cSrcweir 	Reference<XIndexAccess> xIdxColumns(xColumns,UNO_QUERY_THROW);
1047*cdf0e10cSrcweir 	OSL_ENSURE(xColumns.is(),"No columns");
1048*cdf0e10cSrcweir     if ( !xColumns.is() )
1049*cdf0e10cSrcweir         return;
1050*cdf0e10cSrcweir 	Reference<XAlterTable> xAlter(m_xTable,UNO_QUERY);	// can be null
1051*cdf0e10cSrcweir 
1052*cdf0e10cSrcweir 	sal_Int32 nColumnCount = xIdxColumns->getCount();
1053*cdf0e10cSrcweir 	Reference<XDrop> xDrop(xColumns,UNO_QUERY);			// can be null
1054*cdf0e10cSrcweir 	Reference<XAppend> xAppend(xColumns,UNO_QUERY);		// can be null
1055*cdf0e10cSrcweir 	Reference<XDataDescriptorFactory> xColumnFactory(xColumns,UNO_QUERY); // can be null
1056*cdf0e10cSrcweir 
1057*cdf0e10cSrcweir 	sal_Bool bReload = sal_False; // refresh the data
1058*cdf0e10cSrcweir 
1059*cdf0e10cSrcweir 	// contains all columns names which are already handled those which are not in the list will be deleted
1060*cdf0e10cSrcweir 	Reference< XDatabaseMetaData> xMetaData = getMetaData( );
1061*cdf0e10cSrcweir 
1062*cdf0e10cSrcweir 	::std::map< ::rtl::OUString,sal_Bool,::comphelper::UStringMixLess> aColumns(xMetaData.is() ? (xMetaData->supportsMixedCaseQuotedIdentifiers() ? true : false): sal_True);
1063*cdf0e10cSrcweir 	::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aIter = m_vRowList.begin();
1064*cdf0e10cSrcweir 	::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aEnd = m_vRowList.end();
1065*cdf0e10cSrcweir 	// first look for columns where something other than the name changed
1066*cdf0e10cSrcweir     sal_Int32 nPos = 0;
1067*cdf0e10cSrcweir 	for(;aIter != aEnd;++aIter,++nPos)
1068*cdf0e10cSrcweir 	{
1069*cdf0e10cSrcweir 		OSL_ENSURE(*aIter,"OTableRow is null!");
1070*cdf0e10cSrcweir 		OFieldDescription* pField = (*aIter)->GetActFieldDescr();
1071*cdf0e10cSrcweir 		if ( !pField )
1072*cdf0e10cSrcweir 			continue;
1073*cdf0e10cSrcweir 		if ( (*aIter)->IsReadOnly() )
1074*cdf0e10cSrcweir 		{
1075*cdf0e10cSrcweir 			aColumns[pField->GetName()] = sal_True;
1076*cdf0e10cSrcweir 			continue;
1077*cdf0e10cSrcweir 		}
1078*cdf0e10cSrcweir 
1079*cdf0e10cSrcweir 		Reference<XPropertySet> xColumn;
1080*cdf0e10cSrcweir 		if ( xColumns->hasByName(pField->GetName()) )
1081*cdf0e10cSrcweir 		{
1082*cdf0e10cSrcweir 			aColumns[pField->GetName()] = sal_True;
1083*cdf0e10cSrcweir 			xColumns->getByName(pField->GetName()) >>= xColumn;
1084*cdf0e10cSrcweir 			OSL_ENSURE(xColumn.is(),"Column is null!");
1085*cdf0e10cSrcweir 
1086*cdf0e10cSrcweir 			sal_Int32 nType=0,nPrecision=0,nScale=0,nNullable=0;
1087*cdf0e10cSrcweir 			sal_Bool bAutoIncrement = false;
1088*cdf0e10cSrcweir 			::rtl::OUString sTypeName,sDescription;
1089*cdf0e10cSrcweir 
1090*cdf0e10cSrcweir 			xColumn->getPropertyValue(PROPERTY_TYPE)			>>= nType;
1091*cdf0e10cSrcweir 			xColumn->getPropertyValue(PROPERTY_PRECISION)		>>= nPrecision;
1092*cdf0e10cSrcweir 			xColumn->getPropertyValue(PROPERTY_SCALE)			>>= nScale;
1093*cdf0e10cSrcweir 			xColumn->getPropertyValue(PROPERTY_ISNULLABLE)		>>= nNullable;
1094*cdf0e10cSrcweir 			xColumn->getPropertyValue(PROPERTY_ISAUTOINCREMENT) >>= bAutoIncrement;
1095*cdf0e10cSrcweir             xColumn->getPropertyValue(PROPERTY_DESCRIPTION)	    >>= sDescription;
1096*cdf0e10cSrcweir 
1097*cdf0e10cSrcweir             try { xColumn->getPropertyValue(PROPERTY_TYPENAME) >>= sTypeName; }
1098*cdf0e10cSrcweir             catch( const Exception& )
1099*cdf0e10cSrcweir             {
1100*cdf0e10cSrcweir             	OSL_ENSURE( sal_False, "no TypeName property?!" );
1101*cdf0e10cSrcweir                 // since this is a last minute fix for #i41785#, I want to be on the safe side,
1102*cdf0e10cSrcweir                 // and catch errors here as early as possible (instead of the whole process of altering
1103*cdf0e10cSrcweir                 // the columns failing)
1104*cdf0e10cSrcweir                 // Normally, sdbcx::Column objects are expected to have a TypeName property
1105*cdf0e10cSrcweir             }
1106*cdf0e10cSrcweir 
1107*cdf0e10cSrcweir 			//	xColumn->getPropertyValue(PROPERTY_ISCURRENCY,::cppu::bool2any(pField->IsCurrency()));
1108*cdf0e10cSrcweir 			// check if something changed
1109*cdf0e10cSrcweir 			if((nType != pField->GetType()					||
1110*cdf0e10cSrcweir                 sTypeName != pField->GetTypeName()         ||
1111*cdf0e10cSrcweir                 (nPrecision != pField->GetPrecision() && nPrecision )		||
1112*cdf0e10cSrcweir 				nScale != pField->GetScale()				||
1113*cdf0e10cSrcweir 				nNullable != pField->GetIsNullable()		||
1114*cdf0e10cSrcweir                 sDescription != pField->GetDescription()		||
1115*cdf0e10cSrcweir 				bAutoIncrement != pField->IsAutoIncrement())&&
1116*cdf0e10cSrcweir 				xColumnFactory.is())
1117*cdf0e10cSrcweir 			{
1118*cdf0e10cSrcweir 				Reference<XPropertySet> xNewColumn;
1119*cdf0e10cSrcweir 				xNewColumn = xColumnFactory->createDataDescriptor();
1120*cdf0e10cSrcweir 				::dbaui::setColumnProperties(xNewColumn,pField);
1121*cdf0e10cSrcweir 				// first try to alter the column
1122*cdf0e10cSrcweir 				sal_Bool bNotOk = sal_False;
1123*cdf0e10cSrcweir 				try
1124*cdf0e10cSrcweir 				{
1125*cdf0e10cSrcweir 					// first try if we can alter the column
1126*cdf0e10cSrcweir 					if(xAlter.is())
1127*cdf0e10cSrcweir 						xAlter->alterColumnByName(pField->GetName(),xNewColumn);
1128*cdf0e10cSrcweir 				}
1129*cdf0e10cSrcweir 				catch(const SQLException&)
1130*cdf0e10cSrcweir 				{
1131*cdf0e10cSrcweir 					if(xDrop.is() && xAppend.is())
1132*cdf0e10cSrcweir 					{
1133*cdf0e10cSrcweir 						String aMessage( ModuleRes( STR_TABLEDESIGN_ALTER_ERROR ) );
1134*cdf0e10cSrcweir 						aMessage.SearchAndReplaceAscii( "$column$", pField->GetName() );
1135*cdf0e10cSrcweir 
1136*cdf0e10cSrcweir                         SQLExceptionInfo aError( ::cppu::getCaughtException() );
1137*cdf0e10cSrcweir 						OSQLWarningBox aMsg( getView(), aMessage, WB_YES_NO | WB_DEF_YES , &aError );
1138*cdf0e10cSrcweir 						bNotOk = aMsg.Execute() == RET_YES;
1139*cdf0e10cSrcweir 					}
1140*cdf0e10cSrcweir 					else
1141*cdf0e10cSrcweir 						throw;
1142*cdf0e10cSrcweir 				}
1143*cdf0e10cSrcweir 				// if something went wrong or we can't alter columns
1144*cdf0e10cSrcweir 				// drop and append a new one
1145*cdf0e10cSrcweir 				if((!xAlter.is() || bNotOk) && xDrop.is() && xAppend.is())
1146*cdf0e10cSrcweir 				{
1147*cdf0e10cSrcweir 					xDrop->dropByName(pField->GetName());
1148*cdf0e10cSrcweir 					try
1149*cdf0e10cSrcweir 					{
1150*cdf0e10cSrcweir 						xAppend->appendByDescriptor(xNewColumn);
1151*cdf0e10cSrcweir 					}
1152*cdf0e10cSrcweir 					catch(const SQLException&)
1153*cdf0e10cSrcweir 					{ // an error occured so we try to reactivate the old one
1154*cdf0e10cSrcweir 						xAppend->appendByDescriptor(xColumn);
1155*cdf0e10cSrcweir 						throw;
1156*cdf0e10cSrcweir 					}
1157*cdf0e10cSrcweir 				}
1158*cdf0e10cSrcweir 				// exceptions are caught outside
1159*cdf0e10cSrcweir 				xNewColumn = NULL;
1160*cdf0e10cSrcweir 				if(xColumns->hasByName(pField->GetName()))
1161*cdf0e10cSrcweir 					xColumns->getByName(pField->GetName()) >>= xColumn;
1162*cdf0e10cSrcweir 				bReload = sal_True;
1163*cdf0e10cSrcweir 			}
1164*cdf0e10cSrcweir 
1165*cdf0e10cSrcweir 
1166*cdf0e10cSrcweir 		}
1167*cdf0e10cSrcweir 		else if(xColumnFactory.is() && xAlter.is() && nPos < nColumnCount)
1168*cdf0e10cSrcweir 		{ // we can't find the column so we could try it with the index before we drop and append a new column
1169*cdf0e10cSrcweir 			try
1170*cdf0e10cSrcweir 			{
1171*cdf0e10cSrcweir 				Reference<XPropertySet> xNewColumn;
1172*cdf0e10cSrcweir 				xNewColumn = xColumnFactory->createDataDescriptor();
1173*cdf0e10cSrcweir 				::dbaui::setColumnProperties(xNewColumn,pField);
1174*cdf0e10cSrcweir 				xAlter->alterColumnByIndex(nPos,xNewColumn);
1175*cdf0e10cSrcweir 				if(xColumns->hasByName(pField->GetName()))
1176*cdf0e10cSrcweir 				{	// ask for the append by name
1177*cdf0e10cSrcweir 					aColumns[pField->GetName()] = sal_True;
1178*cdf0e10cSrcweir 					xColumns->getByName(pField->GetName()) >>= xColumn;
1179*cdf0e10cSrcweir 					if(xColumn.is())
1180*cdf0e10cSrcweir 						pField->copyColumnSettingsTo(xColumn);
1181*cdf0e10cSrcweir 				}
1182*cdf0e10cSrcweir 				else
1183*cdf0e10cSrcweir 				{
1184*cdf0e10cSrcweir 					OSL_ENSURE(sal_False, "OTableController::alterColumns: invalid column (2)!");
1185*cdf0e10cSrcweir 				}
1186*cdf0e10cSrcweir 			}
1187*cdf0e10cSrcweir 			catch(const SQLException&)
1188*cdf0e10cSrcweir 			{ // we couldn't alter the column so we have to add new columns
1189*cdf0e10cSrcweir 				bReload = sal_True;
1190*cdf0e10cSrcweir 				if(xDrop.is() && xAppend.is())
1191*cdf0e10cSrcweir 				{
1192*cdf0e10cSrcweir 					String aMessage(ModuleRes(STR_TABLEDESIGN_ALTER_ERROR));
1193*cdf0e10cSrcweir 					aMessage.SearchAndReplaceAscii("$column$",pField->GetName());
1194*cdf0e10cSrcweir 					OSQLWarningBox aMsg( getView(), aMessage, WB_YES_NO | WB_DEF_YES );
1195*cdf0e10cSrcweir 					if ( aMsg.Execute() != RET_YES )
1196*cdf0e10cSrcweir 					{
1197*cdf0e10cSrcweir                         Reference<XPropertySet> xNewColumn(xIdxColumns->getByIndex(nPos),UNO_QUERY_THROW);
1198*cdf0e10cSrcweir                         ::rtl::OUString sName;
1199*cdf0e10cSrcweir                         xNewColumn->getPropertyValue(PROPERTY_NAME) >>= sName;
1200*cdf0e10cSrcweir                         aColumns[sName] = sal_True;
1201*cdf0e10cSrcweir                         aColumns[pField->GetName()] = sal_True;
1202*cdf0e10cSrcweir 						continue;
1203*cdf0e10cSrcweir 					}
1204*cdf0e10cSrcweir 				}
1205*cdf0e10cSrcweir 				else
1206*cdf0e10cSrcweir 					throw;
1207*cdf0e10cSrcweir 			}
1208*cdf0e10cSrcweir 		}
1209*cdf0e10cSrcweir 		else
1210*cdf0e10cSrcweir 			bReload = sal_True;
1211*cdf0e10cSrcweir 	} // for(sal_Int32 nPos = 0;aIter != aEnd;++aIter,++nPos)
1212*cdf0e10cSrcweir     // alter column settings
1213*cdf0e10cSrcweir     aIter = m_vRowList.begin();
1214*cdf0e10cSrcweir 
1215*cdf0e10cSrcweir 	// first look for columns where something other than the name changed
1216*cdf0e10cSrcweir 	for(nPos = 0;aIter != aEnd;++aIter,++nPos)
1217*cdf0e10cSrcweir 	{
1218*cdf0e10cSrcweir 		OSL_ENSURE(*aIter,"OTableRow is null!");
1219*cdf0e10cSrcweir 		OFieldDescription* pField = (*aIter)->GetActFieldDescr();
1220*cdf0e10cSrcweir 		if ( !pField )
1221*cdf0e10cSrcweir 			continue;
1222*cdf0e10cSrcweir 		if ( (*aIter)->IsReadOnly() )
1223*cdf0e10cSrcweir 		{
1224*cdf0e10cSrcweir 			aColumns[pField->GetName()] = sal_True;
1225*cdf0e10cSrcweir 			continue;
1226*cdf0e10cSrcweir 		}
1227*cdf0e10cSrcweir 
1228*cdf0e10cSrcweir 		Reference<XPropertySet> xColumn;
1229*cdf0e10cSrcweir 		if ( xColumns->hasByName(pField->GetName()) )
1230*cdf0e10cSrcweir 		{
1231*cdf0e10cSrcweir 			xColumns->getByName(pField->GetName()) >>= xColumn;
1232*cdf0e10cSrcweir             Reference<XPropertySetInfo> xInfo = xColumn->getPropertySetInfo();
1233*cdf0e10cSrcweir             if ( xInfo->hasPropertyByName(PROPERTY_HELPTEXT) )
1234*cdf0e10cSrcweir 				xColumn->setPropertyValue(PROPERTY_HELPTEXT,makeAny(pField->GetHelpText()));
1235*cdf0e10cSrcweir 
1236*cdf0e10cSrcweir 			if(xInfo->hasPropertyByName(PROPERTY_CONTROLDEFAULT))
1237*cdf0e10cSrcweir 				xColumn->setPropertyValue(PROPERTY_CONTROLDEFAULT,pField->GetControlDefault());
1238*cdf0e10cSrcweir 			if(xInfo->hasPropertyByName(PROPERTY_FORMATKEY))
1239*cdf0e10cSrcweir 				xColumn->setPropertyValue(PROPERTY_FORMATKEY,makeAny(pField->GetFormatKey()));
1240*cdf0e10cSrcweir 			if(xInfo->hasPropertyByName(PROPERTY_ALIGN))
1241*cdf0e10cSrcweir 				xColumn->setPropertyValue(PROPERTY_ALIGN,makeAny(dbaui::mapTextAllign(pField->GetHorJustify())));
1242*cdf0e10cSrcweir         } // if ( xColumns->hasByName(pField->GetName()) )
1243*cdf0e10cSrcweir     }
1244*cdf0e10cSrcweir 	// second drop all columns which could be found by name
1245*cdf0e10cSrcweir 	Reference<XNameAccess> xKeyColumns	= getKeyColumns();
1246*cdf0e10cSrcweir 	// now we have to look for the columns who could be deleted
1247*cdf0e10cSrcweir 	if ( xDrop.is() )
1248*cdf0e10cSrcweir 	{
1249*cdf0e10cSrcweir 		Sequence< ::rtl::OUString> aColumnNames = xColumns->getElementNames();
1250*cdf0e10cSrcweir 		const ::rtl::OUString* pIter = aColumnNames.getConstArray();
1251*cdf0e10cSrcweir 		const ::rtl::OUString* pEnd = pIter + aColumnNames.getLength();
1252*cdf0e10cSrcweir 		for(;pIter != pEnd;++pIter)
1253*cdf0e10cSrcweir 		{
1254*cdf0e10cSrcweir 			if(aColumns.find(*pIter) == aColumns.end()) // found a column to delete
1255*cdf0e10cSrcweir 			{
1256*cdf0e10cSrcweir 				if(xKeyColumns.is() && xKeyColumns->hasByName(*pIter)) // check if this column is a member of the primary key
1257*cdf0e10cSrcweir 				{
1258*cdf0e10cSrcweir 					String aMsgT(ModuleRes(STR_TBL_COLUMN_IS_KEYCOLUMN));
1259*cdf0e10cSrcweir 					aMsgT.SearchAndReplaceAscii("$column$",*pIter);
1260*cdf0e10cSrcweir 					String aTitle(ModuleRes(STR_TBL_COLUMN_IS_KEYCOLUMN_TITLE));
1261*cdf0e10cSrcweir 					OSQLMessageBox aMsg(getView(),aTitle,aMsgT,WB_YES_NO| WB_DEF_YES);
1262*cdf0e10cSrcweir 					if(aMsg.Execute() == RET_YES)
1263*cdf0e10cSrcweir 					{
1264*cdf0e10cSrcweir 						xKeyColumns = NULL;
1265*cdf0e10cSrcweir 						dropPrimaryKey();
1266*cdf0e10cSrcweir 					}
1267*cdf0e10cSrcweir 					else
1268*cdf0e10cSrcweir 					{
1269*cdf0e10cSrcweir 						bReload = sal_True;
1270*cdf0e10cSrcweir 						continue;
1271*cdf0e10cSrcweir 					}
1272*cdf0e10cSrcweir 				}
1273*cdf0e10cSrcweir                 try
1274*cdf0e10cSrcweir                 {
1275*cdf0e10cSrcweir                     xDrop->dropByName(*pIter);
1276*cdf0e10cSrcweir 			    }
1277*cdf0e10cSrcweir                 catch (const SQLException&)
1278*cdf0e10cSrcweir                 {
1279*cdf0e10cSrcweir                     String sError( ModuleRes( STR_TABLEDESIGN_COULD_NOT_DROP_COL ) );
1280*cdf0e10cSrcweir                     sError.SearchAndReplaceAscii( "$column$", *pIter );
1281*cdf0e10cSrcweir 
1282*cdf0e10cSrcweir                     SQLException aNewException;
1283*cdf0e10cSrcweir                     aNewException.Message = sError;
1284*cdf0e10cSrcweir                     aNewException.SQLState = ::rtl::OUString::createFromAscii( "S1000" );
1285*cdf0e10cSrcweir                     aNewException.NextException = ::cppu::getCaughtException();
1286*cdf0e10cSrcweir 
1287*cdf0e10cSrcweir                     throw aNewException;
1288*cdf0e10cSrcweir                 }
1289*cdf0e10cSrcweir             }
1290*cdf0e10cSrcweir         }
1291*cdf0e10cSrcweir 	}
1292*cdf0e10cSrcweir 
1293*cdf0e10cSrcweir 	// third append the new columns
1294*cdf0e10cSrcweir 	aIter = m_vRowList.begin();
1295*cdf0e10cSrcweir 	for(;aIter != aEnd;++aIter)
1296*cdf0e10cSrcweir 	{
1297*cdf0e10cSrcweir 		OSL_ENSURE(*aIter,"OTableRow is null!");
1298*cdf0e10cSrcweir 		OFieldDescription* pField = (*aIter)->GetActFieldDescr();
1299*cdf0e10cSrcweir 		if ( !pField || (*aIter)->IsReadOnly() || aColumns.find(pField->GetName()) != aColumns.end() )
1300*cdf0e10cSrcweir 			continue;
1301*cdf0e10cSrcweir 
1302*cdf0e10cSrcweir 		Reference<XPropertySet> xColumn;
1303*cdf0e10cSrcweir 		if(!xColumns->hasByName(pField->GetName()))
1304*cdf0e10cSrcweir 		{
1305*cdf0e10cSrcweir 			if(xColumnFactory.is() && xAppend.is())
1306*cdf0e10cSrcweir 			{// column not found by its name so we assume it is new
1307*cdf0e10cSrcweir 				// Column is new
1308*cdf0e10cSrcweir 				xColumn = xColumnFactory->createDataDescriptor();
1309*cdf0e10cSrcweir 				::dbaui::setColumnProperties(xColumn,pField);
1310*cdf0e10cSrcweir 				xAppend->appendByDescriptor(xColumn);
1311*cdf0e10cSrcweir 				if(xColumns->hasByName(pField->GetName()))
1312*cdf0e10cSrcweir 				{	// ask for the append by name
1313*cdf0e10cSrcweir 					aColumns[pField->GetName()] = sal_True;
1314*cdf0e10cSrcweir 					xColumns->getByName(pField->GetName()) >>= xColumn;
1315*cdf0e10cSrcweir 					if(xColumn.is())
1316*cdf0e10cSrcweir 						pField->copyColumnSettingsTo(xColumn);
1317*cdf0e10cSrcweir 				}
1318*cdf0e10cSrcweir 				else
1319*cdf0e10cSrcweir 				{
1320*cdf0e10cSrcweir 					OSL_ENSURE(sal_False, "OTableController::alterColumns: invalid column!");
1321*cdf0e10cSrcweir 				}
1322*cdf0e10cSrcweir 			}
1323*cdf0e10cSrcweir 		}
1324*cdf0e10cSrcweir 	}
1325*cdf0e10cSrcweir 
1326*cdf0e10cSrcweir 
1327*cdf0e10cSrcweir 	// check if we have to do something with the primary key
1328*cdf0e10cSrcweir 	sal_Bool bNeedDropKey = sal_False;
1329*cdf0e10cSrcweir 	sal_Bool bNeedAppendKey = sal_False;
1330*cdf0e10cSrcweir 	if ( xKeyColumns.is() )
1331*cdf0e10cSrcweir 	{
1332*cdf0e10cSrcweir 		aIter = m_vRowList.begin();
1333*cdf0e10cSrcweir 		for(;aIter != aEnd;++aIter)
1334*cdf0e10cSrcweir 		{
1335*cdf0e10cSrcweir 			OSL_ENSURE(*aIter,"OTableRow is null!");
1336*cdf0e10cSrcweir 			OFieldDescription* pField = (*aIter)->GetActFieldDescr();
1337*cdf0e10cSrcweir 			if ( !pField )
1338*cdf0e10cSrcweir 				continue;
1339*cdf0e10cSrcweir 
1340*cdf0e10cSrcweir 			if	(	pField->IsPrimaryKey()
1341*cdf0e10cSrcweir 				&&	!xKeyColumns->hasByName( pField->GetName() )
1342*cdf0e10cSrcweir 				)
1343*cdf0e10cSrcweir 			{	// new primary key column inserted which isn't already in the columns selection
1344*cdf0e10cSrcweir 				bNeedDropKey = bNeedAppendKey = sal_True;
1345*cdf0e10cSrcweir 				break;
1346*cdf0e10cSrcweir 			}
1347*cdf0e10cSrcweir 			else if	(	!pField->IsPrimaryKey()
1348*cdf0e10cSrcweir 					&&	xKeyColumns->hasByName( pField->GetName() )
1349*cdf0e10cSrcweir 					)
1350*cdf0e10cSrcweir 			{	// found a column which currently is in the primary key, but is marked not to be anymore
1351*cdf0e10cSrcweir 				bNeedDropKey = bNeedAppendKey = sal_True;
1352*cdf0e10cSrcweir 				break;
1353*cdf0e10cSrcweir 			}
1354*cdf0e10cSrcweir 		}
1355*cdf0e10cSrcweir 	}
1356*cdf0e10cSrcweir 	else
1357*cdf0e10cSrcweir 	{	// no primary key available so we check if we should create one
1358*cdf0e10cSrcweir 		bNeedAppendKey = sal_True;
1359*cdf0e10cSrcweir 	}
1360*cdf0e10cSrcweir 
1361*cdf0e10cSrcweir 	if ( bNeedDropKey && xKeyColumns.is() && xKeyColumns->getElementNames().getLength() )
1362*cdf0e10cSrcweir 		dropPrimaryKey();
1363*cdf0e10cSrcweir 
1364*cdf0e10cSrcweir 	if ( bNeedAppendKey )
1365*cdf0e10cSrcweir 	{
1366*cdf0e10cSrcweir 		Reference< XKeysSupplier > xKeySup( m_xTable, UNO_QUERY );
1367*cdf0e10cSrcweir 		appendPrimaryKey( xKeySup ,sal_False);
1368*cdf0e10cSrcweir 	}
1369*cdf0e10cSrcweir 
1370*cdf0e10cSrcweir 	reSyncRows();
1371*cdf0e10cSrcweir 
1372*cdf0e10cSrcweir 	if ( bReload )
1373*cdf0e10cSrcweir 		reload();
1374*cdf0e10cSrcweir }
1375*cdf0e10cSrcweir // -----------------------------------------------------------------------------
1376*cdf0e10cSrcweir void OTableController::dropPrimaryKey()
1377*cdf0e10cSrcweir {
1378*cdf0e10cSrcweir     SQLExceptionInfo aInfo;
1379*cdf0e10cSrcweir     try
1380*cdf0e10cSrcweir     {
1381*cdf0e10cSrcweir 	    Reference<XKeysSupplier> xKeySup(m_xTable,UNO_QUERY);
1382*cdf0e10cSrcweir 	    Reference<XIndexAccess> xKeys;
1383*cdf0e10cSrcweir 	    if(xKeySup.is())
1384*cdf0e10cSrcweir 		    xKeys = xKeySup->getKeys();
1385*cdf0e10cSrcweir 
1386*cdf0e10cSrcweir 	    if(xKeys.is())
1387*cdf0e10cSrcweir 	    {
1388*cdf0e10cSrcweir 		    Reference<XPropertySet> xProp;
1389*cdf0e10cSrcweir 		    for(sal_Int32 i=0;i< xKeys->getCount();++i)
1390*cdf0e10cSrcweir 		    {
1391*cdf0e10cSrcweir                 xProp.set(xKeys->getByIndex(i),UNO_QUERY);
1392*cdf0e10cSrcweir 			    sal_Int32 nKeyType = 0;
1393*cdf0e10cSrcweir 			    xProp->getPropertyValue(PROPERTY_TYPE) >>= nKeyType;
1394*cdf0e10cSrcweir 			    if(KeyType::PRIMARY == nKeyType)
1395*cdf0e10cSrcweir 			    {
1396*cdf0e10cSrcweir 				    Reference<XDrop> xDrop(xKeys,UNO_QUERY);
1397*cdf0e10cSrcweir 				    xDrop->dropByIndex(i); // delete the key
1398*cdf0e10cSrcweir 				    break;
1399*cdf0e10cSrcweir 			    }
1400*cdf0e10cSrcweir 		    }
1401*cdf0e10cSrcweir 	    }
1402*cdf0e10cSrcweir     }
1403*cdf0e10cSrcweir     catch(const SQLContext& e)
1404*cdf0e10cSrcweir 	{
1405*cdf0e10cSrcweir 		aInfo = SQLExceptionInfo(e);
1406*cdf0e10cSrcweir 	}
1407*cdf0e10cSrcweir 	catch(const SQLWarning& e)
1408*cdf0e10cSrcweir 	{
1409*cdf0e10cSrcweir 		aInfo = SQLExceptionInfo(e);
1410*cdf0e10cSrcweir 	}
1411*cdf0e10cSrcweir 	catch(const SQLException& e)
1412*cdf0e10cSrcweir 	{
1413*cdf0e10cSrcweir 		aInfo = SQLExceptionInfo(e);
1414*cdf0e10cSrcweir 	}
1415*cdf0e10cSrcweir     catch( const Exception& )
1416*cdf0e10cSrcweir     {
1417*cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
1418*cdf0e10cSrcweir     }
1419*cdf0e10cSrcweir 
1420*cdf0e10cSrcweir 	showError(aInfo);
1421*cdf0e10cSrcweir }
1422*cdf0e10cSrcweir // -----------------------------------------------------------------------------
1423*cdf0e10cSrcweir void OTableController::assignTable()
1424*cdf0e10cSrcweir {
1425*cdf0e10cSrcweir 	::rtl::OUString sComposedName;
1426*cdf0e10cSrcweir 	// get the table
1427*cdf0e10cSrcweir 	if(m_sName.getLength())
1428*cdf0e10cSrcweir 	{
1429*cdf0e10cSrcweir 		Reference<XNameAccess> xNameAccess;
1430*cdf0e10cSrcweir 		Reference<XTablesSupplier> xSup(getConnection(),UNO_QUERY);
1431*cdf0e10cSrcweir 		if(xSup.is())
1432*cdf0e10cSrcweir 		{
1433*cdf0e10cSrcweir 			xNameAccess = xSup->getTables();
1434*cdf0e10cSrcweir 			OSL_ENSURE(xNameAccess.is(),"no nameaccess for the queries!");
1435*cdf0e10cSrcweir 
1436*cdf0e10cSrcweir 			Reference<XPropertySet> xProp;
1437*cdf0e10cSrcweir 			if(xNameAccess->hasByName(m_sName) && ::cppu::extractInterface(xProp,xNameAccess->getByName(m_sName)) && xProp.is())
1438*cdf0e10cSrcweir 			{
1439*cdf0e10cSrcweir 				m_xTable = xProp;
1440*cdf0e10cSrcweir 				startTableListening();
1441*cdf0e10cSrcweir 
1442*cdf0e10cSrcweir 				// check if we set the table editable
1443*cdf0e10cSrcweir 				Reference<XDatabaseMetaData> xMeta = getConnection()->getMetaData();
1444*cdf0e10cSrcweir 				setEditable( xMeta.is() && !xMeta->isReadOnly() && (isAlterAllowed() || isDropAllowed() || isAddAllowed()) );
1445*cdf0e10cSrcweir 				if(!isEditable())
1446*cdf0e10cSrcweir 				{
1447*cdf0e10cSrcweir                     ::std::for_each(m_vRowList.begin(),m_vRowList.end(),boost::bind( &OTableRow::SetReadOnly, _1, boost::cref( sal_True )));
1448*cdf0e10cSrcweir 				}
1449*cdf0e10cSrcweir 				m_bNew = sal_False;
1450*cdf0e10cSrcweir 				// be notified when the table is in disposing
1451*cdf0e10cSrcweir 				InvalidateAll();
1452*cdf0e10cSrcweir 			}
1453*cdf0e10cSrcweir 		}
1454*cdf0e10cSrcweir 	}
1455*cdf0e10cSrcweir 	//updateTitle();
1456*cdf0e10cSrcweir }
1457*cdf0e10cSrcweir // -----------------------------------------------------------------------------
1458*cdf0e10cSrcweir sal_Bool OTableController::isAddAllowed() const
1459*cdf0e10cSrcweir {
1460*cdf0e10cSrcweir 	Reference<XColumnsSupplier> xColsSup(m_xTable,UNO_QUERY);
1461*cdf0e10cSrcweir 	sal_Bool bAddAllowed = !m_xTable.is();
1462*cdf0e10cSrcweir 	if(xColsSup.is())
1463*cdf0e10cSrcweir 		bAddAllowed = Reference<XAppend>(xColsSup->getColumns(),UNO_QUERY).is();
1464*cdf0e10cSrcweir 
1465*cdf0e10cSrcweir     try
1466*cdf0e10cSrcweir     {
1467*cdf0e10cSrcweir 	    Reference< XDatabaseMetaData > xMetaData = getMetaData( );
1468*cdf0e10cSrcweir 	    bAddAllowed = bAddAllowed || ( xMetaData.is() && xMetaData->supportsAlterTableWithAddColumn());
1469*cdf0e10cSrcweir     }
1470*cdf0e10cSrcweir     catch(Exception&)
1471*cdf0e10cSrcweir     {
1472*cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
1473*cdf0e10cSrcweir         bAddAllowed = sal_False;
1474*cdf0e10cSrcweir     }
1475*cdf0e10cSrcweir 
1476*cdf0e10cSrcweir 	return bAddAllowed;
1477*cdf0e10cSrcweir }
1478*cdf0e10cSrcweir // -----------------------------------------------------------------------------
1479*cdf0e10cSrcweir sal_Bool OTableController::isDropAllowed() const
1480*cdf0e10cSrcweir {
1481*cdf0e10cSrcweir 	Reference<XColumnsSupplier> xColsSup(m_xTable,UNO_QUERY);
1482*cdf0e10cSrcweir 	sal_Bool bDropAllowed = !m_xTable.is();
1483*cdf0e10cSrcweir 	if(xColsSup.is())
1484*cdf0e10cSrcweir 	{
1485*cdf0e10cSrcweir 		Reference<XNameAccess> xNameAccess = xColsSup->getColumns();
1486*cdf0e10cSrcweir 		bDropAllowed = Reference<XDrop>(xNameAccess,UNO_QUERY).is() && xNameAccess->hasElements();
1487*cdf0e10cSrcweir 	}
1488*cdf0e10cSrcweir 
1489*cdf0e10cSrcweir 	Reference< XDatabaseMetaData> xMetaData = getMetaData( );
1490*cdf0e10cSrcweir 	bDropAllowed = bDropAllowed || ( xMetaData.is() && xMetaData->supportsAlterTableWithDropColumn());
1491*cdf0e10cSrcweir 
1492*cdf0e10cSrcweir 	return bDropAllowed;
1493*cdf0e10cSrcweir }
1494*cdf0e10cSrcweir // -----------------------------------------------------------------------------
1495*cdf0e10cSrcweir sal_Bool OTableController::isAlterAllowed() const
1496*cdf0e10cSrcweir {
1497*cdf0e10cSrcweir 	sal_Bool bAllowed(!m_xTable.is() || Reference<XAlterTable>(m_xTable,UNO_QUERY).is());
1498*cdf0e10cSrcweir 	return bAllowed;
1499*cdf0e10cSrcweir }
1500*cdf0e10cSrcweir // -----------------------------------------------------------------------------
1501*cdf0e10cSrcweir void OTableController::reSyncRows()
1502*cdf0e10cSrcweir {
1503*cdf0e10cSrcweir 	sal_Bool bAlterAllowed	= isAlterAllowed();
1504*cdf0e10cSrcweir 	sal_Bool bAddAllowed	= isAddAllowed();
1505*cdf0e10cSrcweir 	::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aIter = m_vRowList.begin();
1506*cdf0e10cSrcweir     ::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aEnd = m_vRowList.end();
1507*cdf0e10cSrcweir 	for(;aIter != aEnd;++aIter)
1508*cdf0e10cSrcweir 	{
1509*cdf0e10cSrcweir 		OSL_ENSURE(*aIter,"OTableRow is null!");
1510*cdf0e10cSrcweir 		OFieldDescription* pField = (*aIter)->GetActFieldDescr();
1511*cdf0e10cSrcweir 		if ( pField )
1512*cdf0e10cSrcweir 			(*aIter)->SetReadOnly(!bAlterAllowed);
1513*cdf0e10cSrcweir 		else
1514*cdf0e10cSrcweir 			(*aIter)->SetReadOnly(!bAddAllowed);
1515*cdf0e10cSrcweir 
1516*cdf0e10cSrcweir 	}
1517*cdf0e10cSrcweir 	static_cast<OTableDesignView*>(getView())->reSync();	// show the windows and fill with our informations
1518*cdf0e10cSrcweir 
1519*cdf0e10cSrcweir 	ClearUndoManager();
1520*cdf0e10cSrcweir 	setModified(sal_False);		// and we are not modified yet
1521*cdf0e10cSrcweir }
1522*cdf0e10cSrcweir // -----------------------------------------------------------------------------
1523*cdf0e10cSrcweir ::rtl::OUString OTableController::createUniqueName(const ::rtl::OUString& _rName)
1524*cdf0e10cSrcweir {
1525*cdf0e10cSrcweir 	::rtl::OUString sName = _rName;
1526*cdf0e10cSrcweir 	Reference< XDatabaseMetaData> xMetaData = getMetaData( );
1527*cdf0e10cSrcweir 
1528*cdf0e10cSrcweir 	::comphelper::UStringMixEqual bCase(xMetaData.is() ? xMetaData->supportsMixedCaseQuotedIdentifiers() : sal_True);
1529*cdf0e10cSrcweir 
1530*cdf0e10cSrcweir 	::std::vector< ::boost::shared_ptr<OTableRow> >::const_iterator aIter = m_vRowList.begin();
1531*cdf0e10cSrcweir     ::std::vector< ::boost::shared_ptr<OTableRow> >::const_iterator aEnd = m_vRowList.end();
1532*cdf0e10cSrcweir 	for(sal_Int32 i=0;aIter != aEnd;++aIter)
1533*cdf0e10cSrcweir 	{
1534*cdf0e10cSrcweir 		OFieldDescription* pFieldDesc = (*aIter)->GetActFieldDescr();
1535*cdf0e10cSrcweir 		if (pFieldDesc && pFieldDesc->GetName().getLength() && bCase(sName,pFieldDesc->GetName()))
1536*cdf0e10cSrcweir 		{ // found a second name of _rName so we need another
1537*cdf0e10cSrcweir 			sName = _rName + ::rtl::OUString::valueOf(++i);
1538*cdf0e10cSrcweir 			aIter = m_vRowList.begin(); // and retry
1539*cdf0e10cSrcweir 		}
1540*cdf0e10cSrcweir 	}
1541*cdf0e10cSrcweir 	return sName;
1542*cdf0e10cSrcweir }
1543*cdf0e10cSrcweir // -----------------------------------------------------------------------------
1544*cdf0e10cSrcweir ::rtl::OUString OTableController::getPrivateTitle() const
1545*cdf0e10cSrcweir {
1546*cdf0e10cSrcweir     ::rtl::OUString sTitle;
1547*cdf0e10cSrcweir 	try
1548*cdf0e10cSrcweir 	{
1549*cdf0e10cSrcweir 		// get the table
1550*cdf0e10cSrcweir 		if ( m_sName.getLength() && getConnection().is() )
1551*cdf0e10cSrcweir 		{
1552*cdf0e10cSrcweir 			if ( m_xTable.is() )
1553*cdf0e10cSrcweir 				sTitle = ::dbtools::composeTableName( getConnection()->getMetaData(), m_xTable, ::dbtools::eInDataManipulation, false, false, false );
1554*cdf0e10cSrcweir 			else
1555*cdf0e10cSrcweir 				sTitle = m_sName;
1556*cdf0e10cSrcweir 		}
1557*cdf0e10cSrcweir 		if ( !sTitle.getLength() )
1558*cdf0e10cSrcweir 		{
1559*cdf0e10cSrcweir             String aName = String(ModuleRes(STR_TBL_TITLE));
1560*cdf0e10cSrcweir 			sTitle = aName.GetToken(0,' ');
1561*cdf0e10cSrcweir             sTitle += ::rtl::OUString::valueOf(getCurrentStartNumber());
1562*cdf0e10cSrcweir 		}
1563*cdf0e10cSrcweir 	}
1564*cdf0e10cSrcweir 	catch( const Exception& )
1565*cdf0e10cSrcweir 	{
1566*cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
1567*cdf0e10cSrcweir 	}
1568*cdf0e10cSrcweir     return sTitle;
1569*cdf0e10cSrcweir }
1570*cdf0e10cSrcweir // -----------------------------------------------------------------------------
1571*cdf0e10cSrcweir void OTableController::reload()
1572*cdf0e10cSrcweir {
1573*cdf0e10cSrcweir 	loadData();					// fill the column information form the table
1574*cdf0e10cSrcweir 	static_cast<OTableDesignView*>(getView())->reSync();	// show the windows and fill with our informations
1575*cdf0e10cSrcweir 	ClearUndoManager();
1576*cdf0e10cSrcweir 	setModified(sal_False);		// and we are not modified yet
1577*cdf0e10cSrcweir 	static_cast<OTableDesignView*>(getView())->Invalidate();
1578*cdf0e10cSrcweir }
1579*cdf0e10cSrcweir // -----------------------------------------------------------------------------
1580*cdf0e10cSrcweir sal_Int32 OTableController::getFirstEmptyRowPosition()
1581*cdf0e10cSrcweir {
1582*cdf0e10cSrcweir 	sal_Int32 nRet = -1;
1583*cdf0e10cSrcweir 	::std::vector< ::boost::shared_ptr<OTableRow> >::const_iterator aIter = m_vRowList.begin();
1584*cdf0e10cSrcweir 	::std::vector< ::boost::shared_ptr<OTableRow> >::const_iterator aEnd = m_vRowList.end();
1585*cdf0e10cSrcweir 	for(;aIter != aEnd;++aIter)
1586*cdf0e10cSrcweir 	{
1587*cdf0e10cSrcweir 		if ( !*aIter || !(*aIter)->GetActFieldDescr() || !(*aIter)->GetActFieldDescr()->GetName().getLength() )
1588*cdf0e10cSrcweir 		{
1589*cdf0e10cSrcweir 			nRet = aIter - m_vRowList.begin();
1590*cdf0e10cSrcweir 			break;
1591*cdf0e10cSrcweir 		}
1592*cdf0e10cSrcweir 	}
1593*cdf0e10cSrcweir     if ( nRet == -1 )
1594*cdf0e10cSrcweir     {
1595*cdf0e10cSrcweir         bool bReadRow = !isAddAllowed();
1596*cdf0e10cSrcweir 	    ::boost::shared_ptr<OTableRow> pTabEdRow(new OTableRow());
1597*cdf0e10cSrcweir 	    pTabEdRow->SetReadOnly(bReadRow);
1598*cdf0e10cSrcweir         nRet = m_vRowList.size();
1599*cdf0e10cSrcweir 	    m_vRowList.push_back( pTabEdRow);
1600*cdf0e10cSrcweir     }
1601*cdf0e10cSrcweir 	return nRet;
1602*cdf0e10cSrcweir }
1603*cdf0e10cSrcweir // -----------------------------------------------------------------------------
1604*cdf0e10cSrcweir bool OTableController::isAutoIncrementPrimaryKey() const
1605*cdf0e10cSrcweir {
1606*cdf0e10cSrcweir     return getSdbMetaData().isAutoIncrementPrimaryKey();
1607*cdf0e10cSrcweir }
1608*cdf0e10cSrcweir // -----------------------------------------------------------------------------
1609