-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MySQL Parser understands VIEWs with a field list. #87
base: master
Are you sure you want to change the base?
Changes from 1 commit
707abe2
3d090a5
8bdf3bf
9953926
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -502,17 +502,63 @@ BEGIN { | |
is( join(',', $t2c2->reference_fields), 'id', 'To field "id"' ); | ||
} | ||
|
||
# Tests for CREATE VIEW statements that contain a column list | ||
# after the view name | ||
# Tests CREATE VIEW statements containing field lists and/or aliases | ||
{ | ||
my $tr = SQL::Translator->new(); | ||
my $data = parse($tr, | ||
my $tr = SQL::Translator->new(); | ||
my $data = parse( | ||
$tr, | ||
q[ | ||
CREATE | ||
VIEW view_foo (id, name) AS | ||
VIEW view_foo (a, b) AS | ||
SELECT id, name FROM thing; | ||
|
||
CREATE | ||
VIEW view_bar AS | ||
SELECT id AS c, name AS d FROM thing; | ||
|
||
CREATE | ||
VIEW view_baz (e, f) AS | ||
SELECT id AS g, name AS h FROM thing; | ||
] | ||
) or die $tr->error; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also test that the view is in fact correctly parsed and that the column names after the view name take precedence over the ones in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added better tests - it now checks VIEW statements that include 1) a field list, 2) aliases in the SELECT statement, and 3) both a field list and aliases. (When both are specified, I believe the field list should override the column aliases.) |
||
|
||
my $schema = $tr->schema; | ||
my @views = $schema->get_views; | ||
is( scalar @views, 3, 'Right number of views (3)' ); | ||
my ( $view1, $view2, $view3 ) = @views; | ||
|
||
is( $view1->name, 'view_foo', 'Found "view_foo" view' ); | ||
is( join( ',', $view1->fields ), | ||
join( ',', qw[ a b ] ), | ||
'View 1 has correct fields' | ||
); | ||
like( | ||
$view1->sql, | ||
qr/create view view_foo as select id as a, name as b\s+from\s+thing/i, | ||
'View 1 has correct sql' | ||
); | ||
|
||
is( $view2->name, 'view_bar', 'Found "view_bar" view' ); | ||
is( join( ',', $view2->fields ), | ||
join( ',', qw[ c d ] ), | ||
'View 2 has correct fields' | ||
); | ||
like( | ||
$view2->sql, | ||
qr/create view view_bar as select id as c, name as d\s+from\s+thing/i, | ||
'View 2 has correct sql' | ||
); | ||
|
||
is( $view3->name, 'view_baz', 'Found "view_baz" view' ); | ||
is( join( ',', $view3->fields ), | ||
join( ',', qw[ e f ] ), | ||
'View 3 has correct fields' | ||
); | ||
like( | ||
$view3->sql, | ||
qr/create view view_baz as select id as e, name as f\s+from\s+thing/i, | ||
'View 3 has correct sql' | ||
); | ||
} | ||
|
||
# cch Tests for: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all, never use
map
in void context for side effects, usefor
instead.Secondly, this all could be simplified to:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More importantly, this is unnecessary if you instead store
$item{'parens_field_list(?)'}
in$views{$view_name}{fields}
and use that in preference to the select aliases in the->add_view
call.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we store the fields in the
%views
hash instead of setting the column aliases, then the generated SQL won't include the aliases.I've used your simplified code (though I'm using
$item{'parens_field_list(?)'}[0]
instead of$item{'parens_field_list(?)'}
, since it seems to be an arrayref within an arrayref) and left the rest the same.