File Coverage

blib/lib/WebService/SQLFormat.pm
Criterion Covered Total %
statement 25 28 89.2
branch 3 6 50.0
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 37 43 86.0


line stmt bran cond sub pod time code
1             package WebService::SQLFormat;
2             $WebService::SQLFormat::VERSION = '0.000007';
3 1     1   227671 use Moo 2.002004;
  1         9866  
  1         7  
4              
5 1     1   1980 use JSON::MaybeXS qw( decode_json );
  1         5966  
  1         71  
6 1     1   744 use LWP::UserAgent ();
  1         38036  
  1         40  
7 1     1   14 use Module::Runtime qw( use_module );
  1         2  
  1         13  
8 1     1   846 use Types::Standard qw( Bool InstanceOf Int Str );
  1         58473  
  1         9  
9 1     1   1282 use Types::URI qw( Uri );
  1         81282  
  1         11  
10              
11             has debug_level => (
12             is => 'ro',
13             isa => Int,
14             default => 0,
15             );
16              
17             has identifier_case => (
18             is => 'ro',
19             isa => Str,
20             predicate => '_has_identifier_case',
21             documentation => q{'upper', 'lower' or 'capitalize'},
22             );
23              
24             has keyword_case => (
25             is => 'ro',
26             isa => Str,
27             predicate => '_has_keyword_case',
28             documentation => q{'upper', 'lower' or 'capitalize'},
29             );
30              
31             has reindent => (
32             is => 'ro',
33             isa => Bool,
34             default => 0,
35             );
36              
37             has strip_comments => (
38             is => 'ro',
39             isa => Bool,
40             default => 0,
41             );
42              
43             has ua => (
44             is => 'ro',
45             isa => InstanceOf ['LWP::UserAgent'],
46             lazy => 1,
47             builder => '_build_ua',
48             );
49              
50             has url => (
51             is => 'ro',
52             isa => Uri,
53             coerce => 1,
54             lazy => 1,
55             default => 'https://sqlformat.org/api/v1/format',
56             );
57              
58             sub _build_ua {
59 1     1   14 my $self = shift;
60 1         14 my $ua = LWP::UserAgent->new;
61 1 50       3445 return $ua unless $self->debug_level;
62              
63 0         0 use_module( 'LWP::ConsoleLogger::Easy', 0.000028 );
64 0         0 LWP::ConsoleLogger::Easy::debug_ua( $ua, $self->debug_level );
65 0         0 return $ua;
66             }
67              
68             sub format_sql {
69 1     1 1 19658 my $self = shift;
70 1         3 my $sql = shift;
71              
72 1 50       34 my $res = $self->ua->post(
    50          
73             $self->url,
74             {
75             (
76             $self->_has_identifier_case
77             ? ( identifier_case => $self->identifier_case )
78             : ()
79             ),
80             (
81             $self->_has_keyword_case
82             ? ( keyword_case => $self->keyword_case )
83             : ()
84             ),
85             reindent => $self->reindent,
86             sql => $sql,
87             strip_comments => $self->strip_comments,
88             }
89             );
90 1         260335 return decode_json( $res->decoded_content )->{result};
91             }
92              
93             1;
94              
95             =pod
96              
97             =encoding UTF-8
98              
99             =head1 NAME
100              
101             WebService::SQLFormat - Format SQL via the sqlformat.org API
102              
103             =head1 VERSION
104              
105             version 0.000007
106              
107             =head1 SYNOPSIS
108              
109             use strict;
110             use warnings;
111             use feature qw( say );
112              
113             use WebService::SQLFormat;
114             my $formatter = WebService::SQLFormat->new(
115             identifier_case => 'upper',
116             reindent => 1,
117             );
118              
119             my $sql = shift @ARGV;
120              
121             say $formatter->format_sql($sql);
122              
123             =head2 CONSTRUCTOR OPTIONS
124              
125             =over 4
126              
127             =item debug_level
128              
129             An integer between 0 and 8. Used to set debugging level for
130             L<LWP::ConsoleLogger::Easy>. Defaults to 0.
131              
132             =item identifier_case
133              
134             Case to use for SQL identifiers. One of 'upper', 'lower' or 'capitalize'. If
135             no value is supplied, identifiers will not be changed.
136              
137             =item keyword_case
138              
139             Case to use for SQL keywords. One of 'upper', 'lower' or 'capitalize'. If no
140             value is supplied, case will not be changed.
141              
142             =item reindent( 0|1)
143              
144             Re-indent supplied SQL. Defaults to 0.
145              
146             =item strip_comments( 0|1 )
147              
148             Remove SQL comments. Defaults to 0.
149              
150             =item ua
151              
152             You may supply your own user agent. Must be of the L<LWP::UserAgent> family.
153              
154             =item url
155              
156             The API url to query. Defaults to L<https://sqlformat.org/api/v1/format>
157              
158             =back
159              
160             =head2 format_sql( $raw_sql )
161              
162             This method expects a scalar containing the SQL which you'd like to format.
163             Returns the formatted SQL.
164              
165             =head1 DESCRIPTION
166              
167             BETA BETA BETA. Subject to change.
168              
169             This module is a thin wrapper around L<https://sqlformat.org>
170              
171             =head1 AUTHOR
172              
173             Olaf Alders <olaf@wundercounter.com>
174              
175             =head1 COPYRIGHT AND LICENSE
176              
177             This software is copyright (c) 2016-2017 by Olaf Alders.
178              
179             This is free software; you can redistribute it and/or modify it under
180             the same terms as the Perl 5 programming language system itself.
181              
182             =cut
183              
184             __END__
185              
186             # ABSTRACT: Format SQL via the sqlformat.org API
187