Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PDO::FETCH_OBJ for select #1028

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 102 additions & 25 deletions src/Medoo.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ class Medoo
*/
public $type;

/**
* The type of select to be return.
*
* @var int
*/
protected $returnType = PDO::FETCH_ASSOC;
/**
* Define whether select return type is object or not
*
* @var bool
*/
private $isReturnObject = false;

/**
* Table prefix.
*
Expand Down Expand Up @@ -471,6 +484,20 @@ public function __construct(array $options)
}
}

public function setReturnType($type){

switch($type){
case PDO::FETCH_OBJ:
$this->isReturnObject = true;
$this->returnType = PDO::FETCH_OBJ;
break;
case PDO::FETCH_ASSOC:
$this->isReturnObject = false;
$this->returnType = PDO::FETCH_ASSOC;
break;
}

}
/**
* Generate a new map key for placeholder.
*
Expand Down Expand Up @@ -1359,20 +1386,24 @@ protected function columnMap($columns, array &$stack, bool $root): array
* @return void
*/
protected function dataMap(
array $data,
$data,
array $columns,
array $columnMap,
array &$stack,
&$stack,
bool $root,
array &$result = null
): void {

if ($root) {
$columnsKey = array_keys($columns);

if (count($columnsKey) === 1 && is_array($columns[$columnsKey[0]])) {

$indexKey = array_keys($columns)[0];
$dataKey = preg_replace("/^(?![_\d])[\p{N}\p{L}\-_]+\./u", "", $indexKey);
$currentStack = [];
if($this->isReturnObject)
$currentStack = (object)[];
else
$currentStack = [];

foreach ($data as $item) {
$this->dataMap($data, $columns[$indexKey], $columnMap, $currentStack, false, $result);
Expand All @@ -1381,13 +1412,19 @@ protected function dataMap(
if (isset($result)) {
$result[$index] = $currentStack;
} else {
$stack[$index] = $currentStack;
if($this->isReturnObject)
$stack->$index = $currentStack;
else
$stack[$index] = $currentStack;
}
}
} else {
$currentStack = [];
if($this->isReturnObject)
$currentStack = (object)[];
else
$currentStack = [];
$this->dataMap($data, $columns, $columnMap, $currentStack, false, $result);

if (isset($result)) {
$result[] = $currentStack;
} else {
Expand All @@ -1400,56 +1437,93 @@ protected function dataMap(

foreach ($columns as $key => $value) {
$isRaw = $this->isRaw($value);

if (is_int($key) || $isRaw) {
$map = $columnMap[$isRaw ? $key : $value];

$columnKey = $map[0];
$item = $data[$columnKey];
//check if its object
if($this->isReturnObject)
$item = $data->$columnKey;
else
$item = $data[$columnKey];


if (isset($map[1])) {
if ($isRaw && in_array($map[1], ['Object', 'JSON'])) {
continue;
}

if (is_null($item)) {
$stack[$columnKey] = null;
if($this->isReturnObject)
$stack->$columnKey = null;
else
$stack[$columnKey] = null;
continue;
}

switch ($map[1]) {

case 'Number':
$stack[$columnKey] = (float) $item;
if($this->isReturnObject)
$stack->$columnKey = (float) $item;
else
$stack[$columnKey] = (float) $item;
break;

case 'Int':
$stack[$columnKey] = (int) $item;
if($this->isReturnObject)
$stack->$columnKey = (int) $item;
else
$stack[$columnKey] = (int) $item;
break;

case 'Bool':
$stack[$columnKey] = (bool) $item;
if($this->isReturnObject)
$stack->$columnKey = (bool) $item;
else
$stack[$columnKey] = (bool) $item;
break;

case 'Object':
$stack[$columnKey] = unserialize($item);
if($this->isReturnObject)
$stack->$columnKey = unserialize($item);
else
$stack[$columnKey] = unserialize($item);
break;

case 'JSON':
$stack[$columnKey] = json_decode($item, true);
if($this->isReturnObject)
$stack->$columnKey = json_decode($item, true);
else
$stack[$columnKey] = json_decode($item, true);
break;

case 'String':
$stack[$columnKey] = $item;
if($this->isReturnObject)
$stack->$columnKey = $item;
else
$stack[$columnKey] = $item;

break;
}
} else {
$stack[$columnKey] = $item;

if($this->isReturnObject)
$stack->$columnKey = $item;
else
$stack[$columnKey] = $item;
}
} else {
$currentStack = [];

if($this->isReturnObject)
$currentStack = (object)[];
else
$currentStack = [];
$this->dataMap($data, $value, $columnMap, $currentStack, false, $result);

$stack[$key] = $currentStack;
if($this->isReturnObject)
$stack->$key = $currentStack;
else
$stack[$key] = $currentStack;
}
}
}
Expand Down Expand Up @@ -1548,18 +1622,21 @@ public function select(string $table, $join, $columns = null, $where = null): ?a
// @codeCoverageIgnoreStart
if ($columns === '*') {
if (isset($callback)) {
while ($data = $statement->fetch(PDO::FETCH_ASSOC)) {
while ($data = $statement->fetch($this->returnType)) {
$callback($data);
}

return null;
}

return $statement->fetchAll(PDO::FETCH_ASSOC);
return $statement->fetchAll($this->returnType);
}

while ($data = $statement->fetch(PDO::FETCH_ASSOC)) {
$currentStack = [];
while ($data = $statement->fetch($this->returnType)) {
if($this->isReturnObject)
$currentStack = (object)[];
else
$currentStack = [];

if (isset($callback)) {
$this->dataMap($data, $columns, $columnMap, $currentStack, true);
Expand Down Expand Up @@ -1835,7 +1912,7 @@ public function get(string $table, $join = null, $columns = null, $where = null)
}

// @codeCoverageIgnoreStart
$data = $query->fetchAll(PDO::FETCH_ASSOC);
$data = $query->fetchAll($this->returnType);

if (isset($data[0])) {
if ($column === '*') {
Expand Down