Aliases are added to Select queries

This commit is contained in:
Visman 2018-07-25 10:51:05 +07:00
parent f942d45e33
commit 9d9cbea844
20 changed files with 76 additions and 76 deletions

View file

@ -15,7 +15,7 @@ class Load extends Method
*/
public function load()
{
$list = $this->c->DB->query('SELECT id FROM ::users WHERE group_id=?i', [$this->c->GROUP_ADMIN])->fetchAll(PDO::FETCH_COLUMN);
$list = $this->c->DB->query('SELECT u.id FROM ::users AS u WHERE u.group_id=?i', [$this->c->GROUP_ADMIN])->fetchAll(PDO::FETCH_COLUMN);
$this->model->list = $list;
$this->c->Cache->set('admins', $list);
return $this->model;

View file

@ -17,7 +17,7 @@ class Load extends Method
$userList = [];
$ipList = [];
$otherList = [];
$stmt = $this->c->DB->query('SELECT id, username, ip, email, message, expire FROM ::bans');
$stmt = $this->c->DB->query('SELECT b.id, b.username, b.ip, b.email, b.message, b.expire FROM ::bans AS b');
while ($row = $stmt->fetch()) {
$name = $this->model->trimToNull($row['username'], true);
if (null !== $name) {

View file

@ -23,9 +23,9 @@ class Manager extends ManagerModel
*/
public function init()
{
$sql = 'SELECT id, cat_name, disp_position
FROM ::categories
ORDER BY disp_position';
$sql = 'SELECT c.id, c.cat_name, c.disp_position
FROM ::categories AS c
ORDER BY c.disp_position';
$this->repository = $this->c->DB->query($sql)->fetchAll(PDO::FETCH_UNIQUE);
return $this;
}

View file

@ -14,9 +14,9 @@ class Load extends Method
*/
public function load()
{
$sql = 'SELECT id, search_for, replace_with
FROM ::censoring
ORDER BY REPLACE(search_for, \'*\', \'\')';
$sql = 'SELECT ce.id, ce.search_for, ce.replace_with
FROM ::censoring AS ce
ORDER BY REPLACE(ce.search_for, \'*\', \'\')';
return $this->c->DB->query($sql)->fetchAll(PDO::FETCH_UNIQUE);
}
}

View file

@ -14,7 +14,7 @@ class Refresh extends Method
*/
public function refresh()
{
$stmt = $this->c->DB->query('SELECT id, search_for, replace_with FROM ::censoring');
$stmt = $this->c->DB->query('SELECT ce.id, ce.search_for, ce.replace_with FROM ::censoring AS ce');
$search = [];
$replace = [];
while ($row = $stmt->fetch()) {

View file

@ -15,7 +15,7 @@ class Load extends Method
*/
public function load()
{
$config = $this->c->DB->query('SELECT conf_name, conf_value FROM ::config')->fetchAll(PDO::FETCH_KEY_PAIR);
$config = $this->c->DB->query('SELECT cf.conf_name, cf.conf_value FROM ::config AS cf')->fetchAll(PDO::FETCH_KEY_PAIR);
$this->model->setAttrs($config);
$this->c->Cache->set('config', $config);
return $this->model;

View file

@ -9,9 +9,9 @@ class CalcStat extends Method
{
/**
* Пересчитывает статистику
*
*
* @throws RuntimeException
*
*
* @return Forum
*/
public function calcStat()
@ -21,19 +21,19 @@ class CalcStat extends Method
}
$vars = [':fid' => $this->model->id];
$sql = 'SELECT COUNT(id) as num_topics, SUM(num_replies) as num_replies
FROM ::topics
WHERE forum_id=?i:fid';
$sql = 'SELECT COUNT(t.id) as num_topics, SUM(t.num_replies) as num_replies
FROM ::topics AS t
WHERE t.forum_id=?i:fid';
$result = $this->c->DB->query($sql, $vars)->fetch();
$this->model->num_topics = $result['num_topics'];
$this->model->num_posts = $result['num_topics'] + $result['num_replies'];
$sql = 'SELECT last_post, last_post_id, last_poster, subject as last_topic
FROM ::topics
WHERE forum_id=?i:fid AND moved_to IS NULL
ORDER BY last_post DESC
$sql = 'SELECT t.last_post, t.last_post_id, t.last_poster, t.subject as last_topic
FROM ::topics AS t
WHERE t.forum_id=?i:fid AND t.moved_to IS NULL
ORDER BY t.last_post DESC
LIMIT 1';
$result = $this->c->DB->query($sql, $vars)->fetch();

View file

@ -255,13 +255,13 @@ class Model extends DataModel
switch ($this->sort_by) {
case 1:
$sortBy = 'posted DESC';
$sortBy = 't.posted DESC';
break;
case 2:
$sortBy = 'subject ASC';
$sortBy = 't.subject ASC';
break;
default:
$sortBy = 'last_post DESC';
$sortBy = 't.last_post DESC';
break;
}
@ -270,10 +270,10 @@ class Model extends DataModel
':offset' => ($this->page - 1) * $this->c->user->disp_topics,
':rows' => $this->c->user->disp_topics,
];
$sql = "SELECT id
FROM ::topics
WHERE forum_id=?i:fid
ORDER BY sticky DESC, {$sortBy}, id DESC
$sql = "SELECT t.id
FROM ::topics AS t
WHERE t.forum_id=?i:fid
ORDER BY t.sticky DESC, {$sortBy}, t.id DESC
LIMIT ?i:offset, ?i:rows";
$this->idsList = $this->c->DB->query($sql, $vars)->fetchAll(PDO::FETCH_COLUMN);

View file

@ -16,9 +16,9 @@ class Manager extends ManagerModel
/**
* Создает новую модель раздела
*
*
* @param array $attrs
*
*
* @return Group
*/
public function create(array $attrs = [])
@ -33,13 +33,13 @@ class Manager extends ManagerModel
/**
* Загрузка списка групп
*
*
* @return Manager
*/
public function init()
{
if (empty($this->flag)) {
$stmt = $this->c->DB->query('SELECT * FROM ::groups ORDER BY g_id');
$stmt = $this->c->DB->query('SELECT g.* FROM ::groups AS g ORDER BY g.g_id');
while ($row = $stmt->fetch()) {
$this->set($row['g_id'], $this->create($row));
}
@ -50,9 +50,9 @@ class Manager extends ManagerModel
/**
* Получение модели группы
*
*
* @param int $id
*
*
* @return null|Group
*/
public function get($id)
@ -66,7 +66,7 @@ class Manager extends ManagerModel
* Обновляет группу в БД
*
* @param Group $group
*
*
* @return Group
*/
public function update(Group $group)
@ -78,7 +78,7 @@ class Manager extends ManagerModel
* Добавляет новую группу в БД
*
* @param Group $group
*
*
* @return int
*/
public function insert(Group $group)

View file

@ -45,9 +45,9 @@ class Online extends Model
$deleteU = false;
if ($detail) {
$sql = 'SELECT user_id, ident, logged, o_position, o_name FROM ::online ORDER BY logged';
$sql = 'SELECT o.user_id, o.ident, o.logged, o.o_position, o.o_name FROM ::online AS o ORDER BY o.logged';
} else {
$sql = 'SELECT user_id, ident, logged FROM ::online ORDER BY logged';
$sql = 'SELECT o.user_id, o.ident, o.logged FROM ::online AS o ORDER BY o.logged';
}
$stmt = $this->c->DB->query($sql);

View file

@ -89,9 +89,9 @@ class View extends Action
$vars = [
':ids' => $arg->idsList,
];
$sql = 'SELECT id, message, poster, posted
FROM ::warnings
WHERE id IN (?ai:ids)';
$sql = 'SELECT w.id, w.message, w.poster, w.posted
FROM ::warnings AS w
WHERE w.id IN (?ai:ids)';
$warnings = $this->c->DB->query($sql, $vars)->fetchAll(PDO::FETCH_GROUP);
$userIds = [];

View file

@ -55,10 +55,10 @@ class Execute extends Method
$vars = [
':key' => $key,
];
$sql = 'SELECT search_time, search_data
FROM ::search_cache
WHERE search_key=?s:key
ORDER BY search_time DESC
$sql = 'SELECT sc.search_time, sc.search_data
FROM ::search_cache AS sc
WHERE sc.search_key=?s:key
ORDER BY sc.search_time DESC
LIMIT 1';
$row = $this->c->DB->query($sql, $vars)->fetch();
@ -215,7 +215,7 @@ class Execute extends Method
switch ($v->serch_in) {
case 1:
$whereIdx[] = 'm.subject_match=0';
$whereIdx[] = 'sm.subject_match=0';
$whereCJK[] = 'p.message LIKE ?s:word';
$usePCJK = true;
if (isset($vars[':author'])) {
@ -223,7 +223,7 @@ class Execute extends Method
}
break;
case 2:
$whereIdx[] = 'm.subject_match=1';
$whereIdx[] = 'sm.subject_match=1';
$whereCJK[] = 't.subject LIKE ?s:word';
$useTCJK = true;
if (isset($vars[':author'])) {
@ -249,7 +249,7 @@ class Execute extends Method
$selectFCJK = 't.id';
$useTCJK = true;
} else {
$selectFIdx = 'm.post_id';
$selectFIdx = 'sm.post_id';
$selectFCJK = 'p.id';
$usePCJK = true;
}
@ -290,7 +290,7 @@ class Execute extends Method
$useTIdx = true;
$useTCJK = true;
} else {
$sortIdx = 'm.post_id';
$sortIdx = 'sm.post_id';
$sortCJK = 'p.id';
$usePCJK = true;
}
@ -298,15 +298,15 @@ class Execute extends Method
break;
}
$usePIdx = $usePIdx || $useTIdx ? 'INNER JOIN ::posts AS p ON p.id=m.post_id ' : '';
$usePIdx = $usePIdx || $useTIdx ? 'INNER JOIN ::posts AS p ON p.id=sm.post_id ' : '';
$useTIdx = $useTIdx ? 'INNER JOIN ::topics AS t ON t.id=p.topic_id ' : '';
$whereIdx = empty($whereIdx) ? '' : ' AND ' . \implode(' AND ', $whereIdx);
$this->queryIdx = "SELECT {$selectFIdx}, {$sortIdx} FROM ::search_words AS w " .
'INNER JOIN ::search_matches AS m ON m.word_id=w.id ' .
$this->queryIdx = "SELECT {$selectFIdx}, {$sortIdx} FROM ::search_words AS sw " .
'INNER JOIN ::search_matches AS sm ON sm.word_id=sw.id ' .
$usePIdx .
$useTIdx .
'WHERE w.word LIKE ?s:word' . $whereIdx;
'WHERE sw.word LIKE ?s:word' . $whereIdx;
if ($usePCJK) {
$this->queryCJK = "SELECT {$selectFCJK}, {$sortCJK} FROM ::posts AS p " .

View file

@ -29,10 +29,10 @@ class Index extends Method
$vars = [
':pid' => $post->id,
];
$sql = 'SELECT w.id, w.word, m.subject_match
FROM ::search_words AS w
INNER JOIN ::search_matches AS m ON w.id=m.word_id
WHERE m.post_id=?i:pid';
$sql = 'SELECT sw.id, sw.word, sm.subject_match
FROM ::search_words AS sw
INNER JOIN ::search_matches AS sm ON sw.id=sm.word_id
WHERE sm.post_id=?i:pid';
$stmt = $this->c->DB->query($sql, $vars);
$mesCurWords = [];
@ -73,9 +73,9 @@ class Index extends Method
$vars = [
':words' => $allWords,
];
$sql = 'SELECT word
FROM ::search_words
WHERE word IN(?as:words)';
$sql = 'SELECT sw.word
FROM ::search_words AS sw
WHERE sw.word IN(?as:words)';
$oldWords = $this->c->DB->query($sql, $vars)->fetchAll(PDO::FETCH_COLUMN);
$newWords = \array_diff($allWords, $oldWords);

View file

@ -15,7 +15,7 @@ class Load extends Method
*/
public function load()
{
$list = $this->c->DB->query('SELECT text, image FROM ::smilies ORDER BY disp_position')->fetchAll(PDO::FETCH_KEY_PAIR); //???? text уникальное?
$list = $this->c->DB->query('SELECT sm.text, sm.image FROM ::smilies AS sm ORDER BY sm.disp_position')->fetchAll(PDO::FETCH_KEY_PAIR); //???? text уникальное?
$this->model->list = $list;
$this->c->Cache->set('smilies', $list);
return $this->model;

View file

@ -22,7 +22,7 @@ class Stats extends Model
$this->load();
}
list($this->topicTotal, $this->postTotal) = $this->c->DB->query('SELECT SUM(num_topics), SUM(num_posts) FROM ::forums')->fetch(PDO::FETCH_NUM);
list($this->topicTotal, $this->postTotal) = $this->c->DB->query('SELECT SUM(f.num_topics), SUM(f.num_posts) FROM ::forums AS f')->fetch(PDO::FETCH_NUM);
return $this;
}

View file

@ -14,8 +14,8 @@ class Load extends Method
*/
public function load()
{
$total = $this->c->DB->query('SELECT COUNT(id)-1 FROM ::users WHERE group_id!=0')->fetchColumn();
$last = $this->c->DB->query('SELECT id, username FROM ::users WHERE group_id!=0 ORDER BY registered DESC LIMIT 1')->fetch();
$total = $this->c->DB->query('SELECT COUNT(u.id)-1 FROM ::users AS u WHERE u.group_id!=0')->fetchColumn();
$last = $this->c->DB->query('SELECT u.id, u.username FROM ::users AS u WHERE u.group_id!=0 ORDER BY u.registered DESC LIMIT 1')->fetch();
$this->model->userTotal = $total;
$this->model->userLast = $last;
$this->c->Cache->set('stats', [

View file

@ -153,7 +153,7 @@ class Model extends DataModel
':tid' => $this->id,
':visit' => $this->hasNew,
];
$sql = 'SELECT MIN(id) FROM ::posts WHERE topic_id=?i:tid AND posted>?i:visit';
$sql = 'SELECT MIN(p.id) FROM ::posts AS p WHERE p.topic_id=?i:tid AND p.posted>?i:visit';
$pid = $this->c->DB->query($sql, $vars)->fetchColumn();
@ -175,7 +175,7 @@ class Model extends DataModel
':tid' => $this->id,
':visit' => $this->hasUnread,
];
$sql = 'SELECT MIN(id) FROM ::posts WHERE topic_id=?i:tid AND posted>?i:visit';
$sql = 'SELECT MIN(p.id) FROM ::posts AS p WHERE p.topic_id=?i:tid AND p.posted>?i:visit';
$pid = $this->c->DB->query($sql, $vars)->fetchColumn();
@ -243,10 +243,10 @@ class Model extends DataModel
':offset' => ($this->page - 1) * $this->c->user->disp_posts,
':rows' => $this->c->user->disp_posts,
];
$sql = 'SELECT id
FROM ::posts
WHERE topic_id=?i:tid
ORDER BY id
$sql = 'SELECT p.id
FROM ::posts AS p
WHERE p.topic_id=?i:tid
ORDER BY p.id
LIMIT ?i:offset, ?i:rows';
$list = $this->c->DB->query($sql, $vars)->fetchAll(PDO::FETCH_COLUMN);

View file

@ -42,10 +42,10 @@ class View extends Action
];
if (! $this->c->user->isGuest && '1' == $this->c->config->o_show_dot) {
$sql = 'SELECT topic_id
FROM ::posts
WHERE poster_id=?i:uid AND topic_id IN (?ai:ids)
GROUP BY topic_id';
$sql = 'SELECT p.topic_id
FROM ::posts AS p
WHERE p.poster_id=?i:uid AND p.topic_id IN (?ai:ids)
GROUP BY p.topic_id';
$dots = $this->c->DB->query($sql, $vars)->fetchAll(PDO::FETCH_COLUMN);
$dots = \array_flip($dots);
} else {

View file

@ -21,7 +21,7 @@ class IsUniqueName extends Action
':name' => $user->username,
':other' => \preg_replace('%[^\p{L}\p{N}]%u', '', $user->username), //???? что за бред :)
];
$result = $this->c->DB->query('SELECT username FROM ::users WHERE (LOWER(username)=LOWER(?s:name) OR LOWER(username)=LOWER(?s:other)) AND id!=?i:id', $vars)->fetchAll();
$result = $this->c->DB->query('SELECT u.username FROM ::users AS u WHERE (LOWER(u.username)=LOWER(?s:name) OR LOWER(u.username)=LOWER(?s:other)) AND u.id!=?i:id', $vars)->fetchAll();
return ! \count($result);
}
}

View file

@ -9,9 +9,9 @@ class UsersNumber extends Action
{
/**
* Подсчет количества пользователей в группе
*
*
* @param Group $group
*
*
* @return int
*/
public function UsersNumber(Group $group)
@ -23,7 +23,7 @@ class UsersNumber extends Action
$vars = [
':gid' => $group->g_id,
];
$sql = 'SELECT COUNT(id) FROM ::users WHERE group_id=?i:gid';
$sql = 'SELECT COUNT(u.id) FROM ::users AS u WHERE u.group_id=?i:gid';
return $this->c->DB->query($sql, $vars)->fetchColumn();
}