MySQL-ORM

 view release on metacpan or  search on metacpan

lib/MySQL/ORM/Generate/Class/Table.pm  view on Meta::CPAN

package MySQL::ORM::Generate::Class::Table;

our $VERSION = '0.01';

use Modern::Perl;
use Moose;
use namespace::autoclean;
use Method::Signatures;
use Data::Printer alias => 'pdump';
use List::MoreUtils qw(uniq);
use MySQL::Util::Lite;
use MySQL::ORM::Generate::Class::ResultClass;
use MySQL::ORM::Generate::Class::ResultClassX;
use MySQL::ORM::Generate::Class::CustomRole;
use SQL::Beautify;
use Text::Trim 'trim';

extends 'MySQL::ORM::Generate::Common';

##############################################################################
# required attributes
##############################################################################

has dir => (
	is       => 'ro',
	isa      => 'Str',
	required => 1,
);

has table => (
	is       => 'ro',
	isa      => 'MySQL::Util::Lite::Table',
	required => 1,
);

has db_class_name => (
	is       => 'ro',
	isa      => 'Str',
	required => 1,
);

##############################################################################
# optional attributes
##############################################################################

has namespace => (
	is      => 'ro',
	isa     => 'Str',
	default => '',
);

##############################################################################
# optional attributes
##############################################################################

##############################################################################
# private attributes
##############################################################################

has _result_class => (
	is      => 'rw',
	isa     => 'MySQL::ORM::Generate::Class::ResultClass',
	lazy    => 1,
	builder => '_build_result_class',
);

lib/MySQL/ORM/Generate/Class/Table.pm  view on Meta::CPAN


##############################################################################
# private methods
##############################################################################

method _get_table_name {

	if ( $self->use_fq_table_names ) {
		return $self->table->get_fq_name;
	}

	return $self->table->name;
}

#method _get_table_name (MySQL::Util::Lite::Table $table) {
#
#	if ( $self->use_fq_table_names ) {
#		return $table->get_fq_name;
#	}
#
#	my $parent_schema = $table->schema_name;
#	my $child_schema  = $self->_get_schema_name;
#
#	if ( $parent_schema ne $child_schema ) {
#
#		# must fully qualify it
#		return $table->get_fq_name;
#	}
#
#	return $table->name;
#}

method _get_parent_table_name (Str :$schema_name!,
						 	   Str :$table_name!) {

	my $parent_fq_name = sprintf '%s.%s', $schema_name, $table_name;

	if ( $self->use_fq_table_names ) {
		return $parent_fq_name;
	}

	my $child_schema_name = $self->_get_schema_name;

	if ( $schema_name ne $child_schema_name ) {

		# must fully qualify it
		return $parent_fq_name;
	}

	return $table_name;
}

method _get_schema_name {

	return $self->table->schema_name;
}

method _get_use_modules {

	my @use = (
		'Modern::Perl',         'Moose',
		'namespace::autoclean', 'Method::Signatures',
		"Data::Printer alias => 'pdump'"
	);

	push @use, $self->_result_class->get_class_name;

	if ( $self->table->has_parents ) {
		push @use, $self->_result_class_x->get_class_name;
	}

	return \@use;
}

method _get_methods {

	my @methods;
	push @methods, $self->_get_method_select;
	push @methods, $self->_get_method_select_one;
	push @methods, $self->_get_method_selectx;
	push @methods, $self->_get_method_selectx_one;
	push @methods, $self->_get_method_get_id;
	push @methods, $self->_get_method_insert;
	push @methods, $self->_get_method_update;
	push @methods, $self->_get_method_upsert;
	push @methods, $self->_get_method_delete;
	push @methods, $self->_get_method_is_pk_autoinc;

	return \@methods;
}

method _get_method_is_pk_autoinc {

	my $bool = 0;

	my $pk = $self->table->get_primary_key;
	if ( $pk and $pk->is_autoinc ) {
		$bool = 1;
	}

	my $body = "return $bool;";

	return $self->method_maker->make_method(
		name => 'is_primary_key_autoinc',
		body => $body
	);
}

method _get_method_delete {

	my $body .= q{
    	my %a = @_;
    	my %w;

    	foreach my $arg (keys %a) {
       		$w{$arg} = $a{$arg}; 
    	}
	    
    	my $ret = $self->SUPER::delete(
    		table  => $self->table_name,
    		where => \%w



( run in 1.264 second using v1.01-cache-2.11-cpan-39bf76dae61 )