| 1 | package Daje::Workflow::GenerateSchema::Create::Schema; | ||
| 2 | use Mojo::Base -base, -signatures; | ||
| 3 | |||
| 4 | # NAME | ||
| 5 | # | ||
| 6 | # Daje::Workflow::GenerateSchema::Create::Schema | ||
| 7 | # | ||
| 8 | # | ||
| 9 | # DESCRIPTION | ||
| 10 | # =========== | ||
| 11 | # Daje::Workflow::GenerateSchema::Create::Schema creates a json representation of a postgres database | ||
| 12 | # | ||
| 13 | # | ||
| 14 | # | ||
| 15 | # | ||
| 16 | # REQUIRES | ||
| 17 | # | ||
| 18 | # Syntax::Operator::Matches | ||
| 19 | # | ||
| 20 | # Scalar::Util | ||
| 21 | # | ||
| 22 | # Mojo::Base | ||
| 23 | # | ||
| 24 | # | ||
| 25 | # METHODS | ||
| 26 | # | ||
| 27 | # build_view_methods($self,) | ||
| 28 | # | ||
| 29 | # get_db_schema($self) | ||
| 30 | # | ||
| 31 | # | ||
| 32 | |||
| 33 | # | ||
| 34 | # LICENSE | ||
| 35 | # ======= | ||
| 36 | # | ||
| 37 | # Copyright (C) janeskil1525. | ||
| 38 | # | ||
| 39 | # This library is free software; you can redistribute it and/or modify | ||
| 40 | # it under the same terms as Perl itself. | ||
| 41 | # | ||
| 42 | # AUTHOR | ||
| 43 | # ====== | ||
| 44 | # janeskil1525 E<lt>janeskil1525@gmail.comE<gt> | ||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | use Scalar::Util qw {reftype}; | ||
| 49 | use Syntax::Operator::Matches qw( matches mismatches ); | ||
| 50 | |||
| 51 | has 'db'; | ||
| 52 | has 'excludes'; | ||
| 53 | |||
| 54 | sub get_db_schema($self, $schema) { | ||
| 55 | $schema = 'public' unless $schema; | ||
| 56 | my @tab; | ||
| 57 | my @vie; | ||
| 58 | my @tables = (); | ||
| 59 | @tables = $self->_get_tables($schema); | ||
| 60 | my $length = scalar @tables; | ||
| 61 | for (my $i = 0; $i < $length; $i++) { | ||
| 62 | my $table->{table}->{table_name} = $tables[$i]->{table_name}; | ||
| 63 | my $column_names = $self->_get_table_column_names($tables[$i]->{table_name}, $schema); | ||
| 64 | $table->{table}->{column_names} = $column_names; | ||
| 65 | my $indexes = $self->_get_table_indexes($tables[$i]->{table_name}, $schema); | ||
| 66 | if (defined $indexes) { | ||
| 67 | $table->{table}->{indexes} = $indexes; | ||
| 68 | } | ||
| 69 | push (@tab, $table); | ||
| 70 | my $temp = 1; | ||
| 71 | } | ||
| 72 | my $result->{tables} = \@tab; | ||
| 73 | my @views = $self->_get_views($schema); | ||
| 74 | $length = scalar @views; | ||
| 75 | for (my $i = 0; $i < $length; $i++ ) { | ||
| 76 | my $view->{view} = $views[$i]; | ||
| 77 | my $column_names = $self->_get_table_column_names($view->{view}->{table_name}, $schema); | ||
| 78 | $view->{view}->{column_names} = $column_names; | ||
| 79 | $view->{view}->{keys} = $self->_get_keys($column_names); | ||
| 80 | push (@vie, $view); | ||
| 81 | } | ||
| 82 | |||
| 83 | $result->{views} = \@vie; | ||
| 84 | return $result; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub build_view_methods($self, $view, $column_names) { | ||
| 88 | |||
| 89 | my $methods->{table_name} = $view->{table_name}; | ||
| 90 | $methods->{keys} = $self->_get_keys($column_names); | ||
| 91 | $methods->{create_endpoint} = 1; | ||
| 92 | my $method = $self->get_view_list($view->{table_name},$column_names); | ||
| 93 | push @{$methods->{methods}}, $method ; | ||
| 94 | |||
| 95 | return $methods; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub _get_keys($self, $column_names) { | ||
| 99 | |||
| 100 | my $keys->{has_companies} = 0; | ||
| 101 | $keys->{has_users} = 0; | ||
| 102 | $keys->{fk} = (); | ||
| 103 | my $length = scalar @{$column_names}; | ||
| 104 | for (my $i = 0; $i < $length; $i++ ) { | ||
| 105 | if (length(@{$column_names}[$i]->{column_name}) > 0) { | ||
| 106 | if (index(@{$column_names}[$i]->{column_name},'_pkey') > -1){ | ||
| 107 | $keys->{pk} = @{$column_names}[$i]->{column_name}; | ||
| 108 | } elsif (@{$column_names}[$i]->{column_name} eq 'companies_fkey') { | ||
| 109 | $keys->{has_companies} = 1; | ||
| 110 | } elsif (@{$column_names}[$i]->{column_name} eq 'users_fkey') { | ||
| 111 | $keys->{has_users} = 1; | ||
| 112 | } elsif (index(@{$column_names}[$i]->{column_name},'_fkey') > -1) { | ||
| 113 | push @{$keys->{fk}}, @{$column_names}[$i]->{column_name}; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | } | ||
| 117 | return $keys; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _get_tables($self, $schema) { | ||
| 121 | |||
| 122 | my $excludes = $self->excludes(); | ||
| 123 | my @tables = $self->db->query(qq { | ||
| 124 | SELECT table_name | ||
| 125 | FROM information_schema.tables | ||
| 126 | WHERE table_schema = ? AND table_name NOT IN(?) | ||
| 127 | AND table_type='BASE TABLE'; | ||
| 128 | },($schema, $excludes))->hashes; | ||
| 129 | @tables = @{ $tables[0] }; | ||
| 130 | |||
| 131 | return @tables; | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | sub _get_views($self, $schema) { | ||
| 136 | |||
| 137 | my @views = $self->db->query(qq { | ||
| 138 | SELECT table_name | ||
| 139 | FROM | ||
| 140 | information_schema.views | ||
| 141 | WHERE | ||
| 142 | table_schema NOT IN ( | ||
| 143 | 'information_schema', 'pg_catalog' | ||
| 144 | ) AND table_schema = ? | ||
| 145 | ORDER BY table_name; | ||
| 146 | },($schema))->hashes; | ||
| 147 | @views = @{$views[0]}; | ||
| 148 | |||
| 149 | return @views; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub _get_table_column_names($self, $table, $schema) { | ||
| 153 | |||
| 154 | $schema = 'public' unless $schema; | ||
| 155 | my @column_names = $self->db->query( | ||
| 156 | qq{ | ||
| 157 | SELECT column_name | ||
| 158 | FROM information_schema.columns | ||
| 159 | WHERE table_schema = ? | ||
| 160 | AND table_name = ? | ||
| 161 | }, ($schema, $table) | ||
| 162 | )->hashes; | ||
| 163 | |||
| 164 | @column_names = @{ $column_names[0] } if @{ $column_names[0] }; | ||
| 165 | return \@column_names; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub _get_table_indexes($self, $table, $schema) { | ||
| 169 | $schema = 'public' unless $schema; | ||
| 170 | my @column_names = $self->db->query( | ||
| 171 | qq{ | ||
| 172 | select | ||
| 173 | t.relname as table_name, | ||
| 174 | i.relname as index_name, | ||
| 175 | a.attname as column_name, | ||
| 176 | insp.nspname as index_schema | ||
| 177 | from | ||
| 178 | pg_class t, | ||
| 179 | pg_class i, | ||
| 180 | pg_index ix, | ||
| 181 | pg_attribute a, | ||
| 182 | pg_namespace insp | ||
| 183 | where | ||
| 184 | t.oid = ix.indrelid | ||
| 185 | and i.oid = ix.indexrelid | ||
| 186 | and a.attrelid = t.oid | ||
| 187 | and a.attnum = ANY(ix.indkey) | ||
| 188 | and t.relkind = 'r' | ||
| 189 | and ix.indisunique | ||
| 190 | and ix.indisprimary = false | ||
| 191 | and t.relname not like 'pg_%' | ||
| 192 | and insp.oid = i.relnamespace | ||
| 193 | and insp.nspname = ? | ||
| 194 | AND t.relname = ? | ||
| 195 | order by | ||
| 196 | t.relname, | ||
| 197 | i.relname; | ||
| 198 | }, ($schema, $table) | ||
| 199 | )->hashes; | ||
| 200 | |||
| 201 | @column_names = @{ $column_names[0] } if @{ $column_names[0] }; | ||
| 202 | return \@column_names; | ||
| 203 | } | ||
| 204 | |||
| 205 | 1; | ||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 | #################### pod generated by Pod::Autopod - keep this line to make pod updates possible #################### | ||
| 210 | |||
| 211 | =head1 NAME | ||
| 212 | |||
| 213 | Daje::Workflow::GenerateSchema::Create::Schema | ||
| 214 | |||
| 215 | |||
| 216 | =head1 DESCRIPTION | ||
| 217 | |||
| 218 | NAME | ||
| 219 | |||
| 220 | Daje::Workflow::GenerateSchema::Create::Schema | ||
| 221 | |||
| 222 | |||
| 223 | Daje::Workflow::GenerateSchema::Create::Schema creates a json representation of a postgres database | ||
| 224 | |||
| 225 | |||
| 226 | |||
| 227 | |||
| 228 | REQUIRES | ||
| 229 | |||
| 230 | Syntax::Operator::Matches | ||
| 231 | |||
| 232 | Scalar::Util | ||
| 233 | |||
| 234 | Mojo::Base | ||
| 235 | |||
| 236 | |||
| 237 | METHODS | ||
| 238 | |||
| 239 | build_view_methods($self,) | ||
| 240 | |||
| 241 | get_db_schema($self) | ||
| 242 | |||
| 243 | |||
| 244 | |||
| 245 | |||
| 246 | =head1 REQUIRES | ||
| 247 | |||
| 248 | L<Syntax::Operator::Matches> | ||
| 249 | |||
| 250 | L<Scalar::Util> | ||
| 251 | |||
| 252 | L<Mojo::Base> | ||
| 253 | |||
| 254 | |||
| 255 | =head1 METHODS | ||
| 256 | |||
| 257 | =head2 build_view_methods($self, | ||
| 258 | |||
| 259 | build_view_methods($self,(); | ||
| 260 | |||
| 261 | =head2 get_db_schema($self, | ||
| 262 | |||
| 263 | get_db_schema($self,(); | ||
| 264 | |||
| 265 | |||
| 266 | =cut | ||
| 267 |