summaryrefslogtreecommitdiff
path: root/library
diff options
context:
space:
mode:
authorGreg Roach <fisharebest@gmail.com>2014-09-18 17:21:41 +0100
committerGreg Roach <fisharebest@gmail.com>2014-09-18 17:36:51 +0100
commit3c972470cebf72a1e75c0d259c5b40b775c38c4c (patch)
tree81a013df4adbe9190b9368dad9e365844cd97e16 /library
parentd41520a343b581ec5e36379e39e1d04685053a99 (diff)
downloadwebtrees-3c972470cebf72a1e75c0d259c5b40b775c38c4c.tar.gz
webtrees-3c972470cebf72a1e75c0d259c5b40b775c38c4c.tar.bz2
webtrees-3c972470cebf72a1e75c0d259c5b40b775c38c4c.zip
Remove unused code from WT_DBStatement.
Diffstat (limited to 'library')
-rw-r--r--library/WT/DBStatement.php238
1 files changed, 139 insertions, 99 deletions
diff --git a/library/WT/DBStatement.php b/library/WT/DBStatement.php
index 4fd1a49d88..58088bf234 100644
--- a/library/WT/DBStatement.php
+++ b/library/WT/DBStatement.php
@@ -1,7 +1,4 @@
<?php
-// Class file for the database access. Extend PHP's native PDO and
-// PDOStatement classes to provide database access with logging, etc.
-//
// webtrees: Web based Family History software
// Copyright (C) 2014 webtrees development team.
//
@@ -20,141 +17,184 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
/**
- * Class WT_DB
+ * Class WT_DBStatement. Extend PHP's native PDOStatement class to provide:
*
- * @method WT_DBStatement execute()
- * @method stdClass[] fetchAll()
+ * Additional options for fetching data.
+ * Logging
+ * Automatic calling of execute() and closeCursor()
*/
class WT_DBStatement {
- //////////////////////////////////////////////////////////////////////////////
- // CONSTRUCTION
- // Decorate a PDOStatement object.
- // See http://en.wikipedia.org/wiki/Decorator_pattern
- //////////////////////////////////////////////////////////////////////////////
- private $pdostatement=null;
-
- // Keep track of calls to execute(), so we can do it automatically
- private $executed=false;
+ /** @var PDOStatement */
+ private $pdo_statement;
- // Keep a copy of the bind variables, for logging
- private $bind_variables=array();
+ /** @var bool Keep track of calls to execute(), so we can do it automatically */
+ private $executed = false;
- // Our constructor just takes a copy of the object to be decorated
- public function __construct(PDOStatement $statement) {
- $this->pdostatement=$statement;
+ /**
+ * Create a WT_DBStatement object from a PDOStatement object.
+ *
+ * @param PDOStatement $pdo_statement
+ */
+ public function __construct(PDOStatement $pdo_statement) {
+ $this->pdo_statement = $pdo_statement;
}
- // Need this function to load BLOB values from streams
- public function bindParam($num, &$value, $type) {
- $this->pdostatement->bindParam($num, $value, $type);
+ /**
+ * Execute a query
+ *
+ * @param array $bind_variables
+ *
+ * @return WT_DBStatement
+ * @throws Exception
+ */
+ public function execute($bind_variables = array()) {
+ if ($this->executed) {
+ throw new Exception('WT_DBStatement::execute() called twice.');
+ }
+
+ // Turn booleans into integers. Otherwise MySQL's strict mode can get upset.
+ foreach ($bind_variables as &$bind_variable) {
+ if ($bind_variable === false) {
+ // Otherwise true=>'1' and false=>''
+ $bind_variable = 0;
+ }
+ }
+ $start = microtime(true);
+ $this->pdo_statement->execute($bind_variables);
+ $end = microtime(true);
+ // If it was a SELECT statement, we cannot run it again.
+ $this->executed = preg_match('/^SELECT /i', $this->pdo_statement->queryString);
+
+ WT_DB::logQuery($this->pdo_statement->queryString, $this->pdo_statement->rowCount(), $end - $start, $bind_variables);
+
return $this;
}
- //////////////////////////////////////////////////////////////////////////////
- // FLUENT INTERFACE
- // Add automatic calling of execute() and closeCursor()
- // See http://en.wikipedia.org/wiki/Fluent_interface
- //////////////////////////////////////////////////////////////////////////////
- public function __call($function, $params) {
- switch ($function) {
- case 'closeCursor':
- $this->executed=false;
- // no break;
- case 'bindColumn':
- case 'bindParam':
- case 'bindValue':
- // TODO: bind variables need to be stored in $this->bind_variables so we can log them
- case 'setAttribute':
- case 'setFetchMode':
- // Functions that return no values become fluent
- call_user_func_array(array($this->pdostatement, $function), $params);
- return $this;
- case 'execute':
- if ($this->executed) {
- trigger_error('WT_DBStatement::execute() called twice.', E_USER_ERROR);
- } else {
- if ($params) {
- $this->bind_variables = $params[0];
- foreach ($params[0] as &$param) {
- if ($param === false) {
- // For consistency, otherwise true=>'1' and false=>''
- $param = 0;
- }
- }
- }
- $start = microtime(true);
- call_user_func_array(array($this->pdostatement, $function), $params);
- $end = microtime(true);
- $this->executed = !preg_match('/^(insert|delete|update|create|alter) /i', $this->pdostatement->queryString);
- WT_DB::logQuery($this->pdostatement->queryString, $this->pdostatement->rowCount(), $end - $start, $this->bind_variables);
- return $this;
- }
- case 'fetch':
- case 'fetchColumn':
- case 'fetchObject':
- case 'fetchAll':
- // Automatically execute the query
- if (!$this->executed) {
- $this->execute();
- $this->executed=true;
- }
- // no break;
- default:
- return call_user_func_array(array($this->pdostatement, $function), $params);
+ /**
+ * Close the cursor, and mark it as not-executed, so we can execute
+ * it again (perhaps with different parameters).
+ *
+ * @return void
+ */
+ public function closeCursor() {
+ $this->pdo_statement->closeCursor();
+ $this->executed = false;
+ }
+
+ /**
+ * Fetch the next row from the cursor.
+ *
+ * Execute the query, if necessary. Typically when there are no parameters.
+ *
+ * @param int $fetch_style
+ *
+ * @return stdClass|array|false
+ */
+ public function fetch($fetch_style=PDO::FETCH_OBJ) {
+ if (!$this->executed) {
+ $this->execute();
}
+
+ return $this->pdo_statement->fetch($fetch_style);
}
- //////////////////////////////////////////////////////////////////////////////
- // FUNCTIONALITY ENHANCEMENTS
- //////////////////////////////////////////////////////////////////////////////
+ /**
+ * Fetch all rows from the cursor, and close it.
+ *
+ * Execute the query, if necessary. Typically when there are no parameters.
+ *
+ * @param int $fetch_style
+ *
+ * @return stdClass|array|null
+ */
+ public function fetchAll($fetch_style=PDO::FETCH_OBJ) {
+ if (!$this->executed) {
+ $this->execute();
+ }
+
+ $rows = $this->pdo_statement->fetchAll($fetch_style);
+ $this->closeCursor();
- // Fetch one row, and close the cursor. e.g. SELECT * FROM foo WHERE pk=bar
+ return $rows === false ? null : $rows;
+ }
+
+ /**
+ * Fetch one row, and close the cursor. e.g. SELECT * FROM foo WHERE pk=bar
+ *
+ * Execute the query, if necessary. Typically when there are no parameters.
+ *
+ * @param int $fetch_style
+ *
+ * @return stdClass|array|null
+ */
public function fetchOneRow($fetch_style=PDO::FETCH_OBJ) {
if (!$this->executed) {
$this->execute();
}
- $row=$this->pdostatement->fetch($fetch_style);
- $this->pdostatement->closeCursor();
- $this->executed=false;
- return $row ? $row : null;
+
+ $row = $this->pdo_statement->fetch($fetch_style);
+ $this->closeCursor();
+
+ return $row === false ? null : $row;
}
- // Fetch one value and close the cursor. e.g. SELECT MAX(foo) FROM bar
+ /**
+ * Fetch one value and close the cursor. e.g. SELECT MAX(foo) FROM bar
+ *
+ * Execute the query, if necessary. Typically when there are no parameters.
+ *
+ * @return string|null
+ */
public function fetchOne() {
if (!$this->executed) {
$this->execute();
}
- $value=$this->pdostatement->fetchColumn();
- $this->pdostatement->closeCursor();
- $this->executed=false;
- return $value===false ? null : $value;
+
+ $value=$this->pdo_statement->fetchColumn();
+ $this->closeCursor();
+
+ return $value === false ? null : $value;
}
- // Fetch two columns, and return an associative array of col1=>col2
+ /**
+ * Fetch two columns, and return an associative array of col1=>col2
+ *
+ * Execute the query, if necessary. Typically when there are no parameters.
+ *
+ * @return string[]
+ */
public function fetchAssoc() {
if (!$this->executed) {
$this->execute();
}
- $rows=array();
- while ($row=$this->pdostatement->fetch(PDO::FETCH_NUM)) {
- $rows[$row[0]]=$row[1];
+
+ $rows = array();
+ while ($row = $this->pdo_statement->fetch(PDO::FETCH_NUM)) {
+ $rows[$row[0]] = $row[1];
}
- $this->pdostatement->closeCursor();
- $this->executed=false;
+ $this->closeCursor();
+
return $rows;
}
- // Fetch all the first column, as an array
+ /**
+ * Fetch all the first column, as an array.
+ *
+ * Execute the query, if necessary. Typically when there are no parameters.
+ *
+ * @return string[]
+ */
public function fetchOneColumn() {
if (!$this->executed) {
$this->execute();
}
- $list=array();
- while ($row=$this->pdostatement->fetch(PDO::FETCH_NUM)) {
- $list[]=$row[0];
+
+ $list = array();
+ while ($row = $this->pdo_statement->fetch(PDO::FETCH_NUM)) {
+ $list[] = $row[0];
}
- $this->pdostatement->closeCursor();
- $this->executed=false;
+ $this->closeCursor();
+
return $list;
}
}