3 namespace RedBeanPHP\Adapter;
5 use RedBeanPHP\Observable as Observable;
6 use RedBeanPHP\Adapter as Adapter;
7 use RedBeanPHP\Driver as Driver;
10 * DBAdapter (Database Adapter)
12 * An adapter class to connect various database systems to RedBean
13 * Database Adapter Class. The task of the database adapter class is to
14 * communicate with the database driver. You can use all sorts of database
15 * drivers with RedBeanPHP. The default database drivers that ships with
16 * the RedBeanPHP library is the RPDO driver ( which uses the PHP Data Objects
17 * Architecture aka PDO ).
19 * @file RedBeanPHP/Adapter/DBAdapter.php
20 * @author Gabor de Mooij and the RedBeanPHP Community.
24 * (c) copyright G.J.G.T. (Gabor) de Mooij and the RedBeanPHP community.
25 * This source file is subject to the BSD/GPLv2 License that is bundled
26 * with this source code in the file license.txt.
28 class DBAdapter extends Observable implements Adapter
43 * Creates an instance of the RedBean Adapter Class.
44 * This class provides an interface for RedBean to work
45 * with ADO compatible DB instances.
47 * @param Driver $database ADO Compatible DB Instance
49 public function __construct( $database )
51 $this->db = $database;
55 * Returns a string containing the most recent SQL query
56 * processed by the database adapter, thus conforming to the
59 * @see Adapter::getSQL
61 * Methods like get(), getRow() and exec() cause this SQL cache
62 * to get filled. If no SQL query has been processed yet this function
63 * will return an empty string.
67 public function getSQL()
75 public function exec( $sql, $bindings = array(), $noevent = FALSE )
79 $this->signal( 'sql_exec', $this );
82 return $this->db->Execute( $sql, $bindings );
88 public function get( $sql, $bindings = array() )
91 $this->signal( 'sql_exec', $this );
93 return $this->db->GetAll( $sql, $bindings );
97 * @see Adapter::getRow
99 public function getRow( $sql, $bindings = array() )
102 $this->signal( 'sql_exec', $this );
104 return $this->db->GetRow( $sql, $bindings );
108 * @see Adapter::getCol
110 public function getCol( $sql, $bindings = array() )
113 $this->signal( 'sql_exec', $this );
115 return $this->db->GetCol( $sql, $bindings );
119 * @see Adapter::getAssoc
121 public function getAssoc( $sql, $bindings = array() )
125 $this->signal( 'sql_exec', $this );
127 $rows = $this->db->GetAll( $sql, $bindings );
134 foreach ( $rows as $row ) {
135 if ( empty( $row ) ) continue;
137 if ( count( $row ) > 2 ) {
138 $key = array_shift( $row );
140 } elseif ( count( $row ) > 1 ) {
141 $key = array_shift( $row );
142 $value = array_shift( $row );
144 $key = array_shift( $row );
148 $assoc[$key] = $value;
155 * @see Adapter::getAssocRow
157 public function getAssocRow($sql, $bindings = array())
160 $this->signal( 'sql_exec', $this );
162 return $this->db->GetAssocRow( $sql, $bindings );
166 * @see Adapter::getCell
168 public function getCell( $sql, $bindings = array(), $noSignal = NULL )
172 if ( !$noSignal ) $this->signal( 'sql_exec', $this );
174 return $this->db->GetOne( $sql, $bindings );
178 * @see Adapter::getCursor
180 public function getCursor( $sql, $bindings = array() )
182 return $this->db->GetCursor( $sql, $bindings );
186 * @see Adapter::getInsertID
188 public function getInsertID()
190 return $this->db->getInsertID();
194 * @see Adapter::getAffectedRows
196 public function getAffectedRows()
198 return $this->db->Affected_Rows();
202 * @see Adapter::getDatabase
204 public function getDatabase()
210 * @see Adapter::startTransaction
212 public function startTransaction()
214 $this->db->StartTrans();
218 * @see Adapter::commit
220 public function commit()
222 $this->db->CommitTrans();
226 * @see Adapter::rollback
228 public function rollback()
230 $this->db->FailTrans();
234 * @see Adapter::close.
236 public function close()