00001 <?php
00026 require_once 'Zend/Db.php';
00027
00031 require_once 'Zend/Db/Statement/Interface.php';
00032
00042 abstract class Zend_Db_Statement implements Zend_Db_Statement_Interface
00043 {
00044
00048 protected $_stmt = null;
00049
00053 protected $_adapter = null;
00054
00060 protected $_fetchMode = Zend_Db::FETCH_ASSOC;
00061
00067 protected $_attribute = array();
00068
00074 protected $_bindColumn = array();
00075
00081 protected $_bindParam = array();
00082
00088 protected $_sqlSplit = array();
00089
00095 protected $_sqlParam = array();
00096
00100 protected $_queryId = null;
00101
00108 public function __construct($adapter, $sql)
00109 {
00110 $this->_adapter = $adapter;
00111 if ($sql instanceof Zend_Db_Select) {
00112 $sql = $sql->assemble();
00113 }
00114 $this->_parseParameters($sql);
00115 $this->_prepare($sql);
00116
00117 $this->_queryId = $this->_adapter->getProfiler()->queryStart($sql);
00118 }
00119
00126 protected function _prepare($sql)
00127 {
00128 return;
00129 }
00130
00135 protected function _parseParameters($sql)
00136 {
00137 $sql = $this->_stripQuoted($sql);
00138
00139
00140 $this->_sqlSplit = preg_split('/(\?|\:[a-zA-Z0-9_]+)/',
00141 $sql, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
00142
00143
00144 $this->_sqlParam = array();
00145 foreach ($this->_sqlSplit as $key => $val) {
00146 if ($val == '?') {
00147 if ($this->_adapter->supportsParameters('positional') === false) {
00151 require_once 'Zend/Db/Statement/Exception.php';
00152 throw new Zend_Db_Statement_Exception("Invalid bind-variable position '$val'");
00153 }
00154 } else if ($val[0] == ':') {
00155 if ($this->_adapter->supportsParameters('named') === false) {
00159 require_once 'Zend/Db/Statement/Exception.php';
00160 throw new Zend_Db_Statement_Exception("Invalid bind-variable name '$val'");
00161 }
00162 }
00163 $this->_sqlParam[] = $val;
00164 }
00165
00166
00167 $this->_bindParam = array();
00168 }
00169
00177 protected function _stripQuoted($sql)
00178 {
00179
00180
00181 $d = $this->_adapter->quoteIdentifier('a');
00182 $d = $d[0];
00183
00184
00185
00186 $de = $this->_adapter->quoteIdentifier($d);
00187 $de = substr($de, 1, 2);
00188 $de = str_replace('\\', '\\\\', $de);
00189
00190
00191
00192 $q = $this->_adapter->quote('a');
00193 $q = $q[0];
00194
00195
00196
00197 $qe = $this->_adapter->quote($q);
00198 $qe = substr($qe, 1, 2);
00199 $qe = str_replace('\\', '\\\\', $qe);
00200
00201
00202
00203
00204 $sql = preg_replace("/$q($qe|\\\\{2}|[^$q])*$q/", '', $sql);
00205
00206 if (!empty($q)) {
00207 $sql = preg_replace("/$q($qe|[^$q])*$q/", '', $sql);
00208 }
00209
00210 return $sql;
00211 }
00212
00222 public function bindColumn($column, &$param, $type = null)
00223 {
00224 $this->_bindColumn[$column] =& $param;
00225 return true;
00226 }
00227
00238 public function bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
00239 {
00240 if (!is_int($parameter) && !is_string($parameter)) {
00244 require_once 'Zend/Db/Statement/Exception.php';
00245 throw new Zend_Db_Statement_Exception('Invalid bind-variable position');
00246 }
00247
00248 $position = null;
00249 if (($intval = (int) $parameter) > 0 && $this->_adapter->supportsParameters('positional')) {
00250 if ($intval >= 1 || $intval <= count($this->_sqlParam)) {
00251 $position = $intval;
00252 }
00253 } else if ($this->_adapter->supportsParameters('named')) {
00254 if ($parameter[0] != ':') {
00255 $parameter = ':' . $parameter;
00256 }
00257 if (in_array($parameter, $this->_sqlParam) !== false) {
00258 $position = $parameter;
00259 }
00260 }
00261
00262 if ($position === null) {
00266 require_once 'Zend/Db/Statement/Exception.php';
00267 throw new Zend_Db_Statement_Exception("Invalid bind-variable position '$parameter'");
00268 }
00269
00270
00271 $this->_bindParam[$position] =& $variable;
00272 return $this->_bindParam($position, $variable, $type, $length, $options);
00273 }
00274
00283 public function bindValue($parameter, $value, $type = null)
00284 {
00285 return $this->bindParam($parameter, $value, $type);
00286 }
00287
00294 public function execute(array $params = null)
00295 {
00296
00297
00298
00299 if ($this->_queryId === null) {
00300 return $this->_execute($params);
00301 }
00302
00303
00304
00305
00306
00307 $prof = $this->_adapter->getProfiler();
00308 $qp = $prof->getQueryProfile($this->_queryId);
00309 if ($qp->hasEnded()) {
00310 $this->_queryId = $prof->queryClone($qp);
00311 $qp = $prof->getQueryProfile($this->_queryId);
00312 }
00313 if ($params !== null) {
00314 $qp->bindParams($params);
00315 } else {
00316 $qp->bindParams($this->_bindParam);
00317 }
00318 $qp->start($this->_queryId);
00319
00320 $retval = $this->_execute($params);
00321
00322 $prof->queryEnd($this->_queryId);
00323
00324 return $retval;
00325 }
00326
00334 public function fetchAll($style = null, $col = null)
00335 {
00336 $data = array();
00337 if ($style === Zend_Db::FETCH_COLUMN && $col === null) {
00338 $col = 0;
00339 }
00340 if ($col === null) {
00341 while ($row = $this->fetch($style)) {
00342 $data[] = $row;
00343 }
00344 } else {
00345 while (false !== ($val = $this->fetchColumn($col))) {
00346 $data[] = $val;
00347 }
00348 }
00349 return $data;
00350 }
00351
00358 public function fetchColumn($col = 0)
00359 {
00360 $data = array();
00361 $col = (int) $col;
00362 $row = $this->fetch(Zend_Db::FETCH_NUM);
00363 if (!is_array($row)) {
00364 return false;
00365 }
00366 return $row[$col];
00367 }
00368
00376 public function fetchObject($class = 'stdClass', array $config = array())
00377 {
00378 $obj = new $class($config);
00379 $row = $this->fetch(Zend_Db::FETCH_ASSOC);
00380 if (!is_array($row)) {
00381 return false;
00382 }
00383 foreach ($row as $key => $val) {
00384 $obj->$key = $val;
00385 }
00386 return $obj;
00387 }
00388
00395 public function getAttribute($key)
00396 {
00397 if (array_key_exists($key, $this->_attribute)) {
00398 return $this->_attribute[$key];
00399 }
00400 }
00401
00409 public function setAttribute($key, $val)
00410 {
00411 $this->_attribute[$key] = $val;
00412 }
00413
00421 public function setFetchMode($mode)
00422 {
00423 switch ($mode) {
00424 case Zend_Db::FETCH_NUM:
00425 case Zend_Db::FETCH_ASSOC:
00426 case Zend_Db::FETCH_BOTH:
00427 case Zend_Db::FETCH_OBJ:
00428 $this->_fetchMode = $mode;
00429 break;
00430 case Zend_Db::FETCH_BOUND:
00431 default:
00432 $this->closeCursor();
00436 require_once 'Zend/Db/Statement/Exception.php';
00437 throw new Zend_Db_Statement_Exception('invalid fetch mode');
00438 break;
00439 }
00440 }
00441
00449 public function _fetchBound($row)
00450 {
00451 foreach ($row as $key => $value) {
00452
00453
00454 if (is_int($key)) {
00455 $key++;
00456 }
00457
00458 if (isset($this->_bindColumn[$key])) {
00459 $this->_bindColumn[$key] = $value;
00460 }
00461 }
00462 return true;
00463 }
00464
00471 public function getAdapter()
00472 {
00473 return $this->_adapter;
00474 }
00475
00481 public function getDriverStatement()
00482 {
00483 return $this->_stmt;
00484 }
00485 }