web-base/core/Api/ExecuteSelect.class.php

110 lines
3.0 KiB
PHP
Raw Normal View History

2020-02-09 23:02:19 +01:00
<?php
namespace Api;
use Api\Parameter\Parameter;
use Api\Parameter\StringType;
class ExecuteSelect extends Request {
public function __construct($user, $externCall = false) {
parent::__construct($user, $externCall, array(
'query' => new StringType('query')
));
$this->isPublic = false;
$this->variableParamCount = true;
}
2020-02-10 00:52:25 +01:00
public function execute($values = array()) {
if(!parent::execute($values)) {
2020-02-09 23:02:19 +01:00
return false;
}
2020-02-10 00:52:25 +01:00
$sql = $this->user->getSQL();
2020-02-09 23:02:19 +01:00
$this->success = false;
$this->result['rows'] = array();
if(count($this->params) === 1) {
2020-02-10 00:52:25 +01:00
$res = $sql->query($this->getParam('query'));
2020-02-09 23:02:19 +01:00
if(!$res) {
2020-02-10 00:52:25 +01:00
$this->lastError = 'Database Error: query() failed with ' . $sql->getLastError();
2020-02-09 23:02:19 +01:00
return false;
}
while($row = $res->fetch_assoc()) {
array_push($this->result['rows'], $row);
}
$this->success = true;
$res->close();
} else {
$aSqlParams = array('');
foreach($this->params as $param) {
if($param->name === 'query') continue;
$value = $param->value;
switch($param->type) {
case Parameter::TYPE_BOOLEAN:
$value = $param->value ? 1 : 0;
case Parameter::TYPE_INT:
$aSqlParams[0] .= 'i';
break;
case Parameter::TYPE_FLOAT:
$aSqlParams[0] .= 'd';
break;
case Parameter::TYPE_DATE:
$value = $value->format('Y-m-d');
$aSqlParams[0] .= 's';
break;
case Parameter::TYPE_TIME:
$value = $value->format('H:i:s');
$aSqlParams[0] .= 's';
break;
case Parameter::TYPE_DATE_TIME:
$value = $value->format('Y-m-d H:i:s');
$aSqlParams[0] .= 's';
break;
case Parameter::TYPE_EMAIL:
default:
$aSqlParams[0] .= 's';
}
$aSqlParams[] = $value;
}
$tmp = array();
foreach($aSqlParams as $key => $value) $tmp[$key] = &$aSqlParams[$key];
2020-02-10 00:52:25 +01:00
if($stmt = $sql->connection->prepare($this->getParam('query'))) {
2020-02-09 23:02:19 +01:00
if(call_user_func_array(array($stmt, "bind_param"), $tmp))
{
if($stmt->execute()) {
$res = $stmt->get_result();
if($res) {
while($row = $res->fetch_assoc()) {
array_push($this->result['rows'], $row);
}
$res->close();
$this->success = true;
} else {
2020-02-10 00:52:25 +01:00
$this->lastError = 'Database Error: execute() failed with ' . $sql->getLastError();
2020-02-09 23:02:19 +01:00
}
} else {
2020-02-10 00:52:25 +01:00
$this->lastError = 'Database Error: get_result() failed with ' . $sql->getLastError();
2020-02-09 23:02:19 +01:00
}
} else {
2020-02-10 00:52:25 +01:00
$this->lastError = 'Database Error: bind_param() failed with ' . $sql->getLastError();
2020-02-09 23:02:19 +01:00
}
$stmt->close();
} else {
2020-02-10 00:52:25 +01:00
$this->lastError = 'Database Error: prepare failed with() ' . $sql->getLastError();
2020-02-09 23:02:19 +01:00
}
}
return $this->success;
}
};
?>