DBD-CSV-0.49/0000755000031300001440000000000012715051740013167 5ustar00merijnusers00000000000000DBD-CSV-0.49/examples/0000755000031300001440000000000012715051740015005 5ustar00merijnusers00000000000000DBD-CSV-0.49/examples/passwd.pl0000644000031300001440000000112411175127333016642 0ustar00merijnusers00000000000000#!/usr/bin/perl use strict; use warnings; use DBI; my $dbh = DBI->connect ("DBI:CSV:"); $dbh->{csv_tables}{passwd} = { sep_char => ":", quote_char => undef, escape_char => undef, file => "/etc/passwd", col_names => [qw( login password uid gid realname directory shell )], }; my $sth = $dbh->prepare ("SELECT * FROM passwd"); $sth->execute; my %fld; my @fld = @{$sth->{NAME_lc}}; $sth->bind_columns (\@fld{@fld}); while ($sth->fetch) { printf "%-14s %5d %5d %-25.25s %-14.14s %s\n", @fld{qw( login uid gid realname shell directory )}; } DBD-CSV-0.49/MANIFEST.SKIP0000755000031300001440000000036312525374555015105 0ustar00merijnusers00000000000000\.aspell\.local.pws \.dbi-git \bCVS\b ~$ \.tgz$ \.tar\.gz$ \.git \.travis.yml blib/ cover_db/ lib/DBI genMETA.pl Makefile MANIFEST.SKIP pm_to_blib sandbox/ tmp/ .releaserc ^xx META.yml valgrind.log tests.skip xt/ t/basic/ t/DBI/ t/DBD/ t/SQL/ DBD-CSV-0.49/ChangeLog0000755000031300001440000003240212715046223014745 0ustar00merijnusers000000000000000.49 - 2016-05-12, H.Merijn Brand * Simplified test-table-name generation * Prefer quote_empty over quote_always for size (Text::CSV_XS => 1.18) * Add CONTRIBUTING.md * It's 2016 * Added docs to warn for reserved words (RT#106529) * Minor spelling corrections (PRC Guillermo O. Freschi) * Test with perl 5.24.0, DBI 1.636, SQL::Statement-1.410, and Text::CSV_XS-1.23 0.48 - 2015-02-11, H.Merijn Brand * Update list of valid Text::CSV_XS attributes 0.47 - 2015-02-11, H.Merijn Brand * Updated copyright to 2015 * Make test-tablename unique for parallel testing * Remove perl recommendation from META as it breaks cpan clients 0.46 - 2014-11-08, H.Merijn Brand * Column type CHAR should return 1 0.45 - 2014-10-23, H.Merijn Brand * Test unauthorized folder access (RT#99508). Requires DBD::File-0.43 * Fix DBI requirement phase (depend more on toolchain) (RT#99732) 0.44 - 2014-08-04, H.Merijn Brand * Table names case sensitiveness not tested case-problematic FS's * Fix defaults in doc (annocpan) * Fix typo in SYNOPSIS (RT#97313) * Workaround eof bug in Text::CSV_XS-1.10 0.43 - 2014-06-30, H.Merijn Brand * Updated copyright to 2014 * Unquote schema's in test for cygwin * Extra guards in Makefile.PL for unmet requirements 0.42 - 2013-08-14, H.Merijn Brand * Optionally skip tests using File::Spec->tempdir () RT#87684 * And document the use of $TMPDIR in README * Make the SYNOPSIS more reflect real-world usage * Detect DBI::Test and use it if available 0.41 - 2013-07-29, H.Merijn Brand * Use File::Spec->tmpdir () for universal existing folder Note that huge $TMP folders may cause the test to run slow * Use File::Spec::rel2abs () instead of abs_path and hoops * Guard against parallel testing, which is unsupported * Guard against streaming tests (they PASS on the DBI-devel) 0.40 - 2013-07-23, H.Merijn Brand * Fix version references in doc * Fix tests for Windows 0.39 - 2013-06-28, H.Merijn Brand * Use f_file in docs. file is deprecated * DBD::CSV's repository moved to github * Add test for row-completeness * Fix UTF-8 tests to better follow Text::CSV_XS auto-encoding * Require more recent versions of low level modules that fixe issues reported for DBD::CSV * Development will also do mosts tests with DBI_SQL_NANO=1 * Added support for f_dir_search from DBD::File (in fact DBI was changed to feature this for DBD::CSV :) * Fix test relying on spell-error in DBI's error message * Document csv_class (still undertested) 0.38 - 2013-01-09, H.Merijn Brand * the tar paxheaders hit again (http://www.perlmonks.org/?node_id=1004571) 0.37 - 2013-01-09, H.Merijn Brand * Fixed RT#80078, resulting in getline calls on undef (Benjamin Booth) * Require latest DBI and SQL::Statement * Updated copyright to 2013 * Fixes for DBI-1.623 (Jens Rehsack) 0.36 - 2012-08-22, H.Merijn Brand * Add line/record number and position in error messages 0.35 - 2012-05-24, H.Merijn Brand * Improved documentation (including mje's contribution) * Tested under 5.16.0 (installed) 0.34 - 2012-05-12, H.Merijn Brand * Updated copyright to 2012 * Require 5.8.1, as DBI does * Tested against perl-5.16.0-RC0 + DBI-1.620 0.33 - 2011-09-07, H.Merijn Brand * NAME / DISTNAME in Makefile.PL 0.32 - 2011-09-07, H.Merijn Brand * TYPE should be numeric * Added tests for return count of delete statements in do * Upped copyright to 2011 * Added tests for return count of update statements (Peter Rabbitson) * Try to catch (more) usernames on Windows as schema names * More cross-checks for META data 0.31 - 2010-09-18, H.Merijn Brand * Require 5.8.1 (effectively already doing so by requiring DBI-1.614) * Update tests to use warnings and done_testing () requires Test::More-0.90, which is also required for DBI * Better diagnostics for empty files * Allow late setting of attributes (RT#61168) - requires DBI-1.614 0.30 - 2010-07-09, H.Merijn Brand * Text::CSV_XS now called with auto_diag * Implement valid_attrs * Next version will require DBI-1.612 or higher This is a transition release 0.29_03 - 2010-07-04, H.Merijn Brand * Remove leftover debug. Causes fails 0.29_02 - 2010-07-02, H.Merijn Brand * More work to keep in sync with DBI development (Jens) * Better diagnostics for missing modules * More spell-check fixes * Tests for ChopBlanks (now fixed in DBD::File) * Tests changed to accept old and new DBI * Statement handle attributes handled in new DBD::File 0.29_01 - 2010-06-11, H.Merijn Brand * Prepare for DBD::File-0.39 (DBI-1.612+) Should still work with older DBI versions * Documentation updates (style, consistency, spell-checking) * Test folder cleanup * More tests for insert * Tested RT#58039 0.29 - 2010-05-03, H.Merijn Brand * Fix git URL in META.yml * Add empty CLONE method to prevent warning when cloning threads * Minimum perl 5.8.1 * Minimum Text::CSV_XS 0.71 * Support for f_encoding (requires DBI-1.611) * Documentation updates 0.28 - 2010-03-16, H.Merijn Brand * Remove t/00_minimumversion.t from distribution * Dropped YAML spec to 1.0 * Sync up with SQL::Statement 1.25 NOTE: Internals of both modules changed. They will change again for SQL::Statement 2.0 0.27 - 2010-02-17, H.Merijn Brand * Upped copyright to 2010 * Doc-fix typo in link (Detlef Pilzecker) * Doc-fix small typo's 0.26 - 2009-11-10, H.Merijn Brand * Adjustments for windows (RT#50544) * Mark all non-\w chars illegal in field and table names * Fix field types after execute (RT#51090, vgdoqd) * Fix for NULL joins. Requires SQL::Statement 1.23 (RT#43010, JBAZIK) 0.25 - 2009-10-15, H.Merijn Brand * Raised minimum perl version to 5.005_03 * Requires Text::CSV_XS 0.43 (needs eof () method and error_diag ()) (RT#33764) * Code cleanup (perlcritic, style, layout) * Implemented f_ext * Implemented f_schema (in DBD::File) * Split csv handles for input (no eol default) and output (default "\r\n") (RT#33767) * Updated info URL's * Cleaned up tests * Tests now use Test::More * Cut down README. No need to be a duplicate module pod * Added TODO to documentation * Added examples/ from docs * Add csv_null * Add csv_* to pass to the parser * Improved error handling and reporting * Table names case sensitiveness * Implemented raw_header (RT#44583) 0.23 - 2005-08-10, Jeff Zucker * fixed bug in automatic column generation, defining col_names => [] will auto generate col1, col2, etc. thanks for bug report, harleypig on AnnoCPAN * added support for multi-character end-of-line separators thanks for suggestion, harleypig on AnnoCPAN 0.22 - 2005-04-01, Jeff Zucker * removed DESTROY and disconnect - use DBD File's instead * added ignore_missing_table flag to avoid warnings with DROP TABLE IF EXISTS 0.21 - 2004-04-18, Jeff Zucker * added support for $dbh->{Active}, should now work well with Class::DBI and other caching systems * added support for DROP TABLE IF EXISTS - doesn't complain if table is already dropped * removed DBD::File from the distribution - it's now distributed with DBI 1.42 and higher 0.2002 - 2002-03-01, Jeff Zucker * added parser-caching for speedier operation, see File.pm prepare() * further adjustments in tests including fix for prototypes to make it perl 5.8 compatible 0.2001 - 2002-01-21, Jeff Zucker * adjusted docs to show Jeff as the new maintainer. * added docs for extended SQL features (joins, etc.) available with SQL::Statement 1.0 and above. * adjusted tests blobs.t, chopblanks.t, and ak-dbd.t to work with all versions of SQL::Statement. 0.1030 - 2002-12-20, Jochen Wiedmann * lib/DBD/File.pm: Locking is now suppressed under VMS. David Webb * lib/DBD/CSV.pm: Added a hint to the docs, that "f_dir=" is required for the current directory on VMS and not "f_dir=.". David Webb 0.1029 - 2001-11-28, Jochen Wiedmann * lib/DBD/CSV.pm (fetch_row): A numeric value must be assigned to $!. 0.1028 - 2001-11-20, Jochen Wiedmann * lib/DBD/CSV.pm (fetch_row): Changed "undef $!" to "$! = ''". David Arnold 0.1026 - 2001-05-04, Jochen Wiedmann * lib/DBD/File.pm (table_info): Added closedir(). Alex Hornby 0.1025 - 2000-12-22, Jochen Wiedmann * lib/DBD/CSV.pm (fetch_row): Now cleaning $! before calling $csv->getline(). Seems to return false errors otherwise in some cases. Jeremy Wadsack * Made Makefile.PL CPAN conformant. * Removed unnecessary stuff in Makefile.PL which verified the DBI installation. 0.1023 - 2000-07-31, Jochen Wiedmann * lib/DBD/File.pm (STORE): Changed croak to die. Guido Flohr 0.1023 - 2000-07-31, Jochen Wiedmann * Minor fix in the docs, thanks to Andreas Grupp, grupp@elektronikschule.de. * Minor compatibility fixes in the test suite for Perl 5.6. 0.1022 - 1999-10-07, Jochen Wiedmann * lib/DBD/File.pm (connect): In the DSN, \ was removed in all cases. It is now possible to use \\ for a DSN containing a backslash, for example f_dir=C:\\tmp. Suggested by Giuliano Cioffi . * lib/DBD/CSV.pm: Lots os minor POD patches, thanks to Adam Di Carlo . * lib/DBD/CSV.pm: More POD patches, thanks to Timothy F Armbruster . 0.1021 - 1999-05-10, Jochen Wiedmann * lib/DBD/CSV.pm (push_row): Fixed typo in docs where DBI:File: was used instead of DBI:CSV: (Mitra ) * lib/DBD/CSV.pm (fetch_row): An undef result from $csv->getline() was sometimes handled incorrectly. Thanks to Syed Muhammad Nayeem . * lib/DBD/File.pm: Removed use of flock under Win95. 0.1020 - 1999-03-17, Jochen Wiedmann * lib/DBD/File.pm (FETCH): Simplified handling of $sth->{'NAME'} due to a change in SQL::Statement 0.1011. (Teun Burgers, Teun Burgers ) 0.1019 - 1999-02-11, Jochen Wiedmann * Makefile.PL (CheckModule): Now requires Text::CSV_XS 0.16, due to an imcompatible change. 0.1018 - 1998-12-30, Jochen Wiedmann * t/lib.pl: Fixed a bug in the test suite. (File::Spec wasn't loaded) 0.1017 - 1998-10-26, Jochen Wiedmann * lib/DBD/CSV.pm: Fixed some examples in the docs. (Brian Millett, bpm@ec-group.com) * Now using File::Spec, if available, for Mac portability. (Chris Nandor, pudge@pobox.com) 0.1016 - 1998-10-20, Jochen Wiedmann * lib/Bundle/DBD/CSV.pm: Added lib/Bundle/DBD/CSV.pm * lib/DBD/File.pm: Added $dbh->table_info(). Added $dbh->type_info_all(). Added $dbh->quote($str, $type). 0.1015 - 1998-09-17, Jochen Wiedmann * lib/DBD/CSV.pm: Fixed bug in the docs, the "undef" argument was missing in conjunction with parameters, Honza Pazdziora . * lib/DBD/CSV.pm: Added csv_eol, csv_sep_char, csv_quote_char and csv_escape_char to DSN. 0.1014 - 1998-08-31, Jochen Wiedmann * Makefile.PL: Added AUTHOR, ABSTRACT_FROM and PREREQ_PM. * Makefile.PL: Fixed error handling by using DBI::set_err. 0.1013 - 1998-08-17, Jochen Wiedmann * lib/DBD/File.pm (drop): $self->{'fh'} is now closed before unlinking it. This makes DROP TABLE work under Win32 and other Non-Unixes. 0.1012 - 1998-08-17, Jochen Wiedmann * lib/DBD/File.pm (open_table): Added binmode. * Added Gerald Richter's modifications for DBD::ConfFile to the test suite. * DBD-File/File.pm: Removed DBD::File::SetError and SetWarning. Replaced $sth->func('get_fbav') with $sth->get_fbav() (Requires DBI 0.94). DBD::File is now a Perl-Only driver. * DBD-File/File.pm (fetch): Fixed a DBD::CSV::SetError to DBD::File::SetError; thanks to Gerald Richter (richter@ecos.de) * DBD-File/File.pm: Fixed some $sth->{attr}'s to $sth->FETCH|STORE, my thanks to Gerald Richter. 0.1011 - 1998-06-25, Jochen Wiedmann * DBD-File/File.pm (execute): Added missing eval around $stmt->execute(); * DBD-File/File.pm (FETCH): $sth->{TYPE} is now returning undef (workaround for a bug in DBI 0.93). 0.1010 - 1998-06-11, Jochen Wiedmann * Added DBD::File, DBD::CSV is derived from it. * CSV.pm (open_table): Added $dbh->{tables}->{$table}->{file}. 0.1002 - 1998-05-21, Jochen Wiedmann * CSV.pm: Now using the misc.files_as_tables feature of SQL::Statement 0.1000 - 1998-05-05, Jochen Wiedmann * Initial version DBD-CSV-0.49/META.yml0000644000031300001440000000172012715051740014440 0ustar00merijnusers00000000000000--- abstract: DBI driver for CSV files author: - Jochen Wiedmann - Jeff Zucker - H.Merijn Brand - Jens Rehsack build_requires: Config: 0 configure_requires: DBI: '1.628' ExtUtils::MakeMaker: 0 dynamic_config: 1 generated_by: Author, CPAN::Meta::Converter version 2.150005 license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: DBD-CSV provides: DBD::CSV: file: lib/DBD/CSV.pm version: '0.49' recommends: DBD::File: '0.44' DBI: '1.636' SQL::Statement: '1.410' Test::More: '1.302015' Text::CSV_XS: '1.23' requires: Cwd: 0 DBD::File: '0.42' DBI: '1.628' Encode: 0 SQL::Statement: '1.405' Test::Harness: 0 Test::More: '0.90' Text::CSV_XS: '1.01' charnames: 0 perl: '5.008001' resources: license: http://dev.perl.org/licenses/ repository: https://github.com/perl5-dbi/DBD-CSV.git version: '0.49' x_installdirs: site DBD-CSV-0.49/MANIFEST0000755000031300001440000000133512715051740014325 0ustar00merijnusers00000000000000ChangeLog MANIFEST MANIFEST.SKIP Makefile.PL CONTRIBUTING.md README lib/Bundle/DBD/CSV.pm lib/DBD/CSV.pm lib/DBD/CSV/TypeInfo.pm lib/DBD/CSV/GetInfo.pm t/00_meta.t t/00_pod_cov.t t/00_pod.t t/10_base.t t/11_dsnlist.t t/20_createdrop.t t/30_insertfetch.t t/31_delete.t t/32_update.t t/40_numrows.t t/41_nulls.t t/42_bindparam.t t/43_blobs.t t/44_listfields.t t/48_utf8.t t/50_chopblanks.t t/51_commit.t t/55_dir_search.t t/60_misc.t t/61_meta.t t/70_csv.t t/71_csv-ext.t t/72_csv-schema.t t/73_csv-case.t t/80_rt.t t/85_error.t t/lib.pl t/tmp.csv examples/passwd.pl META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) DBD-CSV-0.49/lib/0000755000031300001440000000000012715051740013735 5ustar00merijnusers00000000000000DBD-CSV-0.49/lib/DBD/0000755000031300001440000000000012715051740014326 5ustar00merijnusers00000000000000DBD-CSV-0.49/lib/DBD/CSV/0000755000031300001440000000000012715051740014761 5ustar00merijnusers00000000000000DBD-CSV-0.49/lib/DBD/CSV/TypeInfo.pm0000644000031300001440000000372512431663562017071 0ustar00merijnusers00000000000000#!/usr/bin/perl # Don't forget to add version and intellectual property control information. # The %type_info_all hash was automatically generated by # DBI::DBD::Metadata::write_typeinfo_pm v2.014214. package DBD::CSV::TypeInfo; { require Exporter; require DynaLoader; @ISA = qw(Exporter DynaLoader); @EXPORT = qw(type_info_all); use DBI qw(:sql_types); $type_info_all = [ { TYPE_NAME => 0, DATA_TYPE => 1, COLUMN_SIZE => 2, LITERAL_PREFIX => 3, LITERAL_SUFFIX => 4, CREATE_PARAMS => 5, NULLABLE => 6, CASE_SENSITIVE => 7, SEARCHABLE => 8, UNSIGNED_ATTRIBUTE => 9, FIXED_PREC_SCALE => 10, AUTO_UNIQUE_VALUE => 11, LOCAL_TYPE_NAME => 12, MINIMUM_SCALE => 13, MAXIMUM_SCALE => 14, SQL_DATA_TYPE => 15, SQL_DATETIME_SUB => 16, NUM_PREC_RADIX => 17, INTERVAL_PRECISION => 18, }, [ "VARCHAR", SQL_VARCHAR, undef, "'", "'", undef, 0, 1, 1, 0, undef, undef, undef, 1, 999999, undef, undef, undef, undef, ], [ "CHAR", SQL_CHAR, undef, "'", "'", undef, 0, 1, 1, 0, undef, undef, undef, 1, 999999, undef, undef, undef, undef, ], [ "INTEGER", SQL_INTEGER, undef, "", "", undef, 0, 0, 1, 0, undef, undef, undef, 0, 0, undef, undef, undef, undef, ], [ "REAL", SQL_REAL, undef, "", "", undef, 0, 0, 1, 0, undef, undef, undef, 0, 0, undef, undef, undef, undef, ], [ "BLOB", SQL_LONGVARBINARY, undef, "'", "'", undef, 0, 1, 1, 0, undef, undef, undef, 1, 999999, undef, undef, undef, undef, ], [ "BLOB", SQL_LONGVARBINARY, undef, "'", "'", undef, 0, 1, 1, 0, undef, undef, undef, 1, 999999, undef, undef, undef, undef, ], [ "TEXT", SQL_LONGVARCHAR, undef, "'", "'", undef, 0, 1, 1, 0, undef, undef, undef, 1, 999999, undef, undef, undef, undef, ], ]; 1; } DBD-CSV-0.49/lib/DBD/CSV/GetInfo.pm0000644000031300001440000002516012175506441016661 0ustar00merijnusers00000000000000#!/usr/bin/perl # The %info hash was automatically generated by # DBI::DBD::Metadata::write_getinfo_pm v2.014214. package DBD::CSV::GetInfo; use strict; use DBD::CSV; # Beware: not officially documented interfaces... # use DBI::Const::GetInfoType qw(%GetInfoType); # use DBI::Const::GetInfoReturn qw(%GetInfoReturnTypes %GetInfoReturnValues); my $sql_driver = "CSV"; # DBD::CSV uses tw-partr version string my $sql_ver_fmt = "%02d.%02d.0000"; # ODBC version string: ##.##.##### my $sql_driver_ver = sprintf $sql_ver_fmt, split /\./ => $DBD::CSV::VERSION; sub sql_data_source_name { my $dbh = shift; return "dbi:$sql_driver:" . $dbh->{Name}; } # sql_data_source_name sub sql_user_name { my $dbh = shift; # CURRENT_USER is a non-standard attribute, probably undef # Username is a standard DBI attribute return $dbh->{CURRENT_USER} || $dbh->{Username}; } # sql_user_name our %info = ( # 20 => undef, # SQL_ACCESSIBLE_PROCEDURES # 19 => undef, # SQL_ACCESSIBLE_TABLES # 0 => undef, # SQL_ACTIVE_CONNECTIONS # 116 => undef, # SQL_ACTIVE_ENVIRONMENTS # 1 => undef, # SQL_ACTIVE_STATEMENTS # 169 => undef, # SQL_AGGREGATE_FUNCTIONS # 117 => undef, # SQL_ALTER_DOMAIN # 86 => undef, # SQL_ALTER_TABLE # 10021 => undef, # SQL_ASYNC_MODE # 120 => undef, # SQL_BATCH_ROW_COUNT # 121 => undef, # SQL_BATCH_SUPPORT # 82 => undef, # SQL_BOOKMARK_PERSISTENCE # 114 => undef, # SQL_CATALOG_LOCATION # 10003 => undef, # SQL_CATALOG_NAME # 41 => undef, # SQL_CATALOG_NAME_SEPARATOR # 42 => undef, # SQL_CATALOG_TERM # 92 => undef, # SQL_CATALOG_USAGE # 10004 => undef, # SQL_COLLATING_SEQUENCE # 10004 => undef, # SQL_COLLATION_SEQ # 87 => undef, # SQL_COLUMN_ALIAS # 22 => undef, # SQL_CONCAT_NULL_BEHAVIOR # 53 => undef, # SQL_CONVERT_BIGINT # 54 => undef, # SQL_CONVERT_BINARY # 55 => undef, # SQL_CONVERT_BIT # 56 => undef, # SQL_CONVERT_CHAR # 57 => undef, # SQL_CONVERT_DATE # 58 => undef, # SQL_CONVERT_DECIMAL # 59 => undef, # SQL_CONVERT_DOUBLE # 60 => undef, # SQL_CONVERT_FLOAT # 48 => undef, # SQL_CONVERT_FUNCTIONS # 173 => undef, # SQL_CONVERT_GUID # 61 => undef, # SQL_CONVERT_INTEGER # 123 => undef, # SQL_CONVERT_INTERVAL_DAY_TIME # 124 => undef, # SQL_CONVERT_INTERVAL_YEAR_MONTH # 71 => undef, # SQL_CONVERT_LONGVARBINARY # 62 => undef, # SQL_CONVERT_LONGVARCHAR # 63 => undef, # SQL_CONVERT_NUMERIC # 64 => undef, # SQL_CONVERT_REAL # 65 => undef, # SQL_CONVERT_SMALLINT # 66 => undef, # SQL_CONVERT_TIME # 67 => undef, # SQL_CONVERT_TIMESTAMP # 68 => undef, # SQL_CONVERT_TINYINT # 69 => undef, # SQL_CONVERT_VARBINARY # 70 => undef, # SQL_CONVERT_VARCHAR # 122 => undef, # SQL_CONVERT_WCHAR # 125 => undef, # SQL_CONVERT_WLONGVARCHAR # 126 => undef, # SQL_CONVERT_WVARCHAR # 74 => undef, # SQL_CORRELATION_NAME # 127 => undef, # SQL_CREATE_ASSERTION # 128 => undef, # SQL_CREATE_CHARACTER_SET # 129 => undef, # SQL_CREATE_COLLATION # 130 => undef, # SQL_CREATE_DOMAIN # 131 => undef, # SQL_CREATE_SCHEMA # 132 => undef, # SQL_CREATE_TABLE # 133 => undef, # SQL_CREATE_TRANSLATION # 134 => undef, # SQL_CREATE_VIEW # 23 => undef, # SQL_CURSOR_COMMIT_BEHAVIOR # 24 => undef, # SQL_CURSOR_ROLLBACK_BEHAVIOR # 10001 => undef, # SQL_CURSOR_SENSITIVITY # 16 => undef, # SQL_DATABASE_NAME 2 => \&sql_data_source_name, # SQL_DATA_SOURCE_NAME # 25 => undef, # SQL_DATA_SOURCE_READ_ONLY # 119 => undef, # SQL_DATETIME_LITERALS # 17 => undef, # SQL_DBMS_NAME # 18 => undef, # SQL_DBMS_VER # 18 => undef, # SQL_DBMS_VERSION # 170 => undef, # SQL_DDL_INDEX # 26 => undef, # SQL_DEFAULT_TRANSACTION_ISOLATION # 26 => undef, # SQL_DEFAULT_TXN_ISOLATION # 10002 => undef, # SQL_DESCRIBE_PARAMETER # 171 => undef, # SQL_DM_VER # 3 => undef, # SQL_DRIVER_HDBC # 135 => undef, # SQL_DRIVER_HDESC # 4 => undef, # SQL_DRIVER_HENV # 76 => undef, # SQL_DRIVER_HLIB # 5 => undef, # SQL_DRIVER_HSTMT 6 => $INC{"DBD/CSV.pm"}, # SQL_DRIVER_NAME # 77 => undef, # SQL_DRIVER_ODBC_VER 7 => $sql_driver_ver, # SQL_DRIVER_VER # 136 => undef, # SQL_DROP_ASSERTION # 137 => undef, # SQL_DROP_CHARACTER_SET # 138 => undef, # SQL_DROP_COLLATION # 139 => undef, # SQL_DROP_DOMAIN # 140 => undef, # SQL_DROP_SCHEMA # 141 => undef, # SQL_DROP_TABLE # 142 => undef, # SQL_DROP_TRANSLATION # 143 => undef, # SQL_DROP_VIEW # 144 => undef, # SQL_DYNAMIC_CURSOR_ATTRIBUTES1 # 145 => undef, # SQL_DYNAMIC_CURSOR_ATTRIBUTES2 # 27 => undef, # SQL_EXPRESSIONS_IN_ORDERBY # 8 => undef, # SQL_FETCH_DIRECTION # 84 => undef, # SQL_FILE_USAGE # 146 => undef, # SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES1 # 147 => undef, # SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES2 # 81 => undef, # SQL_GETDATA_EXTENSIONS # 88 => undef, # SQL_GROUP_BY # 28 => undef, # SQL_IDENTIFIER_CASE # 29 => undef, # SQL_IDENTIFIER_QUOTE_CHAR # 148 => undef, # SQL_INDEX_KEYWORDS # 149 => undef, # SQL_INFO_SCHEMA_VIEWS # 172 => undef, # SQL_INSERT_STATEMENT # 73 => undef, # SQL_INTEGRITY # 150 => undef, # SQL_KEYSET_CURSOR_ATTRIBUTES1 # 151 => undef, # SQL_KEYSET_CURSOR_ATTRIBUTES2 # 89 => undef, # SQL_KEYWORDS # 113 => undef, # SQL_LIKE_ESCAPE_CLAUSE # 78 => undef, # SQL_LOCK_TYPES # 34 => undef, # SQL_MAXIMUM_CATALOG_NAME_LENGTH # 97 => undef, # SQL_MAXIMUM_COLUMNS_IN_GROUP_BY # 98 => undef, # SQL_MAXIMUM_COLUMNS_IN_INDEX # 99 => undef, # SQL_MAXIMUM_COLUMNS_IN_ORDER_BY # 100 => undef, # SQL_MAXIMUM_COLUMNS_IN_SELECT # 101 => undef, # SQL_MAXIMUM_COLUMNS_IN_TABLE # 30 => undef, # SQL_MAXIMUM_COLUMN_NAME_LENGTH # 1 => undef, # SQL_MAXIMUM_CONCURRENT_ACTIVITIES # 31 => undef, # SQL_MAXIMUM_CURSOR_NAME_LENGTH # 0 => undef, # SQL_MAXIMUM_DRIVER_CONNECTIONS # 10005 => undef, # SQL_MAXIMUM_IDENTIFIER_LENGTH # 102 => undef, # SQL_MAXIMUM_INDEX_SIZE # 104 => undef, # SQL_MAXIMUM_ROW_SIZE # 32 => undef, # SQL_MAXIMUM_SCHEMA_NAME_LENGTH # 105 => undef, # SQL_MAXIMUM_STATEMENT_LENGTH # 20000 => undef, # SQL_MAXIMUM_STMT_OCTETS # 20001 => undef, # SQL_MAXIMUM_STMT_OCTETS_DATA # 20002 => undef, # SQL_MAXIMUM_STMT_OCTETS_SCHEMA # 106 => undef, # SQL_MAXIMUM_TABLES_IN_SELECT # 35 => undef, # SQL_MAXIMUM_TABLE_NAME_LENGTH # 107 => undef, # SQL_MAXIMUM_USER_NAME_LENGTH # 10022 => undef, # SQL_MAX_ASYNC_CONCURRENT_STATEMENTS # 112 => undef, # SQL_MAX_BINARY_LITERAL_LEN # 34 => undef, # SQL_MAX_CATALOG_NAME_LEN # 108 => undef, # SQL_MAX_CHAR_LITERAL_LEN # 97 => undef, # SQL_MAX_COLUMNS_IN_GROUP_BY # 98 => undef, # SQL_MAX_COLUMNS_IN_INDEX # 99 => undef, # SQL_MAX_COLUMNS_IN_ORDER_BY # 100 => undef, # SQL_MAX_COLUMNS_IN_SELECT # 101 => undef, # SQL_MAX_COLUMNS_IN_TABLE # 30 => undef, # SQL_MAX_COLUMN_NAME_LEN # 1 => undef, # SQL_MAX_CONCURRENT_ACTIVITIES # 31 => undef, # SQL_MAX_CURSOR_NAME_LEN # 0 => undef, # SQL_MAX_DRIVER_CONNECTIONS # 10005 => undef, # SQL_MAX_IDENTIFIER_LEN # 102 => undef, # SQL_MAX_INDEX_SIZE # 32 => undef, # SQL_MAX_OWNER_NAME_LEN # 33 => undef, # SQL_MAX_PROCEDURE_NAME_LEN # 34 => undef, # SQL_MAX_QUALIFIER_NAME_LEN # 104 => undef, # SQL_MAX_ROW_SIZE # 103 => undef, # SQL_MAX_ROW_SIZE_INCLUDES_LONG # 32 => undef, # SQL_MAX_SCHEMA_NAME_LEN # 105 => undef, # SQL_MAX_STATEMENT_LEN # 106 => undef, # SQL_MAX_TABLES_IN_SELECT # 35 => undef, # SQL_MAX_TABLE_NAME_LEN # 107 => undef, # SQL_MAX_USER_NAME_LEN # 37 => undef, # SQL_MULTIPLE_ACTIVE_TXN # 36 => undef, # SQL_MULT_RESULT_SETS # 111 => undef, # SQL_NEED_LONG_DATA_LEN # 75 => undef, # SQL_NON_NULLABLE_COLUMNS # 85 => undef, # SQL_NULL_COLLATION # 49 => undef, # SQL_NUMERIC_FUNCTIONS # 9 => undef, # SQL_ODBC_API_CONFORMANCE # 152 => undef, # SQL_ODBC_INTERFACE_CONFORMANCE # 12 => undef, # SQL_ODBC_SAG_CLI_CONFORMANCE # 15 => undef, # SQL_ODBC_SQL_CONFORMANCE # 73 => undef, # SQL_ODBC_SQL_OPT_IEF # 10 => undef, # SQL_ODBC_VER # 115 => undef, # SQL_OJ_CAPABILITIES # 90 => undef, # SQL_ORDER_BY_COLUMNS_IN_SELECT # 38 => undef, # SQL_OUTER_JOINS # 115 => undef, # SQL_OUTER_JOIN_CAPABILITIES # 39 => undef, # SQL_OWNER_TERM # 91 => undef, # SQL_OWNER_USAGE # 153 => undef, # SQL_PARAM_ARRAY_ROW_COUNTS # 154 => undef, # SQL_PARAM_ARRAY_SELECTS # 80 => undef, # SQL_POSITIONED_STATEMENTS # 79 => undef, # SQL_POS_OPERATIONS # 21 => undef, # SQL_PROCEDURES # 40 => undef, # SQL_PROCEDURE_TERM # 114 => undef, # SQL_QUALIFIER_LOCATION # 41 => undef, # SQL_QUALIFIER_NAME_SEPARATOR # 42 => undef, # SQL_QUALIFIER_TERM # 92 => undef, # SQL_QUALIFIER_USAGE # 93 => undef, # SQL_QUOTED_IDENTIFIER_CASE # 11 => undef, # SQL_ROW_UPDATES # 39 => undef, # SQL_SCHEMA_TERM # 91 => undef, # SQL_SCHEMA_USAGE # 43 => undef, # SQL_SCROLL_CONCURRENCY # 44 => undef, # SQL_SCROLL_OPTIONS # 14 => undef, # SQL_SEARCH_PATTERN_ESCAPE # 13 => undef, # SQL_SERVER_NAME # 94 => undef, # SQL_SPECIAL_CHARACTERS # 155 => undef, # SQL_SQL92_DATETIME_FUNCTIONS # 156 => undef, # SQL_SQL92_FOREIGN_KEY_DELETE_RULE # 157 => undef, # SQL_SQL92_FOREIGN_KEY_UPDATE_RULE # 158 => undef, # SQL_SQL92_GRANT # 159 => undef, # SQL_SQL92_NUMERIC_VALUE_FUNCTIONS # 160 => undef, # SQL_SQL92_PREDICATES # 161 => undef, # SQL_SQL92_RELATIONAL_JOIN_OPERATORS # 162 => undef, # SQL_SQL92_REVOKE # 163 => undef, # SQL_SQL92_ROW_VALUE_CONSTRUCTOR # 164 => undef, # SQL_SQL92_STRING_FUNCTIONS # 165 => undef, # SQL_SQL92_VALUE_EXPRESSIONS # 118 => undef, # SQL_SQL_CONFORMANCE # 166 => undef, # SQL_STANDARD_CLI_CONFORMANCE # 167 => undef, # SQL_STATIC_CURSOR_ATTRIBUTES1 # 168 => undef, # SQL_STATIC_CURSOR_ATTRIBUTES2 # 83 => undef, # SQL_STATIC_SENSITIVITY # 50 => undef, # SQL_STRING_FUNCTIONS # 95 => undef, # SQL_SUBQUERIES # 51 => undef, # SQL_SYSTEM_FUNCTIONS # 45 => undef, # SQL_TABLE_TERM # 109 => undef, # SQL_TIMEDATE_ADD_INTERVALS # 110 => undef, # SQL_TIMEDATE_DIFF_INTERVALS # 52 => undef, # SQL_TIMEDATE_FUNCTIONS # 46 => undef, # SQL_TRANSACTION_CAPABLE # 72 => undef, # SQL_TRANSACTION_ISOLATION_OPTION # 46 => undef, # SQL_TXN_CAPABLE # 72 => undef, # SQL_TXN_ISOLATION_OPTION # 96 => undef, # SQL_UNION # 96 => undef, # SQL_UNION_STATEMENT 47 => \&sql_user_name, # SQL_USER_NAME # 10000 => undef, # SQL_XOPEN_CLI_YEAR ); 1; DBD-CSV-0.49/lib/DBD/CSV.pm0000755000031300001440000010050212673004314015316 0ustar00merijnusers00000000000000#!/usr/bin/perl # # DBD::CSV - A DBI driver for CSV and similar structured files # # This module is currently maintained by # # H.Merijn Brand # # See for full acknowledgements the last two pod sections in this file use strict; use warnings; require DynaLoader; require DBD::File; require IO::File; package DBD::CSV; use strict; use vars qw( @ISA $VERSION $ATTRIBUTION $drh $err $errstr $sqlstate ); @ISA = qw( DBD::File ); $VERSION = "0.49"; $ATTRIBUTION = "DBD::CSV $DBD::CSV::VERSION by H.Merijn Brand"; $err = 0; # holds error code for DBI::err $errstr = ""; # holds error string for DBI::errstr $sqlstate = ""; # holds error state for DBI::state $drh = undef; # holds driver handle once initialized sub CLONE # empty method: prevent warnings when threads are cloned { } # CLONE # --- DRIVER ------------------------------------------------------------------- package DBD::CSV::dr; use strict; use Text::CSV_XS (); use vars qw( @ISA @CSV_TYPES ); @CSV_TYPES = ( Text::CSV_XS::IV (), # SQL_TINYINT Text::CSV_XS::IV (), # SQL_BIGINT Text::CSV_XS::PV (), # SQL_LONGVARBINARY Text::CSV_XS::PV (), # SQL_VARBINARY Text::CSV_XS::PV (), # SQL_BINARY Text::CSV_XS::PV (), # SQL_LONGVARCHAR Text::CSV_XS::PV (), # SQL_ALL_TYPES Text::CSV_XS::PV (), # SQL_CHAR Text::CSV_XS::NV (), # SQL_NUMERIC Text::CSV_XS::NV (), # SQL_DECIMAL Text::CSV_XS::IV (), # SQL_INTEGER Text::CSV_XS::IV (), # SQL_SMALLINT Text::CSV_XS::NV (), # SQL_FLOAT Text::CSV_XS::NV (), # SQL_REAL Text::CSV_XS::NV (), # SQL_DOUBLE ); our @ISA = qw( DBD::File::dr ); our $imp_data_size = 0; our $data_sources_attr = undef; sub connect { my ($drh, $dbname, $user, $auth, $attr) = @_; my $dbh = $drh->DBD::File::dr::connect ($dbname, $user, $auth, $attr); $dbh and $dbh->{Active} = 1; $dbh; } # connect # --- DATABASE ----------------------------------------------------------------- package DBD::CSV::db; use strict; our $imp_data_size = 0; our @ISA = qw( DBD::File::db ); sub set_versions { my $this = shift; $this->{csv_version} = $DBD::CSV::VERSION; return $this->SUPER::set_versions (); } # set_versions my %csv_xs_attr; sub init_valid_attributes { my $dbh = shift; # Straight from Text::CSV_XS.pm my @xs_attr = qw( eol sep_char quote_char escape_char binary decode_utf8 auto_diag diag_verbose blank_is_undef empty_is_undef allow_whitespace allow_loose_quotes allow_loose_escapes allow_unquoted_escape always_quote quote_empty quote_space escape_null quote_binary keep_meta_info verbatim types callbacks ); @csv_xs_attr{@xs_attr} = (); $dbh->{csv_xs_valid_attrs} = [ @xs_attr ]; $dbh->{csv_valid_attrs} = { map {("csv_$_" => 1 )} @xs_attr, qw( class tables in csv_in out csv_out skip_first_row null sep quote escape )}; $dbh->{csv_readonly_attrs} = { }; $dbh->{csv_meta} = "csv_tables"; return $dbh->SUPER::init_valid_attributes (); } # init_valid_attributes sub get_csv_versions { my ($dbh, $table) = @_; $table ||= ""; my $class = $dbh->{ImplementorClass}; $class =~ s/::db$/::Table/; my $meta; $table and (undef, $meta) = $class->get_table_meta ($dbh, $table, 1); unless ($meta) { $meta = {}; $class->bootstrap_table_meta ($dbh, $meta, $table); } my $dvsn = eval { $meta->{csv_class}->VERSION (); }; my $dtype = $meta->{csv_class}; $dvsn and $dtype .= " ($dvsn)"; return sprintf "%s using %s", $dbh->{csv_version}, $dtype; } # get_csv_versions sub get_info { my ($dbh, $info_type) = @_; require DBD::CSV::GetInfo; my $v = $DBD::CSV::GetInfo::info{int ($info_type)}; ref $v eq "CODE" and $v = $v->($dbh); return $v; } # get_info sub type_info_all { my $dbh = shift; require DBD::CSV::TypeInfo; return [@$DBD::CSV::TypeInfo::type_info_all]; } # type_info_all # --- STATEMENT ---------------------------------------------------------------- package DBD::CSV::st; use strict; our $imp_data_size = 0; our @ISA = qw(DBD::File::st); package DBD::CSV::Statement; use strict; use Carp; our @ISA = qw(DBD::File::Statement); package DBD::CSV::Table; use strict; use Carp; our @ISA = qw(DBD::File::Table); sub bootstrap_table_meta { my ($self, $dbh, $meta, $table) = @_; $meta->{csv_class} ||= $dbh->{csv_class} || "Text::CSV_XS"; $meta->{csv_eol} ||= $dbh->{csv_eol} || "\r\n"; exists $meta->{csv_skip_first_row} or $meta->{csv_skip_first_row} = $dbh->{csv_skip_first_row}; $self->SUPER::bootstrap_table_meta ($dbh, $meta, $table); } # bootstrap_table_meta sub init_table_meta { my ($self, $dbh, $meta, $table) = @_; $self->SUPER::init_table_meta ($dbh, $table, $meta); my $csv_in = $meta->{csv_in} || $dbh->{csv_csv_in}; unless ($csv_in) { my %opts = ( binary => 1, auto_diag => 1 ); # Allow specific Text::CSV_XS options foreach my $attr (@{$dbh->{csv_xs_valid_attrs}}) { $attr eq "eol" and next; # Handles below exists $dbh->{"csv_$attr"} and $opts{$attr} = $dbh->{"csv_$attr"}; } $dbh->{csv_null} || $meta->{csv_null} and $opts{Text::CSV_XS->version < 1.18 ? "always_quote" : "quote_empty"} = $opts{blank_is_undef} = 1; my $class = $meta->{csv_class}; my $eol = $meta->{csv_eol}; $eol =~ m/^\A(?:[\r\n]|\r\n)\Z/ or $opts{eol} = $eol; for ([ "sep", ',' ], [ "quote", '"' ], [ "escape", '"' ], ) { my ($attr, $def) = ($_->[0]."_char", $_->[1]); $opts{$attr} = exists $meta->{$attr} ? $meta->{$attr} : exists $dbh->{"csv_$attr"} ? $dbh->{"csv_$attr"} : $def; } $meta->{csv_in} = $class->new (\%opts) or $class->error_diag; $opts{eol} = $eol; $meta->{csv_out} = $class->new (\%opts) or $class->error_diag; } } # init_table_meta my %compat_map = map { $_ => "csv_$_" } qw( class eof eol quote_char sep_char escape_char ); __PACKAGE__->register_compat_map (\%compat_map); sub table_meta_attr_changed { my ($class, $meta, $attr, $value) = @_; (my $csv_attr = $attr) =~ s/^csv_//; if (exists $csv_xs_attr{$csv_attr}) { for ("csv_in", "csv_out") { exists $meta->{$_} && exists $meta->{$_}{$csv_attr} and $meta->{$_}{$csv_attr} = $value; } } $class->SUPER::table_meta_attr_changed ($meta, $attr, $value); } # table_meta_attr_changed sub open_data { my ($self, $meta, $attrs, $flags) = @_; $self->SUPER::open_file ($meta, $attrs, $flags); if ($meta && $meta->{fh}) { $attrs->{csv_csv_in} = $meta->{csv_in}; $attrs->{csv_csv_out} = $meta->{csv_out}; if (my $types = $meta->{types}) { # XXX $meta->{types} is nowhere assigned and should better $meta->{csv_types} # The 'types' array contains DBI types, but we need types # suitable for Text::CSV_XS. my $t = []; for (@{$types}) { $_ = $_ ? $DBD::CSV::dr::CSV_TYPES[$_ + 6] || Text::CSV_XS::PV () : Text::CSV_XS::PV (); push @$t, $_; } $meta->{types} = $t; } if (!$flags->{createMode}) { my $array; my $skipRows = defined $meta->{skip_rows} ? $meta->{skip_rows} : defined $meta->{csv_skip_first_row} ? 1 : exists $meta->{col_names} ? 0 : 1; defined $meta->{skip_rows} or $meta->{skip_rows} = $skipRows; if ($skipRows--) { $array = $attrs->{csv_csv_in}->getline ($meta->{fh}) or croak "Missing first row due to ".$attrs->{csv_csv_in}->error_diag; unless ($meta->{raw_header}) { s/\W/_/g for @$array; } defined $meta->{col_names} or $meta->{col_names} = $array; while ($skipRows--) { $attrs->{csv_csv_in}->getline ($meta->{fh}); } } # lockMode is set 1 for DELETE, INSERT or UPDATE # no other case need seeking $flags->{lockMode} and # $meta->{fh}->can ("tell") and $meta->{first_row_pos} = $meta->{fh}->tell (); exists $meta->{col_names} and $array = $meta->{col_names}; if (!$meta->{col_names} || !@{$meta->{col_names}}) { # No column names given; fetch first row and create default # names. my $ar = $meta->{cached_row} = $attrs->{csv_csv_in}->getline ($meta->{fh}); $array = $meta->{col_names}; push @$array, map { "col$_" } 0 .. $#$ar; } } } } # open_file no warnings 'once'; $DBI::VERSION < 1.623 and *open_file = \&open_data; use warnings; sub _csv_diag { my @diag = $_[0]->error_diag; for (2, 3) { defined $diag[$_] or $diag[$_] = "?"; } return @diag; } # _csv_diag sub fetch_row { my ($self, $data) = @_; exists $self->{cached_row} and return $self->{row} = delete $self->{cached_row}; my $tbl = $self->{meta}; my $csv = $self->{csv_csv_in} or return do { $data->set_err ($DBI::stderr, "Fetch from undefined handle"); undef }; my $fields; eval { $fields = $csv->getline ($tbl->{fh}) }; unless ($fields) { $csv->eof and return; my @diag = _csv_diag ($csv); $diag[0] == 2012 and return; # Also EOF (broken in Text::CSV_XS-1.10) my $file = $tbl->{f_fqfn}; croak "Error $diag[0] while reading file $file: $diag[1] \@ line $diag[3] pos $diag[2]"; } @$fields < @{$tbl->{col_names}} and push @$fields, (undef) x (@{$tbl->{col_names}} - @$fields); $self->{row} = (@$fields ? $fields : undef); } # fetch_row sub push_row { my ($self, $data, $fields) = @_; my $tbl = $self->{meta}; my $csv = $self->{csv_csv_out}; my $fh = $tbl->{fh}; unless ($csv->print ($fh, $fields)) { my @diag = _csv_diag ($csv); my $file = $tbl->{f_fqfn}; return do { $data->set_err ($DBI::stderr, "Error $diag[0] while writing file $file: $diag[1] \@ line $diag[3] pos $diag[2]"); undef }; } 1; } # push_row no warnings 'once'; *push_names = \&push_row; use warnings; 1; __END__ =head1 NAME DBD::CSV - DBI driver for CSV files =head1 SYNOPSIS use DBI; # See "Creating database handle" below $dbh = DBI->connect ("dbi:CSV:", undef, undef, { f_ext => ".csv/r", RaiseError => 1, }) or die "Cannot connect: $DBI::errstr"; # Simple statements $dbh->do ("CREATE TABLE foo (id INTEGER, name CHAR (10))"); # Selecting my $sth = $dbh->prepare ("select * from foo"); $sth->execute; $sth->bind_columns (\my ($id, $name)); while ($sth->fetch) { print "id: $id, name: $name\n"; } # Updates my $sth = $dbh->prepare ("UPDATE foo SET name = ? WHERE id = ?"); $sth->execute ("DBI rocks!", 1); $sth->finish; $dbh->disconnect; =head1 DESCRIPTION The DBD::CSV module is yet another driver for the DBI (Database independent interface for Perl). This one is based on the SQL "engine" SQL::Statement and the abstract DBI driver DBD::File and implements access to so-called CSV files (Comma Separated Values). Such files are often used for exporting MS Access and MS Excel data. See L for details on DBI, L for details on SQL::Statement and L for details on the base class DBD::File. =head2 Prerequisites The only system dependent feature that DBD::File uses, is the C function. Thus the module should run (in theory) on any system with a working C, in particular on all Unix machines and on Windows NT. Under Windows 95 and MacOS the use of C is disabled, thus the module should still be usable. Unlike other DBI drivers, you don't need an external SQL engine or a running server. All you need are the following Perl modules, available from any CPAN mirror, for example http://search.cpan.org/ =over 4 =item DBI X A recent version of the L (Database independent interface for Perl). See below why. =item DBD::File X This is the base class for DBD::CSV, and it is part of the DBI distribution. As DBD::CSV requires a matching version of L which is (partly) developed by the same team that maintains DBD::CSV. See META.json or Makefile.PL for the minimum versions. =item SQL::Statement X A simple SQL engine. This module defines all of the SQL syntax for DBD::CSV, new SQL support is added with each release so you should look for updates to SQL::Statement regularly. It is possible to run C without this module if you define the environment variable C<$DBI_SQL_NANO> to 1. This will reduce the SQL support a lot though. See L for more details. Note that the test suite does only test in this mode in the development environment. =item Text::CSV_XS X This module is used to read and write rows in a CSV file. =back =head2 Installation Installing this module (and the prerequisites from above) is quite simple. The simplest way is to install the bundle: $ cpan Bundle::CSV Alternatively, you can name them all $ cpan Text::CSV_XS DBI DBD::CSV or even trust C to resolve all dependencies for you: $ cpan DBD::CSV If you cannot, for whatever reason, use cpan, fetch all modules from CPAN, and build with a sequence like: gzip -d < DBD-CSV-0.40.tgz | tar xf - (this is for Unix users, Windows users would prefer WinZip or something similar) and then enter the following: cd DBD-CSV-0.40 perl Makefile.PL make test If any tests fail, let us know. Otherwise go on with make install UNINST=1 Note that you almost definitely need root or administrator permissions. If you don't have them, read the ExtUtils::MakeMaker man page for details on installing in your own directories. L. =head2 Supported SQL Syntax All SQL processing for DBD::CSV is done by SQL::Statement. See L for more specific information about its feature set. Features include joins, aliases, built-in and user-defined functions, and more. See L for a description of the SQL syntax supported in DBD::CSV. Table- and column-names are case insensitive unless quoted. Column names will be sanitized unless L is true. =head1 Using DBD::CSV with DBI For most things, DBD-CSV operates the same as any DBI driver. See L for detailed usage. =head2 Creating a database handle (connect) Creating a database handle usually implies connecting to a database server. Thus this command reads use DBI; my $dbh = DBI->connect ("dbi:CSV:", "", "", { f_dir => "/home/user/folder", }); The directory tells the driver where it should create or open tables (a.k.a. files). It defaults to the current directory, so the following are equivalent: $dbh = DBI->connect ("dbi:CSV:"); $dbh = DBI->connect ("dbi:CSV:", undef, undef, { f_dir => "." }); $dbh = DBI->connect ("dbi:CSV:f_dir=."); We were told, that VMS might - for whatever reason - require: $dbh = DBI->connect ("dbi:CSV:f_dir="); The preferred way of passing the arguments is by driver attributes: # specify most possible flags via driver flags $dbh = DBI->connect ("dbi:CSV:", undef, undef, { f_schema => undef, f_dir => "data", f_dir_search => [], f_ext => ".csv/r", f_lock => 2, f_encoding => "utf8", csv_eol => "\r\n", csv_sep_char => ",", csv_quote_char => '"', csv_escape_char => '"', csv_class => "Text::CSV_XS", csv_null => 1, csv_tables => { info => { f_file => "info.csv" } }, RaiseError => 1, PrintError => 1, FetchHashKeyName => "NAME_lc", }) or die $DBI::errstr; but you may set these attributes in the DSN as well, separated by semicolons. Pay attention to the semi-colon for C (as seen in many CSV exports from MS Excel) is being escaped in below example, as is would otherwise be seen as attribute separator: $dbh = DBI->connect ( "dbi:CSV:f_dir=$ENV{HOME}/csvdb;f_ext=.csv;f_lock=2;" . "f_encoding=utf8;csv_eol=\n;csv_sep_char=\\;;" . "csv_quote_char=\";csv_escape_char=\\;csv_class=Text::CSV_XS;" . "csv_null=1") or die $DBI::errstr; Using attributes in the DSN is easier to use when the DSN is derived from an outside source (environment variable, database entry, or configure file), whereas specifying entries in the attribute hash is easier to read and to maintain. =head2 Creating and dropping tables You can create and drop tables with commands like the following: $dbh->do ("CREATE TABLE $table (id INTEGER, name CHAR (64))"); $dbh->do ("DROP TABLE $table"); Note that currently only the column names will be stored and no other data. Thus all other information including column type (INTEGER or CHAR (x), for example), column attributes (NOT NULL, PRIMARY KEY, ...) will silently be discarded. This may change in a later release. A drop just removes the file without any warning. See L for more details. Table names cannot be arbitrary, due to restrictions of the SQL syntax. I recommend that table names are valid SQL identifiers: The first character is alphabetic, followed by an arbitrary number of alphanumeric characters. If you want to use other files, the file names must start with "/", "./" or "../" and they must not contain white space. =head2 Inserting, fetching and modifying data The following examples insert some data in a table and fetch it back: First, an example where the column data is concatenated in the SQL string: $dbh->do ("INSERT INTO $table VALUES (1, ". $dbh->quote ("foobar") . ")"); Note the use of the quote method for escaping the word "foobar". Any string must be escaped, even if it does not contain binary data. Next, an example using parameters: $dbh->do ("INSERT INTO $table VALUES (?, ?)", undef, 2, "It's a string!"); Note that you don't need to quote column data passed as parameters. This version is particularly well designed for loops. Whenever performance is an issue, I recommend using this method. You might wonder about the C. Don't wonder, just take it as it is. :-) It's an attribute argument that I have never used and will be passed to the prepare method as the second argument. To retrieve data, you can use the following: my $query = "SELECT * FROM $table WHERE id > 1 ORDER BY id"; my $sth = $dbh->prepare ($query); $sth->execute (); while (my $row = $sth->fetchrow_hashref) { print "Found result row: id = ", $row->{id}, ", name = ", $row->{name}; } $sth->finish (); Again, column binding works: The same example again. my $sth = $dbh->prepare (qq; SELECT * FROM $table WHERE id > 1 ORDER BY id; ;); $sth->execute; my ($id, $name); $sth->bind_columns (undef, \$id, \$name); while ($sth->fetch) { print "Found result row: id = $id, name = $name\n"; } $sth->finish; Of course you can even use input parameters. Here's the same example for the third time: my $sth = $dbh->prepare ("SELECT * FROM $table WHERE id = ?"); $sth->bind_columns (undef, \$id, \$name); for (my $i = 1; $i <= 2; $i++) { $sth->execute ($id); if ($sth->fetch) { print "Found result row: id = $id, name = $name\n"; } $sth->finish; } See L for details on these methods. See L for details on the WHERE clause. Data rows are modified with the UPDATE statement: $dbh->do ("UPDATE $table SET id = 3 WHERE id = 1"); Likewise you use the DELETE statement for removing rows: $dbh->do ("DELETE FROM $table WHERE id > 1"); =head2 Error handling In the above examples we have never cared about return codes. Of course, this is not recommended. Instead we should have written (for example): my $sth = $dbh->prepare ("SELECT * FROM $table WHERE id = ?") or die "prepare: " . $dbh->errstr (); $sth->bind_columns (undef, \$id, \$name) or die "bind_columns: " . $dbh->errstr (); for (my $i = 1; $i <= 2; $i++) { $sth->execute ($id) or die "execute: " . $dbh->errstr (); $sth->fetch and print "Found result row: id = $id, name = $name\n"; } $sth->finish ($id) or die "finish: " . $dbh->errstr (); Obviously this is tedious. Fortunately we have DBI's I attribute: $dbh->{RaiseError} = 1; $@ = ""; eval { my $sth = $dbh->prepare ("SELECT * FROM $table WHERE id = ?"); $sth->bind_columns (undef, \$id, \$name); for (my $i = 1; $i <= 2; $i++) { $sth->execute ($id); $sth->fetch and print "Found result row: id = $id, name = $name\n"; } $sth->finish ($id); }; $@ and die "SQL database error: $@"; This is not only shorter, it even works when using DBI methods within subroutines. =head1 DBI database handle attributes =head2 Metadata The following attributes are handled by DBI itself and not by DBD::File, thus they all work as 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 AutoCommit X Always on =item ChopBlanks X Works =item NUM_OF_FIELDS X Valid after C<$sth-Eexecute> =item NUM_OF_PARAMS X Valid after C<$sth-Eprepare> =item NAME X =item NAME_lc X =item NAME_uc X Valid after C<$sth-Eexecute>; undef for Non-Select statements. =item NULLABLE X Not really working. Always returns an array ref of one's, as DBD::CSV does not verify input data. Valid after C<$sth-Eexecute>; undef for non-Select statements. =back These attributes and methods are not supported: bind_param_inout CursorName LongReadLen LongTruncOk =head1 DBD-CSV specific database handle attributes In addition to the DBI attributes, you can use the following dbh attributes: =head2 DBD::File attributes =over 4 =item f_dir X This attribute is used for setting the directory where CSV files are opened. Usually you set it in the dbh and it defaults to the current directory ("."). However, it may be overridden in statement handles. =item f_dir_search X This attribute optionally defines a list of extra directories to search when opening existing tables. It should be an anonymous list or an array reference listing all folders where tables could be found. my $dbh = DBI->connect ("dbi:CSV:", "", "", { f_dir => "data", f_dir_search => [ "ref/data", "ref/old" ], f_ext => ".csv/r", }) or die $DBI::errstr; =item f_ext X This attribute is used for setting the file extension. =item f_schema X This attribute allows you to set the database schema name. The default is to use the owner of C. C is allowed, but not in the DSN part. my $dbh = DBI->connect ("dbi:CSV:", "", "", { f_schema => undef, f_dir => "data", f_ext => ".csv/r", }) or die $DBI::errstr; =item f_encoding X This attribute allows you to set the encoding of the data. With CSV, it is not possible to set (and remember) the encoding on a column basis, but DBD::File now allows the encoding to be set on the underlying file. If this attribute is not set, or undef is passed, the file will be seen as binary. =item f_lock X With this attribute you can specify a locking mode to be used (if locking is supported at all) for opening tables. By default, tables are opened with a shared lock for reading, and with an exclusive lock for writing. The supported modes are: =over 2 =item 0 X<0> Force no locking at all. =item 1 X<1> Only shared locks will be used. =item 2 X<2> Only exclusive locks will be used. =back =back But see L. =head2 DBD::CSV specific attributes =over 4 =item csv_class The attribute I controls the CSV parsing engine. This defaults to C, but C can be used in some cases, too. Please be aware that C does not care about any edge case as C does and that C is probably about 100 times slower than C. =back =head2 Text::CSV_XS specific attributes =over 4 =item csv_eol X =item csv_sep_char X =item csv_quote_char X =item csv_escape_char X =item csv_csv X The attributes I, I, I and I are corresponding to the respective attributes of the I (usually Text::CSV_CS) object. You may want to set these attributes if you have unusual CSV files like F or MS Excel generated CSV files with a semicolon as separator. Defaults are C<\015\012>", C<,>, C<"> and C<">, respectively. The I attribute defines the end-of-line pattern, which is better known as a record separator pattern since it separates records. The default is windows-style end-of-lines C<\015\012> for output (writing) and unset for input (reading), so if on unix you may want to set this to newline (C<\n>) like this: $dbh->{csv_eol} = "\n"; It is also possible to use multi-character patterns as record separators. For example this file uses newlines as field separators (sep_char) and the pattern "\n__ENDREC__\n" as the record separators (eol): name city __ENDREC__ joe seattle __ENDREC__ sue portland __ENDREC__ To handle this file, you'd do this: $dbh->{eol} = "\n__ENDREC__\n" , $dbh->{sep_char} = "\n" The attributes are used to create an instance of the class I, by default Text::CSV_XS. Alternatively you may pass an instance as I, the latter takes precedence. Note that the I attribute I be set to a true value in that case. Additionally you may overwrite these attributes on a per-table base in the I attribute. =item csv_null X With this option set, all new statement handles will set C and C in the CSV parser and writer, so it knows how to distinguish between the empty string and C or C. You cannot reset it with a false value. You can pass it to connect, or set it later: $dbh = DBI->connect ("dbi:CSV:", "", "", { csv_null => 1 }); $dbh->{csv_null} = 1; =item csv_tables X This hash ref is used for storing table dependent metadata. For any table it contains an element with the table name as key and another hash ref with the following attributes: =item csv_* X All other attributes that start with C and are not described above will be passed to C (without the C prefix). These extra options are only likely to be useful for reading (select) handles. Examples: $dbh->{csv_allow_whitespace} = 1; $dbh->{csv_allow_loose_quotes} = 1; $dbh->{csv_allow_loose_escapes} = 1; See the C documentation for the full list and the documentation. =back =head2 Driver specific attributes =over 4 =item f_file X The name of the file used for the table; defaults to "$dbh->{f_dir}/$table" =item eol X =item sep_char X =item quote_char X =item escape_char X =item class X =item csv X These correspond to the attributes I, I, I, I, I and I. The difference is that they work on a per-table basis. =item col_names X =item skip_first_row X By default DBD::CSV assumes that column names are stored in the first row of the CSV file and sanitizes them (see C below). If this is not the case, you can supply an array ref of table names with the I attribute. In that case the attribute I will be set to FALSE. If you supply an empty array ref, the driver will read the first row for you, count the number of columns and create column names like C, C, ... Note that column names that match reserved SQL words will cause unwanted and sometimes confusing errors. If your CSV has headers that match reserved words, you will require these two attributes. If C looks like select,from 1,2 the select query would result in C