read database util and file cleanup

This commit is contained in:
Little Fluffy Clouds 2025-07-10 18:08:08 +03:00
parent d867d640a4
commit ced4e3241c
5 changed files with 924 additions and 885 deletions

View File

@ -6,24 +6,24 @@ namespace mirzaev\deeproots\models;
// Files of the project // Files of the project
use mirzaev\deeproots\models\core, use mirzaev\deeproots\models\core,
mirzaev\deeproots\models\account\localization; mirzaev\deeproots\models\account\localization;
// Svoboda time // Svoboda time
use svoboda\time\statement as svoboda; use svoboda\time\statement as svoboda;
// Baza database // Baza database
use mirzaev\baza\database, use mirzaev\baza\database,
mirzaev\baza\column, mirzaev\baza\column,
mirzaev\baza\record, mirzaev\baza\record,
mirzaev\baza\enumerations\encoding, mirzaev\baza\enumerations\encoding,
mirzaev\baza\enumerations\type; mirzaev\baza\enumerations\type;
// Framework for Telegram // Framework for Telegram
use Zanzara\Telegram\Type\User as model; use Zanzara\Telegram\Type\User as model;
// Built-in libraries // Built-in libraries
use Exception as exception, use Exception as exception,
RuntimeException as exception_runtime; RuntimeException as exception_runtime;
/** /**
* Telegram account * Telegram account
@ -35,158 +35,158 @@ use Exception as exception,
*/ */
final class telegram extends core final class telegram extends core
{ {
/** /**
* File * File
* *
* @var string $database Path to the database file * @var string $database Path to the database file
*/ */
protected string $file = DATABASES . DIRECTORY_SEPARATOR . 'telegram.baza'; protected string $file = DATABASES . DIRECTORY_SEPARATOR . 'telegram.baza';
/** /**
* Database * Database
* *
* @var database $database The database * @var database $database The database
*/ */
public protected(set) database $database; public protected(set) database $database;
/** /**
* Constructor * Constructor
* *
* @return void * @return void
*/ */
public function __construct() public function __construct()
{ {
// Initializing the database // Initializing the database
$this->database = new database() $this->database = new database()
->encoding(encoding::utf8) ->encoding(encoding::utf8)
->columns( ->columns(
new column('identifier', type::integer_unsigned), new column('identifier', type::integer_unsigned),
new column('domain', type::string, ['length' => 32]), new column('domain', type::string, ['length' => 32]),
new column('name_first', type::string, ['length' => 64]), new column('name_first', type::string, ['length' => 64]),
new column('name_second', type::string, ['length' => 64]), new column('name_second', type::string, ['length' => 64]),
new column('language', type::string, ['length' => 2]), new column('language', type::string, ['length' => 2]),
new column('robot', type::char), new column('robot', type::char),
new column('updated', type::integer_unsigned), new column('updated', type::integer_unsigned),
new column('created', type::integer_unsigned) new column('created', type::integer_unsigned)
) )
->connect($this->file); ->connect($this->file);
} }
/** /**
* Initialize * Initialize
* *
* Searches for the telegram account record in the database, and if it does not find it, then create * Searches for the telegram account record in the database, and if it does not find it, then create
* *
* @param model $telegram The telegram account * @param model $telegram The telegram account
* *
* @throws exception_runtime if update the telegram account record in the database by the telegram account values * @throws exception_runtime if failed to update the telegram account record in the database by the telegram account values
* @throws exception_runtime if failed to find the created telegram account * @throws exception_runtime if failed to find the created telegram account
* @throws exception_runtime if failed to create the telegram account * @throws exception_runtime if failed to create the telegram account
* *
* @return record The telegram account record from the database * @return record The telegram account record from the database
*/ */
public function initialize(model $telegram): record public function initialize(model $telegram): record
{ {
// Searching for the account in the database // Searching for the account in the database
$account = $this->database->read(filter: fn(record $record) => $record->identifier === $telegram->getId(), amount: 1)[0] ?? null; $account = $this->database->read(filter: fn(record $record) => $record->identifier === $telegram->getId(), amount: 1)[0] ?? null;
if ($account instanceof record) { if ($account instanceof record) {
// Found the telegram account record // Found the telegram account record
if ( if (
$account->name_first !== $telegram->getFirstName() || $account->name_first !== $telegram->getFirstName() ||
$account->name_second !== $telegram->getLastName() || $account->name_second !== $telegram->getLastName() ||
$account->domain !== $telegram->getUsername() $account->domain !== $telegram->getUsername()
) { ) {
// The telegram account was updated // The telegram account was updated
// Updating the account in the database // Updating the account in the database
$updated = $this->database->read( $updated = $this->database->read(
filter: fn(record $record) => $record->identifier === $telegram->getId(), filter: fn(record $record) => $record->identifier === $telegram->getId(),
update: function (record &$record) use ($telegram){ update: function (record &$record) use ($telegram){
// Writing new values into the record // Writing new values into the record
$record->name_first = $telegram->getFirstName(); $record->name_first = $telegram->getFirstName();
$record->name_second = $telegram->getLastName(); $record->name_second = $telegram->getLastName();
$record->domain = $telegram->getUsername(); $record->domain = $telegram->getUsername();
$record->updated = svoboda::timestamp(); $record->updated = svoboda::timestamp();
}, },
amount: 1 amount: 1
)[0] ?? null; )[0] ?? null;
if ($updated instanceof record && $updated->values() !== $account->values()) { if ($updated instanceof record && $updated->values() !== $account->values()) {
// Updated the account in the database // Updated the account in the database
// Exit (success) // Exit (success)
return $updated; return $updated;
} else { } else {
// Not updated the account in the database // Not updated the account in the database
// Exit (fail) // Exit (fail)
throw new exception_runtime('Failed to update the account record in the database by the telegram account values'); throw new exception_runtime('Failed to update the account record in the database by the telegram account values');
} }
} }
// Exit (success) // Exit (success)
return $account; return $account;
} else { } else {
// Not found the account record // Not found the account record
if ($this->create($telegram)) { if ($this->create($telegram)) {
// Created the account // Created the account
// Searching for the created telegram account in the database // Searching for the created telegram account in the database
$account = $this->database->read(filter: fn(record $record) => $record->identifier === $telegram->getId(), amount: 1)[0] ?? null; $account = $this->database->read(filter: fn(record $record) => $record->identifier === $telegram->getId(), amount: 1)[0] ?? null;
if ($account instanceof record) { if ($account instanceof record) {
// Found the created telegram account // Found the created telegram account
// Exit (success) // Exit (success)
return $account; return $account;
} else { } else {
// Not found the created telegram account // Not found the created telegram account
// Exit (fail) // Exit (fail)
throw new exception_runtime('Failed to find the created telegram account'); throw new exception_runtime('Failed to find the created telegram account');
} }
} else { } else {
// Not created the telegram account // Not created the telegram account
// Exit (fail) // Exit (fail)
throw new exception_runtime('Failed to create the telegram account'); throw new exception_runtime('Failed to create the telegram account');
} }
} }
} }
/** /**
* Create * Create
* *
* Creates the account record in the database * Creates the account record in the database
* *
* @param model $telegram The telegram account * @param model $telegram The telegram account
* *
* @return int|false The record identifier, if created * @return int|false The record identifier, if created
*/ */
public function create(model $telegram): int|false public function create(model $telegram): int|false
{ {
// Initializing the identifier // Initializing the identifier
$identifier = (int) $telegram->getId(); $identifier = (int) $telegram->getId();
// Initializing the record // Initializing the record
$record = $this->database->record( $record = $this->database->record(
$identifier, $identifier,
$telegram->getUsername(), $telegram->getUsername(),
$telegram->getFirstName(), $telegram->getFirstName(),
$telegram->getLastName(), $telegram->getLastName(),
$telegram->getLanguageCode(), $telegram->getLanguageCode(),
(int) $telegram->isBot(), (int) $telegram->isBot(),
svoboda::timestamp(), svoboda::timestamp(),
svoboda::timestamp() svoboda::timestamp()
); );
// Creating the record in the database // Creating the record in the database
$created = $this->database->write($record); $created = $this->database->write($record);
// Exit (success) // Exit (success)
return $created ? $identifier : false; return $created ? $identifier : false;
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -6,30 +6,30 @@ namespace mirzaev\deeproots;
// Framework for PHP // Framework for PHP
use mirzaev\minimal\core, use mirzaev\minimal\core,
mirzaev\minimal\route; mirzaev\minimal\route;
// Enabling debugging // Enabling debugging
/* ini_set('error_reporting', E_ALL); /* ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1); ini_set('display_errors', 1);
ini_set('display_startup_errors', 1); */ ini_set('display_startup_errors', 1); */
// Initializing path to the public directory // Initializing path to the public directory
define('INDEX', __DIR__); define('INDEX', __DIR__);
// Initializing path to the project root directory // Initializing path to the project root directory
define('ROOT', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR); define('ROOT', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
// Initializing path to the directory of views // Initializing path to the directory of views
define('VIEWS', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'views'); define('VIEWS', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'views');
// Initializing path to the directory of settings // Initializing path to the directory of settings
define('SETTINGS', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'settings'); define('SETTINGS', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'settings');
// Initializing system settings // Initializing system settings
require SETTINGS . DIRECTORY_SEPARATOR . 'system.php'; require SETTINGS . DIRECTORY_SEPARATOR . 'system.php';
// Initializing path to the directory of the storage // Initializing path to the directory of the storage
define('STORAGE', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'storage'); define('STORAGE', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'storage');
// Initializing path to the databases directory // Initializing path to the databases directory
define('DATABASES', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'databases'); define('DATABASES', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'databases');
@ -42,8 +42,8 @@ $core = new core(namespace: __NAMESPACE__);
// Initializing routes // Initializing routes
$core->router $core->router
->write('/', new route('index', 'index'), 'GET') ->write('/', new route('index', 'index'), 'GET')
; ;
// Handling request // Handling request
$core->start(); $core->start();

View File

@ -6,28 +6,28 @@ namespace mirzaev\deeproots;
// Files of the project // Files of the project
use mirzaev\deeproots\models\telegram\middlewares, use mirzaev\deeproots\models\telegram\middlewares,
mirzaev\deeproots\models\telegram\commands, mirzaev\deeproots\models\telegram\commands,
mirzaev\deeproots\models\telegram\settings, mirzaev\deeproots\models\telegram\settings,
mirzaev\deeproots\models\telegram\processes\question\search as process_question_search, mirzaev\deeproots\models\telegram\processes\question\search as process_question_search,
mirzaev\deeproots\models\telegram\buttons\question\search as buttons_question_search, mirzaev\deeproots\models\telegram\buttons\question\search as buttons_question_search,
mirzaev\deeproots\models\telegram\processes\question\create as process_question_create, mirzaev\deeproots\models\telegram\processes\question\create as process_question_create,
mirzaev\deeproots\models\enumerations\language; mirzaev\deeproots\models\enumerations\language;
// Framework for Telegram // Framework for Telegram
use Zanzara\Zanzara as zanzara, use Zanzara\Zanzara as zanzara,
Zanzara\Context as context, Zanzara\Context as context,
Zanzara\Config as config; Zanzara\Config as config;
// Enabling debugging // Enabling debugging
/* ini_set('error_reporting', E_ALL); /* ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1); ini_set('display_errors', 1);
ini_set('display_startup_errors', 1); */ ini_set('display_startup_errors', 1); */
// Initializing path to the public directory // Initializing path to the public directory
define('INDEX', __DIR__); define('INDEX', __DIR__);
// Initializing path to the root directory // Initializing path to the root directory
define('ROOT', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR); define('ROOT', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
// Initializing path to the settings directory // Initializing path to the settings directory
define('SETTINGS', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'settings'); define('SETTINGS', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'settings');
@ -82,10 +82,10 @@ $robot->onCommand('society', [commands::class, 'society']);
// Initializing the robot settings language buttons handlers // Initializing the robot settings language buttons handlers
foreach (language::cases() as $language) { foreach (language::cases() as $language) {
// Iterating over languages // Iterating over languages
// Initializing language buttons // Initializing language buttons
$robot->onCbQueryData(['settings_language_' . $language->name], fn(context $context) => settings::language($context, $language)); $robot->onCbQueryData(['settings_language_' . $language->name], fn(context $context) => settings::language($context, $language));
}; };
$robot->onCbQueryData(['system_questions_search_identifier'], [buttons_question_search::class, 'identifier'])->middleware([middlewares::class, 'system_questions']); $robot->onCbQueryData(['system_questions_search_identifier'], [buttons_question_search::class, 'identifier'])->middleware([middlewares::class, 'system_questions']);

View File

@ -0,0 +1,39 @@
<?php
use mirzaev\baza\database,
mirzaev\baza\column,
mirzaev\baza\record,
mirzaev\baza\enumerations\encoding,
mirzaev\baza\enumerations\type;
// Initializing path constants
define('INDEX', __DIR__);
define('ROOT', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
define('SETTINGS', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'settings');
define('DATABASES', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'databases');
// Initializing dependencies and system settings
require ROOT . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
require SETTINGS . DIRECTORY_SEPARATOR . 'system.php';
// Initializing a path to the Baza database binary file
$file = DATABASES . DIRECTORY_SEPARATOR . 'telegram.baza';
// Instanciating new database class we want to read
$database = new database()
->encoding(encoding::utf8)
->columns(
new column('identifier', type::integer_unsigned),
new column('domain', type::string, ['length' => 32]),
new column('name_first', type::string, ['length' => 64]),
new column('name_second', type::string, ['length' => 64]),
new column('language', type::string, ['length' => 2]),
new column('robot', type::char),
new column('updated', type::integer_unsigned),
new column('created', type::integer_unsigned)
)
->connect($file);
// Reading the whole database array
$read = $database->read();
print_r($read);