Version 1
[yaffs-website] / vendor / gabordemooij / redbean / RedBeanPHP / Adapter / DBAdapter.php
1 <?php
2
3 namespace RedBeanPHP\Adapter;
4
5 use RedBeanPHP\Observable as Observable;
6 use RedBeanPHP\Adapter as Adapter;
7 use RedBeanPHP\Driver as Driver;
8
9 /**
10  * DBAdapter (Database Adapter)
11  *
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 ).
18  *
19  * @file    RedBeanPHP/Adapter/DBAdapter.php
20  * @author  Gabor de Mooij and the RedBeanPHP Community.
21  * @license BSD/GPLv2
22  *
23  * @copyright
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.
27  */
28 class DBAdapter extends Observable implements Adapter
29 {
30         /**
31          * @var Driver
32          */
33         private $db = NULL;
34
35         /**
36          * @var string
37          */
38         private $sql = '';
39
40         /**
41          * Constructor.
42          *
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.
46          *
47          * @param Driver $database ADO Compatible DB Instance
48          */
49         public function __construct( $database )
50         {
51                 $this->db = $database;
52         }
53
54         /**
55          * Returns a string containing the most recent SQL query
56          * processed by the database adapter, thus conforming to the
57          * interface:
58          *
59          * @see Adapter::getSQL
60          *
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.
64          *
65          * @return string
66          */
67         public function getSQL()
68         {
69                 return $this->sql;
70         }
71
72         /**
73          * @see Adapter::exec
74          */
75         public function exec( $sql, $bindings = array(), $noevent = FALSE )
76         {
77                 if ( !$noevent ) {
78                         $this->sql = $sql;
79                         $this->signal( 'sql_exec', $this );
80                 }
81
82                 return $this->db->Execute( $sql, $bindings );
83         }
84
85         /**
86          * @see Adapter::get
87          */
88         public function get( $sql, $bindings = array() )
89         {
90                 $this->sql = $sql;
91                 $this->signal( 'sql_exec', $this );
92
93                 return $this->db->GetAll( $sql, $bindings );
94         }
95
96         /**
97          * @see Adapter::getRow
98          */
99         public function getRow( $sql, $bindings = array() )
100         {
101                 $this->sql = $sql;
102                 $this->signal( 'sql_exec', $this );
103
104                 return $this->db->GetRow( $sql, $bindings );
105         }
106
107         /**
108          * @see Adapter::getCol
109          */
110         public function getCol( $sql, $bindings = array() )
111         {
112                 $this->sql = $sql;
113                 $this->signal( 'sql_exec', $this );
114
115                 return $this->db->GetCol( $sql, $bindings );
116         }
117
118         /**
119          * @see Adapter::getAssoc
120          */
121         public function getAssoc( $sql, $bindings = array() )
122         {
123                 $this->sql = $sql;
124
125                 $this->signal( 'sql_exec', $this );
126
127                 $rows  = $this->db->GetAll( $sql, $bindings );
128
129                 $assoc = array();
130                 if ( !$rows ) {
131                         return $assoc;
132                 }
133
134                 foreach ( $rows as $row ) {
135                         if ( empty( $row ) ) continue;
136
137                         if ( count( $row ) > 2 ) {
138             $key   = array_shift( $row );
139             $value = $row;
140         } elseif ( count( $row ) > 1 ) {
141                                 $key   = array_shift( $row );
142                                 $value = array_shift( $row );
143                         } else {
144                                 $key   = array_shift( $row );
145                                 $value = $key;
146                         }
147
148                         $assoc[$key] = $value;
149                 }
150
151                 return $assoc;
152         }
153
154         /**
155          * @see Adapter::getAssocRow
156          */
157         public function getAssocRow($sql, $bindings = array())
158         {
159                 $this->sql = $sql;
160                 $this->signal( 'sql_exec', $this );
161
162                 return $this->db->GetAssocRow( $sql, $bindings );
163         }
164
165         /**
166          * @see Adapter::getCell
167          */
168         public function getCell( $sql, $bindings = array(), $noSignal = NULL )
169         {
170                 $this->sql = $sql;
171
172                 if ( !$noSignal ) $this->signal( 'sql_exec', $this );
173
174                 return $this->db->GetOne( $sql, $bindings );
175         }
176
177         /**
178          * @see Adapter::getCursor
179          */
180         public function getCursor( $sql, $bindings = array() )
181         {
182                 return $this->db->GetCursor( $sql, $bindings );
183         }
184
185         /**
186          * @see Adapter::getInsertID
187          */
188         public function getInsertID()
189         {
190                 return $this->db->getInsertID();
191         }
192
193         /**
194          * @see Adapter::getAffectedRows
195          */
196         public function getAffectedRows()
197         {
198                 return $this->db->Affected_Rows();
199         }
200
201         /**
202          * @see Adapter::getDatabase
203          */
204         public function getDatabase()
205         {
206                 return $this->db;
207         }
208
209         /**
210          * @see Adapter::startTransaction
211          */
212         public function startTransaction()
213         {
214                 $this->db->StartTrans();
215         }
216
217         /**
218          * @see Adapter::commit
219          */
220         public function commit()
221         {
222                 $this->db->CommitTrans();
223         }
224
225         /**
226          * @see Adapter::rollback
227          */
228         public function rollback()
229         {
230                 $this->db->FailTrans();
231         }
232
233         /**
234          * @see Adapter::close.
235          */
236         public function close()
237         {
238                 $this->db->close();
239         }
240 }