Author: Kore Nordmann
Date: 2007-02-15 11:19:58 +0100 (Thu, 15 Feb 2007)
New Revision: 4654

Log:
- Implemented #9405 (PDO data set)

Added:
   trunk/Graph/src/datasets/pdo.php
   trunk/Graph/src/exceptions/missing_column.php
   trunk/Graph/src/exceptions/statement_not_executed.php
   trunk/Graph/src/exceptions/too_many_columns.php
   trunk/Graph/tests/dataset_pdo_test.php
Modified:
   trunk/Graph/src/graph.php
   trunk/Graph/src/graph_autoload.php
   trunk/Graph/tests/suite.php

Added: trunk/Graph/src/datasets/pdo.php
===================================================================
--- trunk/Graph/src/datasets/pdo.php    2007-02-15 10:15:14 UTC (rev 4653)
+++ trunk/Graph/src/datasets/pdo.php    2007-02-15 10:19:58 UTC (rev 4654)
@@ -0,0 +1,111 @@
+<?php
+/**
+ * File containing the ezcGraphPdoDataSet class
+ *
+ * @package Graph
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+/**
+ * Class to transform PDO results into ezcGraphDataSets
+ *
+ * @package Graph
+ * @mainclass
+ */
+class ezcGraphPdoDataSet extends ezcGraphDataSet
+{
+    /**
+     * Constructor
+     * 
+     * @param PDOStatement $statement
+     * @return ezcGraphPdoDataSet
+     */
+    public function __construct( PDOStatement $statement, array $definition = 
null )
+    {
+        parent::__construct();
+
+        $this->data = array();
+        $this->createFromPdo( $statement, $definition );
+    }
+
+    /**
+     * setData
+     *
+     * Can handle data provided through an array or iterator.
+     * 
+     * @param array|Iterator $data 
+     * @access public
+     * @return void
+     */
+    protected function createFromPdo( PDOStatement $statement, array 
$definition = null ) 
+    {
+        $count = 0;
+
+        if ( $definition === null )
+        {
+            while ( $row = $statement->fetch( PDO::FETCH_NUM ) )
+            {
+                ++$count;
+
+                switch ( count( $row ) )
+                {
+                    case 1:
+                        $this->data[] = $row[0];
+                        break;
+                    case 2:
+                        $this->data[$row[0]] = $row[1];
+                        break;
+                    default:
+                        throw new ezcGraphPdoDataSetTooManyColumnsException( 
$row );
+                }
+            }
+        }
+        else
+        {
+            while ( $row = $statement->fetch( PDO::FETCH_NAMED ) )
+            {
+                ++$count;
+
+                if ( !array_key_exists( $definition[ezcGraph::VALUE], $row ) )
+                {
+                    throw new ezcGraphPdoDataSetMissingColumnException( 
$definition[ezcGraph::VALUE] );
+                }
+
+                $value = $row[$definition[ezcGraph::VALUE]];
+
+                if ( array_key_exists( ezcGraph::KEY, $definition ) )
+                {
+                    if ( !array_key_exists( $definition[ezcGraph::KEY], $row ) 
)
+                    {
+                        throw new ezcGraphPdoDataSetMissingColumnException( 
$definition[ezcGraph::KEY] );
+                    }
+
+                    $this->data[$row[$definition[ezcGraph::KEY]]] = $value;
+                }
+                else
+                {
+                    $this->data[] = $value;
+                }
+            }
+        }
+
+        // Empty result set
+        if ( $count <= 0 )
+        {
+            throw new ezcGraphPdoDataSetStatementNotExecutedException( 
$statement );
+        }
+    }
+
+    /**
+     * Returns the number of elements in this dataset
+     * 
+     * @return int
+     */
+    public function count()
+    {
+        return count( $this->data );
+    }
+}
+
+?>


Property changes on: trunk/Graph/src/datasets/pdo.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: trunk/Graph/src/exceptions/missing_column.php
===================================================================
--- trunk/Graph/src/exceptions/missing_column.php       2007-02-15 10:15:14 UTC 
(rev 4653)
+++ trunk/Graph/src/exceptions/missing_column.php       2007-02-15 10:19:58 UTC 
(rev 4654)
@@ -0,0 +1,24 @@
+<?php
+/**
+ * File containing the ezcGraphPdoDataSetMissingColumnException class
+ *
+ * @package Graph
+ * @version //autogen//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+/**
+ * Exception thrown if a requetsted column could not be found in result set
+ *
+ * @package Graph
+ * @version //autogen//
+ */
+class ezcGraphPdoDataSetMissingColumnException extends ezcGraphException
+{
+    public function __construct( $column )
+    {
+        parent::__construct( "Missing column '{$column}' in result set." );
+    }
+}
+
+?>


Property changes on: trunk/Graph/src/exceptions/missing_column.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: trunk/Graph/src/exceptions/statement_not_executed.php
===================================================================
--- trunk/Graph/src/exceptions/statement_not_executed.php       2007-02-15 
10:15:14 UTC (rev 4653)
+++ trunk/Graph/src/exceptions/statement_not_executed.php       2007-02-15 
10:19:58 UTC (rev 4654)
@@ -0,0 +1,24 @@
+<?php
+/**
+ * File containing the ezcGraphPdoDataSetStatementNotExecutedException class
+ *
+ * @package Graph
+ * @version //autogen//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+/**
+ * Exception thrown if a given statement has not been executed.
+ *
+ * @package Graph
+ * @version //autogen//
+ */
+class ezcGraphPdoDataSetStatementNotExecutedException extends ezcGraphException
+{
+    public function __construct( $statement )
+    {
+        parent::__construct( "Empty result set. Execute the statement before 
using with ezcGraphPdoDataSet." );
+    }
+}
+
+?>


Property changes on: trunk/Graph/src/exceptions/statement_not_executed.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: trunk/Graph/src/exceptions/too_many_columns.php
===================================================================
--- trunk/Graph/src/exceptions/too_many_columns.php     2007-02-15 10:15:14 UTC 
(rev 4653)
+++ trunk/Graph/src/exceptions/too_many_columns.php     2007-02-15 10:19:58 UTC 
(rev 4654)
@@ -0,0 +1,26 @@
+<?php
+/**
+ * File containing the ezcGraphPdoDataSetTooManyColumnsException class
+ *
+ * @package Graph
+ * @version //autogen//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+/**
+ * Exception thrown if a data set has too many columns for a key value 
+ * association.
+ *
+ * @package Graph
+ * @version //autogen//
+ */
+class ezcGraphPdoDataSetTooManyColumnsException extends ezcGraphException
+{
+    public function __construct( $row )
+    {
+        $columnCount = count( $row );
+        parent::__construct( "'{$columnCount}' columns are too many in a 
result." );
+    }
+}
+
+?>


Property changes on: trunk/Graph/src/exceptions/too_many_columns.php
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: trunk/Graph/src/graph.php
===================================================================
--- trunk/Graph/src/graph.php   2007-02-15 10:15:14 UTC (rev 4653)
+++ trunk/Graph/src/graph.php   2007-02-15 10:19:58 UTC (rev 4654)
@@ -110,6 +110,15 @@
      * Font type definition. Used for Palm Format Fonts for Ming driver.
      */
     const PALM_FONT = 3;
+
+    /**
+     * Identifier for keys in complex dataset arrays
+     */
+    const KEY = 0;
+    /**
+     * Identifier for values in complex dataset arrays
+     */
+    const VALUE = 1;
 }
 
 ?>

Modified: trunk/Graph/src/graph_autoload.php
===================================================================
--- trunk/Graph/src/graph_autoload.php  2007-02-15 10:15:14 UTC (rev 4653)
+++ trunk/Graph/src/graph_autoload.php  2007-02-15 10:19:58 UTC (rev 4654)
@@ -86,6 +86,10 @@
     'ezcGraphDataSet'                           => 'Graph/datasets/base.php',
     'ezcGraphArrayDataSet'                      => 'Graph/datasets/array.php',
     'ezcGraphInvalidArrayDataSourceException'   => 
'Graph/exceptions/invalid_data_source.php',
+    'ezcGraphPdoDataSet'                        => 'Graph/datasets/pdo.php',
+    'ezcGraphPdoDataSetTooManyColumnsException' => 
'Graph/exceptions/too_many_columns.php',
+    'ezcGraphPdoDataSetMissingColumnException'  => 
'Graph/exceptions/missing_column.php',
+    'ezcGraphPdoDataSetStatementNotExecutedException' => 
'Graph/exceptions/statement_not_executed.php',
     'ezcGraphNumericDataSet'                    => 
'Graph/datasets/numeric.php',
     'ezcGraphDataSetAveragePolynom'             => 
'Graph/datasets/average.php',
     'ezcGraphDatasetAverageInvalidKeysException' => 
'Graph/exceptions/invalid_keys.php',

Added: trunk/Graph/tests/dataset_pdo_test.php
===================================================================
--- trunk/Graph/tests/dataset_pdo_test.php      2007-02-15 10:15:14 UTC (rev 
4653)
+++ trunk/Graph/tests/dataset_pdo_test.php      2007-02-15 10:19:58 UTC (rev 
4654)
@@ -0,0 +1,355 @@
+<?php
+/**
+ * ezcGraphPdoDataSetTest 
+ * 
+ * @package Graph
+ * @version //autogen//
+ * @subpackage Tests
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+require_once dirname( __FILE__ ) . '/test_case.php';
+
+/**
+ * Tests for ezcGraph class.
+ * 
+ * @package ImageAnalysis
+ * @subpackage Tests
+ */
+class ezcGraphPdoDataSetTest extends ezcGraphTestCase
+{
+    protected $basePath;
+
+    protected $tempDir;
+
+       public static function suite()
+       {
+               return new PHPUnit_Framework_TestSuite( 
"ezcGraphPdoDataSetTest" );
+       }
+
+    protected function setUp()
+    {
+        static $i = 0;
+        $this->tempDir = $this->createTempDir( __CLASS__ . sprintf( '_%03d_', 
++$i ) ) . '/';
+        $this->basePath = dirname( __FILE__ ) . '/data/';
+
+        // Try to build up database connection
+        try
+        {
+            $db = ezcDbInstance::get();
+        }
+        catch ( Exception $e )
+        {
+            $this->markTestSkipped( 'Database connection required for PDO 
statement tests.' );
+        }
+
+        $this->q = new ezcQueryInsert( $db );
+        try
+        {
+            $db->exec( 'DROP TABLE graph_pdo_test' );
+        }
+        catch ( Exception $e ) {} // eat
+
+        // Create test table
+        $db->exec( 'CREATE TABLE graph_pdo_test ( id INT, browser 
VARCHAR(255), hits INT )' );
+
+        // Insert some data
+        $db->exec( "INSERT INTO graph_pdo_test VALUES ( '', 'Firefox', 2567 )" 
);
+        $db->exec( "INSERT INTO graph_pdo_test VALUES ( '', 'Opera', 543 )" );
+        $db->exec( "INSERT INTO graph_pdo_test VALUES ( '', 'Safari', 23 )" );
+        $db->exec( "INSERT INTO graph_pdo_test VALUES ( '', 'Konquror', 812 )" 
);
+        $db->exec( "INSERT INTO graph_pdo_test VALUES ( '', 'Lynx', 431 )" );
+        $db->exec( "INSERT INTO graph_pdo_test VALUES ( '', 'wget', 912 )" );
+    }
+
+    protected function tearDown()
+    {
+        if ( !$this->hasFailed() )
+        {
+            $this->removeTempDir();
+        }
+
+        $db = ezcDbInstance::get();
+        $db->exec( 'DROP TABLE graph_pdo_test' );
+    }
+
+    public function testAutomaticDataSetUsage()
+    {
+        $db = ezcDbInstance::get();
+
+        $statement = $db->prepare( 'SELECT browser, hits FROM graph_pdo_test' 
);
+        $statement->execute();
+
+        $dataset = new ezcGraphPdoDataSet( $statement );
+
+        $dataSetArray = array(
+            'Firefox' => 2567,
+            'Opera' => 543,
+            'Safari' => 23,
+            'Konquror' => 812,
+            'Lynx' => 431,
+            'wget' => 912,
+        );
+
+        $count = 0;
+        foreach ( $dataset as $key => $value )
+        {
+            list( $compareKey, $compareValue ) = each( $dataSetArray );
+
+            $this->assertEquals(
+                $compareKey,
+                $key,
+                'Unexpected key for dataset value.'
+            );
+
+            $this->assertEquals(
+                $compareValue,
+                $value,
+                'Unexpected value for dataset.'
+            );
+
+            ++$count;
+        }
+
+        $this->assertEquals(
+            $count,
+            count( $dataSetArray ),
+            'Too few datasets found.'
+        );
+    }
+
+    public function testAutomaticDataSetUsageSingleColumn()
+    {
+        $db = ezcDbInstance::get();
+
+        $statement = $db->prepare( 'SELECT hits FROM graph_pdo_test' );
+        $statement->execute();
+
+        $dataset = new ezcGraphPdoDataSet( $statement );
+
+        $dataSetArray = array(
+            'Firefox' => 2567,
+            'Opera' => 543,
+            'Safari' => 23,
+            'Konquror' => 812,
+            'Lynx' => 431,
+            'wget' => 912,
+        );
+
+        $count = 0;
+        foreach ( $dataset as $key => $value )
+        {
+            list( $compareKey, $compareValue ) = each( $dataSetArray );
+
+            $this->assertEquals(
+                $count,
+                $key,
+                'Unexpected key for dataset value.'
+            );
+
+            $this->assertEquals(
+                $compareValue,
+                $value,
+                'Unexpected value for dataset.'
+            );
+
+            ++$count;
+        }
+
+        $this->assertEquals(
+            $count,
+            count( $dataSetArray ),
+            'Too few datasets found.'
+        );
+    }
+
+    public function testAutomaticDataSetUsageTooManyRows()
+    {
+        $db = ezcDbInstance::get();
+
+        $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
+        $statement->execute();
+
+        try
+        {
+            $dataset = new ezcGraphPdoDataSet( $statement );
+        }
+        catch ( ezcGraphPdoDataSetTooManyColumnsException $e )
+        {
+            return true;
+        }
+
+        $this->fail( 'Expected ezcGraphPdoDataSetTooManyColumnsException.' );
+    }
+
+    public function testSpecifiedDataSetUsage()
+    {
+        $db = ezcDbInstance::get();
+
+        $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
+        $statement->execute();
+
+        $dataset = new ezcGraphPdoDataSet(
+            $statement,
+            array(
+                ezcGraph::KEY => 'browser',
+                ezcGraph::VALUE => 'hits',
+            )
+        );
+
+        $dataSetArray = array(
+            'Firefox' => 2567,
+            'Opera' => 543,
+            'Safari' => 23,
+            'Konquror' => 812,
+            'Lynx' => 431,
+            'wget' => 912,
+        );
+
+        $count = 0;
+        foreach ( $dataset as $key => $value )
+        {
+            list( $compareKey, $compareValue ) = each( $dataSetArray );
+
+            $this->assertEquals(
+                $compareKey,
+                $key,
+                'Unexpected key for dataset value.'
+            );
+
+            $this->assertEquals(
+                $compareValue,
+                $value,
+                'Unexpected value for dataset.'
+            );
+
+            ++$count;
+        }
+
+        $this->assertEquals(
+            $count,
+            count( $dataSetArray ),
+            'Too few datasets found.'
+        );
+    }
+
+    public function testSpecifiedDataSetUsageSingleColumn()
+    {
+        $db = ezcDbInstance::get();
+
+        $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
+        $statement->execute();
+
+        $dataset = new ezcGraphPdoDataSet(
+            $statement,
+            array(
+                ezcGraph::VALUE => 'hits',
+            )
+        );
+
+        $dataSetArray = array(
+            'Firefox' => 2567,
+            'Opera' => 543,
+            'Safari' => 23,
+            'Konquror' => 812,
+            'Lynx' => 431,
+            'wget' => 912,
+        );
+
+        $count = 0;
+        foreach ( $dataset as $key => $value )
+        {
+            list( $compareKey, $compareValue ) = each( $dataSetArray );
+
+            $this->assertEquals(
+                $count,
+                $key,
+                'Unexpected key for dataset value.'
+            );
+
+            $this->assertEquals(
+                $compareValue,
+                $value,
+                'Unexpected value for dataset.'
+            );
+
+            ++$count;
+        }
+
+        $this->assertEquals(
+            $count,
+            count( $dataSetArray ),
+            'Too few datasets found.'
+        );
+    }
+
+    public function testSpecifiedDataSetUsageBrokenKey()
+    {
+        $db = ezcDbInstance::get();
+
+        $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
+        $statement->execute();
+
+        try
+        {
+            $dataset = new ezcGraphPdoDataSet(
+                $statement,
+                array(
+                    ezcGraph::KEY => 'nonexistant',
+                    ezcGraph::VALUE => 'hits',
+                )
+            );
+        }
+        catch ( ezcGraphPdoDataSetMissingColumnException $e )
+        {
+            return true;
+        }
+
+        $this->fail( 'Expected ezcGraphPdoDataSetMissingColumnException.' );
+    }
+
+    public function testSpecifiedDataSetUsageBrokenValue()
+    {
+        $db = ezcDbInstance::get();
+
+        $statement = $db->prepare( 'SELECT * FROM graph_pdo_test' );
+        $statement->execute();
+
+        try
+        {
+            $dataset = new ezcGraphPdoDataSet(
+                $statement,
+                array(
+                    ezcGraph::VALUE => 'nonexistant',
+                )
+            );
+        }
+        catch ( ezcGraphPdoDataSetMissingColumnException $e )
+        {
+            return true;
+        }
+
+        $this->fail( 'Expected ezcGraphPdoDataSetMissingColumnException.' );
+    }
+
+    public function testNonExceutedQuery()
+    {
+        $db = ezcDbInstance::get();
+
+        $statement = $db->prepare( 'SELECT browser, hits FROM graph_pdo_test' 
);
+
+        try
+        {
+            $dataset = new ezcGraphPdoDataSet( $statement );
+        }
+        catch ( ezcGraphPdoDataSetStatementNotExecutedException $e )
+        {
+            return true;
+        }
+        
+        $this->fail( 'Expected 
ezcGraphPdoDataSetStatementNotExecutedException.' );
+    }
+}
+
+?>


Property changes on: trunk/Graph/tests/dataset_pdo_test.php
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: trunk/Graph/tests/suite.php
===================================================================
--- trunk/Graph/tests/suite.php 2007-02-15 10:15:14 UTC (rev 4653)
+++ trunk/Graph/tests/suite.php 2007-02-15 10:19:58 UTC (rev 4654)
@@ -19,6 +19,7 @@
 require_once 'dataset_test.php';
 require_once 'dataset_average_test.php';
 require_once 'dataset_numeric_test.php';
+require_once 'dataset_pdo_test.php';
 require_once 'element_options_test.php';
 require_once 'legend_test.php';
 require_once 'background_test.php';
@@ -64,6 +65,7 @@
         $this->addTest( ezcGraphDataSetTest::suite() );
         $this->addTest( ezcGraphDataSetAverageTest::suite() );
         $this->addTest( ezcGraphNumericDataSetTest::suite() );
+        $this->addTest( ezcGraphPdoDataSetTest::suite() );
         $this->addTest( ezcGraphElementOptionsTest::suite() );
         $this->addTest( ezcGraphLegendTest::suite() );
         $this->addTest( ezcGraphBackgroundTest::suite() );

-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to