| 1 | package oEdtk::SchemaScan; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Driver-neutral schema introspection, shared by tools/schema_snapshot.pl (the | ||||||||||||||||||||||||||||
| 4 | # golden snapshots) and oEdtk::DBAdmin::schema_Detect (step S51a of the | ||||||||||||||||||||||||||||
| 5 | # fork-integration plan, decision D16). | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # This module loads no oEdtk code on purpose: the snapshot tool must stay free of | ||||||||||||||||||||||||||||
| 8 | # the oEdtk banner that oEdtk.pm prints at load time (it would corrupt the JSON it | ||||||||||||||||||||||||||||
| 9 | # writes). It returns a plain data structure -- per table, the ordered columns | ||||||||||||||||||||||||||||
| 10 | # (name, declared type and size, nullability, default, position, key rank) and the | ||||||||||||||||||||||||||||
| 11 | # secondary indexes. Pure ASCII. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | our @EXPORT_OK = qw( scan_schema driver_of size_from_type ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | our $VERSION = 2.1126; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # The driver name from a DSN, in the tool's vocabulary (undef when unsupported). | ||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub driver_of { | ||||||||||||||||||||||||||
|
Subroutine
driver_of: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 0 | my ($dsn) = @_; | |||||||||||||||||||||||||||
| 24 | 0 | ▶ | return 'Pg' if $dsn =~ /^dbi:Pg:/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | return 'mysql' if $dsn =~ /^dbi:mysql:/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | return 'SQLite' if $dsn =~ /^dbi:SQLite:/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | return; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # The driver name of a live handle, normalised to the same vocabulary. | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub _driver_of_dbh { | ||||||||||||||||||||||||||
|
Subroutine
_driver_of_dbh: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 32 | 0 | my ($dbh) = @_; | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | my $name = $dbh->{Driver}{Name} // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$dbh->{"Driver"}{"Name"} // ""
MC/DC0%0/1
$dbh->{"Driver"}{"Name"} // ""
$dbh->{"Driver"}{"Name"}
Truth table0%0/2
A
A= $dbh->{"Driver"}{"Name"}
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | return 'Pg' if $name =~ /^pg$/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | return 'mysql' if $name =~ /mysql/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | return 'SQLite' if $name =~ /sqlite/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | return $name; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # The declared size of a type such as "VARCHAR2(36)" or "varchar(36)"; undef when | ||||||||||||||||||||||||||||
| 41 | # the type carries no length (INTEGER, CHAR, ...). | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub size_from_type { | ||||||||||||||||||||||||||
|
Subroutine
size_from_type: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 0 | my ($type) = @_; | |||||||||||||||||||||||||||
| 44 | 0 | my $size; | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | $size = $1 if defined $type && $type =~ /\(\s*(\d+)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $type and $type =~ /\(\s*(\d+)/
MC/DC0%0/2
defined $type and $type =~ /\(\s*(\d+)/
defined $type
$type =~ /\(\s*(\d+)/
Truth table0%0/3
A and B
A= defined $typeB= $type =~ /\(\s*(\d+)/
| |||||||||||||||||||||||||||||
| 46 | 0 | return $size; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 50 | # Per-driver introspection: each returns { table => { columns => [...], | ||||||||||||||||||||||||||||
| 51 | # indexes => [...] } }, columns ordered by position. | ||||||||||||||||||||||||||||
| 52 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub _scan_pg { | ||||||||||||||||||||||||||
|
Subroutine
_scan_pg: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||
| 54 | 0 | my ($dbh) = @_; | |||||||||||||||||||||||||||
| 55 | 0 | my %tables; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | my $cols = $dbh->prepare( | |||||||||||||||||||||||||||
| 58 | "SELECT table_name, column_name, data_type, character_maximum_length, | ||||||||||||||||||||||||||||
| 59 | numeric_precision, is_nullable, column_default, ordinal_position | ||||||||||||||||||||||||||||
| 60 | FROM information_schema.columns | ||||||||||||||||||||||||||||
| 61 | WHERE table_schema NOT IN ('pg_catalog','information_schema') | ||||||||||||||||||||||||||||
| 62 | ORDER BY table_name, ordinal_position" | ||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | 0 | $cols->execute; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | while ( my $r = $cols->fetchrow_hashref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | my $size = $r->{character_maximum_length}; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | $size = $r->{numeric_precision} if !defined $size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | push @{ $tables{ $r->{table_name} }{columns} }, | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 69 | { | ||||||||||||||||||||||||||||
| 70 | name => $r->{column_name}, | ||||||||||||||||||||||||||||
| 71 | type => $r->{data_type}, | ||||||||||||||||||||||||||||
| 72 | size => ( defined $size ? 0 + $size : undef ), | ||||||||||||||||||||||||||||
| 73 | nullable => ( $r->{is_nullable} eq 'YES' ? 1 : 0 ), | ||||||||||||||||||||||||||||
| 74 | default => $r->{column_default}, | ||||||||||||||||||||||||||||
| 75 | pos => 0 + $r->{ordinal_position}, | ||||||||||||||||||||||||||||
| 76 | 0 | pk => 0, | |||||||||||||||||||||||||||
| 77 | }; | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my $pk = $dbh->prepare( | |||||||||||||||||||||||||||
| 81 | "SELECT tc.table_name, kcu.column_name, kcu.ordinal_position | ||||||||||||||||||||||||||||
| 82 | FROM information_schema.table_constraints tc | ||||||||||||||||||||||||||||
| 83 | JOIN information_schema.key_column_usage kcu | ||||||||||||||||||||||||||||
| 84 | ON tc.constraint_name = kcu.constraint_name | ||||||||||||||||||||||||||||
| 85 | AND tc.table_schema = kcu.table_schema | ||||||||||||||||||||||||||||
| 86 | WHERE tc.constraint_type = 'PRIMARY KEY'" | ||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | 0 | $pk->execute; | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | while ( my $r = $pk->fetchrow_hashref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | my $t = $tables{ $r->{table_name} } or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | for my $c ( @{ $t->{columns} } ) { | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | $c->{pk} = 0 + $r->{ordinal_position} if $c->{name} eq $r->{column_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | my $idx = $dbh->prepare( | |||||||||||||||||||||||||||
| 97 | "SELECT t.relname AS table_name, i.relname AS index_name, | ||||||||||||||||||||||||||||
| 98 | ix.indisunique AS is_unique, ix.indisprimary AS is_primary, | ||||||||||||||||||||||||||||
| 99 | a.attname AS column_name, k.ord AS ord | ||||||||||||||||||||||||||||
| 100 | FROM pg_index ix | ||||||||||||||||||||||||||||
| 101 | JOIN pg_class i ON i.oid = ix.indexrelid | ||||||||||||||||||||||||||||
| 102 | JOIN pg_class t ON t.oid = ix.indrelid | ||||||||||||||||||||||||||||
| 103 | JOIN pg_namespace n ON n.oid = t.relnamespace | ||||||||||||||||||||||||||||
| 104 | JOIN LATERAL unnest(ix.indkey) WITH ORDINALITY AS k(attnum, ord) ON true | ||||||||||||||||||||||||||||
| 105 | JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum | ||||||||||||||||||||||||||||
| 106 | WHERE n.nspname NOT IN ('pg_catalog','information_schema') | ||||||||||||||||||||||||||||
| 107 | AND NOT ix.indisprimary | ||||||||||||||||||||||||||||
| 108 | ORDER BY t.relname, i.relname, k.ord" | ||||||||||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||||||||||
| 110 | 0 | $idx->execute; | |||||||||||||||||||||||||||
| 111 | 0 | my %seen; | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | while ( my $r = $idx->fetchrow_hashref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | my $key = "$r->{table_name}\0$r->{index_name}"; | |||||||||||||||||||||||||||
| 114 | 0 | ▶ | if ( !$seen{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | $seen{$key} = 1; | |||||||||||||||||||||||||||
| 116 | 0 | push @{ $tables{ $r->{table_name} }{indexes} }, | |||||||||||||||||||||||||||
| 117 | 0 | { name => $r->{index_name}, unique => 0 + $r->{is_unique}, columns => [] }; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 0 | push @{ $tables{ $r->{table_name} }{indexes}[-1]{columns} }, $r->{column_name}; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | return \%tables; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub _scan_mysql { | ||||||||||||||||||||||||||
|
Subroutine
_scan_mysql: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||
| 126 | 0 | my ($dbh) = @_; | |||||||||||||||||||||||||||
| 127 | 0 | my %tables; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | my $cols = $dbh->prepare( | |||||||||||||||||||||||||||
| 130 | "SELECT table_name, column_name, data_type, character_maximum_length, | ||||||||||||||||||||||||||||
| 131 | numeric_precision, is_nullable, column_default, ordinal_position | ||||||||||||||||||||||||||||
| 132 | FROM information_schema.columns | ||||||||||||||||||||||||||||
| 133 | WHERE table_schema = DATABASE() | ||||||||||||||||||||||||||||
| 134 | ORDER BY table_name, ordinal_position" | ||||||||||||||||||||||||||||
| 135 | ); | ||||||||||||||||||||||||||||
| 136 | 0 | $cols->execute; | |||||||||||||||||||||||||||
| 137 | 0 | ▶ | while ( my $r = $cols->fetchrow_hashref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | my $size = $r->{character_maximum_length}; | |||||||||||||||||||||||||||
| 139 | 0 | ▶ | $size = $r->{numeric_precision} if !defined $size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | push @{ $tables{ $r->{table_name} }{columns} }, | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 141 | { | ||||||||||||||||||||||||||||
| 142 | name => $r->{column_name}, | ||||||||||||||||||||||||||||
| 143 | type => $r->{data_type}, | ||||||||||||||||||||||||||||
| 144 | size => ( defined $size ? 0 + $size : undef ), | ||||||||||||||||||||||||||||
| 145 | nullable => ( $r->{is_nullable} eq 'YES' ? 1 : 0 ), | ||||||||||||||||||||||||||||
| 146 | default => $r->{column_default}, | ||||||||||||||||||||||||||||
| 147 | pos => 0 + $r->{ordinal_position}, | ||||||||||||||||||||||||||||
| 148 | 0 | pk => 0, | |||||||||||||||||||||||||||
| 149 | }; | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | my $stat = $dbh->prepare( | |||||||||||||||||||||||||||
| 153 | "SELECT table_name, index_name, non_unique, column_name, seq_in_index | ||||||||||||||||||||||||||||
| 154 | FROM information_schema.statistics | ||||||||||||||||||||||||||||
| 155 | WHERE table_schema = DATABASE() | ||||||||||||||||||||||||||||
| 156 | ORDER BY table_name, index_name, seq_in_index" | ||||||||||||||||||||||||||||
| 157 | ); | ||||||||||||||||||||||||||||
| 158 | 0 | $stat->execute; | |||||||||||||||||||||||||||
| 159 | 0 | my %seen; | |||||||||||||||||||||||||||
| 160 | 0 | ▶ | while ( my $r = $stat->fetchrow_hashref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | if ( $r->{index_name} eq 'PRIMARY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | my $t = $tables{ $r->{table_name} } or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | for my $c ( @{ $t->{columns} } ) { | |||||||||||||||||||||||||||
| 164 | 0 | ▶ | $c->{pk} = 0 + $r->{seq_in_index} if $c->{name} eq $r->{column_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 0 | next; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 0 | my $key = "$r->{table_name}\0$r->{index_name}"; | |||||||||||||||||||||||||||
| 169 | 0 | ▶ | if ( !$seen{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 0 | $seen{$key} = 1; | |||||||||||||||||||||||||||
| 171 | 0 | ▶ | push @{ $tables{ $r->{table_name} }{indexes} }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | { | ||||||||||||||||||||||||||||
| 173 | name => $r->{index_name}, | ||||||||||||||||||||||||||||
| 174 | 0 | unique => ( $r->{non_unique} ? 0 : 1 ), | |||||||||||||||||||||||||||
| 175 | columns => [], | ||||||||||||||||||||||||||||
| 176 | }; | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 0 | push @{ $tables{ $r->{table_name} }{indexes}[-1]{columns} }, $r->{column_name}; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | return \%tables; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | sub _scan_sqlite { | ||||||||||||||||||||||||||
|
Subroutine
_scan_sqlite: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 185 | 0 | my ($dbh) = @_; | |||||||||||||||||||||||||||
| 186 | 0 | my %tables; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | my @names = map { $_->[0] } @{ | |||||||||||||||||||||||||||
| 189 | 0 | $dbh->selectall_arrayref( | |||||||||||||||||||||||||||
| 190 | "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name" | ||||||||||||||||||||||||||||
| 191 | ) | ||||||||||||||||||||||||||||
| 192 | }; | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | for my $tbl (@names) { | |||||||||||||||||||||||||||
| 195 | 0 | my $ti = $dbh->selectall_arrayref("PRAGMA table_info('$tbl')"); | |||||||||||||||||||||||||||
| 196 | 0 | my @cols; | |||||||||||||||||||||||||||
| 197 | 0 | for my $c (@$ti) { | |||||||||||||||||||||||||||
| 198 | 0 | my ( $cid, $name, $type, $notnull, $dflt, $pk ) = @$c; | |||||||||||||||||||||||||||
| 199 | 0 | ▶ | push @cols, | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$pk // 0
MC/DC0%0/1
$pk // 0
$pk
Truth table0%0/2
A
A= $pk
| |||||||||||||||||||||||||||||
| 200 | { | ||||||||||||||||||||||||||||
| 201 | name => $name, | ||||||||||||||||||||||||||||
| 202 | type => $type, | ||||||||||||||||||||||||||||
| 203 | size => size_from_type($type), | ||||||||||||||||||||||||||||
| 204 | nullable => ( $notnull ? 0 : 1 ), | ||||||||||||||||||||||||||||
| 205 | default => $dflt, | ||||||||||||||||||||||||||||
| 206 | pos => 0 + $cid + 1, | ||||||||||||||||||||||||||||
| 207 | pk => 0 + ( $pk // 0 ), | ||||||||||||||||||||||||||||
| 208 | }; | ||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | 0 | $tables{$tbl}{columns} = \@cols; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 0 | my @idxs; | |||||||||||||||||||||||||||
| 213 | 0 | for my $ix ( @{ $dbh->selectall_arrayref("PRAGMA index_list('$tbl')") } ) { | |||||||||||||||||||||||||||
| 214 | 0 | my ( $seq, $iname, $unique ) = @$ix; | |||||||||||||||||||||||||||
| 215 | 0 | my @ic = map { $_->[2] } @{ $dbh->selectall_arrayref("PRAGMA index_info('$iname')") }; | |||||||||||||||||||||||||||
| 216 | 0 | ▶ | push @idxs, { name => $iname, unique => ( $unique ? 1 : 0 ), columns => \@ic }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | 0 | ▶ | $tables{$tbl}{indexes} = \@idxs if @idxs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 0 | return \%tables; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | # Introspect a live handle. Returns { driver => ..., tables => { ... } }. | ||||||||||||||||||||||||||||
| 225 | 0 | ▶ | sub scan_schema { | ||||||||||||||||||||||||||
|
Subroutine
scan_schema: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 226 | 0 | my ($dbh) = @_; | |||||||||||||||||||||||||||
| 227 | 0 | my $driver = _driver_of_dbh($dbh); | |||||||||||||||||||||||||||
| 228 | 0 | ▶ | my $tables = | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 229 | $driver eq 'Pg' ? _scan_pg($dbh) | ||||||||||||||||||||||||||||
| 230 | : $driver eq 'mysql' ? _scan_mysql($dbh) | ||||||||||||||||||||||||||||
| 231 | : $driver eq 'SQLite' ? _scan_sqlite($dbh) | ||||||||||||||||||||||||||||
| 232 | : die "scan_schema: unsupported driver '$driver'\n"; | ||||||||||||||||||||||||||||
| 233 | 0 | return { driver => $driver, tables => $tables }; | |||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 1 | 1; | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | =head1 NAME | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | oEdtk::SchemaScan - driver-neutral database schema introspection | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | use oEdtk::SchemaScan qw(scan_schema); | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | my $schema = scan_schema($dbh); | ||||||||||||||||||||||||||||
| 247 | # { driver => 'Pg', tables => { 'T' => { columns => [...], indexes => [...] } } } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | Introspects a live database handle and returns a plain data structure: for each | ||||||||||||||||||||||||||||
| 252 | table, the ordered columns (name, declared type and size, nullability, default, | ||||||||||||||||||||||||||||
| 253 | position, key rank) and the secondary indexes. It supports PostgreSQL, | ||||||||||||||||||||||||||||
| 254 | MySQL/MariaDB and SQLite. It loads no oEdtk code on purpose, so a snapshot tool | ||||||||||||||||||||||||||||
| 255 | can use it without the load banner. It is shared by C<tools/schema_snapshot.pl> | ||||||||||||||||||||||||||||
| 256 | and C<oEdtk::DBAdmin>'s schema detection. | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | =head2 scan_schema | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | my $schema = scan_schema($dbh); | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | Introspect C<$dbh> and return a hash reference with the C<driver> and C<tables> | ||||||||||||||||||||||||||||
| 265 | keys. An unsupported driver is an error. | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =head2 driver_of | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | my $driver = driver_of($dsn); | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | Return the tool's driver name (C<Pg>, C<mysql> or C<SQLite>) for a DSN, or undef | ||||||||||||||||||||||||||||
| 272 | when the DSN is not supported. | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =head2 size_from_type | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | my $size = size_from_type($type); | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | Return the declared length of a type such as C<VARCHAR2(36)>, or undef when the | ||||||||||||||||||||||||||||
| 279 | type carries no length. | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | The per-driver scanners C<_scan_pg>, C<_scan_mysql> and C<_scan_sqlite>, and the | ||||||||||||||||||||||||||||
| 282 | C<_driver_of_dbh> helper, are internal and are not part of the public API. | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | The oEdtk team. | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =cut | ||||||||||||||||||||||||||||