diff --git a/src/Exceptions/InvalidReturnTypeException.php b/src/Exceptions/InvalidReturnTypeException.php new file mode 100644 index 0000000..969393c --- /dev/null +++ b/src/Exceptions/InvalidReturnTypeException.php @@ -0,0 +1,47 @@ +previous = $previous; + } + + /** + * Get the previous exception. + * + * Returns the previous exception if it exists, or null if there is no previous exception. + * + * @return Throwable|null The previous exception + */ + public function getPreviousException() + { + return $this->previous; + } +} diff --git a/src/MagicObject.php b/src/MagicObject.php index 9f7673d..fac9e42 100644 --- a/src/MagicObject.php +++ b/src/MagicObject.php @@ -17,10 +17,10 @@ use MagicObject\Database\PicoSortable; use MagicObject\Database\PicoSpecification; use MagicObject\Database\PicoTableInfo; -use MagicObject\DataLabel\PicoDataLabel; use MagicObject\Exceptions\FindOptionException; use MagicObject\Exceptions\InvalidAnnotationException; use MagicObject\Exceptions\InvalidQueryInputException; +use MagicObject\Exceptions\InvalidReturnTypeException; use MagicObject\Exceptions\NoDatabaseConnectionException; use MagicObject\Exceptions\NoRecordFoundException; use MagicObject\Util\ClassUtil\PicoAnnotationParser; @@ -671,7 +671,8 @@ public function selectQuery() * - instances of a specified class if the return type matches a class name. * * @throws PDOException If there is an error executing the database query. - * @throws InvalidQueryInputException If there is no query to be executed on the database query. + * @throws InvalidQueryInputException If there is no query to be executed. + * @throws InvalidReturnTypeException If the return type specified is invalid. */ protected function executeNativeQuery() //NOSONAR { @@ -696,26 +697,21 @@ protected function executeNativeQuery() //NOSONAR $queryString = trim($queryString, " \r\n\t "); if(empty($queryString)) { - throw new InvalidQueryInputException("No query found.\r\n".$docComment); + // Try reading the query in another way + preg_match('/@query\s*\(\s*"(.*?)"\s*\)/s', $docComment, $matches); + $queryString = $matches ? $matches[1] : ''; + if(empty($queryString)) + { + throw new InvalidQueryInputException("No query found.\r\n".$docComment); + } } // Get parameter information from the caller function $callerParams = $reflection->getParameters(); // Get return type from the caller function - $returnTypeObj = $reflection->getReturnType(); - - if($returnTypeObj == null) - { - // PHP 5 - preg_match('/@return\s+([^\s]+)/', $docComment, $matches); - $returnType = $matches ? $matches[1] : ''; - } - else - { - // PHP 7 or above - $returnType = $returnTypeObj.""; - } + preg_match('/@return\s+([^\s]+)/', $docComment, $matches); + $returnType = $matches ? $matches[1] : 'void'; // Trim return type $returnType = trim($returnType); @@ -797,35 +793,52 @@ protected function executeNativeQuery() //NOSONAR return json_encode($stmt->fetchAll(PDO::FETCH_OBJ)); } else { try { - // Check for array-type hinting in the return type + // Check for array-type hinting in the return type if (stripos($returnType, "[") !== false) { - $className = trim(explode("[", $returnType)[0]); - if (class_exists($className)) { + $className = trim(explode("[", $returnType)[0]); + if ($className == "stdClass") { + // Return all rows as stdClass objects + return $stmt->fetchAll(PDO::FETCH_OBJ); + } + else if($className == 'MagicObject') { + $result = $stmt->fetchAll(PDO::FETCH_OBJ); $ret = []; - if ($className == "stdClass") { - // Return all rows as stdClass objects - return $stmt->fetchAll(PDO::FETCH_OBJ); - } else { - // Map result rows to the specified class + foreach ($result as $row) { + $ret[] = new MagicObject($row); + } + return $ret; + } + else if (class_exists($className)) { + // Map result rows to the specified class + $obj = new $className(); + if($obj instanceof MagicObject) { $result = $stmt->fetchAll(PDO::FETCH_OBJ); foreach ($result as $row) { $ret[] = new $className($row); } return $ret; - } - } + } + } + throw new InvalidReturnTypeException("Invalid return type for $className"); } else { // Return a single object of the specified class $className = trim($returnType); - if (class_exists($className)) { - $row = $stmt->fetch(PDO::FETCH_OBJ); - return new $className($row); + if($className == 'MagicObject') { + $row = $stmt->fetch(PDO::FETCH_OBJ); + return new MagicObject($row); + } + else if (class_exists($className)) { + $obj = new $className(); + if($obj instanceof MagicObject) { + $row = $stmt->fetch(PDO::FETCH_OBJ); + return $obj->loadData($row); + } } + throw new InvalidReturnTypeException("Invalid return type for $className"); } } catch (Exception $e) { // Log the exception if the class is not found - error_log('Class not found: ' . $e->getMessage()); - return null; + throw new InvalidReturnTypeException("Invalid return type for $className"); } } } diff --git a/tests/caller.php b/tests/caller.php index a934c2c..ce52c94 100644 --- a/tests/caller.php +++ b/tests/caller.php @@ -7,7 +7,7 @@ require_once dirname(__DIR__) . "/vendor/autoload.php"; $databaseCredential = new SecretObject(); -$databaseCredential->loadYamlFile(dirname(dirname(__DIR__)) . "/test.yml", false, true, true); +$databaseCredential->loadYamlFile(dirname(dirname(__DIR__)) . "/test.yml.txt", false, true, true); $databaseCredential->getDatabase()->setDatabaseName("sipro"); $database = new PicoDatabase($databaseCredential->getDatabase(), null, function($sql){ echo $sql.";\r\n\r\n"; @@ -254,9 +254,9 @@ public function native11($supervisorId, $aktif) * * @param int $supervisorId The ID of the table to search for. * @param DateTime[] $timeCreate The date and time when data is created - * @return self[] + * @return MagicObject * @query(" - SELECT supervisor.* + SELECT supervisor.* FROM supervisor WHERE supervisor.waktu_buat in :timeCreate AND supervisor.aktif = :aktif @@ -271,7 +271,7 @@ public function native13($timeCreate, $aktif) $obj = new Supervisor(null, $database); - +/* $native1 = $obj->native1(1, true); $native2 = $obj->native2(1, true); @@ -321,7 +321,15 @@ public function native13($timeCreate, $aktif) echo "Alamat: " . $native9[0]->getTelepon() . "\r\n"; echo "Alamat: " . $native10->getTelepon() . "\r\n"; echo "Alamat: " . $native11[0]->getTelepon() . "\r\n"; +*/ -$native13 = $obj->native13([new DateTime('2023-03-27 15:23:47', new DateTimeZone($databaseCredential->getDatabase()->getTimeZone()))], true); +try +{ +$native13 = $obj->native13([new DateTime('2025-03-27 15:23:47', new DateTimeZone($databaseCredential->getDatabase()->getTimeZone()))], true); echo "\r\nnative13:\r\n"; -print_r($native13); \ No newline at end of file +echo ($native13); +} +catch(Exception $e) +{ + echo $e->getMessage(); +} \ No newline at end of file