Schema Components

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Schema Components

Dave Cross-2

In the documentation of DBIx::Class::Schema::Loader::Base there is a  
reference to adding "schema components"[1] to the generated classes.  
But I can't find any documentation on how you might write such a  
component - or any clearly-labelled examples of a schema component.

I think I have an example of something that I'd like to write as a  
schema component. Please either tell me that I'm wrong, or gently  
nudge me in the right direction.

Here's what I want to do. Recently I've found myself adding a useful  
"get_schema()" method to many of my schema classes. It looks something  
like this:

sub get_schema {
   my @errs;

   foreach (qw[XXX_DB XXX_DB_USER XXX_DB_PASS XXX_DB_HOST]) {
     push @errs, $_ unless defined $ENV{$_};
   }
   if (@errs) {
     croak "You need to set the following environment variables: @errs\n";
   }

   return __PACKAGE__->connect(
     "dbi:mysql:database=$ENV{XXX_DB};host=$ENV{XXX_DB_HOST}",
     $ENV{XXX_DB_USER}, $ENV{XXX_DB_PASS},
     { mysql_enable_utf8 => 1 }
   );
}

The "XXX" is a placeholder for some prefix that is meaningful to  
whatever schema I'm working with and it will change with each project  
(as will, potentially, the DBD name and the options hash).

I've been copying the code into my schema classes, but we all know  
what a Bad Idea that is. And a schema component seemed a  
likely-looking approach for getting round that. I thought I could  
write DBIx::Class::Component::GetSchema which adds the method to any  
schema class that loads it.

But, as I said above, I can't find any explanations or examples of  
schema components that I can steal from.

Does anyone have any suggestions?

Cheers,

Dave...

[1]  
https://metacpan.org/pod/DBIx::Class::Schema::Loader::Base#schema_components



_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Schema Components

Darius Jokilehto-2
Hello Dave,


Darius


On Friday, 16 September 2016, 17:09, Dave Cross <[hidden email]> wrote:



In the documentation of DBIx::Class::Schema::Loader::Base there is a 
reference to adding "schema components"[1] to the generated classes. 
But I can't find any documentation on how you might write such a 
component - or any clearly-labelled examples of a schema component.

I think I have an example of something that I'd like to write as a 
schema component. Please either tell me that I'm wrong, or gently 
nudge me in the right direction.

Here's what I want to do. Recently I've found myself adding a useful 
"get_schema()" method to many of my schema classes. It looks something 
like this:

sub get_schema {
  my @errs;

  foreach (qw[XXX_DB XXX_DB_USER XXX_DB_PASS XXX_DB_HOST]) {
    push @errs, $_ unless defined $ENV{$_};
  }
  if (@errs) {
    croak "You need to set the following environment variables: @errs\n";
  }

  return __PACKAGE__->connect(
    "dbi:mysql:database=$ENV{XXX_DB};host=$ENV{XXX_DB_HOST}",
    $ENV{XXX_DB_USER}, $ENV{XXX_DB_PASS},
    { mysql_enable_utf8 => 1 }
  );
}

The "XXX" is a placeholder for some prefix that is meaningful to 
whatever schema I'm working with and it will change with each project 
(as will, potentially, the DBD name and the options hash).

I've been copying the code into my schema classes, but we all know 
what a Bad Idea that is. And a schema component seemed a 
likely-looking approach for getting round that. I thought I could 
write DBIx::Class::Component::GetSchema which adds the method to any 
schema class that loads it.

But, as I said above, I can't find any explanations or examples of 
schema components that I can steal from.

Does anyone have any suggestions?

Cheers,

Dave...

[1] 
https://metacpan.org/pod/DBIx::Class::Schema::Loader::Base#schema_components



_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...



_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Schema Components

Dave Cross-2

Hi Darius,

Thanks. Yes, I'd seen that documentation. It's a little sparse and  
seems to be aimed at writing components which are loaded into Result  
classes (InflateColumn::DateTime, TimeStamp and stuff like that). I  
suspect (but I could, of course, be wrong) that components which are  
loaded into Schema objects would follow different rules.

Cheers,

Dave...

Quoting Darius Jokilehto <[hidden email]>:

> Hello Dave,
> This might be what you're looking for:  
> https://metacpan.org/pod/distribution/DBIx-Class/lib/DBIx/Class/Manual/Component.pod
> Darius
>
>     On Friday, 16 September 2016, 17:09, Dave Cross <[hidden email]> wrote:
>
> In the documentation of DBIx::Class::Schema::Loader::Base there is a 
> reference to adding "schema components"[1] to the generated classes. 
> But I can't find any documentation on how you might write such a 
> component - or any clearly-labelled examples of a schema component.
>
> I think I have an example of something that I'd like to write as a 
> schema component. Please either tell me that I'm wrong, or gently 
> nudge me in the right direction.
>
> Here's what I want to do. Recently I've found myself adding a useful 
> "get_schema()" method to many of my schema classes. It looks something 
> like this:
>
> sub get_schema {
>   my @errs;
>
>   foreach (qw[XXX_DB XXX_DB_USER XXX_DB_PASS XXX_DB_HOST]) {
>     push @errs, $_ unless defined $ENV{$_};
>   }
>   if (@errs) {
>     croak "You need to set the following environment variables: @errs\n";
>   }
>
>   return __PACKAGE__->connect(
>     "dbi:mysql:database=$ENV{XXX_DB};host=$ENV{XXX_DB_HOST}",
>     $ENV{XXX_DB_USER}, $ENV{XXX_DB_PASS},
>     { mysql_enable_utf8 => 1 }
>   );
> }
>
> The "XXX" is a placeholder for some prefix that is meaningful to 
> whatever schema I'm working with and it will change with each project 
> (as will, potentially, the DBD name and the options hash).
>
> I've been copying the code into my schema classes, but we all know 
> what a Bad Idea that is. And a schema component seemed a 
> likely-looking approach for getting round that. I thought I could 
> write DBIx::Class::Component::GetSchema which adds the method to any 
> schema class that loads it.
>
> But, as I said above, I can't find any explanations or examples of 
> schema components that I can steal from.
>
> Does anyone have any suggestions?
>
> Cheers,
>
> Dave...
>
> [1] 
> https://metacpan.org/pod/DBIx::Class::Schema::Loader::Base#schema_components
>
>
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive:  
> http://www.grokbase.com/group/dbix-class@...




_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Schema Components

Darius Jokilehto-2
Hi Dave,

Ah, right - I see what you mean. Maybe this is more what you're looking for: https://metacpan.org/pod/DBIx::Class::Helper::Schema::DateTime? Looking at the source it looks like the only special thing it really does is inherit from DBIx::Class::Schema.

HTH,
Darius


On Tuesday, 20 September 2016, 12:11, Dave Cross <[hidden email]> wrote:



Hi Darius,

Thanks. Yes, I'd seen that documentation. It's a little sparse and 
seems to be aimed at writing components which are loaded into Result 
classes (InflateColumn::DateTime, TimeStamp and stuff like that). I 
suspect (but I could, of course, be wrong) that components which are 
loaded into Schema objects would follow different rules.

Cheers,

Dave...

Quoting Darius Jokilehto <[hidden email]>:

> Hello Dave,
> This might be what you're looking for: 
> https://metacpan.org/pod/distribution/DBIx-Class/lib/DBIx/Class/Manual/Component.pod
> Darius
>
>    On Friday, 16 September 2016, 17:09, Dave Cross <[hidden email]> wrote:
>
> In the documentation of DBIx::Class::Schema::Loader::Base there is a 
> reference to adding "schema components"[1] to the generated classes. 
> But I can't find any documentation on how you might write such a 
> component - or any clearly-labelled examples of a schema component.
>
> I think I have an example of something that I'd like to write as a 
> schema component. Please either tell me that I'm wrong, or gently 
> nudge me in the right direction.
>
> Here's what I want to do. Recently I've found myself adding a useful 
> "get_schema()" method to many of my schema classes. It looks something 
> like this:
>
> sub get_schema {
>   my @errs;
>
>   foreach (qw[XXX_DB XXX_DB_USER XXX_DB_PASS XXX_DB_HOST]) {
>     push @errs, $_ unless defined $ENV{$_};
>   }
>   if (@errs) {
>     croak "You need to set the following environment variables: @errs\n";
>   }
>
>   return __PACKAGE__->connect(
>     "dbi:mysql:database=$ENV{XXX_DB};host=$ENV{XXX_DB_HOST}",
>     $ENV{XXX_DB_USER}, $ENV{XXX_DB_PASS},
>     { mysql_enable_utf8 => 1 }
>   );
> }
>
> The "XXX" is a placeholder for some prefix that is meaningful to 
> whatever schema I'm working with and it will change with each project 
> (as will, potentially, the DBD name and the options hash).
>
> I've been copying the code into my schema classes, but we all know 
> what a Bad Idea that is. And a schema component seemed a 
> likely-looking approach for getting round that. I thought I could 
> write DBIx::Class::Component::GetSchema which adds the method to any 
> schema class that loads it.
>
> But, as I said above, I can't find any explanations or examples of 
> schema components that I can steal from.
>
> Does anyone have any suggestions?
>
> Cheers,
>
> Dave...
>
> [1] 
> https://metacpan.org/pod/DBIx::Class::Schema::Loader::Base#schema_components
>
>
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: 
> http://www.grokbase.com/group/dbix-class@...




_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...



_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Schema Components

Sym Kat
In reply to this post by Dave Cross-2
Have you looked at dbic-schema-config, it should provide a really simple place to insert the env search (docs on overloading in the pod, I believe), or at least a decent example of doing a similar thing.

Sent from my iPhone

> On Sep 16, 2016, at 9:08 AM, Dave Cross <[hidden email]> wrote:
>
>
> In the documentation of DBIx::Class::Schema::Loader::Base there is a reference to adding "schema components"[1] to the generated classes. But I can't find any documentation on how you might write such a component - or any clearly-labelled examples of a schema component.
>
> I think I have an example of something that I'd like to write as a schema component. Please either tell me that I'm wrong, or gently nudge me in the right direction.
>
> Here's what I want to do. Recently I've found myself adding a useful "get_schema()" method to many of my schema classes. It looks something like this:
>
> sub get_schema {
>  my @errs;
>
>  foreach (qw[XXX_DB XXX_DB_USER XXX_DB_PASS XXX_DB_HOST]) {
>    push @errs, $_ unless defined $ENV{$_};
>  }
>  if (@errs) {
>    croak "You need to set the following environment variables: @errs\n";
>  }
>
>  return __PACKAGE__->connect(
>    "dbi:mysql:database=$ENV{XXX_DB};host=$ENV{XXX_DB_HOST}",
>    $ENV{XXX_DB_USER}, $ENV{XXX_DB_PASS},
>    { mysql_enable_utf8 => 1 }
>  );
> }
>
> The "XXX" is a placeholder for some prefix that is meaningful to whatever schema I'm working with and it will change with each project (as will, potentially, the DBD name and the options hash).
>
> I've been copying the code into my schema classes, but we all know what a Bad Idea that is. And a schema component seemed a likely-looking approach for getting round that. I thought I could write DBIx::Class::Component::GetSchema which adds the method to any schema class that loads it.
>
> But, as I said above, I can't find any explanations or examples of schema components that I can steal from.
>
> Does anyone have any suggestions?
>
> Cheers,
>
> Dave...
>
> [1] https://metacpan.org/pod/DBIx::Class::Schema::Loader::Base#schema_components
>
>
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/dbix-class@...

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Schema Components

Marco
In reply to this post by Dave Cross-2
Excerpts from Dave Cross's message of 2016-09-16 17:08:14 +0100:
>
> In the documentation of DBIx::Class::Schema::Loader::Base there is a  
> reference to adding "schema components"[1] to the generated classes.  
> But I can't find any documentation on how you might write such a  
> component - or any clearly-labelled examples of a schema component.
>
> I think I have an example of something that I'd like to write as a  
> schema component. Please either tell me that I'm wrong, or gently  
> nudge me in the right direction.

I don't know if it is the right direction, but you could simple use
inheritance in this case. Creating a subclass of DBIx::Class::Schema
with your 'get_schema' method or simple overriding ->connection to search
for a sensible environment if not given explicit arguments.


--
Marco Arthur @ (M)arco Creatives

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...