Skip to content

Commit

Permalink
#10 Make query to constructor optional
Browse files Browse the repository at this point in the history
Adds errors
Add NULL support for bind_param
Moves assignment of params so it can handle execute without a bind param
  • Loading branch information
sbuberl committed Mar 5, 2019
1 parent 2f0b287 commit d5951a7
Showing 1 changed file with 53 additions and 9 deletions.
62 changes: 53 additions & 9 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,42 @@ class Statement
private $params;
private $result;
private $stored;
private $error;

public function __construct(Environment $environment, $query)
public function __construct(Environment $environment, $query = null)
{
$this->environment = $environment;
$this->prepare($query);
}

public function error()
{
return $this->error;
}

private function set_error($error)
{
$this->error = $error;
return false;
}

public function bind_param($types, ...$params)
{
$this->params = [];
$length = strlen($types);
if($this->query === null) {
return $this->set_error("Unable to perform a bind_param without a prepare");
} if($length != count($params)) {
return $this->set_error("bind_param's number of types in the string doesn't match number of parameters passed in");
}

$param = current($params);

$length = strlen($types);
for($i = 0; $i < $length; ++$i, $param = next($params)) {
$type = $types[$i];
switch($type) {
if($type == null) {
$this->params[] = 'NULL';
} else {
switch($type) {
case 'i':
$this->params[] = (int) $param;
break;
Expand All @@ -37,13 +57,19 @@ public function bind_param($types, ...$params)
case 'o':
$this->params[] = "'". addslashes((string) $param) . "'";
break;
}
}
}

return true;
}

public function execute()
{
if($this->query === null) {
return $this->set_error("Unable to perform an execute without a prepare");
}

$that = $this;
$count = 0;
$newQuery = preg_replace_callback(
Expand All @@ -53,35 +79,53 @@ function($match) use ($that, &$count) { return $that->params[$count++]; },
);
$result = $this->environment->query($newQuery);
if($result instanceof ResultSet) {
$this->result = $result;
return true;
$this->result = $result;
return true;
} else {
return $result;
return $result;
}
}

public function prepare($query)
{
$this->query = $query;
$this->params = [];
$this->result = null;
$this->stored = false;
return true;
}

public function store_result()
{
if($this->query === null) {
return $this->set_error("Unable to perform a store_result without a prepare");
}

$this->stored = true;
return true;
}

public function get_result()
{
if($this->query === null) {
return $this->set_error("Unable to perform a get_result without a prepare");
}

return $this->result;
}

public function result_metadata()
{
return $this->result;
if($this->query === null) {
return $this->set_error("Unable to perform a result_metadata without a prepare");
}

return $this->result;
}

public function free_result()
{

}


}

0 comments on commit d5951a7

Please sign in to comment.