[ Index ]
 

Code source de Dotclear 2.0-beta6

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

title

Body

[fermer]

/inc/clearbricks/dblayer/ -> class.cursor.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  class cursor
  24  {
  25      private $__con;
  26      private $__data = array();
  27      private $__table;
  28      
  29  	public function __construct(&$con,$table)
  30      {
  31          $this->__con =& $con;
  32          $this->setTable($table);
  33      }
  34      
  35  	public function setTable($table)
  36      {
  37          $this->__table = $table;
  38          $this->__data = array();
  39      }
  40      
  41  	public function setField($n,$v)
  42      {
  43          $this->__data[$n] = $v;
  44      }
  45      
  46  	public function unsetField($n)
  47      {
  48          unset($this->__data[$n]);
  49      }
  50      
  51  	public function isField($n)
  52      {
  53          return isset($this->__data[$n]);
  54      }
  55      
  56  	public function getField($n)
  57      {
  58          if (isset($this->__data[$n])) {
  59              return $this->__data[$n];
  60          }
  61          
  62          return null;
  63      }
  64      
  65  	public function __set($n,$v)
  66      {
  67          $this->setField($n,$v);
  68      }
  69      
  70  	public function __get($n)
  71      {
  72          return $this->getField($n);
  73      }
  74      
  75  	public function clean()
  76      {
  77          $this->__data = array();
  78      }
  79      
  80  	private function formatFields()
  81      {
  82          $data = array();
  83          
  84          foreach ($this->__data as $k => $v)
  85          {
  86              $k = $this->__con->escapeSystem($k);
  87              
  88              if (is_null($v)) {
  89                  $data[$k] = 'NULL';
  90              } elseif (is_string($v)) {
  91                  $data[$k] = "'".$this->__con->escape($v)."'";
  92              } elseif (is_array($v)) {
  93                  $data[$k] = $v[0];
  94              } else {
  95                  $data[$k] = $v;
  96              }
  97          }
  98          
  99          return $data;
 100      }
 101      
 102  	public function getInsert()
 103      {
 104          $data = $this->formatFields();
 105          
 106          $insReq = 'INSERT INTO '.$this->__con->escapeSystem($this->__table)." (\n".
 107                  implode(",\n",array_keys($data))."\n) VALUES (\n".
 108                  implode(",\n",array_values($data))."\n) ";
 109          
 110          return $insReq;
 111      }
 112      
 113  	public function getUpdate($where)
 114      {
 115          $data = $this->formatFields();
 116          $fields = array();
 117          
 118          $updReq = 'UPDATE '.$this->__con->escapeSystem($this->__table)." SET \n";
 119          
 120          foreach ($data as $k => $v) {
 121              $fields[] = $k.' = '.$v."";
 122          }
 123          
 124          $updReq .= implode(",\n",$fields);
 125          $updReq .= "\n".$where;
 126          
 127          return $updReq;
 128      }
 129      
 130  	public function insert()
 131      {
 132          if (!$this->__table) {
 133              throw new Exception('No table name.');
 134          }
 135          
 136          $insReq = $this->getInsert();
 137          
 138          $this->__con->execute($insReq);
 139          
 140          return true;
 141      }
 142      
 143  	public function update($where)
 144      {
 145          if (!$this->__table) {
 146              throw new Exception('No table name.');
 147          }
 148          
 149          $updReq = $this->getUpdate($where);
 150          
 151          $this->__con->execute($updReq);
 152          
 153          return true;
 154      }
 155  }
 156  ?>


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