1efeef26fSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3efeef26fSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4efeef26fSAndrew Rist * or more contributor license agreements. See the NOTICE file 5efeef26fSAndrew Rist * distributed with this work for additional information 6efeef26fSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7efeef26fSAndrew Rist * to you under the Apache License, Version 2.0 (the 8efeef26fSAndrew Rist * "License"); you may not use this file except in compliance 9efeef26fSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11efeef26fSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13efeef26fSAndrew Rist * Unless required by applicable law or agreed to in writing, 14efeef26fSAndrew Rist * software distributed under the License is distributed on an 15efeef26fSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16efeef26fSAndrew Rist * KIND, either express or implied. See the License for the 17efeef26fSAndrew Rist * specific language governing permissions and limitations 18efeef26fSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20efeef26fSAndrew Rist *************************************************************/ 21efeef26fSAndrew Rist 22efeef26fSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_sw.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <cstdarg> 28cdf0e10cSrcweir 29cdf0e10cSrcweir #include <svtools/svmedit.hxx> 30cdf0e10cSrcweir #include <svl/eitem.hxx> 31cdf0e10cSrcweir #include <svl/whiter.hxx> 32cdf0e10cSrcweir #include <sfx2/event.hxx> 33cdf0e10cSrcweir #include <sfx2/dispatch.hxx> 34cdf0e10cSrcweir #include <sfx2/viewfrm.hxx> 35cdf0e10cSrcweir #ifndef _MSGBOX_HXX //autogen 36cdf0e10cSrcweir #include <vcl/msgbox.hxx> 37cdf0e10cSrcweir #endif 38cdf0e10cSrcweir #include <svl/stritem.hxx> 39cdf0e10cSrcweir #include <svl/itemset.hxx> 40cdf0e10cSrcweir #include <sfx2/request.hxx> 41cdf0e10cSrcweir #include <com/sun/star/sdb/CommandType.hpp> 42cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp> 43cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp> 44cdf0e10cSrcweir #include <com/sun/star/sdbc/XDataSource.hpp> 45cdf0e10cSrcweir #include <com/sun/star/sdbcx/XTablesSupplier.hpp> 46cdf0e10cSrcweir #include <com/sun/star/sdbcx/XColumnsSupplier.hpp> 47cdf0e10cSrcweir #include <com/sun/star/sdb/XQueriesSupplier.hpp> 48cdf0e10cSrcweir #include <com/sun/star/sdb/XDatabaseAccess.hpp> 49cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 50cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp> 51cdf0e10cSrcweir #include <comphelper/processfactory.hxx> 52cdf0e10cSrcweir #include <com/sun/star/sdbc/XRowSet.hpp> 53cdf0e10cSrcweir #include <sfx2/frame.hxx> 54cdf0e10cSrcweir #include <fldmgr.hxx> 55cdf0e10cSrcweir #include <fldbas.hxx> 56cdf0e10cSrcweir #include "dbmgr.hxx" 57cdf0e10cSrcweir #include <comphelper/uno3.hxx> 58cdf0e10cSrcweir #include <svx/dataaccessdescriptor.hxx> 59cdf0e10cSrcweir #include <memory> 60cdf0e10cSrcweir 61cdf0e10cSrcweir #include <vcl/svapp.hxx> 62cdf0e10cSrcweir 63cdf0e10cSrcweir #include "view.hxx" 64cdf0e10cSrcweir #include "wrtsh.hxx" 65cdf0e10cSrcweir #include "swtypes.hxx" 66cdf0e10cSrcweir #include "cmdid.h" 67cdf0e10cSrcweir #include "swevent.hxx" 68cdf0e10cSrcweir #include "shells.hrc" 69cdf0e10cSrcweir #include "textsh.hxx" 70cdf0e10cSrcweir #include "swabstdlg.hxx" 71cdf0e10cSrcweir #include "dbui.hrc" 72cdf0e10cSrcweir 73cdf0e10cSrcweir #include <unomid.h> 74cdf0e10cSrcweir 75cdf0e10cSrcweir using namespace ::svx; 76cdf0e10cSrcweir using namespace ::com::sun::star; 77cdf0e10cSrcweir using namespace ::com::sun::star::uno; 78cdf0e10cSrcweir using namespace ::com::sun::star::container; 79cdf0e10cSrcweir using namespace ::com::sun::star::lang; 80cdf0e10cSrcweir using namespace ::com::sun::star::sdb; 81cdf0e10cSrcweir using namespace ::com::sun::star::sdbc; 82cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx; 83cdf0e10cSrcweir using namespace ::com::sun::star::beans; 84cdf0e10cSrcweir 85cdf0e10cSrcweir 86cdf0e10cSrcweir #define DB_DD_DELIM 0x0b 87cdf0e10cSrcweir 88cdf0e10cSrcweir struct DBTextStruct_Impl 89cdf0e10cSrcweir { 90cdf0e10cSrcweir SwDBData aDBData; 91cdf0e10cSrcweir Sequence<Any> aSelection; 92cdf0e10cSrcweir Reference<XResultSet> xCursor; 93cdf0e10cSrcweir Reference<XConnection> xConnection; 94cdf0e10cSrcweir }; 95cdf0e10cSrcweir inline void AddSelList( List& rLst, long nRow ) 96cdf0e10cSrcweir { 97cdf0e10cSrcweir rLst.Insert( (void*)nRow , LIST_APPEND ); 98cdf0e10cSrcweir } 99cdf0e10cSrcweir void SwTextShell::ExecDB(SfxRequest &rReq) 100cdf0e10cSrcweir { 101cdf0e10cSrcweir const SfxItemSet *pArgs = rReq.GetArgs(); 102cdf0e10cSrcweir SwNewDBMgr* pNewDBMgr = GetShell().GetNewDBMgr(); 103cdf0e10cSrcweir sal_uInt16 nSlot = rReq.GetSlot(); 104cdf0e10cSrcweir ::rtl::OUString sSourceArg, sCommandArg; 105cdf0e10cSrcweir sal_Int32 nCommandTypeArg = 0; 106cdf0e10cSrcweir 107cdf0e10cSrcweir const SfxPoolItem* pSourceItem = 0; 108cdf0e10cSrcweir const SfxPoolItem* pCursorItem = 0; 109cdf0e10cSrcweir const SfxPoolItem* pConnectionItem = 0; 110cdf0e10cSrcweir const SfxPoolItem* pCommandItem = 0; 111cdf0e10cSrcweir const SfxPoolItem* pCommandTypeItem = 0; 112cdf0e10cSrcweir const SfxPoolItem* pSelectionItem = 0; 113cdf0e10cSrcweir 114cdf0e10cSrcweir // first get the selection of rows to be inserted 115cdf0e10cSrcweir pArgs->GetItemState(FN_DB_DATA_SELECTION_ANY, sal_False, &pSelectionItem); 116cdf0e10cSrcweir 117cdf0e10cSrcweir Sequence<Any> aSelection; 118cdf0e10cSrcweir if(pSelectionItem) 119cdf0e10cSrcweir ((SfxUsrAnyItem*)pSelectionItem)->GetValue() >>= aSelection; 120cdf0e10cSrcweir 121cdf0e10cSrcweir // get the data source name 122cdf0e10cSrcweir pArgs->GetItemState(FN_DB_DATA_SOURCE_ANY, sal_False, &pSourceItem); 123cdf0e10cSrcweir if(pSourceItem) 124cdf0e10cSrcweir ((const SfxUsrAnyItem*)pSourceItem)->GetValue() >>= sSourceArg; 125cdf0e10cSrcweir 126cdf0e10cSrcweir // get the command 127cdf0e10cSrcweir pArgs->GetItemState(FN_DB_DATA_COMMAND_ANY, sal_False, &pCommandItem); 128cdf0e10cSrcweir if(pCommandItem) 129cdf0e10cSrcweir ((const SfxUsrAnyItem*)pCommandItem)->GetValue() >>= sCommandArg; 130cdf0e10cSrcweir 131cdf0e10cSrcweir // get the command type 132cdf0e10cSrcweir pArgs->GetItemState(FN_DB_DATA_COMMAND_TYPE_ANY, sal_False, &pCommandTypeItem); 133cdf0e10cSrcweir if(pCommandTypeItem) 134cdf0e10cSrcweir ((const SfxUsrAnyItem*)pCommandTypeItem)->GetValue() >>= nCommandTypeArg; 135cdf0e10cSrcweir 136cdf0e10cSrcweir Reference<XConnection> xConnection; 137cdf0e10cSrcweir pArgs->GetItemState(FN_DB_CONNECTION_ANY, sal_False, &pConnectionItem); 138cdf0e10cSrcweir if ( pConnectionItem ) 139cdf0e10cSrcweir ((const SfxUsrAnyItem*)pConnectionItem)->GetValue() >>= xConnection; 140cdf0e10cSrcweir // may be we even get no connection 141cdf0e10cSrcweir if ( !xConnection.is() ) 142cdf0e10cSrcweir { 143cdf0e10cSrcweir Reference<XDataSource> xSource; 144cdf0e10cSrcweir xConnection = pNewDBMgr->GetConnection(sSourceArg, xSource); 145cdf0e10cSrcweir } 146cdf0e10cSrcweir if(!xConnection.is()) 147cdf0e10cSrcweir return ; 148cdf0e10cSrcweir 149cdf0e10cSrcweir // get the cursor, we use to travel, may be NULL 150cdf0e10cSrcweir Reference<XResultSet> xCursor; 151cdf0e10cSrcweir pArgs->GetItemState(FN_DB_DATA_CURSOR_ANY, sal_False, &pCursorItem); 152cdf0e10cSrcweir if ( pCursorItem ) 153cdf0e10cSrcweir ((const SfxUsrAnyItem*)pCursorItem)->GetValue() >>= xCursor; 154cdf0e10cSrcweir 155cdf0e10cSrcweir switch (nSlot) 156cdf0e10cSrcweir { 157cdf0e10cSrcweir case FN_QRY_INSERT: 158cdf0e10cSrcweir { 159cdf0e10cSrcweir if(pSourceItem && pCommandItem && pCommandTypeItem) 160cdf0e10cSrcweir { 161cdf0e10cSrcweir DBTextStruct_Impl* pNew = new DBTextStruct_Impl; 162cdf0e10cSrcweir pNew->aDBData.sDataSource = sSourceArg; 163cdf0e10cSrcweir pNew->aDBData.sCommand = sCommandArg; 164cdf0e10cSrcweir pNew->aDBData.nCommandType = nCommandTypeArg; 165cdf0e10cSrcweir pNew->aSelection = aSelection; 166cdf0e10cSrcweir //if the cursor is NULL, it must be created inside InsertDBTextHdl 167cdf0e10cSrcweir // because it called via a PostUserEvent 168cdf0e10cSrcweir pNew->xCursor = xCursor; 169cdf0e10cSrcweir pNew->xConnection = xConnection; 170cdf0e10cSrcweir 171cdf0e10cSrcweir Application::PostUserEvent( STATIC_LINK( this, SwBaseShell, 172cdf0e10cSrcweir InsertDBTextHdl ), pNew ); 173cdf0e10cSrcweir // the pNew will be removed in InsertDBTextHdl !! 174cdf0e10cSrcweir } 175cdf0e10cSrcweir } 176cdf0e10cSrcweir break; 177cdf0e10cSrcweir 178cdf0e10cSrcweir case FN_QRY_MERGE_FIELD: 179cdf0e10cSrcweir { 180cdf0e10cSrcweir // we don't get any cursor, so we must create our own 181cdf0e10cSrcweir sal_Bool bDisposeResultSet = sal_False; 182cdf0e10cSrcweir if ( !xCursor.is() ) 183cdf0e10cSrcweir { 184cdf0e10cSrcweir xCursor = SwNewDBMgr::createCursor(sSourceArg,sCommandArg,nCommandTypeArg,xConnection); 185cdf0e10cSrcweir bDisposeResultSet = xCursor.is(); 186cdf0e10cSrcweir } 187cdf0e10cSrcweir 188cdf0e10cSrcweir ODataAccessDescriptor aDescriptor; 189cdf0e10cSrcweir aDescriptor.setDataSource(sSourceArg); 190cdf0e10cSrcweir aDescriptor[daCommand] <<= sCommandArg; 191cdf0e10cSrcweir aDescriptor[daCursor] <<= xCursor; 192cdf0e10cSrcweir aDescriptor[daSelection] <<= aSelection; 193cdf0e10cSrcweir aDescriptor[daCommandType] <<= nCommandTypeArg; 194cdf0e10cSrcweir 195cdf0e10cSrcweir SwMergeDescriptor aMergeDesc( DBMGR_MERGE, *GetShellPtr(), aDescriptor ); 196cdf0e10cSrcweir pNewDBMgr->MergeNew(aMergeDesc); 197cdf0e10cSrcweir 198cdf0e10cSrcweir if ( bDisposeResultSet ) 199cdf0e10cSrcweir ::comphelper::disposeComponent(xCursor); 200cdf0e10cSrcweir } 201cdf0e10cSrcweir break; 202cdf0e10cSrcweir 203cdf0e10cSrcweir case FN_QRY_INSERT_FIELD: 204cdf0e10cSrcweir { 205cdf0e10cSrcweir const SfxPoolItem* pColumnItem = 0; 206cdf0e10cSrcweir const SfxPoolItem* pColumnNameItem = 0; 207cdf0e10cSrcweir 208cdf0e10cSrcweir pArgs->GetItemState(FN_DB_COLUMN_ANY, sal_False, &pColumnItem); 209cdf0e10cSrcweir pArgs->GetItemState(FN_DB_DATA_COLUMN_NAME_ANY, sal_False, &pColumnNameItem); 210cdf0e10cSrcweir 211cdf0e10cSrcweir ::rtl::OUString sColumnName; 212cdf0e10cSrcweir if(pColumnNameItem) 213cdf0e10cSrcweir ((SfxUsrAnyItem*)pColumnNameItem)->GetValue() >>= sColumnName; 214cdf0e10cSrcweir String sDBName = sSourceArg; 215cdf0e10cSrcweir sDBName += DB_DELIM; 216cdf0e10cSrcweir sDBName += (String)sCommandArg; 217cdf0e10cSrcweir sDBName += DB_DELIM; 218cdf0e10cSrcweir sDBName += String::CreateFromInt32(nCommandTypeArg); 219cdf0e10cSrcweir sDBName += DB_DELIM; 220cdf0e10cSrcweir sDBName += (String)sColumnName; 221cdf0e10cSrcweir 222cdf0e10cSrcweir SwFldMgr aFldMgr(GetShellPtr()); 223*46d2a04eSHerbert Dürr SwInsertFld_Data aData(TYP_DBFLD, 0, sDBName, aEmptyStr, 0, NULL, sal_True); 224cdf0e10cSrcweir if(pConnectionItem) 225cdf0e10cSrcweir aData.aDBConnection = ((SfxUsrAnyItem*)pConnectionItem)->GetValue(); 226cdf0e10cSrcweir if(pColumnItem) 227cdf0e10cSrcweir aData.aDBColumn = ((SfxUsrAnyItem*)pColumnItem)->GetValue(); 228cdf0e10cSrcweir aFldMgr.InsertFld(aData); 229cdf0e10cSrcweir SfxViewFrame* pViewFrame = GetView().GetViewFrame(); 230cdf0e10cSrcweir uno::Reference< frame::XDispatchRecorder > xRecorder = 231cdf0e10cSrcweir pViewFrame->GetBindings().GetRecorder(); 232cdf0e10cSrcweir if ( xRecorder.is() ) 233cdf0e10cSrcweir { 234cdf0e10cSrcweir SfxRequest aReq( pViewFrame, FN_INSERT_DBFIELD ); 235cdf0e10cSrcweir aReq.AppendItem( SfxUInt16Item(FN_PARAM_FIELD_TYPE, TYP_DBFLD)); 236cdf0e10cSrcweir aReq.AppendItem( SfxStringItem( FN_INSERT_DBFIELD, sDBName )); 237cdf0e10cSrcweir aReq.AppendItem( SfxStringItem( FN_PARAM_1, sCommandArg )); 238cdf0e10cSrcweir aReq.AppendItem( SfxStringItem( FN_PARAM_2, sColumnName )); 239cdf0e10cSrcweir aReq.AppendItem( SfxInt32Item( FN_PARAM_3, nCommandTypeArg)); 240cdf0e10cSrcweir aReq.Done(); 241cdf0e10cSrcweir } 242cdf0e10cSrcweir } 243cdf0e10cSrcweir break; 244cdf0e10cSrcweir 245cdf0e10cSrcweir default: 246cdf0e10cSrcweir ASSERT(!this, falscher Dispatcher); 247cdf0e10cSrcweir return; 248cdf0e10cSrcweir } 249cdf0e10cSrcweir } 250cdf0e10cSrcweir 251cdf0e10cSrcweir /*-------------------------------------------------------------------- 252cdf0e10cSrcweir Beschreibung: 253cdf0e10cSrcweir --------------------------------------------------------------------*/ 254cdf0e10cSrcweir 255cdf0e10cSrcweir IMPL_STATIC_LINK( SwBaseShell, InsertDBTextHdl, DBTextStruct_Impl*, pDBStruct ) 256cdf0e10cSrcweir { 257cdf0e10cSrcweir if( pDBStruct ) 258cdf0e10cSrcweir { 259cdf0e10cSrcweir sal_Bool bDispose = sal_False; 260cdf0e10cSrcweir Reference< sdbc::XConnection> xConnection = pDBStruct->xConnection; 261cdf0e10cSrcweir Reference<XDataSource> xSource = SwNewDBMgr::getDataSourceAsParent(xConnection,pDBStruct->aDBData.sDataSource); 262cdf0e10cSrcweir // #111987# the connection is disposed an so no parent has been found 263cdf0e10cSrcweir if(xConnection.is() && !xSource.is()) 264cdf0e10cSrcweir return 0; 265cdf0e10cSrcweir 266cdf0e10cSrcweir if ( !xConnection.is() ) 267cdf0e10cSrcweir { 268cdf0e10cSrcweir xConnection = SwNewDBMgr::GetConnection(pDBStruct->aDBData.sDataSource, xSource); 269cdf0e10cSrcweir bDispose = sal_True; 270cdf0e10cSrcweir } 271cdf0e10cSrcweir 272cdf0e10cSrcweir Reference< XColumnsSupplier> xColSupp; 273cdf0e10cSrcweir if(xConnection.is()) 274cdf0e10cSrcweir xColSupp = SwNewDBMgr::GetColumnSupplier(xConnection, 275cdf0e10cSrcweir pDBStruct->aDBData.sCommand, 276cdf0e10cSrcweir pDBStruct->aDBData.nCommandType == CommandType::QUERY ? 277cdf0e10cSrcweir SW_DB_SELECT_QUERY : SW_DB_SELECT_TABLE); 278cdf0e10cSrcweir 279cdf0e10cSrcweir if( xColSupp.is() ) 280cdf0e10cSrcweir { 281cdf0e10cSrcweir SwDBData aDBData = pDBStruct->aDBData; 282cdf0e10cSrcweir SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create(); 283cdf0e10cSrcweir DBG_ASSERT(pFact, "SwAbstractDialogFactory fail!"); 284cdf0e10cSrcweir ::std::auto_ptr<AbstractSwInsertDBColAutoPilot>pDlg (pFact->CreateSwInsertDBColAutoPilot( pThis->GetView(), 285cdf0e10cSrcweir xSource, 286cdf0e10cSrcweir xColSupp, 287cdf0e10cSrcweir aDBData, 288cdf0e10cSrcweir DLG_AP_INSERT_DB_SEL )); 289cdf0e10cSrcweir if( RET_OK == pDlg->Execute() ) 290cdf0e10cSrcweir { 291cdf0e10cSrcweir Reference <XResultSet> xResSet = pDBStruct->xCursor; 292cdf0e10cSrcweir pDlg->DataToDoc( pDBStruct->aSelection, xSource, xConnection, xResSet); 293cdf0e10cSrcweir } 294cdf0e10cSrcweir } 295cdf0e10cSrcweir if ( bDispose ) 296cdf0e10cSrcweir ::comphelper::disposeComponent(xConnection); 297cdf0e10cSrcweir } 298cdf0e10cSrcweir 299cdf0e10cSrcweir delete pDBStruct; 300cdf0e10cSrcweir return 0; 301cdf0e10cSrcweir } 302cdf0e10cSrcweir 303cdf0e10cSrcweir 304cdf0e10cSrcweir 305