Skip to content

Commit

Permalink
Standardize examples/docs on Schema::Loader schema naming
Browse files Browse the repository at this point in the history
A new user is very likely to encounter this naming convention these days
  • Loading branch information
ribasushi committed Aug 26, 2013
1 parent d39311e commit a5bd5d8
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package MyDatabase::Main;
package MyApp::Schema;

use warnings;
use strict;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package MyDatabase::Main::Result::Artist;
package MyApp::Schema::Result::Artist;

use warnings;
use strict;
Expand All @@ -11,7 +11,7 @@ __PACKAGE__->add_columns(qw/ artistid name /);

__PACKAGE__->set_primary_key('artistid');

__PACKAGE__->has_many('cds' => 'MyDatabase::Main::Result::Cd');
__PACKAGE__->has_many('cds' => 'MyApp::Schema::Result::Cd');

1;

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package MyDatabase::Main::Result::Cd;
package MyApp::Schema::Result::Cd;

use warnings;
use strict;
Expand All @@ -11,7 +11,7 @@ __PACKAGE__->add_columns(qw/ cdid artist title year /);

__PACKAGE__->set_primary_key('cdid');

__PACKAGE__->belongs_to('artist' => 'MyDatabase::Main::Result::Artist');
__PACKAGE__->has_many('tracks' => 'MyDatabase::Main::Result::Track');
__PACKAGE__->belongs_to('artist' => 'MyApp::Schema::Result::Artist');
__PACKAGE__->has_many('tracks' => 'MyApp::Schema::Result::Track');

1;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package MyDatabase::Main::Result::Track;
package MyApp::Schema::Result::Track;

use warnings;
use strict;
Expand All @@ -11,6 +11,6 @@ __PACKAGE__->add_columns(qw/ trackid cd title/);

__PACKAGE__->set_primary_key('trackid');

__PACKAGE__->belongs_to('cd' => 'MyDatabase::Main::Result::Cd');
__PACKAGE__->belongs_to('cd' => 'MyApp::Schema::Result::Cd');

1;
4 changes: 2 additions & 2 deletions examples/Schema/insertdb.pl
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
use strict;
use warnings;

use MyDatabase::Main;
use MyApp::Schema;

my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
my $schema = MyApp::Schema->connect('dbi:SQLite:db/example.db');

# here's some of the sql that is going to be generated by the schema
# INSERT INTO artist VALUES (NULL,'Michael Jackson');
Expand Down
4 changes: 2 additions & 2 deletions examples/Schema/testdb.pl
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
use warnings;
use strict;

use MyDatabase::Main;
use MyApp::Schema;

my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
my $schema = MyApp::Schema->connect('dbi:SQLite:db/example.db');
# for other DSNs, e.g. MySql, see the perldoc for the relevant dbd
# driver, e.g perldoc L<DBD::mysql>.

Expand Down
8 changes: 4 additions & 4 deletions lib/DBIx/Class/Manual/Cookbook.pod
Original file line number Diff line number Diff line change
Expand Up @@ -1163,14 +1163,14 @@ as each other and your connecting database user has the proper permissions to th
To accomplish this one only needs to specify the DB schema name in the table
declaration, like so...

package MyDatabase::Main::Artist;
package MyApp::Schema::Result::Artist;
use base qw/DBIx::Class::Core/;

__PACKAGE__->table('database1.artist'); # will use "database1.artist" in FROM clause

__PACKAGE__->add_columns(qw/ artist_id name /);
__PACKAGE__->set_primary_key('artist_id');
__PACKAGE__->has_many('cds' => 'MyDatabase::Main::Cd');
__PACKAGE__->has_many('cds' => 'MyApp::Schema::Result::Cd');

1;

Expand All @@ -1186,7 +1186,7 @@ However, one can dynamically "map" to the proper DB schema by overriding the
L<connection|DBIx::Class::Schama/connection> method in your Schema class and
building a renaming facility, like so:

package MyDatabase::Schema;
package MyApp::Schema;
use Moose;

extends 'DBIx::Class::Schema';
Expand Down Expand Up @@ -1232,7 +1232,7 @@ To use this facility, simply add or modify the \%attr hashref that is passed to
L<connection|DBIx::Class::Schama/connect>, as follows:

my $schema
= MyDatabase::Schema->connect(
= MyApp::Schema->connect(
$dsn,
$user,
$pass,
Expand Down
46 changes: 23 additions & 23 deletions lib/DBIx/Class/Manual/Example.pod
Original file line number Diff line number Diff line change
Expand Up @@ -70,56 +70,56 @@ Change directory back from db to the directory app:

Now create some more directories:

mkdir MyDatabase
mkdir MyDatabase/Main
mkdir MyDatabase/Main/Result
mkdir MyDatabase/Main/ResultSet
mkdir MyApp
mkdir MyApp/Schema
mkdir MyApp/Schema/Result
mkdir MyApp/Schema/ResultSet

Then, create the following DBIx::Class::Schema classes:

MyDatabase/Main.pm:
MyApp/Schema.pm:

package MyDatabase::Main;
package MyApp::Schema;
use base qw/DBIx::Class::Schema/;
__PACKAGE__->load_namespaces;

1;


MyDatabase/Main/Result/Artist.pm:
MyApp/Schema/Result/Artist.pm:

package MyDatabase::Main::Result::Artist;
package MyApp::Schema::Result::Artist;
use base qw/DBIx::Class::Core/;
__PACKAGE__->table('artist');
__PACKAGE__->add_columns(qw/ artistid name /);
__PACKAGE__->set_primary_key('artistid');
__PACKAGE__->has_many('cds' => 'MyDatabase::Main::Result::Cd');
__PACKAGE__->has_many('cds' => 'MyApp::Schema::Result::Cd');

1;


MyDatabase/Main/Result/Cd.pm:
MyApp/Schema/Result/Cd.pm:

package MyDatabase::Main::Result::Cd;
package MyApp::Schema::Result::Cd;
use base qw/DBIx::Class::Core/;
__PACKAGE__->load_components(qw/InflateColumn::DateTime/);
__PACKAGE__->table('cd');
__PACKAGE__->add_columns(qw/ cdid artist title/);
__PACKAGE__->set_primary_key('cdid');
__PACKAGE__->belongs_to('artist' => 'MyDatabase::Main::Result::Artist');
__PACKAGE__->has_many('tracks' => 'MyDatabase::Main::Result::Track');
__PACKAGE__->belongs_to('artist' => 'MyApp::Schema::Result::Artist');
__PACKAGE__->has_many('tracks' => 'MyApp::Schema::Result::Track');

1;


MyDatabase/Main/Result/Track.pm:
MyApp/Schema/Result/Track.pm:

package MyDatabase::Main::Result::Track;
package MyApp::Schema::Result::Track;
use base qw/DBIx::Class::Core/;
__PACKAGE__->table('track');
__PACKAGE__->add_columns(qw/ trackid cd title /);
__PACKAGE__->set_primary_key('trackid');
__PACKAGE__->belongs_to('cd' => 'MyDatabase::Main::Result::Cd');
__PACKAGE__->belongs_to('cd' => 'MyApp::Schema::Result::Cd');

1;

Expand All @@ -133,9 +133,9 @@ insertdb.pl
use strict;
use warnings;

use MyDatabase::Main;
use MyApp::Schema;

my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
my $schema = MyApp::Schema->connect('dbi:SQLite:db/example.db');

# here's some of the SQL that is going to be generated by the schema
# INSERT INTO artist VALUES (NULL,'Michael Jackson');
Expand Down Expand Up @@ -199,9 +199,9 @@ testdb.pl:
use strict;
use warnings;

use MyDatabase::Main;
use MyApp::Schema;

my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
my $schema = MyApp::Schema->connect('dbi:SQLite:db/example.db');
# for other DSNs, e.g. MySQL, see the perldoc for the relevant dbd
# driver, e.g perldoc L<DBD::mysql>.

Expand Down Expand Up @@ -354,15 +354,15 @@ are available in the main distribution for DBIx::Class under the
directory F<t/examples/Schema>.

With these scripts we're relying on @INC looking in the current
working directory. You may want to add the MyDatabase namespaces to
working directory. You may want to add the MyApp namespaces to
@INC in a different way when it comes to deployment.

The F<testdb.pl> script is an excellent start for testing your database
model.

This example uses L<DBIx::Class::Schema/load_namespaces> to load in the
appropriate L<Row|DBIx::Class::Row> classes from the MyDatabase::Main::Result namespace,
and any required resultset classes from the MyDatabase::Main::ResultSet
appropriate L<Row|DBIx::Class::Row> classes from the MyApp::Schema::Result namespace,
and any required resultset classes from the MyApp::Schema::ResultSet
namespace (although we created the directory in the directions above we
did not add, or need to add, any resultset classes).

Expand Down
2 changes: 1 addition & 1 deletion lib/DBIx/Class/Manual/Intro.pod
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ If you have a mixed-case database, use the C<preserve_case> option, e.g.:
If you are using L<Catalyst>, then you can use the helper that comes with
L<Catalyst::Model::DBIC::Schema>:

$ script/myapp_create.pl model MyDB DBIC::Schema MyDB::Schema \
$ script/myapp_create.pl model MyModel DBIC::Schema MyApp::Schema \
create=static moniker_map='{ foo => "FOO" }' dbi:SQLite:./myapp.db \
on_connect_do='PRAGMA foreign_keys=ON' quote_char='"'

Expand Down
14 changes: 7 additions & 7 deletions lib/DBIx/Class/Manual/QuickStart.pod
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ Inspect the database:
You can also use a GUI database browser such as
L<SQLite Manager|https://addons.mozilla.org/firefox/addon/sqlite-manager>.

Have a look at the schema classes files in the subdirectory F<MyDatabase>. The
C<MyDatabase::Main> class is the entry point for loading the other classes and
Have a look at the schema classes files in the subdirectory F<MyApp>. The
C<MyApp::Schema> class is the entry point for loading the other classes and
interacting with the database through DBIC and the C<Result> classes correspond
to the tables in the database. L<DBIx::Class::Manual::Example> shows how to
write all that Perl code. That is almost never necessary, though. Instead use
Expand All @@ -46,8 +46,8 @@ chapter L</"Resetting the database"> below shows an example invocation.

A L<schema|DBIx::Class::Manual::Glossary/Schema> object represents the database.

use MyDatabase::Main qw();
my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
use MyApp::Schema qw();
my $schema = MyApp::Schema->connect('dbi:SQLite:db/example.db');

The first four arguments are the same as for L<DBI/connect>.

Expand Down Expand Up @@ -114,7 +114,7 @@ Set up a condition.
}
);

Iterate over result objects of class C<MyDatabase::Main::Result::Artist>.
Iterate over result objects of class C<MyApp::Schema::Result::Artist>.
L<Result|DBIx::Class::Manual::Glossary/Result> objects represent a row and
automatically get accessors for their column names.

Expand Down Expand Up @@ -175,11 +175,11 @@ From a detail to the root:
DBIx-Class/examples/Schema$ perl ./insertdb.pl

# delete the schema classes files
DBIx-Class/examples/Schema$ rm -rf MyDatabase/
DBIx-Class/examples/Schema$ rm -rf MyApp

# recreate schema classes files from database file
DBIx-Class/examples/Schema$ dbicdump \
-o dump_directory=. MyDatabase::Main dbi:SQLite:db/example.db
-o dump_directory=. MyApp::Schema dbi:SQLite:db/example.db

=head2 Where to go next

Expand Down
3 changes: 2 additions & 1 deletion lib/DBIx/Class/Schema.pm
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ particular which module inherits off which.
=back
package MyApp::Schema;
__PACKAGE__->load_namespaces();
__PACKAGE__->load_namespaces(
result_namespace => 'Res',
resultset_namespace => 'RSet',
default_resultset_class => '+MyDB::Othernamespace::RSet',
default_resultset_class => '+MyApp::Othernamespace::RSet',
);
With no arguments, this method uses L<Module::Find> to load all of the
Expand Down
2 changes: 1 addition & 1 deletion lib/DBIx/Class/Storage/DBI/mysql.pm
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ DBIx::Class::Storage::DBI::mysql - Storage::DBI class implementing MySQL specifi
Storage::DBI autodetects the underlying MySQL database, and re-blesses the
C<$storage> object into this class.
my $schema = MyDb::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
my $schema = MyApp::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
=head1 DESCRIPTION
Expand Down

0 comments on commit a5bd5d8

Please sign in to comment.