1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
#########################################################################
package AnyData::Storage::RAM;
#########################################################################
#
# This module is copyright (c), 2000 by Jeff Zucker
# All rights reserved.
#
#########################################################################
use strict;
use warnings;
use vars qw($VERSION $DEBUG);
$VERSION = '0.12';
$DEBUG = 1;
use Data::Dumper;
use AnyData::Storage::File;
sub new {
my $class = shift;
my $self = shift || {};
return bless $self, $class;
}
########
# MOVE set_col_nums and open_table to Storage/Base.pm
#
# ALSO make DBD::AnyData::Statement and DBD::Table simple @ISA for AnyData
sub set_col_nums {
my $self = shift;
my $col_names = $self->{col_names};
return {} unless $col_names ;
return {} unless ref $col_names eq 'ARRAY';
return {} unless scalar @$col_names;
my $col_nums={}; my $i=0;
for (@$col_names) { next unless $_; $col_nums->{$_} = $i; $i++; }
#use Data::Dumper; die Dumper $col_names;
$self->{col_nums}=$col_nums;
return $col_nums;
}
sub open_table {
my( $self, $parser, $file, $read_mode, $data ) = @_;
$data = $self->{recs} if $self->{recs};
#$data ||= $parser->{recs};
#$data = $file if ref $file eq 'ARRAY' and !$data;
#use Data::Dumper; print Dumper $data;
#print ref $parser;
my $rec_sep = $parser->{record_sep};# || "\n";
my $table_ary = [];
my $col_names = $parser->{col_names} || $self->{col_names};
my $cols_supplied = $col_names;
my $url = $file if $file =~ m"^http://|^ftp://";
$self->{open_mode} = $read_mode || 'r';
my $data_type;
$data_type='ARY-ARY' if ref $data eq 'ARRAY' and ref $data->[0] eq 'ARRAY';
$data_type='ARY-HSH' if ref $data eq 'ARRAY' and ref $data->[0] eq 'HASH';
$data_type='ARY-STR' if ref $data eq 'ARRAY' and !$data_type;
$data_type ||= 'STR';
# print "[$data_type]" . ref $data if $data;
# MP3 and ARRAY
if ( $self->{records} && !$data ) {
$table_ary = $self->{records};
$col_names ||= shift @$table_ary;
}
# REMOTE
elsif ( $data ) {
if ($parser->{slurp_mode}) {
($table_ary,$col_names) = $parser->import($data,$self);
shift @$table_ary if (ref $parser) =~ /HTMLtable/ && $url && $cols_supplied;
}
else {
if ($data_type eq 'ARY-STR') {
$data = join '', @$data;
}
if ($data_type eq 'ARY-ARY') {
$table_ary = $data;
}
elsif ($data_type eq 'ARY-HSH') {
print "IMPORT OF HASHES NOT YET IMPLEMENTED!\n"; exit;
}
else {
$data =~ s/\015$//gsm; # ^M = CR from DOS
#use Data::Dumper; print Dumper $data;
my @tmp = split /$rec_sep/, $data;
#use Data::Dumper; print ref $parser, Dumper \@tmp;
if ((ref $parser) =~ /Fixed/ && (!$col_names or !scalar @$col_names)) {
my $colstr = shift @tmp;
# $colstr =~ s/\015$//g; # ^M = CR from DOS
@$col_names = split ',',$colstr;
}
if ((ref $parser) =~ /Paragraph/) {
my $colstr = shift @tmp;
@$col_names = $parser->read_fields($colstr);
#print "@$col_names";
}
for my $line( @tmp ) {
# for (split /$rec_sep/, $data) {
# s/\015$//g; # ^M = CR from DOS
next if $parser->{skip_pattern} and $line =~ $parser->{skip_pattern};
my @row = $parser->read_fields($line);
#print $_;
#use Data::Dumper; print Dumper \@row;
###z MOD
# next unless scalar @row;
# push @$table_ary, \@row;
push @$table_ary, \@row
# unless $parser->{skip_mark}
# and $row[0] eq $parser->{skip_mark};
#
}
}
if ((ref $parser) !~ /Fixed|Paragraph/
&& !$parser->{keep_first_line}
&& !$parser->{col_names}
) {
$col_names = shift @$table_ary;
}
#use Data::Dumper; die Dumper $table_ary;
}
}
# if ($file and !(ref $file eq 'ARRAY') and $file !~ m'^http://|ftp://' and !(scalar @$table_ary) ) {
if ((ref $parser) !~ /XML/ ) {
my $size = scalar @$table_ary if defined $table_ary;
if ($file and !(ref $file eq 'ARRAY') and !$size ) {
if ($file =~ m'^http://|ftp://') {
# ($table_ary,$col_names) =
# $self->get_remote_data($file,$parser);
}
else {
($table_ary,$col_names) =
$self->get_local_data($file,$parser,$read_mode);
}
}
}
my @array = @$col_names if ref $col_names eq 'ARRAY';
#print "@array" if @array;
if ($col_names && scalar @array == 0 ) {
@array = (ref $parser =~ /Fixed/)
? split ',', $col_names
: $parser->read_fields($col_names);
}
my $col_nums;
$col_nums = $self->set_col_nums() if $col_names;
my %table = (
index => 0,
file => $file,
records => $table_ary,
col_nums => $col_nums,
col_names => \@array,
);
for my $key(keys %table) {
$self->{$key}=$table{$key};
}
#use Data::Dumper; print Dumper $self; exit;
#use Data::Dumper; print Dumper $table_ary;
#use Data::Dumper; print Dumper $self->{records} if (ref $parser) =~ /Weblog/;
}
sub close { my $s = shift; undef $s }
sub get_remote_data {
my $self = shift;
my $file = shift;
my $parser = shift;
$ENV = {} unless defined $ENV;
$^W = 0;
undef $@;
my $user = $self->{user} || $self->{username};
my $pass = $self->{pass} || $self->{password};
eval{ require 'LWP/UserAgent.pm'; };
# eval{ require 'File/DosGlob.pm'; };
die "LWP module not found! $@" if $@;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(GET => $file);
$req->authorization_basic($user, $pass) if $user and $pass;
my $res = $ua->request($req);
die "[$file] : " . $res->message if !$res->is_success;
$^W = 1;
return $res->content;
# return $parser->get_data($res->content,$self->{col_names});
}
sub export {
my $self = shift;
my $parser = shift;
print "##";
return unless $parser->{export_on_close} && $self->{open_mode} ne 'r';
# return $parser->export( $self->{records}, $self->{col_names}, $self->{deleted} );
#$self->{file_manager}->str2file($str);
}
sub DESTROY {
#shift->export;
#print "DESTROY";
}
sub get_local_data {
my $self = shift;
my $file = shift;
my $parser = shift;
my $open_mode = shift || 'r';
my $adf = AnyData::Storage::File->new;
# $adf->open_table($parser,$file,'r');
my $fh = $adf->open_local_file($file,$open_mode);
#print Dumper $file,$adf; exit;
$self->{file_manager} = $adf;
$self->{fh} = $fh;
#use Data::Dumper; print Dumper $self;
# my $fh = $adf->{fh};
return([],$self->{col_names}) if 'co' =~ /$open_mode/;
# if ((ref $parser) =~ /HTML/) {
# print "[[$file]]";
# for (<$fh>) { print; }
# }
local $/ = undef;
my $str = <$fh>;
# $fh->close;
#print $str if (ref $parser) =~ /HTML/;
return $self->{col_names} unless $str;
return $parser->get_data($str,$self->{col_names});
}
sub dump {
my $self = shift;
print
"\nTotal Rows = ", scalar @{ $self->{records} },
"\nCurrent Row = ", $self->{index},
"\nData = ", Dumper $self->{records},
;
}
sub col_names { shift->{col_names} }
sub get_col_names {
my $self=shift;
my $parser=shift;
my $c = $self->{col_names} || $parser->{col_names};
#print "###@$c";
#return $c;
# if (!scalar @$c and $self->{data}) {
# $c = shift @{$self->{data}};
# }
# return $c;
}
sub get_file_handle {''}
sub get_file_name {''}
sub seek_first_record { shift->{index}=0 }
sub get_pos { my $s=shift; $s->{CUR}= $s->{index}}
sub go_pos {my $s=shift;$s->{index}=$s->{CUR}}
sub is_deleted { my $s=shift; return $s->{deleted}->{$s->{index}-1} };
sub delete_record {
my $self = shift;
# $self->{records}->[ $self->{index}-1 ]->[-1] = $self->{del_marker};
$self->{deleted}->{ $self->{index}-1 }++;
}
##################################
# fetch_row()
##################################
sub get_record {
my($self,$parser) = @_;
my $currentRow = $self->{index};
return undef unless $self->{records} ;
return undef if $currentRow >= @{ $self->{records} };
$self->{index} = $currentRow+1;
$self->get_pos($self->{index});
#print @{ $self->{records}->[ $currentRow ] };
return $self->{records}->[ $currentRow ];
}
*file2str = \&get_record;
*write_fields = \&push_row;
####################################
# push_row()
####################################
sub push_row {
my($self, $fields, $parser) = @_;
if (! ref $fields) {
$fields =~ s/\012$//;
#chomp $fields;
my @rec = $parser->read_fields($fields);
$fields = \@rec;
}
#use Data::Dumper; print Dumper $fields;
my $currentRow = $self->{index};
$self->{index} = $currentRow+1;
$self->{records}->[$currentRow] = $fields;
return 1;
}
##################################
# truncate()
##################################
sub truncate {
my $self = shift;
return splice @{$self->{records}}, $self->{index},1;
}
#####################################
# push_names()
#####################################
sub print_col_names {
my($self, $parser, $names) = @_;
$self->{col_names} = $names;
$self->{parser}->{col_names} = $names;
my($col_nums) = {};
for (my $i = 0; $i < @$names; $i++) {
$col_nums->{$names->[$i]} = $i;
}
$self->{col_nums} = $col_nums;
}
sub drop {1;}
sub close_table {1;}
sub seek {
my($self, $pos, $whence) = @_;
return unless defined $self->{records};
my($currentRow) = $self->{index};
if ($whence == 0) {
$currentRow = $pos;
} elsif ($whence == 1) {
$currentRow += $pos;
} elsif ($whence == 2) {
$currentRow = @{$self->{records}} + $pos;
} else {
die $self . "->seek: Illegal whence argument ($whence)";
}
if ($currentRow < 0) {
die "Illegal row number: $currentRow";
}
$self->{index} = $currentRow;
}
############################################################################
1;
__END__
sub str2file {
my($self,$rec)=@_;
my @c = caller 3;
if ($c[3] =~ /DELETE/ or $c[3] =~ /UPDATE/) {
$self->delete_record($rec);
return undef if $c[3] =~ /DELETE/;
}
push @{ $self->{table} }, $rec;
# $self->{index}++;
return $rec;
}
sub delete_record{my $self=shift;use Data::Dumper; print Dumper @_}
sub close {1;}
sub seek {
my($self,$pos,$whence) = @_;
if ($pos == 0 && $whence == 0) {
$self->{index}=0;
return $self->{index};
}
if ($pos == 0 && $whence == 2) {
return $self->{index};
}
}
sub truncate {}#use Data::Dumper; print Dumper \@_;}
1;
__END__
|