From: Jess Robinson Date: Tue, 26 Aug 2008 21:46:44 +0000 (+0000) Subject: Added patch from groditi to support views in sqlite X-Git-Tag: v0.11008~304 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ec59a59706d3aede26dbdcfec8e3ba5d64e2d8f7;p=dbsrgits%2FSQL-Translator.git Added patch from groditi to support views in sqlite --- diff --git a/AUTHORS b/AUTHORS index e2bf8af..b3b8ff1 100644 --- a/AUTHORS +++ b/AUTHORS @@ -17,6 +17,7 @@ The following people have contributed to the SQLFairy project: - Chris To - Jason Williams - Ying Zhang +- Guillermo Roditi If you would like to contribute to the project, you can send patches to the developers mailing list: diff --git a/Changes b/Changes index 6196c3e..3d36a19 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,7 @@ +# ---------------------------------------------------------- +# +# ---------------------------------------------------------- +* Added support for CREATE VIEW + tests in the sqlite producer (groditi) # ---------------------------------------------------------- # 0.09001 2008-08-19 diff --git a/lib/SQL/Translator/Producer/SQLite.pm b/lib/SQL/Translator/Producer/SQLite.pm index a65043d..eea69c4 100644 --- a/lib/SQL/Translator/Producer/SQLite.pm +++ b/lib/SQL/Translator/Producer/SQLite.pm @@ -76,6 +76,10 @@ sub produce { push @table_defs, $create, map( { "$_;" } @defs), ""; } + for my $view ( $schema->get_views ) { + push @table_defs, create_view($view, {no_comments => $no_comments,}); + } + # $create .= "COMMIT;\n"; return wantarray ? ($create, @table_defs, "COMMIT;\n") : join("\n", ($create, @table_defs, "COMMIT;\n")); @@ -117,6 +121,30 @@ sub mk_name { return $name; } +sub create_view { + my ($view, $options) = @_; + + my $view_name = $view->name; + debug("PKG: Looking at view '${view_name}'\n"); + + # Header. Should this look like what mysqldump produces? + my $extra = $view->extra; + my $create = ''; + $create .= "--\n-- View: ${view_name}\n--\n" unless $options->{no_comments}; + $create .= 'CREATE'; + $create .= " TEMPORARY" if exists($extra->{temporary}) && $extra->{temporary}; + $create .= ' VIEW'; + $create .= " IF NOT EXISTS" if exists($extra->{if_not_exists}) && $extra->{if_not_exists}; + $create .= " ${view_name}"; + + if( my $sql = $view->sql ){ + $create .= " AS (\n ${sql}\n )"; + } + $create .= ";\n\n"; + return $create; +} + + sub create_table { my ($table, $options) = @_; diff --git a/t/48xml-to-sqlite.t b/t/48xml-to-sqlite.t index 9b8734e..3473824 100644 --- a/t/48xml-to-sqlite.t +++ b/t/48xml-to-sqlite.t @@ -62,5 +62,10 @@ CREATE TABLE Another ( ); +CREATE VIEW email_list AS ( + SELECT email FROM Basic WHERE email IS NOT NULL + ); + + COMMIT; SQL