Skip to content

Commit

Permalink
Added connection management functions for Unix sockets in
Browse files Browse the repository at this point in the history
  • Loading branch information
Netkas committed Feb 12, 2022
1 parent 5263d61 commit 7e1800d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
65 changes: 64 additions & 1 deletion src/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class DB
*/
protected static $telegram;

/**
* The encoding used
*
* @var string
*/
protected static $encoding;

/**
* Initialize
*
Expand Down Expand Up @@ -102,12 +109,68 @@ public static function initialize(
self::$telegram = $telegram;
self::$mysql_credentials = $credentials;
self::$table_prefix = $table_prefix;
self::$encoding = $encoding;

self::defineTables();

return self::$pdo;
}

/**
* Disconnects from the database
*
* @return void
*/
public static function disconnect()
{
self::$pdo = null;
}

/**
* Reconnects to the database
*
* @return void
* @throws TelegramException
*/
public static function connect()
{
if (empty(self::$mysql_credentials))
{
throw new TelegramException('MySQL credentials not provided!');
}

if (isset(self::$mysql_credentials['unix_socket']))
{
$dsn = 'mysql:unix_socket=' . self::$mysql_credentials['unix_socket'];
}
else
{
$dsn = 'mysql:host=' . self::$mysql_credentials['host'];
}

$dsn .= ';dbname=' . self::$mysql_credentials['database'];

if (!empty(self::$mysql_credentials['port']))
{
$dsn .= ';port=' . self::$mysql_credentials['port'];
}

$options = [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . self::$encoding];

try
{
$pdo = new PDO($dsn, self::$mysql_credentials['user'], self::$mysql_credentials['password'], $options);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
catch (PDOException $e)
{
throw new TelegramException($e->getMessage());
}

self::$pdo = $pdo;

}

/**
* External Initialize
*
Expand Down Expand Up @@ -459,7 +522,7 @@ protected static function insertTelegramUpdate(
// Add the update ID to a set
$redis->lPush('telegram_update_ids', $update_id);
}

// Sort the set now instead of later.
$redis->sort('telegram_update_ids');
// Actually add the update to redis
Expand Down
4 changes: 2 additions & 2 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"package": {
"package_name": "net.intellivoid.tdlib",
"name": "Telegram Database Library for Bots",
"version": "1.0.1.4",
"version": "1.0.1.5",
"author": "Zi Xing Narrakas",
"organization": "Intellivoid Technologies",
"description": "Allows bot clients to communicate with the Telegram Bot API server",
Expand Down Expand Up @@ -671,4 +671,4 @@
"files": [
"package.json"
]
}
}

0 comments on commit 7e1800d

Please sign in to comment.