Server IP : 103.119.228.120 / Your IP : 3.144.45.187 Web Server : Apache System : Linux v8.techscape8.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64 User : nobody ( 99) PHP Version : 5.6.40 Disable Function : shell_exec,symlink,system,exec,proc_get_status,proc_nice,proc_terminate,define_syslog_variables,syslog,openlog,closelog,escapeshellcmd,passthru,ocinum cols,ini_alter,leak,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dl,dll,myshellexec,proc_open,socket_bind,proc_close,escapeshellarg,parse_ini_filepopen,fpassthru,exec,passthru,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,popen,show_source,proc_nice,proc_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,dl,symlink,shell_exec,system,dl,passthru,escapeshellarg,escapeshellcmd,myshellexec,c99_buff_prepare,c99_sess_put,fpassthru,getdisfunc,fx29exec,fx29exec2,is_windows,disp_freespace,fx29sh_getupdate,fx29_buff_prepare,fx29_sess_put,fx29shexit,fx29fsearch,fx29ftpbrutecheck,fx29sh_tools,fx29sh_about,milw0rm,imagez,sh_name,myshellexec,checkproxyhost,dosyayicek,c99_buff_prepare,c99_sess_put,c99getsource,c99sh_getupdate,c99fsearch,c99shexit,view_perms,posix_getpwuid,posix_getgrgid,posix_kill,parse_perms,parsesort,view_perms_color,set_encoder_input,ls_setcheckboxall,ls_reverse_all,rsg_read,rsg_glob,selfURL,dispsecinfo,unix2DosTime,addFile,system,get_users,view_size,DirFiles,DirFilesWide,DirPrintHTMLHeaders,GetFilesTotal,GetTitles,GetTimeTotal,GetMatchesCount,GetFileMatchesCount,GetResultFiles,fs_copy_dir,fs_copy_obj,fs_move_dir,fs_move_obj,fs_rmdir,SearchText,getmicrotime MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/local/share/perl5/MySQL/Diff/ |
Upload File : |
package MySQL::Diff::Table; =head1 NAME MySQL::Diff::Table - Table Definition Class =head1 SYNOPSIS use MySQL::Diff::Table my $db = MySQL::Diff::Database->new(%options); my $def = $db->def(); my $name = $db->name(); my $field = $db->field(); my $fields = $db->fields(); # %$fields my $primary_key = $db->primary_key(); my $indices = $db->indices(); # %$indices my $options = $db->options(); my $isfield = $db->isa_field($field); my $isprimary = $db->isa_primary($field); my $isindex = $db->isa_index($field); my $isunique = $db->is_unique($field); my $isfulltext = $db->is_fulltext($field); =head1 DESCRIPTION Parses a table definition into component parts. =cut use warnings; use strict; our $VERSION = '0.50'; # ------------------------------------------------------------------------------ # Libraries use Carp qw(:DEFAULT); use MySQL::Diff::Utils qw(debug); # ------------------------------------------------------------------------------ =head1 METHODS =head2 Constructor =over 4 =item new( %options ) Instantiate the objects, providing the command line options for database access and process requirements. =cut sub new { my $class = shift; my %hash = @_; my $self = {}; bless $self, ref $class || $class; $self->{$_} = $hash{$_} for(keys %hash); debug(3,"\nconstructing new MySQL::Diff::Table"); croak "MySQL::Diff::Table::new called without def params" unless $self->{def}; $self->_parse; return $self; } =back =head2 Public Methods Fuller documentation will appear here in time :) =over 4 =item * def Returns the table definition as a string. =item * name Returns the name of the current table. =item * field Returns the current field definition of the given field. =item * fields Returns an array reference to a list of fields. =item * primary_key Returns a hash reference to fields used as primary key fields. =item * indices Returns a hash reference to fields used as index fields. =item * options Returns the additional options added to the table definition. =item * isa_field Returns 1 if given field is used in the current table definition, otherwise returns 0. =item * isa_primary Returns 1 if given field is defined as a primary key, otherwise returns 0. =item * isa_index Returns 1 if given field is used as an index field, otherwise returns 0. =item * is_unique Returns 1 if given field is used as unique index field, otherwise returns 0. =item * is_fulltext Returns 1 if given field is used as fulltext index field, otherwise returns 0. =item * is_auto_inc Returns 1 if given field is defined as an auto increment field, otherwise returns 0. =back =cut sub def { my $self = shift; return $self->{def}; } sub name { my $self = shift; return $self->{name}; } sub field { my $self = shift; return $self->{fields}{$_[0]}; } sub fields { my $self = shift; return $self->{fields}; } sub primary_key { my $self = shift; return $self->{primary_key}; } sub indices { my $self = shift; return $self->{indices}; } sub options { my $self = shift; return $self->{options}; } sub foreign_key { my $self = shift; return $self->{foreign_key}; } sub isa_field { my $self = shift; return $_[0] && $self->{fields}{$_[0]} ? 1 : 0; } sub isa_primary { my $self = shift; return $_[0] && $self->{primary}{$_[0]} ? 1 : 0; } sub isa_index { my $self = shift; return $_[0] && $self->{indices}{$_[0]} ? 1 : 0; } sub is_unique { my $self = shift; return $_[0] && $self->{unique}{$_[0]} ? 1 : 0; } sub is_fulltext { my $self = shift; return $_[0] && $self->{fulltext}{$_[0]} ? 1 : 0; } sub is_auto_inc { my $self = shift; return $_[0] && $self->{auto_inc}{$_[0]} ? 1 : 0; } # ------------------------------------------------------------------------------ # Private Methods sub _parse { my $self = shift; $self->{def} =~ s/`([^`]+)`/$1/gs; # later versions quote names $self->{def} =~ s/\n+/\n/; $self->{lines} = [ grep ! /^\s*$/, split /(?=^)/m, $self->{def} ]; my @lines = @{$self->{lines}}; debug(4,"parsing table def '$self->{def}'"); my $name; if ($lines[0] =~ /^\s*create\s+table\s+(\S+)\s+\(\s*$/i) { $self->{name} = $1; debug(3,"got table name '$self->{name}'"); shift @lines; } else { croak "couldn't figure out table name"; } while (@lines) { $_ = shift @lines; s/^\s*(.*?),?\s*$/$1/; # trim whitespace and trailing commas debug(4,"line: [$_]"); if (/^PRIMARY\s+KEY\s+(.+)$/) { my $primary = $1; croak "two primary keys in table '$self->{name}': '$primary', '$self->{primary_key}'\n" if $self->{primary_key}; debug(4,"got primary key $primary"); $self->{primary_key} = $primary; $primary =~ s/\((.*?)\)/$1/; $self->{primary}{$_} = 1 for(split(/,/, $primary)); next; } if (/^(?:CONSTRAINT\s+(.*)?)?\s+FOREIGN\s+KEY\s+(.*)$/) { my ($key, $val) = ($1, $2); croak "foreign key '$key' duplicated in table '$name'\n" if $self->{foreign_key}{$key}; debug(1,"got foreign key $key"); $self->{foreign_key}{$key} = $val; next; } if (/^(KEY|UNIQUE(?: KEY)?)\s+(\S+?)(?:\s+USING\s+(?:BTREE|HASH|RTREE))?\s*\((.*)\)(?:\s+USING\s+(?:BTREE|HASH|RTREE))?$/) { my ($type, $key, $val) = ($1, $2, $3); croak "index '$key' duplicated in table '$self->{name}'\n" if $self->{indices}{$key}; $self->{indices}{$key} = $val; $self->{unique}{$key} = 1 if($type =~ /unique/i); debug(4, "got ", defined $self->{unique}{$key} ? 'unique ' : '', "index key '$key': ($val)"); next; } if (/^(FULLTEXT(?:\s+KEY|INDEX)?)\s+(\S+?)\s*\((.*)\)$/) { my ($type, $key, $val) = ($1, $2, $3); croak "FULLTEXT index '$key' duplicated in table '$self->{name}'\n" if $self->{fulltext}{$key}; $self->{indices}{$key} = $val; $self->{fulltext}{$key} = 1; debug(4,"got FULLTEXT index '$key': ($val)"); next; } if (/^\)\s*(.*?);$/) { # end of table definition $self->{options} = $1; debug(4,"got table options '$self->{options}'"); last; } if (/^(\S+)\s*(.*)/) { my ($field, $fdef) = ($1, $2); croak "definition for field '$field' duplicated in table '$self->{name}'\n" if $self->{fields}{$field}; $self->{fields}{$field} = $fdef; debug(4,"got field def '$field': $fdef"); next unless $fdef =~ /\s+AUTO_INCREMENT\b/; $self->{auto_inc}{$field} = 1; debug(4,"got AUTO_INCREMENT field '$field'"); next; } croak "unparsable line in definition for table '$self->{name}':\n$_"; } warn "table '$self->{name}' didn't have terminator\n" unless defined $self->{options}; @lines = grep ! m{^/\*!40\d{3} .*? \*/;}, @lines; @lines = grep ! m{^(SET |DROP TABLE)}, @lines; warn "table '$self->{name}' had trailing garbage:\n", join '', @lines if @lines; } 1; __END__ =head1 COPYRIGHT AND LICENSE Copyright (c) 2000-2016 Adam Spiers. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO L<mysqldiff>, L<MySQL::Diff>, L<MySQL::Diff::Database>, L<MySQL::Diff::Utils> =head1 AUTHOR Adam Spiers <mysqldiff@adamspiers.org> =cut