Driver Service

From Apache OpenOffice Wiki
Jump to: navigation, search



The Driver service is the entry point to create the first contact with any database. As shown in the illustration above, the class that implements the service Driver is responsible for creating a connection object that represents the database on the client side.

The class must be derived from the interface com.sun.star.sdbc.XDriver that defines the methods needed to create a connection object. The code in the following lines shows a snippet of a driver class.

  // --------------------------------------------------------------------------------
  Reference< XConnection > SAL_CALL SkeletonDriver::connect( const ::rtl::OUString& url, 
      const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException)
  {
      // create a new connection with the given properties and append it to our vector
      OConnection* pCon = new OConnection(this);
      Reference< XConnection > xCon = pCon; // important here because otherwise the connection
                                            // could be deleted inside (refcount goes -> 0)
      pCon->construct(url,info);            // late constructor call which can throw exception
                                            // and allows a correct dtor call when so
      m_xConnections.push_back(WeakReferenceHelper(*pCon));
      
      return xCon;
  }
  // --------------------------------------------------------------------------------
  sal_Bool SAL_CALL SkeletonDriver::acceptsURL( const ::rtl::OUString& url ) 
            throw(SQLException, RuntimeException)
  {
      // here we have to look if we support this url format
      // change the URL format to your needs, but please be aware that 
      //the first who accepts the URL wins.
      return (!url.compareTo(::rtl::OUString::createFromAscii("sdbc:skeleton:"),14));
  }
  // --------------------------------------------------------------------------------
  Sequence< DriverPropertyInfo > SAL_CALL SkeletonDriver::getPropertyInfo( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException)
  {
      // if you have something special to say, return it here :-)
      return Sequence< DriverPropertyInfo >();
  }
  // --------------------------------------------------------------------------------
  sal_Int32 SAL_CALL SkeletonDriver::getMajorVersion( ) throw(RuntimeException)
  {
      return 0; // depends on you
  }
  // --------------------------------------------------------------------------------
  sal_Int32 SAL_CALL SkeletonDriver::getMinorVersion( ) throw(RuntimeException)
  {
      return 1; // depends on you
  }
  // --------------------------------------------------------------------------------

The main methods of this class are acceptsURL and connect:

  • The method acceptsURL() is called every time a user wants to create a connection through the DriverManager, because the DriverManager decides the Driver it should ask to connect to the given URL. Therefore this method should be small and run very fast.
  • The method connect() is called after the method acceptsURL() is invoked and returned true. The connect() could be seen as a factory method that creates Connection services specific for a driver implementation. To accomplish this, the Driver class must be singleton. Singleton means that only one instance of the Driver class may exist at the same time.

If more information is required about the other methods, refer to com.sun.star.sdbc.Driver for a complete description.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages