Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 5.90 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. // candidate to extend Kohana_Database_Result
  3. // statement wrapper
  4. /*
  5. The following methods are inherited:
  6. cached()
  7. get()
  8. count()
  9. offsetExists()
  10. offsetGet()
  11. offsetSet()
  12. offsetUnset()
  13. key()
  14. */
  15. class Musterdb_Database_Result_PDO extends Database_Result
  16. {
  17.         protected $_sql;
  18.         protected $_statement;
  19.         protected $_cursor_scroll; // Boolean
  20.         protected $_emulate_cursor; // Boolean
  21.         protected $_fetch_style; // pdo constant
  22.         protected $_parameters; // save for rewind
  23.         // currency
  24.         protected $_total_rows  = 0;
  25.         protected $_current_row = 0;
  26. //      protected $_internal_row = -1;
  27.         protected $_BOF = TRUE;
  28.         protected $_row_data; // current row data
  29.        
  30.         public function __construct($statement, $sql, $as_object, $config = array())
  31.         {
  32.                 $config = $config + array(
  33.                         'cursorscroll' => FALSE,
  34.                         'emulatecursor' => FALSE,
  35.                 );
  36.                 $this->_cursor_scroll = $config['cursorscroll'];
  37.                 $this->_emulate_cursor = $config['emulatecursor'];
  38.                 // Store the statement locally
  39.                 $this->_statement = $this->_result = $statement; // _result = Kohana legacy
  40.  
  41.                 // Store the SQL locally
  42.                 $this->_sql = $this->_query = $sql; // _query = Kohana legacy
  43.  
  44.                 if (is_object($as_object))
  45.                 {
  46.                         // Get the object class name
  47.                         $as_object = get_class($as_object);
  48.                 }
  49.  
  50.                 // Results as objects or associative arrays
  51.                 $this->_as_object = $as_object;
  52.                
  53.                 $this->_total_rows = $statement->rowCount();
  54.                
  55.                 $this->_fetch_style = ($this->_as_object === FALSE)? PDO::FETCH_ASSOC : PDO::FETCH_CLASS;
  56.                
  57.         }
  58.         public function __destruct()
  59.         {
  60.                 $this->_statement = NULL;
  61.         }
  62.         // note that rewind not called at end.
  63.         public function as_array($key = NULL, $value = NULL)
  64.         {
  65.                 $rows = $this->_statement->fetchAll($this->_fetch_style);
  66.                 $results = array();
  67.  
  68.                 if ($key === NULL AND $value === NULL)
  69.                 {
  70.                         // Indexed rows
  71.                         $results = $rows;
  72.  
  73.                 }
  74.                 elseif ($key === NULL)
  75.                 {
  76.                         // Indexed columns
  77.  
  78.                         if ($this->_as_object)
  79.                         {
  80.                                 foreach ($rows as $row)
  81.                                 {
  82.                                         $results[] = $row->$value;
  83.                                 }
  84.                         }
  85.                         else
  86.                         {
  87.                                 foreach ($rows as $row)
  88.                                 {
  89.                                         $results[] = $row[$value];
  90.                                 }
  91.                         }
  92.                 }
  93.                 elseif ($value === NULL)
  94.                 {
  95.                         // Associative rows
  96.  
  97.                         if ($this->_as_object)
  98.                         {
  99.                                 foreach ($rows as $row)
  100.                                 {
  101.                                         $results[$row->$key] = $row;
  102.                                 }
  103.                         }
  104.                         else
  105.                         {
  106.                                 foreach ($rows as $row)
  107.                                 {
  108.                                         $results[$row[$key]] = $row;
  109.                                 }
  110.                         }
  111.                 }
  112.                 else
  113.                 {
  114.                         // Associative columns
  115.  
  116.                         if ($this->_as_object)
  117.                         {
  118.                                 foreach ($rows as $row)
  119.                                 {
  120.                                         $results[$row->$key] = $row->$value;
  121.                                 }
  122.                         }
  123.                         else
  124.                         {
  125.                                 foreach ($rows as $row)
  126.                                 {
  127.                                         $results[$row[$key]] = $row[$value];
  128.                                 }
  129.                         }
  130.                 }
  131.                 if ($this->_cursor_scroll or $this->_emulate_cursor)
  132.                         $this->rewind();
  133.  
  134.                 return $results;
  135.         }
  136.         public function seek($offset)
  137.         {
  138.                 if (!$this->offsetExists($offset))
  139.                         return FALSE;
  140.                 if ($this->_cursor_scroll)
  141.                 {
  142.                         if ($this->_BOF) $this_BOF = FALSE;
  143.                         $this->_current_row = $offet; // $this->_internal_row = $offset;
  144.                         return TRUE;
  145.                 }
  146.                 elseif ($offset < $this->_current_row)
  147.                 {
  148.                         if ($this->_emulate_cursor)
  149.                         {
  150.                                 $this->rewind();
  151.                         }
  152.                         else
  153.                                 throw new Kohana_Exception('Database statement is forward moving only, cannot seek backwards');
  154.                 }
  155.                 // increment next to offset
  156.                 while ($this->_current_row < $offset)
  157.                         $this->next();
  158.                 return TRUE;
  159.         }
  160.         public function current()
  161.         {
  162.                 if ($this->_cursor_scroll)
  163.                 {
  164.                         return $this->_statement->fetch($this->_fetch_style,PDO::FETCH_ORI_ABS,$this->_current_row);
  165.                 }
  166.                 else
  167.                 {
  168.                         if ($this->_BOF) // get first
  169.                                 $this->next();
  170.                         return $this->_row_data;
  171.                 }
  172.         }
  173.         public function next()
  174.         {
  175.                 if ($this->_BOF)
  176.                         $this->_BOF = FALSE;
  177.                 else
  178.                 {
  179.                         $this->_current_row++;
  180.                 }
  181.                 if (!$this->_cursor_scroll)
  182.                 {
  183.                         if ($this->_current_row < $this->_total_rows)
  184.                         {
  185.                                 $this->_row_data = $this->_statement->fetch($this->_fetch_style,PDO::FETCH_ORI_NEXT);
  186.                         }
  187.                         else
  188.                         {
  189.                                 $this->_row_data = NULL;
  190.                         }
  191.                 }
  192.                 return $this;
  193.         }
  194.         public function prev()
  195.         {
  196.                 if ($this->_BOF) return $this;
  197.                 if ($this->_cursor_scroll)
  198.                 {
  199.                         if ($this->_current_row == 0)
  200.                                 $this->_BOF = TRUE;
  201.                         elseif ($this->_current_row > 0)
  202.                                 $this->_current_row--;
  203.                 }
  204.                 elseif ($this->_emulate_cursor)
  205.                 {
  206.                         if ($this->_current_row == 0)
  207.                                 $this->_BOF = TRUE;
  208.                         else
  209.                                 $this->seek($this->_current_row - 1);
  210.                 }
  211.                 else
  212.                 {
  213.                         throw new Kohana_Exception('Database statement is forward moving only, cannot go to previous');
  214.                 }
  215.                 return $this;
  216.         }
  217.         // fetch next
  218.         public function fetch()
  219.         {
  220.                 return $this->next()->current();
  221.         }
  222.         public function rewind()
  223.         {
  224.                 if ($this->_BOF) return $this;
  225.                 $this->_BOF = TRUE;
  226.                 $this->_current_row = 0;
  227.                 if ($this->_emulate_cursor)
  228.                 {
  229.                         $this->_statement->closeCursor();
  230.                         if (empty($this->_parameters))
  231.                                 $this->_statement->execute();
  232.                         else
  233.                                 $this->_statement->execute($this->_parameters);
  234.                 }
  235.                 else
  236.                         throw new Kohana_Exception('Database statement is forward moving only, cannot rewind');
  237.                 return $this;
  238.         }
  239.         public function execute($parameters = array()) // for prepare
  240.         {
  241.                 $statement = $this->_statement;
  242.                 if (empty($parameters))
  243.                         $statement->execute();
  244.                 else
  245.                         $statement->execute($parameters);
  246.                 $this->_total_rows = $statement->rowCount();
  247.                 $this->_parameters = $parameters;
  248.                 return $this;
  249.         }
  250.         public function errorCode()
  251.         {
  252.                 return $this->_statement->errorCode(); // SQLSTATE
  253.         }
  254.         public function errorInfo()
  255.         {
  256.                 return $this->_statement->errorInfo(); // array(0=>SQLSTATE,1=>drivererrorcode,2=>errormessage)
  257.         }
  258.         public function statement()
  259.         {
  260.                 return $this->_statement;
  261.         }
  262.         public function sql()
  263.         {
  264.                 return $this->_sql;
  265.         }
  266.         public function valid()
  267.         {
  268.                 return (boolean) (($this->_current_row >= 0) and ($this->_current_row < $this->_total_rows));
  269.         }
  270.         public function row_count()
  271.         {
  272.                 return $this->_total_rows;
  273.         }
  274.  
  275. }