Back
Home
Up
Next |
|
New UDF library.
Information for creating UDF in C has been available for long time, although not as thoroughly as developers would want. With IB5, ISC decided to provide a set of functions that are often requested by developers, since IB itself has only a few embedded functions.
The functions, written in C, are available as a dynamic library in Windows and as shared library in UNIX. You should understand that UDFs don't get information about character sets and collations from the engine, so be careful when manipulating strings. There is a script, ib_udf.sql, in the examples subdirectory that declares all of the functions listed below. If you want to declare only a subset of these, copy and edit the script file.
This is the list of functions provided. The correct declaration is in the script that comes with IB. Here it's only shown how to invoke them from SQL sentences:
Usage |
Return type |
Purpose |
abs(double precision) |
double precision |
Absolute value |
acos(double precision) |
double precision |
Arccosine |
ascii_char(integer) |
cstring(1) |
#1 |
ASCII character of number |
ascii_val(char) |
integer |
ASCII number of character |
asin(double precision) |
double precision |
Arcsine |
atan(double precision) |
double precision |
Arctangent |
atan2(double precision A, double precision B) |
double precision |
Arctangent of A/B |
bin_and(integer, integer) |
integer |
Bit-wise AND |
bin_or(integer, integer) |
integer |
Bit-wise OR |
bin_xor(integer, integer) |
integer |
Bit-wise XOR |
ceiling(double precision A) |
double precision |
Smallest integer >= A |
cos(double precision) |
double precision |
Cosine |
cosh(double precision) |
double precision |
Hyperbolic cosine |
cot(double precision) |
double precision |
Cotangent |
div(integer, integer) |
double precision |
#2 |
Quotient of integer division |
floor(double precision A) |
double precision |
Largest integer <= A |
ln(double precision) |
double precision |
Natural log |
log(double precision A, double precision B) |
double precision |
Log base A of B |
log10(double precision) |
double precision |
Log base 10 |
lower(cstring) |
cstring |
#3 |
Lowercase |
ltrim(cstring) |
cstring |
Removes leading spaces |
mod(integer, integer) |
double precision |
Remainder of integer division |
pi() |
double precision |
Value of pi |
rand() |
double precision |
Random in [0..1] |
rtrim(cstring) |
cstring |
Removes trailing spaces |
sign(double precision) |
integer |
#4 |
1=positive 0=zero -1=negative |
sin(double precision) |
double precision |
Sine |
sinh(double precision) |
double precision |
Hyperbolic sine |
sqrt(double precision) |
double precision |
Square root |
strlen(cstring) |
integer |
#5 |
String length |
substr(cstring, integer m, integer n) |
double precision |
One-based slice [m,n] |
tan(double precision) |
double precision |
Tangent |
tanh(double precision) |
double precision |
Hyperbolic tangent |
Notes:
- It's ill-declared as returning char(1) instead of cstring(1) so it always produces a string truncation error. It was fixed the first time in Firebird in Dec-2000.
- Even though it returns a double, the result is the quotient of the integer division of the two operands.
- Use with care, since it only knows how to deal with ASCII characters; it won't handle correctly the case for letters outside the English alphabet.
- The result is zero if the argument is zero, otherwise the result is arg/abs(arg) for any representable number except NaN.
- This function is a nuisance: it returns the slice of the string argument starting at m and ending at the n, being one (and not zero) the first position in the string. Remember that n is the ending position, not the length. If n happens to be greater than the length of the string, NULL is returned instead of the string from m to the end.
|