.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "DBD::File 3" .TH DBD::File 3 2024-09-13 "perl v5.40.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME DBD::File \- Base class for writing file based DBI drivers .SH SYNOPSIS .IX Header "SYNOPSIS" This module is a base class for writing other DBDs. It is not intended to function as a DBD itself (though it is possible). If you want to access flat files, use DBD::AnyData, or DBD::CSV (both of which are subclasses of DBD::File). .SH DESCRIPTION .IX Header "DESCRIPTION" The DBD::File module is not a true DBI driver, but an abstract base class for deriving concrete DBI drivers from it. The implication is, that these drivers work with plain files, for example CSV files or INI files. The module is based on the SQL::Statement module, a simple SQL engine. .PP See DBI for details on DBI, SQL::Statement for details on SQL::Statement and DBD::CSV, DBD::DBM or DBD::AnyData for example drivers. .SS Metadata .IX Subsection "Metadata" The following attributes are handled by DBI itself and not by DBD::File, thus they all work as expected: .PP .Vb 10 \& Active \& ActiveKids \& CachedKids \& CompatMode (Not used) \& InactiveDestroy \& AutoInactiveDestroy \& Kids \& PrintError \& RaiseError \& Warn (Not used) .Ve .PP \fIThe following DBI attributes are handled by DBD::File:\fR .IX Subsection "The following DBI attributes are handled by DBD::File:" .PP AutoCommit .IX Subsection "AutoCommit" .PP Always on. .PP ChopBlanks .IX Subsection "ChopBlanks" .PP Works. .PP NUM_OF_FIELDS .IX Subsection "NUM_OF_FIELDS" .PP Valid after \f(CW\*(C`$sth\->execute\*(C'\fR. .PP NUM_OF_PARAMS .IX Subsection "NUM_OF_PARAMS" .PP Valid after \f(CW\*(C`$sth\->prepare\*(C'\fR. .PP NAME .IX Subsection "NAME" .PP Valid after \f(CW\*(C`$sth\->execute\*(C'\fR; undef for Non-Select statements. .PP NULLABLE .IX Subsection "NULLABLE" .PP Not really working, always returns an array ref of ones, except the affected table has been created in this session. Valid after \&\f(CW\*(C`$sth\->execute\*(C'\fR; undef for non-select statements. .PP \fIUnsupported DBI attributes and methods\fR .IX Subsection "Unsupported DBI attributes and methods" .IP bind_param_inout 2 .IX Item "bind_param_inout" .PD 0 .IP CursorName 2 .IX Item "CursorName" .IP LongReadLen 2 .IX Item "LongReadLen" .IP LongTruncOk 2 .IX Item "LongTruncOk" .PD .PP \fIDBD::File specific attributes\fR .IX Subsection "DBD::File specific attributes" .PP In addition to the DBI attributes, you can use the following dbh attributes: .PP f_dir .IX Subsection "f_dir" .PP This attribute is used for setting the directory where the files are opened and it defaults to the current directory (\fI.\fR). Usually you set it on the dbh but it may be overridden per table (see f_meta). .PP When the value for \f(CW\*(C`f_dir\*(C'\fR is a relative path, it is converted into the appropriate absolute path name (based on the current working directory) when the dbh attribute is set. .PP .Vb 1 \& f_dir => "/data/foo/csv", .Ve .PP If \f(CW\*(C`f_dir\*(C'\fR is set to a non-existing location, the connection will fail. See CVE\-2014\-10401 for reasoning. Because of this, folders to use cannot be created after the connection, but must exist before the connection is initiated. .PP See "KNOWN BUGS AND LIMITATIONS". .PP f_dir_search .IX Subsection "f_dir_search" .PP This optional attribute can be set to pass a list of folders to also find existing tables. It will \fBnot\fR be used to create new files. .PP .Vb 1 \& f_dir_search => [ "/data/bar/csv", "/dump/blargh/data" ], .Ve .PP f_ext .IX Subsection "f_ext" .PP This attribute is used for setting the file extension. The format is: .PP .Vb 1 \& extension{/flag} .Ve .PP where the /flag is optional and the extension is case-insensitive. \&\f(CW\*(C`f_ext\*(C'\fR allows you to specify an extension which: .PP .Vb 1 \& f_ext => ".csv/r", .Ve .IP \(bu 4 makes DBD::File prefer \fItable.extension\fR over \fItable\fR. .IP \(bu 4 makes the table name the filename minus the extension. .PP .Vb 1 \& DBI:CSV:f_dir=data;f_ext=.csv .Ve .PP In the above example and when \f(CW\*(C`f_dir\*(C'\fR contains both \fItable.csv\fR and \&\fItable\fR, DBD::File will open \fItable.csv\fR and the table will be named "table". If \fItable.csv\fR does not exist but \fItable\fR does that file is opened and the table is also called "table". .PP If \f(CW\*(C`f_ext\*(C'\fR is not specified and \fItable.csv\fR exists it will be opened and the table will be called "table.csv" which is probably not what you want. .PP NOTE: even though extensions are case-insensitive, table names are not. .PP .Vb 1 \& DBI:CSV:f_dir=data;f_ext=.csv/r .Ve .PP The \f(CW\*(C`r\*(C'\fR flag means the file extension is required and any filename that does not match the extension is ignored. .PP Usually you set it on the dbh but it may be overridden per table (see f_meta). .PP f_schema .IX Subsection "f_schema" .PP This will set the schema name and defaults to the owner of the directory in which the table file resides. You can set \f(CW\*(C`f_schema\*(C'\fR to \&\f(CW\*(C`undef\*(C'\fR. .PP .Vb 5 \& my $dbh = DBI\->connect ("dbi:CSV:", "", "", { \& f_schema => undef, \& f_dir => "data", \& f_ext => ".csv/r", \& }) or die $DBI::errstr; .Ve .PP By setting the schema you affect the results from the tables call: .PP .Vb 1 \& my @tables = $dbh\->tables (); \& \& # no f_schema \& "merijn".foo \& "merijn".bar \& \& # f_schema => "dbi" \& "dbi".foo \& "dbi".bar \& \& # f_schema => undef \& foo \& bar .Ve .PP Defining \f(CW\*(C`f_schema\*(C'\fR to the empty string is equal to setting it to \f(CW\*(C`undef\*(C'\fR so the DSN can be \f(CW"dbi:CSV:f_schema=;f_dir=."\fR. .PP f_lock .IX Subsection "f_lock" .PP The \f(CW\*(C`f_lock\*(C'\fR attribute is used to set the locking mode on the opened table files. Note that not all platforms support locking. By default, tables are opened with a shared lock for reading, and with an exclusive lock for writing. The supported modes are: .PP .Vb 1 \& 0: No locking at all. \& \& 1: Shared locks will be used. \& \& 2: Exclusive locks will be used. .Ve .PP But see KNOWN BUGS below. .PP f_lockfile .IX Subsection "f_lockfile" .PP If you wish to use a lockfile extension other than \f(CW\*(C`.lck\*(C'\fR, simply specify the \f(CW\*(C`f_lockfile\*(C'\fR attribute: .PP .Vb 3 \& $dbh = DBI\->connect ("dbi:DBM:f_lockfile=.foo"); \& $dbh\->{f_lockfile} = ".foo"; \& $dbh\->{dbm_tables}{qux}{f_lockfile} = ".foo"; .Ve .PP If you wish to disable locking, set the \f(CW\*(C`f_lockfile\*(C'\fR to \f(CW0\fR. .PP .Vb 3 \& $dbh = DBI\->connect ("dbi:DBM:f_lockfile=0"); \& $dbh\->{f_lockfile} = 0; \& $dbh\->{dbm_tables}{qux}{f_lockfile} = 0; .Ve .PP f_encoding .IX Subsection "f_encoding" .PP With this attribute, you can set the encoding in which the file is opened. This is implemented using \f(CW\*(C`binmode $fh, ":encoding()"\*(C'\fR. .PP f_meta .IX Subsection "f_meta" .PP Private data area aliasing "sql_meta" in DBI::DBD::SqlEngine which contains information about the tables this module handles. Table meta data might not be available until the table has been accessed for the first time e.g., by issuing a select on it however it is possible to pre-initialize attributes for each table you use. .PP DBD::File recognizes the (public) attributes \f(CW\*(C`f_ext\*(C'\fR, \f(CW\*(C`f_dir\*(C'\fR, \&\f(CW\*(C`f_file\*(C'\fR, \f(CW\*(C`f_encoding\*(C'\fR, \f(CW\*(C`f_lock\*(C'\fR, \f(CW\*(C`f_lockfile\*(C'\fR, \f(CW\*(C`f_schema\*(C'\fR, in addition to the attributes "sql_meta" in DBI::DBD::SqlEngine already supports. Be very careful when modifying attributes you do not know, the consequence might be a destroyed or corrupted table. .PP \&\f(CW\*(C`f_file\*(C'\fR is an attribute applicable to table meta data only and you will not find a corresponding attribute in the dbh. Whilst it may be reasonable to have several tables with the same column names, it is not for the same file name. If you need access to the same file using different table names, use \f(CW\*(C`SQL::Statement\*(C'\fR as the SQL engine and the \&\f(CW\*(C`AS\*(C'\fR keyword: .PP .Vb 1 \& SELECT * FROM tbl AS t1, tbl AS t2 WHERE t1.id = t2.id .Ve .PP \&\f(CW\*(C`f_file\*(C'\fR can be an absolute path name or a relative path name but if it is relative, it is interpreted as being relative to the \f(CW\*(C`f_dir\*(C'\fR attribute of the table meta data. When \f(CW\*(C`f_file\*(C'\fR is set DBD::File will use \f(CW\*(C`f_file\*(C'\fR as specified and will not attempt to work out an alternative for \f(CW\*(C`f_file\*(C'\fR using the \f(CW\*(C`table name\*(C'\fR and \f(CW\*(C`f_ext\*(C'\fR attribute. .PP While \f(CW\*(C`f_meta\*(C'\fR is a private and readonly attribute (which means, you cannot modify it's values), derived drivers might provide restricted write access through another attribute. Well known accessors are \&\f(CW\*(C`csv_tables\*(C'\fR for DBD::CSV, \f(CW\*(C`ad_tables\*(C'\fR for DBD::AnyData and \&\f(CW\*(C`dbm_tables\*(C'\fR for DBD::DBM. .PP \fINew opportunities for attributes from DBI::DBD::SqlEngine\fR .IX Subsection "New opportunities for attributes from DBI::DBD::SqlEngine" .PP sql_table_source .IX Subsection "sql_table_source" .PP \&\f(CW\*(C`$dbh\->{sql_table_source}\*(C'\fR can be set to \&\fIDBD::File::TableSource::FileSystem\fR (and is the default setting of DBD::File). This provides usual behaviour of previous DBD::File releases on .PP .Vb 2 \& @ary = DBI\->data_sources ($driver); \& @ary = DBI\->data_sources ($driver, \e%attr); \& \& @ary = $dbh\->data_sources (); \& @ary = $dbh\->data_sources (\e%attr); \& \& @names = $dbh\->tables ($catalog, $schema, $table, $type); \& \& $sth = $dbh\->table_info ($catalog, $schema, $table, $type); \& $sth = $dbh\->table_info ($catalog, $schema, $table, $type, \e%attr); \& \& $dbh\->func ("list_tables"); .Ve .PP sql_data_source .IX Subsection "sql_data_source" .PP \&\f(CW\*(C`$dbh\->{sql_data_source}\*(C'\fR can be set to either \&\fIDBD::File::DataSource::File\fR, which is default and provides the well known behavior of DBD::File releases prior to 0.41, or \&\fIDBD::File::DataSource::Stream\fR, which reuses already opened file-handle for operations. .PP \fIInternally private attributes to deal with SQL backends\fR .IX Subsection "Internally private attributes to deal with SQL backends" .PP Do not modify any of these private attributes unless you understand the implications of doing so. The behavior of DBD::File and derived DBDs might be unpredictable when one or more of those attributes are modified. .PP sql_nano_version .IX Subsection "sql_nano_version" .PP Contains the version of loaded DBI::SQL::Nano. .PP sql_statement_version .IX Subsection "sql_statement_version" .PP Contains the version of loaded SQL::Statement. .PP sql_handler .IX Subsection "sql_handler" .PP Contains either the text 'SQL::Statement' or 'DBI::SQL::Nano'. .PP sql_ram_tables .IX Subsection "sql_ram_tables" .PP Contains optionally temporary tables. .PP sql_flags .IX Subsection "sql_flags" .PP Contains optional flags to instantiate the SQL::Parser parsing engine when SQL::Statement is used as SQL engine. See SQL::Parser for valid flags. .SS "Driver private methods" .IX Subsection "Driver private methods" \fIDefault DBI methods\fR .IX Subsection "Default DBI methods" .PP data_sources .IX Subsection "data_sources" .PP The \f(CW\*(C`data_sources\*(C'\fR method returns a list of subdirectories of the current directory in the form "dbi:CSV:f_dir=$dirname". .PP If you want to read the subdirectories of another directory, use .PP .Vb 2 \& my ($drh) = DBI\->install_driver ("CSV"); \& my (@list) = $drh\->data_sources (f_dir => "/usr/local/csv_data"); .Ve .PP \fIAdditional methods\fR .IX Subsection "Additional methods" .PP The following methods are only available via their documented name when DBD::File is used directly. Because this is only reasonable for testing purposes, the real names must be used instead. Those names can be computed by replacing the \f(CW\*(C`f_\*(C'\fR in the method name with the driver prefix. .PP f_versions .IX Subsection "f_versions" .PP Signature: .PP .Vb 6 \& sub f_versions (;$) \& { \& my ($table_name) = @_; \& $table_name ||= "."; \& ... \& } .Ve .PP Returns the versions of the driver, including the DBI version, the Perl version, DBI::PurePerl version (if DBI::PurePerl is active) and the version of the SQL engine in use. .PP .Vb 9 \& my $dbh = DBI\->connect ("dbi:File:"); \& my $f_versions = $dbh\->func ("f_versions"); \& print "$f_versions\en"; \& _\|_END_\|_ \& # DBD::File 0.41 using IO::File (1.16) \& # DBI::DBD::SqlEngine 0.05 using SQL::Statement 1.406 \& # DBI 1.623 \& # OS darwin (12.2.1) \& # Perl 5.017006 (darwin\-thread\-multi\-ld\-2level) .Ve .PP Called in list context, f_versions will return an array containing each line as single entry. .PP Some drivers might use the optional (table name) argument and modify version information related to the table (e.g. DBD::DBM provides storage backend information for the requested table, when it has a table name). .SH "KNOWN BUGS AND LIMITATIONS" .IX Header "KNOWN BUGS AND LIMITATIONS" .IP \(bu 4 This module uses flock () internally but flock is not available on all platforms. On MacOS and Windows 95 there is no locking at all (perhaps not so important on MacOS and Windows 95, as there is only a single user). .IP \(bu 4 The module stores details about the handled tables in a private area of the driver handle (\f(CW$drh\fR). This data area is not shared between different driver instances, so several \f(CW\*(C`DBI\->connect ()\*(C'\fR calls will cause different table instances and private data areas. .Sp This data area is filled for the first time when a table is accessed, either via an SQL statement or via \f(CW\*(C`table_info\*(C'\fR and is not destroyed until the table is dropped or the driver handle is released. Manual destruction is possible via f_clear_meta. .Sp The following attributes are preserved in the data area and will evaluated instead of driver globals: .RS 4 .IP f_ext 8 .IX Item "f_ext" .PD 0 .IP f_dir 8 .IX Item "f_dir" .IP f_dir_search 8 .IX Item "f_dir_search" .IP f_lock 8 .IX Item "f_lock" .IP f_lockfile 8 .IX Item "f_lockfile" .IP f_encoding 8 .IX Item "f_encoding" .IP f_schema 8 .IX Item "f_schema" .IP col_names 8 .IX Item "col_names" .IP sql_identifier_case 8 .IX Item "sql_identifier_case" .RE .RS 4 .PD .Sp The following attributes are preserved in the data area only and cannot be set globally. .IP f_file 8 .IX Item "f_file" .RE .RS 4 .Sp The following attributes are preserved in the data area only and are computed when initializing the data area: .IP f_fqfn 8 .IX Item "f_fqfn" .PD 0 .IP f_fqbn 8 .IX Item "f_fqbn" .IP f_fqln 8 .IX Item "f_fqln" .IP table_name 8 .IX Item "table_name" .RE .RS 4 .PD .Sp For DBD::CSV tables this means, once opened "foo.csv" as table named "foo", another table named "foo" accessing the file "foo.txt" cannot be opened. Accessing "foo" will always access the file "foo.csv" in memorized \&\f(CW\*(C`f_dir\*(C'\fR, locking \f(CW\*(C`f_lockfile\*(C'\fR via memorized \f(CW\*(C`f_lock\*(C'\fR. .Sp You can use f_clear_meta or the \f(CW\*(C`f_file\*(C'\fR attribute for a specific table to work around this. .RE .IP \(bu 4 When used with SQL::Statement and temporary tables e.g., .Sp .Vb 1 \& CREATE TEMP TABLE ... .Ve .Sp the table data processing bypasses DBD::File::Table. No file system calls will be made and there are no clashes with existing (file based) tables with the same name. Temporary tables are chosen over file tables, but they will not covered by \f(CW\*(C`table_info\*(C'\fR. .SH AUTHOR .IX Header "AUTHOR" This module is currently maintained by .PP H.Merijn Brand < h.m.brand at xs4all.nl > and Jens Rehsack < rehsack at googlemail.com > .PP The original author is Jochen Wiedmann. .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" .Vb 3 \& Copyright (C) 2009\-2020 by H.Merijn Brand & Jens Rehsack \& Copyright (C) 2004\-2009 by Jeff Zucker \& Copyright (C) 1998\-2004 by Jochen Wiedmann .Ve .PP All rights reserved. .PP 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 in the Perl README file. .SH "SEE ALSO" .IX Header "SEE ALSO" DBI, DBD::DBM, DBD::CSV, Text::CSV, Text::CSV_XS, SQL::Statement, and DBI::SQL::Nano