Author: spadkins
Date: Wed Oct 28 08:54:18 2009
New Revision: 13444

Modified:
   p5ee/trunk/App-Repository/bin/dbsql

Log:
finished CSV support with --csv

Modified: p5ee/trunk/App-Repository/bin/dbsql
==============================================================================
--- p5ee/trunk/App-Repository/bin/dbsql (original)
+++ p5ee/trunk/App-Repository/bin/dbsql Wed Oct 28 08:54:18 2009
@@ -158,6 +158,48 @@
     &App::sub_exit() if ($App::trace);
 }
 
+sub print_table_as_csv {
+    &App::sub_entry if ($App::trace);
+    my ($rows, $columns, $formats, $options) = @_;
+    my ($row, $r, $c, $elem, $format, $len, $f, $heading);
+    my $headings = $options->{headings};
+
+    # compute the number of columns as the max columns of any row
+    my $max_columns = 0;
+    for ($r = 0; $r <= $#$rows; $r++) {
+        $row = $rows->[$r];
+        if ($max_columns < $#$row + 1) {
+            $max_columns = $#$row + 1;
+        }
+    }
+    for ($c = 0; $c <= $#$columns; $c++) {
+        print "," if ($c > 0);
+        $heading = ($headings && $headings->[$c]) ? $headings->[$c] : 
$columns->[$c];
+        print $heading;
+    }
+    print "\n";
+    my ($value);
+    for ($r = 0; $r <= $#$rows; $r++) {
+        $row = $rows->[$r];
+        for ($c = 0; $c <= $#$row; $c++) {
+            print "," if ($c > 0);
+            $value = $row->[$c];
+            if (!defined $value) {
+                # print nothing
+            }
+            elsif ($value =~ /,/) {
+                $value =~ s/,\s?/ /g;
+                print $value;
+            }
+            else {
+                print $value;
+            }
+        }
+        print "\n";
+    }
+    &App::sub_exit() if ($App::trace);
+}
+
 sub determine_sprintf_fmt {
     &App::sub_entry if ($App::trace);
     my ($f) = @_;

Reply via email to