File: Import.pm

package info (click to toggle)
dbix-easy-perl 0.16-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 164 kB
  • ctags: 77
  • sloc: perl: 1,952; makefile: 55
file content (479 lines) | stat: -rw-r--r-- 12,441 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# Import.pm - Easy to Use DBI import interface

# Copyright (C) 2004 Stefan Hornburg (Racke) <racke@linuxia.de>

# Authors: Stefan Hornburg (Racke) <racke@linuxia.de>
# Maintainer: Stefan Hornburg (Racke) <racke@linuxia.de>
# Version: 0.16

# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.

# This file is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this file; see the file COPYING.  If not, write to the Free
# Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

package DBIx::Easy::Import;

use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);

require Exporter;

@ISA = qw(Exporter);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@EXPORT = qw(
);

# Public variables
$VERSION = '0.16';

use DBI;
use DBIx::Easy;

sub new {
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self = {driver => shift, database => shift};

	bless ($self, $class);
}

sub update {
	my ($self, %params) = @_;

	$self->_do_import(%params);
}

sub initialize {
	my ($self, $file, $format) = @_;
	my ($sep_char);
	
	$format = uc($format);

	if ($format =~ /^CSV/) {
		$format = 'CSV';
		if ($') {
			$sep_char = $';
			$sep_char =~ s/^\s+//;
			$sep_char =~ s/\s+$//;
		}
		eval {
			require Text::CSV_XS;
		};
		if ($@) {
			die "$0: couldn't load module Text::CSV_XS\n";
		}
		$self->{func} = \&get_columns_csv;
		$self->{parser} = new Text::CSV_XS ({'binary' => 1, 'sep_char' => $sep_char});
	} elsif ($format eq 'XLS') {
		eval {
			require Spreadsheet::ParseExcel;
		};
		if ($@) {
			die "$0: couldn't load module Spreadsheet::ParseExcel\n";
		}
		$self->{parser} = new Spreadsheet::ParseExcel;
	} elsif ($format eq 'TAB') {
		$self->{func} = \&get_columns_tab;
	} else {
		die qq{$0: unknown format "$format"}, "\n";
	}

	if ($file) {
		# read input from file
		require IO::File;
		$self->{fd_input} = new IO::File;
		$self->{fd_input}->open($file)
			|| die "$0: couldn't open $file: $!\n";
	} else {
		# read input from standard input
		require IO::Handle;
		$self->{fd_input} = new IO::Handle;
		$self->{fd_input}->fdopen(fileno(STDIN),'r');
	}

}

sub _do_import {
	my ($self, %params) = @_;
	my ($format, $sep_char, %colmap, %hcolmap);

	$self->{colflag} = 1;

	$self->initialize($params{file}, $params{'format'});
	
	my @columns;

	if ($params{'columns'}) {
		$self->{colflag} = ! ($params{'columns'} =~ s/\s*[\!^]//);
		
		# setup positive/negative list for columns
		for (split(/\s*,\s*/, $params{'columns'})) {
			$self->{usecol}->{$_} = $self->{colflag};
		}
	}

	if (ref($params{'map'}) eq 'HASH') {
		%colmap = %{$params{'map'}};
	} elsif ($params{'map'}) {
		# parse column name mapping
		my ($head, $name);
		foreach (split (/;/, $params{'map'})) {
			($head, $name) = split /=/;
			$colmap{$head} = $name;
		}
	}

	if (1) {
		my %hcolmap;
		my @columns;

		if ($self->{func}->($self, \@columns) <= 0)  {
			die "$0: couldn't find headline\n";
		}

		if ($params{'map_filter'} eq 'lc') {
			@columns = map {lc($_)} @columns;
		}

		# remove whitespace from column names and mark them
		map {s/^\s+//; s/\s+$//; $hcolmap{$_} = 1;} @columns;

		if ($params{'map'}) {
			my @newcolumns;
        
			# filter column names
			foreach (@columns) {
				if (exists $colmap{$_}) {
					push (@newcolumns, $colmap{$_});
					$hcolmap{$colmap{$_}} = 1;
				} else {
					push (@newcolumns, $_);
				}
			}
			@columns = @newcolumns;
		}

		# add any other columns explicitly selected
		for (sort (keys %{$self->{usecol}})) {
			next if $hcolmap{$_};
			next unless exists $self->{usecol}->{$_};
			next unless $self->{usecol}->{$_};
			push (@columns, $_);
		}

		$self->{fieldmap}->{$params{table}} = \@columns;
	}
	
	# database access
	my $dbif = $self->{dbif} = new DBIx::Easy ($self->{driver} || $params{driver},
							   $self->{database} || $params{database});
	# determine column names
    my @names = $self->column_names ($params{table});
    my $fieldnames = \@names;
	my @values;

	while ($self->{func}->($self, \@columns))  {
		my (@data);
		
		@values = @columns;
		
		# sanity checks on input data
		my $typeref = $dbif -> typemap ($params{table});
		my $sizeref = $dbif -> sizemap ($params{table});

		for (my $i = 0; $i <= $#$fieldnames; $i++) {
			# check for column exclusion
			if (keys %{$self->{usecol}}) {
				# note: we do not check the actual value !!
				if ($self->{colflag} && ! exists $self->{usecol}->{$$fieldnames[$i]}) {
					next;
				}
				if (! $self->{colflag} && exists $self->{usecol}->{$$fieldnames[$i]}) {
					next;
				}
			}
			
			# expand newlines and tabulators
			if (defined $values[$i]) {
				$values[$i] =~ s/\\n/\n/g;
				$values[$i] =~ s/\\t/\t/g;
			}
        
			# check if input exceeds column capacity
			unless (exists $$typeref{$$fieldnames[$i]}) {
				warn ("$0: No type information for column $$fieldnames[$i] found\n");
				next;
			}
			unless (exists $$sizeref{$$fieldnames[$i]}) {
				warn ("$0: No size information for column $$fieldnames[$i] found\n");
				next;
			}
			if ($$typeref{$$fieldnames[$i]} == DBI::SQL_CHAR) {
				if (defined $values[$i]) {
					if (length($values[$i]) > $$sizeref{$$fieldnames[$i]}) {
						warn ("$0: Data for field $$fieldnames[$i] truncated: $values[$i]\n");
						$values[$i] = substr($values[$i], 0,
											 $$sizeref{$$fieldnames[$i]});
					}
				} else {
					# avoid insertion of NULL values
					$values[$i] = '';
				}	
			} elsif ($$typeref{$$fieldnames[$i]} == DBI::SQL_VARCHAR) {
				if (defined $values[$i]) {
					if (length($values[$i]) > $$sizeref{$$fieldnames[$i]}) {
						warn ("$0: Data for field $$fieldnames[$i] truncated: $values[$i]\n");
						$values[$i] = substr($values[$i], 0,
											 $$sizeref{$$fieldnames[$i]});
					}
				} else {
					# avoid insertion of NULL values
					$values[$i] = '';
				}
			}
#        push (@data, $$fieldnames[$i], $values[$i]);
		}
		
		# check if record exists
		my %keymap = $self->key_names ($params{table}, $params{'keys'} || 1, 1);
		my @keys = (keys(%keymap));
		my @terms = map {$_ . ' = ' . $dbif->quote($values[$keymap{$_}])}
			(@keys);
		my $sth = $dbif -> process ('SELECT ' . join(', ', @keys)
								 . " FROM $params{table} WHERE "
								 . join (' AND ', @terms));
		while ($sth -> fetch) {}
		
		if ($sth -> rows () > 1) {
			$" = ', ';
			die ("$0: duplicate key(s) @keys in table $params{table}\n");
		}

		my $update = $sth -> rows ();
		$sth -> finish ();
    
		# generate SQL statement
		for (my $i = 0; $i <= $#$fieldnames; $i++) {
			# check for column exclusion
			if (keys %{$self->{usecol}}) {
				# note: we do not check the actual value !!
				if ($self->{colflag} && ! exists $self->{usecol}->{$$fieldnames[$i]}) {
					next;
				}
				if (! $self->{colflag} && exists $self->{usecol}->{$$fieldnames[$i]}) {
					next;
				}
			}
			# expand newlines
			if (defined $values[$i]) {
				$values[$i] =~ s/\\n/\n/g;
			}
			push (@data, $$fieldnames[$i], $values[$i]);
		}

		if ($update) {
			$dbif -> update ($params{table}, join (' AND ', @terms), @data);
		} else {
			if ($params{'update_only'}) {
				$" = ', ';
				die ("$0: key(s) @keys not found\n");
			}
			$dbif -> insert ($params{table}, @data);
		}
	}
}

# -------------------------------------------------
# FUNCTION: column_names DBIF TABLE [START]
#
# Returns array with column names from table TABLE
# using database connection DBIF.
# Optional parameter START specifies column where
# the array should start with.
# -------------------------------------------------

sub column_names {
    my ($self, $table, $start) = @_;    
    my ($names, $sth);

    $start = 0 unless $start;
    
    if (exists $self->{fieldmap}->{$table}) {
        $names = $self->{fieldmap}->{$table};
    } else {
        $sth = $self->{dbif}-> process ("SELECT * FROM $table WHERE 0 = 1");
        $names = $self->{fieldmap}->{$table} = $sth -> {NAME};
        $sth -> finish ();
    }

    @$names[$start .. $#$names];
}


# --------------------------------------------------
# FUNCTION: key_names DBIF TABLE KEYSPEC [HASH]
#
# Returns array with key names for table TABLE.
# Database connection DBIF may be used to
# retrieve necessary information.
# KEYSPEC contains desired keys, either a numeric
# value or a comma-separated list of keys.
# If HASH is set, a mapping between key name
# and position is returned.
# --------------------------------------------------

sub key_names () {
    my ($self, $table, $keyspec, $hash) = @_;
    
    my ($numkeysleft, $i);
    my @columns = $self->column_names ($table);
    my (@keys, %kmap);
    
    $keyspec =~ s/^\s+//; $keyspec =~ s/\s+$//;

    if ($keyspec =~ /^\d+$/) {
        #
        # passed keys are numeric, figure out the names
        #

        $numkeysleft = $keyspec;

        for ($i = 0; $i < $numkeysleft && $i < @columns; $i++) {
            if (keys %{$self->{usecol}}) {
                # note: we do not check the actual value !!
                if ($self->{colflag} && ! exists $self->{usecol}->{$columns[$i]}) {
                    $numkeysleft++;
                    next;
                }
                if (! $self->{colflag} && exists $self->{usecol}->{$columns[$i]}) {
                    $numkeysleft++;
                    next;
                }
            }
            if ($hash) {
                $kmap{$columns[$i]} = $i;
            } else {
                push (@keys, $columns[$i]);
            }
        }
	} else {
        #
        # key names are passed explicitly
        #

        my %colmap;
        
        for ($i = 0; $i < @columns; $i++) {
            $colmap{$columns[$i]} = $i;
        }
        
        for (split (/\s*,\s*/, $keyspec)) {
            # sanity check
            unless (exists $colmap{$_}) {
                die "$0: key \"$_\" appears not in column list\n";
            }
            
            if ($hash) {
                $kmap{$_} = $colmap{$_};
            } else {
                push (@keys, $_);
            }
        }
    }

    return $hash ? %kmap : @keys;
}

# FUNCTION: get_columns_csv IREF FD COLREF

sub get_columns_csv {
	my ($self, $colref) = @_;
	my $line;
	my $msg;
	my $fd = $self->{fd_input};
	
	while (defined ($line = <$fd>)) {
		if ($self->{parser}->parse($line)) {
			# csv line completed, delete buffer
			@$colref = $self->{parser}->fields();
			$self->{buffer} = '';
			return @$colref;
		} else {
			if (($line =~ tr/"/"/) % 2) {
			# odd number of quotes, try again with next line
				$self->{buffer} = $line;
			} else {
				$msg = "$0: $.: line not in CSV format: " . $self->{parser}->error_input() . "\n";
				die ($msg);
			}
		}
	}
}

# ----------------------------------------
# FUNCTION: get_columns_tab IREF FD COLREF
#
# Get columns from a tab separated file.
# ----------------------------------------

sub get_columns_tab {
	my ($self, $colref) = @_;
	my $line;
	my $fd = $self->{fd_input};
	
	while (defined ($line = <$fd>)) {
		# skip empty/blank/comment lines
		next if $line =~ /^\#/; next if $line =~ /^\s*$/;
		# remove newlines and carriage returns
		chomp ($line);
		$line =~ s/\r$//;

		@$colref = split (/\t/, $line);
		return @$colref;
	}
}

# ----------------------------------------
# FUNCTION: get_columns_xls IREF FD COLREF
#
# Get columns from a XLS spreadsheet.
# ----------------------------------------

sub get_columns_xls {
	my ($iref, $fd, $colref) = @_;

	unless ($iref->{workbook}) {
		# parse the spreadsheet once
		$iref->{workbook} = $iref->{object}->Parse($fd);
		unless ($iref->{workbook}) {
			die "$0: couldn't parse spreadsheet\n";
		}
		$iref->{worksheet} = $iref->{workbook}->{Worksheet}[0];
		$iref->{row} = 0;
	}

	if ($iref->{row} <= $iref->{worksheet}->{MaxRow}) {
		@$colref = map {$_->Value()}
			@{$iref->{worksheet}->{Cells}[$iref->{row}++]};
		return @$colref;
	}
}

sub get_columns {
	my ($self, $colref) = @_;

	return $self->{func}->($self, $colref);
}

1;