[ Index ] |
PHP Cross Reference of YOURLS |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * 4 * This file is part of Aura for PHP. 5 * 6 * @license https://opensource.org/licenses/MIT MIT 7 * 8 */ 9 namespace Aura\Sql; 10 11 use Aura\Sql\Profiler\Profiler; 12 use Aura\Sql\Profiler\ProfilerInterface; 13 use PDO; 14 15 /** 16 * 17 * A lazy-connecting PDO with extended methods. 18 * 19 * @package Aura.Sql 20 * 21 */ 22 class ExtendedPdo extends AbstractExtendedPdo 23 { 24 /** 25 * 26 * Constructor arguments for instantiating the PDO connection. 27 * 28 * @var array 29 * 30 */ 31 protected $args = []; 32 33 /** 34 * 35 * Constructor. 36 * 37 * This overrides the parent so that it can take connection attributes as a 38 * constructor parameter, and set them after connection. 39 * 40 * @param string $dsn The data source name for the connection. 41 * 42 * @param string $username The username for the connection. 43 * 44 * @param string $password The password for the connection. 45 * 46 * @param array $options Driver-specific options for the connection. 47 * 48 * @param array $queries Queries to execute after the connection. 49 * 50 * @param ProfilerInterface $profiler Tracks and logs query profiles. 51 * 52 * @see http://php.net/manual/en/pdo.construct.php 53 * 54 */ 55 public function __construct( 56 $dsn, 57 $username = null, 58 $password = null, 59 array $options = [], 60 array $queries = [], 61 ProfilerInterface $profiler = null 62 ) { 63 // if no error mode is specified, use exceptions 64 if (! isset($options[PDO::ATTR_ERRMODE])) { 65 $options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; 66 } 67 68 // retain the arguments for later 69 $this->args = [ 70 $dsn, 71 $username, 72 $password, 73 $options, 74 $queries 75 ]; 76 77 // retain a profiler, instantiating a default one if needed 78 if ($profiler === null) { 79 $profiler = new Profiler(); 80 } 81 $this->setProfiler($profiler); 82 83 // retain a query parser 84 $parts = explode(':', $dsn); 85 $parser = $this->newParser($parts[0]); 86 $this->setParser($parser); 87 88 // set quotes for identifier names 89 $this->setQuoteName($parts[0]); 90 } 91 92 /** 93 * 94 * Connects to the database. 95 * 96 * @return null 97 * 98 */ 99 public function connect() 100 { 101 if ($this->pdo) { 102 return; 103 } 104 105 // connect 106 $this->profiler->start(__FUNCTION__); 107 list($dsn, $username, $password, $options, $queries) = $this->args; 108 $this->pdo = new PDO($dsn, $username, $password, $options); 109 $this->profiler->finish(); 110 111 // connection-time queries 112 foreach ($queries as $query) { 113 $this->exec($query); 114 } 115 } 116 117 /** 118 * 119 * Disconnects from the database. 120 * 121 * @return null 122 * 123 */ 124 public function disconnect() 125 { 126 $this->profiler->start(__FUNCTION__); 127 $this->pdo = null; 128 $this->profiler->finish(); 129 } 130 131 /** 132 * 133 * The purpose of this method is to hide sensitive data from stack traces. 134 * 135 * @return array 136 * 137 */ 138 public function __debugInfo() 139 { 140 return [ 141 'args' => [ 142 $this->args[0], 143 '****', 144 '****', 145 $this->args[3], 146 $this->args[4], 147 ] 148 ]; 149 } 150 151 /** 152 * 153 * Return the inner PDO (if any) 154 * 155 * @return \PDO 156 * 157 */ 158 public function getPdo() 159 { 160 $this->connect(); 161 return $this->pdo; 162 } 163 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Sep 19 05:10:04 2024 | Cross-referenced by PHPXref 0.7.1 |