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: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class DBAdapter extends Observable implements Adapter
29: {
30: 31: 32:
33: private $db = NULL;
34:
35: 36: 37:
38: private $sql = '';
39:
40: 41: 42: 43: 44: 45: 46: 47: 48:
49: public function __construct( $database )
50: {
51: $this->db = $database;
52: }
53:
54: 55: 56:
57: public function getSQL()
58: {
59: return $this->sql;
60: }
61:
62: 63: 64:
65: public function exec( $sql, $bindings = array(), $noevent = FALSE )
66: {
67: if ( !$noevent ) {
68: $this->sql = $sql;
69: $this->signal( 'sql_exec', $this );
70: }
71:
72: return $this->db->Execute( $sql, $bindings );
73: }
74:
75: 76: 77:
78: public function get( $sql, $bindings = array() )
79: {
80: $this->sql = $sql;
81: $this->signal( 'sql_exec', $this );
82:
83: return $this->db->GetAll( $sql, $bindings );
84: }
85:
86: 87: 88:
89: public function getRow( $sql, $bindings = array() )
90: {
91: $this->sql = $sql;
92: $this->signal( 'sql_exec', $this );
93:
94: return $this->db->GetRow( $sql, $bindings );
95: }
96:
97: 98: 99:
100: public function getCol( $sql, $bindings = array() )
101: {
102: $this->sql = $sql;
103: $this->signal( 'sql_exec', $this );
104:
105: return $this->db->GetCol( $sql, $bindings );
106: }
107:
108: 109: 110:
111: public function getAssoc( $sql, $bindings = array() )
112: {
113: $this->sql = $sql;
114:
115: $this->signal( 'sql_exec', $this );
116:
117: $rows = $this->db->GetAll( $sql, $bindings );
118:
119: $assoc = array();
120: if ( !$rows ) {
121: return $assoc;
122: }
123:
124: foreach ( $rows as $row ) {
125: if ( empty( $row ) ) continue;
126:
127: if ( count( $row ) > 2 ) {
128: $key = array_shift( $row );
129: $value = $row;
130: } elseif ( count( $row ) > 1 ) {
131: $key = array_shift( $row );
132: $value = array_shift( $row );
133: } else {
134: $key = array_shift( $row );
135: $value = $key;
136: }
137:
138: $assoc[$key] = $value;
139: }
140:
141: return $assoc;
142: }
143:
144: 145: 146:
147: public function getAssocRow($sql, $bindings = array())
148: {
149: $this->sql = $sql;
150: $this->signal( 'sql_exec', $this );
151:
152: return $this->db->GetAssocRow( $sql, $bindings );
153: }
154:
155: 156: 157:
158: public function getCell( $sql, $bindings = array(), $noSignal = NULL )
159: {
160: $this->sql = $sql;
161:
162: if ( !$noSignal ) $this->signal( 'sql_exec', $this );
163:
164: return $this->db->GetOne( $sql, $bindings );
165: }
166:
167: 168: 169:
170: public function getCursor( $sql, $bindings = array() )
171: {
172: return $this->db->GetCursor( $sql, $bindings );
173: }
174:
175: 176: 177:
178: public function getInsertID()
179: {
180: return $this->db->getInsertID();
181: }
182:
183: 184: 185:
186: public function getAffectedRows()
187: {
188: return $this->db->Affected_Rows();
189: }
190:
191: 192: 193:
194: public function getDatabase()
195: {
196: return $this->db;
197: }
198:
199: 200: 201:
202: public function startTransaction()
203: {
204: $this->db->StartTrans();
205: }
206:
207: 208: 209:
210: public function commit()
211: {
212: $this->db->CommitTrans();
213: }
214:
215: 216: 217:
218: public function rollback()
219: {
220: $this->db->FailTrans();
221: }
222:
223: 224: 225:
226: public function close()
227: {
228: $this->db->close();
229: }
230: }
231: