| 1 | # -*- perl -*- | ||
| 2 | ##---------------------------------------------------------------------------- | ||
| 3 | ## Database Object Interface - ~/lib/DB/Object/SQLite/Statement.pm | ||
| 4 | ## Version v0.301.0 | ||
| 5 | ## Copyright(c) 2020 DEGUEST Pte. Ltd. | ||
| 6 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 7 | ## Created 2017/07/19 | ||
| 8 | ## Modified 2026/03/22 | ||
| 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 automatically terminate the statement object and | ||
| 15 | # separate them from the connection object (DB::Object). | ||
| 16 | # Connection object last longer than statement objects | ||
| 17 | ##---------------------------------------------------------------------------- | ||
| 18 | package DB::Object::SQLite::Statement; | ||
| 19 | BEGIN | ||
| 20 | { | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | warnings::register_categories( 'DB::Object' ); | ||
| 24 | use parent qw( DB::Object::Statement ); | ||
| 25 | use vars qw( $VERSION $DEBUG ); | ||
| 26 | our $DEBUG = 0; | ||
| 27 | our $VERSION = 'v0.301.0'; | ||
| 28 | }; | ||
| 29 | |||
| 30 | use strict; | ||
| 31 | use warnings; | ||
| 32 | |||
| 33 | # Inherited from DB::Object::Statement | ||
| 34 | # sub bind_param | ||
| 35 | |||
| 36 | # sub commit is called by dbh, so it is in DB::Object::Postgres | ||
| 37 | |||
| 38 | # Customised for Postgres | ||
| 39 | sub distinct | ||
| 40 | { | ||
| 41 | my $self = shift( @_ ); | ||
| 42 | my $what = shift( @_ ); | ||
| 43 | my $query = $self->{query} || | ||
| 44 | return( $self->error( "No query to set as to be ignored." ) ); | ||
| 45 | |||
| 46 | my $type = uc( ( $query =~ /^\s*(\S+)\s+/ )[ 0 ] ); | ||
| 47 | # ALTER for table alteration statements (DB::Object::Tables | ||
| 48 | my @allowed = qw( SELECT ); | ||
| 49 | my $allowed = CORE::join( '|', @allowed ); | ||
| 50 | if( !scalar( grep{ /^$type$/i } @allowed ) ) | ||
| 51 | { | ||
| 52 | return( $self->error( "You may not flag statement of type \U$type\E to be distinct:\n$query" ) ); | ||
| 53 | } | ||
| 54 | # Incompatible. Do not bother going further | ||
| 55 | return( $self ) if( $query =~ /^[[:blank:]]*(?:$allowed)[[:blank:]]+(?:DISTINCT|ALL)[[:blank:]]+/i ); | ||
| 56 | my $clause = defined( $what ) ? "DISTINCT ON ($what)" : "DISTINCT"; | ||
| 57 | |||
| 58 | $query =~ s/^([[:blank:]]*)($allowed)([[blank:]]+)/$1$2 $clause /; | ||
| 59 | # my $sth = $self->prepare( $query ) || | ||
| 60 | # $self->{ 'query' } = $query; | ||
| 61 | # saving parameters to bind later on must have been done previously | ||
| 62 | my $sth = $self->_cache_this( $query ) || | ||
| 63 | return( $self->error( "Error while preparing new ignored query:\n$query" ) ); | ||
| 64 | if( !defined( wantarray() ) ) | ||
| 65 | { | ||
| 66 | $sth->execute() || | ||
| 67 | return( $self->error( "Error while executing new ignored query:\n$query" ) ); | ||
| 68 | } | ||
| 69 | return( $sth ); | ||
| 70 | } | ||
| 71 | |||
| 72 | # Inherited from DB::Object | ||
| 73 | # sub dump | ||
| 74 | |||
| 75 | # Inherited from DB::Object::Statement | ||
| 76 | # sub execute | ||
| 77 | |||
| 78 | # Inherited from DB::Object::Statement | ||
| 79 | # sub executed | ||
| 80 | |||
| 81 | # Inherited from DB::Object::Statement | ||
| 82 | # sub fetchall_arrayref($@) | ||
| 83 | |||
| 84 | # Inherited from DB::Object::Statement | ||
| 85 | # sub fetchcol($;$) | ||
| 86 | |||
| 87 | # Inherited from DB::Object::Statement | ||
| 88 | # sub fetchhash(@) | ||
| 89 | |||
| 90 | # Inherited from DB::Object::Statement | ||
| 91 | # sub fetchrow(@) | ||
| 92 | |||
| 93 | # Inherited from DB::Object::Statement | ||
| 94 | # sub finish | ||
| 95 | |||
| 96 | # https://sqlite.org/lang_insert.html | ||
| 97 | # https://sqlite.org/lang_update.html | ||
| 98 | sub ignore | ||
| 99 | { | ||
| 100 | my $self = shift( @_ ); | ||
| 101 | my $query = $self->{query} || | ||
| 102 | return( $self->error( "No query to set as to be ignored." ) ); | ||
| 103 | |||
| 104 | my $type = uc( ( $query =~ /^[[:blank:]]*(\S+)[[:blank:]]+/ )[ 0 ] ); | ||
| 105 | # ALTER for table alteration statements (DB::Object::Tables | ||
| 106 | my @allowed = qw( INSERT UPDATE ); | ||
| 107 | my $allowed = CORE::join( '|', @allowed ); | ||
| 108 | if( !scalar( grep{ /^$type$/i } @allowed ) ) | ||
| 109 | { | ||
| 110 | return( $self->error( "You may not flag statement of type \U$type\E to be ignored:\n$query" ) ); | ||
| 111 | } | ||
| 112 | # Already done. Do not bother going further | ||
| 113 | return( $self ) if( $query =~ /^[[:blank:]]*(?:$allowed)[[:blank:]]+OR[[:blank:]]+IGNORE[[:blank:]]+/i ); | ||
| 114 | |||
| 115 | $query =~ s/^([[:blank:]]*)($allowed)([[:blank:]]+)/$1$2 OR IGNORE$3/; | ||
| 116 | my $sth = $self->_cache_this( $query ) || | ||
| 117 | return( $self->error( "Error while preparing new ignored query:\n$query" ) ); | ||
| 118 | if( !defined( wantarray() ) ) | ||
| 119 | { | ||
| 120 | $sth->execute() || | ||
| 121 | return( $self->error( "Error while executing new ignored query:\n$query" ) ); | ||
| 122 | } | ||
| 123 | return( $sth ); | ||
| 124 | } | ||
| 125 | |||
| 126 | # Inherited from DB::Object::Statement | ||
| 127 | # sub join | ||
| 128 | |||
| 129 | # Inherited from DB::Object::Statement | ||
| 130 | # sub object | ||
| 131 | |||
| 132 | sub only | ||
| 133 | { | ||
| 134 | return( shift->error( "SELECT | DELETE | UPDATE ONLY is not supported by SQLite." ) ); | ||
| 135 | } | ||
| 136 | |||
| 137 | sub priority | ||
| 138 | { | ||
| 139 | return( shift->error( "Priority is not supported in SQLite." ) ); | ||
| 140 | } | ||
| 141 | |||
| 142 | # rollback is called using the dbh handler and is located in DB::Object::Postgres | ||
| 143 | |||
| 144 | # Inherited from DB::Object::Statement | ||
| 145 | # sub rows(@) | ||
| 146 | |||
| 147 | # Inherited from DB::Object::Statement | ||
| 148 | # sub undo | ||
| 149 | |||
| 150 | sub unprepared_statements | ||
| 151 | { | ||
| 152 | my $self = shift( @_ ); | ||
| 153 | if( $self->{dbh}->{sth} ) | ||
| 154 | { | ||
| 155 | return( $self->{dbh}->{sth}->{sqlite_unprepared_statements} ); | ||
| 156 | } | ||
| 157 | return; | ||
| 158 | } | ||
| 159 | |||
| 160 | # Does nothing in SQLite. This is a Mysql feature | ||
| 161 | sub wait | ||
| 162 | { | ||
| 163 | return( shift( @_ ) ); | ||
| 164 | } | ||
| 165 | |||
| 166 | # NOTE: DESTROY | ||
| 167 | DESTROY | ||
| 168 | { | ||
| 169 | # Do nothing but existing so it is handled by this package | ||
| 170 | # print( STDERR "DESTROY'ing statement $self ($self->{ 'query' })\n" ); | ||
| 171 | }; | ||
| 172 | |||
| 173 | 1; | ||
| 174 | # NOTE: POD | ||
| 175 | __END__ |