File Coverage

blib/lib/App/sqltree.pm
Criterion Covered Total %
statement 37 39 94.8
branch 2 4 50.0
condition 6 18 33.3
subroutine 8 8 100.0
pod 0 1 0.0
total 53 70 75.7


line stmt bran cond sub pod time code
1             package App::sqltree;
2 1     1   464 use strict;
  1         2  
  1         22  
3 1     1   4 use warnings;
  1         1  
  1         20  
4 1     1   782 use File::ShareDir qw/dist_dir/;
  1         6629  
  1         70  
5 1     1   682 use IO::Prompt::Tiny qw/prompt/;
  1         520  
  1         60  
6 1     1   914 use OptArgs qw/arg opt optargs/;
  1         34259  
  1         8  
7 1     1   1269 use Path::Tiny qw/path/;
  1         13307  
  1         78  
8 1     1   792 use Template::Tiny;
  1         1323  
  1         419  
9              
10             our $VERSION = '0.0.5_2';
11              
12             arg driver => (
13             isa => 'Str',
14             comment => 'database driver type (SQLite | Pg)',
15             );
16              
17             arg table => (
18             isa => 'Str',
19             comment => 'the table name',
20             );
21              
22             arg pk => (
23             isa => 'Str',
24             comment => 'the primary key column name',
25             );
26              
27             arg parent => (
28             isa => 'Str',
29             comment => 'the parent column name',
30             );
31              
32             arg type => (
33             isa => 'Str',
34             comment => 'the SQL type of the primary key and parent columns',
35             );
36              
37             opt help => (
38             isa => 'Bool',
39             comment => 'print a usage message and exit',
40             alias => 'h',
41             ishelp => 1,
42             );
43              
44             opt path => (
45             isa => 'Str',
46             comment => 'the path column name',
47             alias => 'p',
48             );
49              
50             opt path_from => (
51             isa => 'Str',
52             comment => 'the source of the path column calculation',
53             alias => 'f',
54             );
55              
56             opt order => (
57             isa => 'Str',
58             comment => 'the order column name',
59             alias => 'o',
60             );
61              
62             opt no_print => (
63             isa => 'Bool',
64             comment => 'do not print the output, just return it',
65             hidden => 1,
66             );
67              
68             sub run {
69 2     2 0 72325 my $opts = shift;
70 2   33     50 my $share_dir =
71             path( $Test::App::sqltree::SHARE_DIR || dist_dir('SQL-Tree') );
72              
73 2   33     56 $opts->{driver} ||= prompt( 'Driver:', 'SQLite' );
74 2         28 my $template = $share_dir->child( $opts->{driver} . '.sql' );
75 2 50       114 die "Unsupported driver type: $opts->{driver}\n" unless -f $template;
76              
77 2   33     77 $opts->{table} ||= prompt( 'Table:', 'items' );
78 2   33     8 $opts->{pk} ||= prompt( 'Primary Key Column:', 'id' );
79 2   33     9 $opts->{parent} ||= prompt( 'Parent Column:', 'parent_id' );
80 2   33     12 $opts->{type} ||= prompt( 'PK/Parent Column Type:', 'integer' );
81              
82 2         10 $opts->{tree} = $opts->{table} . '_tree';
83              
84 2         31 my $tt = Template::Tiny->new;
85 2         45 my $src = $template->slurp_utf8;
86 2         2408 my $sql;
87              
88 2         16 $tt->process( \$src, $opts, \$sql );
89              
90 2 50       83486 if ( $opts->{no_print} ) {
91 2         2892 return split( /[\n\s]*-+SPLIT-+[\n\s]*/m, $sql );
92             }
93             else {
94 0           $sql =~ s/[\n\s]*-+SPLIT-+[\n\s]*/\n\n/mg;
95 0           print $sql;
96             }
97             }
98              
99             1;
100             __END__