Compare commits
No commits in common. "stable" and "3.2.0" have entirely different histories.
|
@ -259,42 +259,42 @@ class database
|
||||||
*/
|
*/
|
||||||
public function unpack(array $binaries): record
|
public function unpack(array $binaries): record
|
||||||
{
|
{
|
||||||
// Declaring the buffer of unpacked values
|
// Declaring the buffer of unpacked values
|
||||||
$unpacked = [];
|
$unpacked = [];
|
||||||
|
|
||||||
foreach ($this->columns as $index => $column) {
|
foreach ($this->columns as $index => $column) {
|
||||||
// Iterating over columns
|
// Iterating over columns
|
||||||
|
|
||||||
|
// Initializing link to the binary value
|
||||||
|
$binary = $binaries[$index] ?? null;
|
||||||
|
|
||||||
// Initializing link to the binary value
|
if ($column->type === type::string) {
|
||||||
$binary = $binaries[$index] ?? null;
|
// String
|
||||||
|
|
||||||
if ($column->type === type::string) {
|
// Unpacking the value
|
||||||
// String
|
$value = unpack($column->type->value . $column->length, $binary ?? str_repeat("\0", $column->length))[1];
|
||||||
|
|
||||||
// Unpacking the value
|
// Deleting NULL-characters
|
||||||
$value = unpack($column->type->value . $column->length, $binary ?? str_repeat("\0", $column->length))[1];
|
$unnulled = str_replace("\0", '', $value);
|
||||||
|
|
||||||
// Deleting NULL-characters
|
// Encoding the unpacked value
|
||||||
$unnulled = str_replace("\0", '', $value);
|
$encoded = mb_convert_encoding($unnulled, $this->encoding->value);
|
||||||
|
|
||||||
// Encoding the unpacked value
|
// Writing into the buffer of readed values
|
||||||
$encoded = mb_convert_encoding($unnulled, $this->encoding->value);
|
$unpacked[] = $encoded;
|
||||||
|
} else {
|
||||||
|
// Other types
|
||||||
|
|
||||||
// Writing into the buffer of readed values
|
// Writing into the buffer of readed values
|
||||||
$unpacked[] = $encoded;
|
$unpacked[] = unpack($column->type->value, $binary ?? "\0")[1];
|
||||||
} else {
|
}
|
||||||
// Other types
|
|
||||||
|
|
||||||
// Writing into the buffer of readed values
|
|
||||||
$unpacked[] = unpack($column->type->value, $binary ?? "\0")[1];
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Implementing the record
|
// Implementing the record
|
||||||
$record = $this->record(...$unpacked);
|
$record = $this->record(...$unpacked);
|
||||||
|
|
||||||
// Exit (success)
|
// Exit (success)
|
||||||
return $record;
|
return $record;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -368,13 +368,8 @@ class database
|
||||||
*
|
*
|
||||||
* @return array|null Readed records
|
* @return array|null Readed records
|
||||||
*/
|
*/
|
||||||
public function read(
|
public function read(?callable $filter = null, ?callable $update = null, bool $delete = false, int $amount = 1, int $offset = 0): ?array
|
||||||
?callable $filter = null,
|
{
|
||||||
?callable $update = null,
|
|
||||||
bool $delete = false,
|
|
||||||
int $amount = 1,
|
|
||||||
int $offset = 0
|
|
||||||
): ?array {
|
|
||||||
// Opening the database file
|
// Opening the database file
|
||||||
$file = fopen($this->database, 'c+b');
|
$file = fopen($this->database, 'c+b');
|
||||||
|
|
||||||
|
@ -419,55 +414,55 @@ class database
|
||||||
// Unpacking the record
|
// Unpacking the record
|
||||||
$record = $this->unpack($binaries);
|
$record = $this->unpack($binaries);
|
||||||
|
|
||||||
if ((bool) array_filter($record->values())) {
|
if ((bool) array_filter($record->values())) {
|
||||||
// The record contains at least one non-empty value
|
// The record contains at least one non-empty value
|
||||||
|
|
||||||
if (is_null($filter) || $filter($record, $records)) {
|
if (is_null($filter) || $filter($record, $records)) {
|
||||||
// Passed the filter
|
// Passed the filter
|
||||||
|
|
||||||
if ($offset-- <= 0) {
|
if ($offset-- <= 0) {
|
||||||
// Offsetted
|
// Offsetted
|
||||||
|
|
||||||
if ($delete) {
|
if ($delete) {
|
||||||
// Requested deleting
|
// Requested deleting
|
||||||
|
|
||||||
// Moving to the beginning of the row
|
// Moving to the beginning of the row
|
||||||
fseek($file, -$this->length, SEEK_CUR);
|
fseek($file, -$this->length, SEEK_CUR);
|
||||||
|
|
||||||
// Writing NUL-characters instead of the record to the database file
|
// Writing NUL-characters instead of the record to the database file
|
||||||
fwrite($file, str_repeat("\0", $this->length));
|
fwrite($file, str_repeat("\0", $this->length));
|
||||||
|
|
||||||
// Moving to the end of the row
|
// Moving to the end of the row
|
||||||
fseek($file, $this->length, SEEK_CUR);
|
fseek($file, $this->length, SEEK_CUR);
|
||||||
} else if ($update) {
|
} else if ($update) {
|
||||||
// Requested updating
|
// Requested updating
|
||||||
|
|
||||||
// Updating the record
|
// Updating the record
|
||||||
$update($record);
|
$update($record);
|
||||||
|
|
||||||
// Packing the updated record
|
// Packing the updated record
|
||||||
$packed = $this->pack($record);
|
$packed = $this->pack($record);
|
||||||
|
|
||||||
// Moving to the beginning of the row
|
// Moving to the beginning of the row
|
||||||
fseek($file, -$this->length, SEEK_CUR);
|
fseek($file, -$this->length, SEEK_CUR);
|
||||||
|
|
||||||
// Writing to the database file
|
// Writing to the database file
|
||||||
fwrite($file, $packed);
|
fwrite($file, $packed);
|
||||||
|
|
||||||
// Moving to the end of the row
|
// Moving to the end of the row
|
||||||
fseek($file, $this->length, SEEK_CUR);
|
fseek($file, $this->length, SEEK_CUR);
|
||||||
}
|
|
||||||
|
|
||||||
// Writing into the buffer of records
|
|
||||||
$records[] = $record;
|
|
||||||
|
|
||||||
// Decreasing the amount iterator
|
|
||||||
--$amount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Writing into the buffer of records
|
||||||
|
$records[] = $record;
|
||||||
|
|
||||||
|
// Decreasing the amount iterator
|
||||||
|
--$amount;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// The record contains only empty values
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// The record contains only empty values
|
||||||
|
}
|
||||||
} catch (exception_logic | exception_invalid_argument | exception_domain $exception) {
|
} catch (exception_logic | exception_invalid_argument | exception_domain $exception) {
|
||||||
// Writing into the buffer of failed to reading records
|
// Writing into the buffer of failed to reading records
|
||||||
|
|
||||||
|
@ -490,25 +485,6 @@ class database
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Count
|
|
||||||
*
|
|
||||||
* @throws exception_runtime If the database is corrupted (counting result is float)
|
|
||||||
*
|
|
||||||
* @return int Amount of records
|
|
||||||
*/
|
|
||||||
public function count(): int
|
|
||||||
{
|
|
||||||
// Deleting the database file cache
|
|
||||||
clearstatcache(true, $this->database);
|
|
||||||
|
|
||||||
// Counting
|
|
||||||
$amount = $this->length > 0 && file_exists($this->database) ? filesize($this->database) / $this->length : 0;
|
|
||||||
|
|
||||||
// Exit (success/fail)
|
|
||||||
return is_int($amount) ? $amount : throw new exception_runtime('The database is corrupted');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Backups
|
* Backups
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
use mirzaev\baza\database,
|
use mirzaev\baza\database,
|
||||||
mirzaev\baza\record,
|
mirzaev\baza\record,
|
||||||
mirzaev\baza\column,
|
mirzaev\baza\column,
|
||||||
|
@ -9,24 +7,11 @@ use mirzaev\baza\database,
|
||||||
mirzaev\baza\enumerations\type;
|
mirzaev\baza\enumerations\type;
|
||||||
|
|
||||||
// Initializing path to the composer loader file (main project)
|
// Initializing path to the composer loader file (main project)
|
||||||
$autoload =
|
$autoload = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
|
||||||
__DIR__ . DIRECTORY_SEPARATOR .
|
|
||||||
'..' . DIRECTORY_SEPARATOR .
|
|
||||||
'..' . DIRECTORY_SEPARATOR .
|
|
||||||
'..' . DIRECTORY_SEPARATOR .
|
|
||||||
'vendor' . DIRECTORY_SEPARATOR .
|
|
||||||
'autoload.php';
|
|
||||||
|
|
||||||
// Reinitializing path to the composer loaded file (depencendy project)
|
// Reinitializing path to the composer loaded file (depencendy project)
|
||||||
if (!file_exists($autoload))
|
if (!file_exists($autoload))
|
||||||
$autoload =
|
$autoload = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'autoload.php';
|
||||||
__DIR__ . DIRECTORY_SEPARATOR .
|
|
||||||
'..' . DIRECTORY_SEPARATOR .
|
|
||||||
'..' . DIRECTORY_SEPARATOR .
|
|
||||||
'..' . DIRECTORY_SEPARATOR .
|
|
||||||
'..' . DIRECTORY_SEPARATOR .
|
|
||||||
'..' . DIRECTORY_SEPARATOR .
|
|
||||||
'autoload.php';
|
|
||||||
|
|
||||||
// Importing files of thr project and dependencies
|
// Importing files of thr project and dependencies
|
||||||
require($autoload);
|
require($autoload);
|
||||||
|
|
Loading…
Reference in New Issue