dbscript
[ class tree: dbscript ] [ index: dbscript ] [ all elements ]

Source for file recordset.php

Documentation is available at recordset.php

  1. <?php
  2.  
  3.   /** 
  4.    * dbscript for PHP 4 & 5 - restful crud framework
  5.    * @version 0.1.2 -- 19-Feb-2007
  6.    * @author Brian Hendrickson <brian@dbscript.net>
  7.    * @link http://dbscript.net/
  8.    * @copyright Copyright 2007 Brian Hendrickson
  9.    * @package dbscript
  10.    * @license http://www.opensource.org/licenses/mit-license.php MIT License
  11.    */
  12.  
  13.   /**
  14.    * Record Set
  15.    * 
  16.    * RecordSets are objects comprised of a join query result resource
  17.    * and a lazy-loading iterator for each table in the result.
  18.    * 
  19.    * Usage:
  20.    * <code>
  21.    * $rs = $db->get_recordset( $people->get_query );
  22.    *
  23.    * while ( $Person = $rs->MoveNext() )
  24.    *   print $Person->name;
  25.    * </code>
  26.    * 
  27.    * More info...
  28.    * {@link http://dbscript.net/recordset}
  29.    * 
  30.    * @package dbscript
  31.    * @author Brian Hendrickson <brian@dbscript.net>
  32.    * @access public
  33.    * @version 0.1.2
  34.    */
  35.  
  36. class RecordSet {
  37.   
  38.   var $query;
  39.   var $table;
  40.   var $result;
  41.   var $rowcount;
  42.   var $fieldlist;
  43.   var $tablelist;
  44.   var $rowmap;
  45.   var $iterator;
  46.   var $activerow;
  47.   var $relations;
  48.   
  49.   function RecordSet$sql {
  50.  
  51.     $db =db_object();
  52.  
  53.     $this->query = $sql;
  54.     $this->result = $db->get_result($sqltrue);
  55.     $this->rowcount = $db->num_rows($this->result);
  56.     $this->fieldlist = array();
  57.     $this->tablelist = array();
  58.  
  59.     // get table and field names from result column headers
  60.  
  61.     for $i 0$i $db->num_fields$this->result )$i++ {
  62.       $col split"\."$db->field_name$this->result$i ) );
  63.       if count$col == && $col[0&& $col[1{
  64.         $this->fieldlist[$col[0]][$col[1]] $i;
  65.         if ($col[1== $db->models[$col[0]]->primary_key{
  66.           $this->tablelist[$col[0]] $i// pk offset
  67.         }
  68.         if ($i == 0$this->table = $col[0];
  69.       else {
  70.         trigger_error'Malformed SQORP query "'.$db->field_name$this->result$i ).'". Example: select people.id as "people.id".'E_USER_ERROR );
  71.       }
  72.     }
  73.  
  74.     $this->rowmap = array();
  75.     $this->relations = array();
  76.     
  77.     // read the primary key value(s) in each row and map them to the result row number
  78.  
  79.     for $i 0$i $db->num_rows$this->result )$i++ {
  80.       foreach $this->tablelist as $table => $pkoffset {
  81.         $pkvalue $db->result_value$this->result$i$pkoffset );
  82.         if $pkvalue {
  83.           $this->rowmap[$table][$pkvalue$i;
  84.           if !$table == $this->table ) ) {
  85.             $this->relations[$db->result_value$this->result$i$this->tablelist[$this->table)][$table][$pkvalue;
  86.           }
  87.         }
  88.       }
  89.     }
  90.     
  91.     $this->iterator = array();
  92.     $this->activerow = array();
  93.  
  94.   }
  95.   
  96.   function MoveFirst$table {
  97.     if array_key_exists$table$this->fieldlist )) {
  98.       if !isset$this->iterator[$table))) {
  99.         $this->iterator[$tablenew ResultIterator$this$table );
  100.       }
  101.       return $this->iterator[$table]->MoveFirst();
  102.     else {
  103.       return false;
  104.     }
  105.   }
  106.   
  107.   function MoveNext$table NULL {
  108.     if ($table === NULL{
  109.       $keys array_keys$this->fieldlist );
  110.       $table $keys[0];
  111.     }
  112.     if array_key_exists$table$this->fieldlist )) {
  113.       if !isset$this->iterator[$table))) {
  114.         $this->iterator[$tablenew ResultIterator$this$table );
  115.       }
  116.       return $this->iterator[$table]->MoveNext();
  117.     else {
  118.       return false;
  119.     }
  120.   }
  121.   
  122.   function FirstChild$parent_pkval$table {
  123.     if array_key_exists$table$this->fieldlist )) {
  124.       if !isset$this->iterator[$table))) {
  125.         $this->iterator[$tablenew ResultIterator$this$table );
  126.       }
  127.       return $this->iterator[$table]->FirstChild$parent_pkval );
  128.     else {
  129.       return false;
  130.     }
  131.   }
  132.  
  133.   function NextChild$parent_pkval$table {
  134.     if array_key_exists$table$this->fieldlist )) {
  135.       if !isset$this->iterator[$table))) {
  136.         $this->iterator[$tablenew ResultIterator$this$table );
  137.       }
  138.       return $this->iterator[$table]->NextChild$parent_pkval );
  139.     else {
  140.       return false;
  141.     }
  142.   }
  143.   
  144.   function Load$table$row {
  145.     $db =db_object();
  146.     trigger_before'Load'$db$this )
  147.     if !$row $this->rowcount )) return false;
  148.     if array_key_exists$table$this->fieldlist )) {
  149.       $this->activerow[$table$db->fetch_array$this->result$row );
  150.       foreach $this->fieldlist[$tableas $field => $idx {
  151.         $this->fieldlist[$table][$field=$this->activerow[$table][$table.".".$field];
  152.       }
  153.       trigger_after'Load'$db$this )
  154.       return $db->iterator_load_record$table$this->fieldlist[$table]$this );
  155.     else {
  156.       return false;
  157.     }
  158.   }
  159.   
  160.   function rewind({
  161.     $table $this->table;
  162.     $row 0;
  163.     if array_key_exists$table$this->fieldlist )) {
  164.       if !isset$this->iterator[$table))) {
  165.         $this->iterator[$tablenew ResultIterator$this$table );
  166.       }
  167.       return $this->iterator[$table]->seek$row );
  168.     else {
  169.       return false;
  170.     }
  171.   }
  172.   
  173.   function num_rows$table {
  174.     if (isset($this->rowmap[$table]))
  175.       return count($this->rowmap[$table]);
  176.     return 0;
  177.   }
  178.  
  179. }
  180.  
  181. ?>

Documentation generated on Mon, 19 Feb 2007 10:25:02 -0800 by phpDocumentor 1.3.1