| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::DB; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | our $VERSION = '2.447'; | ||
| 9 | |||
| 10 | use Encode qw( decode ); | ||
| 11 | #use bytes; # required | ||
| 12 | use Scalar::Util qw( looks_like_number ); | ||
| 13 | |||
| 14 | use DBI::Const::GetInfoType; | ||
| 15 | |||
| 16 | |||
| 17 | sub new { | ||
| 18 | my ( $class, $info, $opt ) = @_; | ||
| 19 | my $db_module = "App::DBBrowser::DB::$info->{plugin}"; | ||
| 20 | eval "require $db_module" or die $@; | ||
| 21 | my $plugin = $db_module->new( $info, $opt ); | ||
| 22 | bless { Plugin => $plugin }, $class; | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 26 | sub get_db_driver { | ||
| 27 | my ( $sf ) = @_; | ||
| 28 | return $sf->{Plugin}->get_db_driver(); | ||
| 29 | } | ||
| 30 | |||
| 31 | |||
| 32 | sub read_login_data { | ||
| 33 | my ( $sf ) = @_; | ||
| 34 | return [] if ! $sf->{Plugin}->can( 'read_login_data' ); | ||
| 35 | my $read_args = $sf->{Plugin}->read_login_data(); | ||
| 36 | return $read_args // []; | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | sub env_variables { | ||
| 41 | my ( $sf ) = @_; | ||
| 42 | return [] if ! $sf->{Plugin}->can( 'env_variables' ); | ||
| 43 | my $env_variables = $sf->{Plugin}->env_variables(); | ||
| 44 | return $env_variables // []; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | sub read_attributes { | ||
| 49 | my ( $sf ) = @_; | ||
| 50 | return [] if ! $sf->{Plugin}->can( 'read_attributes' ); | ||
| 51 | my $read_attributes = $sf->{Plugin}->read_attributes(); | ||
| 52 | return $read_attributes // []; | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | sub set_attributes { | ||
| 57 | my ( $sf ) = @_; | ||
| 58 | return [] if ! $sf->{Plugin}->can( 'set_attributes' ); | ||
| 59 | my $set_attributes = $sf->{Plugin}->set_attributes(); | ||
| 60 | return $set_attributes // []; | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | sub get_db_handle { | ||
| 65 | my ( $sf, $db ) = @_; | ||
| 66 | my $dbh = $sf->{Plugin}->get_db_handle( $db ); | ||
| 67 | if ( $dbh->{Driver}{Name} eq 'SQLite' ) { | ||
| 68 | $dbh->sqlite_create_function( 'regexp', 3, sub { | ||
| 69 | my ( $regex, $string, $case_sensitive ) = @_; | ||
| 70 | $string = '' if ! defined $string; | ||
| 71 | return $string =~ m/$regex/sm if $case_sensitive; | ||
| 72 | return $string =~ m/$regex/ism; | ||
| 73 | } | ||
| 74 | ); | ||
| 75 | $dbh->sqlite_create_function( 'trunc', -1, sub { | ||
| 76 | my ( $number, $places ) = @_; | ||
| 77 | return $number if ! looks_like_number( $number ); | ||
| 78 | $places //= 0; | ||
| 79 | return int( $number * 10 ** $places ) / 10 ** $places; | ||
| 80 | } | ||
| 81 | ); | ||
| 82 | $dbh->sqlite_create_function( 'octet_length', 1, sub { | ||
| 83 | require bytes; | ||
| 84 | return if ! defined $_[0]; | ||
| 85 | return bytes::length $_[0]; | ||
| 86 | } | ||
| 87 | ); | ||
| 88 | } | ||
| 89 | return $dbh; | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | sub get_databases { | ||
| 94 | my ( $sf ) = @_; | ||
| 95 | my ( $user_db, $sys_db ) = $sf->{Plugin}->get_databases(); | ||
| 96 | return $user_db // [], $sys_db // []; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | sub get_schemas { | ||
| 101 | my ( $sf, $dbh, $db, $is_system_db ) = @_; | ||
| 102 | my ( $user_schemas, $sys_schemas ); | ||
| 103 | my $driver = $dbh->{Driver}{Name}; # | ||
| 104 | my $dbms = $sf->{Plugin}{i}{dbms}; | ||
| 105 | if ( $sf->{Plugin}->can( 'get_schemas' ) ) { | ||
| 106 | ( $user_schemas, $sys_schemas ) = $sf->{Plugin}->get_schemas( $dbh, $db, $is_system_db ); | ||
| 107 | } | ||
| 108 | else { | ||
| 109 | if ( $dbms eq 'SQLite' ) { | ||
| 110 | $user_schemas = []; | ||
| 111 | } | ||
| 112 | elsif( $dbms =~ /^(?:mysql|MariaDB)\z/ ) { | ||
| 113 | # MySQL 8.0 Reference Manual / MySQL Glossary / Schema: | ||
| 114 | # In MySQL, physically, a schema is synonymous with a database. | ||
| 115 | # You can substitute the keyword SCHEMA instead of DATABASE in MySQL SQL syntax, | ||
| 116 | if ( $driver eq 'ODBC' ) { | ||
| 117 | # ODBC: $db is the data source name | ||
| 118 | $user_schemas = [ $dbh->get_info( $GetInfoType{SQL_DATABASE_NAME} ) ]; | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | $user_schemas = [ $db ]; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | elsif ( $dbms eq 'Firebird' ) { | ||
| 125 | $user_schemas = []; | ||
| 126 | } | ||
| 127 | elsif ( $dbms eq 'Oracle' ) { | ||
| 128 | # To separate system schemas from the user schemas. | ||
| 129 | my ( $tmp_user_schemas, $tmp_sys_schemas ) = ( [], [] ); | ||
| 130 | for my $sch ( $dbh->selectall_array( "SELECT USERNAME, ORACLE_MAINTAINED FROM ALL_USERS" ) ) { | ||
| 131 | if ( $sch->[1] =~ /^N/i ) { | ||
| 132 | push @$tmp_user_schemas, $sch->[0]; | ||
| 133 | } | ||
| 134 | else { | ||
| 135 | push @$tmp_sys_schemas, $sch->[0]; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | $user_schemas = [ sort @$tmp_user_schemas ]; | ||
| 139 | $sys_schemas = [ sort @$tmp_sys_schemas, 'PUBLIC' ]; | ||
| 140 | } | ||
| 141 | else { | ||
| 142 | my $table_schem; | ||
| 143 | if ( $driver eq 'Pg' ) { | ||
| 144 | # 'pg_schema' holds the unquoted name of the schema | ||
| 145 | $table_schem = 'pg_schema'; | ||
| 146 | } | ||
| 147 | elsif ( $driver eq 'Informix' ) { | ||
| 148 | $table_schem = 'table_owner'; | ||
| 149 | } | ||
| 150 | elsif ( $driver eq 'Sybase' ) { | ||
| 151 | $table_schem = 'TABLE_OWNER'; | ||
| 152 | } | ||
| 153 | else { | ||
| 154 | $table_schem = 'TABLE_SCHEM'; | ||
| 155 | } | ||
| 156 | my $sth; | ||
| 157 | if ( $driver eq 'ODBC' ) { | ||
| 158 | $sth = $dbh->table_info( undef, '%', undef, undef ); | ||
| 159 | } | ||
| 160 | else { | ||
| 161 | $sth = $dbh->table_info( undef, '%', '', '' ); | ||
| 162 | } | ||
| 163 | my $info = $sth->fetchall_hashref( $table_schem ); | ||
| 164 | my $regex_sys; | ||
| 165 | if ( $dbms eq 'Pg' ) { | ||
| 166 | $regex_sys = qr/^(?:pg_|information_schema$)/i; | ||
| 167 | } | ||
| 168 | elsif ( $dbms eq 'DB2' ) { | ||
| 169 | $regex_sys = qr/^(?:SYS|SQLJ$|NULLID$)/i; | ||
| 170 | } | ||
| 171 | elsif ( $dbms eq 'Informix' ) { | ||
| 172 | $regex_sys = qr/^informix\s*\z/i; | ||
| 173 | } | ||
| 174 | elsif ( $dbms eq 'MSSQL' ) { | ||
| 175 | $regex_sys = qr/^(?:sys|information_schema)\z/i; | ||
| 176 | } | ||
| 177 | |||
| 178 | for my $schema ( sort keys %$info ) { | ||
| 179 | if ( defined $regex_sys && $schema =~ $regex_sys ) { | ||
| 180 | push @$sys_schemas, $schema; | ||
| 181 | } | ||
| 182 | else { | ||
| 183 | push @$user_schemas, $schema; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | } | ||
| 187 | } | ||
| 188 | $user_schemas //= []; | ||
| 189 | $sys_schemas //= []; | ||
| 190 | if ( $is_system_db ) { | ||
| 191 | # System database: all schemas are treated as system schemas. | ||
| 192 | return [], [ @$user_schemas, @$sys_schemas ]; | ||
| 193 | } | ||
| 194 | else { | ||
| 195 | if ( $dbms eq 'Pg' && ! @$user_schemas ) { | ||
| 196 | # 5.9.2. The Public Schema | ||
| 197 | # In the previous sections we created tables without specifying any schema names. By default such tables | ||
| 198 | # (and other objects) are automatically put into a schema named “public”. Every new database contains such a schema. | ||
| 199 | $user_schemas = [ 'public' ]; | ||
| 200 | } | ||
| 201 | return $user_schemas, $sys_schemas; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | sub tables_info { | ||
| 207 | my ( $sf, $dbh, $schema, $is_system_schema, $db_attached ) = @_; | ||
| 208 | my $driver = $sf->get_db_driver(); | ||
| 209 | if ( $sf->{Plugin}->can( 'tables_info' ) ) { | ||
| 210 | return $sf->{Plugin}->tables_info( $dbh, $schema, $is_system_schema, $db_attached ); | ||
| 211 | } | ||
| 212 | my ( $table_cat, $table_schem, $table_name, $table_type ); | ||
| 213 | if ( $driver eq 'Pg' ) { | ||
| 214 | $table_cat = 'TABLE_CAT'; | ||
| 215 | $table_schem = 'pg_schema'; | ||
| 216 | $table_name = 'pg_table'; | ||
| 217 | $table_type = 'TABLE_TYPE'; | ||
| 218 | # DBD::Pg 3.16.0: | ||
| 219 | # The TABLE_SCHEM and TABLE_NAME will be quoted via quote_ident(). | ||
| 220 | # Four additional fields specific to DBD::Pg are returned: | ||
| 221 | # pg_schema: the unquoted name of the schema | ||
| 222 | # pg_table: the unquoted name of the table | ||
| 223 | # ... | ||
| 224 | } | ||
| 225 | elsif ( $driver eq 'Informix' ) { | ||
| 226 | $table_cat = 'table_qualifier'; | ||
| 227 | $table_schem = 'table_owner'; | ||
| 228 | $table_name = 'table_name'; | ||
| 229 | $table_type = 'table_type'; | ||
| 230 | } | ||
| 231 | elsif ( $driver eq 'Sybase' ) { | ||
| 232 | $table_cat = 'TABLE_QUALIFIER'; | ||
| 233 | $table_schem = 'TABLE_OWNER'; | ||
| 234 | $table_name = 'TABLE_NAME'; | ||
| 235 | $table_type = 'TABLE_TYPE'; | ||
| 236 | } | ||
| 237 | else { | ||
| 238 | $table_cat = 'TABLE_CAT'; | ||
| 239 | $table_schem = 'TABLE_SCHEM'; | ||
| 240 | $table_name = 'TABLE_NAME'; | ||
| 241 | $table_type = 'TABLE_TYPE'; | ||
| 242 | } | ||
| 243 | my @keys = ( $table_cat, $table_schem, $table_name, $table_type ); | ||
| 244 | my $sth; | ||
| 245 | if ( $driver =~ /^(?:Oracle|ODBC)\z/ ) { | ||
| 246 | $sth = $dbh->table_info( undef, $schema, '%', undef ); | ||
| 247 | } | ||
| 248 | else { | ||
| 249 | $sth = $dbh->table_info( undef, $schema, '%', '' ); | ||
| 250 | } | ||
| 251 | my $info_tables = $sth->fetchall_arrayref( { map { $_ => 1 } @keys } ); | ||
| 252 | my $dbms = $sf->{Plugin}{i}{dbms}; | ||
| 253 | my $schema_key; | ||
| 254 | # Set $schema_key if `table_info` returns everything. | ||
| 255 | if ( $driver =~ /^(?:Informix|Sybase)\z/ ) { | ||
| 256 | $schema_key = $table_schem; | ||
| 257 | } | ||
| 258 | elsif ( $driver eq 'ODBC' && $dbms =~ /^(?:mysql|MariaDB)\z/ ) { | ||
| 259 | # table_cat returns the schema | ||
| 260 | $schema_key = $table_cat; | ||
| 261 | } | ||
| 262 | my ( @user_table_keys, @sys_table_keys ); | ||
| 263 | my $tables_info = {}; | ||
| 264 | |||
| 265 | for my $info_table ( @$info_tables ) { | ||
| 266 | if ( $schema_key && defined $schema && $schema ne $info_table->{$schema_key} ) { | ||
| 267 | next; | ||
| 268 | } | ||
| 269 | if ( $driver eq 'Informix' ) { | ||
| 270 | $info_table->{$table_type} =~ s/\s+\z//; | ||
| 271 | } | ||
| 272 | if ( $dbms eq 'SQLite') { | ||
| 273 | if ( $info_table->{$table_type} =~ /^(?:INDEX|TRIGGER)\z/ ) { | ||
| 274 | next; | ||
| 275 | } | ||
| 276 | if ( $info_table->{$table_name} eq 'sqlite_temp_master' ) { | ||
| 277 | next; # no temp tables | ||
| 278 | } | ||
| 279 | } | ||
| 280 | if ( $dbms eq 'Oracle' && $info_table->{$table_type} eq 'SEQUENCE' ) { | ||
| 281 | next; | ||
| 282 | } | ||
| 283 | my $table_key; | ||
| 284 | # The table name in $table_key is used in the tables-menu but not in SQL code. | ||
| 285 | # To get the table names for SQL code it is used the 'quote_table' routine in Auxil.pm. | ||
| 286 | if ( $db_attached ) { | ||
| 287 | if ( $dbms eq 'SQLite' ) { | ||
| 288 | if ( $info_table->{$table_schem} =~ /^main\z/i ) { | ||
| 289 | $table_key = sprintf "[%s] %s", "\x{001f}" . $info_table->{$table_schem}, $info_table->{$table_name}; | ||
| 290 | # \x{001f} keeps the main tables on top of the tables menu. | ||
| 291 | } | ||
| 292 | else { | ||
| 293 | $table_key = sprintf "[%s] %s", $info_table->{$table_schem}, $info_table->{$table_name}; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | elsif ( $dbms eq 'DuckDB' ) { | ||
| 297 | $table_key = sprintf "[%s] %s", $info_table->{$table_cat}, $info_table->{$table_name}; | ||
| 298 | } | ||
| 299 | } | ||
| 300 | else { | ||
| 301 | $table_key = $info_table->{$table_name}; | ||
| 302 | } | ||
| 303 | if ( $is_system_schema ) { | ||
| 304 | push @sys_table_keys, $table_key; | ||
| 305 | } | ||
| 306 | elsif ( $dbms eq 'SQLite' && $info_table->{$table_name} =~ /^sqlite_/i ) { | ||
| 307 | push @sys_table_keys, $table_key; | ||
| 308 | } | ||
| 309 | elsif ( $info_table->{$table_type} =~ /^SYSTEM/i ) { | ||
| 310 | push @sys_table_keys, $table_key; | ||
| 311 | } | ||
| 312 | else { | ||
| 313 | push @user_table_keys, $table_key; | ||
| 314 | } | ||
| 315 | $tables_info->{$table_key} = [ @{$info_table}{@keys} ]; | ||
| 316 | } | ||
| 317 | return $tables_info, [ sort @user_table_keys ], [ sort @sys_table_keys ]; | ||
| 318 | } | ||
| 319 | |||
| 320 | |||
| 321 | |||
| 322 | |||
| 323 | |||
| 324 | 1; | ||
| 325 | |||
| 326 | |||
| 327 | __END__ |