| 1 | package DBIO::MySQL::Introspect::Columns; | ||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect MySQL/MariaDB columns | ||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||
| 7 | 4 | ▶ | use DBIO::MySQL::Introspect::Util (); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||
| 11 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||
|
Subroutine
fetch: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||
| 12 | 0 | my ($class, $dbh, $tables) = @_; | |||||||||||||||||||||||
| 13 | 0 | my %columns; | |||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||
| 15 | # On MySQL 8 the information_schema result-column labels come back UPPERCASE | ||||||||||||||||||||||||
| 16 | # regardless of the case written in the SELECT, while MariaDB returns them | ||||||||||||||||||||||||
| 17 | # lowercase. Force lowercase hash keys for this fetch so the $row->{lc} reads | ||||||||||||||||||||||||
| 18 | # below resolve on both engines. The sth captures FetchHashKeyName from the | ||||||||||||||||||||||||
| 19 | # dbh at prepare time, so it must be set before prepare (Tables.pm sidesteps | ||||||||||||||||||||||||
| 20 | # this by aliasing every column, which echoes the alias case verbatim). | ||||||||||||||||||||||||
| 21 | 0 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; | |||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||
| 23 | 0 | my $sth = $dbh->prepare(q{ | |||||||||||||||||||||||
| 24 | SELECT | ||||||||||||||||||||||||
| 25 | table_name, | ||||||||||||||||||||||||
| 26 | column_name, | ||||||||||||||||||||||||
| 27 | ordinal_position, | ||||||||||||||||||||||||
| 28 | data_type, | ||||||||||||||||||||||||
| 29 | column_type, | ||||||||||||||||||||||||
| 30 | is_nullable, | ||||||||||||||||||||||||
| 31 | column_default, | ||||||||||||||||||||||||
| 32 | column_key, | ||||||||||||||||||||||||
| 33 | extra, | ||||||||||||||||||||||||
| 34 | character_set_name, | ||||||||||||||||||||||||
| 35 | collation_name, | ||||||||||||||||||||||||
| 36 | column_comment | ||||||||||||||||||||||||
| 37 | FROM information_schema.columns | ||||||||||||||||||||||||
| 38 | WHERE table_schema = DATABASE() | ||||||||||||||||||||||||
| 39 | ORDER BY table_name, ordinal_position | ||||||||||||||||||||||||
| 40 | }); | ||||||||||||||||||||||||
| 41 | 0 | $sth->execute; | |||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||
| 43 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 44 | 0 | ▶ | next unless DBIO::MySQL::Introspect::Util->keep_table($tables, $row->{table_name}); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||
| 46 | 0 | ▶ | my $extra = $row->{extra} // ''; | ||||||||||||||||||||||
|
Condition0%0/2
$row->{"extra"} // ""
MC/DC0%0/1
$row->{"extra"} // ""
$row->{"extra"}
Truth table0%0/2
A
A= $row->{"extra"}
| |||||||||||||||||||||||||
| 47 | 0 | ▶ | my $auto = ($extra =~ /\bauto_increment\b/i) ? 1 : 0; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 48 | 0 | ▶ | my $is_pk = ($row->{column_key} // '') eq 'PRI' ? 1 : 0; | ||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$row->{"column_key"} // ""
MC/DC0%0/1
$row->{"column_key"} // ""
$row->{"column_key"}
Truth table0%0/2
A
A= $row->{"column_key"}
| |||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||
| 50 | 0 | ▶ | push @{ $columns{ $row->{table_name} } }, { | ||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/2
$row->{"is_nullable"} // ""
MC/DC0%0/1
$row->{"is_nullable"} // ""
$row->{"is_nullable"}
Truth table0%0/2
A
A= $row->{"is_nullable"}
| |||||||||||||||||||||||||
| 51 | column_name => $row->{column_name}, | ||||||||||||||||||||||||
| 52 | data_type => $row->{data_type}, | ||||||||||||||||||||||||
| 53 | column_type => $row->{column_type}, | ||||||||||||||||||||||||
| 54 | # `size` is the canonical companion to `data_type`: extracted from | ||||||||||||||||||||||||
| 55 | # `column_type`'s parenthesised number (varchar(100) -> 100, | ||||||||||||||||||||||||
| 56 | # int(11) -> 11, decimal(10,2) -> 10). DBIO::Introspect::Base | ||||||||||||||||||||||||
| 57 | # relies on this to size columns it builds. | ||||||||||||||||||||||||
| 58 | size => DBIO::MySQL::Introspect::Util->column_size($row->{column_type}), | ||||||||||||||||||||||||
| 59 | not_null => (uc($row->{is_nullable} // '') eq 'NO') ? 1 : 0, | ||||||||||||||||||||||||
| 60 | default_value => $row->{column_default}, | ||||||||||||||||||||||||
| 61 | is_auto_increment => $auto, | ||||||||||||||||||||||||
| 62 | is_pk => $is_pk, | ||||||||||||||||||||||||
| 63 | # `pk_position` lets DBIO::Introspect::Base::table_pk_info sort | ||||||||||||||||||||||||
| 64 | # composite primary keys in the right order. Single-column PKs | ||||||||||||||||||||||||
| 65 | # always get position 1. | ||||||||||||||||||||||||
| 66 | pk_position => $is_pk ? $row->{ordinal_position} : undef, | ||||||||||||||||||||||||
| 67 | character_set => $row->{character_set_name}, | ||||||||||||||||||||||||
| 68 | collation => $row->{collation_name}, | ||||||||||||||||||||||||
| 69 | comment => $row->{column_comment}, | ||||||||||||||||||||||||
| 70 | 0 | extra => $extra, | |||||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||
| 74 | 0 | return \%columns; | |||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||
| 77 | 4 | 1; | |||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||
| 79 | __END__ | ||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||
| 81 | =pod | ||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||
| 83 | =encoding UTF-8 | ||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||
| 85 | =head1 NAME | ||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||
| 87 | DBIO::MySQL::Introspect::Columns - Introspect MySQL/MariaDB columns | ||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||
| 89 | =head1 VERSION | ||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||
| 91 | version 0.900001 | ||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||
| 93 | =head1 DESCRIPTION | ||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||
| 95 | Fetches column metadata from C<information_schema.columns>, scoped to the | ||||||||||||||||||||||||
| 96 | current C<DATABASE()> and filtered to the tables surfaced by | ||||||||||||||||||||||||
| 97 | L<DBIO::MySQL::Introspect::Tables>. The C<column_type> size parser and the | ||||||||||||||||||||||||
| 98 | C<$tables> filter are the shared helpers in | ||||||||||||||||||||||||
| 99 | L<DBIO::MySQL::Introspect::Util>. | ||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||
| 101 | =head1 METHODS | ||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||
| 103 | =head2 fetch | ||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||
| 105 | my $columns = DBIO::MySQL::Introspect::Columns->fetch($dbh, $tables); | ||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||
| 107 | Given the tables hashref from L<DBIO::MySQL::Introspect::Tables>, returns a | ||||||||||||||||||||||||
| 108 | hashref keyed by table name. Each value is an arrayref of column hashrefs | ||||||||||||||||||||||||
| 109 | (in C<ordinal_position> order) with keys: C<column_name>, C<data_type>, | ||||||||||||||||||||||||
| 110 | C<column_type>, C<size>, C<not_null>, C<default_value>, C<is_auto_increment>, | ||||||||||||||||||||||||
| 111 | C<is_pk>, C<pk_position>, C<character_set>, C<collation>, C<comment>, | ||||||||||||||||||||||||
| 112 | C<extra>. | ||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||
| 114 | =head1 AUTHOR | ||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||
| 116 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||
| 118 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||
| 120 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||
| 121 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||
| 122 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||
| 124 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||
| 125 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||