From b1b375a64cc518eb58abd9ad023ea0127db56533 Mon Sep 17 00:00:00 2001 From: Death-Satan <49744633+Death-Satan@users.noreply.github.com> Date: Mon, 26 Feb 2024 16:28:45 +0800 Subject: [PATCH] Added FastBuilderWhere --- .../tests/Feature/AppstoreServiceTest.php | 2 +- src/mine-helpers/README.md | 45 +++++- src/mine-helpers/src/FastBuilderWhere.php | 147 ++++++++++++++++++ tests/Helpers/FastBuilderWhereTest.php | 76 +++++++++ 4 files changed, 268 insertions(+), 2 deletions(-) create mode 100644 src/mine-helpers/src/FastBuilderWhere.php create mode 100644 tests/Helpers/FastBuilderWhereTest.php diff --git a/src/app-store/tests/Feature/AppstoreServiceTest.php b/src/app-store/tests/Feature/AppstoreServiceTest.php index 084556be..ce5cc0af 100644 --- a/src/app-store/tests/Feature/AppstoreServiceTest.php +++ b/src/app-store/tests/Feature/AppstoreServiceTest.php @@ -61,7 +61,7 @@ public function setLocale(string $locale) ApplicationContext::getContainer()->set(ConfigInterface::class, new class() implements ConfigInterface { public function get(string $key, mixed $default = null): mixed { - return null; + return []; } public function has(string $keys): bool diff --git a/src/mine-helpers/README.md b/src/mine-helpers/README.md index c3f50078..5399f2b3 100644 --- a/src/mine-helpers/README.md +++ b/src/mine-helpers/README.md @@ -1 +1,44 @@ -# MineAdmin Helpers Library \ No newline at end of file +# MineAdmin Helpers Library + +# FastBuilderWhere + +```php +use Hyperf\Database\Model\Builder; + +use Mine\Helper\FastBuilderWhere as BaseBuilder; + +// old +class oldDao { + public function handleSearch(Builder $builder,array $params) { + if (!empty($params['username'])){ + $builder->where('username',$params['username']); + } + if (!empty($params['user_created_at'])){ + list($startDate,$endDate) = $params['user_created_at']; + $builder->whereBetween('created_at',[ + \Carbon\Carbon::createFromFormat('Y-m-d',$startDate)->startOfDay()->format('Y-m-d'), + \Carbon\Carbon::createFromFormat('Y-m-d',$endDate)->startOfDay()->format('Y-m-d') + ]); + } + if (!empty($params['sign_timestamp_at'])){ + list($startDate,$endDate) = $params['created_at']; + $builder->whereBetween('created_at',[ + \Carbon\Carbon::createFromFormat('Y-m-d',$startDate)->startOfDay()->timestamp, + \Carbon\Carbon::createFromFormat('Y-m-d',$endDate)->startOfDay()->timestamp + ]); + } + return $builder; + } +} + +// new dao +class newDao { + public function handleSearch(BaseBuilder $builder,array $params) { + $builder->eq('username') + ->dateRange('created_at','user_created_at') + ->timestampsRange('created_at','sign_timestamp_at'); + return $builder; + } +} + +``` \ No newline at end of file diff --git a/src/mine-helpers/src/FastBuilderWhere.php b/src/mine-helpers/src/FastBuilderWhere.php new file mode 100644 index 00000000..debff073 --- /dev/null +++ b/src/mine-helpers/src/FastBuilderWhere.php @@ -0,0 +1,147 @@ +buildOperator('=', $column, $key); + } + + public function lt(string $column, null|string $key= null): self + { + return $this->buildOperator('>', $column, $key); + } + + public function ne(string $column, null|string $key= null): self + { + return $this->buildOperator('<>', $column, $key); + } + + public function le(string $column, null|string $key= null): self + { + return $this->buildOperator('<=', $column, $key); + } + + public function ge(string $column, null|string $key= null): self + { + return $this->buildOperator('>=', $column, $key); + } + + public function gt(string $column, null|string $key= null): self + { + return $this->buildOperator('>', $column, $key); + } + + public function timestampsRange(string $column, array|string $keys): self + { + return $this->where( + $column, + function (Builder|ModelBuilder $builder) use ($keys, $column) { + [$start,$end] = $this->getRangeValues($keys); + return $builder->whereBetween( + $column, + [ + $start instanceof CarbonInterface ? $start->timestamp : $start, + $end instanceof CarbonInterface ? $end->timestamp : $end, + ] + ); + }, + '' + ); + } + + public function datetimeRange(string $column, array|string $keys): self + { + return $this->where( + $column, + function (Builder|ModelBuilder $builder) use ($keys, $column) { + [$start,$end] = $this->getRangeValues($keys); + return $builder->whereBetween( + $column, + [ + $start instanceof CarbonInterface ? $start : Carbon::createFromFormat(CarbonInterface::DEFAULT_TO_STRING_FORMAT,$start), + $end instanceof CarbonInterface ? $end : Carbon::createFromFormat(CarbonInterface::DEFAULT_TO_STRING_FORMAT,$end), + ] + ); + }, + '' + ); + } + + public function dateRange(string $column, array|string $keys): self + { + return $this->where( + $column, + function (Builder|ModelBuilder $builder) use ($keys, $column) { + [$start,$end] = $this->getRangeValues($keys); + return $builder->whereBetween( + $column, + [ + $start instanceof CarbonInterface ? $start->startOfDay() : Carbon::createFromFormat('Y-m-d',$start)->startOfDay(), + $end instanceof CarbonInterface ? $end->endOfDay() : Carbon::createFromFormat('Y-m-d',$end)->endOfDay(), + ] + ); + }, + '' + ); + } + + private function buildOperator(string $operator, string $column, null|string $key): self + { + return $this->where( + $column, + function (Builder|ModelBuilder $builder, mixed $value) use ($operator, $column) { + return $builder->where($column, $operator, $value); + }, + $key + ); + } + + private function where(string $column, \Closure $closure, null|string $key): self + { + $this->builder->when(Arr::get($this->params, $this->getKey($column, $key)), $closure); + return $this; + } + + private function getKey(string $column, null|string $key): string + { + return $key ?: $column; + } + + private function getRangeValues(string|array $keys): array|null + { + if (is_string($keys)){ + return Arr::get($this->params,$keys); + } + return [Arr::get($this->params,$keys[0]),Arr::get($this->params,$keys[1])]; + + } + + public function getBuilder(): Builder|ModelBuilder + { + return $this->builder; + } +} diff --git a/tests/Helpers/FastBuilderWhereTest.php b/tests/Helpers/FastBuilderWhereTest.php new file mode 100644 index 00000000..235f695c --- /dev/null +++ b/tests/Helpers/FastBuilderWhereTest.php @@ -0,0 +1,76 @@ +query = new \Hyperf\Database\Query\Builder( + Mockery::mock(ConnectionInterface::class), + Mockery::mock(Grammar::class), + Mockery::mock(Processor::class) + ); + $this->mock = new \Mine\Helper\FastBuilderWhere($this->query,[ + 'username' => '123456', + 'date' => [ + '2023-01-22', + '2023-02-22' + ], + 'datetime' => [ + '2023-01-22 00:00:00', + '2023-01-22 23:59:59', + ], + 'timestamps' => [ + \Carbon\Carbon::now()->startOfDay()->timestamp, + \Carbon\Carbon::now()->endOfDay()->timestamp, + ] + ]); +}); + +test('mock',function (){ + $query = $this->query; + $this->mock->ge('username'); + expect(in_array('123456',$query->getBindings(),true)) + ->toBeTrue() + ->and(count($query->getBindings())) + ->toEqual(1); + $this->mock->ge('username'); + expect(count($query->getBindings())) + ->toEqual(2); + $this->mock->ge('user'); + expect(count($query->getBindings())) + ->toEqual(2); + $this->mock->lt('username'); + expect(in_array('123456',$query->getBindings(),true)) + ->toBeTrue() + ->and(count($query->getBindings())) + ->toEqual(3); + $this->mock->ne('username'); + expect(in_array('123456',$query->getBindings(),true)) + ->toBeTrue() + ->and(count($query->getBindings())) + ->toEqual(4); + $this->mock->lt('username'); + expect(in_array('123456',$query->getBindings(),true)) + ->toBeTrue() + ->and(count($query->getBindings())) + ->toEqual(5); + $this->mock + ->lt('username') + ->eq('username') + ->gt('username') + ->le('username') + ->ge('username'); + expect(in_array('123456',$query->getBindings(),true)) + ->toBeTrue() + ->and(count($query->getBindings())) + ->toEqual(10); + $this->mock->timestampsRange('timestamps','timestamps') + ->dateRange('date','date') + ->datetimeRange('datetime','datetime'); + + expect(in_array('123456',$query->getBindings(),true)) + ->toBeTrue() + ->and(count($query->getBindings())) + ->toEqual(16); +}); \ No newline at end of file