Skip to content

Commit c70f5d2

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents f75de93 + 92001e4 commit c70f5d2

File tree

3 files changed

+63
-20
lines changed

3 files changed

+63
-20
lines changed

src/Bean.php

+15-6
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ abstract class Bean implements \CoolBeans\Contract\Row, \IteratorAggregate
1111
protected \ReflectionClass $reflection;
1212
protected \CoolBeans\Contract\PrimaryKey $primaryKey;
1313

14-
public function __construct(
14+
final public function __construct(
1515
protected \Nette\Database\Table\ActiveRow $row,
16+
private bool $extraColumns = false,
1617
)
1718
{
1819
$this->reflection = new \ReflectionClass(static::class);
1920
$this->primaryKey = \CoolBeans\Contract\PrimaryKey::create($this->row);
2021

21-
if (\CoolBeans\Config::$validateColumns) {
22-
$this->validateMissingColumns();
22+
if (\CoolBeans\Config::$validateColumns && !$this->extraColumns) {
23+
$this->validateMissingProperties();
2324
}
2425

2526
if (\CoolBeans\Config::$validateTableName) {
@@ -112,6 +113,14 @@ public function refresh() : void
112113
$this->initiateProperties();
113114
}
114115

116+
/**
117+
* Returns internal Nette row.
118+
*/
119+
public function getInternalRow() : \Nette\Database\Table\ActiveRow
120+
{
121+
return $this->row;
122+
}
123+
115124
/**
116125
* Selects referenced row from $table where <referencedRowPrimary> = $throughColumn
117126
*/
@@ -134,7 +143,7 @@ protected function related(string $table, ?string $throughColumn = null) : \Nett
134143
* Foo -> foo
135144
* FooBar -> foo_bar
136145
*/
137-
protected function validateTableName() : void
146+
private function validateTableName() : void
138147
{
139148
$tableName = $this->getTableName();
140149
$className = $this->reflection->getShortName();
@@ -152,7 +161,7 @@ protected function validateTableName() : void
152161
/**
153162
* Validates whether every column in database have its column property.
154163
*/
155-
protected function validateMissingColumns() : void
164+
private function validateMissingProperties() : void
156165
{
157166
foreach ($this->row->toArray() as $name => $value) {
158167
if (!$this->offsetExists($name)) {
@@ -164,7 +173,7 @@ protected function validateMissingColumns() : void
164173
/**
165174
* Initiates values into column properties.
166175
*/
167-
protected function initiateProperties() : void
176+
private function initiateProperties() : void
168177
{
169178
foreach ($this->reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
170179
$type = $property->getType();

src/Command/SqlGeneratorCommand.php

+32-8
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,19 @@ private static function getDataType(\ReflectionProperty $property) : string
300300
$enum = new \ReflectionEnum($type->getName());
301301

302302
if ((string) $enum->getBackingType() === 'string') {
303+
$cases = $enum->getCases();
304+
305+
// save large enums as varchar
306+
if (\count($cases) > 10) {
307+
$longestOption = 0;
308+
309+
foreach ($enum->getCases() as $case) {
310+
$longestOption = \max($longestOption, \strlen($case->getBackingValue()));
311+
}
312+
313+
return 'VARCHAR(' . $longestOption . ')';
314+
}
315+
303316
$options = [];
304317

305318
foreach ($enum->getCases() as $case) {
@@ -309,14 +322,11 @@ private static function getDataType(\ReflectionProperty $property) : string
309322
return 'ENUM(\'' . \implode('\',\'', $options) . '\')';
310323
}
311324

325+
// max number of digits in backing of integer
312326
$longestOption = 0;
313327

314328
foreach ($enum->getCases() as $case) {
315-
$length = \mb_strlen((string) $case->getBackingValue());
316-
317-
if ($length > $longestOption) {
318-
$longestOption = $length;
319-
}
329+
$longestOption = \max($longestOption, \strlen((string) $case->getBackingValue()));
320330
}
321331

322332
return 'TINYINT(' . $longestOption . ')';
@@ -352,7 +362,7 @@ private static function getClassIndex(\ReflectionClass $bean) : array
352362
{
353363
$return = [];
354364

355-
foreach ($bean->getAttributes(\CoolBeans\Attribute\ClassIndex::class) as $i => $attribute) {
365+
foreach (self::getClassAttributes($bean, \CoolBeans\Attribute\ClassIndex::class) as $i => $attribute) {
356366
$indexName = 'index_' . $bean->getShortName() . '_' . $i;
357367
$columns = $attribute->newInstance()->columns;
358368
self::validateColumnsExists($bean, $columns);
@@ -385,7 +395,7 @@ private static function getClassUnique(\ReflectionClass $bean) : array
385395
{
386396
$return = [];
387397

388-
foreach ($bean->getAttributes(\CoolBeans\Attribute\ClassUniqueConstraint::class) as $index => $attribute) {
398+
foreach (self::getClassAttributes($bean, \CoolBeans\Attribute\ClassUniqueConstraint::class) as $index => $attribute) {
389399
$constraintName = 'unique_' . $bean->getShortName() . '_' . $index;
390400
$columns = $attribute->newInstance()->columns;
391401
self::validateColumnsExists($bean, $columns);
@@ -413,6 +423,7 @@ private static function getCheck(\ReflectionProperty $property, string $beanName
413423
$type->getName() === 'string' &&
414424
\count($property->getAttributes(\CoolBeans\Attribute\AllowEmptyString::class)) === 0) {
415425
$constraintName = 'check_' . $beanName . '_' . $property->getName() . '_string_not_empty';
426+
416427
$return[] = self::INDENTATION . 'CONSTRAINT `' . $constraintName . '` CHECK (`' . $property->name . '` != \'\')';
417428
}
418429

@@ -423,7 +434,7 @@ private static function getClassCheck(\ReflectionClass $bean) : array
423434
{
424435
$return = [];
425436

426-
foreach ($bean->getAttributes(\CoolBeans\Attribute\ClassCheckConstraint::class) as $index => $attribute) {
437+
foreach (self::getClassAttributes($bean, \CoolBeans\Attribute\ClassCheckConstraint::class) as $index => $attribute) {
427438
$constraintName = 'check_' . $bean->getShortName() . '_' . $index;
428439

429440
$return[] = self::INDENTATION . 'CONSTRAINT `' . $constraintName . '` CHECK (' . $attribute->newInstance()->expression . ')';
@@ -554,4 +565,17 @@ private function getBeans(string $destination) : array
554565

555566
return $beans;
556567
}
568+
569+
private static function getClassAttributes(\ReflectionClass $bean, string $attributeClass) : array
570+
{
571+
$return = $bean->getAttributes($attributeClass);
572+
$parent = $bean->getParentClass();
573+
574+
while ($parent instanceof \ReflectionClass) {
575+
$return = \array_merge($return, $parent->getAttributes($attributeClass));
576+
$parent = $parent->getParentClass();
577+
}
578+
579+
return $return;
580+
}
557581
}

src/Selection.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ abstract class Selection implements \CoolBeans\Contract\Selection
1212

1313
final public function __construct(
1414
protected \Nette\Database\Table\Selection $selection,
15+
private bool $extraColumns = false,
1516
)
1617
{
1718
if (\CoolBeans\Config::$validateTableName) {
@@ -33,6 +34,7 @@ public function getTableName() : string
3334
public function select(string $select) : static
3435
{
3536
$this->selection->select($select);
37+
$this->extraColumns = true;
3638

3739
return $this;
3840
}
@@ -80,9 +82,9 @@ public function group(string $group) : static
8082
/**
8183
* Function to pass order query.
8284
*/
83-
public function order(string $order) : static
85+
public function order(string $order, string|int|float|array|\BackedEnum|\DateTime ...$params) : static
8486
{
85-
$this->selection->order($order);
87+
$this->selection->order($order, ...$params);
8688

8789
return $this;
8890
}
@@ -112,7 +114,7 @@ public function alias(string $tableChain, string $alias) : static
112114
*/
113115
public function fetch() : ?\CoolBeans\Bean
114116
{
115-
return static::createRow($this->selection->fetch());
117+
return $this->createRow($this->selection->fetch());
116118
}
117119

118120
/**
@@ -185,7 +187,7 @@ public function current() : ?\CoolBeans\Bean
185187
$current = $this->selection->current();
186188

187189
return $current instanceof \Nette\Database\Table\ActiveRow ?
188-
static::createRow($current) :
190+
$this->createRow($current) :
189191
null;
190192
}
191193

@@ -197,15 +199,23 @@ public function next() : void
197199
$this->selection->next();
198200
}
199201

202+
/**
203+
* Returns internal Nette selection.
204+
*/
205+
final public function getInternalSelection() : \Nette\Database\Table\Selection
206+
{
207+
return $this->selection;
208+
}
209+
200210
/**
201211
* Function to return specific Row class.
202212
*/
203-
final protected static function createRow(?\Nette\Database\Table\ActiveRow $row) : ?\CoolBeans\Bean
213+
final protected function createRow(?\Nette\Database\Table\ActiveRow $row) : ?\CoolBeans\Bean
204214
{
205215
$rowClassName = static::ROW_CLASS;
206216

207217
return $row instanceof \Nette\Database\Table\ActiveRow
208-
? new $rowClassName($row)
218+
? new $rowClassName($row, $this->extraColumns)
209219
: null;
210220
}
211221

0 commit comments

Comments
 (0)