Commit 546d62ac authored by MBoretto's avatar MBoretto

introducing db external connection

parent aae0d911
...@@ -344,7 +344,13 @@ $telegram->enableMySQL($mysql_credentials, $BOT_NAME . '_'); ...@@ -344,7 +344,13 @@ $telegram->enableMySQL($mysql_credentials, $BOT_NAME . '_');
Consider to use the *utf8mb4* branch if you find some special characters problems. Consider to use the *utf8mb4* branch if you find some special characters problems.
You can also store inline query and chosen inline query in the database. You can also store inline query and chosen inline query in the database.
#### External Database connection
Is possible to provide to the library an external mysql connection. Here's how to configure it:
```php
$telegram->enableExternalMySQL($external_pdo_connection)
//$telegram->enableExternalMySQL($external_pdo_connection, $table_prefix)
```
### Channels Support ### Channels Support
All methods implemented can be used to manage channels. All methods implemented can be used to manage channels.
......
...@@ -74,47 +74,81 @@ class DB ...@@ -74,47 +74,81 @@ class DB
throw new TelegramException('MySQL credentials not provided!'); throw new TelegramException('MySQL credentials not provided!');
} }
self::$telegram = $telegram;
self::$mysql_credentials = $credentials;
self::$table_prefix = $table_prefix;
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database']; $dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database'];
$options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']; $options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'];
try { try {
$pdo = new \PDO($dsn, $credentials['user'], $credentials['password'], $options); $pdo = new \PDO($dsn, $credentials['user'], $credentials['password'], $options);
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING); $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
} catch (\PDOException $e) {
throw new TelegramException($e->getMessage());
}
//Define table self::$pdo = $pdo;
if (!defined('TB_TELEGRAM_UPDATE')) { self::$telegram = $telegram;
define('TB_TELEGRAM_UPDATE', self::$table_prefix.'telegram_update'); self::$mysql_credentials = $credentials;
} self::$table_prefix = $table_prefix;
if (!defined('TB_MESSAGE')) {
define('TB_MESSAGE', self::$table_prefix.'message');
}
if (!defined('TB_INLINE_QUERY')) {
define('TB_INLINE_QUERY', self::$table_prefix.'inline_query');
}
if (!defined('TB_CHOSEN_INLINE_QUERY')) { self::defineTable();
define('TB_CHOSEN_INLINE_QUERY', self::$table_prefix.'chosen_inline_query');
}
if (!defined('TB_USER')) {
define('TB_USER', self::$table_prefix.'user');
}
if (!defined('TB_CHAT')) {
define('TB_CHAT', self::$table_prefix.'chat');
}
if (!defined('TB_USER_CHAT')) {
define('TB_USER_CHAT', self::$table_prefix.'user_chat');
}
} catch (\PDOException $e) { return self::$pdo;
throw new TelegramException($e->getMessage()); }
/**
* External Initialize
*
* Let you use the class with an external already existing Pdo Mysql connection.
*
* @param PDO $external_pdo_connection PDO database object
* @param Telegram $telegram Telegram object to connect with this object
* @param string $table_prefix Table prefix
*
* @return PDO PDO database object
*/
public static function externalInitialize($external_pdo_connection, Telegram $telegram, $table_prefix = null)
{
if (empty($external_pdo_connection)) {
throw new TelegramException('MySQL external connection not provided!');
} }
self::$pdo = $pdo; self::$pdo = $pdo;
self::$telegram = $telegram;
self::$mysql_credentials = null;
self::$table_prefix = $table_prefix;
self::defineTable();
return self::$pdo; return self::$pdo;
} }
/**
* Define all the table with the proper prefix
*/
protected static function defineTable()
{
if (!defined('TB_TELEGRAM_UPDATE')) {
define('TB_TELEGRAM_UPDATE', self::$table_prefix.'telegram_update');
}
if (!defined('TB_MESSAGE')) {
define('TB_MESSAGE', self::$table_prefix.'message');
}
if (!defined('TB_INLINE_QUERY')) {
define('TB_INLINE_QUERY', self::$table_prefix.'inline_query');
}
if (!defined('TB_CHOSEN_INLINE_QUERY')) {
define('TB_CHOSEN_INLINE_QUERY', self::$table_prefix.'chosen_inline_query');
}
if (!defined('TB_USER')) {
define('TB_USER', self::$table_prefix.'user');
}
if (!defined('TB_CHAT')) {
define('TB_CHAT', self::$table_prefix.'chat');
}
if (!defined('TB_USER_CHAT')) {
define('TB_USER_CHAT', self::$table_prefix.'user_chat');
}
}
/** /**
* Check if database connection has been created * Check if database connection has been created
* *
......
...@@ -176,9 +176,9 @@ class Telegram ...@@ -176,9 +176,9 @@ class Telegram
} }
/** /**
* Initialize * Initialize Database connection
* *
* @param array $credential * @param array $credential
* @param string $table_prefix * @param string $table_prefix
*/ */
public function enableMySQL(array $credential, $table_prefix = null) public function enableMySQL(array $credential, $table_prefix = null)
...@@ -187,6 +187,18 @@ class Telegram ...@@ -187,6 +187,18 @@ class Telegram
$this->mysql_enabled = true; $this->mysql_enabled = true;
} }
/**
* Initialize Database external connection
*
* @param PDO $external_pdo_connection PDO database object
* @param string $table_prefix
*/
public function enableExternalMySQL($external_pdo_connection, $table_prefix = null)
{
$this->pdo = DB::externalInitialize($external_pdo_connection, $this, $table_prefix);
$this->mysql_enabled = true;
}
/** /**
* Get commands list * Get commands list
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment