Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix[FixBug]: FixBug #127

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ public function cursor($query, $bindings = [], $useReadPdo = false)
yield $result;
}
}

// clear scroll id
if($scrollId){
$this->connection->clearScroll(['scroll_id'=>$scrollId]);
}
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,15 @@ public function enableFieldNames(): void
}

/**
* @param string $name
*
* @param $column
* @param int $total
* @param int $places
* @param bool $unsigned
* @return PropertyDefinition
*/
public function float($name, $total = 8, $places = 2): PropertyDefinition
public function float($column, $total = 8, $places = 2, $unsigned = false): PropertyDefinition
{
return $this->addColumn('float', $name);
return $this->addColumn('float', $column);
}

/**
Expand Down
26 changes: 20 additions & 6 deletions src/EloquentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Container\Container;
use Illuminate\Support\Arr;

/**
* Class EloquentBuilder
Expand Down Expand Up @@ -132,13 +134,25 @@ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page',

$perPage = $perPage ?: $this->model->getPerPage();

$results = $this->forPage($page, $perPage)->get($columns);

$total = $this->toBase()->getCountForPagination($columns);
if($this->query->distinct){
$aggParams = [
'field' => reset($this->query->distinct),
'precision_threshold'=>40000
];
$total = Arr::get((clone $this)->aggregation('query_distinct_total', 'cardinality', $aggParams)
->getQuery()->getAggregationResults(),'query_distinct_total.value');
$results = $this->forPage($page, $perPage)->get($columns);
}else{
$results = $this->forPage($page, $perPage)->get($columns);
$total = $this->toBase()->getCountForPagination($columns);
}

return new LengthAwarePaginator($results, $total, $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
return Container::getInstance()->makeWith(LengthAwarePaginator::class, [
'items' => $results, 'total' => $total, 'perPage' => $perPage, 'currentPage' => $page,
'options' => [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]
]);
}
}
20 changes: 20 additions & 0 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class QueryBuilder extends BaseBuilder

public $includeInnerHits;

public $distinct;

protected $parentId;

protected $results;
Expand Down Expand Up @@ -124,6 +126,24 @@ public function getOption(string $option)
return $this->options[$option] ?? null;
}

/**
* Force the query to only return distinct results.
*
* @return $this
*/
public function distinct()
{
$columns = func_get_args();

if (count($columns) > 0) {
$this->distinct = is_array($columns[0]) ? $columns[0] : $columns;
} else {
$this->distinct = [];
}

return $this;
}

/**
* Add a where between statement to the query.
*
Expand Down
27 changes: 25 additions & 2 deletions src/QueryGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Str;
use InvalidArgumentException;
use MongoDB\BSON\ObjectID;
use Illuminate\Support\Arr;

class QueryGrammar extends BaseGrammar
{
Expand Down Expand Up @@ -66,13 +67,34 @@ public function compileSelect(Builder $builder): array
unset($params['body']['query']);
}

if($builder->distinct) {
$params['body']['collapse']['field'] = reset($builder->distinct);
}

// print "<pre>";
// print str_replace(' ', ' ', json_encode($params, JSON_PRETTY_PRINT));
// exit;

return $params;
}

/**
* Compile a null clause
*
* @param Builder $builder
* @param array $where
* @return array
*/
protected function compileWhereRaw(Builder $builder, array $where): array
{
if($where['sql'] instanceof \Closure){
$query = $builder->newQuery();
$where['sql']($query);
return Arr::get($this->compileWheres($query),'query.bool');
}
return $where['sql'];
}

/**
* Compile where clauses for a query
*
Expand Down Expand Up @@ -1226,9 +1248,10 @@ public function compileUpdate(Builder $builder, $values)
if(Str::startsWith($column, $builder->from . '.')) {
$column = Str::replaceFirst($builder->from . '.', '', $column);
}
$script[] = 'ctx._source.' . $column . ' = "' . addslashes($value) . '";';
$clause['body']['script']['params'][$column] = $value;
$script[] = 'ctx._source.' . $column . ' = params.'.$column.';';
}
$clause['body']['script'] = implode('', $script);
$clause['body']['script']['source'] = implode('', $script);
return $clause;
}

Expand Down