[ Index ]
 

Code source de Dotclear 2.0-beta6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/inc/clearbricks/dblayer/ -> dblayer.php (source)

   1  <?php
   2  # ***** BEGIN LICENSE BLOCK *****
   3  # This file is part of Clearbricks.
   4  # Copyright (c) 2006 Olivier Meunier and contributors. All rights
   5  # reserved.
   6  #
   7  # Clearbricks is free software; you can redistribute it and/or modify
   8  # it under the terms of the GNU General Public License as published by
   9  # the Free Software Foundation; either version 2 of the License, or
  10  # (at your option) any later version.
  11  # 
  12  # Clearbricks is distributed in the hope that it will be useful,
  13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  # GNU General Public License for more details.
  16  # 
  17  # You should have received a copy of the GNU General Public License
  18  # along with Clearbricks; if not, write to the Free Software
  19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20  #
  21  # ***** END LICENSE BLOCK *****
  22  
  23  /// @defgroup CB_DBLAYER Clearbricks Database Abstraction Layer
  24  /// @ingroup CLEARBRICKS
  25  
  26  require dirname(__FILE__).'/class.cursor.php';
  27  
  28  /**
  29  @ingroup CB_DBLAYER
  30  @brief Clearbricks Database Abstraction Layer interface
  31  
  32  All methods in this interface should be implemented in your database driver.
  33  
  34  Database driver is a class that extends dbLayer, implements i_dbLayer and has
  35  a name of the form (driver name)Connection.
  36  */
  37  interface i_dbLayer
  38  {
  39      /**
  40      This method should open a database connection and return a new resource
  41      link.
  42      
  43      @param    host        <b>string</b>        Database server host
  44      @param    user        <b>string</b>        Database user name
  45      @param    password    <b>string</b>        Database password
  46      @param    database    <b>string</b>        Database name
  47      @returns    <b>resource</b>
  48      */
  49  	function db_connect($host,$user,$password,$database);
  50      
  51      /**
  52      This method should open a persistent database connection and return a new
  53      resource link.
  54      
  55      @param    host        <b>string</b>        Database server host
  56      @param    user        <b>string</b>        Database user name
  57      @param    password    <b>string</b>        Database password
  58      @param    database    <b>string</b>        Database name
  59      @returns    <b>resource</b>
  60      */
  61  	function db_pconnect($host,$user,$password,$database);
  62      
  63      /**
  64      This method should close resource link.
  65      
  66      @param    handle    <b>resource</b>    Resource link
  67      */
  68  	function db_close($handle);
  69      
  70      /**
  71      This method should return database version number.
  72      
  73      @param    handle    <b>resource</b>    Resource link
  74      @returns    <b>string</b>
  75      */
  76  	function db_version($handle);
  77      
  78      /**
  79      This method should run an SQL query and return a resource result.
  80      
  81      @param    handle    <b>resource</b>    Resource link
  82      @param    query    <b>string</b>        SQL query string
  83      @return    <b>resource</b>
  84      */
  85  	function db_query($handle,$query);
  86      
  87      /**
  88      This method should run an SQL query and return a resource result.
  89      
  90      @param    handle    <b>resource</b>    Resource link
  91      @param    query    <b>string</b>        SQL query string
  92      @return    <b>resource</b>
  93      */
  94  	function db_exec($handle,$query);
  95      
  96      /**
  97      This method should return the number of fields in a result.
  98      
  99      @param    res        <b>resource</b>    Resource result
 100      @return    <b>integer</b>
 101      */
 102  	function db_num_fields($res);
 103      
 104      /**
 105      This method should return the number of rows in a result.
 106      
 107      @param    res        <b>resource</b>    Resource result
 108      @return    <b>integer</b>
 109      */
 110  	function db_num_rows($res);
 111      
 112      /**
 113      This method should return the name of the field at the given position
 114      <var>$position</var>.
 115      
 116      @param    res        <b>resource</b>    Resource result
 117      @param    position    <b>integer</b>        Field position
 118      @return    <b>string</b>
 119      */
 120  	function db_field_name($res,$position);
 121      
 122      /**
 123      This method should return the field type a the given position
 124      <var>$position</var>.
 125      
 126      @param    res        <b>resource</b>    Resource result
 127      @param    position    <b>integer</b>        Field position
 128      @return    <b>string</b>
 129      */
 130  	function db_field_type($res,$position);
 131      
 132      /**
 133      This method should fetch one line of result and return an associative array
 134      with field name as key and field value as value.
 135      
 136      @param    res        <b>resource</b>    Resource result
 137      @return    <b>array</b>
 138      */
 139  	function db_fetch_assoc($res);
 140      
 141      /**
 142      This method should move result cursor on given row position <var>$row</var>
 143      and return true on success.
 144      
 145      @param    res        <b>resource</b>    Resource result
 146      @param    row        <b>integer</b>        Row position
 147      @return    <b>boolean</b>
 148      */
 149  	function db_result_seek($res,$row);
 150      
 151      /**
 152      This method should return number of rows affected by INSERT, UPDATE or
 153      DELETE queries.
 154      
 155      @param    handle    <b>resource</b>    Resource link
 156      @param    res        <b>resource</b>    Resource result
 157      @return    <b>integer</b>
 158      */
 159  	function db_changes($handle,$res);
 160      
 161      /**
 162      This method should return an array of all tables in database for the current
 163      connection.
 164      
 165      @param    handle    <b>resource</b>    Resource link
 166      @return    <b>array</b>
 167      */
 168  	function db_get_tables($handle);
 169      
 170      /**
 171      This method should return an associative array of columns in given table
 172      <var>$table</var> with column names in keys an types in values.
 173      
 174      @param    handle    <b>resource</b>    Resource link
 175      @param    table    <b>string</b>        Table name
 176      @return    <b>array</b>
 177      */
 178  	function db_get_columns($handle,$table);
 179      
 180      /**
 181      This method should return the last error string for the current connection.
 182      
 183      @param    handle    <b>resource</b>    Resource link
 184      @return    <b>string</b>
 185      */
 186  	function db_last_error($handle);
 187      
 188      /**
 189      This method should return an escaped string for the current connection.
 190      
 191      @param    str        <b>string</b>        String to escape
 192      @param    handle    <b>resource</b>    Resource link
 193      @return    <b>string</b>
 194      */
 195  	function db_escape_string($str,$handle=null);
 196  }
 197  
 198  /**
 199  @ingroup CB_DBLAYER
 200  @brief Database Abstraction Layer class
 201  
 202  Base class for database abstraction. Each driver extends this class and
 203  implements i_dbLayer interface.
 204  */
 205  class dbLayer
 206  {
 207      protected $__driver = null;        ///< <b>string</b>        Driver name
 208      protected $__version = null;        ///< <b>string</b>        Database version
 209      
 210      protected $__link;                ///< <b>resource</b>    Database resource link
 211      protected $__last_result;        ///< <b>resource</b>    Last result resource
 212      
 213      /**
 214      Static function to use to init database layer. Returns a object extending
 215      dbLayer.
 216      
 217      @param    driver        <b>string</b>        Driver name
 218      @param    host            <b>string</b>        Database hostname
 219      @param    database        <b>string</b>        Database name
 220      @param    user            <b>string</b>        User ID
 221      @param    password        <b>string</b>        Password
 222      @param    persistent    <b>boolean</b>        Persistent connection (false)
 223      @return    <b>object</b>
 224      */
 225  	public static function init($driver,$host,$database,$user='',$password='',$persistent=false)
 226      {
 227          if (file_exists(dirname(__FILE__).'/class.'.$driver.'.php')) {
 228              require dirname(__FILE__).'/class.'.$driver.'.php';
 229              $driver_class = $driver.'Connection';
 230          } else {
 231              trigger_error('Unable to load DB layer for '.$driver,E_USER_ERROR);
 232              exit(1);
 233          }
 234          
 235          return new $driver_class($host,$database,$user,$password,$persistent);
 236      }
 237      
 238      /**
 239      Inits database connection.
 240      
 241      @param    host            <b>string</b>        User ID
 242      @param    database        <b>string</b>        Password
 243      @param    user            <b>string</b>        Server to connect
 244      @param    password        <b>string</b>        Database name
 245      @param    persistent    <b>boolean</b>        Open a persistent connection
 246      */
 247  	public function __construct($host,$database,$user='',$password='',$persistent=false)
 248      {
 249          if ($persistent) {
 250              $this->__link = $this->db_pconnect($host,$user,$password,$database);
 251          } else {
 252              $this->__link = $this->db_connect($host,$user,$password,$database);
 253          }
 254          
 255          $this->__version = $this->db_version($this->__link);
 256      }
 257      
 258      /**
 259      Closes database connection.
 260      */
 261  	public function close()
 262      {
 263          $this->db_close($this->__link);
 264      }
 265      
 266      /**
 267      Returns database driver name
 268      
 269      @return    <b>string</b>
 270      */
 271  	public function driver()
 272      {
 273          return $this->__driver;
 274      }
 275      
 276      /**
 277      Returns database driver version
 278      
 279      @return    <b>string</b>
 280      */
 281  	public function version()
 282      {
 283          return $this->__version;
 284      }
 285      
 286      /**
 287      Returns link resource
 288      
 289      @return    <b>resource</b>
 290      */
 291  	public function link()
 292      {
 293          return $this->__link;
 294      }
 295      
 296      /**
 297      Executes a query and return a recordset. Recordset could be either static
 298      (default beahvior) or dynamic (useful for large results).
 299      $query could be a string or an array of params for a previously prepared
 300      query.
 301      
 302      @param    sql        <b>string</b>        SQL query
 303      @return    <b>record</b>
 304      */
 305  	public function select($sql)
 306      {
 307          $result = $this->db_query($this->__link,$sql);
 308          
 309          $this->__last_result =& $result;
 310          
 311          $info = array();
 312          $info['con'] =& $this;
 313          $info['cols'] = $this->db_num_fields($result);
 314          $info['rows'] = $this->db_num_rows($result);
 315          $info['info'] = array();
 316          
 317          for ($i=0; $i<$info['cols']; $i++) {
 318              $info['info']['name'][] = $this->db_field_name($result,$i);
 319              $info['info']['type'][] = $this->db_field_type($result,$i);
 320          }
 321          
 322          return new record($result,$info);
 323      }
 324      
 325      /**
 326      Executes a query and return true if query succeded.
 327      
 328      @param    sql        <b>string</b>        SQL query
 329      @return    <b>boolean</b>
 330      */
 331  	public function execute($sql)
 332      {
 333          $result = $this->db_exec($this->__link,$sql);
 334          
 335          $this->__last_result =& $result;
 336          
 337          return true;
 338      }
 339      
 340      /**
 341      Begins a transaction.
 342      */
 343  	public function begin()
 344      {
 345          $this->execute('BEGIN');
 346      }
 347      
 348      /**
 349      Commits a transaction.
 350      */
 351  	public function commit()
 352      {
 353          $this->execute('COMMIT');
 354      }
 355      
 356      /**
 357      Rollbacks a transaction.
 358      */
 359  	public function rollback()
 360      {
 361          $this->execute('ROLLBACK');
 362      }
 363      
 364      /**
 365      Vacuum the table given in argument.
 366      
 367      @param    table    <b>string</b>        Table name
 368      */
 369  	public function vacuum($table)
 370      {
 371      }
 372      
 373      /**
 374      Returns an array of all table names.
 375      
 376      @return    <b>array</b>
 377      */
 378  	public function getTables()
 379      {
 380          return $this->db_get_tables($this->__link);
 381      }
 382      
 383      /**
 384      Return an array of columns (name and type) of a given table.
 385      
 386      @param    table    <b>string</b>        Table name
 387      @return    <b>array</b>
 388      */
 389  	public function getColumns($table)
 390      {
 391          return $this->db_get_columns($this->__link,$table);
 392      }
 393      
 394      /**
 395      Returns the number of lines affected by the last DELETE, INSERT or UPDATE
 396      query.
 397      
 398      @return    <b>integer</b>
 399      */
 400  	public function changes()
 401      {
 402          return $this->db_changes($this->__link,$this->__last_result);
 403      }
 404      
 405      /**
 406      Returns the last database error or false if no error.
 407      
 408      @returns <b>string</b>
 409      */
 410  	public function error()
 411      {
 412          $err = $this->db_last_error($this->__link);
 413          
 414          if (!$err) {
 415              return false;
 416          }
 417          
 418          return $err;
 419      }
 420  
 421      /**
 422      Returns a query fragment with date formater.
 423      
 424      The following modifiers are accepted:
 425      
 426      - %d : Day of the month, numeric
 427      - %H : Hour 24 (00..23)
 428      - %M : Minute (00..59)
 429      - %m : Month numeric (01..12)
 430      - %S : Seconds (00..59)
 431      - %Y : Year, numeric, four digits
 432      
 433      @param    field    <b>string</b>        Field name
 434      @param    pattern    <b>string</b>        Date format
 435      @return    <b>string</b>
 436      */
 437  	public function dateFormat($field,$pattern)
 438      {
 439          return
 440          'TO_CHAR('.$field.','."'".$this->escape($pattern)."') ";
 441      }
 442      
 443      /**
 444      Returns a LIMIT query fragment.
 445      
 446      @param    arg1        <b>mixed</b>        array or integer with limit intervals
 447      @param    arg2        <b>mixed</b>        integer or null (null)
 448      @return    <b>string</b>
 449      */
 450  	public function limit($arg1, $arg2=null)
 451      {
 452          if (is_array($arg1))
 453          {
 454              $arg1 = array_values($arg1);
 455              $arg2 = isset($arg1[1]) ? $arg1[1] : null;
 456              $arg1 = $arg1[0];
 457          }
 458          
 459          if ($arg2 === null) {
 460              $sql = ' LIMIT '.(integer) $arg1.' ';
 461          } else {
 462              $sql = ' LIMIT '.(integer) $arg2.' OFFSET '.$arg1.' ';
 463          }
 464          
 465          return $sql;
 466      }
 467      
 468      /**
 469      Returns a IN query fragment where $in could be an array, a string,
 470      an integer or null
 471      
 472      @param    in        <b>mixed</b>        array, string, integer or null
 473      @return    <b>string</b>
 474      */
 475      public function in($in)
 476      {
 477          if (is_null($in))
 478          {
 479              return ' IN (NULL) ';
 480          }
 481          elseif (is_string($in))
 482          {
 483              return " IN ('".$this->escape($in)."') ";
 484          }
 485          elseif (is_array($in))
 486          {
 487              foreach ($in as $i => $v) {
 488                  if (is_null($v)) {
 489                      $in[$i] = 'NULL';
 490                  } elseif (is_string($v)) {
 491                      $in[$i] = "'".$this->escape($v)."'";
 492                  }
 493              }
 494              return ' IN ('.implode(',',$in).') ';
 495          }
 496          else
 497          {
 498              return ' IN ( '.(integer) $in.') ';
 499          }
 500      }
 501      
 502      /**
 503      Returns SQL concatenation of methods arguments. Theses arguments
 504      should be properly escaped when needed.
 505      
 506      @return    <b>string</b>
 507      */
 508  	public function concat()
 509      {
 510          $args = func_get_args();
 511          return implode(' || ',$args);
 512      }
 513      
 514      /**
 515      Returns SQL protected string or array values.
 516      
 517      @param    i        <b>mixed</b>        String or array to protect
 518      @return    <b>mixed</b>
 519      */
 520  	public function escape($i)
 521      {
 522          if (is_array($i)) {
 523              foreach ($i as $k => $s) {
 524                  $i[$k] = $this->db_escape_string($s,$this->__link);
 525              }
 526              return $i;
 527          }
 528          
 529          return $this->db_escape_string($i,$this->__link);
 530      }
 531      
 532      /**
 533      Returns SQL system protected string.
 534      
 535      @param    str        <b>string</b>        String to protect
 536      @return    <b>string</b>
 537      */
 538  	public function escapeSystem($str)
 539      {
 540          return '"'.$str.'"';
 541      }
 542      
 543      /**
 544      Returns a new instance of cursor class on <var>$table</var> for the current
 545      connection.
 546      
 547      @param    table    <b>string</b>        Cursor table
 548      @return    <b>cursor</b>
 549      */
 550  	public function openCursor($table)
 551      {
 552          return new cursor($this,$table);
 553      }
 554  }
 555  
 556  /**
 557  @ingroup CB_DBLAYER
 558  @brief Query Result Record Class
 559  
 560  This class acts as an iterator over database query result. It does not fetch
 561  all results on instantiation and thus, depending on database engine, should not
 562  fill PHP process memory.
 563  */
 564  class record
 565  {
 566      protected $__link;                ///< <b>resource</b>    Database resource link
 567      protected $__result;            ///< <b>resource</b>    Query result resource
 568      protected $__info;                ///< <b>array</b>        Result information array
 569      protected $__extend = array();    ///< <b>array</b>        List of static functions that extend record
 570      
 571      protected $__index = 0;            ///< <b>integer</b>        Current result position
 572      protected $__row = false;        ///< <b>array</b>        Current result row content
 573      private $__fetch = false;
 574      
 575      /**
 576      Creates class instance from result link and some informations.
 577      <var>$info</var> is an array with the following content:
 578      
 579      - con => database object instance
 580      - cols => number of columns
 581      - rows => number of rows
 582      - info
 583        - name => an array with columns names
 584        - type => an array with columns types
 585      
 586      @param    result    <b>resource</b>    Resource result
 587      @param    info        <b>array</b>        Information array
 588      */
 589  	public function __construct($result,$info)
 590      {
 591          $this->__result = $result;
 592          $this->__info = $info;
 593          $this->__link = $info['con']->link();
 594          $this->index(0);
 595      }
 596      
 597      /**
 598      Converts this record to a staticRecord instance.
 599      */
 600  	public function toStatic()
 601      {
 602          return new staticRecord($this->__result,$this->__info);
 603      }
 604      
 605      /**
 606      Magic call function. Calls function in $__extend array if exists, passing it
 607      self object and arguments.
 608      */
 609  	public function __call($f,$args)
 610      {
 611          if (isset($this->__extend[$f]))
 612          {
 613              array_unshift($args,$this);
 614              return call_user_func_array($this->__extend[$f],$args);
 615          }
 616          
 617          trigger_error('Call to undefined method record::'.$f.'()',E_USER_ERROR);
 618      }
 619      
 620      /**
 621      Magic get method. Alias for field().
 622      */
 623  	public function __get($n)
 624      {
 625          return $this->field($n);
 626      }
 627      
 628      /**
 629      Alias for field().
 630      */
 631      public function f($n)
 632      {
 633          return $this->field($n);
 634      }
 635      
 636      /**
 637      Retrieve named <var>$n</var> field value.
 638      
 639      @param    n        <b>string</b>        Field name
 640      @return    <b>string</b>
 641      */
 642  	public function field($n)
 643      {
 644          return $this->__row[$n];
 645      }
 646      
 647      /**
 648      Returns true if a field exists.
 649      
 650      @param    n        <b>string</b>        Field name
 651      @return    <b>string</b>
 652      */
 653  	public function exists($n)
 654      {
 655          return isset($this->__row[$n]);
 656      }
 657      
 658      /**
 659      Extends this instance capabilities by adding all public static methods of
 660      <var>$class</var> to current instance. Class methods should take at least
 661      this record as first parameter.
 662      @see __call()
 663      
 664      @param    class    <b>string</b>        Class name
 665      */
 666  	public function extend($class)
 667      {
 668          if (!class_exists($class)) {
 669              return;
 670          }
 671          
 672          $c = new ReflectionClass($class);
 673          foreach ($c->getMethods() as $m) {
 674              if ($m->isStatic() && $m->isPublic()) {
 675                  $this->__extend[$m->name] = array($class,$m->name);
 676              }
 677          }
 678      }
 679      
 680  	private function setRow()
 681      {
 682          $this->__row = $this->__info['con']->db_fetch_assoc($this->__result);
 683          
 684          if ($this->__row !== false)
 685          {
 686              foreach ($this->__row as $k => $v) {
 687                  $this->__row[] =& $this->__row[$k];
 688              }
 689              return true;
 690          }
 691          else
 692          {
 693              return false;
 694          }
 695      }
 696      
 697      /**
 698      Returns the current index position (O is first) or move to <var>$row</var> if
 699      specified.
 700      
 701      @param    row        <b>integer</b>        Row number to move
 702      @return    <b>integer</b>
 703      */
 704  	public function index($row=null)
 705      {
 706          if ($row === null) {
 707              return $this->__index === null ? 0 : $this->__index;
 708          }
 709          
 710          if ($row < 0 || $row+1 > $this->__info['rows']) {
 711              return false;
 712          }
 713          
 714          if ($this->__info['con']->db_result_seek($this->__result,(integer) $row))
 715          {
 716              $this->__index = $row;
 717              $this->setRow();
 718              $this->__info['con']->db_result_seek($this->__result,(integer) $row);
 719              return true;
 720          }
 721          return false;
 722      }
 723      
 724      /**
 725      This method moves index to one position and return true until index is not
 726      the last one. You can use it to loop over record. Example:
 727      @code
 728      <?php
 729      while ($rs->fetch()) {
 730          echo $rs->field1;
 731      }
 732      ?>
 733      @endcode
 734      
 735      @return    <b>boolean</b>
 736      */
 737  	public function fetch()
 738      {
 739          if (!$this->__fetch) {
 740              $this->__fetch = true;
 741              $i = -1;
 742          } else {
 743              $i = $this->__index;
 744          }
 745          
 746          if (!$this->index($i+1)) {
 747              $this->__fetch = false;
 748              $this->__index = 0;
 749              return false;
 750          }
 751          
 752          return true;
 753      }
 754      
 755      /**
 756      Moves index to first position.
 757      
 758      @return    <b>boolean</b>
 759      */
 760  	public function moveStart()
 761      {
 762          return $this->index(0);
 763      }
 764      
 765      /**
 766      Moves index to last position.
 767      
 768      @return    <b>boolean</b>
 769      */
 770  	public function moveEnd()
 771      {
 772          return $this->index($this->__info['rows']-1);
 773      }
 774      
 775      /**
 776      Moves index to next position.
 777      
 778      @return    <b>boolean</b>
 779      */
 780  	public function moveNext()
 781      {
 782          return $this->index($this->__index+1);
 783      }
 784      
 785      /**
 786      Moves index to previous position.
 787      
 788      @return    <b>boolean</b>
 789      */
 790  	public function movePrev()
 791      {
 792          return $this->index($this->__index-1);
 793      }
 794      
 795      /**
 796      Returns true if index is at last position.
 797      
 798      @return    <b>boolean</b>
 799      */
 800  	public function isEnd()
 801      {
 802          return $this->__index+1 == $this->count();
 803      }
 804      
 805      /**
 806      Returns true if index is at first position.
 807      
 808      @return    <b>boolean</b>
 809      */
 810  	public function isStart()
 811      {
 812          return $this->__index <= 0;
 813      }
 814      
 815      /**
 816      Returns true if record contains no result.
 817      
 818      @return    <b>boolean</b>
 819      */
 820  	public function isEmpty()
 821      {
 822          return $this->count() == 0;
 823      }
 824      
 825      /**
 826      Returns number of rows in record.
 827      
 828      @return    <b>integer</b>
 829      */
 830  	public function count()
 831      {
 832          return $this->__info['rows'];
 833      }
 834      
 835      /**
 836      Returns an array of columns, with name as key and type as value.
 837      
 838      @return    <b>array</b>
 839      */
 840  	public function columns()
 841      {
 842          return $this->__info['info']['name'];
 843      }
 844      
 845      /**
 846      Returns an array of all rows in record.
 847      
 848      @return    <b>array</b>
 849      */
 850  	public function rows()
 851      {
 852          return $this->getData();
 853      }
 854      
 855      /**
 856      Returns an array of all rows in record. This method is called by rows().
 857      
 858      @return    <b>array</b>
 859      */
 860  	protected function getData()
 861      {
 862          $res = array();
 863          
 864          if ($this->count() == 0) {
 865              return $res;
 866          }
 867          
 868          $this->__info['con']->db_result_seek($this->__result,0);
 869          while (($r = $this->__info['con']->db_fetch_assoc($this->__result)) !== false) {
 870              foreach ($r as $k => $v) {
 871                  $r[] =& $r[$k];
 872              }
 873              $res[] = $r;
 874          }
 875          $this->__info['con']->db_result_seek($this->__result,$this->__index);
 876          
 877          return $res;
 878      }
 879  }
 880  
 881  /**
 882  @ingroup CB_DBLAYER
 883  @brief Query Result Static Record Class
 884  
 885  Unlike record class, this one contains all results in an associative array.
 886  */
 887  class staticRecord extends record
 888  {
 889      public $__data = array();    ///< <b>array</b>    Data array
 890      private $__sortfield;
 891      private $__sortsign;
 892      
 893  	public function __construct($result,$info)
 894      {
 895          if (is_array($result))
 896          {
 897              $this->__info = $info;
 898              $this->__data = $result;
 899          }
 900          else
 901          {
 902              parent::__construct($result,$info);
 903              $this->__data = parent::getData();
 904          }
 905          
 906          unset($this->__link);
 907          unset($this->__result);
 908      }
 909      
 910      /**
 911      Returns a new instance of object from an associative array.
 912      
 913      @param    data        <b>array</b>        Data array
 914      @return    <b>staticRecord</b>
 915      */
 916  	public static function newFromArray($data)
 917      {
 918          if (!is_array($data)) {
 919              $data = array();
 920          }
 921          
 922          $data = array_values($data);
 923          
 924          if (empty($data) || !is_array($data[0])) {
 925              $cols = 0;
 926          } else {
 927              $cols = count($data[0]);
 928          }
 929          
 930          $info = array(
 931              'con' => null,
 932              'info' => null,
 933              'cols' => $cols,
 934              'rows' => count($data)
 935          );
 936          
 937          return new self($data,$info);
 938      }
 939      
 940  	public function field($n)
 941      {
 942          return $this->__data[$this->__index][$n];
 943      }
 944      
 945  	public function index($row=null)
 946      {
 947          if ($row === null) {
 948              return $this->__index;
 949          }
 950          
 951          if ($row < 0 || $row+1 > $this->__info['rows']) {
 952              return false;
 953          }
 954          
 955          $this->__index = $row;
 956          return true;
 957      }
 958      
 959  	public function rows()
 960      {
 961          return $this->__data;
 962      }
 963      
 964      /**
 965      Changes value of a given field in the current row.
 966      
 967      @param    n    <b>string</b>        Field name
 968      @param    v    <b>mixed</b>        Field value
 969      */
 970  	public function set($n,$v)
 971      {
 972          if ($this->__index === null) {
 973              return false;
 974          }
 975          
 976          $this->__data[$this->__index][$n] = $v;
 977      }
 978      
 979      /**
 980      Sorts values by a field in a given order.
 981      
 982      @param    field    <b>string</b>        Field name
 983      @param    order    <b>string</b>        Sort type (asc or desc)
 984      */
 985  	public function sort($field,$order='asc')
 986      {
 987          if (!isset($this->__data[0][$field])) {
 988              return false;
 989          }
 990          
 991          $this->__sortfield = $field;
 992          $this->__sortsign = strtolower($order) == 'asc' ? 1 : -1;
 993          
 994          usort($this->__data,array($this,'sortCallback'));
 995          
 996          $this->__sortfield = null;
 997          $this->__sortsign = null;
 998      }
 999      
1000  	private function sortCallback($a,$b)
1001      {
1002          $a = $a[$this->__sortfield];
1003          $b = $b[$this->__sortfield];
1004          
1005          # Integer values
1006          if ($a == (string) (integer) $a && $b == (string) (integer) $b) {
1007              $a = (integer) $a;
1008              $b = (integer) $b;
1009              return ($a - $b) * $this->__sortsign;
1010          }
1011          
1012          return strcmp($a,$b) * $this->__sortsign;
1013      }
1014  }
1015  ?>


Généré le : Fri Feb 23 22:16:06 2007 par Balluche grâce à PHPXref 0.7