| 1 | package UR::DataSource::CSV; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | # There are still a few issues with actually using this thing: | ||
| 6 | # | ||
| 7 | # when running | ||
| 8 | # ur define datasource rdbms --dsn DBD:CSV:f_dir=/tmp/trycsv | ||
| 9 | # the f_dir=... part doesn't get put in as a "server" attribute. | ||
| 10 | # You have to add in it by hand as sub server {} | ||
| 11 | # | ||
| 12 | # after ur update classes, there aren't any id properties defined | ||
| 13 | # for your new classes, because there's no conclusive way to pick | ||
| 14 | # the right one - no unique constraints | ||
| 15 | # | ||
| 16 | # _get_sequence_name_for_table_and_column() and _get_next_value_from_sequence() | ||
| 17 | # aren't implemented yet, so creating new entities and sync_databases | ||
| 18 | # won't work | ||
| 19 | # | ||
| 20 | # There's a bug in even the latest SQL::Statement on CPAN where the processing | ||
| 21 | # of JOIN clauses uses a case sensitive match against upper-case stuff, when it | ||
| 22 | # should be lower-case. It also cannot handle more than one join in the same | ||
| 23 | # statement | ||
| 24 | # | ||
| 25 | # with that out of the way... on to the show! | ||
| 26 | |||
| 27 | require UR; | ||
| 28 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 29 | |||
| 30 | use File::Basename; | ||
| 31 | |||
| 32 | UR::Object::Type->define( | ||
| 33 | class_name => 'UR::DataSource::CSV', | ||
| 34 | is => ['UR::DataSource::RDBMS'], | ||
| 35 | is_abstract => 1, | ||
| 36 | ); | ||
| 37 | |||
| 38 | # RDBMS API | ||
| 39 | |||
| 40 | sub driver { "CSV" } | ||
| 41 | |||
| 42 | sub owner { | ||
| 43 | undef | ||
| 44 | } | ||
| 45 | |||
| 46 | sub login { | ||
| 47 | undef | ||
| 48 | } | ||
| 49 | |||
| 50 | sub auth { | ||
| 51 | undef | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | sub path { | ||
| 56 | my $server = shift->server; | ||
| 57 | |||
| 58 | my @server_opts = split(';', $server); | ||
| 59 | foreach my $opt ( @server_opts ) { | ||
| 60 | my($key,$value) = split('=',$opt); | ||
| 61 | if ($key eq 'f_dir') { | ||
| 62 | return $value; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | return; | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | sub can_savepoint { 0;} # Dosen't support savepoints | ||
| 71 | |||
| 72 | sub _dbi_connect_args { | ||
| 73 | my $self = shift; | ||
| 74 | |||
| 75 | my @connection = $self->SUPER::_dbi_connect_args(@_); | ||
| 76 | delete $connection[3]->{'AutoCommit'}; # DBD::CSV doesn't support autocommit being off | ||
| 77 | return @connection; | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | sub _get_sequence_name_for_table_and_column { | ||
| 82 | Carp::croak("Not implemented yet"); | ||
| 83 | |||
| 84 | my $self = shift->_singleton_object; | ||
| 85 | my ($table_name,$column_name) = @_; | ||
| 86 | |||
| 87 | my $dbh = $self->get_default_handle(); | ||
| 88 | |||
| 89 | # See if the sequence generator "table" is already there | ||
| 90 | my $seq_table = sprintf('URMETA_%s_%s_seq', $table_name, $column_name); | ||
| 91 | unless ($self->{'_has_sequence_generator'}->{$seq_table} or | ||
| 92 | grep {$_ eq $seq_table} $self->get_table_names() ) { | ||
| 93 | unless ($dbh->do("CREATE TABLE IF NOT EXISTS $seq_table (next_value integer PRIMARY KEY AUTOINCREMENT)")) { | ||
| 94 | die "Failed to create sequence generator $seq_table: ".$dbh->errstr(); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | $self->{'_has_sequence_generator'}->{$seq_table} = 1; | ||
| 98 | |||
| 99 | return $seq_table; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _get_next_value_from_sequence { | ||
| 103 | Carp::croak('Not implemented yet'); | ||
| 104 | |||
| 105 | my($self,$sequence_name) = @_; | ||
| 106 | |||
| 107 | my $dbh = $self->get_default_handle(); | ||
| 108 | |||
| 109 | # FIXME can we use a statement handle with a wildcard as the table name here? | ||
| 110 | unless ($dbh->do("INSERT into $sequence_name values(null)")) { | ||
| 111 | die "Failed to INSERT into $sequence_name during id autogeneration: " . $dbh->errstr; | ||
| 112 | } | ||
| 113 | |||
| 114 | my $new_id = $dbh->last_insert_id(undef,undef,$sequence_name,'next_value'); | ||
| 115 | unless (defined $new_id) { | ||
| 116 | die "last_insert_id() returned undef during id autogeneration after insert into $sequence_name: " . $dbh->errstr; | ||
| 117 | } | ||
| 118 | |||
| 119 | unless($dbh->do("DELETE from $sequence_name where next_value = $new_id")) { | ||
| 120 | die "DELETE from $sequence_name for next_value $new_id failed during id autogeneration"; | ||
| 121 | } | ||
| 122 | |||
| 123 | return $new_id; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | # Given a table name, return the complete pathname to it | ||
| 128 | # As ur update classes calls this, $table has been uppercased | ||
| 129 | # already (because most data sources uppercase table names), | ||
| 130 | # so we need to figure out which file they're talking about | ||
| 131 | sub _find_pathname_for_table { | ||
| 132 | my $self = shift; | ||
| 133 | my $table = shift; | ||
| 134 | |||
| 135 | my $path = $self->path; | ||
| 136 | |||
| 137 | my @all_files = glob("$path/*"); | ||
| 138 | # note: this only finds the first one | ||
| 139 | foreach my $pathname ( @all_files ) { | ||
| 140 | if (File::Basename::basename($pathname) eq $table) { | ||
| 141 | return $pathname; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | # column_info doesn't work against a DBD::CSV handle | ||
| 149 | sub get_column_details_from_data_dictionary { | ||
| 150 | my($self,$catalog,$schema,$table,$column) = @_; | ||
| 151 | |||
| 152 | # Convert the SQL wildcards to glob wildcards | ||
| 153 | $table =~ tr/%_/*?/; | ||
| 154 | |||
| 155 | # Convert the SQL wildcards to regex wildcards | ||
| 156 | $column =~ s/%/\\w*/; | ||
| 157 | $column =~ s/_/\\w/; | ||
| 158 | my $column_regex = qr($column); | ||
| 159 | |||
| 160 | my(@matching_files) = $self->_find_pathname_for_table($table); | ||
| 161 | my @found_columns; | ||
| 162 | foreach my $file ( @matching_files ) { | ||
| 163 | my $table_name = File::Basename::basename($file); | ||
| 164 | my $fh = IO::File->new($file); | ||
| 165 | unless ($fh) { | ||
| 166 | $self->warning_message("Can't open file $file for reading: $!"); | ||
| 167 | next; | ||
| 168 | } | ||
| 169 | |||
| 170 | my $header = $fh->getline(); | ||
| 171 | $header =~ s/\r|\n//g; # Remove newline/CR | ||
| 172 | |||
| 173 | my @columns = split($self->get_default_handle->{'csv_sep_char'} ||',' , $header); | ||
| 174 | my $column_order = 0; | ||
| 175 | foreach my $column_name ( @columns ) { | ||
| 176 | $column_order++; | ||
| 177 | next unless $column_name =~ m/$column_regex/; | ||
| 178 | |||
| 179 | push @found_columns, { TABLE_CAT => $catalog, | ||
| 180 | TABLE_SCHEM => $schema, | ||
| 181 | TABLE_NAME => $table_name, | ||
| 182 | COLUMN_NAME => $column_name, | ||
| 183 | DATA_TYPE => 'STRING', # what else could we put here? | ||
| 184 | TYPE_NAME => 'STRING', | ||
| 185 | NULLABLE => 1, # all columns are nullable in CSV files | ||
| 186 | IS_NULLABLE => 'YES', | ||
| 187 | REMARKS => '', | ||
| 188 | COLUMN_DEF => '', | ||
| 189 | SQL_DATA_TYPE => '', # FIXME shouldn't this be something related to DATA_TYPE | ||
| 190 | SQL_DATETIME_SUB => '', | ||
| 191 | CHAH_OCTET_LENGTH => undef, # FIXME this should be the same as column_size, right? | ||
| 192 | ORDINAL_POSITION => $column_order, | ||
| 193 | } | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | my $sponge = DBI->connect("DBI:Sponge:", '','') | ||
| 198 | or return $self->get_default_handle->set_err($DBI::err, "DBI::Sponge: $DBI::errstr"); | ||
| 199 | |||
| 200 | my @returned_names = qw( TABLE_CAT TABLE_SCHEM TABLE_NAME COLUMN_NAME DATA_TYPE TYPE_NAME COLUMN_SIZE | ||
| 201 | BUFFER_LENGTH DECIMAL_DIGITS NUM_PREC_RADIX NULLABLE REMARKS COLUMN_DEF | ||
| 202 | SQL_DATA_TYPE SQL_DATETIME_SUB CHAR_OCTET_LENGTH ORDINAL_POSITION IS_NULLABLE ); | ||
| 203 | my $returned_sth = $sponge->prepare("column_info $table", { | ||
| 204 | rows => [ map { [ @{$_}{@returned_names} ] } @found_columns ], | ||
| 205 | NUM_OF_FIELDS => scalar @returned_names, | ||
| 206 | NAME => \@returned_names, | ||
| 207 | }) or return $self->get_default_handle->set_err($sponge->err(), $sponge->errstr()); | ||
| 208 | |||
| 209 | return $returned_sth; | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | # DBD::CSV doesn't support foreign key tracking | ||
| 214 | # returns a statement handle with no data to read | ||
| 215 | sub get_foreign_key_details_from_data_dictionary { | ||
| 216 | my($self,$fk_catalog,$fk_schema,$fk_table,$pk_catalog,$pk_schema,$pk_table) = @_; | ||
| 217 | |||
| 218 | my $sponge = DBI->connect("DBI:Sponge:", '','') | ||
| 219 | or return $self->get_default_handle->DBI::set_err($DBI::err, "DBI::Sponge: $DBI::errstr"); | ||
| 220 | |||
| 221 | my @returned_names = qw( FK_NAME UK_TABLE_NAME UK_COLUMN_NAME FK_TABLE_NAME FK_COLUMN_NAME ); | ||
| 222 | my $table = $pk_table || $fk_table; | ||
| 223 | my $returned_sth = $sponge->prepare("foreign_key_info $table", { | ||
| 224 | rows => [], | ||
| 225 | NUM_OF_FIELDS => scalar @returned_names, | ||
| 226 | NAME => \@returned_names, | ||
| 227 | }) or return $self->get_default_handle->DBI::set_err($sponge->err(), $sponge->errstr()); | ||
| 228 | |||
| 229 | return $returned_sth; | ||
| 230 | } | ||
| 231 | |||
| 232 | |||
| 233 | sub get_bitmap_index_details_from_data_dictionary { | ||
| 234 | # DBD::CSV dosen't support bitmap indicies, so there aren't any | ||
| 235 | return []; | ||
| 236 | } | ||
| 237 | |||
| 238 | |||
| 239 | sub get_unique_index_details_from_data_dictionary { | ||
| 240 | # DBD::CSV doesn't support unique constraints | ||
| 241 | return {}; | ||
| 242 | } | ||
| 243 | |||
| 244 | sub get_table_details_from_data_dictionary { | ||
| 245 | my($self,$catalog,$schema,$table,$type) = @_; | ||
| 246 | |||
| 247 | # DBD::CSV's table_info seems to always give you back all the "tables" even | ||
| 248 | # if you only asked for details on one of them | ||
| 249 | my $sth = $self->SUPER::get_table_details_from_data_dictionary($catalog,$schema,$table,$type); | ||
| 250 | |||
| 251 | # Yeah, it's kind of silly to have to read in all the data and repackage it | ||
| 252 | # back into another sth | ||
| 253 | my @returned_details; | ||
| 254 | while (my $row = $sth->fetchrow_arrayref()) { | ||
| 255 | next unless ($row->[2] eq $table); | ||
| 256 | push @returned_details, $row; | ||
| 257 | } | ||
| 258 | |||
| 259 | my $sponge = DBI->connect("DBI:Sponge:", '','') | ||
| 260 | or return $self->get_default_handle->DBI::set_err($DBI::err, "DBI::Sponge: $DBI::errstr"); | ||
| 261 | |||
| 262 | my @returned_names = qw( TABLE_CAT TABLE_SCHEM TABLE_NAME TABLE_TYPE REMARKS ); | ||
| 263 | my $returned_sth = $sponge->prepare("table_info $table", { | ||
| 264 | rows => \@returned_details, | ||
| 265 | NUM_OF_FIELDS => scalar @returned_names, | ||
| 266 | NAME => \@returned_names, | ||
| 267 | }) or return $self->get_default_handle->DBI::set_err($sponge->err(), $sponge->errstr()); | ||
| 268 | |||
| 269 | $returned_sth; | ||
| 270 | } | ||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | # By default, make a text dump of the database at commit time. | ||
| 275 | # This should really be a datasource property | ||
| 276 | sub dump_on_commit { | ||
| 277 | 0; | ||
| 278 | } | ||
| 279 | |||
| 280 | |||
| 281 | 1; | ||
| 282 | |||
| 283 | |||
| 284 | =pod | ||
| 285 | |||
| 286 | =head1 NAME | ||
| 287 | |||
| 288 | UR::DataSource::CSV - Parent class for data sources using DBD::CSV | ||
| 289 | |||
| 290 | =head1 DESCRIPTION | ||
| 291 | |||
| 292 | UR::DataSource::CSV is a subclass of L<UR::DataSource::RDBMS> and can be | ||
| 293 | used for interacting with CSV files. Because of the limitations of the | ||
| 294 | underlying modules (such as SQL::Statement only supporting one join at a | ||
| 295 | time), this module is deprecated. | ||
| 296 | |||
| 297 | L<UR::DataSource::File> implements a non-SQL interface for data files, and | ||
| 298 | is the proper way to use a file as a data source for class data. | ||
| 299 | |||
| 300 | =cut |