Skip to content

Commit 3b9255b

Browse files
committed
sqlite: output view fields in SQL
1 parent d0c01db commit 3b9255b

File tree

5 files changed

+16
-9
lines changed

5 files changed

+16
-9
lines changed

lib/SQL/Translator/Parser/SQLite.pm

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ conflict_clause : /on conflict/i conflict_algorigthm
470470
471471
conflict_algorigthm : /(rollback|abort|fail|ignore|replace)/i
472472
473-
parens_field_list : '(' column_list ')'
473+
parens_field_list : '(' column_list ')'
474474
{ $item[2] }
475475
476476
column_list : field_name(s /,/)
@@ -556,11 +556,12 @@ trigger_name : qualified_name
556556
#
557557
# Create View
558558
#
559-
create : CREATE TEMPORARY(?) VIEW view_name AS select_statement
559+
create : CREATE TEMPORARY(?) VIEW view_name parens_field_list(?) AS select_statement
560560
{
561561
push @views, {
562562
name => $item[4]->{'name'},
563-
sql => $item[6],
563+
fields => $item[5][0],
564+
sql => $item[7],
564565
is_temporary => $item[2][0] ? 1 : 0,
565566
}
566567
}
@@ -724,6 +725,7 @@ sub parse {
724725
for my $def (@{ $result->{'views'} || [] }) {
725726
my $view = $schema->add_view(
726727
name => $def->{'name'},
728+
fields => $def->{'fields'},
727729
sql => $def->{'sql'},
728730
);
729731
}

lib/SQL/Translator/Producer/SQLite.pm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ sub create_view {
158158
if exists($extra->{if_not_exists}) && $extra->{if_not_exists};
159159
$create_view .= " ${view_name}";
160160

161+
if (my @fields = $view->fields) {
162+
my $field_list = join ', ', map { _generator->quote($_) } @fields;
163+
$create_view .= " ( ${field_list} )";
164+
}
165+
161166
if (my $sql = $view->sql) {
162167
$create_view .= " AS\n ${sql}";
163168
}

t/48xml-to-sqlite.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ CREATE TABLE "Another" (
6666
6767
DROP VIEW IF EXISTS "email_list";
6868
69-
CREATE VIEW "email_list" AS
69+
CREATE VIEW "email_list" ( "email" ) AS
7070
SELECT email FROM Basic WHERE (email IS NOT NULL);
7171
7272
DROP TRIGGER IF EXISTS "foo_trigger";
@@ -118,7 +118,7 @@ eq_or_diff(
118118
"num" numeric(10,2)
119119
)>,
120120
q<DROP VIEW IF EXISTS "email_list">,
121-
q<CREATE VIEW "email_list" AS
121+
q<CREATE VIEW "email_list" ( "email" ) AS
122122
SELECT email FROM Basic WHERE (email IS NOT NULL)>,
123123
q<DROP TRIGGER IF EXISTS "foo_trigger">,
124124
q<CREATE TRIGGER "foo_trigger" after insert on "Basic" BEGIN update modified=timestamp(); END>,

t/56-sqlite-producer.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ $SQL::Translator::Producer::SQLite::NO_QUOTES = 0;
2525
my $view1_sql1 = [ SQL::Translator::Producer::SQLite::create_view($view1, $create_opts) ];
2626

2727
my $view_sql_replace = [
28-
'CREATE TEMPORARY VIEW IF NOT EXISTS "view_foo" AS
28+
'CREATE TEMPORARY VIEW IF NOT EXISTS "view_foo" ( "id", "name" ) AS
2929
SELECT id, name FROM thing'
3030
];
3131
is_deeply($view1_sql1, $view_sql_replace, 'correct "CREATE TEMPORARY VIEW" SQL');
@@ -38,7 +38,7 @@ $SQL::Translator::Producer::SQLite::NO_QUOTES = 0;
3838

3939
my $view1_sql2 = [ SQL::Translator::Producer::SQLite::create_view($view2, $create_opts) ];
4040
my $view_sql_noreplace = [
41-
'CREATE VIEW "view_foo" AS
41+
'CREATE VIEW "view_foo" ( "id", "name" ) AS
4242
SELECT id, name FROM thing'
4343
];
4444
is_deeply($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL');

t/57-class-dbi.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use SQL::Translator::Producer::SQLite;
2323
my $view1_sql1 = [ SQL::Translator::Producer::SQLite::create_view($view1, $create_opts) ];
2424

2525
my $view_sql_replace = [
26-
'CREATE TEMPORARY VIEW IF NOT EXISTS view_foo AS
26+
'CREATE TEMPORARY VIEW IF NOT EXISTS view_foo ( id, name ) AS
2727
SELECT id, name FROM thing'
2828
];
2929
is_deeply($view1_sql1, $view_sql_replace, 'correct "CREATE TEMPORARY VIEW" SQL');
@@ -36,7 +36,7 @@ use SQL::Translator::Producer::SQLite;
3636

3737
my $view1_sql2 = [ SQL::Translator::Producer::SQLite::create_view($view2, $create_opts) ];
3838
my $view_sql_noreplace = [
39-
'CREATE VIEW view_foo AS
39+
'CREATE VIEW view_foo ( id, name ) AS
4040
SELECT id, name FROM thing'
4141
];
4242
is_deeply($view1_sql2, $view_sql_noreplace, 'correct "CREATE VIEW" SQL');

0 commit comments

Comments
 (0)