Commit 149129d2 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Database.php: wrote some phpdoc. renamed function fakeBindParam()

parent d6cc4a77
......@@ -181,12 +181,15 @@ class Database {
}
/**
* Fires query $sql and fetches result als assoc array.
* $mode
* ROW_REGULAR return result set as assoc array. No check about number of rows.
* ROW_IMPLODE_ALL return result set as one string: implode all rows and columns. No check about number of rows.
* ROW_EXACT_1 return result set as assoc array. It has to be exact one row selected. If not: throw Exception,
* ROW_EMPTY_IS_OK return result set as assoc array. It has to be 0 or one row selected. If not: throw Exception,
* Fires query $sql and fetches result als assoc array (all modes but ROW_KEYS) or as num array (mode: ROW_EKYS). Throws exception.
* $mode
* ROW_REGULAR: Return 2-dimensional assoc array. Every query row is one array row.
* ROW_IMPLODE_ALL: Return string. All cells of all rows imploded to one string.
* ROW_EXPECT_0: Return empty string if there is now record row, Else an exception.
* ROW_EXPECT_1: Return 1-dimensional assoc array if there are exact one row. Else an exception.
* ROW_EXPECT_0_1: Return empty string if there is no row. Return 1- dimensional assoc array if there is one row. Else an exception.
* ROW_EXPECT_GE_1: Like 'ROW_REGULAR'. Throws an exception if there is an empty resultset.
* ROW_KEYS: Return 2-dimensional num(!) array. Every query row is one array row. In $keys are the column names.
*
* @param $sql
* @param string $mode
......@@ -280,37 +283,30 @@ class Database {
/**
* Execute a prepared SQL statement like SELECT, INSERT, UPDATE, DELETE, SHOW, ...
*
* The result of the query can be retrieved by using one of the fetch methods.
* Returns the number of selected rows (SELECT, SHOW, ..) or the affected rows (UPDATE) or the last insert id (INSERT)
*
* @param string $sql SQL statement with prepared statement variable.
* @param array $parameterArray parameter array for prepared statement execution.
* @return mixed
* @return int|mixed
* @throws \qfq\CodeException
* @throws \qfq\DbException
* @throws \qfq\UserException
*/
private function prepareExecute($sql, array $parameterArray = array()) {
$result = null;
$result = 0;
$this->store->setVar(SYSTEM_SQL_FINAL, $sql, STORE_SYSTEM);
$this->store->setVar(SYSTEM_SQL_PARAM_ARRAY, $parameterArray, STORE_SYSTEM);
// Logfile
$this->dbLog($sql, $parameterArray);
// Prepared Statement?
// if (count($parameterArray) === 0) {
// if (false === ($result = $this->mysqli->query($sql))) {
// throw new DbException('[ mysqli: ' . $this->mysqli->errno . ' ] ' . $this->mysqli->error, ERROR_DB_QUERY);
// }
//
// } else {
if (false === ($this->mysqli_stmt = $this->mysqli->prepare($sql))) {
throw new DbException('[ mysqli: ' . $this->mysqli->errno . ' ] ' . $this->mysqli->error, ERROR_DB_PREPARE);
}
if (count($parameterArray) > 0) {
if (false === $this->fakeCallUserFunc($parameterArray)) {
if (false === $this->prepareBindParam($parameterArray)) {
throw new DbException('[ mysqli: ' . $this->mysqli_stmt->errno . ' ] ' . $this->mysqli_stmt->error, ERROR_DB_BIND);
}
}
......@@ -318,7 +314,6 @@ class Database {
if (false === $this->mysqli_stmt->execute()) {
throw new DbException('[ mysqli: ' . $this->mysqli_stmt->errno . ' ] ' . $this->mysqli_stmt->error, ERROR_DB_EXECUTE);
}
// }
$msg = '';
$count = 0;
......@@ -390,19 +385,7 @@ class Database {
/**
* @param $arr
*/
private function fakeCallUserFunc($arr) {
// $type = '';
//
// foreach ($arr as $value) {
// if (is_int($value)) {
// $type .= 'i';
// } elseif (is_double($value)) {
// $type .= 'd';
// } else {
// $type .= 's';
// }
// }
private function prepareBindParam($arr) {
$bindParam = new BindParam();
......@@ -410,45 +393,27 @@ class Database {
$bindParam->add($arr[$ii]);
}
call_user_func_array([$this->mysqli_stmt, 'bind_param'], $bindParam->get());
// switch (count($arr)) {
// case 0:
// break;
// case 1:
// $this->mysqli_stmt->bind_param($type, $arr[0]);
// break;
// case 2:
// $this->mysqli_stmt->bind_param($type, $arr[0], $arr[1]);
// break;
// case 3:
// $this->mysqli_stmt->bind_param($type, $arr[0], $arr[1], $arr[2]);
// break;
// case 4:
// $this->mysqli_stmt->bind_param($type, $arr[0], $arr[1], $arr[2], $arr[3]);
// break;
// case 5:
// $this->mysqli_stmt->bind_param($type, $arr[0], $arr[1], $arr[2], $arr[3], $arr[4]);
// break;
// case 6:
// $this->mysqli_stmt->bind_param($type, $arr[0], $arr[1], $arr[2], $arr[3], $arr[4], $arr[5]);
// break;
// case 7:
// $this->mysqli_stmt->bind_param($type, $arr[0], $arr[1], $arr[2], $arr[3], $arr[4], $arr[5], $arr[6]);
// break;
// default:
// throw new DbException('Oops, too many parameter in prepared statement.', 0);
// }
}
/**
* Fetch all rows of the result as associative array.
*
* @return mixed false in case of error.
* mode:
* ROW_IMPLODE_ALL: Return string. All cells of all rows imploded to one string.
* ROW_KEYS: Retrun num array with column names in $keys
* default: Return 2-dimensional assoc array
*
* @param string $mode
* @param array $keys
* @return array|bool|mixed|string false in case of error.
* Empty string is returned if the query didn't yield any rows.
* All rows as Multi Assoc array if $mode!=IMPLODE_ALL.
* All rows and all columns imploded to one string if $mode=IMPLODE_ALL
*
*/
/**
* @return array|bool|mixed|string
*/
private function fetchAll($mode = '', &$keys = array()) {
if ($this->mysqli_result == null || $this->mysqli_result == false) {
return false;
......@@ -482,6 +447,8 @@ class Database {
}
/**
* Wrapper for sql(), to simplyfy access.
*
* @param $sql
* @param array $keys
* @return array|bool
......@@ -498,6 +465,7 @@ class Database {
* @return string
*/
public function getLastInsertId() {
// Do not try to use $this->mysqli->lastInsertId - this is not valid at any given time.
return $this->mysqli->insert_id;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment