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