Skip to content

Commit 189d2e3

Browse files
committed
Check constraint for empty strings by default, allow override with AllowEmptyString
1 parent a3df067 commit 189d2e3

7 files changed

Lines changed: 44 additions & 3 deletions

File tree

src/Attribute/AllowEmptyString.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace CoolBeans\Attribute;
6+
7+
#[\Attribute(\Attribute::TARGET_PROPERTY)]
8+
final class AllowEmptyString
9+
{
10+
}

src/Attribute/UniqueConstraint.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,4 @@
77
#[\Attribute(\Attribute::TARGET_PROPERTY)]
88
final class UniqueConstraint
99
{
10-
public function __construct()
11-
{
12-
}
1310
}

src/Command/SqlGeneratorCommand.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,16 @@ private static function getCheck(\ReflectionProperty $property, string $beanName
406406
$return[] = self::INDENTATION . 'CONSTRAINT `' . $constraintName . '` CHECK (' . $attribute->newInstance()->expression . ')';
407407
}
408408

409+
$type = $property->getType();
410+
411+
if ($type instanceof \ReflectionNamedType &&
412+
$type->isBuiltin() &&
413+
$type->getName() === 'string' &&
414+
\count($property->getAttributes(\CoolBeans\Attribute\AllowEmptyString::class)) === 0) {
415+
$constraintName = 'check_' . $beanName . '_' . $property->getName() . '_string_not_empty';
416+
$return[] = self::INDENTATION . 'CONSTRAINT `' . $constraintName . '` CHECK (`' . $property->name . '` != \'\')';
417+
}
418+
409419
return $return;
410420
}
411421

tests/Unit/Command/SqlGeneratorCommandTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public function testSimple() : void
2727
2828
CONSTRAINT `check_SimpleBean2_0` CHECK (IF(`col3` = 'abc', `col4` IS NOT NULL, TRUE)),
2929
CONSTRAINT `check_SimpleBean2_col3_0` CHECK (CHAR_LENGTH(`col3`) > 3),
30+
CONSTRAINT `check_SimpleBean2_col3_string_not_empty` CHECK (`col3` != ''),
31+
CONSTRAINT `check_SimpleBean2_col4_string_not_empty` CHECK (`col4` != ''),
32+
CONSTRAINT `check_SimpleBean2_col6_string_not_empty` CHECK (`col6` != ''),
3033
3134
PRIMARY KEY (`id`)
3235
)
@@ -44,6 +47,11 @@ public function testSimple() : void
4447
4548
CONSTRAINT `unique_SimpleBeanAttribute_col3` UNIQUE (`col3`),
4649
50+
CONSTRAINT `check_SimpleBeanAttribute_col3_string_not_empty` CHECK (`col3` != ''),
51+
CONSTRAINT `check_SimpleBeanAttribute_col4_string_not_empty` CHECK (`col4` != ''),
52+
CONSTRAINT `check_SimpleBeanAttribute_col5_string_not_empty` CHECK (`col5` != ''),
53+
CONSTRAINT `check_SimpleBeanAttribute_col6_string_not_empty` CHECK (`col6` != ''),
54+
4755
PRIMARY KEY (`id`)
4856
)
4957
CHARSET = `utf8mb4`
@@ -119,6 +127,8 @@ public function testSimple() : void
119127
CONSTRAINT `unique_AttributeBean_col5` UNIQUE (`col5`),
120128
CONSTRAINT `unique_AttributeBean_col6` UNIQUE (`col6`),
121129
130+
CONSTRAINT `check_AttributeBean_code_string_not_empty` CHECK (`code` != ''),
131+
122132
PRIMARY KEY (`code`)
123133
)
124134
CHARSET = `utf8mb4`
@@ -144,6 +154,11 @@ public function testSimple() : void
144154
145155
FOREIGN KEY (`simple_bean_2_id`) REFERENCES `simple_bean_2`(`id`) ON UPDATE RESTRICT ON DELETE RESTRICT,
146156
157+
CONSTRAINT `check_SimpleBean1_col3_string_not_empty` CHECK (`col3` != ''),
158+
CONSTRAINT `check_SimpleBean1_col4_string_not_empty` CHECK (`col4` != ''),
159+
CONSTRAINT `check_SimpleBean1_col5_string_not_empty` CHECK (`col5` != ''),
160+
CONSTRAINT `check_SimpleBean1_col6_string_not_empty` CHECK (`col6` != ''),
161+
147162
PRIMARY KEY (`id`)
148163
)
149164
CHARSET = `utf8mb4`

tests/Unit/TestBean/SimpleBean2.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ final class SimpleBean2 extends \CoolBeans\Bean
1515
#[\CoolBeans\Attribute\CheckConstraint('CHAR_LENGTH(`col3`) > 3')]
1616
public string $col3;
1717
public ?string $col4;
18+
#[\CoolBeans\Attribute\AllowEmptyString]
1819
public ?string $col5 = null;
1920
public ?string $col6 = 'default';
2021
public \DateTime $col8;

tests/Unit/TestBean/SimpleBeanClassAttribute.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ final class SimpleBeanClassAttribute extends \CoolBeans\Bean
1414
private int $col1;
1515
protected string $col2 = 'default';
1616
public \CoolBeans\PrimaryKey\IntPrimaryKey $id;
17+
#[\CoolBeans\Attribute\AllowEmptyString]
1718
public string $col3;
19+
#[\CoolBeans\Attribute\AllowEmptyString]
1820
public ?string $col4;
21+
#[\CoolBeans\Attribute\AllowEmptyString]
1922
public ?string $col5 = null;
23+
#[\CoolBeans\Attribute\AllowEmptyString]
2024
public ?string $col6 = 'default';
2125
public \DateTime $col8;
2226
public \Nette\Utils\DateTime $col9;

tests/Unit/TestBean/SimpleBeanClassAttribute2.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ final class SimpleBeanClassAttribute2 extends \CoolBeans\Bean
1313
private int $col1;
1414
protected string $col2 = 'default';
1515
public \CoolBeans\PrimaryKey\IntPrimaryKey $id;
16+
#[\CoolBeans\Attribute\AllowEmptyString]
1617
public string $col3;
18+
#[\CoolBeans\Attribute\AllowEmptyString]
1719
public ?string $col4;
20+
#[\CoolBeans\Attribute\AllowEmptyString]
1821
public ?string $col5 = null;
22+
#[\CoolBeans\Attribute\AllowEmptyString]
1923
public ?string $col6 = 'default';
2024
public \DateTime $col8;
2125
public \Nette\Utils\DateTime $col9;

0 commit comments

Comments
 (0)