dbic helper - make user/pass optional for sqlite, add a couple more tests
Rafael Kitover [Sun, 17 May 2009 23:20:51 +0000 (23:20 +0000)]
Makefile.PL
lib/Catalyst/Helper/Model/DBIC/Schema.pm
lib/Catalyst/Model/DBIC/Schema.pm
t/05testapp.t

index ee6910e..304c99f 100644 (file)
@@ -14,7 +14,7 @@ requires 'namespace::clean';
 requires 'Carp::Clan';
 requires 'List::MoreUtils';
 
-test_requires 'Test::More';
+build_requires 'Test::More';
 
 feature 'Catalyst::Helper support',
     -default                      => 0,
@@ -23,10 +23,13 @@ feature 'Catalyst::Helper support',
     'DBIx::Class::Schema::Loader' => '0.04005';
 
 feature 'Caching support',
+    -default                      => 0,
     'DBIx::Class::Cursor::Cached' => 0;
 
 feature 'Replication support',
-    'MooseX::AttributeHelpers' => 0;
+    -default                   => 0,
+    'MooseX::AttributeHelpers' => 0,
+    'Hash::Merge'              => 0;
 
 if(-e 'MANIFEST.SKIP') {
     system("pod2text lib/Catalyst/Model/DBIC/Schema.pm > README");
index edd09ff..8c8d0f6 100644 (file)
@@ -64,40 +64,58 @@ password, and connect options, respectively.  These are optional for
 existing Schemas, but required if you use either of the C<create=>
 options.
 
+username and password can be omitted for C<SQLite> dsns.
+
 Use of either of the C<create=> options requires L<DBIx::Class::Schema::Loader>.
 
 =head1 TYPICAL EXAMPLES
 
-  # Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema,
-  #  and a Model which references it:
+Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema,
+and a Model which references it:
+
   script/myapp_create.pl model CatalystModelName DBIC::Schema \
     MyApp::SchemaClass create=static dbi:mysql:foodb myuname mypass
 
-  # Same, with extra connect_info args
+Same, with extra connect_info args
+user and pass can be omitted for sqlite, since they are always empty
+
   script/myapp_create.pl model CatalystModelName DBIC::Schema \
-    MyApp::SchemaClass create=static dbi:SQLite:foo.db '' '' \
+    MyApp::SchemaClass create=static dbi:SQLite:foo.db \
     AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \
-    on_connect_do='["select 1", "select 2"]'
+    on_connect_do='["select 1", "select 2"]' quote_char='"'
+
+B<ON WINDOWS COMMAND LINES QUOTING RULES ARE DIFFERENT>
+
+In C<cmd.exe> the above example would be:
+
+  script/myapp_create.pl model CatalystModelName DBIC::Schema \
+    MyApp::SchemaClass create=static dbi:SQLite:foo.db \
+    AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \
+    on_connect_do="[\"select 1\", \"select 2\"]" quote_char="\""
+
+Same, but with extra Schema::Loader args (separate multiple values by commas):
 
-  # Same, but with extra Schema::Loader args (separate multiple values by commas):
   script/myapp_create.pl model CatalystModelName DBIC::Schema \
     MyApp::SchemaClass create=static db_schema=foodb components=Foo,Bar \
-    exclude='^wibble|wobble$' moniker_map='{ foo => "FFFFUUUU" }' \
+    exclude='^wibble|wobble$' moniker_map='{ foo => "FOO" }' \
     dbi:Pg:dbname=foodb myuname mypass
 
-  # See DBIx::Class::Schema::Loader::Base for list of options
+See L<DBIx::Class::Schema::Loader::Base> for a list of options
+
+Create a dynamic DBIx::Class::Schema::Loader-based Schema,
+and a Model which references it (B<DEPRECATED>):
 
-  # Create a dynamic DBIx::Class::Schema::Loader-based Schema,
-  #  and a Model which references it:
   script/myapp_create.pl model CatalystModelName DBIC::Schema \
     MyApp::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass
 
-  # Reference an existing Schema of any kind, and provide some connection information for ->config:
+Reference an existing Schema of any kind, and provide some connection information for ->config:
+
   script/myapp_create.pl model CatalystModelName DBIC::Schema \
     MyApp::SchemaClass dbi:mysql:foodb myuname mypass
 
-  # Same, but don't supply connect information yet (you'll need to do this
-  #  in your app config, or [not recommended] in the schema itself).
+Same, but don't supply connect information yet (you'll need to do this
+in your app config, or [not recommended] in the schema itself).
+
   script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass
 
 =cut
@@ -262,7 +280,7 @@ sub _build_helper_connect_info {
 
     my @connect_info = @$connect_info;
 
-    my ($dsn, $user, $password) = splice @connect_info, 0, 3;
+    my ($dsn, $user, $password) = $self->_get_dsn_user_pass(\@connect_info);
 
     tie my %helper_connect_info, 'Tie::IxHash';
 
@@ -328,12 +346,28 @@ sub _data_struct_to_string {
     return Data::Dumper->Dump([$data]);
 }
 
+sub _get_dsn_user_pass {
+    my ($self, $connect_info) = @_;
+
+    my $dsn = shift @$connect_info;
+    my ($user, $password);
+
+    if ($dsn =~ /sqlite/i) {
+        ($user, $password) = ('', '');
+        shift @$connect_info while $connect_info->[0] eq '';
+    } else {
+        ($user, $password) = splice @$connect_info, 0, 2;
+    }
+    
+    ($dsn, $user, $password)
+}
+
 sub _parse_connect_info {
     my ($self, $connect_info) = @_;
 
     my @connect_info = @$connect_info;
 
-    my ($dsn, $user, $password) = splice @connect_info, 0, 3;
+    my ($dsn, $user, $password) = $self->_get_dsn_user_pass(\@connect_info);
 
     tie my %connect_info, 'Tie::IxHash';
     @connect_info{qw/dsn user password/} = ($dsn, $user, $password);
@@ -565,3 +599,5 @@ it under the same terms as Perl itself.
 =cut
 
 1;
+__END__
+# vim:sts=4 sw=4:
index e39ffcf..9c5c2f5 100644 (file)
@@ -246,6 +246,7 @@ Or using L<Config::General>:
             user   postgres
             password ''
             auto_savepoint 1
+           quote_char """
             on_connect_do   some SQL statement
             on_connect_do   another SQL statement
         </connect_info>
@@ -270,6 +271,7 @@ Or using L<YAML>:
           LongTruncOk: 1
           on_connect_do: [ "alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS'" ]
           cursor_class: 'DBIx::Class::Cursor::Cached'
+         quote_char: '"'
 
 The old arrayref style with hashrefs for L<DBI> then L<DBIx::Class> options is also
 supported:
index d820518..5d71ce7 100644 (file)
@@ -12,21 +12,44 @@ plan skip_all => 'Enable this optional test with $ENV{C_M_DBIC_SCHEMA_TESTAPP}'
 my $test_params = [
     [ 'TestSchema', 'DBIC::Schema', '' ],
     [ 'TestSchemaDSN', 'DBIC::Schema', q{fakedsn fakeuser fakepass "{ AutoCommit => 1 }"} ],
+    [ 'TestSchemaDSN', 'DBIC::Schema', q{create=static roles=Caching moniker_map="{ roles => \"ROLE\" }" constraint="^users\z" dbi:SQLite:testdb.db "" "" on_connect_do="[\"select 1\", \"select 2\"]" quote_char="\""} ],
+    [ 'TestSchemaDSN', 'DBIC::Schema', q{create=static roles=Caching moniker_map="{ roles => \"ROLE\" }" dbi:SQLite:testdb.db on_connect_do="[\"select 1\", \"select 2\"]" quote_char="\""} ],
 ];
 
-plan tests => (2 * @$test_params);
+plan tests => (2 * @$test_params + 2);
 
 my $test_dir   = $FindBin::Bin;
 my $blib_dir   = File::Spec->catdir ($test_dir, '..', 'blib', 'lib');
 my $cat_dir    = File::Spec->catdir ($test_dir, 'TestApp');
 my $catlib_dir = File::Spec->catdir ($cat_dir, 'lib');
+my $schema_dir = File::Spec->catdir ($catlib_dir, 'TestSchemaDSN');
 my $creator    = File::Spec->catfile($cat_dir, 'script', 'testapp_create.pl');
 my $model_dir  = File::Spec->catdir ($catlib_dir, 'TestApp', 'Model');
+my $db         = File::Spec->catdir ($cat_dir, 'testdb.db');
 
 chdir($test_dir);
 system("catalyst.pl TestApp");
 chdir($cat_dir);
 
+# create test db
+open my $sql, '|-', 'sqlite3', $db or die $!;
+print $sql <<'EOF';
+CREATE TABLE users (                       
+        id            INTEGER PRIMARY KEY, 
+        username      TEXT,                
+        password      TEXT,                
+        email_address TEXT,                
+        first_name    TEXT,                
+        last_name     TEXT,                
+        active        INTEGER              
+);
+CREATE TABLE roles (
+        id   INTEGER PRIMARY KEY,
+        role TEXT
+);
+EOF
+close $sql;
+
 foreach my $tparam (@$test_params) {
    my ($model, $helper, $args) = @$tparam;
    system("$^X -I$blib_dir $creator model $model $helper $model $args");
@@ -34,6 +57,16 @@ foreach my $tparam (@$test_params) {
    ok( -f $model_path, "$model_path is a file" );
    my $compile_rv = system("$^X -I$blib_dir -I$catlib_dir -c $model_path");
    ok($compile_rv == 0, "perl -c $model_path");
+
+   if ($args =~ /create=static/) {
+      my $glob = File::Spec->catfile($schema_dir, 'Result', '*');
+      my $tables =()= glob($glob);
+      if ($args =~ /constraint/) {
+         is $tables, 1, 'constraint works';
+      } else {
+         is $tables, 2, 'correct number of tables';
+      }
+   }
 }
 
 chdir($test_dir);