⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file.pm

📁 Astercon2 开源软交换 2.2.0
💻 PM
📖 第 1 页 / 共 2 页
字号:
    "'$str'";}sub commit ($) {    my($dbh) = shift;    if ($dbh->FETCH('Warn')) {	warn("Commit ineffective while AutoCommit is on", -1);    }    1;}sub rollback ($) {    my($dbh) = shift;    if ($dbh->FETCH('Warn')) {	warn("Rollback ineffective while AutoCommit is on", -1);    }    0;}package DBD::File::st; # ====== STATEMENT ======$DBD::File::st::imp_data_size = 0;sub bind_param ($$$;$) {    my($sth, $pNum, $val, $attr) = @_;    $sth->{f_params}->[$pNum-1] = $val;    1;}sub execute {    my $sth = shift;    my $params;    if (@_) {	$sth->{'f_params'} = ($params = [@_]);    } else {	$params = $sth->{'f_params'};    }    $sth->finish;    my $stmt = $sth->{'f_stmt'};    my $result = eval { $stmt->execute($sth, $params); };    return $sth->set_err(1,$@) if $@;    if ($stmt->{'NUM_OF_FIELDS'}) { # is a SELECT statement	$sth->STORE(Active => 1);	$sth->STORE('NUM_OF_FIELDS', $stmt->{'NUM_OF_FIELDS'})	 if !$sth->FETCH('NUM_OF_FIELDS');    }    return $result;}sub finish {    my $sth = shift;    $sth->SUPER::STORE(Active => 0);    delete $sth->{f_stmt}->{data};    return 1;}sub fetch ($) {    my $sth = shift;    my $data = $sth->{f_stmt}->{data};    if (!$data  ||  ref($data) ne 'ARRAY') {	$sth->set_err(1, "Attempt to fetch row from a Non-SELECT statement");	return undef;    }    my $dav = shift @$data;    if (!$dav) {        $sth->finish;	return undef;    }    if ($sth->FETCH('ChopBlanks')) {	map { $_ =~ s/\s+$// if $_; $_ } @$dav;    }    $sth->_set_fbav($dav);}*fetchrow_arrayref = \&fetch;sub FETCH ($$) {    my ($sth, $attrib) = @_;    return undef if ($attrib eq 'TYPE'); # Workaround for a bug in DBI 0.93    return $sth->FETCH('f_stmt')->{'NAME'} if ($attrib eq 'NAME');    if ($attrib eq 'NULLABLE') {	my($meta) = $sth->FETCH('f_stmt')->{'NAME'}; # Intentional !	if (!$meta) {	    return undef;	}	my($names) = [];	my($col);	foreach $col (@$meta) {	    push(@$names, 1);	}	return $names;    }    if ($attrib eq (lc $attrib)) {	# Private driver attributes are lower cased	return $sth->{$attrib};    }    # else pass up to DBI to handle    return $sth->SUPER::FETCH($attrib);}sub STORE ($$$) {    my ($sth, $attrib, $value) = @_;    if ($attrib eq (lc $attrib)) {	# Private driver attributes are lower cased	$sth->{$attrib} = $value;	return 1;    }    return $sth->SUPER::STORE($attrib, $value);}sub DESTROY ($) {    my $sth = shift;    $sth->finish if $sth->SUPER::FETCH('Active');}sub rows ($) { shift->{'f_stmt'}->{'NUM_OF_ROWS'} };package DBD::File::Statement;# We may have a working flock() built-in but that doesn't mean that locking# will work on NFS (flock() may hang hard)my $locking = eval { flock STDOUT, 0; 1 };# Jochen's old check for flock()## my $locking = $^O ne 'MacOS'  &&#               ($^O ne 'MSWin32' || !Win32::IsWin95())  &&#               $^O ne 'VMS';@DBD::File::Statement::ISA = qw(DBI::SQL::Nano::Statement);my $open_table_re =    $haveFileSpec ?    sprintf('(?:%s|%s|%s)',	    quotemeta(File::Spec->curdir()),	    quotemeta(File::Spec->updir()),	    quotemeta(File::Spec->rootdir()))    : '(?:\.?\.)?\/';sub get_file_name($$$) {    my($self,$data,$table)=@_;    $table =~ s/^\"//; # handle quoted identifiers    $table =~ s/\"$//;    my $file = $table;    if ( $file !~ /^$open_table_re/o     and $file !~ m!^[/\\]!   # root     and $file !~ m!^[a-z]\:! # drive letter    ) {	$file = $haveFileSpec ?	    File::Spec->catfile($data->{Database}->{'f_dir'}, $table)		: $data->{Database}->{'f_dir'} . "/$table";    }    return($table,$file);}sub open_table ($$$$$) {    my($self, $data, $table, $createMode, $lockMode) = @_;    my $file;    ($table,$file) = $self->get_file_name($data,$table);    require IO::File;    my $fh;    my $safe_drop = 1 if $self->{ignore_missing_table};    if ($createMode) {	if (-f $file) {	    die "Cannot create table $table: Already exists";	}	if (!($fh = IO::File->new($file, "a+"))) {	    die "Cannot open $file for writing: $!";	}	if (!$fh->seek(0, 0)) {	    die " Error while seeking back: $!";	}    } else {	if (!($fh = IO::File->new($file, ($lockMode ? "r+" : "r")))) {	    die " Cannot open $file: $!" unless $safe_drop;	}    }    binmode($fh) if $fh;    if ($locking and $fh) {	if ($lockMode) {	    if (!flock($fh, 2)) {		die " Cannot obtain exclusive lock on $file: $!";	    }	} else {	    if (!flock($fh, 1)) {		die "Cannot obtain shared lock on $file: $!";	    }	}    }    my $columns = {};    my $array = [];    my $pos = $fh->tell() if $fh;    my $tbl = {	file => $file,	fh => $fh,	col_nums => $columns,	col_names => $array,	first_row_pos => $pos,    };    my $class = ref($self);    $class =~ s/::Statement/::Table/;    bless($tbl, $class);    $tbl;}package DBD::File::Table;@DBD::File::Table::ISA = qw(DBI::SQL::Nano::Table);sub drop ($) {    my($self) = @_;    # We have to close the file before unlinking it: Some OS'es will    # refuse the unlink otherwise.    $self->{'fh'}->close() if $self->{fh};    unlink($self->{'file'});    return 1;}sub seek ($$$$) {    my($self, $data, $pos, $whence) = @_;    if ($whence == 0  &&  $pos == 0) {	$pos = $self->{'first_row_pos'};    } elsif ($whence != 2  ||  $pos != 0) {	die "Illegal seek position: pos = $pos, whence = $whence";    }    if (!$self->{'fh'}->seek($pos, $whence)) {	die "Error while seeking in " . $self->{'file'} . ": $!";    }}sub truncate ($$) {    my($self, $data) = @_;    if (!$self->{'fh'}->truncate($self->{'fh'}->tell())) {	die "Error while truncating " . $self->{'file'} . ": $!";    }    1;}1;__END__=head1 NAMEDBD::File - Base class for writing DBI drivers=head1 SYNOPSIS This module is a base class for writing other DBDs. It is not intended to function as a DBD itself. If you want to access flatfiles, use DBD::AnyData, or DBD::CSV, (both of which are subclasses of DBD::File).=head1 DESCRIPTIONThe DBD::File module is not a true DBI driver, but an abstractbase class for deriving concrete DBI drivers from it. The implication is,that these drivers work with plain files, for example CSV files orINI files. The module is based on the SQL::Statement module, a simpleSQL engine.See L<DBI> for details on DBI, L<SQL::Statement> for details onSQL::Statement and L<DBD::CSV> or L<DBD::IniFile> for exampledrivers.=head2 MetadataThe following attributes are handled by DBI itself and not by DBD::File,thus they all work like expected:    Active    ActiveKids    CachedKids    CompatMode             (Not used)    InactiveDestroy    Kids    PrintError    RaiseError    Warn                   (Not used)The following DBI attributes are handled by DBD::File:=over 4=item AutoCommitAlways on=item ChopBlanksWorks=item NUM_OF_FIELDSValid after C<$sth->execute>=item NUM_OF_PARAMSValid after C<$sth->prepare>=item NAMEValid after C<$sth->execute>; undef for Non-Select statements.=item NULLABLENot really working, always returns an array ref of one's, as DBD::CSVdoesn't verify input data. Valid after C<$sth->execute>; undef forNon-Select statements.=backThese attributes and methods are not supported:    bind_param_inout    CursorName    LongReadLen    LongTruncOkAdditional to the DBI attributes, you can use the following dbhattribute:=over 4=item f_dirThis attribute is used for setting the directory where CSV files areopened. Usually you set it in the dbh, it defaults to the currentdirectory ("."). However, it is overwritable in the statement handles.=back=head2 Driver private methods=over 4=item data_sourcesThe C<data_sources> method returns a list of subdirectories of the currentdirectory in the form "DBI:CSV:f_dir=$dirname".If you want to read the subdirectories of another directory, use    my($drh) = DBI->install_driver("CSV");    my(@list) = $drh->data_sources('f_dir' => '/usr/local/csv_data' );=item list_tablesThis method returns a list of file names inside $dbh->{'f_dir'}.Example:    my($dbh) = DBI->connect("DBI:CSV:f_dir=/usr/local/csv_data");    my(@list) = $dbh->func('list_tables');Note that the list includes all files contained in the directory, eventhose that have non-valid table names, from the view of SQL.=back=head1 KNOWN BUGS=over 8=item *The module is using flock() internally. However, this function is notavailable on all platforms. Using flock() is disabled on MacOS andWindows 95: There's no locking at all (perhaps not so important onMacOS and Windows 95, as there's a single user anyways).=back=head1 AUTHOR AND COPYRIGHTThis module is currently maintained byJeff Zucker < jzucker @ cpan.org >The original author is Jochen Wiedmann.Copyright (C) 2004 by Jeff ZuckerCopyright (C) 1998 by Jochen WiedmannAll rights reserved.You may freely distribute and/or modify this module under the terms of either the GNU General Public License (GPL) or the Artistic License, as specified inthe Perl README file.=head1 SEE ALSOL<DBI>, L<Text::CSV_XS>, L<SQL::Statement>=cut

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -