Created
September 8, 2010 14:56
-
-
Save zigorou/570241 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package MySQL::Partition::Definition; | |
| use strict; | |
| use warnings; | |
| use parent qw(Class::Accessor::Fast); | |
| use Data::Util qw(is_array_ref is_scalar_ref is_integer is_number is_value); | |
| use DBI qw(:sql_types); | |
| our $VERSION = '0.01'; | |
| __PACKAGE__->mk_accessors( | |
| qw/is_parent type columns name values engine comment data_directory index_directory max_rows min_rows tablespace nodegroup sub_partitions/ | |
| ); | |
| sub PARTITION_TYPE_RANGE () { 1; }; | |
| sub PARTITION_TYPE_LIST () { 2; }; | |
| sub PARTITION_TYPE_HASH () { 3; }; | |
| sub PARTITION_TYPE_KEY () { 4; }; | |
| sub PARTITION_VALUE_MAX_VALUE () { \'MAXVALUE' }; | |
| sub new { | |
| my ( $class, %args ) = @_; | |
| $args{is_parent} = 1 unless ( defined $args{is_parent} ); | |
| if ( exists $args{sub_partitions} ) { | |
| my @sub_partitions; | |
| for my $sub_args ( @{$args{sub_partitions}} ) { | |
| $sub_args->{is_parent} = 0; | |
| push( @sub_partitions, $class->new( %$sub_args ) ); | |
| } | |
| $args{sub_partitions} = \@sub_partitions; | |
| } | |
| $class->SUPER::new(\%args); | |
| } | |
| sub as_query { | |
| my $self = shift; | |
| my ( $stmt, @bind ); | |
| $stmt = sprintf('%s %s ', $self->is_parent ? 'PARTITION' : 'SUBPARTITION', $self->name); | |
| if ( $self->is_parent ) { | |
| if ( is_number $self->values ) { | |
| $stmt .= 'VALUES LESS THAN (?) '; | |
| push( @bind, [ $self->values, ( is_integer $self->values ) ? SQL_INTEGER : SQL_FLOAT ] ); | |
| } | |
| elsif ( is_scalar_ref $self->values ) { | |
| my $values = $self->values; | |
| $stmt .= sprintf('VALUES LESS THAN %s ', $$values); | |
| } | |
| elsif ( is_array_ref $self->values ) { | |
| my @values = @{$self->values}; | |
| $stmt .= sprintf('VALUES IN (%s) ', substr('?, ' x @values, 0, -2)); | |
| push( @bind, map { [ $_, SQL_INTEGER ] } @values ); | |
| } | |
| } | |
| if ( defined $self->engine ) { | |
| $stmt .= sprintf('ENGINE = %s ', $self->engine); | |
| } | |
| if ( defined $self->comment ) { | |
| $stmt .= 'COMMENT = ? '; | |
| push(@bind, [ $self->comment, SQL_VARCHAR ]); | |
| } | |
| if ( defined $self->data_directory ) { | |
| $stmt .= 'DATA DIRECTORY = ? '; | |
| push(@bind, [ $self->data_directory, SQL_VARCHAR ]); | |
| } | |
| if ( defined $self->index_directory ) { | |
| $stmt .= 'INDEX DIRECTORY = ? '; | |
| push(@bind, [ $self->index_directory, SQL_VARCHAR ]); | |
| } | |
| if ( defined $self->max_rows ) { | |
| $stmt .= 'MAX_ROWS = ? '; | |
| push(@bind, [ $self->max_rows, SQL_INTEGER ]); | |
| } | |
| if ( defined $self->min_rows ) { | |
| $stmt .= 'MIN_ROWS = ? '; | |
| push(@bind, [ $self->min_rows, SQL_INTEGER ]); | |
| } | |
| if ( defined $self->tablespace ) { | |
| $stmt .= sprintf('TABLESPACE = (%s)', $self->tablespace); | |
| } | |
| if ( defined $self->nodegroup ) { | |
| $stmt .= sprintf('NODEGROUP = %s', $self->nodegroup); | |
| } | |
| if ( defined $self->sub_partitions ) { | |
| for ( @{$self->sub_partitions} ) { | |
| my ( $sub_stmt, @sub_bind ) = $_->as_query; | |
| $stmt .= $sub_stmt; | |
| push(@bind, @sub_bind); | |
| } | |
| } | |
| chop $stmt; | |
| return ( $stmt, @bind ); | |
| } | |
| 1; | |
| __END__ | |
| =head1 NAME | |
| MySQL::Partition::Definition - write short description for MySQL::Partition::Definition | |
| =head1 SYNOPSIS | |
| use MySQL::Partition::Definition; | |
| =head1 DESCRIPTION | |
| =head1 METHODS | |
| =head1 AUTHOR | |
| Toru Yamaguchi E<lt>[email protected]<gt> | |
| =head1 LICENSE | |
| This module is licensed under the same terms as Perl itself. | |
| =head1 SEE ALSO | |
| =cut | |
| # Local Variables: | |
| # mode: perl | |
| # perl-indent-level: 4 | |
| # indent-tabs-mode: nil | |
| # coding: utf-8-unix | |
| # End: | |
| # | |
| # vim: expandtab shiftwidth=4: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment