| 1 | # -*- perl -*- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | ## SQL API Abstraction - ~/lib/DB/Object/SQLite/Tables.pm | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | ## Version v1.0.2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | ## Created 2017/07/19 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | ## Modified 2026/10/01 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | ## All rights reserved | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | ## under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # This package's purpose is to separate the object of the tables from the main | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # DB::Object package so that when they get DESTROY'ed, it does not interrupt | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # the SQL connection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | package DB::Object::SQLite::Tables; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | BEGIN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | warnings::register_categories( 'DB::Object' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 3 | ▶ | use parent qw( DB::Object::Tables ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | ▶ | use vars qw( $VERSION $DEBUG $TYPE_TO_CONSTANT ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # <https://metacpan.org/pod/DBD::SQLite::Constants> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # <https://www.sqlite.org/datatype3.html> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # <https://metacpan.org/pod/DBD::SQLite::Constants#datatypes-(fundamental_datatypes)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # NULL, INTEGER, REAL, TEXT, BLOB | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 3 | our $TYPE_TO_CONSTANT = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | qr/^(INT|INTEGER|TINYINT|SMALLINT|MEDIUMINT|BIGINT|UNSIGNED\s+BIG\s+INT|INT2|INT8)/ => { constant => '', name => 'SQLITE_INTEGER', type => 'integer' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | qr/^(CHARACTER\(\d+\)|VARCHAR\(\d+\)|VARYING\s+CHARACTER\(\d+\)|NCHAR\(\d+\)|NATIVE\s+CHARACTER\(\d+\)|NVARCHAR\(\d+\)|TEXT|CLOB)/ => { constant => '', name => 'SQLITE_TEXT', type => 'text' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | qr/^BLOB/ => { constant => '', name => 'SQLITE_BLOB', type => 'blob' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | qr/^(REAL|DOUBLE\s+DOUBLE\s+PRECISION|FLOAT)/ => { constant => '', name => 'SQLITE_FLOAT', type => 'float' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | qr/^(NUMERIC|DECIMAL\(\d+,\d+\)|BOOLEAN|DATETIME|DATE)/ => { constant => '', name => 'SQLITE_NULL', type => 'bool' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | our $DEBUG = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | our $VERSION = 'v1.0.2'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | sub init | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | return( shift->DB::Object::Tables::init( @_ ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # Inherited from DB::Object::Tables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # sub alter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | sub create | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create: not called CC=27 SCAR=66.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # $tbl->create( [ 'ROW 1', 'ROW 2'... ], { 'temporary' => 1, 'TYPE' => ISAM }, $obj ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $data = shift( @_ ) || []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift @_ || []
MC/DC0%0/1
shift @_ || []
shift @_
Truth table0%0/2
A
A= shift @_
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $opt = shift( @_ ) || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift @_ || {}
MC/DC0%0/1
shift @_ || {}
shift @_
Truth table0%0/2
A
A= shift @_
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my $sth = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $table = $self->{table}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # Set temporary in the object, so we can use it to recreate the table creation info as string: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # $table->create( [ ... ], { ... }, $obj )->as_string(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my $temp = $self->{temporary} = delete( $opt->{temporary} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # Check possible options | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | my $allowed = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | type => qr/^(ISAM|MYISAM|HEAP)$/i, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | auto_increment => qr/^(1|0)$/, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | avg_row_length => qr/^\d+$/, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | checksum => qr/^(1|0)$/, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | comment => qr//, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | max_rows => qr/^\d+$/, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | min_rows => qr/^\d+$/, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | pack_keys => qr/^(1|0)$/, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | password => qr//, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | delay_key_write => qr/^\d+$/, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | row_format => qr/^(default|dynamic|static|compressed)$/i, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | raid_type => qr/^(?:1|STRIPED|RAID0|RAID_CHUNKS\s*=\s*\d+|RAID_CHUNKSIZE\s*=\s*\d+)$/, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my @options = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my @errors = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # Avoid working for nothing, make this condition | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if( %$opt ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | my %lc_opt = map{ lc( $_ ) => $opt->{ $_ } } keys( %$opt ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $opt = \%lc_opt; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | foreach my $key ( keys( %$opt ) ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | next if( $opt->{ $key } =~ /^\s*$/ || !exists( $allowed->{ $key } ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opt->{$key} =~ /^\s*$/ or not exists $allowed->{$key}
MC/DC0%0/2
$opt->{$key} =~ /^\s*$/ or not exists $allowed->{$key}
$opt->{$key} =~ /^\s*$/
not exists $allowed->{$key}
Truth table0%0/3
A or not(B)
A= $opt->{$key} =~ /^\s*$/B= not exists $allowed->{$key}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if( $opt->{ $key } !~ /$allowed->{ $key }/ ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | push( @errors, $key ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | push( @options, $key ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | $opt->{comment} = "'" . quotemeta( $opt->{comment} ) . "'" if( exists( $opt->{comment} ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | $opt->{password} = "'" . $opt->{password} . "'" if( exists( $opt->{password} ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if( @errors ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | warn( "The options '", join( ', ', @errors ), "' were either not recognized or malformed and thus were ignored." ) if( $self->_is_warnings_enabled( 'DB::Object' ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | # Check statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | my $select = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if( $sth && ref( $sth ) && ( $sth->isa( 'DB::Object::Statement' ) || $sth->can( 'as_string' ) ) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
$sth and ref $sth and $sth->isa("DB::Object::Statement") || $sth->can("as_string")
$sth and ref $sth
$sth->isa("DB::Object::Statement") or $sth->can("as_string")
MC/DC0%0/4
$sth and ref $sth and $sth->isa("DB::Object::Statement") || $sth->can("as_string")
$sth
ref $sth
$sth->isa("DB::Object::Statement")
$sth->can("as_string")
Truth table0%0/5
A and B and C or D
A= $sthB= ref $sthC= $sth->isa("DB::Object::Statement")D= $sth->can("as_string")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | $select = $sth->as_string(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if( $select !~ /^\s*(?:IGNORE|REPLACE)*\s*\bSELECT\s+/ ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | return( $self->error( "SELECT statement to use to create table is invalid:\n$select" ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if( $self->exists() == 0 ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | my $query = 'CREATE ' . ( $temp ? 'TEMPORARY ' : '' ) . "TABLE $table "; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | # Structure of table if any - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | # structure may very well be provided using a select statement, such as: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | # CREATE TEMPORARY TABLE ploppy TYPE=HEAP COMMENT='this is kewl' MAX_ROWS=10 SELECT * FROM some_table LIMIT 0,0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | my $def = "(\n" . CORE::join( ",\n", @$data ) . "\n)" if( $data && ref( $data ) && @$data ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$data and ref $data and @$data
$data and ref $data
MC/DC0%0/3
$data and ref $data and @$data
$data
ref $data
@$data
Truth table0%0/4
A and B and C
A= $dataB= ref $dataC= @$data
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | my $tdef = CORE::join( ' ', map{ "\U$_\E = $opt->{ $_ }" } @options ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if( !$def && !$select ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$def or $select
MC/DC0%0/2
$def or $select
$def
$select
Truth table0%0/3
A or B
A= $defB= $select
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | return( $self->error( "Lacking table '$table' structure information to create it." ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | $query .= join( ' ', $def, $tdef, $select ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | my $new = $self->prepare( $query ) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->prepare($query) || (return $self->error("Error while preparing query to create table '${table}':\n$query", $self->errstr))
MC/DC0%0/1
$self->prepare($query) || (return $self->error("Error while preparing query to create table '${table}':\n$query", $self->errstr))
$self->prepare($query)
Truth table0%0/2
A
A= $self->prepare($query)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | return( $self->error( "Error while preparing query to create table '$table':\n$query", $self->errstr() ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | # Trick so other method may follow, such as as_string(), fetchrow(), rows() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if( !defined( wantarray() ) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | $new->execute || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | return( $self->error( "Error while executing query to create table '$table':\n$query", $new->errstr() ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | $self->reset_structure; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | return( $new ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | return( $self->error( "Table '$table' already exists." ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | sub create_info | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_info: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | my $table = $self->{table}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | $self->structure || return( $self->pass_error ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | my $struct = $self->{structure}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | my $fields = $self->{fields}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | my $default = $self->{default}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | my $primary = $self->{primary}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | my @output = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | foreach my $field ( sort{ $fields->{ $a } <=> $fields->{ $b } } keys( %$fields ) ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | push( @output, "$field $struct->{ $field }" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | push( @output, "PRIMARY KEY(" . CORE::join( ',', @$primary ) . ")" ) if( $primary && @$primary ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$primary and @$primary
MC/DC0%0/2
$primary and @$primary
$primary
@$primary
Truth table0%0/3
A and B
A= $primaryB= @$primary
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | my $info = $self->stat( $table ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | my @opt = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | push( @opt, "TYPE = $info->{type}" ) if( $info->{type} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | my $addons = $info->{create_options}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | if( $addons ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | $addons =~ s/(\A|\s+)([\w\_]+)\s*=\s*/$1\U$2\E=/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | push( @opt, $addons ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | push( @opt, "COMMENT='" . quotemeta( $info->{comment} ) . "'" ) if( $info->{comment} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | my $str = "CREATE TABLE $table (\n\t" . CORE::join( ",\n\t", @output ) . "\n)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | $str .= ' ' . CORE::join( ' ', @opt ) if( @opt ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | $str .= ';'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | return( @output ? $str : undef() ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | # NOTE: sub default is inherited from DB::Object::Tables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | # sub default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | # NOTE: sub drop is inherited from DB::Object::Tables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | # sub drop | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | # NOTE: exists is inherited from DB::Object::Tables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub lock { return( shift->error( "There is no table locking in SQLite." ) ); } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lock: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | # NOTE: sub name is inherited from DB::Object::Tables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | # sub name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | # NOTE: sub null is inherited from DB::Object::Tables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | # sub null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | # NOTE: sub primary is inherited from DB::Object::Tables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # sub primary | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | sub on_conflict | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
on_conflict: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | my $q = $self->_reset_query; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | return( $q->on_conflict( @_ ) ) if( !defined( wantarray() ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | if( wantarray() ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | my( @val ) = $q->on_conflict( @_ ) || return( $self->pass_error( $q->error ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$q->on_conflict(@_) || (return $self->pass_error($q->error))
MC/DC0%0/1
$q->on_conflict(@_) || (return $self->pass_error($q->error))
$q->on_conflict(@_)
Truth table0%0/2
A
A= $q->on_conflict(@_)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | return( @val ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | my $val = $q->on_conflict( @_ ) || return( $self->pass_error( $q->error ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$q->on_conflict(@_) || (return $self->pass_error($q->error))
MC/DC0%0/1
$q->on_conflict(@_) || (return $self->pass_error($q->error))
$q->on_conflict(@_)
Truth table0%0/2
A
A= $q->on_conflict(@_)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | return( $val ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | sub rename | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rename: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | my $table = $self->{table} || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | return( $self->error( 'No table was provided to rename' ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'table'} || (return $self->error('No table was provided to rename'))
MC/DC0%0/1
$self->{'table'} || (return $self->error('No table was provided to rename'))
$self->{'table'}
Truth table0%0/2
A
A= $self->{'table'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | my $new = shift( @_ ) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift @_ || (return $self->error("No new table name was provided to rename table '${table}'."))
MC/DC0%0/1
shift @_ || (return $self->error("No new table name was provided to rename table '${table}'."))
shift @_
Truth table0%0/2
A
A= shift @_
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | return( $self->error( "No new table name was provided to rename table '$table'." ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | ▶ | if( $new !~ /^[a-zA-Z][\w\_]+$/ ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | return( $self->error( "Bad new table name '$new'." ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | my $query = "ALTER TABLE $table RENAME TO $new"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | my $sth = $self->prepare( $query ) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->prepare($query) || (return $self->error("Error while preparing query to rename table '${table}' into '${new}':\n$query", $self->errstr))
MC/DC0%0/1
$self->prepare($query) || (return $self->error("Error while preparing query to rename table '${table}' into '${new}':\n$query", $self->errstr))
$self->prepare($query)
Truth table0%0/2
A
A= $self->prepare($query)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | return( $self->error( "Error while preparing query to rename table '$table' into '$new':\n$query", $self->errstr() ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | if( !defined( wantarray() ) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | ▶ | $sth->execute || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | return( $self->error( "Error while executing query to rename table '$table' into '$new':\n$query", $sth->errstr() ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | $self->reset_structure; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | return( $sth ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | sub stat { return( shift->database_object->stat( @_ ) ); } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stat: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | # TODO: Must implement a cache mechanism for DB::Object::SQLite::structure() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | # <https://www.sqlite.org/pragma.html#pragma_table_info> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | sub structure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
structure: not called CC=26 SCAR=65.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | return( $self->_clone( $self->{_cache_structure} ) ) if( $self->{_cache_structure} && !CORE::length( $self->{_reset_structure} // '' ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$self->{"_cache_structure"} and not length($self->{"_reset_structure"} // "")
$self->{"_reset_structure"} // ""
MC/DC0%0/2
$self->{"_cache_structure"} and not length($self->{"_reset_structure"} // "")
$self->{"_cache_structure"}
$self->{"_reset_structure"}
Truth table0%0/3
A and not(B)
A= $self->{"_cache_structure"}B= $self->{"_reset_structure"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | my $table = $self->{table} || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | return( $self->error( "No table provided to get its structure." ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'table'} || (return $self->error('No table provided to get its structure.'))
MC/DC0%0/1
$self->{'table'} || (return $self->error('No table provided to get its structure.'))
$self->{'table'}
Truth table0%0/2
A
A= $self->{'table'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | my $dbo = $self->database_object || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->database_object || (return $self->error('No database handle could be found.'))
MC/DC0%0/1
$self->database_object || (return $self->error('No database handle could be found.'))
$self->database_object
Truth table0%0/2
A
A= $self->database_object
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | return( $self->error( "No database handle could be found." ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | my $sth1 = $self->prepare_cached( "SELECT * FROM sqlite_master WHERE name = ?" ) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->prepare_cached('SELECT * FROM sqlite_master WHERE name = ?') || (return $self->error(qq[An error occured while preparing the sql query to get the details of table "$table": ], $self->errstr))
MC/DC0%0/1
$self->prepare_cached('SELECT * FROM sqlite_master WHERE name = ?') || (return $self->error(qq[An error occured while preparing the sql query to get the details of table "$table": ], $self->errstr))
$self->prepare_cached('SELECT * FROM sqlite_master WHERE name = ?')
Truth table0%0/2
A
A= $self->prepare_cached('SELECT * FROM sqlite_master WHERE name = ?')
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | return( $self->error( "An error occured while preparing the sql query to get the details of table \"$table\": ", $self->errstr() ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | $sth1->execute( $table ) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | return( $self->error( "An error occured while executing the sql query to get the details of table \"$table\": ", $sth1->errstr ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | my $def = $sth1->fetchrow_hashref; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | $sth1->finish; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | # table or view | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | $self->{type} = $def->{type}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | my $struct = $self->{structure}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | my $fields = $self->{fields}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | my $types_dict = $dbo->datatype_dict; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | ▶ | $self->_load_class( 'DB::Object::Fields::Field' ) || return( $self->pass_error ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | my $q = $self->_reset_query; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | # .header on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | # PRAGMA table_info('dummy'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | # cid|name|type|notnull|dflt_value|pk | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | my $query = "PRAGMA table_info(${table})"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | # <https://www.sqlite.org/pragma.html> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | my $sth = $dbo->prepare_cached( $query ) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$dbo->prepare_cached($query) || (return $self->error("Error while preparing query to get table '${table}' columns specification: ", $self->errstr))
MC/DC0%0/1
$dbo->prepare_cached($query) || (return $self->error("Error while preparing query to get table '${table}' columns specification: ", $self->errstr))
$dbo->prepare_cached($query)
Truth table0%0/2
A
A= $dbo->prepare_cached($query)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | return( $self->error( "Error while preparing query to get table '$table' columns specification: ", $self->errstr() ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | $sth->execute || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | return( $self->error( "Error while executing query to get table '$table' columns specification: ", $sth->errstr() ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | my @primary = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | my $ref = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | my $c = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | # Mysql: field, type, null, key, default, extra | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | # Postgres: tablename, field, field_num, type, len, comment, is_nullable, key, foreign_key, default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | # SQLite: cid, name, type, notnull, dflt_value, pk | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | while( $ref = $sth->fetchrow_hashref() ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | my $def = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | name => $ref->{name}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | default => $ref->{dflt_value}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | is_nullable => ( $ref->{notnull} ? 0 : 1 ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | is_primary => ( $ref->{is_primary} ? 1 : 0 ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | pos => ++$c, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | #query_object => $q, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | size => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | type => $ref->{type}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | #table_object => $self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | ▶ | if( $def->{type} =~ /^([^\(]+)\(([^\)]+)\)$/ ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | @$def{qw( type size )} = ( $1, $2 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | my( $const_def, $dict ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | ▶ | if( CORE::exists( $types_dict->{ $def->{type} } ) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | $const_def = $types_dict->{ $def->{type} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | # Get the constant | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | DATA_TYPE_RE: foreach my $type ( keys( %$types_dict ) ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | if( $def->{type} =~ /$types_dict->{ $type }->{re}/i ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | $const_def = $types_dict->{ $type }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | last DATA_TYPE_RE; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | ▶ | if( defined( $const_def ) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | my $const_keys = [keys( %$const_def )]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | my $dict = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | @$dict{ @$const_keys } = @$const_def{ @$const_keys }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | $def->{datatype} = $dict; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | $def->{query_object} = $q; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | $def->{table_object} = $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | my @define = ( $def->{type} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | ▶ | push( @define, "DEFAULT '$def->{default}'" ) if( defined( $def->{default} ) && length( $def->{default} // '' ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
defined $def->{"default"} and length($def->{"default"} // "")
$def->{"default"} // ""
MC/DC0%0/2
defined $def->{"default"} and length($def->{"default"} // "")
defined $def->{"default"}
$def->{"default"}
Truth table0%0/3
A and B
A= defined $def->{"default"}B= $def->{"default"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | ▶ | push( @define, "NOT NULL" ) if( $ref->{notnull} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | ▶ | push( @primary, $def->{name} ) if( $ref->{pk} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | $struct->{ $def->{name} } = CORE::join( ' ', @define ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | ▶ | my $field = DB::Object::Fields::Field->new( %$def, debug => $self->debug ) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
'DB::Object::Fields::Field'->new(%$def, 'debug', $self->debug) || (return $self->pass_error('DB::Object::Fields::Field'->error))
MC/DC0%0/1
'DB::Object::Fields::Field'->new(%$def, 'debug', $self->debug) || (return $self->pass_error('DB::Object::Fields::Field'->error))
'DB::Object::Fields::Field'->new(%$def, 'debug', $self->debug)
Truth table0%0/2
A
A= 'DB::Object::Fields::Field'->new(%$def, 'debug', $self->debug)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | return( $self->pass_error( DB::Object::Fields::Field->error ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | $fields->{ $def->{name} } = $field; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | $sth->finish; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | ▶ | if( @primary ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | $self->{primary} = \@primary; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | $self->{fields} = $fields; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | $self->{_cache_structure} = $struct; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | return( $self->_clone( $struct ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | ▶ | sub table_info { return( shift->database_object->tables_info( @_ ) ); } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
table_info: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | sub unlock { return( shift->error( "Locking and unlocking of tables is unsupportde in SQLite." ) ); } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unlock: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | # NOTE: sub _simple_exist is inherited from DB::Object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | # sub _simple_exist | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | # NOTE: DESTROY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | DESTROY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | # Do nothing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | # DB::Object::Tables are never destroyed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | # They are just gateway to tables, and they are cached by DB::Object::table() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | # print( STDERR "DESTROY'ing table $self ($self->{table})\n" ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | # NOTE: POD | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||