| 1 | package DBIx::Util::Schema; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 4 | 2 | our $DATE = '2020-05-06'; # DATE | |||||||||||||||||||||||||||
| 5 | 2 | our $DIST = 'DBIx-Util-Schema'; # DIST | |||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.002'; # VERSION | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use strict 'subs', 'vars'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Log::ger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use List::Util qw(first); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 17 | table_exists | ||||||||||||||||||||||||||||
| 18 | has_table | ||||||||||||||||||||||||||||
| 19 | has_all_tables | ||||||||||||||||||||||||||||
| 20 | has_any_table | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | column_exists | ||||||||||||||||||||||||||||
| 23 | has_column | ||||||||||||||||||||||||||||
| 24 | has_all_columns | ||||||||||||||||||||||||||||
| 25 | has_any_column | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | list_tables | ||||||||||||||||||||||||||||
| 28 | list_columns | ||||||||||||||||||||||||||||
| 29 | list_indexes | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # TODO: | ||||||||||||||||||||||||||||
| 33 | # primary_key_columns | ||||||||||||||||||||||||||||
| 34 | # has_primary_key | ||||||||||||||||||||||||||||
| 35 | # has_index_on (has_index?) | ||||||||||||||||||||||||||||
| 36 | # has_unique_index_on | ||||||||||||||||||||||||||||
| 37 | # has_a_unique_index | ||||||||||||||||||||||||||||
| 38 | # | ||||||||||||||||||||||||||||
| 39 | # has_foreign_key | ||||||||||||||||||||||||||||
| 40 | # | ||||||||||||||||||||||||||||
| 41 | # is_null_column | ||||||||||||||||||||||||||||
| 42 | # is_not_null_column | ||||||||||||||||||||||||||||
| 43 | # is_unique_column | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2 | our %SPEC; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2 | $SPEC{':package'} = { | |||||||||||||||||||||||||||
| 48 | v => 1.1, | ||||||||||||||||||||||||||||
| 49 | summary => 'Utility routines related to database schema', | ||||||||||||||||||||||||||||
| 50 | }; | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 2 | our %arg0_dbh = ( | |||||||||||||||||||||||||||
| 53 | dbh => { | ||||||||||||||||||||||||||||
| 54 | schema => ['obj*'], | ||||||||||||||||||||||||||||
| 55 | summary => 'DBI database handle', | ||||||||||||||||||||||||||||
| 56 | req => 1, | ||||||||||||||||||||||||||||
| 57 | pos => 0, | ||||||||||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | our %arg1_table = ( | |||||||||||||||||||||||||||
| 62 | table => { | ||||||||||||||||||||||||||||
| 63 | schema => ['str*'], | ||||||||||||||||||||||||||||
| 64 | summary => 'Table name', | ||||||||||||||||||||||||||||
| 65 | req => 1, | ||||||||||||||||||||||||||||
| 66 | pos => 1, | ||||||||||||||||||||||||||||
| 67 | }, | ||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 2 | our %arg1rest_tables = ( | |||||||||||||||||||||||||||
| 71 | tables => { | ||||||||||||||||||||||||||||
| 72 | schema => ['array*', of=>'str*'], | ||||||||||||||||||||||||||||
| 73 | summary => 'Table names', | ||||||||||||||||||||||||||||
| 74 | req => 1, | ||||||||||||||||||||||||||||
| 75 | pos => 1, | ||||||||||||||||||||||||||||
| 76 | slurpy => 1, | ||||||||||||||||||||||||||||
| 77 | }, | ||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2 | our %arg2_column = ( | |||||||||||||||||||||||||||
| 81 | column => { | ||||||||||||||||||||||||||||
| 82 | schema => ['str*'], | ||||||||||||||||||||||||||||
| 83 | summary => 'Table column name', | ||||||||||||||||||||||||||||
| 84 | req => 1, | ||||||||||||||||||||||||||||
| 85 | pos => 2, | ||||||||||||||||||||||||||||
| 86 | }, | ||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 2 | our %arg2rest_columns = ( | |||||||||||||||||||||||||||
| 90 | columns => { | ||||||||||||||||||||||||||||
| 91 | schema => ['array*', of=>'str*'], | ||||||||||||||||||||||||||||
| 92 | summary => 'Table column names', | ||||||||||||||||||||||||||||
| 93 | req => 1, | ||||||||||||||||||||||||||||
| 94 | pos => 2, | ||||||||||||||||||||||||||||
| 95 | slurpy => 1, | ||||||||||||||||||||||||||||
| 96 | }, | ||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | $SPEC{has_table} = { | ||||||||||||||||||||||||||||
| 100 | 2 | v => 1.1, | |||||||||||||||||||||||||||
| 101 | summary => 'Check whether database has a certain table', | ||||||||||||||||||||||||||||
| 102 | args => { | ||||||||||||||||||||||||||||
| 103 | %arg0_dbh, | ||||||||||||||||||||||||||||
| 104 | %arg1_table, | ||||||||||||||||||||||||||||
| 105 | }, | ||||||||||||||||||||||||||||
| 106 | args_as => "array", | ||||||||||||||||||||||||||||
| 107 | result_naked => 1, | ||||||||||||||||||||||||||||
| 108 | }; | ||||||||||||||||||||||||||||
| 109 | 15 | ▶ | sub has_table { | ||||||||||||||||||||||||||
|
Subroutine
has_table: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 15 | my ($dbh, $table) = @_; | |||||||||||||||||||||||||||
| 111 | 15 | my $sth; | |||||||||||||||||||||||||||
| 112 | 15 | ▶ | if ($table =~ /(.+)\.(.+)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | $sth = $dbh->table_info(undef, $1, $2, undef); | |||||||||||||||||||||||||||
| 114 | } else { | ||||||||||||||||||||||||||||
| 115 | 15 | $sth = $dbh->table_info(undef, undef, $table, undef); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 15 | ▶ | $sth->fetchrow_hashref ? 1:0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # alias for has_table | ||||||||||||||||||||||||||||
| 122 | 2 | $SPEC{table_exists} = { %{$SPEC{has_table}}, summary=>'Alias for has_table()' }; | |||||||||||||||||||||||||||
| 123 | 2 | *table_exists = \&has_table; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | $SPEC{has_all_tables} = { | ||||||||||||||||||||||||||||
| 126 | 2 | v => 1.1, | |||||||||||||||||||||||||||
| 127 | summary => 'Check whether database has all specified tables', | ||||||||||||||||||||||||||||
| 128 | args => { | ||||||||||||||||||||||||||||
| 129 | %arg0_dbh, | ||||||||||||||||||||||||||||
| 130 | %arg1rest_tables, | ||||||||||||||||||||||||||||
| 131 | }, | ||||||||||||||||||||||||||||
| 132 | args_as => "array", | ||||||||||||||||||||||||||||
| 133 | result_naked => 1, | ||||||||||||||||||||||||||||
| 134 | }; | ||||||||||||||||||||||||||||
| 135 | 3 | ▶ | sub has_all_tables { | ||||||||||||||||||||||||||
|
Subroutine
has_all_tables: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | 3 | my ($dbh, @tables) = @_; | |||||||||||||||||||||||||||
| 137 | 3 | my $sth; | |||||||||||||||||||||||||||
| 138 | 3 | for my $table (@tables) { | |||||||||||||||||||||||||||
| 139 | 6 | ▶ | if ($table =~ /(.+)\.(.+)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | $sth = $dbh->table_info(undef, $1, $2, undef); | |||||||||||||||||||||||||||
| 141 | } else { | ||||||||||||||||||||||||||||
| 142 | 6 | $sth = $dbh->table_info(undef, undef, $table, undef); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 6 | ▶ | return 0 unless $sth->fetchrow_hashref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 2 | 1; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | $SPEC{has_any_table} = { | ||||||||||||||||||||||||||||
| 150 | 2 | v => 1.1, | |||||||||||||||||||||||||||
| 151 | summary => 'Check whether database has at least one of specified tables', | ||||||||||||||||||||||||||||
| 152 | args => { | ||||||||||||||||||||||||||||
| 153 | %arg0_dbh, | ||||||||||||||||||||||||||||
| 154 | %arg1rest_tables, | ||||||||||||||||||||||||||||
| 155 | }, | ||||||||||||||||||||||||||||
| 156 | args_as => "array", | ||||||||||||||||||||||||||||
| 157 | result_naked => 1, | ||||||||||||||||||||||||||||
| 158 | }; | ||||||||||||||||||||||||||||
| 159 | 4 | ▶ | sub has_any_table { | ||||||||||||||||||||||||||
|
Subroutine
has_any_table: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||||||||||||||||||||
| 160 | 4 | my ($dbh, @tables) = @_; | |||||||||||||||||||||||||||
| 161 | 4 | my $sth; | |||||||||||||||||||||||||||
| 162 | 4 | for my $table (@tables) { | |||||||||||||||||||||||||||
| 163 | 5 | ▶ | if ($table =~ /(.+)\.(.+)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | $sth = $dbh->table_info(undef, $1, $2, undef); | |||||||||||||||||||||||||||
| 165 | } else { | ||||||||||||||||||||||||||||
| 166 | 5 | $sth = $dbh->table_info(undef, undef, $table, undef); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 5 | ▶ | return 1 if $sth->fetchrow_hashref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 1 | ▶ | @tables ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | $SPEC{has_column} = { | ||||||||||||||||||||||||||||
| 174 | 2 | v => 1.1, | |||||||||||||||||||||||||||
| 175 | summary => 'Check whether a table has a specified column', | ||||||||||||||||||||||||||||
| 176 | args => { | ||||||||||||||||||||||||||||
| 177 | %arg0_dbh, | ||||||||||||||||||||||||||||
| 178 | %arg1_table, | ||||||||||||||||||||||||||||
| 179 | %arg2_column, | ||||||||||||||||||||||||||||
| 180 | }, | ||||||||||||||||||||||||||||
| 181 | args_as => "array", | ||||||||||||||||||||||||||||
| 182 | result_naked => 1, | ||||||||||||||||||||||||||||
| 183 | }; | ||||||||||||||||||||||||||||
| 184 | 5 | ▶ | sub has_column { | ||||||||||||||||||||||||||
|
Subroutine
has_column: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 185 | 5 | my ($dbh, $table, $column) = @_; | |||||||||||||||||||||||||||
| 186 | 5 | ▶ | return 0 unless has_table($dbh, $table); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 5 | my @columns = list_columns($dbh, $table); | |||||||||||||||||||||||||||
| 188 | 5 | ▶ | (grep {$_->{COLUMN_NAME} eq $column} @columns) ? 1:0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # alias for has_column | ||||||||||||||||||||||||||||
| 192 | 2 | $SPEC{column_exists} = { %{$SPEC{has_column}}, summary=>'Alias for has_column()' }; | |||||||||||||||||||||||||||
| 193 | 2 | *column_exists = \&has_column; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | $SPEC{has_all_columns} = { | ||||||||||||||||||||||||||||
| 196 | 2 | v => 1.1, | |||||||||||||||||||||||||||
| 197 | summary => 'Check whether a table has all specified columns', | ||||||||||||||||||||||||||||
| 198 | args => { | ||||||||||||||||||||||||||||
| 199 | %arg0_dbh, | ||||||||||||||||||||||||||||
| 200 | %arg1_table, | ||||||||||||||||||||||||||||
| 201 | %arg2rest_columns, | ||||||||||||||||||||||||||||
| 202 | }, | ||||||||||||||||||||||||||||
| 203 | args_as => "array", | ||||||||||||||||||||||||||||
| 204 | result_naked => 1, | ||||||||||||||||||||||||||||
| 205 | }; | ||||||||||||||||||||||||||||
| 206 | 3 | ▶ | sub has_all_columns { | ||||||||||||||||||||||||||
|
Subroutine
has_all_columns: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 207 | 3 | my ($dbh, $table, @columns) = @_; | |||||||||||||||||||||||||||
| 208 | 3 | ▶ | return 0 unless has_table($dbh, $table); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 3 | my @all_columns = list_columns($dbh, $table); | |||||||||||||||||||||||||||
| 210 | 3 | for my $column (@columns) { | |||||||||||||||||||||||||||
| 211 | 8 | ▶ | unless (grep {$_->{COLUMN_NAME} eq $column} @all_columns) { return 0 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | 2 | 1; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | $SPEC{has_any_column} = { | ||||||||||||||||||||||||||||
| 217 | 2 | v => 1.1, | |||||||||||||||||||||||||||
| 218 | summary => 'Check whether a table has at least one of specified columns', | ||||||||||||||||||||||||||||
| 219 | args => { | ||||||||||||||||||||||||||||
| 220 | %arg0_dbh, | ||||||||||||||||||||||||||||
| 221 | %arg1_table, | ||||||||||||||||||||||||||||
| 222 | %arg2rest_columns, | ||||||||||||||||||||||||||||
| 223 | }, | ||||||||||||||||||||||||||||
| 224 | args_as => "array", | ||||||||||||||||||||||||||||
| 225 | result_naked => 1, | ||||||||||||||||||||||||||||
| 226 | }; | ||||||||||||||||||||||||||||
| 227 | 4 | ▶ | sub has_any_column { | ||||||||||||||||||||||||||
|
Subroutine
has_any_column: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 228 | 4 | my ($dbh, $table, @columns) = @_; | |||||||||||||||||||||||||||
| 229 | 4 | ▶ | return 0 unless has_table($dbh, $table); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 4 | my @all_columns = list_columns($dbh, $table); | |||||||||||||||||||||||||||
| 231 | 4 | for my $column (@columns) { | |||||||||||||||||||||||||||
| 232 | 4 | ▶ | if (grep {$_->{COLUMN_NAME} eq $column} @all_columns) { return 1 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | 1 | ▶ | @columns ? 0:1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | $SPEC{list_tables} = { | ||||||||||||||||||||||||||||
| 238 | 2 | v => 1.1, | |||||||||||||||||||||||||||
| 239 | summary => 'List table names in a database', | ||||||||||||||||||||||||||||
| 240 | args => { | ||||||||||||||||||||||||||||
| 241 | %arg0_dbh, | ||||||||||||||||||||||||||||
| 242 | }, | ||||||||||||||||||||||||||||
| 243 | args_as => "array", | ||||||||||||||||||||||||||||
| 244 | result_naked => 1, | ||||||||||||||||||||||||||||
| 245 | }; | ||||||||||||||||||||||||||||
| 246 | 2 | ▶ | sub list_tables { | ||||||||||||||||||||||||||
|
Subroutine
list_tables: called CC=12 SCAR=26.5
Pod
documented
| |||||||||||||||||||||||||||||
| 247 | 2 | my ($dbh) = @_; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 2 | my $driver = $dbh->{Driver}{Name}; | |||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 2 | my @res; | |||||||||||||||||||||||||||
| 252 | 2 | my $sth = $dbh->table_info(undef, undef, undef, undef); | |||||||||||||||||||||||||||
| 253 | 2 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | 12 | my $name = $row->{TABLE_NAME}; | |||||||||||||||||||||||||||
| 255 | 12 | my $schem = $row->{TABLE_SCHEM}; | |||||||||||||||||||||||||||
| 256 | 12 | my $type = $row->{TABLE_TYPE}; | |||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 12 | ▶ | if ($driver eq 'mysql') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | # mysql driver returns database name as schema, so that's useless | ||||||||||||||||||||||||||||
| 260 | 0 | $schem = ''; | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 12 | ▶ | next if $type eq 'VIEW'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 12 | ▶ | next if $type eq 'INDEX'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | 8 | ▶ | next if $schem =~ /^(information_schema)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 8 | ▶ | if ($driver eq 'Pg') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 268 | 0 | ▶ | next if $schem =~ /^(pg_catalog)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | } elsif ($driver eq 'SQLite') { | ||||||||||||||||||||||||||||
| 270 | 8 | ▶ | next if $schem =~ /^(temp)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 6 | ▶ | next if $name =~ /^(sqlite_master|sqlite_temp_master)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 4 | ▶ | push @res, join( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 275 | "", | ||||||||||||||||||||||||||||
| 276 | $schem, | ||||||||||||||||||||||||||||
| 277 | length($schem) ? "." : "", | ||||||||||||||||||||||||||||
| 278 | $name, | ||||||||||||||||||||||||||||
| 279 | ); | ||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | 2 | sort @res; | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | $SPEC{list_indexes} = { | ||||||||||||||||||||||||||||
| 285 | 2 | v => 1.1, | |||||||||||||||||||||||||||
| 286 | summary => 'List indexes for a table in a database', | ||||||||||||||||||||||||||||
| 287 | description => <<'_', | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | SQLite notes: information is retrieved from DBI's table_info(). Autoindex for | ||||||||||||||||||||||||||||
| 290 | primary key is not listed using table_info(), but this function adds it by | ||||||||||||||||||||||||||||
| 291 | looking at `sqlite_master` table. | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | MySQL notes: information is retrieved from statistics_info(). Note that a | ||||||||||||||||||||||||||||
| 294 | multi-column index is reported as separate rows by statistics_info(), one for | ||||||||||||||||||||||||||||
| 295 | each indexed column. But this function merges them and returns the list of | ||||||||||||||||||||||||||||
| 296 | columns in `columns`. | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | _ | ||||||||||||||||||||||||||||
| 299 | args => { | ||||||||||||||||||||||||||||
| 300 | %arg0_dbh, | ||||||||||||||||||||||||||||
| 301 | %arg1_table, | ||||||||||||||||||||||||||||
| 302 | }, | ||||||||||||||||||||||||||||
| 303 | args_as => "array", | ||||||||||||||||||||||||||||
| 304 | result_naked => 1, | ||||||||||||||||||||||||||||
| 305 | }; | ||||||||||||||||||||||||||||
| 306 | 1 | ▶ | sub list_indexes { | ||||||||||||||||||||||||||
|
Subroutine
list_indexes: called CC=38 SCAR=57.4
Pod
documented
| |||||||||||||||||||||||||||||
| 307 | 1 | my ($dbh, $wanted_table) = @_; | |||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 1 | my $driver = $dbh->{Driver}{Name}; | |||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 1 | my @res; | |||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 1 | ▶ | if ($driver eq 'SQLite') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 1 | my @wanted_tables; | |||||||||||||||||||||||||||
| 316 | 1 | ▶ | if (defined $wanted_table) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 0 | @wanted_tables = ($wanted_table); | |||||||||||||||||||||||||||
| 318 | } else { | ||||||||||||||||||||||||||||
| 319 | 1 | @wanted_tables = list_tables($dbh); | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | 1 | ▶ | for (@wanted_tables) { $_ = $1 if /.+\.(.+)/ } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 1 | my $sth = $dbh->prepare("SELECT * FROM sqlite_master"); | |||||||||||||||||||||||||||
| 324 | 1 | $sth->execute; | |||||||||||||||||||||||||||
| 325 | 1 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 4 | ▶ | next unless $row->{type} eq 'index'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | 2 | ▶ | next unless grep { $_ eq $row->{tbl_name} } @wanted_tables; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | 2 | ▶ | next unless $row->{name} =~ /\Asqlite_autoindex_.+_(\d+)\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | 1 | my $col_num = $1; | |||||||||||||||||||||||||||
| 330 | 1 | my @cols = list_columns($dbh, $row->{tbl_name}); | |||||||||||||||||||||||||||
| 331 | push @res, { | ||||||||||||||||||||||||||||
| 332 | name => "PRIMARY", | ||||||||||||||||||||||||||||
| 333 | table => $row->{tbl_name}, | ||||||||||||||||||||||||||||
| 334 | 1 | columns => [$cols[$col_num-1]{COLUMN_NAME}], | |||||||||||||||||||||||||||
| 335 | is_unique => 1, | ||||||||||||||||||||||||||||
| 336 | is_pk => 1, | ||||||||||||||||||||||||||||
| 337 | }; | ||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | 1 | $sth = $dbh->table_info(undef, undef, undef, undef); | |||||||||||||||||||||||||||
| 341 | ROW: | ||||||||||||||||||||||||||||
| 342 | 1 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | 6 | ▶ | next unless $row->{TABLE_TYPE} eq 'INDEX'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 2 | my $table = $row->{TABLE_NAME}; | |||||||||||||||||||||||||||
| 346 | 2 | my $schem = $row->{TABLE_SCHEM}; | |||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | # match table name | ||||||||||||||||||||||||||||
| 349 | 2 | ▶ | if (defined $wanted_table) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | 0 | ▶ | if ($wanted_table =~ /(.+)\.(.+)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | 0 | ▶ | next unless $schem eq $1 && $table eq $2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$schem eq $1 and $table eq $2
MC/DC0%0/2
$schem eq $1 and $table eq $2
$schem eq $1
$table eq $2
Truth table0%0/3
A and B
A= $schem eq $1B= $table eq $2
| |||||||||||||||||||||||||||||
| 352 | } else { | ||||||||||||||||||||||||||||
| 353 | 0 | ▶ | next unless $table eq $wanted_table; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 2 | ▶ | next unless my $sql = $row->{sqlite_sql}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | #use DD; dd $row; | ||||||||||||||||||||||||||||
| 359 | 1 | ▶ | $sql =~ s/\A\s*CREATE\s+(UNIQUE\s+)?INDEX\s+//is or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | 0 | log_trace "Not a CREATE INDEX statement, skipped: $row->{sqlite_sql}"; | |||||||||||||||||||||||||||
| 361 | 0 | next ROW; | |||||||||||||||||||||||||||
| 362 | }; | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 1 | ▶ | $row->{is_unique} = $1 ? 1:0; # not-standard, backward compat | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | 1 | ▶ | $row->{NON_UNIQUE} = $1 ? 0:1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 1 | ▶ | $sql =~ s/\A(\S+)\s+//s | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | or die "Can't extract index name from sqlite_sql: $sql"; | ||||||||||||||||||||||||||||
| 369 | 1 | $row->{name} = $1; # non-standard, backward compat | |||||||||||||||||||||||||||
| 370 | 1 | $row->{INDEX_NAME} = $1; | |||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 1 | ▶ | $sql =~ s/\AON\s*(\S+)\s*\(\s*(.+)\s*\)//s | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | or die "Can't extract indexed table+columns from sqlite_sql: $sql"; | ||||||||||||||||||||||||||||
| 374 | 1 | ▶ | $row->{table} = $table // $1; # non-standard, backward-compat | ||||||||||||||||||||||||||
|
Condition33%1/3
$table // $1
MC/DC0%0/2
$table // $1
$table
$1
Truth table33%1/3
A // B
A= $tableB= $1
| |||||||||||||||||||||||||||||
| 375 | 1 | $row->{columns} = [split /\s*,\s*/, $2]; # non-standard | |||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 1 | push @res, $row; | |||||||||||||||||||||||||||
| 378 | } # while row | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | } elsif ($driver eq 'mysql') { | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | 0 | my $sth = $dbh->statistics_info(undef, undef, undef, undef, undef); | |||||||||||||||||||||||||||
| 383 | 0 | $sth->execute; | |||||||||||||||||||||||||||
| 384 | 0 | my @res0; | |||||||||||||||||||||||||||
| 385 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 386 | 0 | ▶ | if (defined $wanted_table) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 387 | 0 | ▶ | if ($wanted_table =~ /(.+)\.(.+)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 388 | 0 | ▶ | next unless $row->{TABLE_SCHEM} eq $1 && $row->{TABLE_NAME} eq $2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$row->{"TABLE_SCHEM"} eq $1 and $row->{"TABLE_NAME"} eq $2
MC/DC0%0/2
$row->{"TABLE_SCHEM"} eq $1 and $row->{"TABLE_NAME"} eq $2
$row->{"TABLE_SCHEM"} eq $1
$row->{"TABLE_NAME"} eq $2
Truth table0%0/3
A and B
A= $row->{"TABLE_SCHEM"} eq $1B= $row->{"TABLE_NAME"} eq $2
| |||||||||||||||||||||||||||||
| 389 | } else { | ||||||||||||||||||||||||||||
| 390 | 0 | ▶ | next unless $row->{TABLE_NAME} eq $wanted_table; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | 0 | $row->{table} = $row->{TABLE_NAME}; # non-standard, backward-compat | |||||||||||||||||||||||||||
| 394 | 0 | $row->{name} = $row->{INDEX_NAME}; # non-standard, backward-compat | |||||||||||||||||||||||||||
| 395 | 0 | ▶ | $row->{is_unique} = $row->{NON_UNIQUE} ? 0:1; # non-standard, backward-compat | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 396 | 0 | ▶ | $row->{is_pk} = $row->{INDEX_NAME} eq 'PRIMARY' ? 1:0; # non-standard, backward-compat | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 0 | push @res0, $row; | |||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | # merge separated per-indexed-column result into a single all-columns | ||||||||||||||||||||||||||||
| 402 | # result | ||||||||||||||||||||||||||||
| 403 | 0 | my @index_names; | |||||||||||||||||||||||||||
| 404 | 0 | ▶ | for my $row (@res0) { push @index_names, $row->{INDEX_NAME} unless grep { $row->{INDEX_NAME} eq $_ } @index_names } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | 0 | for my $index_name (@index_names) { | |||||||||||||||||||||||||||
| 406 | 0 | my @hashes = grep { $_->{INDEX_NAME} eq $index_name } @res0; | |||||||||||||||||||||||||||
| 407 | 0 | ▶ | if (@hashes == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 408 | 0 | push @res, $hashes[0]; | |||||||||||||||||||||||||||
| 409 | } else { | ||||||||||||||||||||||||||||
| 410 | 0 | my %merged_hash; | |||||||||||||||||||||||||||
| 411 | 0 | $merged_hash{columns} = []; | |||||||||||||||||||||||||||
| 412 | 0 | for my $hash (@hashes) { | |||||||||||||||||||||||||||
| 413 | 0 | $merged_hash{columns}[ $hash->{ORDINAL_POSITION}-1 ] = $hash->{COLUMN_NAME}; | |||||||||||||||||||||||||||
| 414 | 0 | for (keys %$hash) { $merged_hash{$_} = $hash->{$_} } | |||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||
| 416 | 0 | delete $merged_hash{ORDINAL_POSITION}; | |||||||||||||||||||||||||||
| 417 | 0 | push @res, \%merged_hash; | |||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | } else { | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 0 | die "Driver $driver is not yet supported for list_indexes"; | |||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 1 | @res; | |||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | $SPEC{list_columns} = { | ||||||||||||||||||||||||||||
| 430 | 2 | v => 1.1, | |||||||||||||||||||||||||||
| 431 | summary => 'List columns of a table', | ||||||||||||||||||||||||||||
| 432 | args => { | ||||||||||||||||||||||||||||
| 433 | %arg0_dbh, | ||||||||||||||||||||||||||||
| 434 | %arg1_table, | ||||||||||||||||||||||||||||
| 435 | }, | ||||||||||||||||||||||||||||
| 436 | args_as => "array", | ||||||||||||||||||||||||||||
| 437 | result_naked => 1, | ||||||||||||||||||||||||||||
| 438 | }; | ||||||||||||||||||||||||||||
| 439 | 14 | ▶ | sub list_columns { | ||||||||||||||||||||||||||
|
Subroutine
list_columns: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 440 | 14 | my ($dbh, $table) = @_; | |||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 14 | my @res; | |||||||||||||||||||||||||||
| 443 | 14 | my ($schema, $utable); | |||||||||||||||||||||||||||
| 444 | 14 | ▶ | if ($table =~ /\./) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 445 | 0 | ($schema, $utable) = split /\./, $table; | |||||||||||||||||||||||||||
| 446 | } else { | ||||||||||||||||||||||||||||
| 447 | 14 | $schema = undef; | |||||||||||||||||||||||||||
| 448 | 14 | $utable = $table; | |||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||
| 450 | 14 | my $sth = $dbh->column_info(undef, $schema, $utable, undef); | |||||||||||||||||||||||||||
| 451 | 14 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 452 | 58 | push @res, $row; | |||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||
| 454 | 14 | sort @res; | |||||||||||||||||||||||||||
| 455 | } | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 2 | 1; | |||||||||||||||||||||||||||
| 458 | # ABSTRACT: Utility routines related to database schema | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | __END__ | ||||||||||||||||||||||||||||