Examples to explain the definition of database overloaded functions

This article is shared from the Huawei Cloud Community " GaussDB Database SQL Series - Defining Overloaded Functions " by Gauss Squirrel Club Assistant 2.

I. Introduction

In this article, we will introduce the concept, usage, and examples of user-defined function overloading in the GaussDB database. User-defined functions are commonly used "programming tools" in SQL that allow us to customize functions to process and manipulate data. Function overloading refers to defining multiple functions with the same name but different parameters in a database to achieve different functions.

2. Definition of function overloading

Function overloading is a feature that allows multiple functions with the same name to be defined in the same database. These functions with the same name differ in parameter type, number, or order, so different functions can be called based on the parameters passed in. Through function overloading, we can use the same function name to implement multiple functions, improving the readability and reusability of the code.

3. Instructions for creating custom overloaded functions in GaussDB

• PostgreSQL-compatible functions or functions with the PACKAGE attribute support overloading. When specifying REPLACE, if the number of parameters, type, and return value change, the original function will not be replaced, but a new function will be created.

• You cannot create overloaded functions that differ only in formal parameter names (the function name and parameter list type are the same).

• Formal parameters are not supported only in overloads where the custom ref cursor type is different from the sys_refcursor type.

• Function overloads that differ only in the data type they return are not supported.

• Function overloads that differ only in default values ​​are not supported.

• The variable needs to have a clear specific type when calling an overloaded function.

• PACKAGE indicates whether the function supports overloading.

• Overloading or replacement of package functions and non-package functions is not allowed.

• The package function does not support VARIADIC type parameters.

• Modification of the function's package attribute is not allowed.

4. Example of custom overloaded function in GaussDB database

Using function overloading, we can choose different functions to implement different functions according to specific situations, making the code more concise and readable.

Example 1: Create a package attribute overload function to obtain generated views based on different SQL conditions

1. Obtain employee information based on employee name (parameter)

--根据员工姓名(参数)获取员工信息

CREATE OR REPLACE FUNCTION f_test1_overload(c_name VARCHAR)

RETURNS VOID PACKAGE

LANGUAGE plpgsql

AS $$

DECLARE

s_sql TEXT;

BEGIN

s_sql := 'CREATE OR REPLACE VIEW v_company1 AS SELECT * FROM company1 where name=''' || c_name || '''' ;

EXECUTE s_sql;

END $$;

--调用

select f_test1_overload('Allen');

--查看执行结果

select * from v_company1;

Call result:

cke_131.png

2. Obtain employee information based on employee name or employee age

--根据员工姓名或员工年龄获取员工信息

CREATE OR REPLACE FUNCTION f_test1_overload(c_name VARCHAR,c_age INT)

RETURNS VOID PACKAGE

LANGUAGE plpgsql

AS $$

DECLARE

s_sql TEXT;

BEGIN

s_sql := 'CREATE OR REPLACE VIEW v_company2 AS SELECT * FROM company1 where name=''' || c_name || ''' OR age >= ''' || c_age || '''' ;

EXECUTE s_sql;

END $$;

--调用

select f_test1_overload('Allen',25);

--查看执行结果

select * from v_company2;

Call result:

cke_132.png

Example 2: Create a package attribute overload function to calculate the graphic area based on different parameters.

1. Find the area based on length and width

-- Find the area based on length and width

CREATE OR REPLACE FUNCTION f_test2_overload(length INT, width INT)

RETURNS INT PACKAGE

LANGUAGE plpgsql

AS $$

BEGIN

RETURN length * width;

END $$;

--调用

SELECT f_test2_overload(5,10)

Call result:

cke_133.png

2. Find the area S=πr squared based on the radius.

--根据半径求面积S=πr平方

CREATE OR REPLACE FUNCTION f_test2_overload(radius INT)

RETURNS FLOAT PACKAGE

LANGUAGE plpgsql

AS $$

BEGIN

RETURN 3.14 * radius * radius;

END $$;

--调用

SELECT f_test2_overload(10)

Call result:

cke_134.png

Special Note:

It should be noted that it is not necessary to implement custom overloaded functions in the database, because the database system usually already provides a rich set of built-in functions and operators that can meet most data processing needs. If you need to implement special data processing logic, you can first consider using built-in functions and operators. If the requirements cannot be met, then consider custom overloaded functions.

5. Summary

In the GaussDB database, function overloading is an important feature, allowing us to define multiple functions with the same name in the same database and call different functions based on different parameters. Through function overloading, we can improve the readability and reusability of the code and achieve unified management of multiple functions under one function name. Whether it is based on differences in parameter type, number, or order, function overloading provides us with more flexibility and selectivity.

When applying function overloading, we should be careful to avoid defining too many overloaded functions that may lead to confusion and redundancy. Reasonable use of function overloading can improve the maintainability and scalability of the code, making database applications more efficient and reliable.

Click to follow and learn about Huawei Cloud’s new technologies as soon as possible~

Alibaba Cloud suffered a serious failure and all products were affected (restored). Tumblr cooled down the Russian operating system Aurora OS 5.0. New UI unveiled Delphi 12 & C++ Builder 12, RAD Studio 12. Many Internet companies urgently recruit Hongmeng programmers. UNIX time is about to enter the 1.7 billion era (already entered). Meituan recruits troops and plans to develop the Hongmeng system App. Amazon develops a Linux-based operating system to get rid of Android's dependence on .NET 8 on Linux. The independent size is reduced by 50%. FFmpeg 6.1 "Heaviside" is released
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4526289/blog/10143249