Skip to content

Commit 0647dcb

Browse files
committed
set utf8mb4 as default and add possibility to set own encoding
1 parent 0c97f93 commit 0647dcb

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/DB.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,18 @@ class DB
6767
* @param array $credentials Database connection details
6868
* @param Telegram $telegram Telegram object to connect with this object
6969
* @param string $table_prefix Table prefix
70+
* @param string $encoding Database character encoding
7071
*
7172
* @return PDO PDO database object
7273
*/
73-
public static function initialize(array $credentials, Telegram $telegram, $table_prefix = null)
74+
public static function initialize(array $credentials, Telegram $telegram, $table_prefix = null, $encoding = 'utf8mb4')
7475
{
7576
if (empty($credentials)) {
7677
throw new TelegramException('MySQL credentials not provided!');
7778
}
7879

7980
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database'];
80-
$options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'];
81+
$options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $encoding];
8182
try {
8283
$pdo = new \PDO($dsn, $credentials['user'], $credentials['password'], $options);
8384
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);

src/Telegram.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ public function __construct($api_key, $bot_name)
174174
*
175175
* @return Telegram
176176
*/
177-
public function enableMySql(array $credential, $table_prefix = null)
177+
public function enableMySql(array $credential, $table_prefix = null, $encoding = 'utf8mb4')
178178
{
179-
$this->pdo = DB::initialize($credential, $this, $table_prefix);
179+
$this->pdo = DB::initialize($credential, $this, $table_prefix, $encoding);
180180
ConversationDB::initializeConversation();
181181
$this->mysql_enabled = true;
182182
return $this;

structure.sql

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS `user` (
77
`updated_at` timestamp NULL DEFAULT NULL COMMENT 'Entry date update',
88
PRIMARY KEY (`id`),
99
KEY `username` (`username`)
10-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
10+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
1111

1212
CREATE TABLE IF NOT EXISTS `chat` (
1313
`id` bigint COMMENT 'Unique user or chat identifier',
@@ -18,7 +18,7 @@ CREATE TABLE IF NOT EXISTS `chat` (
1818
`old_id` bigint DEFAULT NULL COMMENT 'Unique chat identifieri this is filled when a chat is converted to a superchat',
1919
PRIMARY KEY (`id`),
2020
KEY `old_id` (`old_id`)
21-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
21+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
2222

2323
CREATE TABLE IF NOT EXISTS `user_chat` (
2424
`user_id` bigint COMMENT 'Unique user identifier',
@@ -28,7 +28,7 @@ CREATE TABLE IF NOT EXISTS `user_chat` (
2828
ON DELETE CASCADE ON UPDATE CASCADE,
2929
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`)
3030
ON DELETE CASCADE ON UPDATE CASCADE
31-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
31+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
3232

3333
CREATE TABLE IF NOT EXISTS `inline_query` (
3434
`id` bigint UNSIGNED COMMENT 'Unique identifier for this query.',
@@ -43,7 +43,7 @@ CREATE TABLE IF NOT EXISTS `inline_query` (
4343
FOREIGN KEY (`user_id`)
4444
REFERENCES `user` (`id`)
4545

46-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
46+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
4747

4848
CREATE TABLE IF NOT EXISTS `chosen_inline_result` (
4949
`id` bigint UNSIGNED AUTO_INCREMENT COMMENT 'Unique identifier for chosen query.',
@@ -59,7 +59,7 @@ CREATE TABLE IF NOT EXISTS `chosen_inline_result` (
5959
FOREIGN KEY (`user_id`)
6060
REFERENCES `user` (`id`)
6161

62-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
62+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
6363

6464
CREATE TABLE IF NOT EXISTS `callback_query` (
6565
`id` bigint UNSIGNED COMMENT 'Unique identifier for this query.',
@@ -74,7 +74,7 @@ CREATE TABLE IF NOT EXISTS `callback_query` (
7474
FOREIGN KEY (`user_id`)
7575
REFERENCES `user` (`id`)
7676

77-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
77+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
7878

7979
CREATE TABLE IF NOT EXISTS `message` (
8080
`chat_id` bigint COMMENT 'Chat identifier.',
@@ -86,7 +86,7 @@ CREATE TABLE IF NOT EXISTS `message` (
8686
`forward_date` timestamp NULL DEFAULT NULL COMMENT 'For forwarded messages, date the original message was sent in Unix time',
8787
`reply_to_chat` bigint NULL DEFAULT NULL COMMENT 'Chat identifier.',
8888
`reply_to_message` bigint UNSIGNED DEFAULT NULL COMMENT 'Message is a reply to another message.',
89-
`text` TEXT DEFAULT NULL COMMENT 'For text messages, the actual UTF-8 text of the message max message length 4096 char utf8',
89+
`text` TEXT DEFAULT NULL COMMENT 'For text messages, the actual UTF-8 text of the message max message length 4096 char utf8mb4',
9090
`entities` TEXT DEFAULT NULL COMMENT 'For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text',
9191
`audio` TEXT DEFAULT NULL COMMENT 'Audio object. Message is an audio file, information about the file',
9292
`document` TEXT DEFAULT NULL COMMENT 'Document object. Message is a general file, information about the file',
@@ -129,7 +129,7 @@ CREATE TABLE IF NOT EXISTS `message` (
129129
FOREIGN KEY (`new_chat_member`) REFERENCES `user` (`id`),
130130
FOREIGN KEY (`left_chat_member`) REFERENCES `user` (`id`)
131131

132-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
132+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
133133

134134
CREATE TABLE IF NOT EXISTS `telegram_update` (
135135
`id` bigint UNSIGNED COMMENT 'The update\'s unique identifier.',
@@ -149,7 +149,7 @@ CREATE TABLE IF NOT EXISTS `telegram_update` (
149149
FOREIGN KEY (`inline_query_id`) REFERENCES `inline_query` (`id`),
150150
FOREIGN KEY (`chosen_inline_result_id`) REFERENCES `chosen_inline_result` (`id`),
151151
FOREIGN KEY (`callback_query_id`) REFERENCES `callback_query` (`id`)
152-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
152+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
153153

154154
CREATE TABLE IF NOT EXISTS `conversation` (
155155
`id` bigint(20) unsigned AUTO_INCREMENT COMMENT 'Row unique id',
@@ -170,4 +170,4 @@ CREATE TABLE IF NOT EXISTS `conversation` (
170170
REFERENCES `user` (`id`),
171171
FOREIGN KEY (`chat_id`)
172172
REFERENCES `chat` (`id`)
173-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
173+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

0 commit comments

Comments
 (0)