Skip to content

Commit

Permalink
improve queries
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev committed May 4, 2024
1 parent 42f8d0d commit 23a273c
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions src/DataSource/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Database\Eloquent\{Builder as EloquentBuilder, RelationNotFoundException};
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\{Cache, DB, Schema};
use Illuminate\Support\Facades\{Cache, Schema};
use PowerComponents\LivewirePowerGrid\Components\Filters\{Builders\Number};
use PowerComponents\LivewirePowerGrid\{Column,
Components\Filters\Builders\Boolean,
Expand Down Expand Up @@ -147,16 +147,16 @@ public function filterContains(): Builder

$search = $this->getBeforeSearchMethod($field, $search);

$hasColumn = in_array($field, $columnList, true);
$hasColumn = isset($columnList[$field]);

$query->when($search, function () use ($column, $query, $search, $table, $field, $hasColumn) {
$query->when($search != '', function () use ($column, $query, $search, $table, $field, $hasColumn) {
if (($sqlRaw = strval(data_get($column, 'searchableRaw')))) {
$query->orWhereRaw($sqlRaw . ' ' . Sql::like($query) . ' \'%' . $search . '%\'');
}

if ($hasColumn && blank(data_get($column, 'searchableRaw')) && $search) {
if ($hasColumn && blank(data_get($column, 'searchableRaw'))) {
try {
$columnType = DB::getSchemaBuilder()->getColumnType($table, $field);
$columnType = $this->getColumnType($table, $field);

/** @phpstan-ignore-next-line */
$driverName = $query->getConnection()->getConfig('driver');
Expand Down Expand Up @@ -299,11 +299,37 @@ private function splitField(string $field): array
return [$table, $field];
}

private function getColumnType(string $modelTable, string $field = null): ?string
{
try {
return $this->getColumnList($modelTable)[$field];
} catch (\Throwable $throwable) {
logger()->warning('PowerGrid [getColumnType] warning: ', [
'table' => $modelTable,
'field' => $field,
'throwable' => $throwable->getTrace(),
]);

return null;
}
}

private function getColumnList(string $modelTable): array
{
try {
return (array) Cache::remember('powergrid_columns_in_' . $modelTable, 600, fn () => Schema::getColumnListing($modelTable));
} catch (\Exception) {
return (array) Cache::remember(
'powergrid_columns_in_' . $modelTable,
60 * 60 * 3,
fn () => collect(Schema::getColumns($modelTable))
->pluck('type', 'name')
->toArray()
);
} catch (\Throwable $throwable) {
logger()->warning('PowerGrid [getColumnList] warning: ', [
'table' => $modelTable,
'throwable' => $throwable->getTrace(),
]);

return Schema::getColumnListing($modelTable);
}
}
Expand Down

0 comments on commit 23a273c

Please sign in to comment.