Skip to content

Commit 9246862

Browse files
authored
Merge pull request #26 from farshadth/refactor
add getOne and getAll for indexed columns
2 parents ca106b1 + ae528de commit 9246862

14 files changed

+64
-35
lines changed

src/Commands/MakeInterfaceRepository.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ public function handle(): void
7272

7373
$baseContent = substr_replace($baseContent, $this->writeGetOneFunction($getOneStub, 'id', 'int'), -2, 0);
7474
$baseContent = substr_replace($baseContent, $this->writeGetAllFunction($getAllStub, 'id', 'int'), -2, 0);
75+
$columnsInfo = $this->getAllColumnsInTable($this->tableName);
76+
77+
$indexes = $this->extractIndexes($this->tableName);
78+
foreach ($indexes as $index) {
79+
$columnInfo = collect($columnsInfo)->where('COLUMN_NAME', $index->COLUMN_NAME)->first();
80+
$baseContent = substr_replace($baseContent, $this->writeGetOneFunction($getOneStub, $index->COLUMN_NAME, $this->getDataType($columnInfo->COLUMN_TYPE, $columnInfo->DATA_TYPE)), -2, 0);
81+
82+
if($index->Non_unique == 1) {
83+
$baseContent = substr_replace($baseContent, $this->writeGetOneFunction($getAllStub, $index->COLUMN_NAME, $this->getDataType($columnInfo->COLUMN_TYPE, $columnInfo->DATA_TYPE)), -2, 0);
84+
}
85+
}
7586

7687
if ($this->detectForeignKeys) {
7788
foreach ($foreignKeys as $_foreignKey) {

src/Creators/CreatorMySqlRepository.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,18 @@ public function createFunctions(): array
7373
$functions['__construct'] = $this->getConstruct($this->tableName, $this->factoryName, $hasSoftDelete, $constructContent);
7474
$functions['getOneById'] = $this->writeGetOneFunction($getOneStub, 'id', 'int');
7575
$functions['getAllByIds'] = $this->writeGetAllFunction($getAllStub, 'id', 'int');
76+
$columnsInfo = $this->getAllColumnsInTable($this->tableName);
7677

7778
$indexes = $this->extractIndexes($this->tableName);
7879
foreach ($indexes as $index) {
80+
$columnInfo = collect($columnsInfo)->where('COLUMN_NAME', $index->COLUMN_NAME)->first();
7981
$indx = 'getOneBy' . ucfirst(Str::camel($index->COLUMN_NAME));
80-
$functions[$indx] = $this->writeGetOneFunction($getOneStub, $index->COLUMN_NAME, $this->entityName);
81-
$indx = 'getAllBy' . ucfirst(Str::plural(Str::camel($index->COLUMN_NAME)));
82-
$functions[$indx] = $this->writeGetAllFunction($getAllStub, $index->COLUMN_NAME, $this->entityName);
82+
$functions[$indx] = $this->writeGetOneFunction($getOneStub, $index->COLUMN_NAME, $this->getDataType($columnInfo->COLUMN_TYPE, $columnInfo->DATA_TYPE));
83+
84+
if($index->Non_unique == 1) {
85+
$indx = 'getAllBy' . ucfirst(Str::plural(Str::camel($index->COLUMN_NAME)));
86+
$functions[$indx] = $this->writeGetAllFunction($getAllStub, $index->COLUMN_NAME, $this->entityName);
87+
}
8388
}
8489

8590
if ($this->detectForeignKeys) {

src/Creators/CreatorRepository.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,30 @@ public function __construct(
3737

3838
private function writeFunction(string $functionStub, string $functionName, string $columnName, string $attributeType): string
3939
{
40+
$columnNameSingle = Str::camel($columnName);
41+
4042
if ($functionName === 'getOneBy') {
4143
$functionReturnType = 'null|{{ EntityName }}';
4244
$functionName .= ucfirst(Str::camel($columnName));
4345
$columnName = Str::camel($columnName);
4446
$redisCashFunction = $this->getRedisCashFunctionGetOneBy($this->strategyName);
45-
46-
4747
} elseif ($functionName === 'getAllBy') {
4848
$functionReturnType = 'Collection';
4949
$functionName .= ucfirst(Str::plural(Str::camel($columnName)));
5050
$columnName = Str::plural(Str::camel($columnName));
5151
$redisCashFunction = $this->getRedisCashFunctionGetAllBy($this->strategyName);
52-
53-
5452
} elseif ($functionName === 'create') {
5553
$functionReturnType = $attributeType;
5654
$redisCashFunction = $this->getRedisCashFunctionCreate($this->strategyName);
57-
5855
} elseif (in_array($functionName, ['update', 'remove', 'restore'])) {
5956
$functionReturnType = 'int';
6057
$redisCashFunction = $this->getRedisCashFunctionUpdate($this->strategyName);
61-
6258
}
59+
60+
$redisCashFunction = str_replace(['{{ FunctionName }}', '{{ ColumnName }}', '{{ ColumnNameSingle }}'], [$functionName, $columnName, $columnNameSingle], $redisCashFunction);
61+
6362
return str_replace(['{{ FunctionName }}', '{{ AttributeType }}', '{{ AttributeName }}', '{{ FunctionReturnType }}','{{redisFunction}}'],
64-
[$functionName, $attributeType, Str::camel($columnName), $functionReturnType,$redisCashFunction],
63+
[$functionName, $attributeType, Str::camel($columnName), $functionReturnType, $redisCashFunction],
6564
$functionStub);
6665
}
6766
private function writeSqlAttribute(string $attributeStub, string $sqlRepositoryVariable, string $sqlRepositoryName): string
@@ -119,12 +118,18 @@ public function createFunctions(): array
119118
$functions['__construct'] = $this->getConstructRedis($setterSqlStub, $constructStub);
120119
$functions['getOneById'] = $this->writeFunction($functionStub, 'getOneBy', 'id', 'int');
121120
$functions['getAllByIds'] = $this->writeFunction($functionStub, 'getAllBy', 'id', 'array');
121+
$columnsInfo = $this->getAllColumnsInTable($this->tableName);
122+
122123
$indexes = $this->extractIndexes($this->tableName);
123124
foreach ($indexes as $index) {
124-
$fun_name = ucfirst(Str::plural(Str::camel($index->COLUMN_NAME)));
125-
$functions['getAllBy' . $fun_name] = $this->writeFunction($functionStub, 'getAllBy', $index->COLUMN_NAME, 'array');
125+
$columnInfo = collect($columnsInfo)->where('COLUMN_NAME', $index->COLUMN_NAME)->first();
126126
$fun_name = ucfirst(Str::camel($index->COLUMN_NAME));
127-
$functions['getOneBy' . $fun_name] = $this->writeFunction($functionStub, 'getOneBy', $index->COLUMN_NAME, 'int');
127+
$functions['getOneBy' . $fun_name] = $this->writeFunction($functionStub, 'getOneBy', $index->COLUMN_NAME, $this->getDataType($columnInfo->COLUMN_TYPE, $columnInfo->DATA_TYPE));
128+
129+
if($index->Non_unique == 1) {
130+
$fun_name = ucfirst(Str::plural(Str::camel($index->COLUMN_NAME)));
131+
$functions['getAllBy' . $fun_name] = $this->writeFunction($functionStub, 'getAllBy', $index->COLUMN_NAME, 'array');
132+
}
128133
}
129134
if ($this->detectForeignKeys) {
130135
$foreignKeys = $this->extractForeignKeys($this->tableName);

src/CustomMySqlQueries.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ public function extractIndexes(string $tableName): Collection
102102
->orderBy('ORDINAL_POSITION')
103103
->get();
104104

105+
$indexesData = DB::select("SHOW INDEX FROM $tableName WHERE Key_name != 'PRIMARY'");
106+
107+
collect($indexes)->each(function ($index) use ($indexesData) {
108+
$indexesData = collect($indexesData)->where('Column_name', $index->COLUMN_NAME)->first();
109+
$index->Non_unique = $indexesData->Non_unique;
110+
$index->Index_type = $indexesData->Index_type;
111+
});
112+
105113
return $indexes;
106114
}
107115

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11

2-
public function getOneBy{{ FunctionName }}(int ${{ AttributeName }}): null|{{ EntityName }};
2+
public function getOneBy{{ FunctionName }}({{ AttributeType }} ${{ AttributeName }}): null|{{ EntityName }};

stubs/Repositories/Mysql/mysql.getOneBy.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public function getOneBy{{ FunctionName }}(int ${{ AttributeName }}): null|{{ EntityName }}
1+
public function getOneBy{{ FunctionName }}({{ AttributeType }} ${{ AttributeName }}): null|{{ EntityName }}
22
{
33
${{ EntityVariableName }} = $this->newQuery()
44
->where('{{ ColumnName }}', ${{ AttributeName }})
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
$cacheKey = $this->redisRepository->makeKey([
2-
'function_name' => 'getAllByIds',
3-
'id' => $ids,
2+
'function_name' => '{{ FunctionName }}',
3+
'{{ ColumnNameSingle }}' => ${{ ColumnName }},
44
]);
55

66
$data = $this->redisRepository->get($cacheKey);
77

88
if (is_null($data)) {
9-
$data = $this->repository->getAllByIds($ids);
9+
$data = $this->repository->{{ FunctionName }}(${{ ColumnName }});
1010
$this->redisRepository->put($cacheKey, $data, Time::HALF_HOUR_BY_SECOND);
1111
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
$cacheKey = $this->redisRepository->makeKey([
2-
'function_name' => 'getAllByIds',
3-
'id' => $ids,
2+
'function_name' => '{{ FunctionName }}',
3+
'{{ ColumnNameSingle }}' => ${{ ColumnName }},
44
]);
55

66
$entity = $this->redisRepository->get($cacheKey);
77

88
if (is_null($entity)) {
9-
$entity = $this->repository->getAllByIds($ids);
9+
$entity = $this->repository->{{ FunctionName }}(${{ ColumnName }});
1010
$this->redisRepository->put($cacheKey, $entity);
1111
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
$entity = $this->redisRepository->get();
22

33
if (is_null($entity)) {
4-
$entity = $this->repository->getAllByIds($id);
4+
$entity = $this->repository->{{ FunctionName }}(${{ ColumnName }});
55
$this->redisRepository->put($entity);
66
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
$cacheKey = $this->redisRepository->makeKey([
2-
'function_name' => 'getAllByIds',
3-
'id' => $ids,
2+
'function_name' => '{{ FunctionName }}',
3+
'{{ ColumnNameSingle }}' => ${{ ColumnName }},
44
]);
55

66
$data = $this->redisRepository->get($cacheKey);
77

88
if (is_null($data)) {
9-
$data = $this->repository->getAllByIds($ids);
9+
$data = $this->repository->{{ FunctionName }}(${{ ColumnName }});
1010
$this->redisRepository->put($cacheKey, $data, Time::HALF_HOUR_BY_SECOND);
1111
}

0 commit comments

Comments
 (0)