Skip to content

Commit

Permalink
Disable usage column on MySQL installs
Browse files Browse the repository at this point in the history
Closes #12
  • Loading branch information
Tam committed Jan 15, 2020
1 parent 7f2773f commit 8f57073
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [Unreleased] 1.0.6
## 1.0.6 - 2020-01-15
### Fixed
- Fix issue with pgsql when usage column is enabled
- Disable usage column on MySQL installs

## 1.0.5 - 2019-06-26
### Added
Expand Down
6 changes: 5 additions & 1 deletion src/elements/db/TagQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace ether\tagManager\elements\db;

use Craft;
use ether\tagManager\TagManager;
use yii\db\Expression;

Expand All @@ -22,14 +23,17 @@ class TagQuery extends \craft\elements\db\TagQuery

protected function afterPrepare (): bool
{
if (Craft::$app->getDb()->getDriverName() === 'mysql')
return parent::afterPrepare();

if (!TagManager::getInstance()->getSettings()->enableUsage)
return parent::afterPrepare();

if (count($this->query->select) === 1 && strtoupper($this->query->select[0]) === 'COUNT(*)')
return parent::afterPrepare();

$getUsage = new Expression(
'(SELECT COUNT(*) FROM (SELECT [[r.sourceId]], [[r.sourceSiteId]] FROM {{%relations}} r WHERE [[r.targetId]] = [[elements.id]] GROUP BY [[r.sourceId]], [[r.sourceSiteId]]) as usage) as [[usage]]'
'(SELECT COUNT(*) FROM (SELECT [[r.sourceId]], [[r.sourceSiteId]] FROM {{%relations}} r WHERE [[r.targetId]] = [[elements.id]] GROUP BY [[r.sourceId]], [[r.sourceSiteId]]) as [[usage]]) as [[usage]]'
);

$this->query->addSelect(new Expression('[[subquery.usage]] as [[usage]]'));
Expand Down
5 changes: 3 additions & 2 deletions src/templates/_settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

{{ forms.lightswitchField({
label: 'Enable Usage'|t('tag-manager'),
instructions: 'Will enable the usage column in the CP (has no effect on regular tag queries, may be slow on larger sites).'|t('tag-manager'),
instructions: '**[PostgreSQL Only]** Will enable the usage column in the CP (has no effect on regular tag queries, may be slow on larger sites).'|t('tag-manager'),
name: 'enableUsage',
on: settings.enableUsage,
on: craft.app.db.driverName == 'pgsql' and settings.enableUsage,
first: true,
disabled: craft.app.db.driverName == 'mysql'
}) }}
4 changes: 2 additions & 2 deletions src/translations/en/tag-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
return [
'Usage' => 'Usage',
'Enable Usage' => 'Enable Usage',
'Will enable the usage column in the CP (has no effect on regular tag queries, may be slow on larger sites).' =>
'Will enable the usage column in the CP (has no effect on regular tag queries, may be slow on larger sites).',
'**[PostgreSQL Only]** Will enable the usage column in the CP (has no effect on regular tag queries, may be slow on larger sites).' =>
'**[PostgreSQL Only]** Will enable the usage column in the CP (has no effect on regular tag queries, may be slow on larger sites).',
];

0 comments on commit 8f57073

Please sign in to comment.