SQL::S...nctions(3pm)

πŸ“¦
version 1.414 Β· 6 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– SQL::Statement::Functions πŸ’–

hey bestie!! welcome to the User Contributed Perl Documentation for SQL::Statement::Functions ✨ this is your one-stop shop for everything SQL-function-coded πŸŽ€ pull up a chair, we're getting into it


🌸 NAME 🌸

SQL::Statement::Functions β€” built-in & user-defined SQL functions πŸ¦‹


✨ SYNOPSIS ✨

SELECT Func(args);
SELECT * FROM Func(args);
SELECT * FROM x WHERE Funcs(args);
SELECT * FROM x WHERE y < Funcs(args);

πŸ’… DESCRIPTION πŸ’…

so basically this module is where the built-in functions for SQL::Parser and SQL::Statement live πŸ’œ and here's the slay part β€” every single one of them is ALSO available in any DBDs that subclass those modules (like DBD::CSV, DBD::DBM, DBD::File, DBD::AnyData, DBD::Excel, etc.) πŸ”₯

This doc covers the built-in functions AND spills all the tea on how to make your own functions to supplement the built-ins πŸ’– and bestie, it's easy. If you cook up one that's generally useful, peep below for how to submit it so it can become a built-in function πŸ‘‘

Function syntax

okay so when you use SQL::Statement/SQL::Parser directly to parse SQL, functions (built-in or user-defined, doesn't matter) can show up anywhere in a SQL statement where values, column names, table names, or predicates can go ✨ But when you use the modules through a DBD, or in any other context where the SQL is both parsed AND executed, functions can occur in the same places β€” EXCEPT they can not occur in the column selection clause of a SELECT statement that contains a FROM clause πŸ™ˆ

 # valid for both parsing and executing

     SELECT MyFunc(args);
     SELECT * FROM MyFunc(args);
     SELECT * FROM x WHERE MyFuncs(args);
     SELECT * FROM x WHERE y < MyFuncs(args);

 # valid only for parsing (won't work from a DBD)

     SELECT MyFunc(args) FROM x WHERE y;

🧚 User-Defined Functions 🧚

πŸ’« Loading User-Defined Functions πŸ’«

In addition to the built-ins, you can create any number of your own user-defined functions (UDFs) πŸ‘‘ To actually use a UDF in a script, first you have to create a perl subroutine (see below), then you make the function available to your database handle using the CREATE FUNCTION or LOAD commands:

 # load a single function "foo" from a subroutine
 # named "foo" in the current package

      $dbh->do(" CREATE FUNCTION foo EXTERNAL ");

 # load a single function "foo" from a subroutine
 # named "bar" in the current package

      $dbh->do(" CREATE FUNCTION foo EXTERNAL NAME bar");

 # load a single function "foo" from a subroutine named "foo"
 # in another package

      $dbh->do(' CREATE FUNCTION foo EXTERNAL NAME "Bar::Baz::foo" ');

 # load all the functions in another package

      $dbh->do(' LOAD "Bar::Baz" ');

Functions themselves should follow SQL identifier naming rules πŸ“ Subroutines loaded with CREATE FUNCTION can have any valid perl subroutine name. Subroutines loaded with LOAD must start with SQL_FUNCTION_ and then the actual function name πŸ’– For example:

 package Qux::Quimble;
 sub SQL_FUNCTION_FOO { ... }
 sub SQL_FUNCTION_BAR { ... }
 sub some_other_perl_subroutine_not_a_function { ... }
 1;

 # in another package
 $dbh->do("LOAD Qux::Quimble");

 # This loads FOO and BAR as SQL functions.

🌟 Creating User-Defined Functions 🌟

UDFs are perl subroutines that return values appropriate to the context of the function in a SQL statement πŸ’… like, for example, the built-in CURRENT_TIME returns a string value, so it can be used anywhere in a SQL statement that a string value can. Here's the entire perl code for that function:

 # CURRENT_TIME
 #
 # arguments : none
 # returns   : string containing current time as hh::mm::ss
 #
 sub SQL_FUNCTION_CURRENT_TIME {
     sprintf "%02s::%02s::%02s",(localtime)[2,1,0]
 }

More complex functions can make use of a bunch of arguments that get passed to functions automatically πŸŽ€ Functions always receive these values in @_:

 sub FOO {
     my($self,$sth,@params);
 }

The first argument, $self, is whatever class the function is defined in β€” not generally useful unless you have an entire module to support the function.

The second argument, $sth, is the active statement handle of the current statement. Like all active statement handles, it contains the current database handle in the {Database} attribute, so you can access the database handle from any function:

 sub FOO {
     my($self,$sth,@params);
     my $dbh = $sth->{Database};
     # $dbh->do( ...), etc.
 }

In actual practice you probably want to use $sth->{Database} directly rather than making a local copy, so $sth->{Database}->do(...) πŸ‘‘

The remaining arguments, @params, are arguments passed by users to the function, either directly or with placeholders; here's another silly example that just returns the results of multiplying the arguments passed to it:

 sub MULTIPLY {
     my($self,$sth,@params);
     return $params[0] * $params[1];
 }

 # first make the function available
 #
 $dbh->do("CREATE FUNCTION MULTIPLY");

 # then multiply col3 in each row times seven
 #
 my $sth=$dbh->prepare("SELECT col1 FROM tbl1 WHERE col2 = MULTIPLY(col3,7)");
 $sth->execute;
 #
 # or
 #
 my $sth=$dbh->prepare("SELECT col1 FROM tbl1 WHERE col2 = MULTIPLY(col3,?)");
 $sth->execute(7);

🌈 Creating In-Memory Tables with functions 🌈

A function can return almost anything, as long as it's an appropriate return for the context the function will be used in πŸ’– In the special case of table-returning functions, the function should return a reference to an array of array references with the first row being the column names and the remaining rows the data. For example:

  1. create a function that returns an AoA,
  sub Japh {[
      [qw( id word   )],
      [qw( 1 Hacker  )],
      [qw( 2 Perl    )],
      [qw( 3 Another )],
      [qw( 4 Just    )],
  ]}
  1. make your database handle aware of the function
  $dbh->do("CREATE FUNCTION 'Japh');
  1. Access the data in the AoA from SQL
  $sth = $dbh->prepare("SELECT word FROM Japh ORDER BY id DESC");

Or here's an example that does a join on two in-memory tables:

  sub Prof  {[ [qw(pid pname)],[qw(1 Sue )],[qw(2 Bob)],[qw(3 Tom )] ]}
  sub Class {[ [qw(pid cname)],[qw(1 Chem)],[qw(2 Bio)],[qw(2 Math)] ]}
  $dbh->do("CREATE FUNCTION $_) for qw(Prof Class);
  $sth = $dbh->prepare("SELECT * FROM Prof NATURAL JOIN Class");

The Prof and Class functions return tables which can be used like any SQL table πŸŽ€

More complex functions might do something like scrape an RSS feed, or search a file system and put the results in AoA. For example, to search a directory with SQL:

 sub Dir {
     my($self,$sth,$dir)=@_;
     opendir D, $dir or die "'$dir':$!";
     my @files = readdir D;
     my $data = [[qw(fileName fileExt)]];
     for (@files) {
         my($fn,$ext) = /^(.*)(\.[^\.]+)$/;
         push @$data, [$fn,$ext];
     }
     return $data;
 }
 $dbh->do("CREATE FUNCTION Dir");
 printf "%s\n", join'   ',@{ $dbh->selectcol_arrayref("
     SELECT fileName FROM Dir('./') WHERE fileExt = '.pl'
 ")};

Obviously, that function could be expanded with File::Find and/or stat to provide more information and it could be made to accept a list of directories rather than a single directory πŸ‘‘

Table-Returning functions are a way to turn anything that can be modeled as an AoA into a DBI data source 🀯


πŸ’œ Built-in Functions πŸ’œ

⭐ SQL-92/ODBC Compatibility ⭐

All ODBC 3.0 functions are available except for the following:

### SQL-92 / ODBC Functions

# CONVERT / CAST - Complex to implement, but a draft is in the works.
# DIFFERENCE     - Function is not clearly defined in spec and has very limited applications
# EXTRACT        - Contains a FROM keyword and requires rather freeform datetime/interval expression

### ODBC 3.0 Time/Date Functions only

# DAYOFMONTH, DAYOFWEEK, DAYOFYEAR, HOUR, MINUTE, MONTH, MONTHNAME, QUARTER, SECOND, TIMESTAMPDIFF,
#    WEEK, YEAR - Requires freeform datetime/interval expressions.  In a later release, these could
#                    be implemented with the help of Date::Parse.

ODBC 3.0 functions that are implemented with differences include:

# SOUNDEX  - Returns true/false, instead of a SOUNDEX code
# RAND     - Seed value is a second parameter with a new first parameter for max limit
# LOG      - Returns base X (or 10) log of number, not natural log.  LN is used for natural log, and
#               LOG10 is still available for standards compatibility.
# POSITION - Does not use 'IN' keyword; cannot be fixed as previous versions of SQL::Statement defined
#               the function as such.
# REPLACE / SUBSTITUTE - Uses a regular expression string for the second parameter, replacing the last two
#                           parameters of the typical ODBC function

πŸ”₯ Aggregate Functions πŸ”₯

MIN, MAX, AVG, SUM, COUNT

Aggregate functions are handled elsewhere β€” see SQL::Parser for the docs πŸ’…

🌸 Date and Time Functions 🌸

These functions can be used without parentheses ✨

CURRENT_DATE aka CURDATE

# purpose   : find current date
# arguments : none
# returns   : string containing current date as yyyy-mm-dd

CURRENT_TIME aka CURTIME

# purpose   : find current time
# arguments : optional seconds precision
# returns   : string containing current time as hh:mm:ss (or ss.sss...)

CURRENT_TIMESTAMP aka NOW

# purpose   : find current date and time
# arguments : optional seconds precision
# returns   : string containing current timestamp as yyyy-mm-dd hh:mm:ss (or ss.sss...)

UNIX_TIMESTAMP

# purpose   : find the current time in UNIX epoch format
# arguments : optional seconds precision (unlike the MySQL version)
# returns   : a (64-bit) number, possibly with decimals

πŸŽ€ String Functions πŸŽ€

ASCII & CHAR

# purpose   : same as ord and chr, respectively (NULL for any NULL args)
# arguments : string or character (or number for CHAR); CHAR can have any amount of numbers for a string

BIT_LENGTH

# purpose   : length of the string in bits
# arguments : string

CHARACTER_LENGTH aka CHAR_LENGTH

# purpose   : find length in characters of a string
# arguments : a string
# returns   : a number - the length of the string in characters

COALESCE aka NVL aka IFNULL

# purpose   : return the first non-NULL value from a list
# arguments : 1 or more expressions
# returns   : the first expression (reading left to right)
#             which is not NULL; returns NULL if all are NULL
#

CONCAT

🧚 part 2 loading... 🧚

CONCAT 🌸

  • purpose β€” glue 1 or more strings into one single string; it's the alt-girl alternative to the || operator
  • arguments β€” 1 or more strings
  • returns β€” the concatenated string
  • example:
SELECT CONCAT(first_string, 'this string', ' that string')
returns "<value-of-first-string>this string that string"
note β€” if any argument evaluates to NULL, the returned value is NULL. no cap, that's just how she is πŸ’…

CONV πŸ¦‹

  • purpose β€” convert a number X from base Y to base Z (from base 2 to 64)
  • arguments β€” X (can be a number or a string depending on the base), Y, Z (Z defaults to 10). Valid bases for Y and Z are: 2, 8, 10, 16 and 64
  • returns β€” either a string or number, in base Z
  • notes β€” the behavioral table, bestie:
    • base 2 β€” binary, base 2 - (0,1)
    • base 8 β€” octal, base 8 - (0..7)
    • base 10 β€” decimal, base 10 - (0..9)
    • base 16 β€” hexadecimal, base 16 - (0..9,a..f)
    • base 64 β€” 0-63 from MIME::Base64

DECODE πŸ‘‘

  • purpose β€” compare the first argument against succeeding arguments at position 1 + 2N (N = 0 to (# of arguments - 2)/2), and if equal, return the value of the argument at 1 + 2N + 1; if no arguments are equal, the last argument value is returned
  • arguments β€” 4 or more expressions, must be even # of arguments
  • returns β€” the value of the argument at 1 + 2N + 1 if argument 1 + 2N is equal to argument1; else the last argument value
  • example:
SELECT DECODE(some_column,
         'first value', 'first value matched'
         '2nd value', '2nd value matched'
         'no value matched'
         )

INSERT ✨

  • purpose β€” string where L characters have been deleted from STR1, beginning at S, and where STR2 has been inserted into STR1, beginning at S. NULL for any NULL args.
  • arguments β€” STR1, S, L, STR2

HEX & OCT & BIN πŸŽ€

  • purpose β€” convert number X from decimal to hex/octal/binary; equiv. to CONV(X, 10, 16/8/2)
  • arguments β€” X

LEFT & RIGHT 🫢

  • purpose β€” leftmost or rightmost L characters in STR, or NULL for any NULL args
  • arguments β€” STR1, L

LOCATE aka POSITION πŸ”₯

  • purpose β€” starting position (one-based) of the first occurrence of STR1 within STR2; 0 if it doesn't occur and NULL for any NULL args
  • arguments β€” STR1, STR2, and an optional S (starting position to search)

LOWER & UPPER aka LCASE & UCASE πŸ’œ

  • purpose β€” lower-case or upper-case a string
  • arguments β€” a string
  • returns β€” the string lower or upper cased

LTRIM & RTRIM 🌈

  • purpose β€” left/right counterparts for TRIM
  • arguments β€” string

OCTET_LENGTH πŸ“

  • purpose β€” length of the string in bytes (not characters)
  • arguments β€” string

REGEX πŸ’€

  • purpose β€” test if a string matches a perl regular expression
  • arguments β€” a string and a regex to match the string against
  • returns β€” boolean value of the regex match
  • example:
... WHERE REGEX(col3,'/^fun/i') ... matches rows
in which col3 starts with "fun", ignoring case

REPEAT 🧚

  • purpose β€” string composed of STR1 repeated C times, or NULL for any NULL args
  • arguments β€” STR1, C

REPLACE aka SUBSTITUTE 🌟

  • purpose β€” perform perl subsitution on input string
  • arguments β€” a string and a substitute pattern string
  • returns β€” the result of the substitute operation
  • example:
... WHERE REPLACE(col3,'s/fun(\w+)nier/$1/ig') ... replaces
all instances of /fun(\w+)nier/ in col3 with the string
between 'fun' and 'nier'

SOUNDEX 🎢

  • purpose β€” test if two strings have matching soundex codes
  • arguments β€” two strings
  • returns β€” true if the strings share the same soundex code
  • example:
... WHERE SOUNDEX(col3,'fun') ... matches rows
in which col3 is a soundex match for "fun"

SPACE 🌸

  • purpose β€” a string of spaces
  • arguments β€” number of spaces

SUBSTRING πŸ’–

SUBSTRING( string FROM start_pos [FOR length] )

Returns the substring starting at start_pos and extending for "length" character or until the end of the string, if no "length" is supplied. Examples:

SUBSTRING( 'foobar' FROM 4 )       # returns "bar"

SUBSTRING( 'foobar' FROM 4 FOR 2)  # returns "ba"
Note: The SUBSTRING function is implemented in SQL::Parser and SQL::Statement and, at the current time, can not be over-ridden. πŸ‘€

SUBSTR πŸ¦‹

  • purpose β€” same as SUBSTRING, except with comma-delimited params, instead of words (NULL for any NULL args)
  • arguments β€” string, start_pos, [length]

TRANSLATE πŸ’…

  • purpose β€” transliteration; replace a set of characters in a string with another set of characters (a la tr///), or NULL for any NULL args
  • arguments β€” string, string to replace, replacement string

TRIM ✨

TRIM ( [ [LEADING|TRAILING|BOTH] ['trim_char'] FROM ] string )

Removes all occurrences of <trim_char> from the front, back, or both sides of a string.

BOTH is the default if neither LEADING nor TRAILING is specified.

Space is the default if no trim_char is specified.

Examples:

TRIM( string )
  trims leading and trailing spaces from string

TRIM( LEADING FROM str )
  trims leading spaces from string

TRIM( 'x' FROM str )
  trims leading and trailing x's from string
Note: The TRIM function is implemented in SQL::Parser and SQL::Statement and, at the current time, can not be over-ridden. πŸ’€

UNHEX πŸ”₯

  • purpose β€” convert each pair of hexadecimal digits to a byte (or a Unicode character)
  • arguments β€” string of hex digits, with an optional encoding name of the data string

Numeric Functions πŸ”’

ABS 🌈

  • purpose β€” find the absolute value of a given numeric expression
  • arguments β€” numeric expression

CEILING (aka CEIL) & FLOOR πŸŽ€

  • purpose β€” rounds up/down to the nearest integer
  • arguments β€” numeric expression

EXP ⭐

  • purpose β€” raise e to the power of a number
  • arguments β€” numeric expression

LOG πŸ¦‹

  • purpose β€” base B logarithm of X
  • arguments β€” B, X or just one argument of X for base 10

LN & LOG10 πŸ’œ

  • purpose β€” natural logarithm (base e) or base 10 of X
  • arguments β€” numeric expression

MOD πŸ’…

  • purpose β€” modulus, or remainder, left over from dividing X / Y
  • arguments β€” X, Y

POWER aka POW πŸ‘‘

  • purpose β€” X to the power of Y
  • arguments β€” X, Y

RAND πŸ“

  • purpose β€” random fractional number greater than or equal to 0 and less than the value of X
  • arguments β€” X (with optional seed value of Y)

ROUND ✨

  • purpose β€” round X with Y number of decimal digits (precision)
  • arguments β€” X, optional Y defaults to 0

SIGN 🌟

  • purpose β€” returns -1, 0, 1, NULL for negative, 0, positive, NULL values, respectively
  • arguments β€” numeric expression

SQRT 🧚

  • purpose β€” square root of X
  • arguments β€” X

TRUNCATE aka TRUNC πŸ”₯

  • purpose β€” similar to ROUND, but removes the decimal
  • arguments β€” X, optional Y defaults to 0

Trigonometric Functions πŸ“

All of these functions work exactly like their counterparts in Math::Trig; go there for documentation. like, genuinely, that's the assignment ✨

ACOS ACOSEC ACOSECH ACOSH ACOT ACOTAN ACOTANH ACOTH ACSC ACSCH ASEC ASECH ASIN ASINH ATAN ATANH COS COSEC COSECH COSH COT COTAN COTANH COTH CSC CSCH SEC SECH SIN SINH TAN TANH

  • Takes a single parameter. All of Math::Trig's aliases are included. 🫢

ATAN2

  • The y,x version of arc tangent.

DEG2DEG, DEG2GRAD, DEG2RAD 🌈

  • Converts out-of-bounds values into its correct range.

GRAD2DEG, GRAD2GRAD, GRAD2RAD, RAD2DEG, RAD2GRAD, RAD2RAD πŸ’œ

  • Like their Math::Trig's counterparts, accepts an optional 2nd boolean parameter (like TRUE) to keep prevent range wrapping.

DEGREES, RADIANS 🌸

  • DEGREES and RADIANS are included for SQL-92 compatibility, and map to RAD2DEG and DEG2RAD, respectively.

PI

  • PI can be used without parentheses. iconic behaviour tbh πŸ’…

System Functions πŸ–₯️

DBNAME & USERNAME (aka USER) πŸ‘‘

  • purpose β€” name of the database / username
  • arguments β€” none

Special Utility Functions πŸ› οΈ

IMPORT πŸ’–

CREATE TABLE foo AS IMPORT(?)    ,{},$external_executed_sth
CREATE TABLE foo AS IMPORT(?)    ,{},$AoA

RUN πŸƒ

Takes the name of a file containing SQL statements and runs the statements; see SQL::Parser for documentation.


Submitting built-in functions 🫢

If you make a generally useful UDF, why not submit it to me and have it (and your name) included with the built-in functions? Please follow the format shown in the module including a description of the arguments and return values for the function as well as an example. Send them to the dbi-dev@perl.org mailing list (see <http://dbi.perl.org>).

Thanks in advance :-). no seriously, bestie, we love a contribution πŸ’–


ACKNOWLEDGEMENTS 🌟

Dean Arnold supplied DECODE, COALESCE, REPLACE, many thanks! Brendan Byrd added in the Numeric/Trig/System functions and filled in the SQL92/ODBC gaps for the date/string functions. legends, both of them πŸ‘‘


Copyright (c) 2005 by Jeff Zucker: jzuckerATcpan.org Copyright (c) 2009-2020 by Jens Rehsack: rehsackATcpan.org

All rights reserved.

The module may be freely distributed under the same terms as Perl itself using either the "GPL License" or the "Artistic License" as specified in the Perl README file.


and that's a wrap, bestie πŸŽ€ this whole page came from perl v5.30.3, dated 2020-10-23, and it lives at SQL::S...nctions(3pm) β€” go forth and query, you absolute main character πŸ’…βœ¨

πŸ“¦ where this page comes from