use sql statement

This commit is contained in:
Jean-Christian Denis 2023-04-23 23:30:36 +02:00
parent 28ccb5fd0c
commit b3f9dd1bce
Signed by: JcDenis
GPG key ID: 1B5B8C5B90B6C951

View file

@ -18,6 +18,10 @@ use dcAuth;
use dcBlog; use dcBlog;
use dcCore; use dcCore;
use dcUtils; use dcUtils;
use Dotclear\Database\Statement\{
JoinStatement,
SelectStatement
};
use Dotclear\Helper\Date; use Dotclear\Helper\Date;
class Utils class Utils
@ -29,28 +33,46 @@ class Utils
return []; return [];
} }
$req = 'SELECT COUNT(*) AS count, U.user_id ' . $sql = new SelectStatement();
'FROM ' . dcCore::app()->prefix . dcBlog::POST_TABLE_NAME . ' P ' . $sql
'INNER JOIN ' . dcCore::app()->prefix . dcAuth::USER_TABLE_NAME . ' U ON U.user_id = P.user_id ' . ->from($sql->as(dcCore::app()->prefix . dcBlog::POST_TABLE_NAME, 'P'))
"WHERE blog_id='" . dcCore::app()->con->escapeStr((string) dcCore::app()->blog->id) . "' " . ->columns([
'AND post_status=1 AND user_status=1 ' . $sql->count('*', 'count'),
self::period('post_dt', $period) . 'U.user_id'
'GROUP BY U.user_id ' . ])
'ORDER BY count ' . ($sort_desc ? 'DESC' : 'ASC') . ' , U.user_id ASC ' . ->join(
dcCore::app()->con->limit(abs((int) $limit)); (new JoinStatement())
->inner()
->from($sql->as(dcCore::app()->prefix . dcAuth::USER_TABLE_NAME, 'U'))
->on('U.user_id = P.user_id')
->statement()
)
->where('blog_id = ' . $sql->quote(dcCore::app()->blog->id))
->and('post_status = ' . dcBlog::POST_PUBLISHED)
->and('user_status = 1')
->group('U.user_id')
->order('count ' . ($sort_desc ? 'DESC' : 'ASC') . ' , U.user_id ASC')
->limit(abs((int) $limit));
$rs = dcCore::app()->con->select($req); self::period($sql, $period, 'post_dt');
if ($rs->isEmpty()) {
$rs = $sql->select();
if (is_null($rs) || $rs->isEmpty()) {
return []; return [];
} }
$res = []; $res = [];
$i = 0; $i = 0;
while ($rs->fetch()) { while ($rs->fetch()) {
$user = dcCore::app()->con->select( $sql = new SelectStatement();
'SELECT * FROM ' . dcCore::app()->prefix . dcAuth::USER_TABLE_NAME . " WHERE user_id='" . $rs->f('user_id') . "' " $user = $sql
); ->from(dcCore::app()->prefix . dcAuth::USER_TABLE_NAME)
if ($user->isEmpty()) { ->column('*')
->where('user_id = ' . $sql->quote($rs->f('user_id')))
->select();
if (is_null($user) || $user->isEmpty()) {
continue; continue;
} }
@ -92,41 +114,60 @@ class Utils
return []; return [];
} }
$req = 'SELECT COUNT(*) AS count, comment_email ' . $sql = new SelectStatement();
'FROM ' . dcCore::app()->prefix . dcBlog::POST_TABLE_NAME . ' P, ' . dcCore::app()->prefix . dcBlog::COMMENT_TABLE_NAME . ' C ' . $sql
'WHERE P.post_id=C.post_id ' . ->from($sql->as(dcCore::app()->prefix . dcBlog::POST_TABLE_NAME, 'P'))
"AND blog_id='" . dcCore::app()->con->escapeStr((string) dcCore::app()->blog->id) . "' " . ->from($sql->as(dcCore::app()->prefix . dcBlog::COMMENT_TABLE_NAME, 'C'))
'AND post_status=1 AND comment_status=1 ' . ->columns([
self::period('comment_dt', $period); $sql->count('*', 'count'),
'comment_email'
])
->where('blog_id = ' . $sql->quote(dcCore::app()->blog->id))
->and('P.post_id = C.post_id')
->and('post_status = ' . dcBlog::POST_PUBLISHED)
->and('comment_status = ' . dcBlog::COMMENT_PUBLISHED)
->group('comment_email')
->order('count ' . ($sort_desc ? 'DESC' : 'ASC'))
->limit(abs((int) $limit))
;
self::period($sql, $period, 'comment_dt');
if ($exclude) { if ($exclude) {
$req .= 'AND comment_email NOT IN (' . $sql->and('comment_email NOT IN (' .
' SELECT U.user_email ' . (new SelectStatement())
' FROM ' . dcCore::app()->prefix . dcAuth::USER_TABLE_NAME . ' U' . ->from($sql->as(dcCore::app()->prefix . dcAuth::USER_TABLE_NAME, 'U'))
' INNER JOIN ' . dcCore::app()->prefix . dcBlog::POST_TABLE_NAME . ' P ON P.user_id = U.user_id ' . ->column('U.user_email')
" WHERE blog_id='" . dcCore::app()->con->escapeStr((string) dcCore::app()->blog->id) . "' " . ->join(
' GROUP BY U.user_email) '; (new JoinStatement())
->inner()
->from($sql->as(dcCore::app()->prefix . dcBlog::POST_TABLE_NAME, 'P'))
->on('P.user_id = U.user_id')
->statement()
)
->where('blog_id = ' . $sql->quote(dcCore::app()->blog->id))
->group('U.user_email')
->statement() .
')');
} }
$req .= 'GROUP BY comment_email ' . $rs = $sql->select();
'ORDER BY count ' . ($sort_desc ? 'DESC' : 'ASC') . ' ' . if (is_null($rs) || $rs->isEmpty()) {
dcCore::app()->con->limit(abs((int) $limit));
$rs = dcCore::app()->con->select($req);
if ($rs->isEmpty()) {
return []; return [];
} }
$res = []; $res = [];
$i = 0; $i = 0;
while ($rs->fetch()) { while ($rs->fetch()) {
$user = dcCore::app()->con->select( $sql = new SelectStatement();
'SELECT * FROM ' . dcCore::app()->prefix . dcBlog::COMMENT_TABLE_NAME . ' ' . $user = $sql
"WHERE comment_email='" . $rs->f('comment_email') . "' " . ->from(dcCore::app()->prefix . dcBlog::COMMENT_TABLE_NAME)
'ORDER BY comment_dt DESC' ->column('*')
); ->where('comment_email = ' . $sql->quote($rs->f('comment_email')))
->order('comment_dt DESC')
->select();
if (!$user->f('comment_author')) { if (is_null($user) || !$user->f('comment_author')) {
continue; continue;
} }
@ -148,7 +189,7 @@ class Utils
return $i ? $res : []; return $i ? $res : [];
} }
private static function period(string $field, string $period): string private static function period(SelectStatement $sql, string $period, string $field): void
{ {
$pattern = '%Y-%m-%d %H:%M:%S'; $pattern = '%Y-%m-%d %H:%M:%S';
$time = 0; $time = 0;
@ -174,10 +215,10 @@ class Utils
break; break;
default: default:
return ''; return;
} }
return "AND $field > TIMESTAMP '" . Date::str($pattern, time() - $time) . "' "; $sql->and($field . ' > TIMESTAMP ' . $sql->quote(Date::str($pattern, time() - $time)));
} }
public static function periods(): array public static function periods(): array