| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::From; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use App::DBBrowser::Auxil; | ||
| 9 | |||
| 10 | |||
| 11 | sub new { | ||
| 12 | my ( $class, $info, $options, $d ) = @_; | ||
| 13 | bless { | ||
| 14 | i => $info, | ||
| 15 | o => $options, | ||
| 16 | d => $d | ||
| 17 | }, $class; | ||
| 18 | } | ||
| 19 | |||
| 20 | |||
| 21 | sub from_sql { | ||
| 22 | my ( $sf, $table_key ) = @_; | ||
| 23 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 24 | my ( $table, $table_alias, $join_columns, $aliases ); | ||
| 25 | if ( $table_key eq 'Join' ) { | ||
| 26 | require App::DBBrowser::From::Join; | ||
| 27 | my $new_j = App::DBBrowser::From::Join->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 28 | $sf->{d}{table_origin} = 'join'; | ||
| 29 | if ( ! eval { ( $table, $join_columns, $aliases ) = $new_j->join_tables(); 1 } ) { | ||
| 30 | $ax->print_error_message( $@ ); | ||
| 31 | return; | ||
| 32 | } | ||
| 33 | return if ! defined $table; | ||
| 34 | } | ||
| 35 | elsif ( $table_key eq 'Union' ) { | ||
| 36 | require App::DBBrowser::From::Union; | ||
| 37 | my $new_u = App::DBBrowser::From::Union->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 38 | $sf->{d}{table_origin} = 'union'; | ||
| 39 | if ( ! eval { ( $table, $table_alias ) = $new_u->union_tables(); 1 } ) { | ||
| 40 | $ax->print_error_message( $@ ); | ||
| 41 | return; | ||
| 42 | } | ||
| 43 | return if ! defined $table; | ||
| 44 | } | ||
| 45 | elsif ( $table_key eq 'Subquery' ) { | ||
| 46 | require App::DBBrowser::From::Subquery; | ||
| 47 | my $sq = App::DBBrowser::From::Subquery->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 48 | $sf->{d}{table_origin} = 'subquery'; | ||
| 49 | if ( ! eval { ( $table, $table_alias ) = $sq->subquery_as_main_table(); 1 } ) { | ||
| 50 | $ax->print_error_message( $@ ); | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | return if ! defined $table; | ||
| 54 | } | ||
| 55 | elsif ( $table_key eq 'Cte' ) { | ||
| 56 | require App::DBBrowser::From::Cte; | ||
| 57 | my $cte = App::DBBrowser::From::Cte->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 58 | $sf->{d}{table_origin} = 'cte'; | ||
| 59 | if ( ! eval { $table = $cte->cte_as_main_table() ; 1 } ) { | ||
| 60 | $ax->print_error_message( $@ ); | ||
| 61 | return; | ||
| 62 | } | ||
| 63 | return if ! defined $table; | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | $sf->{d}{table_origin} = 'ordinary'; | ||
| 67 | if ( ! eval { ( $table, $table_alias ) = $sf->__ordinary_table( $table_key ); 1 } ) { | ||
| 68 | $ax->print_error_message( $@ ); | ||
| 69 | return; | ||
| 70 | } | ||
| 71 | return if ! defined $table; | ||
| 72 | } | ||
| 73 | if ( length $table_alias ) { | ||
| 74 | $table .= ' ' . $table_alias; | ||
| 75 | } | ||
| 76 | my ( $column_names, $column_types ) = $ax->column_names_and_types( $table ); | ||
| 77 | if ( ! defined $column_names ) { | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | my $sql = {}; | ||
| 81 | $ax->reset_sql( $sql ); | ||
| 82 | $sql->{table} = $table; | ||
| 83 | $sql->{table_alias} = $table_alias; | ||
| 84 | my $data_types = {}; | ||
| 85 | if ( $sf->{d}{table_origin} eq 'join' ) { | ||
| 86 | # join: use qualified column names. | ||
| 87 | $sql->{columns} = $join_columns; | ||
| 88 | @{$data_types}{@$join_columns} = @$column_types; | ||
| 89 | } | ||
| 90 | else { | ||
| 91 | $sql->{columns} = $column_names; | ||
| 92 | @{$data_types}{@$column_names} = @$column_types; | ||
| 93 | } | ||
| 94 | $sql->{data_types} = $data_types; | ||
| 95 | $sql->{alias} = $aliases // {}; | ||
| 96 | return $sql; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | sub __ordinary_table { | ||
| 101 | my ( $sf, $table_key ) = @_; | ||
| 102 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 103 | $sf->{d}{stmt_types} = [ 'Select' ]; | ||
| 104 | my $sql = { table => '()' }; | ||
| 105 | $ax->reset_sql( $sql ); | ||
| 106 | $ax->print_sql_info( $ax->get_sql_info( $sql ) ); ## | ||
| 107 | $sf->{d}{table_key} = $table_key; | ||
| 108 | my $table = $ax->qq_table( $sf->{d}{tables_info}{$table_key} ); | ||
| 109 | my $alias = $ax->table_alias( $sql, 'ordinary_table', $table ); | ||
| 110 | return $table, $alias; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | 1; | ||
| 116 | |||
| 117 | __END__ |