line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SQL::Translator::Producer::GraphQL; |
2
|
3
|
|
|
3
|
|
737539
|
use strict; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
107
|
|
3
|
3
|
|
|
3
|
|
19
|
use warnings; |
|
3
|
|
|
|
|
30
|
|
|
3
|
|
|
|
|
149
|
|
4
|
3
|
|
|
3
|
|
413
|
use GraphQL::Plugin::Convert::DBIC; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
554
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = "0.05"; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
my $dbic_schema_class_track = 'CLASS00000'; |
9
|
|
|
|
|
|
|
sub produce { |
10
|
3
|
|
|
3
|
0
|
1321931
|
my $translator = shift; |
11
|
3
|
|
|
|
|
60
|
my $schema = $translator->schema; |
12
|
3
|
|
|
|
|
146
|
my $dbic_schema_class = ++$dbic_schema_class_track; |
13
|
3
|
|
|
|
|
79
|
my $dbic_translator = bless { %$translator }, ref $translator; |
14
|
3
|
|
|
|
|
71
|
$dbic_translator->producer_args({ prefix => $dbic_schema_class }); |
15
|
3
|
|
|
|
|
159
|
my $perl = dbic_produce($dbic_translator); |
16
|
3
|
|
|
2
|
|
463
|
eval $perl; |
|
2
|
|
|
2
|
|
22
|
|
|
2
|
|
|
2
|
|
5
|
|
|
2
|
|
|
2
|
|
760
|
|
|
2
|
|
|
2
|
|
16789
|
|
|
2
|
|
|
2
|
|
5
|
|
|
2
|
|
|
2
|
|
44
|
|
|
2
|
|
|
2
|
|
9
|
|
|
2
|
|
|
2
|
|
4
|
|
|
2
|
|
|
2
|
|
395
|
|
|
2
|
|
|
2
|
|
15
|
|
|
2
|
|
|
2
|
|
5
|
|
|
2
|
|
|
2
|
|
177
|
|
|
2
|
|
|
2
|
|
12
|
|
|
2
|
|
|
2
|
|
5
|
|
|
2
|
|
|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: $@" if $@; |
18
|
3
|
|
|
|
|
33
|
my $converted = GraphQL::Plugin::Convert::DBIC->to_graphql($dbic_schema_class->connect); |
19
|
3
|
|
|
|
|
850512
|
$converted->{schema}->to_doc; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
{ |
23
|
|
|
|
|
|
|
# from SQL::Translator::Producer::DBIx::Class::File; |
24
|
3
|
|
|
3
|
|
1024
|
use SQL::Translator::Schema::Constants; |
|
3
|
|
|
|
|
801
|
|
|
3
|
|
|
|
|
287
|
|
25
|
3
|
|
|
3
|
|
1368
|
use SQL::Translator::Utils qw(header_comment); |
|
3
|
|
|
|
|
6411
|
|
|
3
|
|
|
|
|
158
|
|
26
|
3
|
|
|
3
|
|
649
|
use Data::Dumper (); |
|
3
|
|
|
|
|
6780
|
|
|
3
|
|
|
|
|
2046
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
{ |
41
|
3
|
|
|
3
|
0
|
9
|
my ($translator) = @_; |
42
|
3
|
|
|
|
|
58
|
my $no_comments = $translator->no_comments; |
43
|
3
|
|
|
|
|
79
|
my $add_drop_table = $translator->add_drop_table; |
44
|
3
|
|
|
|
|
67
|
my $schema = $translator->schema; |
45
|
3
|
|
|
|
|
120
|
my $output = ''; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# Steal the XML producers "prefix" arg for our namespace? |
48
|
|
|
|
|
|
|
my $dbixschema = $translator->producer_args()->{prefix} || |
49
|
3
|
|
50
|
|
|
49
|
$schema->name || 'My::Schema'; |
50
|
3
|
|
50
|
|
|
142
|
my $pkclass = $parser2PK{$translator->parser_type} || ''; |
51
|
|
|
|
|
|
|
|
52
|
3
|
|
|
|
|
101
|
my %tt_vars = (); |
53
|
3
|
|
|
|
|
11
|
$tt_vars{dbixschema} = $dbixschema; |
54
|
3
|
|
|
|
|
9
|
$tt_vars{pkclass} = $pkclass; |
55
|
|
|
|
|
|
|
|
56
|
3
|
|
|
|
|
13
|
my $schemaoutput .= << "DATA"; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
package ${dbixschema}; |
59
|
|
|
|
|
|
|
use base 'DBIx::Class::Schema'; |
60
|
|
|
|
|
|
|
use strict; |
61
|
|
|
|
|
|
|
use warnings; |
62
|
|
|
|
|
|
|
DATA |
63
|
|
|
|
|
|
|
|
64
|
3
|
|
|
|
|
8
|
my %tableoutput = (); |
65
|
3
|
|
|
|
|
7
|
my %tableextras = (); |
66
|
3
|
|
|
|
|
19
|
foreach my $table ($schema->get_tables) |
67
|
|
|
|
|
|
|
{ |
68
|
10
|
|
|
|
|
1458
|
my $tname = $table->name; |
69
|
10
|
|
|
|
|
1059
|
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
|
|
|
|
|
37
|
{ $_->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
|
50
|
|
|
|
86588
|
($_->{extra} ? (extra => $_->{extra}) : ()), |
92
|
|
|
|
|
|
|
} } |
93
|
|
|
|
|
|
|
} ($table->get_fields); |
94
|
|
|
|
|
|
|
|
95
|
10
|
|
|
|
|
17016
|
$output .= "\n__PACKAGE__->add_columns("; |
96
|
10
|
|
|
|
|
30
|
foreach my $f (@fields) |
97
|
|
|
|
|
|
|
{ |
98
|
71
|
|
|
|
|
115
|
local $Data::Dumper::Terse = 1; |
99
|
71
|
|
|
|
|
239
|
$output .= "\n '" . (keys %$f)[0] . "' => " ; |
100
|
71
|
|
|
|
|
279
|
my $colinfo = |
101
|
|
|
|
|
|
|
Data::Dumper->Dump([values %$f], |
102
|
|
|
|
|
|
|
[''] # keys %$f] |
103
|
|
|
|
|
|
|
); |
104
|
71
|
|
|
|
|
4542
|
chomp($colinfo); |
105
|
71
|
|
|
|
|
206
|
$output .= $colinfo . ","; |
106
|
|
|
|
|
|
|
} |
107
|
10
|
|
|
|
|
22
|
$output .= "\n);\n"; |
108
|
|
|
|
|
|
|
|
109
|
10
|
|
|
|
|
37
|
my $pk = $table->primary_key; |
110
|
10
|
50
|
|
|
|
1832
|
if($pk) |
111
|
|
|
|
|
|
|
{ |
112
|
10
|
|
|
|
|
35
|
my @pk = map { $_->name } ($pk->fields); |
|
10
|
|
|
|
|
3539
|
|
113
|
10
|
|
|
|
|
1007
|
$output .= "__PACKAGE__->set_primary_key("; |
114
|
10
|
|
|
|
|
45
|
$output .= "'" . join("', '", @pk) . "');\n"; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
10
|
|
|
|
|
35
|
foreach my $cont ($table->get_constraints) |
118
|
|
|
|
|
|
|
{ |
119
|
|
|
|
|
|
|
# print Data::Dumper::Dumper($cont->type); |
120
|
17
|
100
|
|
|
|
1069
|
if($cont->type =~ /foreign key/i) |
121
|
|
|
|
|
|
|
{ |
122
|
|
|
|
|
|
|
# $output .= "\n__PACKAGE__->belongs_to('" . |
123
|
|
|
|
|
|
|
# $cont->fields->[0]->name . "', '" . |
124
|
|
|
|
|
|
|
# "${dbixschema}::" . $cont->reference_table . "');\n"; |
125
|
|
|
|
|
|
|
|
126
|
5
|
|
|
|
|
514
|
$tableextras{$table->name} .= "\n__PACKAGE__->belongs_to('" . |
127
|
|
|
|
|
|
|
$cont->fields->[0]->name . "', '" . |
128
|
|
|
|
|
|
|
"${dbixschema}::" . $cont->reference_table . "');\n"; |
129
|
|
|
|
|
|
|
|
130
|
5
|
|
|
|
|
2770
|
my $other = "\n__PACKAGE__->has_many('" . |
131
|
|
|
|
|
|
|
$table->name. "', '" . |
132
|
|
|
|
|
|
|
"${dbixschema}::" . $table->name. "', '" . |
133
|
|
|
|
|
|
|
$cont->fields->[0]->name . "');"; |
134
|
5
|
|
|
|
|
3133
|
$tableextras{$cont->reference_table} .= $other; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
10
|
|
|
|
|
724
|
$tableoutput{$table->name} .= $output; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
3
|
|
|
|
|
324
|
foreach my $to (keys %tableoutput) |
142
|
|
|
|
|
|
|
{ |
143
|
10
|
|
|
|
|
60
|
$output .= $tableoutput{$to}; |
144
|
10
|
|
|
|
|
39
|
$schemaoutput .= "\n__PACKAGE__->register_class('${to}', '${dbixschema}::${to}');\n"; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
3
|
|
|
|
|
10
|
foreach my $te (keys %tableextras) |
148
|
|
|
|
|
|
|
{ |
149
|
8
|
|
|
|
|
33
|
$output .= "\npackage ${dbixschema}::$te;\n"; |
150
|
8
|
|
|
|
|
24
|
$output .= $tableextras{$te} . "\n"; |
151
|
|
|
|
|
|
|
# $tableoutput{$te} .= $tableextras{$te} . "\n"; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# print "$output\n"; |
155
|
3
|
|
|
|
|
195
|
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 | [![Build Status](https://travis-ci.org/graphql-perl/SQL-Translator-Producer-GraphQL.svg?branch=master)](https://travis-ci.org/graphql-perl/SQL-Translator-Producer-GraphQL) | |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
[![CPAN version](https://badge.fury.io/pl/SQL-Translator-Producer-GraphQL.svg)](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
|
|
|
|
|
|
|
1; |