Skip to content

Commit

Permalink
PdoResultDriver::getColumns() [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 5, 2023
1 parent 4679cd6 commit 8e68d12
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Database/Drivers/MySqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,40 @@ public function getColumnTypes(\PDOStatement $statement): array
}


/** @return Reflection\Column */
public function getResultColumns(\PDOStatement $statement): array

Check failure on line 208 in src/Database/Drivers/MySqlDriver.php

View workflow job for this annotation

GitHub Actions / PHPStan

PHPDoc tag @return with type Nette\Database\Reflection\Column is incompatible with native type array.
{
$columns = [];
$count = $statement->columnCount();
for ($col = 0; $col < $count; $col++) {
$meta = $statement->getColumnMeta($col);
$type = Nette\Database\IStructure::FIELD_TEXT; // nebo null?
if (isset($meta['native_type'])) {
$type = Nette\Database\Helpers::detectType($meta['native_type']);
if ($type === Nette\Database\IStructure::FIELD_TIME) {
$type = Nette\Database\IStructure::FIELD_TIME_INTERVAL;
} elseif ($type === Nette\Database\IStructure::FIELD_FLOAT && $meta['precision'] === 0) {
$type = Nette\Database\IStructure::FIELD_INTEGER;
}
}
$columns[] = new Reflection\Column(
name: $meta['name'],
table: null,
nativeType: $meta['native_type'],
type: $type,
size: null,
nullable: null,
default: null,
autoIncrement: null,
primary: null,
vendor: $meta,
);
}

return $columns;
}


public function isSupported(string $item): bool
{
// MULTI_COLUMN_AS_OR_COND due to mysql bugs:
Expand Down
5 changes: 5 additions & 0 deletions src/Database/Drivers/PdoDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Nette;
use Nette\Database\DriverException;
use Nette\Database\Reflection;
use PDO;
use PDOException;

Expand Down Expand Up @@ -142,4 +143,8 @@ public function detectExceptionClass(\PDOException $e): ?string
{
return null;
}


/** @return Reflection\Column[] */
abstract public function getResultColumns(\PDOStatement $statement): array;
}
10 changes: 10 additions & 0 deletions src/Database/Drivers/PdoResultDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Database\Drivers;

use Nette;
use Nette\Database\Reflection;


/**
Expand Down Expand Up @@ -53,14 +54,23 @@ public function getRowCount(): int
}


/** @return Reflection\Column[] */
public function getColumns(): array
{
return $this->driver->getResultColumns($this->result);
}


public function getColumnTypes(): array
{
// zrusit
return $this->driver->getColumnTypes($this->result);
}


public function getColumnMeta(int $col): array
{
// zrusit
return $this->result->getColumnMeta($col);
}

Expand Down
9 changes: 9 additions & 0 deletions src/Database/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,17 @@ public function getRowCount(): ?int
}


/** @return Reflection\Column[] */
public function getColumns(): array
{
// add cache per connection & query, kontrolovat limit, dat moznost vypnout
return $this->result->getColumns();
}


public function getColumnTypes(): array
{
// zrusit
$this->types ??= $this->result->getColumnTypes();
return $this->types;
}
Expand Down

0 comments on commit 8e68d12

Please sign in to comment.