Skip to content

Instantly share code, notes, and snippets.

@moznion
Last active August 29, 2015 14:02
Show Gist options
  • Save moznion/fd979ff843571b4716ed to your computer and use it in GitHub Desktop.
Save moznion/fd979ff843571b4716ed to your computer and use it in GitHub Desktop.
package Your::App::DB::Schema;
use strict;
use warnings;
use Teng::Schema::Declare;
table {
name "table_name";
pk "id";
columns qw( id col1 col2 col3 );
};
1;
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Teng;
use Your::App::DB::Schema;
my $table_name = 'table_name';
my $schema = Your::App::DB::Schema->instance;
my $table = $schema->get_table($table_name);
open my $fh, '>', "${table_name}_dumped.csv";
my $columns = $table->{columns};
print $fh ';' . join(',', @$columns) . "\n";
my $teng = Teng->new({
# some settings
});
my @rows = $teng->search_by_sql(qq{
SELECT * FROM $table_name ORDER BY branch_id ASC
});
for my $row (@rows) {
my @data;
for my $column (@$columns) {
(my $data = $row->$column) =~ s/,/\\,/g;
push @data, $data;
}
print $fh join(',', @data) . "\n";
}
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment