LCOV - code coverage report
Current view: top level - sw/source/uibase/shells - textsh2.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 1 129 0.8 %
Date: 2015-06-13 12:38:46 Functions: 2 7 28.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <cstdarg>
      21             : 
      22             : #include <svtools/svmedit.hxx>
      23             : #include <svl/eitem.hxx>
      24             : #include <svl/whiter.hxx>
      25             : #include <sfx2/event.hxx>
      26             : #include <sfx2/dispatch.hxx>
      27             : #include <sfx2/viewfrm.hxx>
      28             : #include <vcl/msgbox.hxx>
      29             : #include <svl/stritem.hxx>
      30             : #include <svl/itemset.hxx>
      31             : #include <sfx2/request.hxx>
      32             : #include <com/sun/star/sdb/CommandType.hpp>
      33             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      34             : #include <com/sun/star/container/XNameAccess.hpp>
      35             : #include <com/sun/star/sdbc/XDataSource.hpp>
      36             : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
      37             : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
      38             : #include <com/sun/star/sdb/XQueriesSupplier.hpp>
      39             : #include <com/sun/star/sdb/XDatabaseAccess.hpp>
      40             : #include <com/sun/star/beans/XPropertySet.hpp>
      41             : #include <com/sun/star/container/XChild.hpp>
      42             : #include <comphelper/processfactory.hxx>
      43             : #include <com/sun/star/sdbc/XRowSet.hpp>
      44             : #include <sfx2/frame.hxx>
      45             : #include <fldmgr.hxx>
      46             : #include <fldbas.hxx>
      47             : #include "dbmgr.hxx"
      48             : #include <comphelper/uno3.hxx>
      49             : #include <svx/dataaccessdescriptor.hxx>
      50             : 
      51             : #include <vcl/svapp.hxx>
      52             : 
      53             : #include "view.hxx"
      54             : #include "wrtsh.hxx"
      55             : #include "swtypes.hxx"
      56             : #include "cmdid.h"
      57             : #include "swevent.hxx"
      58             : #include "shells.hrc"
      59             : #include "textsh.hxx"
      60             : #include "swabstdlg.hxx"
      61             : #include "dbui.hrc"
      62             : 
      63             : #include <unomid.h>
      64             : 
      65             : #include <boost/scoped_ptr.hpp>
      66             : 
      67             : using namespace ::svx;
      68             : using namespace ::com::sun::star;
      69             : using namespace ::com::sun::star::uno;
      70             : using namespace ::com::sun::star::container;
      71             : using namespace ::com::sun::star::lang;
      72             : using namespace ::com::sun::star::sdb;
      73             : using namespace ::com::sun::star::sdbc;
      74             : using namespace ::com::sun::star::sdbcx;
      75             : using namespace ::com::sun::star::beans;
      76             : using namespace ::com::sun::star::frame;
      77             : 
      78           0 : struct DBTextStruct_Impl
      79             : {
      80             :     SwDBData aDBData;
      81             :     Sequence<Any> aSelection;
      82             :     Reference<XResultSet>   xCursor;
      83             :     Reference<XConnection>  xConnection;
      84             : };
      85             : 
      86           0 : void SwTextShell::ExecDB(SfxRequest &rReq)
      87             : {
      88           0 :     const SfxItemSet *pArgs = rReq.GetArgs();
      89           0 :     SwDBManager* pDBManager = GetShell().GetDBManager();
      90           0 :     OUString sSourceArg, sCommandArg;
      91           0 :     sal_Int32 nCommandTypeArg = 0;
      92             : 
      93           0 :     const SfxPoolItem* pSourceItem = 0;
      94           0 :     const SfxPoolItem* pCursorItem = 0;
      95           0 :     const SfxPoolItem* pConnectionItem = 0;
      96           0 :     const SfxPoolItem* pCommandItem = 0;
      97           0 :     const SfxPoolItem* pCommandTypeItem = 0;
      98           0 :     const SfxPoolItem* pSelectionItem = 0;
      99             : 
     100             :     // first get the selection of rows to be inserted
     101           0 :     pArgs->GetItemState(FN_DB_DATA_SELECTION_ANY, false, &pSelectionItem);
     102             : 
     103           0 :     Sequence<Any> aSelection;
     104           0 :     if(pSelectionItem)
     105           0 :         static_cast<const SfxUsrAnyItem*>(pSelectionItem)->GetValue() >>= aSelection;
     106             : 
     107             :     // get the data source name
     108           0 :     pArgs->GetItemState(FN_DB_DATA_SOURCE_ANY, false, &pSourceItem);
     109           0 :     if(pSourceItem)
     110           0 :         static_cast<const SfxUsrAnyItem*>(pSourceItem)->GetValue() >>= sSourceArg;
     111             : 
     112             :     // get the command
     113           0 :     pArgs->GetItemState(FN_DB_DATA_COMMAND_ANY, false, &pCommandItem);
     114           0 :     if(pCommandItem)
     115           0 :         static_cast<const SfxUsrAnyItem*>(pCommandItem)->GetValue() >>= sCommandArg;
     116             : 
     117             :     // get the command type
     118           0 :     pArgs->GetItemState(FN_DB_DATA_COMMAND_TYPE_ANY, false, &pCommandTypeItem);
     119           0 :     if(pCommandTypeItem)
     120           0 :         static_cast<const SfxUsrAnyItem*>(pCommandTypeItem)->GetValue() >>= nCommandTypeArg;
     121             : 
     122           0 :     Reference<XConnection> xConnection;
     123           0 :     pArgs->GetItemState(FN_DB_CONNECTION_ANY, false, &pConnectionItem);
     124           0 :     if ( pConnectionItem )
     125           0 :         static_cast<const SfxUsrAnyItem*>(pConnectionItem)->GetValue() >>= xConnection;
     126             :     // may be we even get no connection
     127           0 :     if ( !xConnection.is() )
     128             :     {
     129           0 :         Reference<XDataSource> xSource;
     130           0 :         xConnection = SwDBManager::GetConnection(sSourceArg, xSource);
     131             :     }
     132           0 :     if(!xConnection.is())
     133           0 :         return ;
     134             : 
     135             :     // get the cursor, we use to travel, may be NULL
     136           0 :     Reference<XResultSet> xCursor;
     137           0 :     pArgs->GetItemState(FN_DB_DATA_CURSOR_ANY, false, &pCursorItem);
     138           0 :     if ( pCursorItem )
     139           0 :         static_cast<const SfxUsrAnyItem*>(pCursorItem)->GetValue() >>= xCursor;
     140             : 
     141           0 :     switch (rReq.GetSlot())
     142             :     {
     143             :         case FN_QRY_INSERT:
     144             :             {
     145           0 :                 if(pSourceItem && pCommandItem && pCommandTypeItem)
     146             :                 {
     147           0 :                     DBTextStruct_Impl* pNew     = new DBTextStruct_Impl;
     148           0 :                     pNew->aDBData.sDataSource   = sSourceArg;
     149           0 :                     pNew->aDBData.sCommand      = sCommandArg;
     150           0 :                     pNew->aDBData.nCommandType  = nCommandTypeArg;
     151           0 :                     pNew->aSelection            = aSelection;
     152             :                     //if the cursor is NULL, it must be created inside InsertDBTextHdl
     153             :                     // because it called via a PostUserEvent
     154           0 :                     pNew->xCursor               = xCursor;
     155           0 :                     pNew->xConnection           = xConnection;
     156             : 
     157           0 :                     Application::PostUserEvent( LINK( this, SwBaseShell,
     158           0 :                                                       InsertDBTextHdl ), pNew );
     159             :                     // the pNew will be removed in InsertDBTextHdl !!
     160             :                 }
     161             :             }
     162           0 :             break;
     163             : 
     164             :         case FN_QRY_MERGE_FIELD:
     165             :             {
     166             :                 // we don't get any cursor, so we must create our own
     167           0 :                 bool bDisposeResultSet = false;
     168           0 :                 if ( !xCursor.is() )
     169             :                 {
     170           0 :                     xCursor = SwDBManager::createCursor(sSourceArg,sCommandArg,nCommandTypeArg,xConnection);
     171           0 :                     bDisposeResultSet = xCursor.is();
     172             :                 }
     173             : 
     174           0 :                 ODataAccessDescriptor aDescriptor;
     175           0 :                 aDescriptor.setDataSource(sSourceArg);
     176           0 :                 aDescriptor[daCommand]      <<= sCommandArg;
     177           0 :                 aDescriptor[daCursor]       <<= xCursor;
     178           0 :                 aDescriptor[daSelection]    <<= aSelection;
     179           0 :                 aDescriptor[daCommandType]  <<= nCommandTypeArg;
     180             : 
     181           0 :                 SwMergeDescriptor aMergeDesc( DBMGR_MERGE, *GetShellPtr(), aDescriptor );
     182           0 :                 pDBManager->MergeNew(aMergeDesc);
     183             : 
     184           0 :                 if ( bDisposeResultSet )
     185           0 :                     ::comphelper::disposeComponent(xCursor);
     186             :             }
     187           0 :             break;
     188             : 
     189             :         case FN_QRY_INSERT_FIELD:
     190             :             {
     191           0 :                 const SfxPoolItem* pColumnItem = 0;
     192           0 :                 const SfxPoolItem* pColumnNameItem = 0;
     193             : 
     194           0 :                 pArgs->GetItemState(FN_DB_COLUMN_ANY, false, &pColumnItem);
     195           0 :                 pArgs->GetItemState(FN_DB_DATA_COLUMN_NAME_ANY, false, &pColumnNameItem);
     196             : 
     197           0 :                 OUString sColumnName;
     198           0 :                 if(pColumnNameItem)
     199           0 :                     static_cast<const SfxUsrAnyItem*>(pColumnNameItem)->GetValue() >>= sColumnName;
     200           0 :                 OUString sDBName = sSourceArg;
     201           0 :                 sDBName += OUString(DB_DELIM);
     202           0 :                 sDBName += sCommandArg;
     203           0 :                 sDBName += OUString(DB_DELIM);
     204           0 :                 sDBName += OUString::number(nCommandTypeArg);
     205           0 :                 sDBName += OUString(DB_DELIM);
     206           0 :                 sDBName += sColumnName;
     207             : 
     208           0 :                 SwFieldMgr aFieldMgr(GetShellPtr());
     209           0 :                 SwInsertField_Data aData(TYP_DBFLD, 0, sDBName, OUString(), 0);
     210           0 :                 if(pConnectionItem)
     211           0 :                     aData.m_aDBConnection = static_cast<const SfxUsrAnyItem*>(pConnectionItem)->GetValue();
     212           0 :                 if(pColumnItem)
     213           0 :                     aData.m_aDBColumn = static_cast<const SfxUsrAnyItem*>(pColumnItem)->GetValue();
     214           0 :                 aFieldMgr.InsertField(aData);
     215           0 :                 SfxViewFrame* pViewFrame = GetView().GetViewFrame();
     216             :                 uno::Reference< XDispatchRecorder > xRecorder =
     217           0 :                         pViewFrame->GetBindings().GetRecorder();
     218           0 :                 if ( xRecorder.is() )
     219             :                 {
     220           0 :                     SfxRequest aReq( pViewFrame, FN_INSERT_DBFIELD );
     221           0 :                     aReq.AppendItem( SfxUInt16Item(FN_PARAM_FIELD_TYPE, TYP_DBFLD));
     222           0 :                     aReq.AppendItem( SfxStringItem( FN_INSERT_DBFIELD, sDBName ));
     223           0 :                     aReq.AppendItem( SfxStringItem( FN_PARAM_1, sCommandArg ));
     224           0 :                     aReq.AppendItem( SfxStringItem( FN_PARAM_2, sColumnName ));
     225           0 :                     aReq.AppendItem( SfxInt32Item( FN_PARAM_3, nCommandTypeArg));
     226           0 :                     aReq.Done();
     227           0 :                 }
     228             :             }
     229           0 :             break;
     230             : 
     231             :         default:
     232             :             OSL_ENSURE(false, "wrong dispatcher");
     233           0 :             return;
     234           0 :     }
     235             : }
     236             : 
     237           0 : IMPL_LINK( SwBaseShell, InsertDBTextHdl, DBTextStruct_Impl*, pDBStruct )
     238             : {
     239           0 :     if( pDBStruct )
     240             :     {
     241           0 :         bool bDispose = false;
     242           0 :         Reference< sdbc::XConnection> xConnection = pDBStruct->xConnection;
     243           0 :         Reference<XDataSource> xSource = SwDBManager::getDataSourceAsParent(xConnection,pDBStruct->aDBData.sDataSource);
     244             :         // #111987# the connection is disposed an so no parent has been found
     245           0 :         if(xConnection.is() && !xSource.is())
     246           0 :             return 0;
     247             : 
     248           0 :         if ( !xConnection.is()  )
     249             :         {
     250           0 :             xConnection = SwDBManager::GetConnection(pDBStruct->aDBData.sDataSource, xSource);
     251           0 :             bDispose = true;
     252             :         }
     253             : 
     254           0 :         Reference< XColumnsSupplier> xColSupp;
     255           0 :         if(xConnection.is())
     256           0 :             xColSupp = SwDBManager::GetColumnSupplier(xConnection,
     257             :                                     pDBStruct->aDBData.sCommand,
     258           0 :                                     pDBStruct->aDBData.nCommandType == CommandType::QUERY ?
     259           0 :                                         SwDBSelect::QUERY : SwDBSelect::TABLE);
     260             : 
     261           0 :         if( xColSupp.is() )
     262             :         {
     263           0 :             SwDBData aDBData = pDBStruct->aDBData;
     264           0 :             SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
     265             :             OSL_ENSURE(pFact, "SwAbstractDialogFactory fail!");
     266           0 :             boost::scoped_ptr<AbstractSwInsertDBColAutoPilot>pDlg (pFact->CreateSwInsertDBColAutoPilot(GetView(),
     267             :                                                                                                 xSource,
     268             :                                                                                                 xColSupp,
     269           0 :                                                                                                 aDBData));
     270           0 :             if( RET_OK == pDlg->Execute() )
     271             :             {
     272           0 :                 Reference <XResultSet> xResSet = pDBStruct->xCursor;
     273           0 :                 pDlg->DataToDoc( pDBStruct->aSelection, xSource, xConnection, xResSet);
     274           0 :             }
     275             :         }
     276           0 :         if ( bDispose )
     277           0 :             ::comphelper::disposeComponent(xConnection);
     278             :     }
     279             : 
     280           0 :     delete pDBStruct;
     281           0 :     return 0;
     282         177 : }
     283             : 
     284             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11