File: sqla-format

package info (click to toggle)
libsql-abstract-perl 2.000001-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 744 kB
  • sloc: perl: 3,443; makefile: 8
file content (61 lines) | stat: -rwxr-xr-x 1,354 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env perl

use warnings;
use strict;

use Getopt::Long;
my $p = Getopt::Long::Parser->new(config => [qw( gnu_getopt no_ignore_case )]);
my $opts = { profile => 'console', help => \&showhelp };
$p->getoptions( $opts, qw(
  profile|p=s
  help|h
)) or showhelp();

sub showhelp {
  require Pod::Usage;
  Pod::Usage::pod2usage( -verbose => 0, -exitval => 2 );
}

require SQL::Abstract::Tree;
my $sqlat = SQL::Abstract::Tree->new({ profile => $opts->{profile}, fill_in_placeholders => 0 });

my $chunk = '';
my $leftover = '';
do {
  $chunk = $leftover . $chunk if length $leftover;

  if ($chunk =~ / \A (.+?) (?:
    (?<=\S)\:\s+\'[^\n]+  # pasting DBIC_TRACE output directly
      |
    \;(?: \s | \z)
      |
    \z
      |
    ^ \s* (?=SELECT|INSERT|UPDATE|DELETE)
  ) (.*) /smix) {

    $leftover = $2;
    print $sqlat->format($1);
    print "\n";
  }
  else {
    $leftover = $chunk;
  }
} while ( (read *STDIN, $chunk, 4096) or length $leftover );

=head1 NAME

sqla-format - An intelligent SQL formatter

=head1 SYNOPSIS

 ~$ sqla-format << log.sql

 ~$ myprogram -v | sqla-format -p html > sqltrace.html

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Arthur Axel "fREW" Schmidt.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.