diff --git a/lib/chibi-sql b/lib/chibi-sql index 9b27422f..22910a18 160000 --- a/lib/chibi-sql +++ b/lib/chibi-sql @@ -1 +1 @@ -Subproject commit 9b27422f780840f9814d8fc49017716cf6e7fea6 +Subproject commit 22910a186efbcb9bc86a3ae3eb6f4aff34096406 diff --git a/src/Models/SearchParsers/TagSearchParser.php b/src/Models/SearchParsers/TagSearchParser.php index ff6cd484..d359b0a0 100644 --- a/src/Models/SearchParsers/TagSearchParser.php +++ b/src/Models/SearchParsers/TagSearchParser.php @@ -31,7 +31,7 @@ class TagSearchParser extends AbstractSearchParser if ($orderByString == 'popularity') $this->statement->setOrderBy('post_count', $orderDir); elseif ($orderByString == 'alpha') - $this->statement->setOrderBy('tag.name', $orderDir); + $this->statement->setOrderBy(new Sql\CaseFunctor('tag.name'), $orderDir); else return false; return true; diff --git a/src/Models/SearchServices/AbstractSearchService.php b/src/Models/SearchServices/AbstractSearchService.php index ebd34de4..fcea3c9c 100644 --- a/src/Models/SearchServices/AbstractSearchService.php +++ b/src/Models/SearchServices/AbstractSearchService.php @@ -72,7 +72,7 @@ abstract class AbstractSearchService $stmt = new Sql\SelectStatement(); $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); - $stmt->setSource($innerStmt); + $stmt->setSource($innerStmt, 'inner_stmt'); return Database::fetchOne($stmt)['count']; } diff --git a/src/Upgrades/mysql/Upgrade12.sql b/src/Upgrades/mysql/Upgrade12.sql index da5e4a5a..0286f5cc 100644 --- a/src/Upgrades/mysql/Upgrade12.sql +++ b/src/Upgrades/mysql/Upgrade12.sql @@ -1,12 +1,19 @@ ALTER TABLE post ADD COLUMN comment_date INTEGER DEFAULT NULL; -CREATE TRIGGER comment_update_date AFTER UPDATE ON comment FOR EACH ROW +DROP TRIGGER comment_update; + +CREATE TRIGGER comment_update AFTER UPDATE ON comment FOR EACH ROW BEGIN + UPDATE post SET comment_count = comment_count + 1 WHERE post.id = new.post_id; + UPDATE post SET comment_count = comment_count - 1 WHERE post.id = old.post_id; UPDATE post SET comment_date = (SELECT MAX(comment_date) FROM comment WHERE comment.post_id = post.id); END; -CREATE TRIGGER comment_insert_date AFTER INSERT ON comment FOR EACH ROW +DROP TRIGGER comment_insert; + +CREATE TRIGGER comment_insert AFTER INSERT ON comment FOR EACH ROW BEGIN + UPDATE post SET comment_count = comment_count + 1 WHERE post.id = new.post_id; UPDATE post SET comment_date = (SELECT MAX(comment_date) FROM comment WHERE comment.post_id = post.id); END; diff --git a/src/Upgrades/mysql/Upgrade13.sql b/src/Upgrades/mysql/Upgrade13.sql index 34dc94da..1abaa8bc 100644 --- a/src/Upgrades/mysql/Upgrade13.sql +++ b/src/Upgrades/mysql/Upgrade13.sql @@ -1,17 +1,27 @@ ALTER TABLE favoritee ADD COLUMN fav_date INTEGER DEFAULT NULL; ALTER TABLE post ADD COLUMN fav_date INTEGER DEFAULT NULL; -CREATE TRIGGER favoritee_update_date AFTER UPDATE ON favoritee FOR EACH ROW +DROP TRIGGER favoritee_update; + +CREATE TRIGGER favoritee_update AFTER UPDATE ON favoritee FOR EACH ROW BEGIN + UPDATE post SET fav_count = fav_count + 1 WHERE post.id = new.post_id; + UPDATE post SET fav_count = fav_count - 1 WHERE post.id = old.post_id; UPDATE post SET fav_date = (SELECT MAX(fav_date) FROM favoritee WHERE favoritee.post_id = post.id); END; -CREATE TRIGGER favoritee_insert_date AFTER INSERT ON favoritee FOR EACH ROW +DROP TRIGGER favoritee_insert; + +CREATE TRIGGER favoritee_insert AFTER INSERT ON favoritee FOR EACH ROW BEGIN + UPDATE post SET fav_count = fav_count + 1 WHERE post.id = new.post_id; UPDATE post SET fav_date = (SELECT MAX(fav_date) FROM favoritee WHERE favoritee.post_id = post.id); END; -CREATE TRIGGER favoritee_delete_date AFTER DELETE ON favoritee FOR EACH ROW +DROP TRIGGER favoritee_delete; + +CREATE TRIGGER favoritee_delete AFTER DELETE ON favoritee FOR EACH ROW BEGIN + UPDATE post SET fav_count = fav_count - 1 WHERE post.id = old.post_id; UPDATE post SET fav_date = (SELECT MAX(fav_date) FROM favoritee WHERE favoritee.post_id = post.id); END; diff --git a/src/Upgrades/mysql/Upgrade14.sql b/src/Upgrades/mysql/Upgrade14.sql new file mode 100644 index 00000000..e69de29b diff --git a/src/Upgrades/sqlite/Upgrade14.sql b/src/Upgrades/sqlite/Upgrade14.sql new file mode 100644 index 00000000..9ee28036 --- /dev/null +++ b/src/Upgrades/sqlite/Upgrade14.sql @@ -0,0 +1,48 @@ +--merge multiple triggers into singular ones + +DROP TRIGGER comment_update; +DROP TRIGGER comment_update_date; + +CREATE TRIGGER comment_update AFTER UPDATE ON comment FOR EACH ROW +BEGIN + UPDATE post SET comment_count = comment_count + 1 WHERE post.id = new.post_id; + UPDATE post SET comment_count = comment_count - 1 WHERE post.id = old.post_id; + UPDATE post SET comment_date = (SELECT MAX(comment_date) FROM comment WHERE comment.post_id = post.id); +END; + +DROP TRIGGER comment_insert; +DROP TRIGGER comment_insert_date; + +CREATE TRIGGER comment_insert AFTER INSERT ON comment FOR EACH ROW +BEGIN + UPDATE post SET comment_count = comment_count + 1 WHERE post.id = new.post_id; + UPDATE post SET comment_date = (SELECT MAX(comment_date) FROM comment WHERE comment.post_id = post.id); +END; + +DROP TRIGGER favoritee_update; +DROP TRIGGER favoritee_update_date; + +CREATE TRIGGER favoritee_update AFTER UPDATE ON favoritee FOR EACH ROW +BEGIN + UPDATE post SET fav_count = fav_count + 1 WHERE post.id = new.post_id; + UPDATE post SET fav_count = fav_count - 1 WHERE post.id = old.post_id; + UPDATE post SET fav_date = (SELECT MAX(fav_date) FROM favoritee WHERE favoritee.post_id = post.id); +END; + +DROP TRIGGER favoritee_insert; +DROP TRIGGER favoritee_insert_date; + +CREATE TRIGGER favoritee_insert AFTER INSERT ON favoritee FOR EACH ROW +BEGIN + UPDATE post SET fav_count = fav_count + 1 WHERE post.id = new.post_id; + UPDATE post SET fav_date = (SELECT MAX(fav_date) FROM favoritee WHERE favoritee.post_id = post.id); +END; + +DROP TRIGGER favoritee_delete; +DROP TRIGGER favoritee_delete_date; + +CREATE TRIGGER favoritee_delete AFTER DELETE ON favoritee FOR EACH ROW +BEGIN + UPDATE post SET fav_count = fav_count - 1 WHERE post.id = old.post_id; + UPDATE post SET fav_date = (SELECT MAX(fav_date) FROM favoritee WHERE favoritee.post_id = post.id); +END; diff --git a/upgrade.php b/upgrade.php index 65deffff..fb417df7 100644 --- a/upgrade.php +++ b/upgrade.php @@ -57,7 +57,7 @@ foreach ($upgrades as $upgradePath) { try { - \Chibi\Database::exec(new \Chibi\Sql\RawStatement($query)); + \Chibi\Database::execUnprepared(new \Chibi\Sql\RawStatement($query)); } catch (Exception $e) {