The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

DBIx::Class::ResultDDL::SchemaLoaderMixin - Modify Schema Loader to generate ResultDDL notation

SYNOPSIS

  package MyLoader;
  use parent
    'DBIx::Class::ResultDDL::SchemaLoaderMixin', # mixin first
    'DBIx::Class::Schema::Loader::DBI::mysql';
  
  1;

You can then use it with the loader_class option:

  use DBIx::Class::Schema::Loader qw/ make_schema_at /;
  my %options= ...;
  my @conn_info= (
    'dbi:mysql:my_database',
    $user, $pass,
    { loader_class => 'MyLoader' }
  );
  make_schema_at($package, \%options, \@conn_info);

You can also use this custom loader class to inject some DBIC settings that SchemaLoader doesn't know about:

  package MyLoader;
  use parent
    'DBIx::Class::ResultDDL::SchemaLoaderMixin', # mixin first
    'DBIx::Class::Schema::Loader::DBI::mysql';
  
  sub generate_resultddl_import_line {
    return "use DBIx::Class::ResultDDL qw/ -V2 -inflate_datetime -inflate_json /;\n"
  }
  
  sub generate_column_info_sugar {
    my ($self, $class, $colname, $colinfo)= @_;
    if ($colname eq 'jsoncol' || $colname eq 'textcol') {
      $colinfo->{serializer_class}= 'JSON'
    }
    $self->next::method($class, $colname, $colinfo);
  }
  
  1;

DESCRIPTION

This module overrides behavior of DBIx::Class::Schema::Loader::Base to generate Result files that use DBIx::Class::ResultDDL notation. ::Schema::Loader::Base is the base class for all of the actual loader classes, which are invoked by ::Schema::Loader (but do not share a class hierarchy with ::Schema::Loader itself).

This is essentially a Role, but Schema Loader isn't based on Moo(se) and this ResultDDL distribution does not yet depend on Moo(se), so it just uses plain perl multiple inheritance. Inherit from the mixin first so that its methods take priority. (it does override private methods of schema loader, so without the Role mechanism to verify it, there is a chance parts just stop working if Schema Loader changes its internals. But it's a development-time tool, and you'll see the output change, and the output will still be valid)

METHODS

The following methods are public so that you can override them:

generate_resultddl_import_line

  $perl_stmt= $loader->generate_resultddl_import_line($class);

This should return a string like "use DBIx::Class::ResultDDL qw/ -V2 /;\n". Don't forget the trailing semicolon.

generate_column_info_sugar

  $perl_stmt= $loader->generate_column_info_sugar($class, $col_name, $col_info);

This runs for each column being generated on the result class. It takes the name of the result class, the name of the column, and the hashref of DBIC %col_info that ::Schema::Loader created. It then returns the string of $generated_perl to appear in "col $col_name => $generated_perl;\n".

If you override this, you can use the class and column name to decide if you want to alter the $col_info before SchemaLoaderMixin works its magic. For instance, you might supply datetimes or serializer classes that ::Schema::Loader wouldn't know you wanted.

You could also munge the returned string, or just create a string if your own.

generate_relationship_sugar

  $perl_stmt= $loader->generate_relationship_sugar(
    $class, $rel_type, %rel_name, $foreign_class, $col_map, $attrs
  );

This method takes the typical arguments of one of the relationship-defining methods of DBIC and returns the equivalent sugar-ized form. (namely, it attempts to convert the $foreign_class and $col_map into the simplified version used by ResultDDL, and replace $attrs with sugar functions where available.)

generate_relationship_attr_sugar

  $perl_expr= $loader->generate_relationship_attr_sugar(\%attrs);

This is a piece of "generate_relationship_sugar" that deals only with the replacement of relationship attributes with equivalent sugar functions.

THANKS

Thanks to Clippard Instrument Laboratory Inc. for supporting open source, including portions of this module.

AUTHOR

Michael Conrad <mconrad@intellitree.com>

VERSION

version 2.04

COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Michael Conrad, IntelliTree Solutions llc.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.