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