Support Doctrine 3

This commit is contained in:
Marcin Łojewski
2021-03-24 11:40:46 +01:00
parent 09d710fc84
commit 49d1c76a61
4 changed files with 53 additions and 29 deletions

View File

@@ -2,7 +2,7 @@
/**
* Nextcloud - user_sql
*
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
* @copyright 2021 Marcin Łojewski <dev@mlojewski.me>
* @author Marcin Łojewski <dev@mlojewski.me>
*
* This program is free software: you can redistribute it and/or modify
@@ -22,6 +22,7 @@
namespace OCA\UserSQL\Query;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Exception as DBALException;
use OC\DB\Connection;
use OC\DB\ConnectionFactory;
use OCA\UserSQL\Constant\DB;
@@ -119,16 +120,17 @@ class DataQuery
["app" => $this->appName]
);
if ($result->execute() !== true) {
$error = $result->errorInfo();
try {
$result = $result->execute();
return $result;
} catch (DBALException $exception) {
$this->logger->error(
"Could not execute the query: " . implode(", ", $error),
"Could not execute the query: " . $exception->getMessage(),
["app" => $this->appName]
);
return false;
}
return $result;
}
/**
@@ -150,12 +152,15 @@ class DataQuery
);
if ($this->properties[DB::DRIVER] == 'mysql') {
if ($this->properties[DB::SSL_CA])
$parameters["driverOptions"][\PDO::MYSQL_ATTR_SSL_CA] = \OC::$SERVERROOT.'/'.$this->properties[DB::SSL_CA];
if ($this->properties[DB::SSL_CERT])
$parameters["driverOptions"][\PDO::MYSQL_ATTR_SSL_CERT] = \OC::$SERVERROOT.'/'.$this->properties[DB::SSL_CERT];
if ($this->properties[DB::SSL_KEY])
$parameters["driverOptions"][\PDO::MYSQL_ATTR_SSL_KEY] = \OC::$SERVERROOT.'/'.$this->properties[DB::SSL_KEY];
if ($this->properties[DB::SSL_CA]) {
$parameters["driverOptions"][\PDO::MYSQL_ATTR_SSL_CA] = \OC::$SERVERROOT . '/' . $this->properties[DB::SSL_CA];
}
if ($this->properties[DB::SSL_CERT]) {
$parameters["driverOptions"][\PDO::MYSQL_ATTR_SSL_CERT] = \OC::$SERVERROOT . '/' . $this->properties[DB::SSL_CERT];
}
if ($this->properties[DB::SSL_KEY]) {
$parameters["driverOptions"][\PDO::MYSQL_ATTR_SSL_KEY] = \OC::$SERVERROOT . '/' . $this->properties[DB::SSL_KEY];
}
}
$this->connection = $connectionFactory->getConnection(
@@ -185,7 +190,7 @@ class DataQuery
return false;
}
$row = $result->fetch(\PDO::FETCH_COLUMN);
$row = $result->fetchOne();
if ($row === false) {
return $failure;
}
@@ -211,8 +216,7 @@ class DataQuery
return false;
}
$column = $result->fetchAll(\PDO::FETCH_COLUMN);
return $column;
return $result->fetchFirstColumn();
}
/**
@@ -232,8 +236,7 @@ class DataQuery
return false;
}
$result->setFetchMode(\PDO::FETCH_CLASS, $entityClass);
$entity = $result->fetch();
$entity = $result->fetchAssociative();
if ($entity === false) {
return null;
@@ -247,7 +250,16 @@ class DataQuery
return null;
}
return $entity;
return self::arrayToObject($entity, $entityClass);
}
private function arrayToObject($array, $entityClass)
{
$object = new $entityClass();
foreach ($array as $name => $value) {
$object->$name = $array[$name];
}
return $object;
}
/**
@@ -269,9 +281,15 @@ class DataQuery
return false;
}
$result->setFetchMode(\PDO::FETCH_CLASS, $entityClass);
$entities = $result->fetchAll();
return self::iterableToObjectArray($result->iterateAssociative(), $entityClass);
}
return $entities;
private function iterableToObjectArray($array, $entityClass)
{
$result = array();
foreach ($array as $element) {
$result[] = self::arrayToObject($element, $entityClass);
}
return $result;
}
}