Skip to content

Commit

Permalink
fix some
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 2, 2022
1 parent 23ea168 commit 23d618d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
10 changes: 7 additions & 3 deletions src/Model2Inner.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ protected function init(): void
$om = $this->getOuterModel();

foreach ($om->getFields() as $name => $oField) {
if ($oField->getReference() !== null) {
// if ($oField->getReference() !== null) {
// continue;
// }

// TODO calculated fields, needs to copy (and maybe update some) more properties
if ($oField instanceof Field\SqlExpressionField) {
continue;
}

$fieldDefaults = [get_class($oField)];
foreach ([
'type',
'actual',
'system',
'default',
'never_persist',
Expand All @@ -41,7 +45,7 @@ protected function init(): void
$fieldDefaults[$prop] = $oField->{$prop};
}

$this->addField($name, $fieldDefaults);
$this->addField($oField->actual ?? $name, $fieldDefaults);
}
}
}
2 changes: 1 addition & 1 deletion src/Persistence/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ public function getFieldSqlExpression(Field $field, Expression $expression): Exp
$prop = [
$field->hasJoin()
? ($field->getJoin()->foreign_alias ?: $field->getJoin()->short_name)
: ($field->getOwner()->table_alias ?: $field->getOwner()->table),
: ($field->getOwner()->table_alias ?: (is_object($field->getOwner()->table) ? '__inner__' : $field->getOwner()->table)),
$field->getPersistenceName(),
];
} else {
Expand Down
10 changes: 5 additions & 5 deletions tests/ExpressionSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testBasic(): void

if ($this->getDatabasePlatform() instanceof SqlitePlatform) {
$this->assertSame(
'select "id", "total_net", "total_vat", ("total_net"+"total_vat") "total_gross" from "invoice"',
'select "id", "total_net", "total_vat", ("total_net"+"total_vat") "total_gross" from (select "id", "total_net", "total_vat" from "invoice") "__inner__"',
$i->action('select')->render()[0]
);
}
Expand All @@ -51,7 +51,7 @@ public function testBasic(): void

if ($this->getDatabasePlatform() instanceof SqlitePlatform) {
$this->assertEquals(
'select "id", "total_net", "total_vat", ("total_net"+"total_vat") "total_gross", (("total_net"+"total_vat")*2) "double_total_gross" from "invoice"',
'select "id", "total_net", "total_vat", ("total_net"+"total_vat") "total_gross", (("total_net"+"total_vat")*2) "double_total_gross" from (select "id", "total_net", "total_vat" from "invoice") "__inner__"',
$i->action('select')->render()[0]
);
}
Expand All @@ -76,7 +76,7 @@ public function testBasicCallback(): void

if ($this->getDatabasePlatform() instanceof SqlitePlatform) {
$this->assertSame(
'select "id", "total_net", "total_vat", ("total_net"+"total_vat") "total_gross" from "invoice"',
'select "id", "total_net", "total_vat", ("total_net"+"total_vat") "total_gross" from (select "id", "total_net", "total_vat" from "invoice") "__inner__"',
$i->action('select')->render()[0]
);
}
Expand Down Expand Up @@ -104,7 +104,7 @@ public function testQuery(): void

if ($this->getDatabasePlatform() instanceof SqlitePlatform) {
$this->assertSame(
'select "id", "total_net", "total_vat", (select sum("total_net") from "invoice") "sum_net" from "invoice"',
'select "id", "total_net", "total_vat", (select sum("total_net") from (select "id", "total_net", "total_vat" from "invoice") "__inner__") "sum_net" from (select "id", "total_net", "total_vat" from "invoice") "__inner__"',
$i->action('select')->render()[0]
);
}
Expand Down Expand Up @@ -151,7 +151,7 @@ public function testExpressions(): void
}

$this->assertSameSql(
'select "id", "name", "surname", "cached_name", (' . $concatSql . ') "full_name" from "user" where ((' . $concatSql . ') != "cached_name")',
'select "id", "name", "surname", "cached_name", (' . $concatSql . ') "full_name" from (select "id", "name", "surname", "cached_name" from "user") "__inner__" where ((' . $concatSql . ') != "cached_name")',
$m->action('select')->render()[0]
);

Expand Down
2 changes: 1 addition & 1 deletion tests/RandomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ public function testTableWithSchema(): void
$this->assertSame($render, $doc->action('select')->render());

$this->assertSameSql(
'select "id", "name", "user_id", (select "name" from "' . $userSchema . '"."user" "_u_e8701ad48ba0" where "id" = "' . $docSchema . '"."doc"."user_id") "user" from "' . $docSchema . '"."doc" where (select "name" from "' . $userSchema . '"."user" "_u_e8701ad48ba0" where "id" = "' . $docSchema . '"."doc"."user_id") = :a',
'select "id", "name", "user_id", (select "name" from (select "id", "name" from "' . $userSchema . '"."user") "_u_e8701ad48ba0" where "id" = "__inner__"."user_id") "user" from (select "id", "name", "user_id" from "' . $docSchema . '"."doc") "__inner__" where (select "name" from (select "id", "name" from "' . $userSchema . '"."user") "_u_e8701ad48ba0" where "id" = "__inner__"."user_id") = :a',
$render[0]
);

Expand Down
8 changes: 4 additions & 4 deletions tests/ReferenceSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testLink(): void
$u->hasMany('Orders', ['model' => $o]);

$this->assertSameSql(
'select "id", "amount", "user_id" from "order" "_O_7442e29d7d53" where "user_id" = "user"."id"',
'select "id", "amount", "user_id" from (select "id", "amount", "user_id" from "order") "_O_7442e29d7d53" where "user_id" = "__inner__"."id"',
$u->refLink('Orders')->action('select')->render()[0]
);
}
Expand Down Expand Up @@ -114,7 +114,7 @@ public function testLink2(): void
$u->hasMany('cur', ['model' => $c, 'our_field' => 'currency_code', 'their_field' => 'code']);

$this->assertSameSql(
'select "id", "code", "name" from "currency" "_c_b5fddf1ef601" where "code" = "user"."currency_code"',
'select "id", "code", "name" from (select "id", "code", "name" from "currency") "_c_b5fddf1ef601" where "code" = "__inner__"."currency_code"',
$u->refLink('cur')->action('select')->render()[0]
);
}
Expand Down Expand Up @@ -153,7 +153,7 @@ public function testBasicOne(): void
$o->addCondition('amount', '<', 9);

$this->assertSameSql(
'select "id", "name" from "user" "_u_e8701ad48ba0" where "id" in (select "user_id" from "order" where ("amount" > :a and "amount" < :b))',
'select "id", "name" from (select "id", "name" from "user") "_u_e8701ad48ba0" where "id" in (select "user_id" from (select "id", "amount", "user_id" from "order") "__inner__" where ("amount" > :a and "amount" < :b))',
$o->ref('user_id')->action('select')->render()[0]
);
}
Expand Down Expand Up @@ -225,7 +225,7 @@ public function testRelatedExpression(): void
$i->addExpression('total_net', $i->refLink('line')->action('fx', ['sum', 'total_net']));

$this->assertSameSql(
'select "id", "ref_no", (select sum("total_net") from "invoice_line" "_l_6438c669e0d0" where "invoice_id" = "invoice"."id") "total_net" from "invoice"',
'select "id", "ref_no", (select sum("total_net") from (select "id", "invoice_id", "total_net", "total_vat", "total_gross" from "invoice_line") "_l_6438c669e0d0" where "invoice_id" = "__inner__"."id") "total_net" from (select "id", "ref_no" from "invoice") "__inner__"',
$i->action('select')->render()[0]
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/WithTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public function testWith(): void

// tests
$this->assertSameSql(
'with "i" ("user_id", "invoiced") as (select "user_id", "net" from "invoice" where "net" > :a)' . "\n"
. 'select "user"."id", "user"."name", "user"."salary", "_i"."invoiced" from "user" inner join "i" "_i" on "_i"."user_id" = "user"."id"',
'with "i" ("user_id", "invoiced") as (select "user_id", "net" from (select "id", "net", "user_id" from "invoice") "__inner__" where "net" > :a)' . "\n"
. 'select "__inner__"."id", "__inner__"."name", "__inner__"."salary", "_i"."invoiced" from (select "id", "name", "salary", "invoiced" from "user") "__inner__" inner join "i" "_i" on "_i"."user_id" = "__inner__"."id"',
$m->action('select')->render()[0]
);
$this->assertSame([
Expand Down

0 comments on commit 23d618d

Please sign in to comment.