| 1 | package DBIx::Fast::Schema; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use v5.38; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Object::Pad 0.807; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||
| 6 | 4 | our $VERSION = '0.22'; | |||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Carp 'carp'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | class DBIx::Fast::Schema :isa(DBIx::Fast::Base) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | field $tables :reader = {}; | ||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | method _set_tables ($t) { $tables = $t } | ||||||||||||||||||||||||||||||||||
|
Subroutine
_set_tables: called CC=1 SCAR=0.0
Subroutine
tables: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | method _load_tables_name () { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_load_tables_name: called CC=6 SCAR=20.7
| |||||||||||||||||||||||||||||||||||||
| 15 | 1 | my $driver = $self->dbix->dbd; | |||||||||||||||||||||||||||||||||||
| 16 | 1 | my $sql = ''; | |||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | if ( $driver eq 'MariaDB' || $driver eq 'mysql' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%1/3
$driver eq "MariaDB" or $driver eq "mysql"
MC/DC0%0/2
$driver eq "MariaDB" or $driver eq "mysql"
$driver eq "MariaDB"
$driver eq "mysql"
Truth table33%1/3
A or B
A= $driver eq "MariaDB"B= $driver eq "mysql"
| |||||||||||||||||||||||||||||||||||||
| 19 | 0 | $sql = 'SHOW TABLES'; | |||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||
| 21 | elsif ( $driver eq 'SQLite' ) { | ||||||||||||||||||||||||||||||||||||
| 22 | 1 | $sql = "SELECT name FROM sqlite_master WHERE type='table'"; | |||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||
| 24 | elsif ( $driver eq 'Pg' ) { | ||||||||||||||||||||||||||||||||||||
| 25 | 0 | $sql = "SELECT tablename as name FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema'"; | |||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||
| 27 | else { | ||||||||||||||||||||||||||||||||||||
| 28 | 0 | $self->dbix->Exception("Error driver - _load_tables_name"); | |||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||
| 31 | # Hashref, not undef, so a database with zero tables leaves `tables` | ||||||||||||||||||||||||||||||||||||
| 32 | # honouring its documented contract (TableName gives a clean | ||||||||||||||||||||||||||||||||||||
| 33 | # "not exist" instead of an "undefined value as a HASH reference"). | ||||||||||||||||||||||||||||||||||||
| 34 | 1 | my $table_list = {}; | |||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||
| 36 | 1 | for my $table ( @{ $self->dbix->array($sql) } ) { | |||||||||||||||||||||||||||||||||||
| 37 | 2 | $table_list->{$table} = time(); | |||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||
| 40 | 1 | $tables = $table_list; | |||||||||||||||||||||||||||||||||||
| 41 | 4 | } | |||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | method clear_cache () { $tables = {} } | ||||||||||||||||||||||||||||||||||
|
Subroutine
clear_cache: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||
| 45 | 6 | ▶ | method get_indexes ($table) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
get_indexes: called CC=7 SCAR=21.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 46 | 6 | ▶ | $self->dbix->Exception("Table required") unless $table; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||
| 48 | 4 | my $driver = $self->dbix->dbd; | |||||||||||||||||||||||||||||||||||
| 49 | 4 | my $qtable = $self->dbix->_quote_ident_path($table); | |||||||||||||||||||||||||||||||||||
| 50 | 4 | my $sql = ''; | |||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||
| 52 | 4 | ▶ | if ( $driver eq 'MariaDB' || $driver eq 'mysql' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%1/3
$driver eq "MariaDB" or $driver eq "mysql"
MC/DC0%0/2
$driver eq "MariaDB" or $driver eq "mysql"
$driver eq "MariaDB"
$driver eq "mysql"
Truth table33%1/3
A or B
A= $driver eq "MariaDB"B= $driver eq "mysql"
| |||||||||||||||||||||||||||||||||||||
| 53 | 0 | $sql = "SHOW INDEX FROM $qtable"; | |||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||
| 55 | elsif ( $driver eq 'Pg' ) { | ||||||||||||||||||||||||||||||||||||
| 56 | 0 | $sql = qq{ | |||||||||||||||||||||||||||||||||||
| 57 | SELECT | ||||||||||||||||||||||||||||||||||||
| 58 | i.relname as index_name, | ||||||||||||||||||||||||||||||||||||
| 59 | a.attname as column_name, | ||||||||||||||||||||||||||||||||||||
| 60 | ix.indisunique as is_unique | ||||||||||||||||||||||||||||||||||||
| 61 | FROM | ||||||||||||||||||||||||||||||||||||
| 62 | pg_class t, | ||||||||||||||||||||||||||||||||||||
| 63 | pg_class i, | ||||||||||||||||||||||||||||||||||||
| 64 | pg_index ix, | ||||||||||||||||||||||||||||||||||||
| 65 | pg_attribute a | ||||||||||||||||||||||||||||||||||||
| 66 | WHERE | ||||||||||||||||||||||||||||||||||||
| 67 | t.oid = ix.indrelid | ||||||||||||||||||||||||||||||||||||
| 68 | AND i.oid = ix.indexrelid | ||||||||||||||||||||||||||||||||||||
| 69 | AND a.attrelid = t.oid | ||||||||||||||||||||||||||||||||||||
| 70 | AND a.attnum = ANY(ix.indkey) | ||||||||||||||||||||||||||||||||||||
| 71 | AND t.oid = to_regclass(?) | ||||||||||||||||||||||||||||||||||||
| 72 | }; | ||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||
| 74 | elsif ( $driver eq 'SQLite' ) { | ||||||||||||||||||||||||||||||||||||
| 75 | 4 | $sql = "SELECT * FROM sqlite_master WHERE type='index' AND tbl_name=?"; | |||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||
| 78 | # Route through all() so the query is tracked (profiler / last_sql). | ||||||||||||||||||||||||||||||||||||
| 79 | 4 | ▶ | my @bind = $driver =~ /^(Pg|SQLite)$/ ? ($table) : (); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 80 | 4 | return $self->dbix->all($sql, @bind); | |||||||||||||||||||||||||||||||||||
| 81 | 4 | } | |||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||
| 83 | # Deprecated alias | ||||||||||||||||||||||||||||||||||||
| 84 | 3 | ▶ | method get_indexs ($table) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
get_indexs: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 85 | 3 | state $warned; | |||||||||||||||||||||||||||||||||||
| 86 | 3 | ▶ | carp "DBIx::Fast::Schema::get_indexs is deprecated; use get_indexes" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 87 | unless $warned++; | ||||||||||||||||||||||||||||||||||||
| 88 | 3 | return $self->get_indexes($table); | |||||||||||||||||||||||||||||||||||
| 89 | 4 | } | |||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||
| 91 | 2 | ▶ | method primary_keys ($table) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
primary_keys: called CC=9 SCAR=25.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 92 | 2 | ▶ | $self->dbix->Exception("Table name required") unless $table; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||
| 94 | 1 | my $driver = $self->dbix->dbd; | |||||||||||||||||||||||||||||||||||
| 95 | 1 | my $qtable = $self->dbix->_quote_ident_path($table); | |||||||||||||||||||||||||||||||||||
| 96 | 1 | my $sql = ''; | |||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||
| 98 | 1 | ▶ | if ( $driver eq 'MariaDB' || $driver eq 'mysql' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%1/3
$driver eq "MariaDB" or $driver eq "mysql"
MC/DC0%0/2
$driver eq "MariaDB" or $driver eq "mysql"
$driver eq "MariaDB"
$driver eq "mysql"
Truth table33%1/3
A or B
A= $driver eq "MariaDB"B= $driver eq "mysql"
| |||||||||||||||||||||||||||||||||||||
| 99 | 0 | $sql = "SHOW KEYS FROM $qtable WHERE Key_name = 'PRIMARY'"; | |||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||
| 101 | elsif ( $driver eq 'Pg' ) { | ||||||||||||||||||||||||||||||||||||
| 102 | 0 | $sql = qq{ | |||||||||||||||||||||||||||||||||||
| 103 | SELECT a.attname | ||||||||||||||||||||||||||||||||||||
| 104 | FROM pg_index i | ||||||||||||||||||||||||||||||||||||
| 105 | JOIN pg_attribute a ON a.attrelid = i.indrelid | ||||||||||||||||||||||||||||||||||||
| 106 | AND a.attnum = ANY(i.indkey) | ||||||||||||||||||||||||||||||||||||
| 107 | WHERE i.indrelid = ?::regclass | ||||||||||||||||||||||||||||||||||||
| 108 | AND i.indisprimary | ||||||||||||||||||||||||||||||||||||
| 109 | }; | ||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||
| 111 | elsif ( $driver eq 'SQLite' ) { | ||||||||||||||||||||||||||||||||||||
| 112 | 1 | $sql = "PRAGMA table_info($qtable)"; | |||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||
| 115 | 1 | ▶ | my @bind = $driver eq 'Pg' ? ($table) : (); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 116 | 1 | my $rows = $self->dbix->all($sql, @bind); | |||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||
| 118 | 1 | my @keys; | |||||||||||||||||||||||||||||||||||
| 119 | 1 | ▶ | if ( $driver eq 'SQLite' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 120 | 1 | @keys = map { $_->{name} } grep { $_->{pk} } @$rows; | |||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||
| 122 | else { | ||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | @keys = map { $_->{Column_name} || $_->{attname} } @$rows; | ||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$_->{'Column_name'} || $_->{'attname'}
MC/DC0%0/2
$_->{'Column_name'} || $_->{'attname'}
$_->{'Column_name'}
$_->{'attname'}
Truth table0%0/3
A || B
A= $_->{'Column_name'}B= $_->{'attname'}
| |||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||
| 126 | 1 | return \@keys; | |||||||||||||||||||||||||||||||||||
| 127 | 4 | } | |||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||
| 129 | 4 | ▶ | method table_info ($table = undef) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
table_info: called CC=8 SCAR=22.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 130 | 4 | ▶ | $self->dbix->Exception("Table name required") unless $table; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||
| 132 | 2 | my $driver = $self->dbix->dbd; | |||||||||||||||||||||||||||||||||||
| 133 | 2 | my $qtable = $self->dbix->_quote_ident_path($table); | |||||||||||||||||||||||||||||||||||
| 134 | 2 | my $sql = ''; | |||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||
| 136 | 2 | ▶ | if ( $driver eq 'MariaDB' || $driver eq 'mysql' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%1/3
$driver eq "MariaDB" or $driver eq "mysql"
MC/DC0%0/2
$driver eq "MariaDB" or $driver eq "mysql"
$driver eq "MariaDB"
$driver eq "mysql"
Truth table33%1/3
A or B
A= $driver eq "MariaDB"B= $driver eq "mysql"
| |||||||||||||||||||||||||||||||||||||
| 137 | 0 | $sql = "SHOW CREATE TABLE $qtable"; | |||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||
| 139 | elsif ( $driver eq 'Pg' ) { | ||||||||||||||||||||||||||||||||||||
| 140 | # Resolve the (optionally schema-qualified) name through the | ||||||||||||||||||||||||||||||||||||
| 141 | # search_path like the server does; matching on table_name alone | ||||||||||||||||||||||||||||||||||||
| 142 | # merged same-named tables from different schemas. | ||||||||||||||||||||||||||||||||||||
| 143 | 0 | $sql = qq{ | |||||||||||||||||||||||||||||||||||
| 144 | SELECT c.column_name, c.data_type, c.character_maximum_length | ||||||||||||||||||||||||||||||||||||
| 145 | FROM information_schema.columns c | ||||||||||||||||||||||||||||||||||||
| 146 | JOIN pg_class k ON k.relname = c.table_name | ||||||||||||||||||||||||||||||||||||
| 147 | JOIN pg_namespace n ON n.oid = k.relnamespace | ||||||||||||||||||||||||||||||||||||
| 148 | AND n.nspname = c.table_schema | ||||||||||||||||||||||||||||||||||||
| 149 | WHERE k.oid = to_regclass(?) | ||||||||||||||||||||||||||||||||||||
| 150 | ORDER BY c.ordinal_position | ||||||||||||||||||||||||||||||||||||
| 151 | }; | ||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||
| 153 | elsif ( $driver eq 'SQLite' ) { | ||||||||||||||||||||||||||||||||||||
| 154 | 2 | $sql = "PRAGMA table_info($qtable)"; | |||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||
| 157 | 2 | my @bind; | |||||||||||||||||||||||||||||||||||
| 158 | 2 | ▶ | push @bind, $table if $driver eq 'Pg'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||
| 160 | 2 | return $self->dbix->all($sql, @bind); | |||||||||||||||||||||||||||||||||||
| 161 | 4 | } | |||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||
| 163 | 2 | ▶ | method table_size ($table) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
table_size: called CC=6 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 164 | 2 | ▶ | $self->dbix->Exception("Table name required") unless $table; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||
| 166 | 1 | my $driver = $self->dbix->dbd; | |||||||||||||||||||||||||||||||||||
| 167 | 1 | my $qtable = $self->dbix->_quote_ident_path($table); | |||||||||||||||||||||||||||||||||||
| 168 | 1 | my $sql = ''; | |||||||||||||||||||||||||||||||||||
| 169 | 1 | my @params; | |||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||
| 171 | 1 | ▶ | if ( $driver eq 'MariaDB' || $driver eq 'mysql' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%1/3
$driver eq "MariaDB" or $driver eq "mysql"
MC/DC0%0/2
$driver eq "MariaDB" or $driver eq "mysql"
$driver eq "MariaDB"
$driver eq "mysql"
Truth table33%1/3
A or B
A= $driver eq "MariaDB"B= $driver eq "mysql"
| |||||||||||||||||||||||||||||||||||||
| 172 | 0 | $sql = "SELECT table_rows as `rows`, data_length + index_length as size_bytes FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = ?"; | |||||||||||||||||||||||||||||||||||
| 173 | 0 | @params = ($table); | |||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||
| 175 | elsif ( $driver eq 'Pg' ) { | ||||||||||||||||||||||||||||||||||||
| 176 | 0 | $sql = q{SELECT reltuples::bigint as rows, pg_total_relation_size(?) as size_bytes FROM pg_class WHERE relname = ?}; | |||||||||||||||||||||||||||||||||||
| 177 | 0 | @params = ( $table, $table ); | |||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||
| 179 | elsif ( $driver eq 'SQLite' ) { | ||||||||||||||||||||||||||||||||||||
| 180 | 1 | $sql = "SELECT count(*) as rows FROM $qtable"; | |||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||
| 183 | 1 | return $self->dbix->hash($sql, @params); | |||||||||||||||||||||||||||||||||||
| 184 | 4 | } | |||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||
| 187 | 4 | 1; | |||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||
| 189 | __END__ | ||||||||||||||||||||||||||||||||||||