diff --git a/src/Connection.php b/src/Connection.php index a7ade69..7b0bb3c 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -171,6 +171,11 @@ public function cursor($query, $bindings = [], $useReadPdo = false) yield $result; } } + + // clear scroll id + if($scrollId){ + $this->connection->clearScroll(['scroll_id'=>$scrollId]); + } } /** diff --git a/src/Database/Schema/Blueprint.php b/src/Database/Schema/Blueprint.php index d109513..bdbd7aa 100644 --- a/src/Database/Schema/Blueprint.php +++ b/src/Database/Schema/Blueprint.php @@ -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); } /** diff --git a/src/EloquentBuilder.php b/src/EloquentBuilder.php index 05915ab..554731e 100644 --- a/src/EloquentBuilder.php +++ b/src/EloquentBuilder.php @@ -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 @@ -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, + ] ]); } } diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 0009970..7a8b8ec 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -39,6 +39,8 @@ class QueryBuilder extends BaseBuilder public $includeInnerHits; + public $distinct; + protected $parentId; protected $results; @@ -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. * diff --git a/src/QueryGrammar.php b/src/QueryGrammar.php index d3895e3..205255b 100644 --- a/src/QueryGrammar.php +++ b/src/QueryGrammar.php @@ -9,6 +9,7 @@ use Illuminate\Support\Str; use InvalidArgumentException; use MongoDB\BSON\ObjectID; +use Illuminate\Support\Arr; class QueryGrammar extends BaseGrammar { @@ -66,6 +67,10 @@ public function compileSelect(Builder $builder): array unset($params['body']['query']); } + if($builder->distinct) { + $params['body']['collapse']['field'] = reset($builder->distinct); + } + // print "
";
         // print str_replace('    ', '  ', json_encode($params, JSON_PRETTY_PRINT));
         // exit;
@@ -73,6 +78,23 @@ public function compileSelect(Builder $builder): array
         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
      *
@@ -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;
     }