Tag: cws_src680_qiq
User: fs      
Date: 06/05/18 02:17:39

Modified:
 /dba/dbaccess/source/ui/misc/
  defaultobjectnamecheck.cxx

Log:
 #i51143# added new classes for checking table and query names against both 
table and query containers (if the database supports queries in queries)

File Changes:

Directory: /dba/dbaccess/source/ui/misc/
========================================

File [changed]: defaultobjectnamecheck.cxx
Url: 
http://dba.openoffice.org/source/browse/dba/dbaccess/source/ui/misc/defaultobjectnamecheck.cxx?r1=1.1.2.1&r2=1.1.2.2
Delta lines:  +184 -3
---------------------
--- defaultobjectnamecheck.cxx  17 May 2006 11:42:22 -0000      1.1.2.1
+++ defaultobjectnamecheck.cxx  18 May 2006 09:17:37 -0000      1.1.2.2
@@ -4,9 +4,9 @@
  *
  *  $RCSfile: defaultobjectnamecheck.cxx,v $
  *
- *  $Revision: 1.1.2.1 $
+ *  $Revision: 1.1.2.2 $
  *
- *  last change: $Author: fs $ $Date: 2006/05/17 11:42:22 $
+ *  last change: $Author: fs $ $Date: 2006/05/18 09:17:37 $
  *
  *  The Contents of this file are made available subject to
  *  the terms of GNU Lesser General Public License Version 2.1.
@@ -49,11 +49,23 @@
 #ifndef _COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP_
 #include <com/sun/star/lang/IllegalArgumentException.hpp>
 #endif
+#ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
+#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDB_XQUERIESSUPPLIER_HPP_
+#include <com/sun/star/sdb/XQueriesSupplier.hpp>
+#endif
+#ifndef _COM_SUN_STAR_SDB_COMMANDTYPE_HPP_
+#include <com/sun/star/sdb/CommandType.hpp>
+#endif
 /** === end UNO includes === **/
 
 #ifndef _DBHELPER_DBEXCEPTION_HXX_
 #include <connectivity/dbexception.hxx>
 #endif
+#ifndef CONNECTIVITY_INC_CONNECTIVITY_DBMETADATA_HXX
+#include <connectivity/dbmetadata.hxx>
+#endif
 
 #ifndef _RTL_USTRBUF_HXX_
 #include <rtl/ustrbuf.hxx>
@@ -66,6 +78,9 @@
 #include <tools/string.hxx>
 #endif
 
+#include <vector>
+#include <boost/shared_ptr.hpp>
+
 //........................................................................
 namespace dbaui
 {
@@ -78,10 +93,18 @@
     using ::com::sun::star::container::XHierarchicalNameAccess;
     using ::com::sun::star::sdbc::SQLException;
     using ::com::sun::star::uno::Exception;
+    using ::com::sun::star::sdbc::XConnection;
+    using ::com::sun::star::sdbcx::XTablesSupplier;
+    using ::com::sun::star::sdb::XQueriesSupplier;
+    using ::com::sun::star::uno::UNO_QUERY_THROW;
+    using ::com::sun::star::uno::makeAny;
+    using ::com::sun::star::uno::Any;
        /** === end UNO using === **/
 
     using namespace dbtools;
 
+    namespace CommandType = ::com::sun::star::sdb::CommandType;
+
        //====================================================================
        //= helper
        //====================================================================
@@ -95,6 +118,46 @@
             aError.Message = sErrorMessage;
             _out_rErrorToDisplay = aError;
         }
+
+        /** retrieves an container of queries or tables from a given SDB-level 
connection
+            @param  _rxSdbLevelConnection
+                the connection object. Must not be <NULL/>.
+            @param  _nCommandType
+                the CommandType specifying whether tables or queries should be 
obtained. Only
+                CommandType::TABLE and CommandType::QUERY are valid values, 
for all other values,
+                the behavior is undefined.
+            @throws RuntimeException
+                if the given connection does not provide the desired object 
container
+        */
+        Reference< XNameAccess > lcl_getObjectContainer( const Reference< 
XConnection >& _rxSdbLevelConnection, sal_Int32 _nCommandType )
+        {
+            OSL_ENSURE( _rxSdbLevelConnection.is(), "lcl_getObjectContainer: 
this will crash!" );
+
+            Reference< XNameAccess > xContainer;
+            switch ( _nCommandType )
+            {
+                case CommandType::TABLE:
+                {
+                    Reference< XTablesSupplier > xSupplier( 
_rxSdbLevelConnection, UNO_QUERY_THROW );
+                    xContainer = Reference< XNameAccess >( 
xSupplier->getTables(), UNO_QUERY_THROW );
+                }
+                break;
+
+                case CommandType::QUERY:
+                {
+                    Reference< XQueriesSupplier > xSupplier( 
_rxSdbLevelConnection, UNO_QUERY_THROW );
+                    xContainer = Reference< XNameAccess >( 
xSupplier->getQueries(), UNO_QUERY_THROW );
+                }
+                break;
+
+                default:
+                    OSL_ENSURE( false, "lcl_getObjectContainer: unsupported 
command type!" );
+                    break;
+            }
+
+            return xContainer;
+        }
+
     }
 
        //====================================================================
@@ -191,6 +254,124 @@
 
         lcl_fillNameExistsError( _rObjectName, _out_rErrorToDisplay );
         return false;
+    }
+
+    //====================================================================
+    //= TablesAndQueriesNameCheck_Impl
+    //====================================================================
+    typedef ::std::vector< ::boost::shared_ptr< IObjectNameCheck > >    
CheckerPtrArray;
+    struct TablesAndQueriesNameCheck_Impl
+    {
+        CheckerPtrArray aSlaveChecks;
+    };
+
+    namespace
+    {
+        /** constructs a TablesAndQueriesNameCheck instance
+        */
+        void    lcl_construct( TablesAndQueriesNameCheck_Impl& _rImpl, const 
Reference< XConnection >& _rxSdbLevelConnection )
+        {
+            try
+            {
+                _rImpl.aSlaveChecks.push_back( ::boost::shared_ptr< 
IObjectNameCheck >( new PlainNameCheck( 
+                    lcl_getObjectContainer( _rxSdbLevelConnection, 
CommandType::TABLE ) ) ) );
+
+                _rImpl.aSlaveChecks.push_back( ::boost::shared_ptr< 
IObjectNameCheck >( new PlainNameCheck( 
+                    lcl_getObjectContainer( _rxSdbLevelConnection, 
CommandType::QUERY ) ) ) );
+            }
+            catch( const Exception& )
+            {
+               DBG_UNHANDLED_EXCEPTION();
+            }
+        }
+    }
+
+    //====================================================================
+    //= TablesAndQueriesNameCheck
+    //====================================================================
+    //--------------------------------------------------------------------
+    TablesAndQueriesNameCheck::TablesAndQueriesNameCheck( const Reference< 
XConnection >& _rxSdbLevelConnection )
+        :m_pImpl( new TablesAndQueriesNameCheck_Impl )
+    {
+        lcl_construct( *m_pImpl, _rxSdbLevelConnection );
+    }
+
+    //--------------------------------------------------------------------
+    TablesAndQueriesNameCheck::~TablesAndQueriesNameCheck()
+    {
+    }
+
+    //--------------------------------------------------------------------
+    bool TablesAndQueriesNameCheck::isNameValid( const ::rtl::OUString& 
_rObjectName, ::dbtools::SQLExceptionInfo& _out_rErrorToDisplay ) const
+    {
+        bool bIsValid = true;
+        for (   CheckerPtrArray::const_iterator loop = 
m_pImpl->aSlaveChecks.begin();
+                ( loop != m_pImpl->aSlaveChecks.end() ) && bIsValid;
+                ++loop
+            )
+        {
+            bIsValid &= (*loop)->isNameValid( _rObjectName, 
_out_rErrorToDisplay );
+        }
+
+        if ( bIsValid )
+            return true;
+
+        // adjust the error message
+        String sNameIsUsed( ModuleRes( STR_NAME_ALREADY_USED_IN_DB ) );
+        sNameIsUsed.SearchAndReplaceAllAscii( "$name$", _rObjectName );
+        String sNeedDistinctNames( ModuleRes( 
STR_QUERY_AND_TABLE_DISTINCT_NAMES ) );
+        _out_rErrorToDisplay = SQLException(
+            sNameIsUsed, NULL, ::rtl::OUString(), 0, makeAny( SQLException(
+                sNeedDistinctNames, NULL, ::rtl::OUString(), 0, Any() ) ) );
+        return false;
+    }
+
+    //====================================================================
+    //= DynamicTableOrQueryNameCheck_Impl
+    //====================================================================
+    struct DynamicTableOrQueryNameCheck_Impl
+    {
+        ::boost::shared_ptr< IObjectNameCheck > pDelegateeCheck;
+    };
+
+    //====================================================================
+    //= DynamicTableOrQueryNameCheck
+    //====================================================================
+    //--------------------------------------------------------------------
+    DynamicTableOrQueryNameCheck::DynamicTableOrQueryNameCheck( const 
Reference< XConnection >& _rxSdbLevelConnection, sal_Int32 _nCommandType )
+        :m_pImpl( new DynamicTableOrQueryNameCheck_Impl )
+    {
+        if ( !_rxSdbLevelConnection.is() )
+            throw IllegalArgumentException();
+        if ( ( _nCommandType != CommandType::QUERY ) && ( _nCommandType != 
CommandType::TABLE ) )
+            throw IllegalArgumentException();
+
+        bool bSupportsQueriesInFrom = false;
+        try
+        {
+            DatabaseMetaData aMeta( _rxSdbLevelConnection );
+            bSupportsQueriesInFrom = aMeta.supportsSubqueriesInFrom();
+        }
+        catch( const Exception& )
+        {
+               DBG_UNHANDLED_EXCEPTION();
+        }
+
+        if ( bSupportsQueriesInFrom )
+            m_pImpl->pDelegateeCheck.reset( new TablesAndQueriesNameCheck( 
_rxSdbLevelConnection ) );
+        else
+            m_pImpl->pDelegateeCheck.reset( new PlainNameCheck( 
lcl_getObjectContainer( _rxSdbLevelConnection, _nCommandType ) ) );
+    }
+
+    //--------------------------------------------------------------------
+    DynamicTableOrQueryNameCheck::~DynamicTableOrQueryNameCheck()
+    {
+    }
+
+    //--------------------------------------------------------------------
+    bool DynamicTableOrQueryNameCheck::isNameValid( const ::rtl::OUString& 
_rObjectName, ::dbtools::SQLExceptionInfo& _out_rErrorToDisplay ) const
+    {
+        return m_pImpl->pDelegateeCheck->isNameValid( _rObjectName, 
_out_rErrorToDisplay );
     }
 
 //........................................................................




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to