| 1 | package DBIO::MSSQL::Introspect::Columns; | ||||||||||||||||||||
| 2 | # ABSTRACT: Introspect MSSQL columns | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 0 | ▶ | sub fetch { | ||||||||||||||||||
|
Subroutine
fetch: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||||||||
| 10 | 0 | my ($class, $dbh, $schema, $tables) = @_; | |||||||||||||||||||
| 11 | 0 | ▶ | $schema //= 'dbo'; | ||||||||||||||||||
|
Condition0%0/2
$schema //= "dbo"
MC/DC0%0/1
$schema //= "dbo"
$schema
Truth table0%0/2
A
A= $schema
| |||||||||||||||||||||
| 12 | 0 | my %columns; | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | # Pull all columns for the schema in one go. | ||||||||||||||||||||
| 15 | 0 | my $col_sth = $dbh->prepare(q{ | |||||||||||||||||||
| 16 | SELECT table_name, column_name, data_type, | ||||||||||||||||||||
| 17 | is_nullable, column_default, ordinal_position, | ||||||||||||||||||||
| 18 | character_maximum_length | ||||||||||||||||||||
| 19 | FROM INFORMATION_SCHEMA.COLUMNS | ||||||||||||||||||||
| 20 | WHERE table_schema = ? | ||||||||||||||||||||
| 21 | ORDER BY table_name, ordinal_position | ||||||||||||||||||||
| 22 | }); | ||||||||||||||||||||
| 23 | 0 | $col_sth->execute($schema); | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 0 | ▶ | while (my $row = $col_sth->fetchrow_hashref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | 0 | ▶ | next unless exists $tables->{ $row->{table_name} }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | 0 | ▶ | push @{ $columns{ $row->{table_name} } }, { | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$row->{"is_nullable"} // "YES"
MC/DC0%0/1
$row->{"is_nullable"} // "YES"
$row->{"is_nullable"}
Truth table0%0/2
A
A= $row->{"is_nullable"}
| |||||||||||||||||||||
| 28 | column_name => $row->{column_name}, | ||||||||||||||||||||
| 29 | data_type => $row->{data_type}, | ||||||||||||||||||||
| 30 | not_null => (lc($row->{is_nullable} // 'YES') eq 'no') ? 1 : 0, | ||||||||||||||||||||
| 31 | default_value => $row->{column_default}, | ||||||||||||||||||||
| 32 | is_pk => 0, | ||||||||||||||||||||
| 33 | pk_position => 0, | ||||||||||||||||||||
| 34 | is_identity => 0, | ||||||||||||||||||||
| 35 | is_auto_increment => 0, | ||||||||||||||||||||
| 36 | size => $row->{character_maximum_length}, | ||||||||||||||||||||
| 37 | 0 | }; | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | # Primary-key membership. | ||||||||||||||||||||
| 41 | 0 | my $pk_sth = $dbh->prepare(q{ | |||||||||||||||||||
| 42 | SELECT kcu.table_name, kcu.column_name, kcu.ordinal_position | ||||||||||||||||||||
| 43 | FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc | ||||||||||||||||||||
| 44 | JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu | ||||||||||||||||||||
| 45 | ON tc.constraint_name = kcu.constraint_name | ||||||||||||||||||||
| 46 | AND tc.table_schema = kcu.table_schema | ||||||||||||||||||||
| 47 | AND tc.table_name = kcu.table_name | ||||||||||||||||||||
| 48 | WHERE tc.constraint_type = 'PRIMARY KEY' | ||||||||||||||||||||
| 49 | AND tc.table_schema = ? | ||||||||||||||||||||
| 50 | }); | ||||||||||||||||||||
| 51 | 0 | $pk_sth->execute($schema); | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 0 | ▶ | while (my $row = $pk_sth->fetchrow_hashref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 0 | ▶ | my $list = $columns{ $row->{table_name} } or next; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 0 | for my $col (@$list) { | |||||||||||||||||||
| 56 | 0 | ▶ | if ($col->{column_name} eq $row->{column_name}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 0 | $col->{is_pk} = 1; | |||||||||||||||||||
| 58 | 0 | ▶ | $col->{pk_position} = $row->{ordinal_position} || 1; | ||||||||||||||||||
|
Condition0%0/2
$row->{'ordinal_position'} || 1
MC/DC0%0/1
$row->{'ordinal_position'} || 1
$row->{'ordinal_position'}
Truth table0%0/2
A
A= $row->{'ordinal_position'}
| |||||||||||||||||||||
| 59 | 0 | last; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | # Identity columns (auto-increment). | ||||||||||||||||||||
| 65 | 0 | my $id_sth = $dbh->prepare(q{ | |||||||||||||||||||
| 66 | SELECT c.table_name, c.column_name | ||||||||||||||||||||
| 67 | FROM INFORMATION_SCHEMA.COLUMNS c | ||||||||||||||||||||
| 68 | JOIN sys.columns sc ON c.column_name = sc.name AND c.table_name = OBJECT_NAME(sc.object_id) | ||||||||||||||||||||
| 69 | WHERE c.table_schema = ? AND sc.is_identity = 1 | ||||||||||||||||||||
| 70 | }); | ||||||||||||||||||||
| 71 | 0 | $id_sth->execute($schema); | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | ▶ | while (my $row = $id_sth->fetchrow_hashref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 0 | ▶ | my $list = $columns{ $row->{table_name} } or next; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 0 | for my $col (@$list) { | |||||||||||||||||||
| 76 | 0 | ▶ | if ($col->{column_name} eq $row->{column_name}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 0 | $col->{is_identity} = 1; | |||||||||||||||||||
| 78 | 0 | $col->{is_auto_increment} = 1; | |||||||||||||||||||
| 79 | 0 | last; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | return \%columns; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 2 | 1; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | __END__ | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =pod | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | =encoding UTF-8 | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =head1 NAME | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | DBIO::MSSQL::Introspect::Columns - Introspect MSSQL columns | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =head1 VERSION | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | version 0.900001 | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | =head1 DESCRIPTION | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | Fetches column metadata via C<INFORMATION_SCHEMA.COLUMNS>. Primary key | ||||||||||||||||||||
| 106 | information is joined in via C<INFORMATION_SCHEMA.KEY_COLUMN_USAGE> + | ||||||||||||||||||||
| 107 | C<INFORMATION_SCHEMA.TABLE_CONSTRAINTS>. | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =head1 METHODS | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =head2 fetch | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | my $columns = DBIO::MSSQL::Introspect::Columns->fetch($dbh, $schema, $tables); | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | Given the tables hashref from L<DBIO::MSSQL::Introspect::Tables>, | ||||||||||||||||||||
| 116 | returns a hashref keyed by table name. Each value is an arrayref of | ||||||||||||||||||||
| 117 | column hashrefs in C<ordinal_position> order with keys: | ||||||||||||||||||||
| 118 | C<column_name>, C<data_type>, C<not_null>, C<default_value>, | ||||||||||||||||||||
| 119 | C<is_pk>, C<pk_position>, C<is_identity>, C<size>. | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | =head1 AUTHOR | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | DBIO & DBIx::Class Authors | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||
| 128 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||
| 129 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 132 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =cut | ||||||||||||||||||||