Skip to content

get_path_segment() and is_path_matching() sqlpage functions#1266

Merged
lovasoa merged 3 commits into
sqlpage:mainfrom
olivierauverlot:get_path_segment
Apr 11, 2026
Merged

get_path_segment() and is_path_matching() sqlpage functions#1266
lovasoa merged 3 commits into
sqlpage:mainfrom
olivierauverlot:get_path_segment

Conversation

@olivierauverlot

Copy link
Copy Markdown
Contributor

Two new useful functions for building RESTful APIs. They avoid using SQL queries to extract an element from a URL and to check whether a URL matches a pattern. The endpoint code is more readable and more performant. The method is the same regardless of the database.

@lovasoa lovasoa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks! Do you have an example of how you would use these functions?

Maybe a regex matching function would be more generally useful, for both matching and extracting components?

@olivierauverlot

olivierauverlot commented Apr 4, 2026

Copy link
Copy Markdown
Contributor Author

A fake example of how to use these functions. The following code is placed in the /api/v1/users/404.sql file. It allows to manage 3 endpoints.

-- Returns user information (lookup based on UID)
-- GET /api/v1/users/42
SELECT 
    'json' AS component, 
    JSON_OBJECT(
        'name', name,
        'surname', surname
    ) AS contents
FROM users
WHERE   sqlppage.is_path_matching(sqlpage.path(),'/api/v1/users/%') <> ''
   AND  id = sqlpage.get_path_segment(sqlpage.path(),4);

-- Returns all accounts of a user (lookup based on UID)
-- GET /api/v1/users/42/accounts
SELECT
    'json' AS component,
    JSON_OBJECT(
        'uid', sqlpage.get_path_segment(sqlpage.path(),4),
        'logins', (
            SELECT JSON_GROUP_ARRAY(
                JSON_OBJECT(
                    'group', group,
                    'homedir', homedir
                )
            ) FROM accounts
            WHERE accounts.uid = sqlpage.get_path_segment(sqlpage.path(),4)
        )
    ) AS contents
WHERE sqlpage.is_path_matching(sqlpage.path(),'/api/v1/users/%/accounts')  <> '';

-- Returns detailed account information (lookup based on UID and login)
-- GET /api/v1/users/42/accounts/jdoe
SELECT
    'json' AS component,
    JSON_OBJECT(
        'uid', uid,
        'group', group
        'homedir', homedir
    ) AS contents
FROM
    accounts
WHERE  sqlppage.is_path_matching(sqlpage.path(),'/api/v1/users/%/accounts/%')  <> ''
  AND  accounts.uid = sqlpage.get_path_segment(sqlpage.path(),4)
  AND  accounts.login = sqlpage.get_path_segment(sqlpage.path(),6);

…gment that is an integer. If a segment in the pattern is ''%s'', it will match any non-empty segment in the path
@olivierauverlot

olivierauverlot commented Apr 6, 2026

Copy link
Copy Markdown
Contributor Author
-- Returns detailed account information (lookup based on UID and login)
-- GET /api/v1/users/42/accounts/jdoe
SELECT
    'json' AS component,
    JSON_OBJECT(
        'uid', uid,
        'group', group
        'homedir', homedir
    ) AS contents
FROM
    accounts
WHERE  sqlppage.is_path_matching(sqlpage.path(),'/api/v1/users/%d/accounts/%s')  <> ''
  AND  accounts.uid = sqlpage.get_path_segment(sqlpage.path(),4)
  AND  accounts.login = sqlpage.get_path_segment(sqlpage.path(),6);

@lovasoa

lovasoa commented Apr 7, 2026

Copy link
Copy Markdown
Collaborator

@olivierauverlot I opened #1271 as a more general alternative. What do you think ?

@lovasoa

lovasoa commented Apr 8, 2026

Copy link
Copy Markdown
Collaborator

@olivierauverlot , I just merged sqlpage.rgex_match(pattern, text). Can you test it and report whether it fully covers your use case ?

@olivierauverlot

Copy link
Copy Markdown
Contributor Author

It looks a good solution but I'm affraid that the use of regex could frighten some users. A good document with examples will be requiered. I will test during this weekend.

@olivierauverlot

Copy link
Copy Markdown
Contributor Author

Just tested. That seems ok for me :) It's a very nice and important feature.

@lovasoa lovasoa merged commit a3f91eb into sqlpage:main Apr 11, 2026
14 checks passed
lovasoa added a commit that referenced this pull request Apr 11, 2026
lovasoa added a commit that referenced this pull request Apr 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants