Skip to content

Package constants#8916

Open
Noremos wants to merge 23 commits intoFirebirdSQL:masterfrom
Noremos:metacache_package_constants
Open

Package constants#8916
Noremos wants to merge 23 commits intoFirebirdSQL:masterfrom
Noremos:metacache_package_constants

Conversation

@Noremos
Copy link
Contributor

@Noremos Noremos commented Feb 25, 2026

This PR adds a new database object - Package Constant (#1036). See README.packages.txt for more information.
Usage examples:

set autoterm;
CREATE PACKAGE MY_PACKAGE
AS
BEGIN
    CONSTANT PUBLIC_CONST INTEGER = 10;
    FUNCTION MY_SECRET_PRINT() RETURNS INT;
END;

CREATE PACKAGE BODY MY_PACKAGE
AS
BEGIN
    CONSTANT SECRET_1 INTEGER = 15;
    CONSTANT SECRET_2 INTEGER = 30;

    FUNCTION MY_SECRET_PRINT() RETURNS INT
    AS
    BEGIN
        RETURN SECRET_1 * SECRET_2;
    END
END;
commit;

select MY_PACKAGE.PUBLIC_CONST from rdb$database ;
select MY_PACKAGE.MY_SECRET_PRINT() from rdb$database;

The implementation has three key points:

  1. The package constants code is flexible enough to implement package variables, global constants, and additional package elements.
  2. All nodes now have an 'is constant' flag, similar to the 'is deterministic' flag. This is necessary to determine whether an expression can initialize a constant.
  3. In the CreateAlterPackageNode node, a lot of code had to be copied and pasted, and adding constants led to complete chaos. Therefore, I decided to slightly refactor this node. All functionality remains the same; only code duplication was removed.

@sim1984
Copy link
Contributor

sim1984 commented Feb 25, 2026

Are comments supported for constants? I didn't see this in the documentation.
Let me clarify what I mean:

COMMENT ON CONSTANT [<schema>.]<package>.<const_name> IS 'Text'

Comments are simply supported for packaged procedures and functions. It would make sense to do the same for constants (and any package objects, for that matter).

COMMENT ON PROCEDURE [<schema>.]<package>.<proc_name> IS 'Text';
COMMENT ON FUNCTION [<schema>.]<package>.<func_name> IS 'Text';

@Noremos
Copy link
Contributor Author

Noremos commented Feb 25, 2026

Are comments supported for constants?

No, I didn't plan to implement comments, but I think it's possible. I will take a look

@Noremos
Copy link
Contributor Author

Noremos commented Feb 25, 2026

Are comments supported for constants? I didn't see this in the documentation. Let me clarify what I mean:

COMMENT ON CONSTANT [<schema>.]<package>.<const_name> IS 'Text'

Ok, it wasn't that hard, I implemented the feature as suggested.

Examples:

SQL> comment on constant PUBLIC.MY_PACKAGE.PUBLIC_CONST is 'my description';
SQL> comment on constant MY_PACKAGE.SECRET_1 is 'The Magic Value!'; 

}

private:
CachedResource<Jrd::Constant, Jrd::RoutinePermanent> m_constant;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need adding Jrd:: in this case - we are already in Jrd namespace.

Copy link
Member

@AlexPeshkoff AlexPeshkoff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally OK, just minor correction please

if (ssDefiner.asBool())
invoker = dbb->getUserId(getPermanent()->owner);

makeValue(tdbb, attachment, CONST.RDB$CONSTANT_BLR);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure - is it OK to invoke makeValue() when MINISCAN is set? Certainly, constants should not have as complex dependencies as procedures/functions, but anyway - may be better leave it for reload in such a case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants