Commit 7f07f73f authored by Armando Lüscher's avatar Armando Lüscher Committed by GitHub

Merge pull request #618 from noplanman/clean_db_code

Clean up DB code
parents 57858427 bce79590
...@@ -6,10 +6,12 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c ...@@ -6,10 +6,12 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
## [Unreleased] ## [Unreleased]
### Added ### Added
### Changed ### Changed
- Updated and optimised all DB classes, removing a lot of bulky code.
### Deprecated ### Deprecated
### Removed ### Removed
### Fixed ### Fixed
- Ensure named SQL statement parameters are unique. - Ensure named SQL statement parameters are unique.
- Channel selection when using `DB::selectChats()`.
### Security ### Security
## [0.48.0] - 2017-08-26 ## [0.48.0] - 2017-08-26
......
...@@ -12,7 +12,6 @@ namespace Longman\TelegramBot; ...@@ -12,7 +12,6 @@ namespace Longman\TelegramBot;
use Exception; use Exception;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
use PDO;
/** /**
* Class BotanDB * Class BotanDB
...@@ -20,7 +19,7 @@ use PDO; ...@@ -20,7 +19,7 @@ use PDO;
class BotanDB extends DB class BotanDB extends DB
{ {
/** /**
* Initilize botan shortener table * Initialize botan shortener table
*/ */
public static function initializeBotanDb() public static function initializeBotanDb()
{ {
...@@ -32,11 +31,11 @@ class BotanDB extends DB ...@@ -32,11 +31,11 @@ class BotanDB extends DB
/** /**
* Select cached shortened URL from the database * Select cached shortened URL from the database
* *
* @param string $url * @param string $url
* @param integer $user_id * @param string $user_id
* *
* @return array|bool * @return array|bool
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws TelegramException
*/ */
public static function selectShortUrl($url, $user_id) public static function selectShortUrl($url, $user_id)
{ {
...@@ -46,14 +45,16 @@ class BotanDB extends DB ...@@ -46,14 +45,16 @@ class BotanDB extends DB
try { try {
$sth = self::$pdo->prepare(' $sth = self::$pdo->prepare('
SELECT `short_url` FROM `' . TB_BOTAN_SHORTENER . '` SELECT `short_url`
WHERE `user_id` = :user_id AND `url` = :url FROM `' . TB_BOTAN_SHORTENER . '`
WHERE `user_id` = :user_id
AND `url` = :url
ORDER BY `created_at` DESC ORDER BY `created_at` DESC
LIMIT 1 LIMIT 1
'); ');
$sth->bindParam(':user_id', $user_id, PDO::PARAM_INT); $sth->bindValue(':user_id', $user_id);
$sth->bindParam(':url', $url, PDO::PARAM_STR); $sth->bindValue(':url', $url);
$sth->execute(); $sth->execute();
return $sth->fetchColumn(); return $sth->fetchColumn();
...@@ -65,12 +66,12 @@ class BotanDB extends DB ...@@ -65,12 +66,12 @@ class BotanDB extends DB
/** /**
* Insert shortened URL into the database * Insert shortened URL into the database
* *
* @param string $url * @param string $url
* @param integer $user_id * @param string $user_id
* @param string $short_url * @param string $short_url
* *
* @return bool * @return bool
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws TelegramException
*/ */
public static function insertShortUrl($url, $user_id, $short_url) public static function insertShortUrl($url, $user_id, $short_url)
{ {
...@@ -83,15 +84,13 @@ class BotanDB extends DB ...@@ -83,15 +84,13 @@ class BotanDB extends DB
INSERT INTO `' . TB_BOTAN_SHORTENER . '` INSERT INTO `' . TB_BOTAN_SHORTENER . '`
(`user_id`, `url`, `short_url`, `created_at`) (`user_id`, `url`, `short_url`, `created_at`)
VALUES VALUES
(:user_id, :url, :short_url, :date) (:user_id, :url, :short_url, :created_at)
'); ');
$created_at = self::getTimestamp(); $sth->bindValue(':user_id', $user_id);
$sth->bindValue(':url', $url);
$sth->bindParam(':user_id', $user_id, PDO::PARAM_INT); $sth->bindValue(':short_url', $short_url);
$sth->bindParam(':url', $url, PDO::PARAM_STR); $sth->bindValue(':created_at', self::getTimestamp());
$sth->bindParam(':short_url', $short_url, PDO::PARAM_STR);
$sth->bindParam(':date', $created_at, PDO::PARAM_STR);
return $sth->execute(); return $sth->execute();
} catch (Exception $e) { } catch (Exception $e) {
......
...@@ -17,7 +17,7 @@ use PDO; ...@@ -17,7 +17,7 @@ use PDO;
class ConversationDB extends DB class ConversationDB extends DB
{ {
/** /**
* Initilize conversation table * Initialize conversation table
*/ */
public static function initializeConversation() public static function initializeConversation()
{ {
...@@ -29,12 +29,12 @@ class ConversationDB extends DB ...@@ -29,12 +29,12 @@ class ConversationDB extends DB
/** /**
* Select a conversation from the DB * Select a conversation from the DB
* *
* @param int $user_id * @param string $user_id
* @param int $chat_id * @param string $chat_id
* @param bool $limit * @param bool $limit
* *
* @return array|bool * @return array|bool
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws TelegramException
*/ */
public static function selectConversation($user_id, $chat_id, $limit = null) public static function selectConversation($user_id, $chat_id, $limit = null)
{ {
...@@ -43,39 +43,45 @@ class ConversationDB extends DB ...@@ -43,39 +43,45 @@ class ConversationDB extends DB
} }
try { try {
$query = 'SELECT * FROM `' . TB_CONVERSATION . '` '; $sql = '
$query .= 'WHERE `status` = :status '; SELECT *
$query .= 'AND `chat_id` = :chat_id '; FROM `' . TB_CONVERSATION . '`
$query .= 'AND `user_id` = :user_id '; WHERE `status` = :status
AND `chat_id` = :chat_id
AND `user_id` = :user_id
';
if ($limit !== null) { if ($limit !== null) {
$query .= ' LIMIT :limit'; $sql .= ' LIMIT :limit';
}
$sth = self::$pdo->prepare($sql);
$sth->bindValue(':status', 'active');
$sth->bindValue(':user_id', $user_id);
$sth->bindValue(':chat_id', $chat_id);
if ($limit !== null) {
$sth->bindValue(':limit', $limit, PDO::PARAM_INT);
} }
$sth = self::$pdo->prepare($query);
$status = 'active';
$sth->bindParam(':status', $status);
$sth->bindParam(':user_id', $user_id);
$sth->bindParam(':chat_id', $chat_id);
$sth->bindParam(':limit', $limit, PDO::PARAM_INT);
$sth->execute(); $sth->execute();
$results = $sth->fetchAll(PDO::FETCH_ASSOC); return $sth->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) { } catch (Exception $e) {
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} }
return $results;
} }
/** /**
* Insert the conversation in the database * Insert the conversation in the database
* *
* @param int $user_id * @param string $user_id
* @param int $chat_id * @param string $chat_id
* @param string $command * @param string $command
* *
* @return bool * @return bool
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws TelegramException
*/ */
public static function insertConversation($user_id, $chat_id, $command) public static function insertConversation($user_id, $chat_id, $command)
{ {
...@@ -85,31 +91,25 @@ class ConversationDB extends DB ...@@ -85,31 +91,25 @@ class ConversationDB extends DB
try { try {
$sth = self::$pdo->prepare('INSERT INTO `' . TB_CONVERSATION . '` $sth = self::$pdo->prepare('INSERT INTO `' . TB_CONVERSATION . '`
( (`status`, `user_id`, `chat_id`, `command`, `notes`, `created_at`, `updated_at`)
`status`, `user_id`, `chat_id`, `command`, `notes`, `created_at`, `updated_at` VALUES
) (:status, :user_id, :chat_id, :command, :notes, :created_at, :updated_at)
VALUES (
:status, :user_id, :chat_id, :command, :notes, :created_at, :updated_at
)
'); ');
$status = 'active'; $date = self::getTimestamp();
$notes = '[]';
$date = self::getTimestamp();
$sth->bindParam(':status', $status); $sth->bindValue(':status', 'active');
$sth->bindParam(':command', $command); $sth->bindValue(':command', $command);
$sth->bindParam(':user_id', $user_id); $sth->bindValue(':user_id', $user_id);
$sth->bindParam(':chat_id', $chat_id); $sth->bindValue(':chat_id', $chat_id);
$sth->bindParam(':notes', $notes); $sth->bindValue(':notes', '[]');
$sth->bindParam(':created_at', $date); $sth->bindValue(':created_at', $date);
$sth->bindParam(':updated_at', $date); $sth->bindValue(':updated_at', $date);
$status = $sth->execute(); return $sth->execute();
} catch (Exception $e) { } catch (Exception $e) {
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} }
return $status;
} }
/** /**
...@@ -119,69 +119,13 @@ class ConversationDB extends DB ...@@ -119,69 +119,13 @@ class ConversationDB extends DB
* @param array $where_fields_values * @param array $where_fields_values
* *
* @return bool * @return bool
* @throws TelegramException
*/ */
public static function updateConversation(array $fields_values, array $where_fields_values) public static function updateConversation(array $fields_values, array $where_fields_values)
{ {
return self::update(TB_CONVERSATION, $fields_values, $where_fields_values); // Auto update the update_at field.
}
/**
* Update the conversation in the database
*
* @param string $table
* @param array $fields_values
* @param array $where_fields_values
*
* @todo This function is generic should be moved in DB.php
*
* @return bool
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function update($table, array $fields_values, array $where_fields_values)
{
if (!self::isDbConnected()) {
return false;
}
//Auto update the field update_at
$fields_values['updated_at'] = self::getTimestamp(); $fields_values['updated_at'] = self::getTimestamp();
//Values return self::update(TB_CONVERSATION, $fields_values, $where_fields_values);
$update = '';
$tokens = [];
$tokens_counter = 0;
$a = 0;
foreach ($fields_values as $field => $value) {
if ($a) {
$update .= ', ';
}
++$a;
++$tokens_counter;
$update .= '`' . $field . '` = :' . $tokens_counter;
$tokens[':' . $tokens_counter] = $value;
}
//Where
$a = 0;
$where = '';
foreach ($where_fields_values as $field => $value) {
if ($a) {
$where .= ' AND ';
} else {
++$a;
$where .= 'WHERE ';
}
++$tokens_counter;
$where .= '`' . $field . '`= :' . $tokens_counter;
$tokens[':' . $tokens_counter] = $value;
}
$query = 'UPDATE `' . $table . '` SET ' . $update . ' ' . $where;
try {
$sth = self::$pdo->prepare($query);
$status = $sth->execute($tokens);
} catch (Exception $e) {
throw new TelegramException($e->getMessage());
}
return $status;
} }
} }
This diff is collapsed.
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