-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Evan Stanton <[email protected]>
- Loading branch information
1 parent
2179488
commit f092d1d
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# What is this extension? | ||
>*Transform boolean type <> PL/Perl.* | ||
`bool_plperl` is a C-based transformation extension that allows conversion between PostgreSQL- and Perl-native boolean values. | ||
|
||
# When should you use it? | ||
|
||
`bool_plperl` should be considered when writing or using PL/Perl functions within PostgreSQL that involve boolean values. This extension ensures that boolean values from PostgreSQL are properly interpreted in Perl and vice versa, eliminating potential logical errors. | ||
|
||
# Example use case. | ||
|
||
Library: | ||
|
||
A public library wants to check if a particular book is available for borrowing by means of a PL/Perl function. Due to the boolean value interpretation differences between PostgreSQL and Perl, they use the `bool_plperl` extension to make this process seamless. | ||
|
||
# Example test script. | ||
|
||
``` | ||
-- Sample table for library books | ||
CREATE TABLE library_books ( | ||
book_id serial PRIMARY KEY, | ||
title text, | ||
is_available bool | ||
); | ||
-- Populate the table with a sample book | ||
INSERT INTO library_books (title, is_available) VALUES | ||
('The Great Gatsby', true), | ||
('To Kill a Mockingbird', false), | ||
('Brothers Grimm Fairy Tales', true); | ||
-- Create a PL/Perl function to check book availability | ||
CREATE FUNCTION is_book_available(book_title text) RETURNS bool | ||
TRANSFORM FOR TYPE bool | ||
AS $$ | ||
my ($book_title) = @_; | ||
my $rv = spi_exec_query("SELECT is_available FROM library_books WHERE title = '$book_title'"); | ||
return $rv->{rows}[0]->{is_available}; | ||
$$ LANGUAGE plperl; | ||
-- Test the function | ||
SELECT title, is_book_available(title) as available_status FROM library_books WHERE title = 'The Great Gatsby'; | ||
title | available_status | ||
------------------+------------------ | ||
The Great Gatsby | t | ||
(1 row) | ||
``` |