SQL 2 Excel Pro - Using a Custom Database Driver PDF Print E-mail

You can define your own custom database driver if there are no supplied driver for your database, or if you are missing a required extension for the supplied driver. As long as you have some way of connecteing and querying your database through PHP then you should be able to define a custom driver for SQL 2 Excel Pro.

SQL 2 Excel Pro ships with a sample driver that you can use as a starting point. The sample driver is located here:

./administrator/components/com_sql2excel/helpers/driver_sample.php

The sample driver looks like this:

   1 
   2 
   3 
   4 
   5 
   6 
   7 
   8 
   9 
  10 
  11 
  12 
  13 
  14 
  15 
  16 
  17 
  18 
  19 
  20 
  21 
  22 
  23 
  24 
  25 
  26 
  27 
  28 
  29 
  30 
  31 
  32 
  33 
  34 
  35 
  36 
  37 
  38 
  39 
  40 
  41 
  42 
  43 
  44 
  45 
  46 
  47 
  48 
  49 

  50 
  51 
  52 
  53 
  54 
  55 
  56 
  57 
  58 
  59 
  60 
  61 
  62 
  63 
  64 
  65 
  66 
  67 
  68 
  69 
  70 
  71 
  72 
  73 
  74 
  75 
  76 
  77 
  78 
  79 
  80 
  81 
  82 
  83 
  84 
  85 
  86 
  87 
  88 
  89 
  90 
  91 
  92 
  93 
  94 
  95 
  96 
  97 
  98 
  99 
 100 
 101 
 102 
 103 
 104 
 105 
 106 
 107 
 108 
 109 
 110 
 111 
 112 
 113 
 114 
 115 
 116 
 117 
 118 
 119 
 120 
 121 
 122 
 123 
 124 
 125 
 126 
 127 
 128 
 129 
 130 
 131 
 132 
 133 
 134 
 135 
 136 
 137 
 138 
 139 
 140 
 141 
 142 
 143 
 144 
 145 
 146 
 147 
<?php

/**********************************************************************
 *
 * Sample Custom Database Connection Script for a MySQL database
 *
 *    Note:  SQL 2 Excel Pro comes with a built-in driver for MySQL
 *           so it doesn't make much sense for writing a custom driver.
 *           This code is just supplied as an example on how to write
 *           a custom driver. You can test this driver against your 
 *           Joomla database before you start writing your own driver.
 *
 **********************************************************************/


// No direct access
defined'_JEXEC' ) or die( 'Restricted access' );


class 
sql2excel_customDB 
{

    
/*************************************************************
    *
    *  connect()
    *
    *     Returns:
    *       - Database Connection Object if successful
    *         - NULL if unsuccessful
    *
    **************************************************************/
    
function connect($hostname$database$username$password$test=0) {
    
           
$option = array (); 
           
$option ['driver'] = 'mysql'
           
$option ['host'] = $hostname ;
           
$option ['user'] = $username;
           
$option ['password'] = $password;
           
$option ['database'] = trim($database);
           
$option ['prefix'] = 'jos_'
        
$db JFactory::getDBO ();
        
$db = & JDatabase::getInstance ($option);

        
        if ( 
get_class($db) != 'JDatabaseMySQL' ) {    
            
// No valid connection! Return NULL            
            
if ( $test == ) {
                
// Output Debug Information for Connection Test
                
print '<p><font color="red">Database Connection Error :</font>
                  </p>'
;
                print 
'<pre>';
                
print_r($db);
                print 
'</pre>';
            }            
            
$db null;
        }
                
        return 
$db;
    }
    
    

    
/*************************************************************
    *
    * query()
    *
    *     Returns an array with result objects if successful
    *
    *         Expeccted array format is shown below.
    *
    *        SELECT id, title FROM jos_content LIMIT 0,2
    *
    *
    *        Array
    *        (
    *            [0] => stdClass Object
    *                (
    *                    [id] => 1
    *                    [title] => Welcome to Joomla!
    *                )
    *
    *            [1] => stdClass Object
    *                (
    *                    [id] => 2
    *                    [title] => Newsflash 1
    *                )
    *        )
    *
    *
    *
    *
    *
    *      Return Error Message (as a string) if unsuccessful
    **************************************************************/
    
function query($db$query,$returnResults=1) {
    
        if ( 
$db ) {
            
$db->setQuery$query );

            if ( 
$returnResults ) {            
                
// Query returning reults                
                
$rows $db->loadObjectList();
                if ( !
$rows ) {
                    if ( 
is_null($rows) ) {
                        
// Return error message
                        
return $db->getErrorMsg();
                    } else {
                        
// No results
                        
return '';
                    }
                } else {
                    return 
$rows;
                }
                
            } else {            
                
// No returned results (Update query from Scheduler etc)        
                
$ret $db->query();
                if ( 
$ret ) {
                    return 
true;
                } else {
                    return 
$db->getErrorMsg();
                }            
            }            
        } else {
            return 
false;
        }
    }

    
/*************************************************************
    *
    * test()
    *
    *   Optional Query Test Function
    *
    *
    **************************************************************/
    
function test($db) {
        
$ret false;
        if ( 
$db ) {
            
$query="SELECT id, title from jos_content LIMIT 0,2";
            
$rows sql2excel_customDB::query($db$query);
            return 
$rows;
        }
    }
    
}

?>



The driver contains one class, sql2excel_customDB, which should have these three methods:

  1. connect()

    This method should return the database connection handle. Return NULL if unsuccessful.

  2. query()

    This method is used to run the queries. Called by SQL 2 Excel to get the query results for the Worksheets. The returned format should be an array of objects, like this:
    Array
    (
    [0] => stdClass Object
    (
    [id] => 1
    [title] => Welcome to Joomla!
    )

    [1] => stdClass Object
    (
    [id] => 2
    [title] => Newsflash 1
    )
    )

    The driver should return the strings in UTF8 character set if possible for full UTF8 support. If your driver returns the strings in another character set then you may get corrupted output. The "Limited Character Set Support" Excel Writer is less sensitive to the input character set and may be used together with a non-UTF8 database driver as a workaround.

  3. test() [optional]

    This method can be defined to run a simple test query against the database. Only used in the database connection test screen to verify that you can do a query against the database.

 

Tips for writing your own driver

  1. Start with the supplied sample driver. Copy the driver file to a safe location on your webserver, e.g. a folder which is not touched when installing/uninstalling components or upgrading Joomla. Your webserver do need to be able to read/access the file so it typically means that the file has to be located somewhere inside your htdocs/public_html folder (or below).

  2. Setup a new database connection in SQL 2 Excel Pro. Define it as a custom driver and point to the driver file. Specify the full path on disk to the driver file.

    customdbconnect

  3. Fill in the username/password and database for the Joomla database.

  4. Save the driver

  5. Test it. Make sure you get an ok connection. Once you have a valid connection test - try using the custom database in a Worksheet.

  6. Once you have this working, copy the driver to your new custom copy and start modifying the code. Start with the connect() method. Run the database test from the SQL 2 Excel Pro Database page to verify your code.
  7. Move to the query() method once you have got the connect() method to work. Make sure you get the returned array/object format correct from your custom driver. See above for the required format.

  8. Once you have got the query() method working, try using your custom database connection in a Worksheet.

    • Set the output format to HTML when testing your driver. This allows you to see and correct errors from yoru code easier
    • Make sure you don't Cache your Workbook that you use for test

  9. Finally, if you do get it work - please consider sharing your code with others! I would be more than happy to post your driver code here on Joomla-R-Us.com.

 

Sample Custom Drivers

Driver Database Remarks
driver_sample.zip MySQL Sample custom driver supplied with SQL 2 Excel Pro
driver_sample_mssql.zip SQL Server
User supplied driver used to connect to a MS SQL Server database using ADODB:odbc

 

Comments (0)add comment

Write comment

security image
Write the displayed characters


busy
 
 
  Design by augs-burg.de & go-vista.de  
 
     
 
   
 

The Joomla!(R) name is used under a limited license from Open Source Matters in the United States and other countries. http://Joomla-R-Us.com is not affiliated with or endorsed by Open Source Matters or the Joomla! Project.