Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 6ae03fb

Browse files
committed
Fix-numeric (mariaDb)
1 parent 304cf15 commit 6ae03fb

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,37 @@ It work on MariaDb.
309309
- three
310310
```
311311

312+
### Handling of `numeric` (#numeric, #MariaDb)
313+
precision-default = 10
314+
scale-default = 2
315+
316+
- You can define attribute like "numeric(precision,scale)":
317+
```yaml
318+
test_table:
319+
properties:
320+
my_property:
321+
x-db-type: decimal(12,4)
322+
```
323+
DB-Result = decimal(12,4)
324+
325+
- You can define attribute like "numeric(precision)" with default scale-default = 2:
326+
```yaml
327+
test_table:
328+
properties:
329+
my_property:
330+
x-db-type: decimal(12)
331+
```
332+
DB-Result = decimal(12,2)
333+
334+
- You can define attribute like "numeric" with precision-default = 10 and scale-default = 2:
335+
```yaml
336+
test_table:
337+
properties:
338+
my_property:
339+
x-db-type: decimal
340+
```
341+
DB-Result = decimal(10,2)
342+
312343
## Screenshots
313344

314345
Gii Generator Form:

src/lib/ColumnToCode.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,56 @@ public function isEnum():bool
150150
return !empty($this->column->enumValues);
151151
}
152152

153+
public function isDecimal()
154+
{
155+
return self::isDecimalByDbType($this->column->dbType);
156+
}
157+
158+
/**
159+
* @param $dbType
160+
* @return array|false
161+
*/
162+
public static function isDecimalByDbType($dbType)
163+
{
164+
$precision = null;
165+
$scale = null;
166+
167+
// https://runebook.dev/de/docs/mariadb/decimal/index
168+
$precisionDefault = 10;
169+
$scaleDefault = 2;
170+
171+
preg_match_all('/(decimal\()+(\d)+(,)+(\d)+(\))/', $dbType, $matches);
172+
if (!empty($matches[4][0])) {
173+
$precision = $matches[2][0];
174+
$scale = $matches[4][0];
175+
}
176+
177+
if (empty($precision)){
178+
preg_match_all('/(decimal\()+(\d)+(\))/', $dbType, $matches);
179+
if (!empty($matches[2][0])) {
180+
$precision = $matches[2][0];
181+
$scale = $scaleDefault;
182+
}
183+
}
184+
185+
if (empty($precision)){
186+
if (strtolower($dbType) === 'decimal'){
187+
$precision = $precisionDefault;
188+
$scale = $scaleDefault;
189+
}
190+
}
191+
192+
if (empty($precision)){
193+
return false;
194+
}
195+
196+
return [
197+
'precision' => (int)$precision,
198+
'scale' => (int)$scale,
199+
'dbType' => "decimal($precision,$scale)",
200+
];
201+
}
202+
153203
public static function escapeQuotes(string $str):string
154204
{
155205
return str_replace(["'", '"', '$'], ["\\'", "\\'", '\$'], $str);
@@ -208,7 +258,6 @@ private function resolve():void
208258
if ($dbType === 'varchar') {
209259
$type = $dbType = 'string';
210260
}
211-
$this->isBuiltinType = $this->getIsBuiltinType($type, $dbType);
212261
$fluentSize = $this->column->size ? '(' . $this->column->size . ')' : '()';
213262
$rawSize = $this->column->size ? '(' . $this->column->size . ')' : '';
214263
$this->rawParts['nullable'] = $this->column->allowNull ? 'NULL' : 'NOT NULL';
@@ -223,11 +272,17 @@ private function resolve():void
223272
$this->column->dbType . (strpos($this->column->dbType, '(') !== false ? '' : $rawSize);
224273
} elseif ($this->isEnum()) {
225274
$this->resolveEnumType();
275+
} elseif ($this->isDecimal()) {
276+
$this->fluentParts['type'] = $dbType;
277+
$this->rawParts['type'] = $dbType;
226278
} else {
227279
$this->fluentParts['type'] = $type . $fluentSize;
228280
$this->rawParts['type'] =
229281
$this->column->dbType . (strpos($this->column->dbType, '(') !== false ? '' : $rawSize);
230282
}
283+
284+
$this->isBuiltinType = $this->getIsBuiltinType($type, $dbType);
285+
231286
$this->resolveDefaultValue();
232287
}
233288

src/lib/migrations/MysqlMigrationBuilder.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace cebe\yii2openapi\lib\migrations;
99

10+
use cebe\yii2openapi\lib\ColumnToCode;
1011
use cebe\yii2openapi\lib\items\DbIndex;
1112
use yii\base\NotSupportedException;
1213
use yii\db\ColumnSchema;
@@ -56,6 +57,19 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired):
5657
if ($current->type === $desired->type && !$desired->size && $this->isDbDefaultSize($current)) {
5758
$desired->size = $current->size;
5859
}
60+
61+
if ($decimalAttributes = ColumnToCode::isDecimalByDbType($desired->dbType)){
62+
$desired->precision = $decimalAttributes['precision'];
63+
$desired->scale = $decimalAttributes['scale'];
64+
$desired->type = 'decimal';
65+
$desired->size = $decimalAttributes['precision'];
66+
foreach (['precision', 'scale', 'dbType'] as $decimalAttr) {
67+
if ($current->$decimalAttr !== $desired->$decimalAttr) {
68+
$changedAttributes[] = $decimalAttr;
69+
}
70+
}
71+
}
72+
5973
foreach (['type', 'size', 'allowNull', 'defaultValue', 'enumValues'] as $attr) {
6074
if ($current->$attr !== $desired->$attr) {
6175
$changedAttributes[] = $attr;

0 commit comments

Comments
 (0)