diff --git a/src/Db/DbPropertiesFactory.php b/src/Db/DbPropertiesFactory.php index d1c17779c..6f3c72fec 100644 --- a/src/Db/DbPropertiesFactory.php +++ b/src/Db/DbPropertiesFactory.php @@ -20,6 +20,7 @@ use danog\MadelineProto\Magic; use danog\MadelineProto\Settings\Database\DriverDatabaseAbstract; use danog\MadelineProto\Settings\Database\Mysql; use danog\MadelineProto\Settings\Database\SerializerType; +use danog\MadelineProto\Settings\Database\SqlAbstract; use danog\MadelineProto\Settings\DatabaseAbstract; /** @@ -47,8 +48,13 @@ final class DbPropertiesFactory ), 'innerMadelineProto' => false, 'cacheTtl' => $dbSettings->getCacheTtl(), + 'intKey' => false, ], $config); + if ($dbSettings instanceof SqlAbstract) { + $dbSettings->intKey = $config['intKey']; + } + if ($config['innerMadelineProto'] && $config['serializer'] !== SerializerType::IGBINARY && $config['serializer'] !== SerializerType::SERIALIZE diff --git a/src/Db/MysqlArray.php b/src/Db/MysqlArray.php index bdc2eb550..0cf480a14 100644 --- a/src/Db/MysqlArray.php +++ b/src/Db/MysqlArray.php @@ -16,7 +16,7 @@ namespace danog\MadelineProto\Db; -use Amp\Sql\Result; +use Amp\Sql\SqlResult; use danog\MadelineProto\Db\Driver\Mysql; use danog\MadelineProto\Exception; use danog\MadelineProto\Logger; @@ -88,7 +88,7 @@ final class MysqlArray extends SqlArray * * @psalm-param self::STATEMENT_* $stmt */ - protected function execute(string $sql, array $params = []): Result + protected function execute(string $sql, array $params = []): SqlResult { foreach ($params as $key => $value) { $value = $this->pdo->quote($value); @@ -115,10 +115,13 @@ final class MysqlArray extends SqlArray protected function prepareTable(): void { //Logger::log("Creating/checking table {$this->table}", Logger::WARNING); + \assert($this->dbSettings instanceof DatabaseMysql); + $keyType = $this->dbSettings->intKey ? 'BIGINT' : 'VARCHAR(255)'; + $this->db->query(" CREATE TABLE IF NOT EXISTS `{$this->table}` ( - `key` VARCHAR(255) NOT NULL, + `key` $keyType NOT NULL, `value` LONGBLOB NULL, `ts` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`key`) @@ -127,7 +130,9 @@ final class MysqlArray extends SqlArray CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci' "); - \assert($this->dbSettings instanceof DatabaseMysql); + if ($this->dbSettings->intKey) { + $this->db->query("ALTER TABLE `{$this->table}` MODIFY `key` BIGINT"); + } if ($this->dbSettings->getOptimizeIfWastedGtMb() !== null) { try { $database = $this->dbSettings->getDatabase(); diff --git a/src/Db/PostgresArray.php b/src/Db/PostgresArray.php index 3b2b2af94..d4fc1566f 100644 --- a/src/Db/PostgresArray.php +++ b/src/Db/PostgresArray.php @@ -19,6 +19,7 @@ namespace danog\MadelineProto\Db; use danog\MadelineProto\Db\Driver\Postgres; use danog\MadelineProto\Exception; use danog\MadelineProto\Logger; +use danog\MadelineProto\Settings\Database\Postgres as DatabasePostgres; use danog\MadelineProto\Settings\Database\SerializerType; /** @@ -90,14 +91,18 @@ final class PostgresArray extends PostgresArrayBytea protected function prepareTable(): void { //Logger::log("Creating/checking table {$this->table}", Logger::WARNING); - + \assert($this->dbSettings instanceof DatabasePostgres); + $keyType = $this->dbSettings->intKey ? 'BIGINT' : 'VARCHAR(255)'; $this->db->query(" CREATE TABLE IF NOT EXISTS \"{$this->table}\" ( - \"key\" VARCHAR(255) PRIMARY KEY NOT NULL, + \"key\" $keyType PRIMARY KEY NOT NULL, \"value\" BYTEA NOT NULL ); "); + if ($this->dbSettings->intKey) { + $this->db->query("ALTER TABLE \"{$this->table}\" MODIFY \"key\" BIGINT"); + } } protected function moveDataFromTableToTable(string $from, string $to): void diff --git a/src/Db/SqlArray.php b/src/Db/SqlArray.php index afc52973d..3d1eebed1 100644 --- a/src/Db/SqlArray.php +++ b/src/Db/SqlArray.php @@ -16,8 +16,8 @@ namespace danog\MadelineProto\Db; -use Amp\Sql\Pool; -use Amp\Sql\Result; +use Amp\Sql\SqlConnectionPool; +use Amp\Sql\SqlResult; /** * Generic SQL database backend. @@ -30,7 +30,7 @@ use Amp\Sql\Result; */ abstract class SqlArray extends DriverArray { - protected Pool $db; + protected SqlConnectionPool $db; protected const SQL_GET = 0; protected const SQL_SET = 1; @@ -148,7 +148,7 @@ abstract class SqlArray extends DriverArray * * @psalm-param self::STATEMENT_* $stmt */ - protected function execute(string $sql, array $params = []): Result + protected function execute(string $sql, array $params = []): SqlResult { return $this->db->prepare($sql)->execute($params); } diff --git a/src/MTProto.php b/src/MTProto.php index 963e4eb01..88cde00db 100644 --- a/src/MTProto.php +++ b/src/MTProto.php @@ -388,9 +388,9 @@ final class MTProto implements TLCallback, LoggerGetter, SettingsGetter * @see DbPropertiesFactory */ protected static array $dbProperties = [ - 'sponsoredMessages' => ['innerMadelineProto' => true], + 'sponsoredMessages' => ['innerMadelineProto' => true, 'intKey' => true], 'channelParticipants' => ['innerMadelineProto' => true], - 'getUpdatesQueue' => ['innerMadelineProto' => true], + 'getUpdatesQueue' => ['innerMadelineProto' => true, 'intKey' => true], 'session' => ['innerMadelineProto' => true, 'enableCache' => false, 'optimizeIfWastedGtMb' => 1], ]; diff --git a/src/MTProtoTools/MinDatabase.php b/src/MTProtoTools/MinDatabase.php index 3bec660e3..ffec23b6f 100644 --- a/src/MTProtoTools/MinDatabase.php +++ b/src/MTProtoTools/MinDatabase.php @@ -67,7 +67,7 @@ final class MinDatabase implements TLCallback * @see DbPropertiesFactory */ protected static array $dbProperties = [ - 'db' => ['innerMadelineProto' => true], + 'db' => ['innerMadelineProto' => true, 'intKey' => true], ]; private LocalKeyedMutex $localMutex; diff --git a/src/MTProtoTools/PeerDatabase.php b/src/MTProtoTools/PeerDatabase.php index dc62e7e60..bf17ea432 100644 --- a/src/MTProtoTools/PeerDatabase.php +++ b/src/MTProtoTools/PeerDatabase.php @@ -86,10 +86,12 @@ final class PeerDatabase implements TLCallback 'db' => [ 'innerMadelineProto' => true, 'table' => 'MTProto_chats', + 'intKey' => true, ], 'fullDb' => [ 'innerMadelineProto' => true, 'table' => 'MTProto_full_chats', + 'intKey' => true, ], 'usernames' => [ 'innerMadelineProto' => true, diff --git a/src/MTProtoTools/PeerHandler.php b/src/MTProtoTools/PeerHandler.php index ac2ea3dd4..20c16b524 100644 --- a/src/MTProtoTools/PeerHandler.php +++ b/src/MTProtoTools/PeerHandler.php @@ -380,7 +380,7 @@ trait PeerHandler if (is_numeric($id)) { $id = (int) $id; Assert::true($id !== 0, "An invalid ID was specified!"); - if (DialogId::getType($id) === DialogId::SECRET_CHAT) { + if (DialogId::isSecretChat($id)) { $id = $this->getSecretChat($id)->otherID; } if (!$this->peerDatabase->isset($id)) { @@ -645,7 +645,7 @@ trait PeerHandler public function getFullInfo(mixed $id): array { $partial = $this->getInfo($id); - if (DialogId::getType($partial['bot_api_id']) === DialogId::SECRET_CHAT) { + if (DialogId::isSecretChat($partial['bot_api_id'])) { return $partial; } $full = $this->peerDatabase->getFull($partial['bot_api_id']); diff --git a/src/SecretChats/SecretChatController.php b/src/SecretChats/SecretChatController.php index 5cfffb8b6..c068f4fae 100644 --- a/src/SecretChats/SecretChatController.php +++ b/src/SecretChats/SecretChatController.php @@ -61,9 +61,9 @@ final class SecretChatController implements Stringable * @see DbPropertiesFactory */ protected static array $dbProperties = [ - 'incoming' => ['innerMadelineProto' => true], - 'outgoing' => ['innerMadelineProto' => true], - 'randomIdMap' => ['innerMadelineProto' => true], + 'incoming' => ['innerMadelineProto' => true, 'intKey' => true], + 'outgoing' => ['innerMadelineProto' => true, 'intKey' => true], + 'randomIdMap' => ['innerMadelineProto' => true, 'intKey' => true], ]; /** diff --git a/src/Settings/Database/SqlAbstract.php b/src/Settings/Database/SqlAbstract.php index d29d9ce12..38c6e1249 100644 --- a/src/Settings/Database/SqlAbstract.php +++ b/src/Settings/Database/SqlAbstract.php @@ -49,6 +49,11 @@ abstract class SqlAbstract extends DriverDatabaseAbstract */ protected string $uri = 'tcp://127.0.0.1'; + /** + * @internal Internal value used by inner settings, do not change. + */ + public bool $intKey = false; + /** * Get maximum connection limit. *