| 1 | package SQL::Translator::Producer::GraphQL; | ||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 4 | 3 | ▶ | use GraphQL::Plugin::Convert::DBIC; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||
| 6 | 3 | our $VERSION = "0.05"; | |||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||
| 8 | 3 | my $dbic_schema_class_track = 'CLASS00000'; | |||||||||||||||||||||||
| 9 | 3 | ▶ | sub produce { | ||||||||||||||||||||||
|
Subroutine
produce: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||
| 10 | 3 | my $translator = shift; | |||||||||||||||||||||||
| 11 | 3 | my $schema = $translator->schema; | |||||||||||||||||||||||
| 12 | 3 | my $dbic_schema_class = ++$dbic_schema_class_track; | |||||||||||||||||||||||
| 13 | 3 | my $dbic_translator = bless { %$translator }, ref $translator; | |||||||||||||||||||||||
| 14 | 3 | $dbic_translator->producer_args({ prefix => $dbic_schema_class }); | |||||||||||||||||||||||
| 15 | 3 | my $perl = dbic_produce($dbic_translator); | |||||||||||||||||||||||
| 16 | 3 | ▶ | eval $perl; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 17 | 3 | ▶ | die "Failed to make DBIx::Class::Schema: $@" if $@; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 18 | 3 | my $converted = GraphQL::Plugin::Convert::DBIC->to_graphql($dbic_schema_class->connect); | |||||||||||||||||||||||
| 19 | 3 | $converted->{schema}->to_doc; | |||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||
| 22 | { | ||||||||||||||||||||||||
| 23 | # from SQL::Translator::Producer::DBIx::Class::File; | ||||||||||||||||||||||||
| 24 | 3 | ▶ | use SQL::Translator::Schema::Constants; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 25 | 3 | ▶ | use SQL::Translator::Utils qw(header_comment); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 26 | 3 | ▶ | use Data::Dumper (); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||
| 28 | ## Skip all column type translation, as we want to use whatever the parser got. | ||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||
| 30 | ## Translate parsers -> PK::Auto::Foo, however | ||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||
| 32 | 3 | my %parser2PK = ( | |||||||||||||||||||||||
| 33 | MySQL => 'PK::Auto::MySQL', | ||||||||||||||||||||||||
| 34 | PostgreSQL => 'PK::Auto::Pg', | ||||||||||||||||||||||||
| 35 | DB2 => 'PK::Auto::DB2', | ||||||||||||||||||||||||
| 36 | Oracle => 'PK::Auto::Oracle', | ||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||
| 39 | sub dbic_produce | ||||||||||||||||||||||||
| 40 | 3 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
dbic_produce: called CC=12 SCAR=25.0
Pod
undocumented
| |||||||||||||||||||||||||
| 41 | 3 | my ($translator) = @_; | |||||||||||||||||||||||
| 42 | 3 | my $no_comments = $translator->no_comments; | |||||||||||||||||||||||
| 43 | 3 | my $add_drop_table = $translator->add_drop_table; | |||||||||||||||||||||||
| 44 | 3 | my $schema = $translator->schema; | |||||||||||||||||||||||
| 45 | 3 | my $output = ''; | |||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||
| 47 | # Steal the XML producers "prefix" arg for our namespace? | ||||||||||||||||||||||||
| 48 | my $dbixschema = $translator->producer_args()->{prefix} || | ||||||||||||||||||||||||
| 49 | 3 | ▶ | $schema->name || 'My::Schema'; | ||||||||||||||||||||||
|
Condition40%2/5
$translator->producer_args->{'prefix'} || $schema->name || 'My::Schema'
$translator->producer_args->{"prefix"} or $schema->name
MC/DC0%0/2
$translator->producer_args->{'prefix'} || $schema->name || 'My::Schema'
$translator->producer_args->{"prefix"}
$schema->name
Truth table33%1/3
A or B
A= $translator->producer_args->{"prefix"}B= $schema->name
| |||||||||||||||||||||||||
| 50 | 3 | ▶ | my $pkclass = $parser2PK{$translator->parser_type} || ''; | ||||||||||||||||||||||
|
Condition50%1/2
$parser2PK{$translator->parser_type} || ''
MC/DC0%0/1
$parser2PK{$translator->parser_type} || ''
$parser2PK{$translator->parser_type}
Truth table50%1/2
A
A= $parser2PK{$translator->parser_type}
| |||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||
| 52 | 3 | my %tt_vars = (); | |||||||||||||||||||||||
| 53 | 3 | $tt_vars{dbixschema} = $dbixschema; | |||||||||||||||||||||||
| 54 | 3 | $tt_vars{pkclass} = $pkclass; | |||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||
| 56 | 3 | my $schemaoutput .= << "DATA"; | |||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||
| 58 | package ${dbixschema}; | ||||||||||||||||||||||||
| 59 | use base 'DBIx::Class::Schema'; | ||||||||||||||||||||||||
| 60 | use strict; | ||||||||||||||||||||||||
| 61 | use warnings; | ||||||||||||||||||||||||
| 62 | DATA | ||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||
| 64 | 3 | my %tableoutput = (); | |||||||||||||||||||||||
| 65 | 3 | my %tableextras = (); | |||||||||||||||||||||||
| 66 | 3 | foreach my $table ($schema->get_tables) | |||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||
| 68 | 10 | my $tname = $table->name; | |||||||||||||||||||||||
| 69 | 10 | my $output .= qq{ | |||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||
| 71 | package ${dbixschema}::${tname}; | ||||||||||||||||||||||||
| 72 | use base 'DBIx::Class'; | ||||||||||||||||||||||||
| 73 | use strict; | ||||||||||||||||||||||||
| 74 | use warnings; | ||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||
| 76 | __PACKAGE__->load_components(qw/${pkclass} Core/); | ||||||||||||||||||||||||
| 77 | __PACKAGE__->table('${tname}'); | ||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||
| 79 | }; | ||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||
| 81 | my @fields = map | ||||||||||||||||||||||||
| 82 | { | ||||||||||||||||||||||||
| 83 | 10 | { $_->name => { | |||||||||||||||||||||||
| 84 | name => $_->name, | ||||||||||||||||||||||||
| 85 | is_auto_increment => $_->is_auto_increment, | ||||||||||||||||||||||||
| 86 | is_foreign_key => $_->is_foreign_key, | ||||||||||||||||||||||||
| 87 | is_nullable => $_->is_nullable, | ||||||||||||||||||||||||
| 88 | default_value => $_->default_value, | ||||||||||||||||||||||||
| 89 | data_type => $_->data_type, | ||||||||||||||||||||||||
| 90 | size => $_->size, | ||||||||||||||||||||||||
| 91 | 71 | ▶ | ($_->{extra} ? (extra => $_->{extra}) : ()), | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 92 | } } | ||||||||||||||||||||||||
| 93 | } ($table->get_fields); | ||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||
| 95 | 10 | $output .= "\n__PACKAGE__->add_columns("; | |||||||||||||||||||||||
| 96 | 10 | foreach my $f (@fields) | |||||||||||||||||||||||
| 97 | { | ||||||||||||||||||||||||
| 98 | 71 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||
| 99 | 71 | $output .= "\n '" . (keys %$f)[0] . "' => " ; | |||||||||||||||||||||||
| 100 | 71 | my $colinfo = | |||||||||||||||||||||||
| 101 | Data::Dumper->Dump([values %$f], | ||||||||||||||||||||||||
| 102 | [''] # keys %$f] | ||||||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||||||
| 104 | 71 | chomp($colinfo); | |||||||||||||||||||||||
| 105 | 71 | $output .= $colinfo . ","; | |||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||
| 107 | 10 | $output .= "\n);\n"; | |||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||
| 109 | 10 | my $pk = $table->primary_key; | |||||||||||||||||||||||
| 110 | 10 | ▶ | if($pk) | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||
| 112 | 10 | my @pk = map { $_->name } ($pk->fields); | |||||||||||||||||||||||
| 113 | 10 | $output .= "__PACKAGE__->set_primary_key("; | |||||||||||||||||||||||
| 114 | 10 | $output .= "'" . join("', '", @pk) . "');\n"; | |||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||
| 117 | 10 | foreach my $cont ($table->get_constraints) | |||||||||||||||||||||||
| 118 | { | ||||||||||||||||||||||||
| 119 | # print Data::Dumper::Dumper($cont->type); | ||||||||||||||||||||||||
| 120 | 17 | ▶ | if($cont->type =~ /foreign key/i) | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 121 | { | ||||||||||||||||||||||||
| 122 | # $output .= "\n__PACKAGE__->belongs_to('" . | ||||||||||||||||||||||||
| 123 | # $cont->fields->[0]->name . "', '" . | ||||||||||||||||||||||||
| 124 | # "${dbixschema}::" . $cont->reference_table . "');\n"; | ||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||
| 126 | 5 | $tableextras{$table->name} .= "\n__PACKAGE__->belongs_to('" . | |||||||||||||||||||||||
| 127 | $cont->fields->[0]->name . "', '" . | ||||||||||||||||||||||||
| 128 | "${dbixschema}::" . $cont->reference_table . "');\n"; | ||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||
| 130 | 5 | my $other = "\n__PACKAGE__->has_many('" . | |||||||||||||||||||||||
| 131 | $table->name. "', '" . | ||||||||||||||||||||||||
| 132 | "${dbixschema}::" . $table->name. "', '" . | ||||||||||||||||||||||||
| 133 | $cont->fields->[0]->name . "');"; | ||||||||||||||||||||||||
| 134 | 5 | $tableextras{$cont->reference_table} .= $other; | |||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||
| 138 | 10 | $tableoutput{$table->name} .= $output; | |||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||
| 141 | 3 | foreach my $to (keys %tableoutput) | |||||||||||||||||||||||
| 142 | { | ||||||||||||||||||||||||
| 143 | 10 | $output .= $tableoutput{$to}; | |||||||||||||||||||||||
| 144 | 10 | $schemaoutput .= "\n__PACKAGE__->register_class('${to}', '${dbixschema}::${to}');\n"; | |||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||
| 147 | 3 | foreach my $te (keys %tableextras) | |||||||||||||||||||||||
| 148 | { | ||||||||||||||||||||||||
| 149 | 8 | $output .= "\npackage ${dbixschema}::$te;\n"; | |||||||||||||||||||||||
| 150 | 8 | $output .= $tableextras{$te} . "\n"; | |||||||||||||||||||||||
| 151 | # $tableoutput{$te} .= $tableextras{$te} . "\n"; | ||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||
| 154 | # print "$output\n"; | ||||||||||||||||||||||||
| 155 | 3 | return "${output}\n\n${schemaoutput}\n1;\n"; | |||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||
| 159 | =encoding utf-8 | ||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||
| 161 | =head1 NAME | ||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||
| 163 | SQL::Translator::Producer::GraphQL - GraphQL schema producer for SQL::Translator | ||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||
| 165 | =begin markdown | ||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||
| 167 | # PROJECT STATUS | ||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||
| 169 | | OS | Build status | | ||||||||||||||||||||||||
| 170 | |:-------:|--------------:| | ||||||||||||||||||||||||
| 171 | | Linux | [](https://travis-ci.org/graphql-perl/SQL-Translator-Producer-GraphQL) | | ||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||
| 173 | [](https://metacpan.org/pod/SQL::Translator::Producer::GraphQL) | ||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||
| 175 | =end markdown | ||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||
| 177 | =head1 SYNOPSIS | ||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||
| 179 | use SQL::Translator; | ||||||||||||||||||||||||
| 180 | use SQL::Translator::Producer::GraphQL; | ||||||||||||||||||||||||
| 181 | my $t = SQL::Translator->new( parser => '...' ); | ||||||||||||||||||||||||
| 182 | $t->producer('GraphQL'); | ||||||||||||||||||||||||
| 183 | $t->translate; | ||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||
| 185 | =head1 DESCRIPTION | ||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||
| 187 | This module will produce a L<GraphQL::Schema> from the given | ||||||||||||||||||||||||
| 188 | L<SQL::Translator::Schema>. It does this by first | ||||||||||||||||||||||||
| 189 | turning it into a L<DBIx::Class::Schema> using | ||||||||||||||||||||||||
| 190 | L<SQL::Translator::Producer::DBIx::Class::File>, then passing it to | ||||||||||||||||||||||||
| 191 | L<GraphQL::Plugin::Convert::DBIC/to_graphql>. | ||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||
| 193 | =head1 ARGUMENTS | ||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||
| 195 | Currently none. | ||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||
| 197 | =head1 DEBUGGING | ||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||
| 199 | To debug, set environment variable C<GRAPHQL_DEBUG> to a true value. | ||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||
| 201 | =head1 AUTHOR | ||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||
| 203 | Ed J, C<< <etj at cpan.org> >> | ||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||
| 205 | Based heavily on L<SQL::Translator::Producer::DBIxSchemaDSL>. | ||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||
| 207 | =head1 LICENSE | ||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||
| 209 | Copyright (C) Ed J | ||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||
| 211 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||
| 212 | it under the same terms as Perl itself. | ||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||
| 214 | =cut | ||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||
| 216 | 3 | 1; | |||||||||||||||||||||||