Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostgreSQL/SQLite: Sympa tries creating temporary views in databases unnecessarily (#1812) #1813

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions src/lib/Sympa/DatabaseDriver/PostgreSQL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,11 @@ sub connect {

# - Configure Postgres to use ISO format dates.
# - Set client encoding to UTF8.
# - Create a temporary view "dual" for portable SQL statements.
# Note: utf8 flagging must be disabled so that we will consistently use
# UTF-8 bytestring as internal format.
# Note: PostgreSQL <= 8.0.x didn't support temporary view but >= 7.3.x
# supported CREATE OR REPLACE statement.
$self->__dbh->{pg_enable_utf8} = 0; # For DBD::Pg 3.x
$self->__dbh->do("SET DATESTYLE TO 'ISO';");
$self->__dbh->do("SET NAMES 'utf8'");
defined $self->__dbh->do(
q{CREATE TEMPORARY VIEW dual AS SELECT 'X'::varchar(1) AS dummy;})
or $self->__dbh->do(
q{CREATE OR REPLACE VIEW dual AS SELECT 'X'::varchar(1) AS dummy;});

return 1;
}
Expand Down
2 changes: 0 additions & 2 deletions src/lib/Sympa/DatabaseDriver/SQLite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ sub connect {
} else {
$self->__dbh->func(5000, 'busy_timeout');
}
# Create a temoprarhy view "dual" for portable SQL statements.
$self->__dbh->do(q{CREATE TEMPORARY VIEW dual AS SELECT 'X' AS dummy;});

# Create a function MD5().
$self->__dbh->func(
Expand Down
15 changes: 15 additions & 0 deletions src/lib/Sympa/DatabaseManager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ sub instance {
unless $self = Sympa::Database->new($db_conf->{'db_type'}, %$db_conf)
and $self->connect;

# Compatibility concern.
# - Create a temporary view "dual" for portable SQL statements.
if (ref $self eq 'Sympa::DatabaseDriver::PostgreSQL') {
# Note: PostgreSQL <= 8.0.x didn't support temporary view but >= 7.3.x
# supported CREATE OR REPLACE statement.
defined $self->__dbh->do(
q{CREATE TEMPORARY VIEW dual AS SELECT 'X'::varchar(1) AS dummy;})
or $self->__dbh->do(
q{CREATE OR REPLACE VIEW dual AS SELECT 'X'::varchar(1) AS dummy;}
);
} elsif (ref $self eq 'Sympa::DatabaseDriver::SQLite') {
$self->__dbh->do(
q{CREATE TEMPORARY VIEW dual AS SELECT 'X' AS dummy;});
}

# At once connection succeeded, we keep trying to connect.
# Unless in a web context, because we can't afford long response time on
# the web interface.
Expand Down
5 changes: 5 additions & 0 deletions t/Database_SQLite.t
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,10 @@ if (3035005 <= $DBD::SQLite::sqlite_version_number) {
qr/not support/, 'delete_field(unsupported)';
}

my $sth = $sdm->do_prepared_query(q{SELECT '', 0, NULL FROM dual});
my $rows = $sth->fetchall_arrayref if $sth;
is_deeply $rows, [['', 0, undef]], 'temporary view "dual" exists';
$sth->finish;

done_testing();

Loading