DBD::CSV:如何使用两个 f_ext-options“.csv”生成不同的行为?和“.csv/r”?

发布于 2024-08-31 19:06:08 字数 574 浏览 4 评论 0原文

这是来自 DBD::File 文档:

f_ext

此属性用于设置打开 (CSV) 文件的文件扩展名。有几种可能性。

    DBI:CSV:f_dir=data;f_ext=.csv  

在这种情况下,如果 datadir 中同时存在 table.csv 和 table,则 DBD::File 将仅打开 table.csv。该表仍将命名为 table。如果您的 datadir 包含带有扩展名的文件,并且您没有传递此属性,则您的表将被命名为 table.csv,这可能不是您想要的。扩展名始终不区分大小写。表名不是。

    DBI:CSV:f_dir=data;f_ext=.csv/r

在这种情况下,需要扩展名,并且所有不匹配的文件名都将被忽略。

我不可能使用“.csv/r”和“.csv”这两个选项生成不同的行为。有人可以给我举一个例子,让我可以看到“.csv/r”和“.csv”之间的区别吗?

This is from the DBD::File-documentation:

f_ext

This attribute is used for setting the file extension where (CSV) files are opened. There are several possibilities.

    DBI:CSV:f_dir=data;f_ext=.csv  

In this case, DBD::File will open only table.csv if both table.csv and table exist in the datadir. The table will still be named table. If your datadir has files with extensions, and you do not pass this attribute, your table is named table.csv, which is probably not what you wanted. The extension is always case-insensitive. The table names are not.

    DBI:CSV:f_dir=data;f_ext=.csv/r

In this case the extension is required, and all filenames that do not match are ignored.

It was not possible for me to generate different behavior with the two options ".csv/r" and ".csv". Could someone show me an example, where I can see the difference between ".csv/r" and ".csv"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慕巷 2024-09-07 19:06:08

我似乎也无法让它做任何不同的事情。代码的相关部分是

sub file2table
{
    my ($data, $dir, $file, $file_is_tab, $quoted) = @_;

    $file eq "." || $file eq ".."       and return;

    my ($ext, $req) = ("", 0);
    if ($data->{f_ext}) {
        ($ext, my $opt) = split m/\//, $data->{f_ext};
        if ($ext && $opt) {
            $opt =~ m/r/i and $req = 1;
            }
        }

    (my $tbl = $file) =~ s/$ext$//i;
    $file_is_tab and $file = "$tbl$ext";

    # Fully Qualified File Name
    my $fqfn;
    unless ($quoted) { # table names are case insensitive in SQL
        opendir my $dh, $dir or croak "Can't open '$dir': $!";
        my @f = grep { lc $_ eq lc $file } readdir $dh;
        @f == 1 and $file = $f[0];
        closedir $dh or croak "Can't close '$dir': $!";
        }
    $fqfn = File::Spec->catfile ($dir, $file);

    $file = $fqfn;
    if ($ext) {
        if ($req) {
            # File extension required
            $file =~ s/$ext$//i                 or  return;
            }
        else {
            # File extension optional, skip if file with extension exists
            grep m/$ext$/i, glob "$fqfn.*"      and return;
            $file =~ s/$ext$//i;
            }
        }

    $data->{f_map}{$tbl} = $fqfn;
    return $tbl;
    } # file2table

I can't seem to get it to do anything different either. The relevant section of code is

sub file2table
{
    my ($data, $dir, $file, $file_is_tab, $quoted) = @_;

    $file eq "." || $file eq ".."       and return;

    my ($ext, $req) = ("", 0);
    if ($data->{f_ext}) {
        ($ext, my $opt) = split m/\//, $data->{f_ext};
        if ($ext && $opt) {
            $opt =~ m/r/i and $req = 1;
            }
        }

    (my $tbl = $file) =~ s/$ext$//i;
    $file_is_tab and $file = "$tbl$ext";

    # Fully Qualified File Name
    my $fqfn;
    unless ($quoted) { # table names are case insensitive in SQL
        opendir my $dh, $dir or croak "Can't open '$dir': $!";
        my @f = grep { lc $_ eq lc $file } readdir $dh;
        @f == 1 and $file = $f[0];
        closedir $dh or croak "Can't close '$dir': $!";
        }
    $fqfn = File::Spec->catfile ($dir, $file);

    $file = $fqfn;
    if ($ext) {
        if ($req) {
            # File extension required
            $file =~ s/$ext$//i                 or  return;
            }
        else {
            # File extension optional, skip if file with extension exists
            grep m/$ext$/i, glob "$fqfn.*"      and return;
            $file =~ s/$ext$//i;
            }
        }

    $data->{f_map}{$tbl} = $fqfn;
    return $tbl;
    } # file2table
电影里的梦 2024-09-07 19:06:08

这是否表明了差异?:

sandbox % echo "a,b,c" > foo
sandbox % echo "a,b,c" > foo.csv
sandbox % echo "a,b,c" > bar
sandbox % echo "a,b,c" > baz.csv
sandbox % perl -MDBI -wle'print for DBI->connect("dbi:CSV:f_ext=.csv")->tables'
"merijn".baz
"merijn".bar
"merijn".foo
sandbox % perl -MDBI -wle'print for DBI->connect("dbi:CSV:f_ext=.csv/r")->tables'
"merijn".baz
"merijn".foo
sandbox %

f_ext=.csv 仅使 .csv 成为首选项,但不是要求:在第一种情况下,文件“bar”没有仍使用 .csv 扩展名,但选择“foo.csv”而不是“foo”。对于 f_ext=.csv/r",“bar”将被忽略,因为它没有“.csv”扩展名。

Does this demonstrate the difference?:

sandbox % echo "a,b,c" > foo
sandbox % echo "a,b,c" > foo.csv
sandbox % echo "a,b,c" > bar
sandbox % echo "a,b,c" > baz.csv
sandbox % perl -MDBI -wle'print for DBI->connect("dbi:CSV:f_ext=.csv")->tables'
"merijn".baz
"merijn".bar
"merijn".foo
sandbox % perl -MDBI -wle'print for DBI->connect("dbi:CSV:f_ext=.csv/r")->tables'
"merijn".baz
"merijn".foo
sandbox %

f_ext=.csv only makes the .csv a preference, but nor a requirement: in the first case, the file "bar" with no .csv extension is still used, but "foo.csv" is chosen over "foo". With f_ext=.csv/r", "bar" is ignored, as it has no ".csv" extension.

小梨窩很甜 2024-09-07 19:06:08

现在,在 DBD::File 的 0.39 版本中,这部分看起来像这样:

sub file2table
{
    my ($self, $meta, $file, $file_is_table, $respect_case) = @_;

    $file eq "." || $file eq ".."   and return; # XXX would break a possible DBD::Dir

    my ($ext, $req) = ("", 0);
    if ($meta->{f_ext}) {
    ($ext, my $opt) = split m/\//, $meta->{f_ext};
    if ($ext && $opt) {
        $opt =~ m/r/i and $req = 1;
        }
    }

    # (my $tbl = $file) =~ s/$ext$//i;
    my ($tbl, $dir, $user_spec_file);
    if ($file_is_table and defined $meta->{f_file}) {
    $tbl = $file;
    ($file, $dir, undef) = File::Basename::fileparse ($meta->{f_file});
    $user_spec_file = 1;
    }
    else {
    ($tbl, $dir, undef) = File::Basename::fileparse ($file, $ext);
    $user_spec_file = 0;
    }

    -d File::Spec->catdir ($meta->{f_dir}, $dir) or
    croak (File::Spec->catdir ($meta->{f_dir}, $dir) . ": $!");

    !$respect_case and $meta->{sql_identifier_case} == 1 and # XXX SQL_IC_UPPER
        $tbl = uc $tbl;
    !$respect_case and $meta->{sql_identifier_case} == 2 and # XXX SQL_IC_LOWER
        $tbl = lc $tbl;
    my $searchdir = File::Spec->file_name_is_absolute ($dir)
    ? $dir
    : Cwd::abs_path (File::Spec->catdir ($meta->{f_dir}, $dir));
    $searchdir eq $meta->{f_dir} and
    $dir = "";

    unless ($user_spec_file) {
    $file_is_table and $file = "$tbl$ext";

    # Fully Qualified File Name
    my $cmpsub;
    if ($respect_case) {
        $cmpsub = sub {
        my ($fn, undef, $sfx) = File::Basename::fileparse ($_, qr/\.[^.]*/);
        $fn eq $tbl and
            return (lc $sfx eq lc $ext or !$req && !$sfx);
        return 0;
        }
        }
    else {
        $cmpsub = sub {
        my ($fn, undef, $sfx) = File::Basename::fileparse ($_, qr/\.[^.]*/);
        lc $fn eq lc $tbl and
            return (lc $sfx eq lc $ext or !$req && !$sfx);
        return 0;
        }
        }

    opendir my $dh, $searchdir or croak "Can't open '$searchdir': $!";
    my @f = sort { length $b <=> length $a } grep { &$cmpsub ($_) } readdir $dh;
    @f > 0 && @f <= 2 and $file = $f[0];
    !$respect_case && $meta->{sql_identifier_case} == 4 and # XXX SQL_IC_MIXED
        ($tbl = $file) =~ s/$ext$//i;
    closedir $dh or croak "Can't close '$searchdir': $!";

    #(my $tdir = $dir) =~ s{^\./}{};    # XXX We do not want all tables to start with ./
    #$tdir and $tbl = File::Spec->catfile ($tdir, $tbl);
    $dir and $tbl = File::Spec->catfile ($dir, $tbl);

    my $tmpfn = $file;
    if ($ext) {
        if ($req) {
        # File extension required
    $tmpfn =~ s/$ext$//i            or  return;
        }
#       else {
#       # File extension optional, skip if file with extension exists
#       grep m/$ext$/i, glob "$fqfn.*"  and return;
#       $tmpfn =~ s/$ext$//i;
#       }
        }
    }

    my $fqfn = File::Spec->catfile ($searchdir, $file);
    my $fqbn = File::Spec->catfile ($searchdir, $tbl);

    $meta->{f_fqfn} = $fqfn;
    $meta->{f_fqbn} = $fqbn;
    !defined $meta->{f_lockfile} && $meta->{f_lockfile} and
    $meta->{f_fqln} = $meta->{f_fqbn} . $meta->{f_lockfile};

    $meta->{table_name} = $tbl;

    return $tbl;
    } # file2table

据我所知,两个 f_ext-options 正在按预期工作。

Now in version 0.39 of DBD::File this part looks like this:

sub file2table
{
    my ($self, $meta, $file, $file_is_table, $respect_case) = @_;

    $file eq "." || $file eq ".."   and return; # XXX would break a possible DBD::Dir

    my ($ext, $req) = ("", 0);
    if ($meta->{f_ext}) {
    ($ext, my $opt) = split m/\//, $meta->{f_ext};
    if ($ext && $opt) {
        $opt =~ m/r/i and $req = 1;
        }
    }

    # (my $tbl = $file) =~ s/$ext$//i;
    my ($tbl, $dir, $user_spec_file);
    if ($file_is_table and defined $meta->{f_file}) {
    $tbl = $file;
    ($file, $dir, undef) = File::Basename::fileparse ($meta->{f_file});
    $user_spec_file = 1;
    }
    else {
    ($tbl, $dir, undef) = File::Basename::fileparse ($file, $ext);
    $user_spec_file = 0;
    }

    -d File::Spec->catdir ($meta->{f_dir}, $dir) or
    croak (File::Spec->catdir ($meta->{f_dir}, $dir) . ": $!");

    !$respect_case and $meta->{sql_identifier_case} == 1 and # XXX SQL_IC_UPPER
        $tbl = uc $tbl;
    !$respect_case and $meta->{sql_identifier_case} == 2 and # XXX SQL_IC_LOWER
        $tbl = lc $tbl;
    my $searchdir = File::Spec->file_name_is_absolute ($dir)
    ? $dir
    : Cwd::abs_path (File::Spec->catdir ($meta->{f_dir}, $dir));
    $searchdir eq $meta->{f_dir} and
    $dir = "";

    unless ($user_spec_file) {
    $file_is_table and $file = "$tbl$ext";

    # Fully Qualified File Name
    my $cmpsub;
    if ($respect_case) {
        $cmpsub = sub {
        my ($fn, undef, $sfx) = File::Basename::fileparse ($_, qr/\.[^.]*/);
        $fn eq $tbl and
            return (lc $sfx eq lc $ext or !$req && !$sfx);
        return 0;
        }
        }
    else {
        $cmpsub = sub {
        my ($fn, undef, $sfx) = File::Basename::fileparse ($_, qr/\.[^.]*/);
        lc $fn eq lc $tbl and
            return (lc $sfx eq lc $ext or !$req && !$sfx);
        return 0;
        }
        }

    opendir my $dh, $searchdir or croak "Can't open '$searchdir': $!";
    my @f = sort { length $b <=> length $a } grep { &$cmpsub ($_) } readdir $dh;
    @f > 0 && @f <= 2 and $file = $f[0];
    !$respect_case && $meta->{sql_identifier_case} == 4 and # XXX SQL_IC_MIXED
        ($tbl = $file) =~ s/$ext$//i;
    closedir $dh or croak "Can't close '$searchdir': $!";

    #(my $tdir = $dir) =~ s{^\./}{};    # XXX We do not want all tables to start with ./
    #$tdir and $tbl = File::Spec->catfile ($tdir, $tbl);
    $dir and $tbl = File::Spec->catfile ($dir, $tbl);

    my $tmpfn = $file;
    if ($ext) {
        if ($req) {
        # File extension required
    $tmpfn =~ s/$ext$//i            or  return;
        }
#       else {
#       # File extension optional, skip if file with extension exists
#       grep m/$ext$/i, glob "$fqfn.*"  and return;
#       $tmpfn =~ s/$ext$//i;
#       }
        }
    }

    my $fqfn = File::Spec->catfile ($searchdir, $file);
    my $fqbn = File::Spec->catfile ($searchdir, $tbl);

    $meta->{f_fqfn} = $fqfn;
    $meta->{f_fqbn} = $fqbn;
    !defined $meta->{f_lockfile} && $meta->{f_lockfile} and
    $meta->{f_fqln} = $meta->{f_fqbn} . $meta->{f_lockfile};

    $meta->{table_name} = $tbl;

    return $tbl;
    } # file2table

As far as I can see, the two f_ext-options are working as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文