DBD-XMLSimple

 view release on metacpan or  search on metacpan

lib/DBD/XMLSimple.pm  view on Meta::CPAN

			$colnames_seen{$leaf->name()}++;
		}
		# Also include 'id'
		if (defined(my $id = $record->att('id'))) {
			$colnames_seen{id}++;
		}
	}

	my @col_names = sort keys %colnames_seen;
	if (!@col_names) {
		carp "Empty table, creating dummy column '_dummy'";
		@col_names = ('_dummy');
	}
	my %col_nums = map { $col_names[$_] => $_ } 0..$#col_names;

	# Second pass — save row values
	for my $record (@records) {
		my %row;

		# Include id if present
		if (defined(my $id = $record->att('id'))) {
			$row{id} = $id;
		}

		for my $leaf ($record->children) {
			my $key = $leaf->name;
			if (defined $row{$key}) {
				$row{$key} .= ',' . $leaf->field();
			} else {
				$row{$key} = $leaf->field();
			}
		}

		# Now produce array in canonical column order
		push @rows, [ map { $row{$_} } @col_names ];
	}

	$data->{rows} = \@rows;

	# Store table metadata
	$data->{col_names} = \@col_names;
	$data->{col_nums}  = \%col_nums;
	$data->{row_count} = scalar @rows;

	return DBD::XMLSimple::Table->new($data, $data);
}

# Table handle
package DBD::XMLSimple::Table;
use base qw(DBI::DBD::SqlEngine::Table);

use strict;
use warnings;

sub new
{
	my($class, $data, $attr, $flags) = @_;

	$attr->{table} = $data;
	$attr->{readonly} = 1;
	$attr->{cursor} = 0;

	$attr->{rows} = $data->{rows};
	$attr->{col_nums} = $data->{col_nums};

	my $rc = $class->SUPER::new($data, $attr, $flags);

	$rc->{col_names} = $attr->{col_names};

	return $rc;
}

sub fetch_row($$)
{
	my($self, $data) = @_;

	if($self->{'cursor'} >= $data->{'row_count'}) {
		return undef;
	}

	$self->{row} = $self->{rows}[ $self->{cursor}++ ];
	return $self->{row};
}

sub seek($$$$)
{
	my($self, $data, $pos, $whence) = @_;

	print "seek $pos $whence, not yet implemented\n";
}

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

sub open_data
{
	# my($className, $meta, $attrs, $flags) = @_;
}

sub bootstrap_table_meta
{
	my($class, $dbh, $meta, $table, @other) = @_;

	$class->SUPER::bootstrap_table_meta($dbh, $meta, $table, @other);

	$meta->{table} = $table;

	$meta->{sql_data_source} ||= __PACKAGE__;
}

sub get_table_meta($$$$;$)
{
	my($class, $dbh, $table, $file_is_table, $respect_case) = @_;

	my $meta = $class->SUPER::get_table_meta($dbh, $table, $respect_case, $file_is_table);

	$table = $meta->{table};

	return unless $table;

	return($table, $meta);
}

=head1 AUTHOR

Nigel Horne, C<< <njh at nigelhorne.com> >>

=head1 BUGS

=head1 SEE ALSO

=over 4

=item * Test coverage report: L<https://nigelhorne.github.io/DBD-XMLSimple/coverage/>

=item * L<DBD::AnyData>, which was also used as a template for this module.

=back



( run in 2.256 seconds using v1.01-cache-2.11-cpan-13bb782fe5a )