Merge pull request #348 from SanderKleykens/feature/postgres-compatibility

PostgreSQL compatibility
This commit is contained in:
James Cole 2016-10-07 05:32:10 +02:00 committed by GitHub
commit f5cb87f5c3
5 changed files with 82 additions and 16 deletions

View File

@ -257,9 +257,19 @@ class ReportHelper implements ReportHelperInterface
->where('transaction_journals.date', '>=', $start->format('Y-m-d')) ->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d')) ->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
->where( ->where(
function (Builder $q) use ($ids) { // source.account_id in accountIds XOR destination.account_id in accountIds
$q->whereIn('source.account_id', $ids) function (Builder $sourceXorDestinationQuery) use ($ids) {
->whereIn('destination.account_id', $ids, 'xor'); $sourceXorDestinationQuery->where(
function (Builder $inSourceButNotDestinationQuery) use ($ids) {
$inSourceButNotDestinationQuery->whereIn('source.account_id', $ids)
->whereNotIn('destination.account_id', $ids);
}
)->orWhere(
function (Builder $inDestinationButNotSourceQuery) use ($ids) {
$inDestinationButNotSourceQuery->whereIn('destination.account_id', $ids)
->whereNotIn('source.account_id', $ids);
}
);
} }
) )
->get(['tags.id', 'tags.tag', 'transaction_journals.id as journal_id', 'destination.amount']); ->get(['tags.id', 'tags.tag', 'transaction_journals.id as journal_id', 'destination.amount']);

View File

@ -185,7 +185,7 @@ class ImportStorage
->where('rules.active', 1) ->where('rules.active', 1)
->orderBy('rule_groups.order', 'ASC') ->orderBy('rule_groups.order', 'ASC')
->orderBy('rules.order', 'ASC') ->orderBy('rules.order', 'ASC')
->get(['rules.*']); ->get(['rules.*', 'rule_groups.order']);
Log::debug(sprintf('Found %d user rules.', $set->count())); Log::debug(sprintf('Found %d user rules.', $set->count()));
return $set; return $set;

View File

@ -23,6 +23,7 @@ use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Steam; use Steam;
@ -413,10 +414,24 @@ class AccountRepository implements AccountRepositoryInterface
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0); $join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0);
} }
); );
$set = join(', ', $accountIds); $query->where(
$query->whereRaw('(source.account_id in (' . $set . ') XOR destination.account_id in (' . $set . '))'); // source.account_id in accountIds XOR destination.account_id in accountIds
function (Builder $sourceXorDestinationQuery) use ($accountIds) {
$sourceXorDestinationQuery->where(
function (Builder $inSourceButNotDestinationQuery) use ($accountIds) {
$inSourceButNotDestinationQuery->whereIn('source.account_id', $accountIds)
->whereNotIn('destination.account_id', $accountIds);
} }
)->orWhere(
function (Builder $inDestinationButNotSourceQuery) use ($accountIds) {
$inDestinationButNotSourceQuery->whereIn('destination.account_id', $accountIds)
->whereNotIn('source.account_id', $accountIds);
}
);
}
);
}
// that should do it: // that should do it:
$fields = TransactionJournal::queryFields(); $fields = TransactionJournal::queryFields();
$complete = $query->get($fields); $complete = $query->get($fields);

View File

@ -23,6 +23,7 @@ use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@ -368,9 +369,22 @@ class BudgetRepository implements BudgetRepositoryInterface
} }
if ($accounts->count() > 0) { if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray(); $accountIds = $accounts->pluck('id')->toArray();
$set = join(', ', $accountIds); $query->where(
$query->whereRaw('(source.account_id in (' . $set . ') XOR destination.account_id in (' . $set . '))'); // source.account_id in accountIds XOR destination.account_id in accountIds
function (Builder $sourceXorDestinationQuery) use ($accountIds) {
$sourceXorDestinationQuery->where(
function (Builder $inSourceButNotDestinationQuery) use ($accountIds) {
$inSourceButNotDestinationQuery->whereIn('source.account_id', $accountIds)
->whereNotIn('destination.account_id', $accountIds);
}
)->orWhere(
function (Builder $inDestinationButNotSourceQuery) use ($accountIds) {
$inDestinationButNotSourceQuery->whereIn('destination.account_id', $accountIds)
->whereNotIn('source.account_id', $accountIds);
}
);
}
);
} }
if ($budgets->count() > 0) { if ($budgets->count() > 0) {
$budgetIds = $budgets->pluck('id')->toArray(); $budgetIds = $budgets->pluck('id')->toArray();
@ -445,9 +459,22 @@ class BudgetRepository implements BudgetRepositoryInterface
if ($accounts->count() > 0) { if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray(); $accountIds = $accounts->pluck('id')->toArray();
$query->where(
$set = join(', ', $accountIds); // source.account_id in accountIds XOR destination.account_id in accountIds
$query->whereRaw('(source.account_id in (' . $set . ') XOR destination.account_id in (' . $set . '))'); function (Builder $sourceXorDestinationQuery) use ($accountIds) {
$sourceXorDestinationQuery->where(
function (Builder $inSourceButNotDestinationQuery) use ($accountIds) {
$inSourceButNotDestinationQuery->whereIn('source.account_id', $accountIds)
->whereNotIn('destination.account_id', $accountIds);
}
)->orWhere(
function (Builder $inDestinationButNotSourceQuery) use ($accountIds) {
$inDestinationButNotSourceQuery->whereIn('destination.account_id', $accountIds)
->whereNotIn('source.account_id', $accountIds);
}
);
}
);
} }
$ids = $query->get(['transaction_journals.id'])->pluck('id')->toArray(); $ids = $query->get(['transaction_journals.id'])->pluck('id')->toArray();
$sum = '0'; $sum = '0';

View File

@ -18,6 +18,7 @@ use FireflyIII\Models\Category;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@ -487,9 +488,22 @@ class CategoryRepository implements CategoryRepositoryInterface
} }
if ($accounts->count() > 0) { if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray(); $accountIds = $accounts->pluck('id')->toArray();
$set = join(', ', $accountIds); $query->where(
$query->whereRaw('(source.account_id in (' . $set . ') XOR destination.account_id in (' . $set . '))'); // source.account_id in accountIds XOR destination.account_id in accountIds
function (Builder $sourceXorDestinationQuery) use ($accountIds) {
$sourceXorDestinationQuery->where(
function (Builder $inSourceButNotDestinationQuery) use ($accountIds) {
$inSourceButNotDestinationQuery->whereIn('source.account_id', $accountIds)
->whereNotIn('destination.account_id', $accountIds);
}
)->orWhere(
function (Builder $inDestinationButNotSourceQuery) use ($accountIds) {
$inDestinationButNotSourceQuery->whereIn('destination.account_id', $accountIds)
->whereNotIn('source.account_id', $accountIds);
}
);
}
);
} }
if ($categories->count() > 0) { if ($categories->count() > 0) {
$categoryIds = $categories->pluck('id')->toArray(); $categoryIds = $categories->pluck('id')->toArray();