[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/dbal/src/ -> lmbSimpleDb.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/dbal/src/query/lmbInsertQuery.class.php');
  10  lmb_require('limb/dbal/src/query/lmbSelectQuery.class.php');
  11  lmb_require('limb/dbal/src/query/lmbUpdateQuery.class.php');
  12  lmb_require('limb/dbal/src/query/lmbDeleteQuery.class.php');
  13  lmb_require('limb/dbal/src/criteria/lmbSQLFieldCriteria.class.php');
  14  
  15  /**

  16   * class lmbSimpleDb.

  17   *

  18   * @package dbal

  19   * @version $Id: lmbSimpleDb.class.php 5945 2007-06-06 08:31:43Z pachanga $

  20   */
  21  class lmbSimpleDb
  22  {
  23    protected $conn;
  24  
  25    function __construct($conn)
  26    {
  27      $this->conn = $conn;
  28    }
  29  
  30    function getType()
  31    {
  32      return $this->conn->getType();
  33    }
  34  
  35    function select($table, $criteria = null, $order = '')
  36    {
  37      $query = $this->getSelectQuery($table);
  38  
  39      if($criteria)
  40        $query->addCriteria(lmbSQLCriteria :: objectify($criteria));
  41  
  42      if($order)
  43        $query->addOrder($order);
  44  
  45      return $query->getRecordSet($this->conn);
  46    }
  47  
  48    function selectAsArray($table, $criteria = null, $order = '', $key_field = '')
  49    {
  50      $rs = $this->select($table, $criteria, $order);
  51      return $rs->getArray($key_field);
  52    }
  53  
  54    function getFirstRecordFrom($table_name, $criteria = null, $order = '')
  55    {
  56      $rs = $this->select($table_name, $criteria, $order);
  57      $rs->rewind();
  58      if($rs->valid())
  59        return $rs->current();
  60      else
  61        return new lmbSet();
  62    }
  63  
  64    function count($table_name, $criteria = null)
  65    {
  66      $rs = $this->select($table_name, $criteria);
  67      return $rs->count();
  68    }
  69  
  70    function getSelectQuery($table)
  71    {
  72      $query = new lmbSelectQuery(null, $this->conn);
  73      $query->addTable($table);
  74      return $query;
  75    }
  76  
  77    function insert($table, $values, $primary_key = 'id')
  78    {
  79      $query = new lmbInsertQuery($table, $this->conn);
  80  
  81      foreach($values as $key => $value)
  82        $query->addField($key , $value);
  83  
  84      $stmt = $query->getStatement($this->conn);
  85  
  86      if($primary_key)
  87      {
  88        if(isset($values[$primary_key]))
  89        {
  90          $stmt->execute();
  91          return $values[$primary_key];
  92        }
  93        else
  94          return $stmt->insertId($primary_key);
  95      }
  96      else
  97        $stmt->execute();
  98    }
  99  
 100    function update($table, $values, $criteria = null)
 101    {
 102      $query = new lmbUpdateQuery($table, $this->conn);
 103  
 104      if($criteria)
 105        $query->addCriteria(lmbSQLCriteria :: objectify($criteria));
 106  
 107      foreach($values as $key => $value)
 108        $query->addField($key, $value);
 109  
 110      $stmt = $query->getStatement($this->conn);
 111      $stmt->execute();
 112      return $stmt->getAffectedRowCount();
 113    }
 114  
 115    function delete($table, $criteria = null)
 116    {
 117      $query = new lmbDeleteQuery($table, $this->conn);
 118  
 119      if($criteria)
 120        $query->addCriteria(lmbSQLCriteria :: objectify($criteria));
 121  
 122      $stmt = $query->getStatement($this->conn);
 123      $stmt->execute();
 124      return $stmt->getAffectedRowCount();
 125    }
 126  
 127    function truncateDb()
 128    {
 129      $info = $this->conn->getDatabaseInfo();
 130      foreach($info->getTableList() as $table)
 131        $this->conn->newStatement("DELETE FROM $table")->execute();
 132    }
 133  
 134    function disconnect()
 135    {
 136      $this->conn->disconnect();
 137    }
 138  
 139    function begin()
 140    {
 141      $this->conn->beginTransaction();
 142    }
 143  
 144    function commit()
 145    {
 146      $this->conn->commitTransaction();
 147    }
 148  
 149    function rollback()
 150    {
 151      $this->conn->rollbackTransaction();
 152    }
 153  }
 154  
 155  ?>


Generated: Sat Aug 30 04:38:32 2008 Cross-referenced by PHPXref 0.7