App-Repository
view release on metacpan or search on metacpan
lib/App/Repository.pm view on Meta::CPAN
* Signature: $rep->import_rows($table, $columns, $file);
* Signature: $rep->import_rows($table, $columns, $file, $options);
* Param: $table string
* Param: $columns ARRAY names of columns of the fields in the file
* Param: $file string
* Param: $options named
* Param: replace boolean rows should replace existing rows based on unique indexes
* Param: field_sep char character which separates the fields in the file (can by "\t")
* Param: field_quote char character which optionally encloses the fields in the file (i.e. '"')
* Param: field_escape char character which escapes the quote chars within quotes (i.e. "\")
* Return: void
* Throws: App::Exception::Repository
* Since: 0.01
Sample Usage:
$rep->import_rows("usr","usr.dat");
# root:x:0:0:root:/root:/bin/bash
$rep->import_rows("usr",
lib/App/Repository.pm view on Meta::CPAN
{ field_sep => ":", });
=cut
sub import_rows {
&App::sub_entry if ($App::trace);
my ($self, $table, $columns, $file, $options) = @_;
$columns = $self->_get_default_columns($table) if (!$columns);
my $field_sep = $options->{field_sep} || ",";
my $field_quote = $options->{field_quote};
my $field_escape = $options->{field_escape};
open(App::Repository::DBI::FILE, "< $file") || die "Unable to open $file for reading: $!";
my (@row, $quoted_field_regexp, $field_regexp);
while (<App::Repository::DBI::FILE>) {
chomp;
if ($field_quote) {
@row = ();
# TODO: use the _read_rows_from_file() method
# TODO: incorporate escaping
$field_regexp = "$field_sep?$field_quote([^$field_quote]*)$field_quote";
lib/App/Repository.pm view on Meta::CPAN
* Signature: $rep->export_rows($table, $columns, $file);
* Signature: $rep->export_rows($table, $columns, $file, $options);
* Param: $table string
* Param: $file string
* Param: $options named
* Param: columns ARRAY names of columns of the fields in the file
* Param: replace boolean rows should replace existing rows based on unique indexes
* Param: field_sep char character which separates the fields in the file (can by "\t")
* Param: field_quote char character which optionally encloses the fields in the file (i.e. '"')
* Param: field_escape char character which escapes the quote chars within quotes (i.e. "\")
* Return: void
* Throws: App::Exception::Repository
* Since: 0.01
Sample Usage:
$rep->export_rows("usr","usr.dat");
# root:x:0:0:root:/root:/bin/bash
$rep->export_rows("usr", "passwd.dat" ,{
lib/App/Repository.pm view on Meta::CPAN
=cut
sub export_rows {
&App::sub_entry if ($App::trace);
my ($self, $table, $columns, $file, $options) = @_;
$columns = $self->_get_default_columns($table) if (!$columns);
my $rows = $self->get_rows($table, {}, $columns);
my $field_sep = $options->{field_sep} || ",";
my $field_quote = $options->{field_quote};
my $field_escape = $options->{field_escape};
open(App::Repository::DBI::FILE, "> $file") || die "Unable to open $file for writing: $!";
my ($i, $value);
foreach my $row (@$rows) {
if ($field_quote) {
for ($i = 0; $i <= $#$row; $i++) {
print App::Repository::DBI::FILE $field_sep if ($i > 0);
$value = $row->[$i];
if ($value =~ /$field_sep/) {
if ($field_escape) {
$value =~ s/$field_escape/$field_escape$field_escape/g;
$value =~ s/$field_quote/$field_escape$field_quote/g;
}
print App::Repository::DBI::FILE $field_quote, $value, $field_quote;
}
else {
print App::Repository::DBI::FILE $value;
}
}
}
else {
print App::Repository::DBI::FILE join($field_sep, @$row), "\n";
lib/App/Repository.pm view on Meta::CPAN
}
sub _read_rows_from_file {
&App::sub_entry if ($App::trace);
my ($self, $fh, $cols, $options) = @_;
my $maxrows = $options->{maxrows};
my $null_value = $options->{null_value};
$null_value = '\N' if (!defined $null_value);
my $field_sep = $options->{field_sep} || ",";
my $field_quote = $options->{field_quote} || "";
my $field_escape = $options->{field_escape} || "";
die "TODO: field_escape not yet implemented" if ($field_escape);
my $fieldsep_regexp = ($field_sep eq "|") ? '\|' : $field_sep;
my $quoted_field_regexp = "$field_sep?$field_quote([^$field_quote]*)$field_quote";
my $field_regexp = "$field_sep?([^$field_sep]*)";
my $num_cols = $#$cols + 1;
my $rows_read = 0;
my $rows = [];
my ($num_values_read, $line, $line_remainder, $row);
while (<$fh>) {
chomp;
$line = $_;
if ($line) {
if (!$field_quote && !$field_escape) {
$row = [ map { $_ eq $null_value ? undef : $_ } split(/$fieldsep_regexp/, $line) ];
$num_values_read = $#$row + 1;
}
else {
$num_values_read = 0;
$line_remainder = $line;
$row = [];
while ($line_remainder) {
if ($line_remainder =~ s/^$quoted_field_regexp//) {
push(@$row, $1 eq $null_value ? undef : $1);
lib/App/Repository/MySQL.pm view on Meta::CPAN
* Param: $file string
* Param: $options named
* Param: columns ARRAY names of columns of the fields in the file
* Param: import_method string [basic=invokes generic superclass to do work,
insert=loads with multiple-row inserts,
<otherwise>=use "load data infile"]
* Param: local boolean file is on client machine rather than database server
* Param: replace boolean rows should replace existing rows based on unique indexes
* Param: field_sep char character which separates the fields in the file (can by "\t")
* Param: field_quote char character which optionally encloses the fields in the file (i.e. '"')
* Param: field_escape char character which escapes the quote chars within quotes (i.e. "\")
* Return: void
* Throws: App::Exception::Repository
* Since: 0.01
Note: If you want to call this with $options->{local}, you will probably
need to make sure that mysql_local_infile=1 is in your DSN. This might
require a line like the following in your "app.conf" file.
dbioptions = mysql_local_infile=1
lib/App/Repository/MySQL.pm view on Meta::CPAN
$nrows = $self->insert_rows($table, $columns, $file, $options);
}
elsif ($import_method eq "insert_mysql") {
$nrows = $self->insert_rows_mysql($table, $columns, $file, $options);
}
else {
my $local = $options->{local};
$local = 1 if (!defined $local);
my $local_modifier = $local ? " local" : "";
my $sql = "load data$local_modifier infile '$file' into table $table";
if ($options->{field_sep} || $options->{field_quote} || $options->{field_escape}) {
$sql .= "\nfields";
$sql .= "\n terminated by '$options->{field_sep}'" if ($options->{field_sep});
$sql .= "\n optionally enclosed by '$options->{field_quote}'" if ($options->{field_quote});
$sql .= "\n escaped by '$options->{field_escape}'" if ($options->{field_escape});
}
$sql .= "\n(" . join(",", @$columns) . ")\n";
my $context_options = $self->{context}{options};
my $debug_sql = $context_options->{debug_sql};
my ($timer, $elapsed_time);
if ($debug_sql) {
$timer = $self->_get_timer();
print $App::DEBUG_FILE "DEBUG_SQL: import_rows()\n";
print $App::DEBUG_FILE $sql;
}
lib/App/Repository/MySQL.pm view on Meta::CPAN
* Signature: $rep->export_rows($table, $file);
* Signature: $rep->export_rows($table, $file, $options);
* Param: $table string
* Param: $file string
* Param: $options named
* Param: columns ARRAY names of columns of the fields in the file
* Param: export_method string [basic=invokes generic superclass to do work]
* Param: field_sep char character which separates the fields in the file (can by "\t")
* Param: field_quote char character which optionally encloses the fields in the file (i.e. '"')
* Param: field_escape char character which escapes the quote chars within quotes (i.e. "\")
* Return: void
* Throws: App::Exception::Repository
* Since: 0.01
Sample Usage:
$rep->export_rows("usr","usr.dat");
# root:x:0:0:root:/root:/bin/bash
$rep->export_rows("usr", "passwd.dat" ,{
lib/App/Repository/MySQL.pm view on Meta::CPAN
if ($options->{export_method} && $options->{export_method} eq "basic") {
$self->SUPER::export_rows($table, $file, $options);
}
else {
my $columns = $options->{columns} || $self->{table}{$table}{columns};
my $where_clause = $self->_mk_where_clause($table, $params, $options);
my $sql = "select\n " . join(",\n ", @$columns);
$sql .= "\n$where_clause" if ($where_clause);
$sql .= "\ninto outfile '$file'";
if ($options->{field_sep} || $options->{field_quote} || $options->{field_escape}) {
$sql .= "\nfields";
$sql .= "\n terminated by '$options->{field_sep}'" if ($options->{field_sep});
$sql .= "\n optionally enclosed by '$options->{field_quote}'" if ($options->{field_quote});
$sql .= "\n escaped by '$options->{field_escape}'" if ($options->{field_escape});
}
$sql .= "\n";
my $context_options = $self->{context}{options};
my $debug_sql = $context_options->{debug_sql};
my ($timer, $elapsed_time);
if ($debug_sql) {
$timer = $self->_get_timer();
print $App::DEBUG_FILE "DEBUG_SQL: export_rows()\n";
print $App::DEBUG_FILE $sql;
}
( run in 0.499 second using v1.01-cache-2.11-cpan-c21f80fb71c )