blib/lib/DBIx/QuickORM/Command/GenerateSQL.pm

filter: Statementstmt 100% 53 / 53 Branchbran 100% 24 / 24 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 95.5% 86 / 90 CC 15 SCAR 23.2
CC
15
Cov
98%
run
14.0
_usage
2.0
BEGIN
1.0
CRAP 10.2
« blib/lib/DBIx/QuickORM/Role/LookupTable.pm ↑ Summary blib/lib/DBIx/QuickORM/ORM.pm »
1 package DBIx::QuickORM::Command::GenerateSQL;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.000033';
6  
7 1 ▶use Getopt::Yath;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =pod
10  
11 =encoding UTF-8
12  
13 =head1 NAME
14  
15 DBIx::QuickORM::Command::GenerateSQL - The qorm-generate-sql command: print an ORM class's schema as SQL.
16  
17 =head1 DESCRIPTION
18  
19 What C<qorm-generate-sql> runs. It loads an ORM class and prints the SQL its
20 schema definitions create, through C<build_sql> (see
21 L<DBIx::QuickORM::ORM> and L<DBIx::QuickORM::Manual::SchemaBuilding>), with
22 no database.
23  
24 =head1 SYNOPSIS
25  
26 =for highlighter language=shell
27  
28 qorm-generate-sql -I lib --pretty --dialect postgresql My::App > schema.sql
29  
30 =for highlighter language=perl
31  
32 From Perl:
33  
34 my $exit = DBIx::QuickORM::Command::GenerateSQL->run('--pretty', '--dialect', 'postgresql', 'My::App');
35  
36 =head1 OPTIONS
37  
38 =cut
39  
40 1 ▶option_group {category => 'Options', group => 'generate'} => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
41 1 option dialect => (
42 type => 'Scalar',
43 short => 'd',
44 description => "The database to write SQL for: postgresql, mysql, mariadb, sqlite, or duckdb. Required.",
45 );
46  
47 1 option version => (
48 type => 'Scalar',
49 short => 'V',
50 description => "The server version to write for, such as 17 or 10.6. The newest is assumed without one.",
51 );
52  
53 1 option table => (
54 type => 'List',
55 short => 't',
56 description => "Write only this table or view. Repeat for more.",
57 );
58  
59 1 option pretty => (
60 type => 'Bool',
61 short => 'p',
62 description => "Write each table's columns and constraints on lines of their own, with blank lines between tables.",
63 );
64  
65 1 option include => (
66 type => 'List',
67 short => 'I',
68 description => "Add a directory to the module search path, as perl -I does. Repeat for more.",
69 );
70  
71 1 option help => (
72 type => 'Bool',
73 short => 'h',
74 description => "Print this help and exit.",
75 );
76 1 };
77  
78 =pod
79  
80 =over 4
81  
82 =item --dialect NAME, -d NAME
83  
84 The database to write SQL for: C<postgresql>, C<mysql>, C<mariadb>,
85 C<sqlite>, or C<duckdb>. Required.
86  
87 =item --version VERSION, -V VERSION
88  
89 The server version to write for, such as C<17> or C<10.6>. Without one, the
90 newest is assumed.
91  
92 =item --table NAME, -t NAME
93  
94 Write only this table or view. Repeat for more. The settings, extensions,
95 SQL from the ORM's C<schema> block, and the named enums the tables use are
96 still written.
97  
98 =item --pretty, -p
99  
100 Lay the SQL out to read: each table's columns on lines of their own, lined
101 up in columns (name, type, default, C<NOT NULL>, the rest), the key and the
102 table constraints set apart by blank lines, and a blank line between the
103 statements before the tables, each table, each view, and the SQL after.
104  
105 =item --include DIR, -I DIR
106  
107 Add a directory to the module search path, as C<perl -I> does. Repeat for
108 more.
109  
110 =item --help, -h
111  
112 Print the usage and exit.
113  
114 =back
115  
116 =head1 PUBLIC METHODS
117  
118 =over 4
119  
120 =item $exit = DBIx::QuickORM::Command::GenerateSQL->run(@args)
121  
122 A class method that runs the command on the command-line arguments given:
123 prints the SQL and returns the exit status.
124  
125 # the users table (and what goes around the tables), for PostgreSQL 13
126 my $exit = DBIx::QuickORM::Command::GenerateSQL->run(
127 '--pretty',
128 '--dialect', 'postgresql',
129 '--version', '13',
130 '--table', 'users',
131 'My::App',
132 );
133  
134 C<@args> is the command line: options and the ORM class name. The SQL goes
135 to standard output. The exit status is 0 on success, 1 for an error (printed
136 to standard error), and 2 for bad usage.
137  
138 =back
139  
140 =cut
141  
142 15 ▶sub run {
Subroutine run: called CC=14 SCAR=26.4
Pod documented
143 15 my $class = shift;
144 15 my @args = @_;
145  
146 15 ▶ my $parsed = eval { parse_options([@args], skip_non_opts => 1) } or return $class->_usage("$@");
Branch: 1 branch
BranchTrueFalseTotal
my $parsed = eval { do { parse_options([@args], "skip_non_opts", 1) } } or return $class->_usage("$@") 14 1 15
147 14 my $settings = $parsed->settings->generate;
148 14 ▶ return $class->_usage() if $settings->help;
Branch: 1 branch
BranchTrueFalseTotal
if $settings->help 1 13 14
149  
150 13 my @classes = @{$parsed->skipped};
151 13 ▶ return $class->_usage("Give one ORM class, such as My::App") unless @classes == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @classes == 1 2 11 13
152 11 ▶ return $class->_usage("--dialect is required") unless defined $settings->dialect;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $settings->dialect 1 10 11
153 10 my ($orm) = @classes;
154 10 ▶ return $class->_usage("'$orm' is not a package name") unless $orm =~ m/^\w+(?:::\w+)*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $orm =~ /^\w+(?:::\w+)*\z/ 1 9 10
155  
156 9 my $sql = eval {
157 9 ▶ local @INC = (@{$settings->include // []}, @INC);
Condition50%1/2
$settings->include // []
l!l
90
MC/DC0%0/1
$settings->include // []
$settings->include
Truth table50%1/2
A
A= $settings->include
Aresult
11
00
158 9 (my $file = "$orm.pm") =~ s{::}{/}g;
159 9 ▶ require $file unless $orm->can('build_sql');
Branch: 1 branch
BranchTrueFalseTotal
unless $orm->can("build_sql") 2 7 9
160 8 ▶ die "$orm is not a DBIx::QuickORM ORM class\n" unless $orm->isa('DBIx::QuickORM::ORM');
Branch: 1 branch
BranchTrueFalseTotal
unless $orm->isa("DBIx::QuickORM::ORM") 1 7 8
161 scalar $orm->build_sql(
162 dialect => $settings->dialect,
163 (defined($settings->version) ? (version => $settings->version) : ()),
164 7 ▶ (@{$settings->table // []} ? (tables => [@{$settings->table}]) : ()),
Branch: 3 branches
BranchTrueFalseTotal
defined $settings->version ? : 3 4 7
@{$settings->table // [];} ? : 1 6 7
$settings->pretty ? : 1 6 7
Condition50%1/2
$settings->table // []
l!l
70
MC/DC0%0/1
$settings->table // []
$settings->table
Truth table50%1/2
A
A= $settings->table
Aresult
11
00
165 pretty => $settings->pretty ? 1 : 0,
166 );
167 };
168 9 ▶ unless (defined $sql) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sql) 3 6 9
169 3 print STDERR $@;
170 3 return 1;
171 }
172  
173 6 print $sql;
174 6 return 0;
175 }
176  
177 =head1 PRIVATE METHODS
178  
179 =over 4
180  
181 =item $exit = $class->_usage($error)
182  
183 Prints C<$error>, if any, and the usage to standard error (standard output
184 for C<--help>), and returns 2 for an error, 0 for help.
185  
186 =back
187  
188 =cut
189  
190 6 ▶sub _usage {
Subroutine _usage: called CC=2 SCAR=6.9
191 6 my $class = shift;
192 6 my ($error) = @_;
193 6 my $usage = "Usage: qorm-generate-sql [options] ORM::Class > schema.sql\n\n" . options()->docs('cli');
194 6 ▶ if (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 5 1 6
195 5 chomp $error;
196 5 print STDERR "$error\n\n$usage";
197 5 return 2;
198 }
199 1 print $usage;
200 1 return 0;
201 }
202  
203 1 1;
204  
205 __END__
« blib/lib/DBIx/QuickORM/Role/LookupTable.pm ↑ Summary blib/lib/DBIx/QuickORM/ORM.pm »