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