From 1cd4fe8a9a278e2a11d12f32eb483f0a29fc44b1 Mon Sep 17 00:00:00 2001 From: mirzaev Date: Fri, 31 Jan 2025 11:54:33 +0700 Subject: [PATCH] fixed type::char and reading empty records --- .editorconfig | 0 .gitignore | 0 mirzaev/baza/system/column.php | 0 mirzaev/baza/system/database.php | 31 ++++++++++--------- mirzaev/baza/system/enumerations/encoding.php | 0 mirzaev/baza/system/enumerations/type.php | 4 +-- mirzaev/baza/system/record.php | 0 mirzaev/baza/tests/.gitignore | 2 +- mirzaev/baza/tests/record.php | 22 ++++++++++--- 9 files changed, 37 insertions(+), 22 deletions(-) mode change 100644 => 100755 .editorconfig mode change 100644 => 100755 .gitignore mode change 100644 => 100755 mirzaev/baza/system/column.php mode change 100644 => 100755 mirzaev/baza/system/database.php mode change 100644 => 100755 mirzaev/baza/system/enumerations/encoding.php mode change 100644 => 100755 mirzaev/baza/system/enumerations/type.php mode change 100644 => 100755 mirzaev/baza/system/record.php mode change 100644 => 100755 mirzaev/baza/tests/.gitignore mode change 100644 => 100755 mirzaev/baza/tests/record.php diff --git a/.editorconfig b/.editorconfig old mode 100644 new mode 100755 diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/mirzaev/baza/system/column.php b/mirzaev/baza/system/column.php old mode 100644 new mode 100755 diff --git a/mirzaev/baza/system/database.php b/mirzaev/baza/system/database.php old mode 100644 new mode 100755 index 1ee2ac3..94e3290 --- a/mirzaev/baza/system/database.php +++ b/mirzaev/baza/system/database.php @@ -48,7 +48,7 @@ class database * * @var string $database Path to the database file */ - public protected(set) string $database = __DIR__ . DIRECTORY_SEPARATOR . 'database.ba'; + public protected(set) string $database = __DIR__ . DIRECTORY_SEPARATOR . 'database.baza'; /** * Backups @@ -259,20 +259,20 @@ class database */ public function unpack(array $binaries): record { - if (count($binaries) === count($this->columns)) { - // Amount of binery values matches amount of columns - // Declaring the buffer of unpacked values $unpacked = []; - foreach (array_combine($binaries, $this->columns) as $binary => $column) { + foreach ($this->columns as $index => $column) { // Iterating over columns + + // Initializing link to the binary value + $binary = $binaries[$index] ?? null; if ($column->type === type::string) { // String // Unpacking the value - $value = unpack($column->type->value . $column->length, $binary)[1]; + $value = unpack($column->type->value . $column->length, $binary ?? str_repeat("\0", $column->length))[1]; // Deleting NULL-characters $unnulled = str_replace("\0", '', $value); @@ -286,7 +286,7 @@ class database // Other types // Writing into the buffer of readed values - $unpacked[] = unpack($column->type->value, $binary)[1]; + $unpacked[] = unpack($column->type->value, $binary ?? "\0")[1]; } } @@ -295,12 +295,6 @@ class database // Exit (success) return $record; - } else { - // Amount of binery values not matches amount of columns - - // Exit (fail) - throw new exception_invalid_argument('Amount of binary values not matches amount of columns'); - } } /** @@ -377,7 +371,7 @@ class database public function read(?callable $filter = null, ?callable $update = null, bool $delete = false, int $amount = 1, int $offset = 0): ?array { // Opening the database file - $file = fopen($this->database, 'r+b'); + $file = fopen($this->database, 'c+b'); if (flock($file, LOCK_EX)) { // The file was locked @@ -408,6 +402,9 @@ class database // Reading the binary value from the database $binaries[] = fread($file, $column->type->size()); } + + // Terminate loop when end of file is reached + /* if (feof($file)) break 2; */ } // Terminate loop when end of file is reached @@ -417,6 +414,9 @@ class database // Unpacking the record $record = $this->unpack($binaries); + if ((bool) array_filter($record->values())) { + // The record contains at least one non-empty value + if (is_null($filter) || $filter($record, $records)) { // Passed the filter @@ -460,6 +460,9 @@ class database --$amount; } } +} else { + // The record contains only empty values +} } catch (exception_logic | exception_invalid_argument $exception) { // Writing into the buffer of failed to reading records /* $failed[] = $record; */ diff --git a/mirzaev/baza/system/enumerations/encoding.php b/mirzaev/baza/system/enumerations/encoding.php old mode 100644 new mode 100755 diff --git a/mirzaev/baza/system/enumerations/type.php b/mirzaev/baza/system/enumerations/type.php old mode 100644 new mode 100755 index a3485eb..4e62b46 --- a/mirzaev/baza/system/enumerations/type.php +++ b/mirzaev/baza/system/enumerations/type.php @@ -45,8 +45,8 @@ enum type: string { // Exit (success) return match ($this) { - type::char, type::string, type::short => 'string', - type::char_unsigned, type::short_unsigned, type::integer, type::integer_unsigned, type::long, type::long_unsigned, type::long_long, type::long_long_unsigned => 'integer', + type::string, type::short => 'string', + type::char, type::char_unsigned, type::short_unsigned, type::integer, type::integer_unsigned, type::long, type::long_unsigned, type::long_long, type::long_long_unsigned => 'integer', type::float, type::double => 'double', type::null => 'NULL', default => throw new exception_unexpected_value('Not found the type') diff --git a/mirzaev/baza/system/record.php b/mirzaev/baza/system/record.php old mode 100644 new mode 100755 diff --git a/mirzaev/baza/tests/.gitignore b/mirzaev/baza/tests/.gitignore old mode 100644 new mode 100755 index ed041f1..3fed9a6 --- a/mirzaev/baza/tests/.gitignore +++ b/mirzaev/baza/tests/.gitignore @@ -1 +1 @@ -temporary +temporary/* diff --git a/mirzaev/baza/tests/record.php b/mirzaev/baza/tests/record.php old mode 100644 new mode 100755 index 94ba81a..eaf27e3 --- a/mirzaev/baza/tests/record.php +++ b/mirzaev/baza/tests/record.php @@ -6,11 +6,18 @@ use mirzaev\baza\database, mirzaev\baza\enumerations\encoding, mirzaev\baza\enumerations\type; +// Initializing path to the composer loader file (main project) +$autoload = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; + +// Reinitializing path to the composer loaded file (depencendy project) +if (!file_exists($autoload)) + $autoload = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'autoload.php'; + // Importing files of thr project and dependencies -require(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'); +require($autoload); // Initializing path to the database file -$file = __DIR__ . DIRECTORY_SEPARATOR . 'temporary' . DIRECTORY_SEPARATOR . 'database.ba'; +$file = __DIR__ . DIRECTORY_SEPARATOR . 'temporary' . DIRECTORY_SEPARATOR . 'database.baza'; echo "Started testing\n\n\n"; @@ -37,9 +44,10 @@ $database = (new database()) new column('name', type::string, ['length' => 32]), new column('second_name', type::string, ['length' => 64]), new column('age', type::integer), - new column('height', type::float) + new column('height', type::float), + new column('active', type::char) ) - ->connect(__DIR__ . DIRECTORY_SEPARATOR . 'temporary' . DIRECTORY_SEPARATOR . 'database.ba'); + ->connect($file); echo '[' . ++$action . "] Initialized the database\n"; @@ -48,7 +56,8 @@ $record = $database->record( 'Arsen', 'Mirzaev', 24, - 165.5 + 165.5, + 1 ); echo '[' . ++$action . "] Initialized the record\n"; @@ -60,6 +69,7 @@ echo '[' . ++$action . '][' . ++$test . '][' . ($record->name === 'Arsen' ? 'SUC echo '[' . $action . '][' . ++$test . '][' . ($record->second_name === 'Mirzaev' ? 'SUCCESS' : 'FAIL') . "][\"second_name\"] Expected: \"Mirzaev\" (string). Actual: \"$record->second_name\" (" . gettype($record->second_name) . ")\n"; echo '[' . $action . '][' . ++$test . '][' . ($record->age === 24 ? 'SUCCESS' : 'FAIL') . "][\"age\"] Expected: \"24\" (integer). Actual: \"$record->age\" (" . gettype($record->age) . ")\n"; echo '[' . $action . '][' . ++$test . '][' . ($record->height === 165.5 ? 'SUCCESS' : 'FAIL') . "][\"height\"] Expected: \"165.5\" (double). Actual: \"$record->height\" (" . gettype($record->height) . ")\n"; +echo '[' . $action . '][' . ++$test . '][' . ($record->active === 1 ? 'SUCCESS' : 'FAIL') . "][\"active\"] Expected: \"1\" (integer). Actual: \"$record->active\" (" . gettype($record->active) . ")\n"; echo '[' . $action . "] The record parameters checks have been completed\n"; @@ -77,6 +87,7 @@ $record_ivan = $database->record( 'Ivanov', 24, (float) 210, + 0 ); echo '[' . ++$action . "] Initialized the record\n"; @@ -92,6 +103,7 @@ $record_ivan = $database->record( 'Esenina', 19, (float) 165, + 1 ); echo '[' . ++$action . "] Initialized the record\n";