Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
TelegramBot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
TelegramBot
Commits
7f07f73f
Commit
7f07f73f
authored
Sep 11, 2017
by
Armando Lüscher
Committed by
GitHub
Sep 11, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #618 from noplanman/clean_db_code
Clean up DB code
parents
57858427
bce79590
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
392 additions
and
439 deletions
+392
-439
CHANGELOG.md
CHANGELOG.md
+2
-0
BotanDB.php
src/BotanDB.php
+19
-20
ConversationDB.php
src/ConversationDB.php
+42
-98
DB.php
src/DB.php
+329
-321
No files found.
CHANGELOG.md
View file @
7f07f73f
...
...
@@ -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
...
...
src/BotanDB.php
View file @
7f07f73f
...
...
@@ -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
* Initi
a
lize 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
->
bind
Param
(
':user_id'
,
$user_id
,
PDO
::
PARAM_INT
);
$sth
->
bind
Param
(
':url'
,
$url
,
PDO
::
PARAM_STR
);
$sth
->
bind
Value
(
':user_id'
,
$user_id
);
$sth
->
bind
Value
(
':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
)
{
...
...
src/ConversationDB.php
View file @
7f07f73f
...
...
@@ -17,7 +17,7 @@ use PDO;
class
ConversationDB
extends
DB
{
/**
* Initilize conversation table
* Initi
a
lize 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
->
bind
Param
(
':status'
,
$status
);
$sth
->
bind
Param
(
':command'
,
$command
);
$sth
->
bind
Param
(
':user_id'
,
$user_id
);
$sth
->
bind
Param
(
':chat_id'
,
$chat_id
);
$sth
->
bind
Param
(
':notes'
,
$notes
);
$sth
->
bind
Param
(
':created_at'
,
$date
);
$sth
->
bind
Param
(
':updated_at'
,
$date
);
$sth
->
bind
Value
(
':status'
,
'active'
);
$sth
->
bind
Value
(
':command'
,
$command
);
$sth
->
bind
Value
(
':user_id'
,
$user_id
);
$sth
->
bind
Value
(
':chat_id'
,
$chat_id
);
$sth
->
bind
Value
(
':notes'
,
'[]'
);
$sth
->
bind
Value
(
':created_at'
,
$date
);
$sth
->
bind
Value
(
':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
);
}
}
src/DB.php
View file @
7f07f73f
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment