mirror of
https://github.com/standardebooks/web.git
synced 2025-07-06 23:00:28 -04:00
Remove unnecessary test from DbConnection
This commit is contained in:
parent
6a3a5d5ec3
commit
aa8bdfed0e
1 changed files with 76 additions and 77 deletions
|
@ -153,102 +153,100 @@ class DbConnection{
|
||||||
do{
|
do{
|
||||||
try{
|
try{
|
||||||
$columnCount = $handle->columnCount();
|
$columnCount = $handle->columnCount();
|
||||||
if($columnCount > 0){
|
|
||||||
|
|
||||||
$metadata = [];
|
if($columnCount == 0){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
for($i = 0; $i < $columnCount; $i++){
|
$metadata = [];
|
||||||
$metadata[$i] = $handle->getColumnMeta($i);
|
|
||||||
if($metadata[$i] && preg_match('/^(Is|Has|Can)[A-Z]/u', $metadata[$i]['name']) === 1){
|
for($i = 0; $i < $columnCount; $i++){
|
||||||
// MySQL doesn't have a native boolean type, so fake it here if the column
|
$metadata[$i] = $handle->getColumnMeta($i);
|
||||||
// name starts with Is, Has, or Can and is followed by an uppercase letter
|
if($metadata[$i] && preg_match('/^(Is|Has|Can)[A-Z]/u', $metadata[$i]['name']) === 1){
|
||||||
$metadata[$i]['native_type'] = 'BOOL';
|
// MySQL doesn't have a native boolean type, so fake it here if the column
|
||||||
|
// name starts with Is, Has, or Can and is followed by an uppercase letter
|
||||||
|
$metadata[$i]['native_type'] = 'BOOL';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$rows = $handle->fetchAll(\PDO::FETCH_NUM);
|
||||||
|
|
||||||
|
foreach($rows as $row){
|
||||||
|
$object = new $class();
|
||||||
|
|
||||||
|
for($i = 0; $i < $handle->columnCount(); $i++){
|
||||||
|
if($metadata[$i] === false){
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$rows = $handle->fetchAll(\PDO::FETCH_NUM);
|
if($row[$i] === null){
|
||||||
|
$object->{$metadata[$i]['name']} = null;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
switch($metadata[$i]['native_type'] ?? null){
|
||||||
|
case 'DATETIME':
|
||||||
|
case 'TIMESTAMP':
|
||||||
|
$object->{$metadata[$i]['name']} = new DateTimeImmutable($row[$i], new DateTimeZone('UTC'));
|
||||||
|
break;
|
||||||
|
|
||||||
if(!is_array($rows)){
|
case 'LONG':
|
||||||
continue;
|
case 'TINY':
|
||||||
}
|
case 'SHORT':
|
||||||
|
case 'INT24':
|
||||||
|
case 'LONGLONG':
|
||||||
|
$object->{$metadata[$i]['name']} = intval($row[$i]);
|
||||||
|
break;
|
||||||
|
|
||||||
foreach($rows as $row){
|
case 'FLOAT':
|
||||||
$object = new $class();
|
case 'DOUBLE':
|
||||||
|
$object->{$metadata[$i]['name']} = floatval($row[$i]);
|
||||||
|
break;
|
||||||
|
|
||||||
for($i = 0; $i < $handle->columnCount(); $i++){
|
case 'BOOL':
|
||||||
if($metadata[$i] === false){
|
$object->{$metadata[$i]['name']} = $row[$i] == 1 ? true : false;
|
||||||
continue;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
if($row[$i] === null){
|
case 'STRING':
|
||||||
$object->{$metadata[$i]['name']} = null;
|
// We don't check the type VAR_STRING here because in MariaDB, enums are always of type STRING.
|
||||||
}
|
// Since this check is slow, we don't want to run it unnecessarily.
|
||||||
else{
|
if($class == 'stdClass'){
|
||||||
switch($metadata[$i]['native_type'] ?? null){
|
$object->{$metadata[$i]['name']} = $row[$i];
|
||||||
case 'DATETIME':
|
}
|
||||||
case 'TIMESTAMP':
|
else{
|
||||||
$object->{$metadata[$i]['name']} = new DateTimeImmutable($row[$i], new DateTimeZone('UTC'));
|
// If the column is a string and we're filling a typed object, check if the object property is a backed enum. If so, generate it using from(). Otherwise, fill it with a string.
|
||||||
break;
|
// Note: Using ReflectionProperty in this way is pretty slow. Maybe we'll think of a
|
||||||
|
// better way to automatically fill enum types later.
|
||||||
case 'LONG':
|
try{
|
||||||
case 'TINY':
|
$rp = new ReflectionProperty($object, $metadata[$i]['name']);
|
||||||
case 'SHORT':
|
/** @var ?ReflectionNamedType $property */
|
||||||
case 'INT24':
|
$property = $rp->getType();
|
||||||
case 'LONGLONG':
|
if($property !== null){
|
||||||
$object->{$metadata[$i]['name']} = intval($row[$i]);
|
$type = $property->getName();
|
||||||
break;
|
if(is_a($type, 'BackedEnum', true)){
|
||||||
|
$object->{$metadata[$i]['name']} = $type::from($row[$i]);
|
||||||
case 'FLOAT':
|
|
||||||
case 'DOUBLE':
|
|
||||||
$object->{$metadata[$i]['name']} = floatval($row[$i]);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'BOOL':
|
|
||||||
$object->{$metadata[$i]['name']} = $row[$i] == 1 ? true : false;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'STRING':
|
|
||||||
// We don't check the type VAR_STRING here because in MariaDB, enums are always of type STRING.
|
|
||||||
// Since this check is slow, we don't want to run it unnecessarily.
|
|
||||||
if($class == 'stdClass'){
|
|
||||||
$object->{$metadata[$i]['name']} = $row[$i];
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
// If the column is a string and we're filling a typed object, check if the object property is a backed enum. If so, generate it using from(). Otherwise, fill it with a string.
|
|
||||||
// Note: Using ReflectionProperty in this way is pretty slow. Maybe we'll think of a
|
|
||||||
// better way to automatically fill enum types later.
|
|
||||||
try{
|
|
||||||
$rp = new ReflectionProperty($object, $metadata[$i]['name']);
|
|
||||||
/** @var ?ReflectionNamedType $property */
|
|
||||||
$property = $rp->getType();
|
|
||||||
if($property !== null){
|
|
||||||
$type = $property->getName();
|
|
||||||
if(is_a($type, 'BackedEnum', true)){
|
|
||||||
$object->{$metadata[$i]['name']} = $type::from($row[$i]);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
$object->{$metadata[$i]['name']} = $row[$i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
$object->{$metadata[$i]['name']} = $row[$i];
|
$object->{$metadata[$i]['name']} = $row[$i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(\Exception){
|
else{
|
||||||
$object->{$metadata[$i]['name']} = $row[$i];
|
$object->{$metadata[$i]['name']} = $row[$i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
catch(\Exception){
|
||||||
|
$object->{$metadata[$i]['name']} = $row[$i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
$object->{$metadata[$i]['name']} = $row[$i];
|
$object->{$metadata[$i]['name']} = $row[$i];
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$result[] = $object;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$result[] = $object;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(\PDOException $ex){
|
catch(\PDOException $ex){
|
||||||
|
@ -258,7 +256,8 @@ class DbConnection{
|
||||||
throw $ex;
|
throw $ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}while($handle->nextRowset());
|
}
|
||||||
|
while($handle->nextRowset());
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue