Special Ops 2.50
[specialops2.git] / lib / SO2_PDO.php
blobadbfb0c78ea95a3df9c1ce5c1e48d4e3eb33d2f0
1 <?php
2 /**
3 * SO2 custom PDO class. Way better than the so2mysqli one.
5 * @author Ant P <p@cpi.merseine.nu>
6 * @license file://../COPYING
7 * @version 2.15
8 */
9 class SO2_PDO extends PDO
11 const QSMART = PDO::FETCH_LAZY;
12 const QVALUE = PDO::FETCH_COLUMN;
13 const QROW = PDO::FETCH_NUM;
14 const QASSOC = PDO::FETCH_ASSOC;
15 const QOBJ = PDO::FETCH_OBJ;
16 const QNONE = null;
18 /**
19 * This is made of amazing.
20 * @param $q string SQL query
21 * @param $params mixed Array or single parameter for prepared statement placeholders
22 * @param $fetch int Determines the return type. Default is auto-detection
23 * @return mixed
25 public function q($q, $params = null, $fetch = self::QSMART)
27 //echo '<!--'.$q.print_r($params,1).print_r(debug_backtrace(), 1)."-->\n\n";
29 if ( self::QSMART == $fetch ) {
30 if ( strpos($q, 'SELECT') === 0 ) { // Default for selects is to return assoc array
31 $fetch = self::QASSOC;
32 } else { // Anything else is null
33 $fetch = self::QNONE;
37 $tmp = $this->prepare($q);
38 $tmp->execute((array)$params);
39 switch ( $fetch ) {
40 case self::QVALUE: // Return first column
41 return $tmp->fetchColumn(0);
42 case self::QROW: // Return numeric array
43 return $tmp->fetch(PDO::FETCH_NUM);
44 case self::QASSOC: // Return assoc array
45 return $tmp->fetch(PDO::FETCH_ASSOC);
46 case self::QOBJ: // Return the result directly; can then be used with PDO's fetch*()
47 return $tmp;
48 case self::QNONE: // Return nothing
49 return;
50 default:
51 trigger_error('Unknown return type requested in SO2_PDO::q(): '.$fetch, E_USER_WARNING);